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: 23 136 16.9 %
Date: 2026-11-23 02:04:52 Functions: 4 18 22.2 %
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           0 : Inet6VpnPrefix::Inet6VpnPrefix() : prefixlen_(0) {
      18           0 : }
      19             : 
      20           0 : int Inet6VpnPrefix::FromProtoPrefix(const BgpProtoPrefix &proto_prefix,
      21             :                                     Inet6VpnPrefix *prefix, uint32_t *label) {
      22           0 :     size_t nlri_size = proto_prefix.prefix.size();
      23           0 :     size_t expected_min_nlri_size =
      24           0 :         BgpProtoPrefix::kLabelSize + RouteDistinguisher::kSize;
      25             : 
      26           0 :     if (nlri_size < expected_min_nlri_size)
      27           0 :         return -1;
      28           0 :     if (nlri_size > expected_min_nlri_size + Address::kMaxV6Bytes)
      29           0 :         return -1;
      30             : 
      31           0 :     size_t label_offset = 0;
      32           0 :     *label = proto_prefix.ReadLabel(label_offset);
      33           0 :     size_t rd_offset = label_offset + BgpProtoPrefix::kLabelSize;
      34           0 :     prefix->rd_ = RouteDistinguisher(&proto_prefix.prefix[rd_offset]);
      35             : 
      36           0 :     size_t prefix_offset = rd_offset + RouteDistinguisher::kSize;
      37           0 :     prefix->prefixlen_ = proto_prefix.prefixlen - prefix_offset * 8;
      38           0 :     Ip6Address::bytes_type bt = { { 0 } };
      39           0 :     copy(proto_prefix.prefix.begin() + prefix_offset,
      40             :         proto_prefix.prefix.end(), bt.begin());
      41           0 :     prefix->addr_ = Ip6Address(bt);
      42             : 
      43           0 :     return 0;
      44             : }
      45             : 
      46           0 : 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           0 :     return FromProtoPrefix(proto_prefix, prefix, label);
      54             : }
      55             : 
      56           0 : void Inet6VpnPrefix::BuildProtoPrefix(uint32_t label,
      57             :                                       BgpProtoPrefix *proto_prefix) const {
      58           0 :     proto_prefix->prefix.clear();
      59           0 :     size_t prefix_size = (prefixlen_ + 7) / 8;
      60           0 :     size_t nlri_size =
      61           0 :         BgpProtoPrefix::kLabelSize + RouteDistinguisher::kSize + prefix_size;
      62             : 
      63           0 :     proto_prefix->prefix.resize(nlri_size, 0);
      64           0 :     size_t label_offset = 0;
      65           0 :     proto_prefix->WriteLabel(label_offset, label);
      66           0 :     size_t rd_offset = label_offset + BgpProtoPrefix::kLabelSize;
      67           0 :     copy(rd_.GetData(), rd_.GetData() + RouteDistinguisher::kSize,
      68           0 :         proto_prefix->prefix.begin() + rd_offset);
      69             : 
      70           0 :     size_t prefix_offset = rd_offset + RouteDistinguisher::kSize;
      71           0 :     proto_prefix->prefixlen = prefix_offset * 8 + prefixlen_;
      72           0 :     const Ip6Address::bytes_type &addr_bytes = addr_.to_bytes();
      73           0 :     copy(addr_bytes.begin(), addr_bytes.begin() + prefix_size,
      74           0 :         proto_prefix->prefix.begin() + prefix_offset);
      75           0 : }
      76             : 
      77             : // RD:inet6-prefix
      78           0 : Inet6VpnPrefix Inet6VpnPrefix::FromString(const string &str,
      79             :                                           boost::system::error_code *errorp) {
      80           0 :     Inet6VpnPrefix prefix;
      81             : 
      82           0 :     size_t pos = str.find(':');
      83           0 :     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           0 :     pos = str.find(':', (pos + 1));
      90           0 :     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           0 :     string rdstr = str.substr(0, pos);
      97           0 :     boost::system::error_code rderr;
      98           0 :     prefix.rd_ = RouteDistinguisher::FromString(rdstr, &rderr);
      99           0 :     if (rderr.failed()) {
     100           0 :         if (errorp != NULL) {
     101           0 :             *errorp = rderr;
     102             :         }
     103           0 :         return prefix;
     104             :     }
     105             : 
     106           0 :     string ip6pstr(str, pos + 1);
     107           0 :     boost::system::error_code pfxerr = Inet6SubnetParse(ip6pstr, &prefix.addr_,
     108             :                                                         &prefix.prefixlen_);
     109           0 :     if (errorp != NULL) {
     110           0 :         *errorp = pfxerr;
     111             :     }
     112           0 :     return prefix;
     113           0 : }
     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        1508 : int Inet6VpnPrefix::CompareTo(const Inet6VpnPrefix &other) const {
     129        1508 :     int res = route_distinguisher().CompareTo(other.route_distinguisher());
     130        1508 :     if (res != 0) {
     131           0 :         return res;
     132             :     }
     133        1508 :     Ip6Address laddr = addr();
     134        1508 :     Ip6Address raddr = other.addr();
     135        1508 :     if (laddr < raddr) {
     136         226 :         return -1;
     137             :     }
     138        1282 :     if (laddr > raddr) {
     139          64 :         return 1;
     140             :     }
     141        1218 :     if (prefixlen() < other.prefixlen()) {
     142           0 :         return -1;
     143             :     }
     144        1218 :     if (prefixlen() > other.prefixlen()) {
     145           0 :         return 1;
     146             :     }
     147        1218 :     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         737 : Inet6VpnRoute::Inet6VpnRoute(const Inet6VpnPrefix &prefix) : prefix_(prefix) {
     156         737 : }
     157             : 
     158        1508 : int Inet6VpnRoute::CompareTo(const Route &rhs) const {
     159        1508 :     const Inet6VpnRoute &other = static_cast<const Inet6VpnRoute &>(rhs);
     160        1508 :     return prefix_.CompareTo(other.GetPrefix());
     161             : }
     162             : 
     163         769 : string Inet6VpnRoute::ToString() const {
     164         769 :     string repr = prefix_.route_distinguisher().ToString() + ":";
     165         769 :     repr += prefix_.addr().to_string();
     166             :     char strplen[5];
     167         769 :     snprintf(strplen, sizeof(strplen), "/%d", prefix_.prefixlen());
     168         769 :     repr.append(strplen);
     169             : 
     170        1538 :     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           0 : void Inet6VpnRoute::BuildProtoPrefix(BgpProtoPrefix *prefix,
     180             :                                      const BgpAttr*,
     181             :                                     uint32_t label,
     182             :                                     uint32_t l3_label) const {
     183           0 :     prefix_.BuildProtoPrefix(label, prefix);
     184           0 : }
     185             : 
     186             : // XXX dest_nh should have been pointer. See if can change
     187           0 : void Inet6VpnRoute::BuildBgpProtoNextHop(vector<uint8_t> &dest_nh,
     188             :                                          IpAddress src_nh) const {
     189           0 :     dest_nh.resize(sizeof(Ip6Address::bytes_type) + RouteDistinguisher::kSize,
     190           0 :                    0);
     191           0 :     Ip6Address source_addr;
     192           0 :     if (src_nh.is_v4()) {
     193           0 :         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           0 :     Ip6Address::bytes_type addr_bytes = source_addr.to_bytes();
     201           0 :     copy(addr_bytes.begin(), addr_bytes.end(),
     202           0 :               dest_nh.begin() + RouteDistinguisher::kSize);
     203           0 : }
     204             : 
     205           0 : DBEntryBase::KeyPtr Inet6VpnRoute::GetDBRequestKey() const {
     206             :     Inet6VpnTable::RequestKey *key =
     207           0 :         new Inet6VpnTable::RequestKey(GetPrefix(), NULL);
     208           0 :     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