LCOV - code coverage report
Current view: top level - bgp - bgp_message_builder.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 123 203 60.6 %
Date: 2026-11-16 02:06:09 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/bgp_message_builder.h"
       7             : 
       8             : #include <vector>
       9             : 
      10             : #include "bgp/bgp_log.h"
      11             : #include "bgp/bgp_ribout.h"
      12             : #include "bgp/bgp_route.h"
      13             : #include "bgp/bgp_server.h"
      14             : #include "net/bgp_af.h"
      15             : #include "extended-community/tag.h"
      16             : #include "large-community/tag.h"
      17             : 
      18             : using std::unique_ptr;
      19             : 
      20           4 : BgpMessage::BgpMessage() : table_(NULL), datalen_(0) {
      21           4 : }
      22             : 
      23           8 : BgpMessage::~BgpMessage() {
      24           8 : }
      25             : 
      26        7526 : bool BgpMessage::StartReach(const RibOut *ribout, const RibOutAttr *roattr,
      27             :                             const BgpRoute *route) {
      28        7526 :     BgpProto::Update update;
      29        7527 :     const BgpAttr *attr = roattr->attr();
      30        7527 :     Address::Family family = table_->family();
      31             : 
      32        7527 :     BgpAttrOrigin *origin = new BgpAttrOrigin(attr->origin());
      33        7528 :     update.path_attributes.push_back(origin);
      34             : 
      35       11579 :     if ((BgpAf::FamilyToAfi(family) == BgpAf::IPv4) &&
      36        4052 :         (BgpAf::FamilyToSafi(family) == BgpAf::Unicast)) {
      37             :         BgpAttrNextHop *nh =
      38           0 :             new BgpAttrNextHop(attr->nexthop().to_v4().to_ulong());
      39           0 :         update.path_attributes.push_back(nh);
      40             :     }
      41             : 
      42        7527 :     if (attr->med()) {
      43        3796 :         BgpAttrMultiExitDisc *med = new BgpAttrMultiExitDisc(attr->med());
      44        3796 :         update.path_attributes.push_back(med);
      45             :     }
      46             : 
      47        7527 :     if (ribout->peer_type() == BgpProto::IBGP) {
      48           0 :         BgpAttrLocalPref *lp = new BgpAttrLocalPref(attr->local_pref());
      49           0 :         update.path_attributes.push_back(lp);
      50             :     }
      51             : 
      52        7528 :     if (attr->atomic_aggregate()) {
      53           0 :         BgpAttrAtomicAggregate *aa = new BgpAttrAtomicAggregate;
      54           0 :         update.path_attributes.push_back(aa);
      55             :     }
      56             : 
      57        7528 :     if (attr->aggregator_as_num()) {
      58           0 :         if (ribout->as4_supported()) {
      59             :             BgpAttr4ByteAggregator *agg = new BgpAttr4ByteAggregator(
      60           0 :                 attr->aggregator_as_num(),
      61           0 :                 attr->aggregator_adderess().to_v4().to_ulong());
      62           0 :             update.path_attributes.push_back(agg);
      63             :         } else {
      64           0 :             if (attr->aggregator_as_num() > 0xffff) {
      65             :                 // For old neighbor, need to send AS4_Aggregator along with
      66             :                 // AS_TRANS in regular aggregator attribute
      67             :                 BgpAttrAggregator *agg = new BgpAttrAggregator(
      68           0 :                     AS_TRANS, attr->aggregator_adderess().to_v4().to_ulong());
      69           0 :                 update.path_attributes.push_back(agg);
      70             :                 BgpAttrAs4Aggregator *as4_agg = new BgpAttrAs4Aggregator(
      71           0 :                     attr->aggregator_as_num(),
      72           0 :                     attr->aggregator_adderess().to_v4().to_ulong());
      73           0 :                 update.path_attributes.push_back(as4_agg);
      74             :             } else {
      75             :                 BgpAttrAggregator *agg = new BgpAttrAggregator(
      76           0 :                     attr->aggregator_as_num(),
      77           0 :                     attr->aggregator_adderess().to_v4().to_ulong());
      78           0 :                 update.path_attributes.push_back(agg);
      79             :             }
      80             :         }
      81             :     }
      82             : 
      83        7528 :     if (!attr->originator_id().is_unspecified()) {
      84             :         BgpAttrOriginatorId *originator_id =
      85           0 :             new BgpAttrOriginatorId(attr->originator_id().to_ulong());
      86           0 :         update.path_attributes.push_back(originator_id);
      87             :     }
      88             : 
      89        7528 :     if (attr->cluster_list()) {
      90             :         ClusterListSpec *clist =
      91           0 :             new ClusterListSpec(attr->cluster_list()->cluster_list());
      92           0 :         update.path_attributes.push_back(clist);
      93             :     }
      94             : 
      95             : 
      96        7528 :     if (ribout->as4_supported()) {
      97           0 :         if (attr->aspath_4byte()) {
      98             :             AsPath4ByteSpec *path = new AsPath4ByteSpec(
      99           0 :                                 attr->aspath_4byte()->path());
     100           0 :             update.path_attributes.push_back(path);
     101             :         }
     102             :     } else {
     103        7528 :         if (attr->as_path()) {
     104        7528 :             AsPathSpec *path = new AsPathSpec(attr->as_path()->path());
     105        7529 :             update.path_attributes.push_back(path);
     106             :         }
     107        7526 :         if (attr->as4_path()) {
     108           0 :             As4PathSpec *path = new As4PathSpec(attr->as4_path()->path());
     109           0 :             update.path_attributes.push_back(path);
     110             :         }
     111             :     }
     112             : 
     113        7526 :     if (attr->edge_discovery()) {
     114             :         EdgeDiscoverySpec *edspec =
     115           0 :             new EdgeDiscoverySpec(attr->edge_discovery()->edge_discovery());
     116           0 :         update.path_attributes.push_back(edspec);
     117             :     }
     118             : 
     119        7526 :     if (attr->edge_forwarding()) {
     120             :         EdgeForwardingSpec *efspec =
     121           0 :             new EdgeForwardingSpec(attr->edge_forwarding()->edge_forwarding());
     122           0 :         update.path_attributes.push_back(efspec);
     123             :     }
     124             : 
     125        7526 :     if (attr->community() && attr->community()->communities().size()) {
     126           0 :         CommunitySpec *comm = new CommunitySpec;
     127           0 :         comm->communities = attr->community()->communities();
     128           0 :         update.path_attributes.push_back(comm);
     129             :     }
     130             : 
     131        7526 :     ExtCommunitySpec *ext_comm = new ExtCommunitySpec;
     132        7525 :     if (attr->ext_community() && attr->ext_community()->communities().size()) {
     133             :         const ExtCommunity::ExtCommunityList &v =
     134        6350 :                 attr->ext_community()->communities();
     135        6350 :         for (ExtCommunity::ExtCommunityList::const_iterator it = v.begin();
     136       43931 :                 it != v.end(); ++it) {
     137       37577 :             uint64_t value = get_value(it->data(), it->size());
     138       37575 :             ext_comm->communities.push_back(value);
     139             :         }
     140             :     }
     141             : 
     142        7527 :     LargeCommunitySpec *large_comm = new LargeCommunitySpec;
     143        7527 :     if (attr->large_community() && attr->large_community()->communities().size()) {
     144           0 :         uint16_t tag_index = 0;
     145             :         const LargeCommunity::LargeCommunityList &v =
     146           0 :                 attr->large_community()->communities();
     147             :         uint64_t tid64;
     148             :         uint32_t asn, tid, value;
     149           0 :         for (LargeCommunity::LargeCommunityList::const_iterator it = v.begin();
     150           0 :              it != v.end(); ++it) {
     151           0 :                 TagLC tag_lc {*it};
     152           0 :                 tid64 = tag_lc.tag();
     153           0 :                 if ((tid64 & 0xFFFF0000) == 0) {
     154           0 :                     asn = tag_lc.as_number();
     155           0 :                     tid = ((tid64 & 0xFFFF00000000) >> 16) |
     156           0 :                         (tid64 & 0xFFFF);
     157           0 :                     if (asn <= AS2_MAX) {
     158           0 :                         Tag tag {as2_t(asn), tid};
     159           0 :                         ext_comm->communities.push_back(tag.GetExtCommunityValue());
     160             :                     } else {
     161           0 :                         Tag4ByteAs tag4 {asn, tag_index};
     162           0 :                         Tag tag {tag_index, tid};
     163           0 :                         ext_comm->communities.push_back(tag.GetExtCommunityValue());
     164           0 :                         ext_comm->communities.push_back(tag4.GetExtCommunityValue());
     165           0 :                         tag_index++;
     166             :                     }
     167           0 :                     continue;
     168           0 :                 }
     169             : 
     170           0 :                 for (int i = 0; i < 3; i++) {
     171           0 :                     value = get_value(it->data() + 4*i, 4);
     172           0 :                     large_comm->communities.push_back(value);
     173             :                 }
     174             :         }
     175           0 :         update.path_attributes.push_back(large_comm);
     176             :     }
     177        7526 :     if (ext_comm->EncodeLength()) {
     178        6351 :         update.path_attributes.push_back(ext_comm);
     179             :     }
     180             : 
     181        7531 :     if (attr->origin_vn_path() && attr->origin_vn_path()->origin_vns().size()) {
     182           0 :         OriginVnPathSpec *ovnpath_spec = new OriginVnPathSpec;
     183             :         const OriginVnPath::OriginVnList &v =
     184           0 :             attr->origin_vn_path()->origin_vns();
     185           0 :         for (OriginVnPath::OriginVnList::const_iterator it = v.begin();
     186           0 :              it != v.end(); ++it) {
     187           0 :             uint64_t value = get_value(it->data(), it->size());
     188           0 :             ovnpath_spec->origin_vns.push_back(value);
     189             :         }
     190           0 :         update.path_attributes.push_back(ovnpath_spec);
     191             :     }
     192             : 
     193        7531 :     if (attr->pmsi_tunnel()) {
     194             :         PmsiTunnelSpec *pmsi_spec =
     195           0 :             new PmsiTunnelSpec(attr->pmsi_tunnel()->pmsi_tunnel());
     196           0 :         update.path_attributes.push_back(pmsi_spec);
     197             :     }
     198             : 
     199        7530 :     std::vector<uint8_t> nh;
     200             : 
     201        7530 :     route->BuildBgpProtoNextHop(nh, attr->nexthop());
     202             : 
     203             :     BgpMpNlri *nlri = new BgpMpNlri(
     204        7528 :         BgpAttribute::MPReachNlri, BgpAf::FamilyToAfi(family),
     205        7528 :         BgpAf::FamilyToSafi(family), nh);
     206        7525 :     update.path_attributes.push_back(nlri);
     207             : 
     208        7525 :     BgpProtoPrefix *prefix = new BgpProtoPrefix;
     209        7526 :     const uint32_t label = (family == Address::INET) ? 0 : roattr->label();
     210        7525 :     route->BuildProtoPrefix(prefix, attr, label, roattr->l3_label());
     211        7526 :     nlri->nlri.push_back(prefix);
     212             : 
     213        7529 :     int result = BgpProto::Encode(&update, data_, sizeof(data_),
     214        7529 :                                   &encode_offsets_, ribout->as4_supported());
     215        7527 :     if (result <= 0) {
     216           0 :         BGP_LOG_WARNING_STR(BgpMessageSend, BGP_LOG_FLAG_ALL,
     217             :             "Error encoding reach message for route " << route->ToString() <<
     218             :             " in table " << (table_ ? table_->name() : "unknown"));
     219           0 :         table_->server()->increment_message_build_error();
     220           0 :         return false;
     221             :     }
     222             : 
     223        7527 :     num_reach_route_++;
     224        7527 :     datalen_ = result;
     225        7527 :     return true;
     226        7527 : }
     227             : 
     228        2859 : bool BgpMessage::StartUnreach(const BgpRoute *route) {
     229        2859 :     BgpProto::Update update;
     230        2858 :     Address::Family family = table_->family();
     231             : 
     232             :     BgpMpNlri *nlri =
     233             :         new BgpMpNlri(BgpAttribute::MPUnreachNlri,
     234        2858 :         BgpAf::FamilyToAfi(family), BgpAf::FamilyToSafi(family));
     235        2857 :     update.path_attributes.push_back(nlri);
     236             : 
     237        2857 :     BgpProtoPrefix *prefix = new BgpProtoPrefix;
     238        2857 :     route->BuildProtoPrefix(prefix);
     239        2856 :     nlri->nlri.push_back(prefix);
     240             : 
     241             :     int result =
     242        2854 :         BgpProto::Encode(&update, data_, sizeof(data_), &encode_offsets_);
     243        2858 :     if (result <= 0) {
     244           0 :         BGP_LOG_WARNING_STR(BgpMessageSend, BGP_LOG_FLAG_ALL,
     245             :             "Error encoding unreach message for route " << route->ToString() <<
     246             :             " in table " << (table_ ? table_->name() : "unknown"));
     247           0 :         table_->server()->increment_message_build_error();
     248           0 :         return false;
     249             :     }
     250             : 
     251        2858 :     num_unreach_route_++;
     252        2858 :     datalen_ = result;
     253        2858 :     return true;
     254        2858 : }
     255             : 
     256       10379 : void BgpMessage::Reset() {
     257       10379 :     Message::Reset();
     258       10378 :     table_ = NULL;
     259       10378 :     encode_offsets_.ClearOffsets();
     260       10386 :     datalen_ = 0;
     261       10386 : }
     262             : 
     263       10379 : bool BgpMessage::Start(const RibOut *ribout, bool cache_repr,
     264             :     const RibOutAttr *roattr, const BgpRoute *route) {
     265       10379 :     Reset();
     266       10386 :     table_ = ribout->table();
     267             : 
     268       10386 :     if (roattr->IsReachable()) {
     269        7526 :         return StartReach(ribout, roattr, route);
     270             :     } else {
     271        2859 :         return StartUnreach(route);
     272             :     }
     273             : }
     274             : 
     275       10357 : bool BgpMessage::UpdateLength(const char *tag, int size, int delta) {
     276       10357 :     int offset = encode_offsets_.FindOffset(tag);
     277       10338 :     if (offset < 0) {
     278           0 :         return false;
     279             :     }
     280       10338 :     int value = get_value(&data_[offset], size);
     281       10338 :     value += delta;
     282       10338 :     put_value(&data_[offset], size, value);
     283       10341 :     return true;
     284             : }
     285             : 
     286        3452 : bool BgpMessage::AddRoute(const BgpRoute *route, const RibOutAttr *roattr) {
     287        3452 :     uint8_t *data = data_ + datalen_;
     288        3452 :     size_t size = sizeof(data_) - datalen_;
     289        3452 :     Address::Family family = table_->family();
     290             : 
     291        3452 :     BgpMpNlri nlri;
     292        3450 :     nlri.afi = BgpAf::FamilyToAfi(family);
     293        3450 :     nlri.safi = BgpAf::FamilyToSafi(family);
     294        3450 :     BgpProtoPrefix *prefix = new BgpProtoPrefix;
     295        3450 :     if (roattr) {
     296        3450 :         const uint32_t label = (family == Address::INET) ? 0 : roattr->label();
     297        3449 :         route->BuildProtoPrefix(prefix, roattr->attr(), label);
     298             :     } else {
     299           0 :         route->BuildProtoPrefix(prefix);
     300             :     }
     301        3448 :     nlri.nlri.push_back(prefix);
     302             : 
     303        3443 :     int result = BgpProto::Encode(&nlri, data, size);
     304        3454 :     if (result <= 0) {
     305           0 :         return false;
     306             :     }
     307             : 
     308        3454 :     datalen_ += result;
     309        3454 :     if (roattr->IsReachable()) {
     310        1341 :         num_reach_route_++;
     311             :     } else {
     312        2113 :         num_unreach_route_++;
     313             :     }
     314             : 
     315        3454 :     if (!UpdateLength("BgpMsgLength", 2, result)) {
     316           0 :         assert(false);
     317             :         return false;
     318             :     }
     319             : 
     320        3456 :     if (!UpdateLength("BgpPathAttribute", 2, result)) {
     321           0 :         assert(false);
     322             :         return false;
     323             :     }
     324             : 
     325        3455 :     if (!UpdateLength("MpReachUnreachNlri", 2, result)) {
     326           0 :         assert(false);
     327             :         return false;
     328             :     }
     329             : 
     330        3456 :     return true;
     331        3456 : }
     332             : 
     333       10386 : void BgpMessage::Finish() {
     334       10386 : }
     335             : 
     336       10380 : const uint8_t *BgpMessage::GetData(IPeerUpdate *peer, size_t *lenp,
     337             :     const string **msg_str, string *temp) {
     338       10380 :     *lenp = datalen_;
     339       10380 :     return data_;
     340             : }
     341             : 
     342           4 : Message *BgpMessageBuilder::Create() const {
     343           4 :     return new BgpMessage;
     344             : }
     345             : 
     346           1 : BgpMessageBuilder::BgpMessageBuilder()
     347           1 : : MessageBuilder() {
     348           1 : }

Generated by: LCOV version 1.14