LCOV - code coverage report
Current view: top level - bgp - xmpp_message_builder.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 270 413 65.4 %
Date: 2026-11-23 02:04:52 Functions: 25 31 80.6 %
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/xmpp_message_builder.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include <algorithm>
      11             : 
      12             : #include "bgp/ipeer.h"
      13             : #include "bgp/bgp_server.h"
      14             : #include "bgp/bgp_table.h"
      15             : #include "bgp/extended-community/etree.h"
      16             : #include "bgp/extended-community/mac_mobility.h"
      17             : #include "bgp/ermvpn/ermvpn_route.h"
      18             : #include "bgp/evpn/evpn_route.h"
      19             : #include "bgp/mvpn/mvpn_route.h"
      20             : #include "bgp/routing-instance/routing_instance.h"
      21             : #include "bgp/security_group/security_group.h"
      22             : #include "db/db.h"
      23             : #include "net/community_type.h"
      24             : #include "schema/xmpp_multicast_types.h"
      25             : #include "schema/xmpp_mvpn_types.h"
      26             : #include "schema/xmpp_enet_types.h"
      27             : #include "xmpp/xmpp_init.h"
      28             : 
      29             : using pugi::xml_attribute;
      30             : using pugi::xml_document;
      31             : using pugi::xml_node;
      32             : using std::ostringstream;
      33             : using std::copy;
      34             : using std::fill;
      35             : using std::string;
      36             : using std::stringstream;
      37             : using std::vector;
      38             : 
      39        3095 : static inline const char *AfiName(uint16_t afi) {
      40        3095 :     switch (afi) {
      41         976 :     case BgpAf::IPv4:
      42         976 :         return "1";
      43             :         break;
      44         644 :     case BgpAf::IPv6:
      45         644 :         return "2";
      46             :         break;
      47        1475 :     case BgpAf::L2Vpn:
      48        1475 :         return "25";
      49             :         break;
      50             :     }
      51           0 :     assert(false);
      52             :     return NULL;
      53             : }
      54             : 
      55        3096 : static inline const char *XmppSafiName(uint8_t safi) {
      56        3096 :     switch (safi) {
      57        1621 :     case BgpAf::Unicast:
      58        1621 :         return "1";
      59             :         break;
      60           0 :     case BgpAf::Mpls:
      61           0 :         return "4";
      62             :         break;
      63           0 :     case BgpAf::MVpn:
      64           0 :         return "5";
      65             :         break;
      66           0 :     case BgpAf::Mcast:
      67           0 :         return "241";
      68             :         break;
      69        1475 :     case BgpAf::Enet:
      70        1475 :         return "242";
      71             :         break;
      72             :     }
      73           0 :     assert(false);
      74             :     return NULL;
      75             : }
      76             : 
      77           4 : BgpXmppMessage::BgpXmppMessage()
      78           4 :     : table_(NULL),
      79           4 :       writer_(XmlWriter(&repr_)),
      80           4 :       is_reachable_(false),
      81           4 :       cache_routes_(false),
      82           4 :       repr_valid_(false),
      83           4 :       mobility_(0, false),
      84           8 :       etree_leaf_(false) {
      85           4 :     msg_begin_.reserve(kMaxFromToLength);
      86           4 : }
      87             : 
      88           8 : BgpXmppMessage::~BgpXmppMessage() {
      89           8 : }
      90             : 
      91        3096 : void BgpXmppMessage::Reset() {
      92        3096 :     Message::Reset();
      93        3096 :     table_ = NULL;
      94        3096 :     is_reachable_ = false;
      95        3096 :     cache_routes_ = false;
      96        3096 :     repr_valid_ = false;
      97        3096 :     repr_.clear();
      98        3096 : }
      99             : 
     100        3096 : bool BgpXmppMessage::Start(const RibOut *ribout, bool cache_routes,
     101             :     const RibOutAttr *roattr, const BgpRoute *route) {
     102        3096 :     Reset();
     103        3096 :     table_ = ribout->table();
     104        3096 :     is_reachable_ = roattr->IsReachable();
     105        3095 :     cache_routes_ = cache_routes;
     106        3095 :     Address::Family family = table_->family();
     107             : 
     108        3095 :     if (is_reachable_) {
     109        2370 :         const BgpAttr *attr = roattr->attr();
     110        2370 :         ProcessCommunity(attr->community());
     111        2370 :         ProcessExtCommunity(attr->ext_community());
     112             :     }
     113             : 
     114             :     // Reserve space for the begin line that contains the message opening tag
     115             :     // with from and to attributes. Actual value gets patched in when GetData
     116             :     // is called.
     117        3095 :     repr_.append(kMaxFromToLength, ' ');
     118             : 
     119             :     // Add opening tags for event and items. The closing tags are added when
     120             :     // GetData is called.
     121        3095 :     repr_ += "\n\t<event xmlns=\"http://jabber.org/protocol/pubsub\">";
     122        3096 :     repr_ += "\n\t\t<items node=\"";
     123        3096 :     repr_ += AfiName(BgpAf::FamilyToAfi(family));
     124        3096 :     repr_ += "/";
     125        3096 :     repr_ += XmppSafiName(BgpAf::FamilyToXmppSafi(family));
     126        3096 :     repr_ += "/";
     127        3096 :     repr_ += table_->routing_instance()->name();
     128        3096 :     repr_ += "\">\n";
     129             : 
     130        3096 :     if (table_->family() == Address::ERMVPN) {
     131           0 :         AddMcastRoute(route, roattr);
     132        3096 :     } else if (table_->family() == Address::EVPN) {
     133        1475 :         AddEnetRoute(route, roattr);
     134        1621 :     } else if (table_->family() == Address::INET6) {
     135         644 :         AddInet6Route(route, roattr);
     136         977 :     } else if (table_->family() == Address::MVPN) {
     137           0 :         AddMvpnRoute(route, roattr);
     138             :     } else {
     139         977 :         AddInetRoute(route, roattr);
     140             :     }
     141        3096 :     return true;
     142             : }
     143             : 
     144        3096 : void BgpXmppMessage::Finish() {
     145        3096 : }
     146             : 
     147         268 : bool BgpXmppMessage::AddRoute(const BgpRoute *route, const RibOutAttr *roattr) {
     148         268 :     assert(is_reachable_ == roattr->IsReachable());
     149         268 :     if (is_reachable_ && num_reach_route_ >= kMaxReachCount)
     150           0 :         return false;
     151         268 :     if (!is_reachable_ && num_unreach_route_ >= kMaxUnreachCount)
     152           0 :         return false;
     153             : 
     154         268 :     if (is_reachable_) {
     155         225 :         const BgpAttr *attr = roattr->attr();
     156         225 :         ProcessCommunity(attr->community());
     157         225 :         ProcessExtCommunity(attr->ext_community());
     158             :     }
     159             : 
     160         268 :     if (table_->family() == Address::ERMVPN) {
     161           0 :         return AddMcastRoute(route, roattr);
     162         268 :     } else if (table_->family() == Address::EVPN) {
     163         234 :         return AddEnetRoute(route, roattr);
     164          34 :     } else if (table_->family() == Address::INET6) {
     165          29 :         return AddInet6Route(route, roattr);
     166           5 :     } else if (table_->family() == Address::MVPN) {
     167           0 :         return AddMvpnRoute(route, roattr);
     168             :     } else {
     169           5 :         return AddInetRoute(route, roattr);
     170             :     }
     171             : }
     172             : 
     173        1399 : void BgpXmppMessage::EncodeNextHop(const BgpRoute *route,
     174             :                                    const RibOutAttr::NextHop &nexthop,
     175             :                                    autogen::ItemType *item) {
     176        1399 :     autogen::NextHopType item_nexthop;
     177             : 
     178        1399 :     const IpAddress &address = nexthop.address();
     179        1399 :     if (address.is_v4()) {
     180        1399 :         item_nexthop.af = BgpAf::IPv4;
     181        1399 :         item_nexthop.address = address.to_v4().to_string();
     182             :     } else {
     183           0 :         item_nexthop.af = BgpAf::IPv6;
     184           0 :         item_nexthop.address = address.to_v6().to_string();
     185             :     }
     186        1399 :     item_nexthop.label = nexthop.label();
     187        1399 :     item_nexthop.virtual_network = GetVirtualNetwork(nexthop);
     188        1399 :     item_nexthop.tag_list.tag = nexthop.tag_list();
     189        1399 :     item_nexthop.is_new_tags_list = true;
     190             : 
     191             :     // If there's a non-zero label and encap list is empty use mpls over gre
     192             :     // as default encap.
     193        1399 :     if (item_nexthop.label) {
     194        1399 :         vector<string> &encap_list =
     195             :             item_nexthop.tunnel_encapsulation_list.tunnel_encapsulation;
     196        1399 :         if (nexthop.encap().empty()) {
     197           0 :             encap_list.push_back(string("gre"));
     198             :         } else {
     199        1398 :             encap_list = nexthop.encap();
     200             :         }
     201             :     }
     202             : 
     203        1398 :     item->entry.next_hops.next_hop.push_back(item_nexthop);
     204        1398 : }
     205             : 
     206        1271 : void BgpXmppMessage::AddIpReach(const BgpRoute *route,
     207             :                                 const RibOutAttr *roattr) {
     208        1271 :     if (!roattr->repr().empty()) {
     209           0 :         repr_ += roattr->repr();
     210           0 :         return;
     211             :     }
     212        1271 :     Address::Family family = table_->family();
     213             : 
     214        1271 :     autogen::ItemType item;
     215        1271 :     item.entry.nlri.af = BgpAf::FamilyToAfi(family);
     216        1271 :     item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
     217        1271 :     item.entry.nlri.address = route->ToString();
     218        1271 :     item.entry.version = 1;
     219        1271 :     item.entry.virtual_network = GetVirtualNetwork(route, roattr);
     220        1271 :     item.entry.local_preference = roattr->attr()->local_pref();
     221        1271 :     item.entry.med = roattr->attr()->med();
     222        1271 :     item.entry.sequence_number = mobility_.sequence_number;
     223        1271 :     item.entry.mobility.seqno = mobility_.sequence_number;
     224        1271 :     item.entry.mobility.sticky = mobility_.sticky;
     225             : 
     226        1271 :     assert(!roattr->nexthop_list().empty());
     227             : 
     228             :     // Encode all next-hops in the list.
     229        4067 :     BOOST_FOREACH(const RibOutAttr::NextHop &nexthop, roattr->nexthop_list()) {
     230        1399 :         EncodeNextHop(route, nexthop, &item);
     231             :     }
     232             : 
     233        1270 :     for (vector<int>::const_iterator it = security_group_list_.begin();
     234        2541 :          it != security_group_list_.end(); ++it) {
     235        1271 :         item.entry.security_group_list.security_group.push_back(*it);
     236             :     }
     237             : 
     238        1271 :     for (vector<string>::const_iterator it = community_list_.begin();
     239        1271 :          it != community_list_.end(); ++it) {
     240           0 :         item.entry.community_tag_list.community_tag.push_back(*it);
     241             :     }
     242             : 
     243             :     // Encode load balance attribute.
     244        1270 :     if (!load_balance_attribute_.IsDefault())
     245           0 :         load_balance_attribute_.Encode(&item.entry.load_balance);
     246             : 
     247        1270 :     xml_node node = doc_.append_child("item");
     248        1271 :     node.append_attribute("id") = route->ToXmppIdString().c_str();
     249             : 
     250             :     // Remember the previous size.
     251             :     // Using remove_child instead of reset allows memory pages allocated for
     252             :     // the xml_document to be reused during the lifetime of the xml_document.
     253        1271 :     size_t pos = repr_.size();
     254        1271 :     item.Encode(&node);
     255        1271 :     doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
     256        1271 :     doc_.remove_child(node);
     257             : 
     258             :     // Cache the substring starting at the previous size.
     259        1271 :     if (cache_routes_)
     260           0 :         roattr->set_repr(repr_, pos);
     261        1271 : }
     262             : 
     263         384 : void BgpXmppMessage::AddIpUnreach(const BgpRoute *route) {
     264         384 :     repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
     265         384 : }
     266             : 
     267         982 : bool BgpXmppMessage::AddInetRoute(const BgpRoute *route,
     268             :                                   const RibOutAttr *roattr) {
     269         982 :     if (is_reachable_) {
     270         726 :         num_reach_route_++;
     271         726 :         AddIpReach(route, roattr);
     272             :     } else {
     273         256 :         num_unreach_route_++;
     274         256 :         AddIpUnreach(route);
     275             :     }
     276         982 :     return true;
     277             : }
     278             : 
     279         673 : bool BgpXmppMessage::AddInet6Route(const BgpRoute *route,
     280             :                                    const RibOutAttr *roattr) {
     281         673 :     if (is_reachable_) {
     282         545 :         num_reach_route_++;
     283         545 :         AddIpReach(route, roattr);
     284             :     } else {
     285         128 :         num_unreach_route_++;
     286         128 :         AddIpUnreach(route);
     287             :     }
     288         673 :     return true;
     289             : }
     290             : 
     291        1453 : void BgpXmppMessage::EncodeEnetNextHop(const BgpRoute *route,
     292             :                                        const RibOutAttr::NextHop &nexthop,
     293             :                                        autogen::EnetItemType *item) {
     294        1453 :     autogen::EnetNextHopType item_nexthop;
     295             : 
     296        1453 :     item_nexthop.af = BgpAf::IPv4;
     297        1453 :     item_nexthop.address = nexthop.address().to_v4().to_string();
     298        1453 :     item_nexthop.label = nexthop.label();
     299        1453 :     item_nexthop.l3_label = nexthop.l3_label();
     300        1453 :     if (!nexthop.mac().IsZero())
     301           0 :         item_nexthop.mac = nexthop.mac().ToString();
     302             : 
     303             :     // If encap list is empty use mpls over gre as default encap.
     304        1453 :     vector<string> &encap_list =
     305             :         item_nexthop.tunnel_encapsulation_list.tunnel_encapsulation;
     306        1453 :     if (nexthop.encap().empty()) {
     307           0 :         encap_list.push_back(string("gre"));
     308             :     } else {
     309        1453 :         encap_list = nexthop.encap();
     310             :     }
     311        1453 :     item_nexthop.tag_list.tag = nexthop.tag_list();
     312        1453 :     item_nexthop.is_new_tags_list = true;
     313        1453 :     item->entry.next_hops.next_hop.push_back(item_nexthop);
     314        1453 : }
     315             : 
     316        1325 : void BgpXmppMessage::AddEnetReach(const BgpRoute *route,
     317             :                                   const RibOutAttr *roattr) {
     318        1325 :     if (!roattr->repr().empty()) {
     319           0 :         repr_ += roattr->repr();
     320           0 :         return;
     321             :     }
     322        1325 :     Address::Family family = table_->family();
     323             : 
     324        1325 :     autogen::EnetItemType item;
     325        1325 :     item.entry.nlri.af = BgpAf::FamilyToAfi(family);
     326        1325 :     item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
     327             : 
     328        1325 :     EvpnRoute *evpn_route =
     329             :         static_cast<EvpnRoute *>(const_cast<BgpRoute *>(route));
     330        1325 :     const EvpnPrefix &evpn_prefix = evpn_route->GetPrefix();
     331        1325 :     item.entry.nlri.ethernet_tag = evpn_prefix.tag();
     332        1325 :     item.entry.nlri.mac = evpn_prefix.mac_addr().ToString();
     333        2650 :     item.entry.nlri.address = evpn_prefix.ip_address().to_string() + "/" +
     334        3974 :         integerToString(evpn_prefix.ip_address_length());
     335        1325 :     item.entry.nlri.source = evpn_prefix.source().to_string();
     336        1325 :     item.entry.nlri.group = evpn_prefix.group().to_string();
     337             : 
     338        1325 :     item.entry.virtual_network = GetVirtualNetwork(route, roattr);
     339        1325 :     item.entry.local_preference = roattr->attr()->local_pref();
     340        1325 :     item.entry.med = roattr->attr()->med();
     341        1325 :     item.entry.sequence_number = mobility_.sequence_number;
     342        1325 :     item.entry.mobility.seqno = mobility_.sequence_number;
     343        1325 :     item.entry.mobility.sticky = mobility_.sticky;
     344        1325 :     item.entry.etree_leaf = etree_leaf_;
     345             : 
     346        1325 :     for (vector<int>::const_iterator it = security_group_list_.begin();
     347        2650 :          it != security_group_list_.end(); ++it) {
     348        1325 :         item.entry.security_group_list.security_group.push_back(*it);
     349             :     }
     350             : 
     351        1325 :     const BgpOList *olist = roattr->attr()->olist().get();
     352        1325 :     assert((olist == NULL) != roattr->nexthop_list().empty());
     353             : 
     354        1325 :     if (olist) {
     355           0 :         assert(olist->olist().subcode == BgpAttribute::OList);
     356           0 :         BOOST_FOREACH(const BgpOListElem *elem, olist->elements()) {
     357           0 :             autogen::EnetNextHopType nh;
     358           0 :             nh.af = BgpAf::IPv4;
     359           0 :             nh.address = elem->address.to_string();
     360           0 :             nh.label = elem->label;
     361           0 :             nh.tunnel_encapsulation_list.tunnel_encapsulation = elem->encap;
     362           0 :             item.entry.olist.next_hop.push_back(nh);
     363           0 :         }
     364             :     }
     365             : 
     366        1325 :     const BgpOList *leaf_olist = roattr->attr()->leaf_olist().get();
     367        1325 :     assert((leaf_olist == NULL) != roattr->nexthop_list().empty());
     368             : 
     369        1325 :     if (leaf_olist) {
     370           0 :         assert(leaf_olist->olist().subcode == BgpAttribute::LeafOList);
     371           0 :         BOOST_FOREACH(const BgpOListElem *elem, leaf_olist->elements()) {
     372           0 :             autogen::EnetNextHopType nh;
     373           0 :             nh.af = BgpAf::IPv4;
     374           0 :             nh.address = elem->address.to_string();
     375           0 :             nh.label = elem->label;
     376           0 :             nh.tunnel_encapsulation_list.tunnel_encapsulation = elem->encap;
     377           0 :             item.entry.leaf_olist.next_hop.push_back(nh);
     378           0 :         }
     379             :     }
     380             : 
     381        4231 :     BOOST_FOREACH(const RibOutAttr::NextHop &nexthop, roattr->nexthop_list()) {
     382        1453 :         EncodeEnetNextHop(route, nexthop, &item);
     383             :     }
     384             : 
     385        2266 :     for (const auto &peer_name : route->peer_sources()) {
     386         941 :         item.entry.peers.peer.push_back(peer_name);
     387             :     }
     388             : 
     389        1325 :     xml_node node = doc_.append_child("item");
     390        1325 :     node.append_attribute("id") = route->ToXmppIdString().c_str();
     391             : 
     392             :     // Remember the previous size.
     393             :     // Using remove_child instead of reset allows memory pages allocated for
     394             :     // the xml_document to be reused during the lifetime of the xml_document.
     395        1325 :     size_t pos = repr_.size();
     396        1325 :     item.Encode(&node);
     397        1325 :     doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
     398        1325 :     doc_.remove_child(node);
     399             : 
     400             :     // Cache the substring starting at the previous size.
     401        1325 :     if (cache_routes_)
     402           0 :         roattr->set_repr(repr_, pos);
     403        1325 : }
     404             : 
     405         384 : void BgpXmppMessage::AddEnetUnreach(const BgpRoute *route) {
     406         384 :     repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
     407         384 : }
     408             : 
     409        1709 : bool BgpXmppMessage::AddEnetRoute(const BgpRoute *route,
     410             :     const RibOutAttr *roattr) {
     411        1709 :     if (is_reachable_) {
     412        1325 :         num_reach_route_++;
     413        1325 :         AddEnetReach(route, roattr);
     414             :     } else {
     415         384 :         num_unreach_route_++;
     416         384 :         AddEnetUnreach(route);
     417             :     }
     418        1709 :     return true;
     419             : }
     420             : 
     421             : //
     422             : // Note that there's no need to cache the string representation since a given
     423             : // mcast route is sent to exactly one xmpp peer.
     424             : //
     425           0 : void BgpXmppMessage::AddMcastReach(const BgpRoute *route,
     426             :                                    const RibOutAttr *roattr) {
     427           0 :     Address::Family family = table_->family();
     428           0 :     autogen::McastItemType item;
     429           0 :     item.entry.nlri.af = BgpAf::FamilyToAfi(family);
     430           0 :     item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
     431             : 
     432           0 :     ErmVpnRoute *ermvpn_route =
     433             :         static_cast<ErmVpnRoute *>(const_cast<BgpRoute *>(route));
     434           0 :     item.entry.nlri.group = ermvpn_route->GetPrefix().group().to_string();
     435           0 :     item.entry.nlri.source =  ermvpn_route->GetPrefix().source().to_string();
     436           0 :     item.entry.nlri.source_label = roattr->label();
     437           0 :     if (!roattr->source_address().is_unspecified()) {
     438             :         item.entry.nlri.source_address =
     439           0 :            roattr->source_address().to_string();
     440             :     }
     441             : 
     442           0 :     const BgpOList *olist = roattr->attr()->olist().get();
     443           0 :     assert(olist->olist().subcode == BgpAttribute::OList);
     444           0 :     BOOST_FOREACH(const BgpOListElem *elem, olist->elements()) {
     445           0 :         autogen::McastNextHopType nh;
     446           0 :         nh.af = BgpAf::IPv4;
     447           0 :         nh.address = elem->address.to_string();
     448           0 :         nh.label = integerToString(elem->label);
     449           0 :         nh.tunnel_encapsulation_list.tunnel_encapsulation = elem->encap;
     450           0 :         item.entry.olist.next_hop.push_back(nh);
     451           0 :     }
     452             : 
     453             :     // Using remove_child instead of reset allows memory pages allocated for
     454             :     // the xml_document to be reused during the lifetime of the xml_document.
     455           0 :     xml_node node = doc_.append_child("item");
     456           0 :     node.append_attribute("id") = route->ToXmppIdString().c_str();
     457           0 :     item.Encode(&node);
     458           0 :     doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
     459           0 :     doc_.remove_child(node);
     460           0 : }
     461             : 
     462           0 : void BgpXmppMessage::AddMcastUnreach(const BgpRoute *route) {
     463           0 :     repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
     464           0 : }
     465             : 
     466           0 : bool BgpXmppMessage::AddMcastRoute(const BgpRoute *route,
     467             :                                    const RibOutAttr *roattr) {
     468           0 :     if (is_reachable_) {
     469           0 :         num_reach_route_++;
     470           0 :         AddMcastReach(route, roattr);
     471             :     } else {
     472           0 :         num_unreach_route_++;
     473           0 :         AddMcastUnreach(route);
     474             :     }
     475           0 :     return true;
     476             : }
     477             : 
     478           0 : void BgpXmppMessage::AddMvpnReach(const BgpRoute *route,
     479             :                                   const RibOutAttr *roattr) {
     480           0 :     Address::Family family = table_->family();
     481           0 :     autogen::MvpnItemType item;
     482           0 :     item.entry.nlri.af = BgpAf::FamilyToAfi(family);
     483           0 :     item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
     484             : 
     485           0 :     MvpnRoute *mvpn_route =
     486             :         static_cast<MvpnRoute *>(const_cast<BgpRoute *>(route));
     487           0 :     item.entry.nlri.group = mvpn_route->GetPrefix().group().to_string();
     488           0 :     item.entry.nlri.source =  mvpn_route->GetPrefix().source().to_string();
     489           0 :     item.entry.nlri.route_type = mvpn_route->GetPrefix().type();
     490           0 :     assert((item.entry.nlri.route_type == MvpnPrefix::SourceActiveADRoute) ||
     491             :             (item.entry.nlri.route_type == MvpnPrefix::SourceTreeJoinRoute));
     492             : 
     493           0 :     if (item.entry.nlri.route_type == MvpnPrefix::SourceActiveADRoute) {
     494           0 :         const BgpOList *olist = roattr->attr()->olist().get();
     495           0 :         assert(olist->olist().subcode == BgpAttribute::OList);
     496           0 :         BOOST_FOREACH(const BgpOListElem *elem, olist->elements()) {
     497           0 :             autogen::MvpnNextHopType nh;
     498           0 :             nh.af = BgpAf::IPv4;
     499           0 :             nh.address = elem->address.to_string();
     500           0 :             nh.label = elem->label;
     501           0 :             nh.tunnel_encapsulation_list.tunnel_encapsulation = elem->encap;
     502           0 :             item.entry.olist.next_hop.push_back(nh);
     503           0 :         }
     504             :     }
     505             : 
     506             :     // Using remove_child instead of reset allows memory pages allocated for
     507             :     // the xml_document to be reused during the lifetime of the xml_document.
     508           0 :     xml_node node = doc_.append_child("item");
     509           0 :     node.append_attribute("id") = route->ToXmppIdString().c_str();
     510           0 :     item.Encode(&node);
     511           0 :     doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
     512           0 :     doc_.remove_child(node);
     513           0 : }
     514             : 
     515           0 : void BgpXmppMessage::AddMvpnUnreach(const BgpRoute *route) {
     516           0 :     repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
     517           0 : }
     518             : 
     519           0 : bool BgpXmppMessage::AddMvpnRoute(const BgpRoute *route,
     520             :                                    const RibOutAttr *roattr) {
     521           0 :     if (is_reachable_) {
     522           0 :         num_reach_route_++;
     523           0 :         AddMvpnReach(route, roattr);
     524             :     } else {
     525           0 :         num_unreach_route_++;
     526           0 :         AddMvpnUnreach(route);
     527             :     }
     528           0 :     return true;
     529             : }
     530             : 
     531        3096 : const uint8_t *BgpXmppMessage::GetData(IPeerUpdate *peer, size_t *lenp,
     532             :     const string **msg_str, string *temp) {
     533             :     // Build begin line that contains message opening tag with from and to
     534             :     // attributes.
     535        3096 :     msg_begin_.clear();
     536        3096 :     msg_begin_ += "\n<message from=\"";
     537        3096 :     msg_begin_ += XmppInit::kControlNodeJID;
     538        3096 :     msg_begin_ += "\" to=\"";
     539        3096 :     msg_begin_ += peer->ToString();
     540        3096 :     msg_begin_ += "/";
     541        3096 :     msg_begin_ += XmppInit::kBgpPeer;
     542        3096 :     msg_begin_ += "\">";
     543             : 
     544             :     // Add closing tags if this is the first peer to which the message will
     545             :     // be sent.
     546        3096 :     if (!repr_valid_) {
     547        3096 :         repr_ += "\t\t</items>\n\t</event>\n</message>\n";
     548        3096 :         repr_valid_ = true;
     549             :     }
     550             : 
     551             :     // Replace the begin line if it fits in the space reserved at the start
     552             :     // of repr_. Use fill and copy instead of string::replace as the latter
     553             :     // seems to construct a new temporary string to hold the input data to
     554             :     // be copied.
     555             :     // Otherwise build a new string with the begin line and the rest of the
     556             :     // message in repr_.
     557        3096 :     size_t begin_size = msg_begin_.size();
     558        3096 :     if (begin_size <= kMaxFromToLength) {
     559        3096 :         size_t extra = kMaxFromToLength - begin_size;
     560        3096 :         char *data = const_cast<char *>(repr_.c_str());
     561        3096 :         fill(data, data + extra, ' ');
     562        3096 :         copy(msg_begin_.c_str(), msg_begin_.c_str() + begin_size, data + extra);
     563        3094 :         *lenp = repr_.size() - extra;
     564        3094 :         *msg_str = &repr_;
     565        3094 :         return reinterpret_cast<const uint8_t *>(repr_.c_str()) + extra;
     566             :     } else {
     567           0 :         *temp = msg_begin_ + string(repr_, kMaxFromToLength);
     568           0 :         *lenp = temp->size();
     569           0 :         *msg_str = NULL;
     570           0 :         return reinterpret_cast<const uint8_t *>(temp->c_str());
     571             :     }
     572             : }
     573             : 
     574        2595 : void BgpXmppMessage::ProcessCommunity(const Community *community) {
     575        2595 :     community_list_.clear();
     576        2596 :     if (community == NULL)
     577        2596 :         return;
     578           0 :     BOOST_FOREACH(uint32_t value, community->communities()) {
     579           0 :         community_list_.push_back(CommunityType::CommunityToString(value));
     580             :     }
     581             : }
     582             : 
     583        2595 : void BgpXmppMessage::ProcessExtCommunity(const ExtCommunity *ext_community) {
     584        2595 :     mobility_.sequence_number = 0;
     585        2595 :     mobility_.sticky = false;
     586        2595 :     etree_leaf_ = false;
     587        2595 :     security_group_list_.clear();
     588        2595 :     load_balance_attribute_ = LoadBalance::LoadBalanceAttribute();
     589        2596 :     if (ext_community == NULL)
     590           0 :         return;
     591             : 
     592        2596 :     as_t as_number =  table_->server()->autonomous_system();
     593        2595 :     bool sg_asn_match = true;
     594        2595 :     for (ExtCommunity::ExtCommunityList::const_iterator iter =
     595        2595 :         ext_community->communities().begin();
     596        9270 :         iter != ext_community->communities().end(); ++iter) {
     597        6675 :         if (ExtCommunity::is_security_group(*iter)) {
     598        2595 :             SecurityGroup sg(*iter);
     599        2595 :             if (as_number <= AS2_MAX)
     600        2595 :                 if (sg.as_number() != as_number && !sg.IsGlobal())
     601           0 :                     continue;
     602        2596 :             security_group_list_.push_back(sg.security_group_id());
     603        4080 :         } else if (ExtCommunity::is_security_group4(*iter)) {
     604           0 :             SecurityGroup4ByteAs sg(*iter);
     605           0 :             if (sg.as_number() != as_number)
     606           0 :                 sg_asn_match = false;
     607        4080 :         } else if (ExtCommunity::is_mac_mobility(*iter)) {
     608           0 :             MacMobility mm(*iter);
     609           0 :             mobility_.sequence_number = mm.sequence_number();
     610           0 :             mobility_.sticky = mm.sticky();
     611        4080 :         } else if (ExtCommunity::is_load_balance(*iter)) {
     612           0 :             LoadBalance load_balance(*iter);
     613           0 :             load_balance.FillAttribute(&load_balance_attribute_);
     614        4080 :         } else if (ExtCommunity::is_etree(*iter)) {
     615         160 :             ETree etree(*iter);
     616         160 :             etree_leaf_ = etree.leaf();
     617             :         }
     618             :     }
     619        2595 :     if (!sg_asn_match)
     620           0 :         security_group_list_.clear();
     621             : }
     622             : 
     623        3995 : string BgpXmppMessage::GetVirtualNetwork(
     624             :     const RibOutAttr::NextHop &nexthop) const {
     625        3995 :     int index = nexthop.origin_vn_index();
     626        3995 :     if (index > 0) {
     627             :         const RoutingInstanceMgr *manager =
     628           0 :             table_->routing_instance()->manager();
     629           0 :         return manager->GetVirtualNetworkByVnIndex(index);
     630        3995 :     } else if (index == 0) {
     631        3995 :         return table_->routing_instance()->GetVirtualNetworkName();
     632             :     } else {
     633           0 :         return "unresolved";
     634             :     }
     635             : }
     636             : 
     637        2596 : string BgpXmppMessage::GetVirtualNetwork(const BgpRoute *route,
     638             :     const RibOutAttr *roattr) const {
     639        2596 :     if (!is_reachable_) {
     640           0 :         return "unresolved";
     641        2596 :     } else if (roattr->nexthop_list().empty()) {
     642           0 :         if (roattr->vrf_originated()) {
     643           0 :             return table_->routing_instance()->GetVirtualNetworkName();
     644             :         } else {
     645           0 :             return "unresolved";
     646             :         }
     647             :     } else {
     648        2596 :         return GetVirtualNetwork(roattr->nexthop_list().front());
     649             :     }
     650             : }
     651             : 
     652           1 : BgpXmppMessageBuilder::BgpXmppMessageBuilder() {
     653           1 : }
     654             : 
     655           4 : Message *BgpXmppMessageBuilder::Create() const {
     656           4 :     return new BgpXmppMessage;
     657             : }

Generated by: LCOV version 1.14