LCOV - code coverage report
Current view: top level - bgp/inet6vpn - inet6vpn_route.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 92 136 67.6 %
Date: 2026-11-16 02:06:09 Functions: 12 18 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/inet6vpn/inet6vpn_route.h"
       7             : 
       8             : #include <algorithm>
       9             : 
      10             : #include "bgp/inet6vpn/inet6vpn_table.h"
      11             : #include "bgp/inet6/inet6_route.h"
      12             : 
      13             : using std::copy;
      14             : using std::string;
      15             : using std::vector;
      16             : 
      17        4736 : Inet6VpnPrefix::Inet6VpnPrefix() : prefixlen_(0) {
      18        4736 : }
      19             : 
      20        3968 : int Inet6VpnPrefix::FromProtoPrefix(const BgpProtoPrefix &proto_prefix,
      21             :                                     Inet6VpnPrefix *prefix, uint32_t *label) {
      22        3968 :     size_t nlri_size = proto_prefix.prefix.size();
      23        3968 :     size_t expected_min_nlri_size =
      24        3968 :         BgpProtoPrefix::kLabelSize + RouteDistinguisher::kSize;
      25             : 
      26        3968 :     if (nlri_size < expected_min_nlri_size)
      27           0 :         return -1;
      28        3968 :     if (nlri_size > expected_min_nlri_size + Address::kMaxV6Bytes)
      29           0 :         return -1;
      30             : 
      31        3968 :     size_t label_offset = 0;
      32        3968 :     *label = proto_prefix.ReadLabel(label_offset);
      33        3968 :     size_t rd_offset = label_offset + BgpProtoPrefix::kLabelSize;
      34        3968 :     prefix->rd_ = RouteDistinguisher(&proto_prefix.prefix[rd_offset]);
      35             : 
      36        3968 :     size_t prefix_offset = rd_offset + RouteDistinguisher::kSize;
      37        3968 :     prefix->prefixlen_ = proto_prefix.prefixlen - prefix_offset * 8;
      38        3968 :     Ip6Address::bytes_type bt = { { 0 } };
      39        3968 :     copy(proto_prefix.prefix.begin() + prefix_offset,
      40             :         proto_prefix.prefix.end(), bt.begin());
      41        3968 :     prefix->addr_ = Ip6Address(bt);
      42             : 
      43        3968 :     return 0;
      44             : }
      45             : 
      46        3968 : int Inet6VpnPrefix::FromProtoPrefix(BgpServer *server,
      47             :                                     const BgpProtoPrefix &proto_prefix,
      48             :                                     const BgpAttr *attr,
      49             :                                     const Address::Family family,
      50             :                                     Inet6VpnPrefix *prefix,
      51             :                                     BgpAttrPtr *new_attr, uint32_t *label,
      52             :                                     uint32_t *l3_label) {
      53        3968 :     return FromProtoPrefix(proto_prefix, prefix, label);
      54             : }
      55             : 
      56        4612 : void Inet6VpnPrefix::BuildProtoPrefix(uint32_t label,
      57             :                                       BgpProtoPrefix *proto_prefix) const {
      58        4612 :     proto_prefix->prefix.clear();
      59        4612 :     size_t prefix_size = (prefixlen_ + 7) / 8;
      60        4612 :     size_t nlri_size =
      61        4612 :         BgpProtoPrefix::kLabelSize + RouteDistinguisher::kSize + prefix_size;
      62             : 
      63        4612 :     proto_prefix->prefix.resize(nlri_size, 0);
      64        4613 :     size_t label_offset = 0;
      65        4613 :     proto_prefix->WriteLabel(label_offset, label);
      66        4613 :     size_t rd_offset = label_offset + BgpProtoPrefix::kLabelSize;
      67        4613 :     copy(rd_.GetData(), rd_.GetData() + RouteDistinguisher::kSize,
      68        4613 :         proto_prefix->prefix.begin() + rd_offset);
      69             : 
      70        4615 :     size_t prefix_offset = rd_offset + RouteDistinguisher::kSize;
      71        4615 :     proto_prefix->prefixlen = prefix_offset * 8 + prefixlen_;
      72        4615 :     const Ip6Address::bytes_type &addr_bytes = addr_.to_bytes();
      73        4615 :     copy(addr_bytes.begin(), addr_bytes.begin() + prefix_size,
      74        4615 :         proto_prefix->prefix.begin() + prefix_offset);
      75        4613 : }
      76             : 
      77             : // RD:inet6-prefix
      78         768 : Inet6VpnPrefix Inet6VpnPrefix::FromString(const string &str,
      79             :                                           boost::system::error_code *errorp) {
      80         768 :     Inet6VpnPrefix prefix;
      81             : 
      82         768 :     size_t pos = str.find(':');
      83         768 :     if (pos == string::npos) {
      84           0 :         if (errorp != NULL) {
      85           0 :             *errorp = make_error_code(boost::system::errc::invalid_argument);
      86             :         }
      87           0 :         return prefix;
      88             :     }
      89         768 :     pos = str.find(':', (pos + 1));
      90         768 :     if (pos == string::npos) {
      91           0 :         if (errorp != NULL) {
      92           0 :             *errorp = make_error_code(boost::system::errc::invalid_argument);
      93             :         }
      94           0 :         return prefix;
      95             :     }
      96         768 :     string rdstr = str.substr(0, pos);
      97         768 :     boost::system::error_code rderr;
      98         768 :     prefix.rd_ = RouteDistinguisher::FromString(rdstr, &rderr);
      99         768 :     if (rderr.failed()) {
     100           0 :         if (errorp != NULL) {
     101           0 :             *errorp = rderr;
     102             :         }
     103           0 :         return prefix;
     104             :     }
     105             : 
     106         768 :     string ip6pstr(str, pos + 1);
     107         768 :     boost::system::error_code pfxerr = Inet6SubnetParse(ip6pstr, &prefix.addr_,
     108             :                                                         &prefix.prefixlen_);
     109         768 :     if (errorp != NULL) {
     110           0 :         *errorp = pfxerr;
     111             :     }
     112         768 :     return prefix;
     113         768 : }
     114             : 
     115           0 : string Inet6VpnPrefix::ToString() const {
     116           0 :     Inet6Prefix prefix(addr_, prefixlen_);
     117           0 :     return (rd_.ToString() + ":" + prefix.ToString());
     118             : }
     119             : 
     120             : // Check whether 'this' is more specific than rhs.
     121           0 : bool Inet6VpnPrefix::IsMoreSpecific(const Inet6VpnPrefix &rhs) const {
     122           0 :     Inet6Prefix this_prefix(addr_, prefixlen_);
     123           0 :     Inet6Prefix match_prefix(rhs.addr(), rhs.prefixlen());
     124             : 
     125           0 :     return this_prefix.IsMoreSpecific(match_prefix);
     126             : }
     127             : 
     128      100840 : int Inet6VpnPrefix::CompareTo(const Inet6VpnPrefix &other) const {
     129      100840 :     int res = route_distinguisher().CompareTo(other.route_distinguisher());
     130      100874 :     if (res != 0) {
     131       66686 :         return res;
     132             :     }
     133       34188 :     Ip6Address laddr = addr();
     134       34181 :     Ip6Address raddr = other.addr();
     135       34176 :     if (laddr < raddr) {
     136       10908 :         return -1;
     137             :     }
     138       23294 :     if (laddr > raddr) {
     139        8011 :         return 1;
     140             :     }
     141       15279 :     if (prefixlen() < other.prefixlen()) {
     142           0 :         return -1;
     143             :     }
     144       15283 :     if (prefixlen() > other.prefixlen()) {
     145           0 :         return 1;
     146             :     }
     147       15282 :     return 0;
     148             : }
     149             : 
     150           0 : bool Inet6VpnPrefix::operator==(const Inet6VpnPrefix &rhs) const {
     151           0 :     return (rd_ == rhs.rd_ && addr_ == rhs.addr_ &&
     152           0 :             prefixlen_ == rhs.prefixlen_);
     153             : }
     154             : 
     155       13938 : Inet6VpnRoute::Inet6VpnRoute(const Inet6VpnPrefix &prefix) : prefix_(prefix) {
     156       13937 : }
     157             : 
     158      100842 : int Inet6VpnRoute::CompareTo(const Route &rhs) const {
     159      100842 :     const Inet6VpnRoute &other = static_cast<const Inet6VpnRoute &>(rhs);
     160      100842 :     return prefix_.CompareTo(other.GetPrefix());
     161             : }
     162             : 
     163       32351 : string Inet6VpnRoute::ToString() const {
     164       32351 :     string repr = prefix_.route_distinguisher().ToString() + ":";
     165       32355 :     repr += prefix_.addr().to_string();
     166             :     char strplen[5];
     167       32354 :     snprintf(strplen, sizeof(strplen), "/%d", prefix_.prefixlen());
     168       32352 :     repr.append(strplen);
     169             : 
     170       64710 :     return repr;
     171           0 : }
     172             : 
     173           0 : void Inet6VpnRoute::SetKey(const DBRequestKey *reqkey) {
     174           0 :     const Inet6VpnTable::RequestKey *key =
     175             :         static_cast<const Inet6VpnTable::RequestKey *>(reqkey);
     176           0 :     prefix_ = key->prefix;
     177           0 : }
     178             : 
     179        4612 : void Inet6VpnRoute::BuildProtoPrefix(BgpProtoPrefix *prefix,
     180             :                                      const BgpAttr*,
     181             :                                     uint32_t label,
     182             :                                     uint32_t l3_label) const {
     183        4612 :     prefix_.BuildProtoPrefix(label, prefix);
     184        4613 : }
     185             : 
     186             : // XXX dest_nh should have been pointer. See if can change
     187        2456 : void Inet6VpnRoute::BuildBgpProtoNextHop(vector<uint8_t> &dest_nh,
     188             :                                          IpAddress src_nh) const {
     189        2456 :     dest_nh.resize(sizeof(Ip6Address::bytes_type) + RouteDistinguisher::kSize,
     190        2456 :                    0);
     191        2456 :     Ip6Address source_addr;
     192        2456 :     if (src_nh.is_v4()) {
     193        2456 :         source_addr = Ip6Address::v4_mapped(src_nh.to_v4());
     194           0 :     } else if (src_nh.is_v6()) {
     195           0 :         source_addr = src_nh.to_v6();
     196             :     } else {
     197           0 :         assert(0);
     198             :     }
     199             : 
     200        2456 :     Ip6Address::bytes_type addr_bytes = source_addr.to_bytes();
     201        2456 :     copy(addr_bytes.begin(), addr_bytes.end(),
     202        2456 :               dest_nh.begin() + RouteDistinguisher::kSize);
     203        2456 : }
     204             : 
     205        5560 : DBEntryBase::KeyPtr Inet6VpnRoute::GetDBRequestKey() const {
     206             :     Inet6VpnTable::RequestKey *key =
     207        5560 :         new Inet6VpnTable::RequestKey(GetPrefix(), NULL);
     208        5557 :     return KeyPtr(key);
     209             : }
     210             : 
     211             : // Check whether 'this' is more specific than rhs.
     212           0 : bool Inet6VpnRoute::IsMoreSpecific(const string &other) const {
     213           0 :     boost::system::error_code ec;
     214             : 
     215           0 :     Inet6VpnPrefix other_prefix = Inet6VpnPrefix::FromString(other, &ec);
     216           0 :     if (!ec) {
     217           0 :         return GetPrefix().IsMoreSpecific(other_prefix);
     218             :     }
     219             : 
     220           0 :     return false;
     221             : }
     222             : 
     223             : // Check whether 'this' is less specific than rhs.
     224           0 : bool Inet6VpnRoute::IsLessSpecific(const string &other) const {
     225           0 :     boost::system::error_code ec;
     226             : 
     227           0 :     Inet6VpnPrefix other_prefix = Inet6VpnPrefix::FromString(other, &ec);
     228           0 :     if (!ec) {
     229           0 :         return other_prefix.IsMoreSpecific(GetPrefix());
     230             :     }
     231             : 
     232           0 :     return false;
     233             : }

Generated by: LCOV version 1.14