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 477539 : static inline const char *AfiName(uint16_t afi) {
40 477539 : switch (afi) {
41 270282 : case BgpAf::IPv4:
42 270282 : return "1";
43 : break;
44 196838 : case BgpAf::IPv6:
45 196838 : return "2";
46 : break;
47 10439 : case BgpAf::L2Vpn:
48 10439 : return "25";
49 : break;
50 : }
51 0 : assert(false);
52 : return NULL;
53 : }
54 :
55 477561 : static inline const char *XmppSafiName(uint8_t safi) {
56 477561 : switch (safi) {
57 457973 : case BgpAf::Unicast:
58 457973 : return "1";
59 : break;
60 11 : case BgpAf::Mpls:
61 11 : return "4";
62 : break;
63 6478 : case BgpAf::MVpn:
64 6478 : return "5";
65 : break;
66 2667 : case BgpAf::Mcast:
67 2667 : return "241";
68 : break;
69 10439 : case BgpAf::Enet:
70 10439 : return "242";
71 : break;
72 : }
73 0 : assert(false);
74 : return NULL;
75 : }
76 :
77 143 : BgpXmppMessage::BgpXmppMessage()
78 144 : : table_(NULL),
79 144 : writer_(XmlWriter(&repr_)),
80 144 : is_reachable_(false),
81 144 : cache_routes_(false),
82 144 : repr_valid_(false),
83 144 : mobility_(0, false),
84 287 : etree_leaf_(false) {
85 144 : msg_begin_.reserve(kMaxFromToLength);
86 144 : }
87 :
88 288 : BgpXmppMessage::~BgpXmppMessage() {
89 288 : }
90 :
91 477480 : void BgpXmppMessage::Reset() {
92 477480 : Message::Reset();
93 477479 : table_ = NULL;
94 477479 : is_reachable_ = false;
95 477479 : cache_routes_ = false;
96 477479 : repr_valid_ = false;
97 477479 : repr_.clear();
98 477478 : }
99 :
100 477482 : bool BgpXmppMessage::Start(const RibOut *ribout, bool cache_routes,
101 : const RibOutAttr *roattr, const BgpRoute *route) {
102 477482 : Reset();
103 477476 : table_ = ribout->table();
104 477469 : is_reachable_ = roattr->IsReachable();
105 477464 : cache_routes_ = cache_routes;
106 477464 : Address::Family family = table_->family();
107 :
108 477461 : if (is_reachable_) {
109 318255 : const BgpAttr *attr = roattr->attr();
110 318242 : ProcessCommunity(attr->community());
111 318234 : 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 477468 : repr_.append(kMaxFromToLength, ' ');
118 :
119 : // Add opening tags for event and items. The closing tags are added when
120 : // GetData is called.
121 477491 : repr_ += "\n\t<event xmlns=\"http://jabber.org/protocol/pubsub\">";
122 477516 : repr_ += "\n\t\t<items node=\"";
123 477545 : repr_ += AfiName(BgpAf::FamilyToAfi(family));
124 477554 : repr_ += "/";
125 477574 : repr_ += XmppSafiName(BgpAf::FamilyToXmppSafi(family));
126 477561 : repr_ += "/";
127 477561 : repr_ += table_->routing_instance()->name();
128 477549 : repr_ += "\">\n";
129 :
130 477551 : if (table_->family() == Address::ERMVPN) {
131 2667 : AddMcastRoute(route, roattr);
132 474882 : } else if (table_->family() == Address::EVPN) {
133 10439 : AddEnetRoute(route, roattr);
134 464434 : } else if (table_->family() == Address::INET6) {
135 196842 : AddInet6Route(route, roattr);
136 267594 : } else if (table_->family() == Address::MVPN) {
137 6478 : AddMvpnRoute(route, roattr);
138 : } else {
139 261129 : AddInetRoute(route, roattr);
140 : }
141 477567 : return true;
142 : }
143 :
144 477601 : void BgpXmppMessage::Finish() {
145 477601 : }
146 :
147 610942 : bool BgpXmppMessage::AddRoute(const BgpRoute *route, const RibOutAttr *roattr) {
148 610942 : assert(is_reachable_ == roattr->IsReachable());
149 610941 : if (is_reachable_ && num_reach_route_ >= kMaxReachCount)
150 244 : return false;
151 610697 : if (!is_reachable_ && num_unreach_route_ >= kMaxUnreachCount)
152 8 : return false;
153 :
154 610689 : if (is_reachable_) {
155 517796 : const BgpAttr *attr = roattr->attr();
156 517794 : ProcessCommunity(attr->community());
157 517792 : ProcessExtCommunity(attr->ext_community());
158 : }
159 :
160 610709 : if (table_->family() == Address::ERMVPN) {
161 129 : return AddMcastRoute(route, roattr);
162 610579 : } else if (table_->family() == Address::EVPN) {
163 2091 : return AddEnetRoute(route, roattr);
164 608488 : } else if (table_->family() == Address::INET6) {
165 122747 : return AddInet6Route(route, roattr);
166 485739 : } else if (table_->family() == Address::MVPN) {
167 190464 : return AddMvpnRoute(route, roattr);
168 : } else {
169 295307 : return AddInetRoute(route, roattr);
170 : }
171 : }
172 :
173 530389 : void BgpXmppMessage::EncodeNextHop(const BgpRoute *route,
174 : const RibOutAttr::NextHop &nexthop,
175 : autogen::ItemType *item) {
176 530389 : autogen::NextHopType item_nexthop;
177 :
178 530374 : const IpAddress &address = nexthop.address();
179 530373 : if (address.is_v4()) {
180 527058 : item_nexthop.af = BgpAf::IPv4;
181 527058 : item_nexthop.address = address.to_v4().to_string();
182 : } else {
183 3306 : item_nexthop.af = BgpAf::IPv6;
184 3306 : item_nexthop.address = address.to_v6().to_string();
185 : }
186 530643 : item_nexthop.label = nexthop.label();
187 530639 : item_nexthop.virtual_network = GetVirtualNetwork(nexthop);
188 530625 : item_nexthop.tag_list.tag = nexthop.tag_list();
189 530461 : 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 530461 : if (item_nexthop.label) {
194 521472 : vector<string> &encap_list =
195 : item_nexthop.tunnel_encapsulation_list.tunnel_encapsulation;
196 521472 : if (nexthop.encap().empty()) {
197 100622 : encap_list.push_back(string("gre"));
198 : } else {
199 420833 : encap_list = nexthop.encap();
200 : }
201 : }
202 :
203 530392 : item->entry.next_hops.next_hop.push_back(item_nexthop);
204 530445 : }
205 :
206 626415 : void BgpXmppMessage::AddIpReach(const BgpRoute *route,
207 : const RibOutAttr *roattr) {
208 626415 : if (!roattr->repr().empty()) {
209 98208 : repr_ += roattr->repr();
210 98208 : return;
211 : }
212 528202 : Address::Family family = table_->family();
213 :
214 528200 : autogen::ItemType item;
215 528141 : item.entry.nlri.af = BgpAf::FamilyToAfi(family);
216 528137 : item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
217 528137 : item.entry.nlri.address = route->ToString();
218 528204 : item.entry.version = 1;
219 528204 : item.entry.virtual_network = GetVirtualNetwork(route, roattr);
220 528211 : item.entry.local_preference = roattr->attr()->local_pref();
221 528196 : item.entry.med = roattr->attr()->med();
222 528177 : item.entry.sequence_number = mobility_.sequence_number;
223 528177 : item.entry.mobility.seqno = mobility_.sequence_number;
224 528177 : item.entry.mobility.sticky = mobility_.sticky;
225 :
226 528177 : assert(!roattr->nexthop_list().empty());
227 :
228 : // Encode all next-hops in the list.
229 1588987 : BOOST_FOREACH(const RibOutAttr::NextHop &nexthop, roattr->nexthop_list()) {
230 530395 : EncodeNextHop(route, nexthop, &item);
231 : }
232 :
233 528082 : for (vector<int>::const_iterator it = security_group_list_.begin();
234 850591 : it != security_group_list_.end(); ++it) {
235 322553 : item.entry.security_group_list.security_group.push_back(*it);
236 : }
237 :
238 528045 : for (vector<string>::const_iterator it = community_list_.begin();
239 539893 : it != community_list_.end(); ++it) {
240 11866 : item.entry.community_tag_list.community_tag.push_back(*it);
241 : }
242 :
243 : // Encode load balance attribute.
244 528015 : if (!load_balance_attribute_.IsDefault())
245 4 : load_balance_attribute_.Encode(&item.entry.load_balance);
246 :
247 528010 : xml_node node = doc_.append_child("item");
248 527960 : 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 528083 : size_t pos = repr_.size();
254 528081 : item.Encode(&node);
255 528000 : doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
256 528206 : doc_.remove_child(node);
257 :
258 : // Cache the substring starting at the previous size.
259 528172 : if (cache_routes_)
260 29114 : roattr->set_repr(repr_, pos);
261 528178 : }
262 :
263 249551 : void BgpXmppMessage::AddIpUnreach(const BgpRoute *route) {
264 249551 : repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
265 249648 : }
266 :
267 556425 : bool BgpXmppMessage::AddInetRoute(const BgpRoute *route,
268 : const RibOutAttr *roattr) {
269 556425 : if (is_reachable_) {
270 424367 : num_reach_route_++;
271 424367 : AddIpReach(route, roattr);
272 : } else {
273 132058 : num_unreach_route_++;
274 132058 : AddIpUnreach(route);
275 : }
276 556460 : return true;
277 : }
278 :
279 319576 : bool BgpXmppMessage::AddInet6Route(const BgpRoute *route,
280 : const RibOutAttr *roattr) {
281 319576 : if (is_reachable_) {
282 202058 : num_reach_route_++;
283 202058 : AddIpReach(route, roattr);
284 : } else {
285 117518 : num_unreach_route_++;
286 117518 : AddIpUnreach(route);
287 : }
288 319596 : return true;
289 : }
290 :
291 9097 : void BgpXmppMessage::EncodeEnetNextHop(const BgpRoute *route,
292 : const RibOutAttr::NextHop &nexthop,
293 : autogen::EnetItemType *item) {
294 9097 : autogen::EnetNextHopType item_nexthop;
295 :
296 9097 : item_nexthop.af = BgpAf::IPv4;
297 9097 : item_nexthop.address = nexthop.address().to_v4().to_string();
298 9097 : item_nexthop.label = nexthop.label();
299 9097 : item_nexthop.l3_label = nexthop.l3_label();
300 9097 : if (!nexthop.mac().IsZero())
301 20 : item_nexthop.mac = nexthop.mac().ToString();
302 :
303 : // If encap list is empty use mpls over gre as default encap.
304 9097 : vector<string> &encap_list =
305 : item_nexthop.tunnel_encapsulation_list.tunnel_encapsulation;
306 9097 : if (nexthop.encap().empty()) {
307 0 : encap_list.push_back(string("gre"));
308 : } else {
309 9097 : encap_list = nexthop.encap();
310 : }
311 9097 : item_nexthop.tag_list.tag = nexthop.tag_list();
312 9097 : item_nexthop.is_new_tags_list = true;
313 9097 : item->entry.next_hops.next_hop.push_back(item_nexthop);
314 9097 : }
315 :
316 10542 : void BgpXmppMessage::AddEnetReach(const BgpRoute *route,
317 : const RibOutAttr *roattr) {
318 10542 : if (!roattr->repr().empty()) {
319 0 : repr_ += roattr->repr();
320 0 : return;
321 : }
322 10542 : Address::Family family = table_->family();
323 :
324 10542 : autogen::EnetItemType item;
325 10542 : item.entry.nlri.af = BgpAf::FamilyToAfi(family);
326 10542 : item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
327 :
328 10542 : EvpnRoute *evpn_route =
329 : static_cast<EvpnRoute *>(const_cast<BgpRoute *>(route));
330 10542 : const EvpnPrefix &evpn_prefix = evpn_route->GetPrefix();
331 10542 : item.entry.nlri.ethernet_tag = evpn_prefix.tag();
332 10542 : item.entry.nlri.mac = evpn_prefix.mac_addr().ToString();
333 21084 : item.entry.nlri.address = evpn_prefix.ip_address().to_string() + "/" +
334 31626 : integerToString(evpn_prefix.ip_address_length());
335 10542 : item.entry.nlri.source = evpn_prefix.source().to_string();
336 10542 : item.entry.nlri.group = evpn_prefix.group().to_string();
337 :
338 10542 : item.entry.virtual_network = GetVirtualNetwork(route, roattr);
339 10542 : item.entry.local_preference = roattr->attr()->local_pref();
340 10542 : item.entry.med = roattr->attr()->med();
341 10542 : item.entry.sequence_number = mobility_.sequence_number;
342 10542 : item.entry.mobility.seqno = mobility_.sequence_number;
343 10542 : item.entry.mobility.sticky = mobility_.sticky;
344 10542 : item.entry.etree_leaf = etree_leaf_;
345 :
346 10542 : for (vector<int>::const_iterator it = security_group_list_.begin();
347 21092 : it != security_group_list_.end(); ++it) {
348 10550 : item.entry.security_group_list.security_group.push_back(*it);
349 : }
350 :
351 10542 : const BgpOList *olist = roattr->attr()->olist().get();
352 10542 : assert((olist == NULL) != roattr->nexthop_list().empty());
353 :
354 10542 : if (olist) {
355 1573 : assert(olist->olist().subcode == BgpAttribute::OList);
356 14883 : BOOST_FOREACH(const BgpOListElem *elem, olist->elements()) {
357 6655 : autogen::EnetNextHopType nh;
358 6655 : nh.af = BgpAf::IPv4;
359 6655 : nh.address = elem->address.to_string();
360 6655 : nh.label = elem->label;
361 6655 : nh.tunnel_encapsulation_list.tunnel_encapsulation = elem->encap;
362 6655 : item.entry.olist.next_hop.push_back(nh);
363 6655 : }
364 : }
365 :
366 10542 : const BgpOList *leaf_olist = roattr->attr()->leaf_olist().get();
367 10542 : assert((leaf_olist == NULL) != roattr->nexthop_list().empty());
368 :
369 10542 : if (leaf_olist) {
370 1573 : assert(leaf_olist->olist().subcode == BgpAttribute::LeafOList);
371 1573 : 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 28736 : BOOST_FOREACH(const RibOutAttr::NextHop &nexthop, roattr->nexthop_list()) {
382 9097 : EncodeEnetNextHop(route, nexthop, &item);
383 : }
384 :
385 11459 : for (const auto &peer_name : route->peer_sources()) {
386 917 : item.entry.peers.peer.push_back(peer_name);
387 : }
388 :
389 10542 : xml_node node = doc_.append_child("item");
390 10542 : 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 10542 : size_t pos = repr_.size();
396 10542 : item.Encode(&node);
397 10542 : doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
398 10542 : doc_.remove_child(node);
399 :
400 : // Cache the substring starting at the previous size.
401 10542 : if (cache_routes_)
402 1983 : roattr->set_repr(repr_, pos);
403 10542 : }
404 :
405 1988 : void BgpXmppMessage::AddEnetUnreach(const BgpRoute *route) {
406 1988 : repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
407 1988 : }
408 :
409 12530 : bool BgpXmppMessage::AddEnetRoute(const BgpRoute *route,
410 : const RibOutAttr *roattr) {
411 12530 : if (is_reachable_) {
412 10542 : num_reach_route_++;
413 10542 : AddEnetReach(route, roattr);
414 : } else {
415 1988 : num_unreach_route_++;
416 1988 : AddEnetUnreach(route);
417 : }
418 12530 : 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 2336 : void BgpXmppMessage::AddMcastReach(const BgpRoute *route,
426 : const RibOutAttr *roattr) {
427 2336 : Address::Family family = table_->family();
428 2336 : autogen::McastItemType item;
429 2336 : item.entry.nlri.af = BgpAf::FamilyToAfi(family);
430 2336 : item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
431 :
432 2336 : ErmVpnRoute *ermvpn_route =
433 : static_cast<ErmVpnRoute *>(const_cast<BgpRoute *>(route));
434 2336 : item.entry.nlri.group = ermvpn_route->GetPrefix().group().to_string();
435 2336 : item.entry.nlri.source = ermvpn_route->GetPrefix().source().to_string();
436 2336 : item.entry.nlri.source_label = roattr->label();
437 2336 : if (!roattr->source_address().is_unspecified()) {
438 : item.entry.nlri.source_address =
439 146 : roattr->source_address().to_string();
440 : }
441 :
442 2336 : const BgpOList *olist = roattr->attr()->olist().get();
443 2336 : assert(olist->olist().subcode == BgpAttribute::OList);
444 7362 : BOOST_FOREACH(const BgpOListElem *elem, olist->elements()) {
445 2513 : autogen::McastNextHopType nh;
446 2513 : nh.af = BgpAf::IPv4;
447 2513 : nh.address = elem->address.to_string();
448 2513 : nh.label = integerToString(elem->label);
449 2513 : nh.tunnel_encapsulation_list.tunnel_encapsulation = elem->encap;
450 2513 : item.entry.olist.next_hop.push_back(nh);
451 2513 : }
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 2336 : xml_node node = doc_.append_child("item");
456 2336 : node.append_attribute("id") = route->ToXmppIdString().c_str();
457 2336 : item.Encode(&node);
458 2336 : doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
459 2336 : doc_.remove_child(node);
460 2336 : }
461 :
462 460 : void BgpXmppMessage::AddMcastUnreach(const BgpRoute *route) {
463 460 : repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
464 460 : }
465 :
466 2796 : bool BgpXmppMessage::AddMcastRoute(const BgpRoute *route,
467 : const RibOutAttr *roattr) {
468 2796 : if (is_reachable_) {
469 2336 : num_reach_route_++;
470 2336 : AddMcastReach(route, roattr);
471 : } else {
472 460 : num_unreach_route_++;
473 460 : AddMcastUnreach(route);
474 : }
475 2796 : return true;
476 : }
477 :
478 196816 : void BgpXmppMessage::AddMvpnReach(const BgpRoute *route,
479 : const RibOutAttr *roattr) {
480 196816 : Address::Family family = table_->family();
481 196816 : autogen::MvpnItemType item;
482 196816 : item.entry.nlri.af = BgpAf::FamilyToAfi(family);
483 196816 : item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
484 :
485 196816 : MvpnRoute *mvpn_route =
486 : static_cast<MvpnRoute *>(const_cast<BgpRoute *>(route));
487 196816 : item.entry.nlri.group = mvpn_route->GetPrefix().group().to_string();
488 196816 : item.entry.nlri.source = mvpn_route->GetPrefix().source().to_string();
489 196816 : item.entry.nlri.route_type = mvpn_route->GetPrefix().type();
490 196816 : assert((item.entry.nlri.route_type == MvpnPrefix::SourceActiveADRoute) ||
491 : (item.entry.nlri.route_type == MvpnPrefix::SourceTreeJoinRoute));
492 :
493 196816 : if (item.entry.nlri.route_type == MvpnPrefix::SourceActiveADRoute) {
494 196722 : const BgpOList *olist = roattr->attr()->olist().get();
495 196722 : assert(olist->olist().subcode == BgpAttribute::OList);
496 590166 : BOOST_FOREACH(const BgpOListElem *elem, olist->elements()) {
497 196722 : autogen::MvpnNextHopType nh;
498 196722 : nh.af = BgpAf::IPv4;
499 196722 : nh.address = elem->address.to_string();
500 196722 : nh.label = elem->label;
501 196722 : nh.tunnel_encapsulation_list.tunnel_encapsulation = elem->encap;
502 196722 : item.entry.olist.next_hop.push_back(nh);
503 196722 : }
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 196816 : xml_node node = doc_.append_child("item");
509 196816 : node.append_attribute("id") = route->ToXmppIdString().c_str();
510 196816 : item.Encode(&node);
511 196816 : doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
512 196816 : doc_.remove_child(node);
513 196816 : }
514 :
515 126 : void BgpXmppMessage::AddMvpnUnreach(const BgpRoute *route) {
516 126 : repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
517 126 : }
518 :
519 196942 : bool BgpXmppMessage::AddMvpnRoute(const BgpRoute *route,
520 : const RibOutAttr *roattr) {
521 196942 : if (is_reachable_) {
522 196816 : num_reach_route_++;
523 196816 : AddMvpnReach(route, roattr);
524 : } else {
525 126 : num_unreach_route_++;
526 126 : AddMvpnUnreach(route);
527 : }
528 196942 : return true;
529 : }
530 :
531 14177446 : 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 14177446 : msg_begin_.clear();
536 14177487 : msg_begin_ += "\n<message from=\"";
537 14177554 : msg_begin_ += XmppInit::kControlNodeJID;
538 14177600 : msg_begin_ += "\" to=\"";
539 14177698 : msg_begin_ += peer->ToString();
540 14177762 : msg_begin_ += "/";
541 14177775 : msg_begin_ += XmppInit::kBgpPeer;
542 14177725 : msg_begin_ += "\">";
543 :
544 : // Add closing tags if this is the first peer to which the message will
545 : // be sent.
546 14177745 : if (!repr_valid_) {
547 477614 : repr_ += "\t\t</items>\n\t</event>\n</message>\n";
548 477610 : 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 14177741 : size_t begin_size = msg_begin_.size();
558 14177667 : if (begin_size <= kMaxFromToLength) {
559 9983403 : size_t extra = kMaxFromToLength - begin_size;
560 9983403 : char *data = const_cast<char *>(repr_.c_str());
561 9983409 : fill(data, data + extra, ' ');
562 9983389 : copy(msg_begin_.c_str(), msg_begin_.c_str() + begin_size, data + extra);
563 9983238 : *lenp = repr_.size() - extra;
564 9983228 : *msg_str = &repr_;
565 9983228 : return reinterpret_cast<const uint8_t *>(repr_.c_str()) + extra;
566 : } else {
567 4194264 : *temp = msg_begin_ + string(repr_, kMaxFromToLength);
568 4194304 : *lenp = temp->size();
569 4194304 : *msg_str = NULL;
570 4194304 : return reinterpret_cast<const uint8_t *>(temp->c_str());
571 : }
572 : }
573 :
574 836016 : void BgpXmppMessage::ProcessCommunity(const Community *community) {
575 836016 : community_list_.clear();
576 836015 : if (community == NULL)
577 824163 : return;
578 35597 : BOOST_FOREACH(uint32_t value, community->communities()) {
579 11874 : community_list_.push_back(CommunityType::CommunityToString(value));
580 : }
581 : }
582 :
583 836007 : void BgpXmppMessage::ProcessExtCommunity(const ExtCommunity *ext_community) {
584 836007 : mobility_.sequence_number = 0;
585 836007 : mobility_.sticky = false;
586 836007 : etree_leaf_ = false;
587 836007 : security_group_list_.clear();
588 835999 : load_balance_attribute_ = LoadBalance::LoadBalanceAttribute();
589 836178 : if (ext_community == NULL)
590 208044 : return;
591 :
592 628134 : as_t as_number = table_->server()->autonomous_system();
593 628108 : bool sg_asn_match = true;
594 628083 : for (ExtCommunity::ExtCommunityList::const_iterator iter =
595 628108 : ext_community->communities().begin();
596 3685941 : iter != ext_community->communities().end(); ++iter) {
597 3057868 : if (ExtCommunity::is_security_group(*iter)) {
598 431446 : SecurityGroup sg(*iter);
599 431445 : if (as_number <= AS2_MAX)
600 234837 : if (sg.as_number() != as_number && !sg.IsGlobal())
601 2 : continue;
602 431452 : security_group_list_.push_back(sg.security_group_id());
603 2626504 : } else if (ExtCommunity::is_security_group4(*iter)) {
604 196608 : SecurityGroup4ByteAs sg(*iter);
605 196608 : if (sg.as_number() != as_number)
606 0 : sg_asn_match = false;
607 2429898 : } else if (ExtCommunity::is_mac_mobility(*iter)) {
608 1080 : MacMobility mm(*iter);
609 1080 : mobility_.sequence_number = mm.sequence_number();
610 1080 : mobility_.sticky = mm.sticky();
611 2428782 : } else if (ExtCommunity::is_load_balance(*iter)) {
612 4 : LoadBalance load_balance(*iter);
613 4 : load_balance.FillAttribute(&load_balance_attribute_);
614 2428692 : } else if (ExtCommunity::is_etree(*iter)) {
615 9401 : ETree etree(*iter);
616 9401 : etree_leaf_ = etree.leaf();
617 : }
618 : }
619 628010 : if (!sg_asn_match)
620 0 : security_group_list_.clear();
621 : }
622 :
623 1067672 : string BgpXmppMessage::GetVirtualNetwork(
624 : const RibOutAttr::NextHop &nexthop) const {
625 1067672 : int index = nexthop.origin_vn_index();
626 1067653 : if (index > 0) {
627 : const RoutingInstanceMgr *manager =
628 68981 : table_->routing_instance()->manager();
629 68989 : return manager->GetVirtualNetworkByVnIndex(index);
630 998672 : } else if (index == 0) {
631 61206 : return table_->routing_instance()->GetVirtualNetworkName();
632 : } else {
633 937466 : return "unresolved";
634 : }
635 : }
636 :
637 538737 : string BgpXmppMessage::GetVirtualNetwork(const BgpRoute *route,
638 : const RibOutAttr *roattr) const {
639 538737 : if (!is_reachable_) {
640 0 : return "unresolved";
641 538737 : } else if (roattr->nexthop_list().empty()) {
642 1573 : if (roattr->vrf_originated()) {
643 1573 : return table_->routing_instance()->GetVirtualNetworkName();
644 : } else {
645 0 : return "unresolved";
646 : }
647 : } else {
648 537107 : return GetVirtualNetwork(roattr->nexthop_list().front());
649 : }
650 : }
651 :
652 50 : BgpXmppMessageBuilder::BgpXmppMessageBuilder() {
653 52 : }
654 :
655 144 : Message *BgpXmppMessageBuilder::Create() const {
656 144 : return new BgpXmppMessage;
657 : }
|