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 32224 : static inline const char *AfiName(uint16_t afi) {
40 32224 : switch (afi) {
41 17676 : case BgpAf::IPv4:
42 17676 : return "1";
43 : break;
44 14154 : case BgpAf::IPv6:
45 14154 : return "2";
46 : break;
47 398 : case BgpAf::L2Vpn:
48 398 : return "25";
49 : break;
50 : }
51 0 : assert(false);
52 : return NULL;
53 : }
54 :
55 32233 : static inline const char *XmppSafiName(uint8_t safi) {
56 32233 : switch (safi) {
57 31836 : case BgpAf::Unicast:
58 31836 : 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 398 : case BgpAf::Enet:
70 398 : 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 32202 : void BgpXmppMessage::Reset() {
92 32202 : Message::Reset();
93 32202 : table_ = NULL;
94 32202 : is_reachable_ = false;
95 32202 : cache_routes_ = false;
96 32202 : repr_valid_ = false;
97 32202 : repr_.clear();
98 32202 : }
99 :
100 32203 : bool BgpXmppMessage::Start(const RibOut *ribout, bool cache_routes,
101 : const RibOutAttr *roattr, const BgpRoute *route) {
102 32203 : Reset();
103 32202 : table_ = ribout->table();
104 32200 : is_reachable_ = roattr->IsReachable();
105 32201 : cache_routes_ = cache_routes;
106 32201 : Address::Family family = table_->family();
107 :
108 32199 : if (is_reachable_) {
109 18360 : const BgpAttr *attr = roattr->attr();
110 18360 : ProcessCommunity(attr->community());
111 18360 : 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 32205 : repr_.append(kMaxFromToLength, ' ');
118 :
119 : // Add opening tags for event and items. The closing tags are added when
120 : // GetData is called.
121 32204 : repr_ += "\n\t<event xmlns=\"http://jabber.org/protocol/pubsub\">";
122 32216 : repr_ += "\n\t\t<items node=\"";
123 32228 : repr_ += AfiName(BgpAf::FamilyToAfi(family));
124 32229 : repr_ += "/";
125 32235 : repr_ += XmppSafiName(BgpAf::FamilyToXmppSafi(family));
126 32233 : repr_ += "/";
127 32233 : repr_ += table_->routing_instance()->name();
128 32229 : repr_ += "\">\n";
129 :
130 32231 : if (table_->family() == Address::ERMVPN) {
131 0 : AddMcastRoute(route, roattr);
132 32230 : } else if (table_->family() == Address::EVPN) {
133 398 : AddEnetRoute(route, roattr);
134 31832 : } else if (table_->family() == Address::INET6) {
135 14155 : AddInet6Route(route, roattr);
136 17673 : } else if (table_->family() == Address::MVPN) {
137 0 : AddMvpnRoute(route, roattr);
138 : } else {
139 17676 : AddInetRoute(route, roattr);
140 : }
141 32236 : return true;
142 : }
143 :
144 32236 : void BgpXmppMessage::Finish() {
145 32236 : }
146 :
147 15368 : bool BgpXmppMessage::AddRoute(const BgpRoute *route, const RibOutAttr *roattr) {
148 15368 : assert(is_reachable_ == roattr->IsReachable());
149 15368 : if (is_reachable_ && num_reach_route_ >= kMaxReachCount)
150 0 : return false;
151 15368 : if (!is_reachable_ && num_unreach_route_ >= kMaxUnreachCount)
152 0 : return false;
153 :
154 15368 : if (is_reachable_) {
155 10278 : const BgpAttr *attr = roattr->attr();
156 10275 : ProcessCommunity(attr->community());
157 10274 : ProcessExtCommunity(attr->ext_community());
158 : }
159 :
160 15365 : if (table_->family() == Address::ERMVPN) {
161 0 : return AddMcastRoute(route, roattr);
162 15365 : } else if (table_->family() == Address::EVPN) {
163 114 : return AddEnetRoute(route, roattr);
164 15250 : } else if (table_->family() == Address::INET6) {
165 9389 : return AddInet6Route(route, roattr);
166 5859 : } else if (table_->family() == Address::MVPN) {
167 0 : return AddMvpnRoute(route, roattr);
168 : } else {
169 5863 : return AddInetRoute(route, roattr);
170 : }
171 : }
172 :
173 28236 : void BgpXmppMessage::EncodeNextHop(const BgpRoute *route,
174 : const RibOutAttr::NextHop &nexthop,
175 : autogen::ItemType *item) {
176 28236 : autogen::NextHopType item_nexthop;
177 :
178 28229 : const IpAddress &address = nexthop.address();
179 28227 : if (address.is_v4()) {
180 28227 : item_nexthop.af = BgpAf::IPv4;
181 28227 : 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 28284 : item_nexthop.label = nexthop.label();
187 28281 : item_nexthop.virtual_network = GetVirtualNetwork(nexthop);
188 28284 : item_nexthop.tag_list.tag = nexthop.tag_list();
189 28237 : 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 28237 : if (item_nexthop.label) {
194 28109 : vector<string> &encap_list =
195 : item_nexthop.tunnel_encapsulation_list.tunnel_encapsulation;
196 28109 : if (nexthop.encap().empty()) {
197 0 : encap_list.push_back(string("gre"));
198 : } else {
199 28107 : encap_list = nexthop.encap();
200 : }
201 : }
202 :
203 28213 : item->entry.next_hops.next_hop.push_back(item_nexthop);
204 28229 : }
205 :
206 28258 : void BgpXmppMessage::AddIpReach(const BgpRoute *route,
207 : const RibOutAttr *roattr) {
208 28258 : if (!roattr->repr().empty()) {
209 0 : repr_ += roattr->repr();
210 0 : return;
211 : }
212 28261 : Address::Family family = table_->family();
213 :
214 28262 : autogen::ItemType item;
215 28260 : item.entry.nlri.af = BgpAf::FamilyToAfi(family);
216 28261 : item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
217 28259 : item.entry.nlri.address = route->ToString();
218 28278 : item.entry.version = 1;
219 28278 : item.entry.virtual_network = GetVirtualNetwork(route, roattr);
220 28272 : item.entry.local_preference = roattr->attr()->local_pref();
221 28271 : item.entry.med = roattr->attr()->med();
222 28262 : item.entry.sequence_number = mobility_.sequence_number;
223 28262 : item.entry.mobility.seqno = mobility_.sequence_number;
224 28262 : item.entry.mobility.sticky = mobility_.sticky;
225 :
226 28262 : assert(!roattr->nexthop_list().empty());
227 :
228 : // Encode all next-hops in the list.
229 84721 : BOOST_FOREACH(const RibOutAttr::NextHop &nexthop, roattr->nexthop_list()) {
230 28237 : EncodeNextHop(route, nexthop, &item);
231 : }
232 :
233 28230 : for (vector<int>::const_iterator it = security_group_list_.begin();
234 42530 : it != security_group_list_.end(); ++it) {
235 14308 : item.entry.security_group_list.security_group.push_back(*it);
236 : }
237 :
238 28226 : for (vector<string>::const_iterator it = community_list_.begin();
239 28224 : 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 28225 : if (!load_balance_attribute_.IsDefault())
245 0 : load_balance_attribute_.Encode(&item.entry.load_balance);
246 :
247 28224 : xml_node node = doc_.append_child("item");
248 28208 : 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 28236 : size_t pos = repr_.size();
254 28236 : item.Encode(&node);
255 28210 : doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
256 28265 : doc_.remove_child(node);
257 :
258 : // Cache the substring starting at the previous size.
259 28258 : if (cache_routes_)
260 1753 : roattr->set_repr(repr_, pos);
261 28259 : }
262 :
263 18818 : void BgpXmppMessage::AddIpUnreach(const BgpRoute *route) {
264 18818 : repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
265 18835 : }
266 :
267 23536 : bool BgpXmppMessage::AddInetRoute(const BgpRoute *route,
268 : const RibOutAttr *roattr) {
269 23536 : if (is_reachable_) {
270 14122 : num_reach_route_++;
271 14122 : AddIpReach(route, roattr);
272 : } else {
273 9414 : num_unreach_route_++;
274 9414 : AddIpUnreach(route);
275 : }
276 23549 : return true;
277 : }
278 :
279 23543 : bool BgpXmppMessage::AddInet6Route(const BgpRoute *route,
280 : const RibOutAttr *roattr) {
281 23543 : if (is_reachable_) {
282 14137 : num_reach_route_++;
283 14137 : AddIpReach(route, roattr);
284 : } else {
285 9406 : num_unreach_route_++;
286 9406 : AddIpUnreach(route);
287 : }
288 23544 : return true;
289 : }
290 :
291 384 : void BgpXmppMessage::EncodeEnetNextHop(const BgpRoute *route,
292 : const RibOutAttr::NextHop &nexthop,
293 : autogen::EnetItemType *item) {
294 384 : autogen::EnetNextHopType item_nexthop;
295 :
296 384 : item_nexthop.af = BgpAf::IPv4;
297 384 : item_nexthop.address = nexthop.address().to_v4().to_string();
298 384 : item_nexthop.label = nexthop.label();
299 384 : item_nexthop.l3_label = nexthop.l3_label();
300 384 : 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 384 : vector<string> &encap_list =
305 : item_nexthop.tunnel_encapsulation_list.tunnel_encapsulation;
306 384 : if (nexthop.encap().empty()) {
307 0 : encap_list.push_back(string("gre"));
308 : } else {
309 384 : encap_list = nexthop.encap();
310 : }
311 384 : item_nexthop.tag_list.tag = nexthop.tag_list();
312 384 : item_nexthop.is_new_tags_list = true;
313 384 : item->entry.next_hops.next_hop.push_back(item_nexthop);
314 384 : }
315 :
316 384 : void BgpXmppMessage::AddEnetReach(const BgpRoute *route,
317 : const RibOutAttr *roattr) {
318 384 : if (!roattr->repr().empty()) {
319 0 : repr_ += roattr->repr();
320 0 : return;
321 : }
322 384 : Address::Family family = table_->family();
323 :
324 384 : autogen::EnetItemType item;
325 384 : item.entry.nlri.af = BgpAf::FamilyToAfi(family);
326 384 : item.entry.nlri.safi = BgpAf::FamilyToXmppSafi(family);
327 :
328 384 : EvpnRoute *evpn_route =
329 : static_cast<EvpnRoute *>(const_cast<BgpRoute *>(route));
330 384 : const EvpnPrefix &evpn_prefix = evpn_route->GetPrefix();
331 384 : item.entry.nlri.ethernet_tag = evpn_prefix.tag();
332 384 : item.entry.nlri.mac = evpn_prefix.mac_addr().ToString();
333 768 : item.entry.nlri.address = evpn_prefix.ip_address().to_string() + "/" +
334 1152 : integerToString(evpn_prefix.ip_address_length());
335 384 : item.entry.nlri.source = evpn_prefix.source().to_string();
336 384 : item.entry.nlri.group = evpn_prefix.group().to_string();
337 :
338 384 : item.entry.virtual_network = GetVirtualNetwork(route, roattr);
339 384 : item.entry.local_preference = roattr->attr()->local_pref();
340 384 : item.entry.med = roattr->attr()->med();
341 384 : item.entry.sequence_number = mobility_.sequence_number;
342 384 : item.entry.mobility.seqno = mobility_.sequence_number;
343 384 : item.entry.mobility.sticky = mobility_.sticky;
344 384 : item.entry.etree_leaf = etree_leaf_;
345 :
346 384 : for (vector<int>::const_iterator it = security_group_list_.begin();
347 768 : it != security_group_list_.end(); ++it) {
348 384 : item.entry.security_group_list.security_group.push_back(*it);
349 : }
350 :
351 384 : const BgpOList *olist = roattr->attr()->olist().get();
352 384 : assert((olist == NULL) != roattr->nexthop_list().empty());
353 :
354 384 : 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 384 : const BgpOList *leaf_olist = roattr->attr()->leaf_olist().get();
367 384 : assert((leaf_olist == NULL) != roattr->nexthop_list().empty());
368 :
369 384 : 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 1152 : BOOST_FOREACH(const RibOutAttr::NextHop &nexthop, roattr->nexthop_list()) {
382 384 : EncodeEnetNextHop(route, nexthop, &item);
383 : }
384 :
385 384 : for (const auto &peer_name : route->peer_sources()) {
386 0 : item.entry.peers.peer.push_back(peer_name);
387 : }
388 :
389 384 : xml_node node = doc_.append_child("item");
390 384 : 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 384 : size_t pos = repr_.size();
396 384 : item.Encode(&node);
397 384 : doc_.print(writer_, "\t", pugi::format_default, pugi::encoding_auto, 3);
398 384 : doc_.remove_child(node);
399 :
400 : // Cache the substring starting at the previous size.
401 384 : if (cache_routes_)
402 96 : roattr->set_repr(repr_, pos);
403 384 : }
404 :
405 128 : void BgpXmppMessage::AddEnetUnreach(const BgpRoute *route) {
406 128 : repr_ += "\t\t\t<retract id=\"" + route->ToXmppIdString() + "\" />\n";
407 128 : }
408 :
409 512 : bool BgpXmppMessage::AddEnetRoute(const BgpRoute *route,
410 : const RibOutAttr *roattr) {
411 512 : if (is_reachable_) {
412 384 : num_reach_route_++;
413 384 : AddEnetReach(route, roattr);
414 : } else {
415 128 : num_unreach_route_++;
416 128 : AddEnetUnreach(route);
417 : }
418 512 : 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 120069 : 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 120069 : msg_begin_.clear();
536 120085 : msg_begin_ += "\n<message from=\"";
537 120104 : msg_begin_ += XmppInit::kControlNodeJID;
538 120135 : msg_begin_ += "\" to=\"";
539 120162 : msg_begin_ += peer->ToString();
540 120145 : msg_begin_ += "/";
541 120154 : msg_begin_ += XmppInit::kBgpPeer;
542 120164 : msg_begin_ += "\">";
543 :
544 : // Add closing tags if this is the first peer to which the message will
545 : // be sent.
546 120166 : if (!repr_valid_) {
547 32238 : repr_ += "\t\t</items>\n\t</event>\n</message>\n";
548 32242 : 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 120170 : size_t begin_size = msg_begin_.size();
558 120138 : if (begin_size <= kMaxFromToLength) {
559 120152 : size_t extra = kMaxFromToLength - begin_size;
560 120152 : char *data = const_cast<char *>(repr_.c_str());
561 120150 : fill(data, data + extra, ' ');
562 120142 : copy(msg_begin_.c_str(), msg_begin_.c_str() + begin_size, data + extra);
563 120082 : *lenp = repr_.size() - extra;
564 120074 : *msg_str = &repr_;
565 120074 : 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 28634 : void BgpXmppMessage::ProcessCommunity(const Community *community) {
575 28634 : community_list_.clear();
576 28633 : if (community == NULL)
577 28633 : return;
578 0 : BOOST_FOREACH(uint32_t value, community->communities()) {
579 0 : community_list_.push_back(CommunityType::CommunityToString(value));
580 : }
581 : }
582 :
583 28632 : void BgpXmppMessage::ProcessExtCommunity(const ExtCommunity *ext_community) {
584 28632 : mobility_.sequence_number = 0;
585 28632 : mobility_.sticky = false;
586 28632 : etree_leaf_ = false;
587 28632 : security_group_list_.clear();
588 28630 : load_balance_attribute_ = LoadBalance::LoadBalanceAttribute();
589 28670 : if (ext_community == NULL)
590 128 : return;
591 :
592 28542 : as_t as_number = table_->server()->autonomous_system();
593 28535 : bool sg_asn_match = true;
594 28530 : for (ExtCommunity::ExtCommunityList::const_iterator iter =
595 28535 : ext_community->communities().begin();
596 181710 : iter != ext_community->communities().end(); ++iter) {
597 153204 : if (ExtCommunity::is_security_group(*iter)) {
598 14709 : SecurityGroup sg(*iter);
599 14706 : if (as_number <= AS2_MAX)
600 14706 : if (sg.as_number() != as_number && !sg.IsGlobal())
601 0 : continue;
602 14710 : security_group_list_.push_back(sg.security_group_id());
603 138521 : } 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 138521 : } 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 138493 : } else if (ExtCommunity::is_load_balance(*iter)) {
612 0 : LoadBalance load_balance(*iter);
613 0 : load_balance.FillAttribute(&load_balance_attribute_);
614 138484 : } else if (ExtCommunity::is_etree(*iter)) {
615 384 : ETree etree(*iter);
616 384 : etree_leaf_ = etree.leaf();
617 : }
618 : }
619 28507 : if (!sg_asn_match)
620 0 : security_group_list_.clear();
621 : }
622 :
623 56926 : string BgpXmppMessage::GetVirtualNetwork(
624 : const RibOutAttr::NextHop &nexthop) const {
625 56926 : int index = nexthop.origin_vn_index();
626 56921 : if (index > 0) {
627 : const RoutingInstanceMgr *manager =
628 0 : table_->routing_instance()->manager();
629 0 : return manager->GetVirtualNetworkByVnIndex(index);
630 56924 : } else if (index == 0) {
631 2175 : return table_->routing_instance()->GetVirtualNetworkName();
632 : } else {
633 54749 : return "unresolved";
634 : }
635 : }
636 :
637 28661 : string BgpXmppMessage::GetVirtualNetwork(const BgpRoute *route,
638 : const RibOutAttr *roattr) const {
639 28661 : if (!is_reachable_) {
640 0 : return "unresolved";
641 28661 : } 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 28651 : 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 : }
|