OpenSDN source code
bgp_message_builder.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
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 BgpMessage::BgpMessage() : table_(NULL), datalen_(0) {
21 }
22 
24 }
25 
26 bool BgpMessage::StartReach(const RibOut *ribout, const RibOutAttr *roattr,
27  const BgpRoute *route) {
28  BgpProto::Update update;
29  const BgpAttr *attr = roattr->attr();
30  Address::Family family = table_->family();
31 
32  BgpAttrOrigin *origin = new BgpAttrOrigin(attr->origin());
33  update.path_attributes.push_back(origin);
34 
35  if ((BgpAf::FamilyToAfi(family) == BgpAf::IPv4) &&
36  (BgpAf::FamilyToSafi(family) == BgpAf::Unicast)) {
37  BgpAttrNextHop *nh =
38  new BgpAttrNextHop(attr->nexthop().to_v4().to_ulong());
39  update.path_attributes.push_back(nh);
40  }
41 
42  if (attr->med()) {
43  BgpAttrMultiExitDisc *med = new BgpAttrMultiExitDisc(attr->med());
44  update.path_attributes.push_back(med);
45  }
46 
47  if (ribout->peer_type() == BgpProto::IBGP) {
48  BgpAttrLocalPref *lp = new BgpAttrLocalPref(attr->local_pref());
49  update.path_attributes.push_back(lp);
50  }
51 
52  if (attr->atomic_aggregate()) {
54  update.path_attributes.push_back(aa);
55  }
56 
57  if (attr->aggregator_as_num()) {
58  if (ribout->as4_supported()) {
60  attr->aggregator_as_num(),
61  attr->aggregator_adderess().to_v4().to_ulong());
62  update.path_attributes.push_back(agg);
63  } else {
64  if (attr->aggregator_as_num() > 0xffff) {
65  // For old neighbor, need to send AS4_Aggregator along with
66  // AS_TRANS in regular aggregator attribute
68  AS_TRANS, attr->aggregator_adderess().to_v4().to_ulong());
69  update.path_attributes.push_back(agg);
71  attr->aggregator_as_num(),
72  attr->aggregator_adderess().to_v4().to_ulong());
73  update.path_attributes.push_back(as4_agg);
74  } else {
76  attr->aggregator_as_num(),
77  attr->aggregator_adderess().to_v4().to_ulong());
78  update.path_attributes.push_back(agg);
79  }
80  }
81  }
82 
83  if (!attr->originator_id().is_unspecified()) {
84  BgpAttrOriginatorId *originator_id =
85  new BgpAttrOriginatorId(attr->originator_id().to_ulong());
86  update.path_attributes.push_back(originator_id);
87  }
88 
89  if (attr->cluster_list()) {
90  ClusterListSpec *clist =
92  update.path_attributes.push_back(clist);
93  }
94 
95 
96  if (ribout->as4_supported()) {
97  if (attr->aspath_4byte()) {
98  AsPath4ByteSpec *path = new AsPath4ByteSpec(
99  attr->aspath_4byte()->path());
100  update.path_attributes.push_back(path);
101  }
102  } else {
103  if (attr->as_path()) {
104  AsPathSpec *path = new AsPathSpec(attr->as_path()->path());
105  update.path_attributes.push_back(path);
106  }
107  if (attr->as4_path()) {
108  As4PathSpec *path = new As4PathSpec(attr->as4_path()->path());
109  update.path_attributes.push_back(path);
110  }
111  }
112 
113  if (attr->edge_discovery()) {
114  EdgeDiscoverySpec *edspec =
116  update.path_attributes.push_back(edspec);
117  }
118 
119  if (attr->edge_forwarding()) {
120  EdgeForwardingSpec *efspec =
122  update.path_attributes.push_back(efspec);
123  }
124 
125  if (attr->community() && attr->community()->communities().size()) {
126  CommunitySpec *comm = new CommunitySpec;
127  comm->communities = attr->community()->communities();
128  update.path_attributes.push_back(comm);
129  }
130 
131  ExtCommunitySpec *ext_comm = new ExtCommunitySpec;
132  if (attr->ext_community() && attr->ext_community()->communities().size()) {
134  attr->ext_community()->communities();
135  for (ExtCommunity::ExtCommunityList::const_iterator it = v.begin();
136  it != v.end(); ++it) {
137  uint64_t value = get_value(it->data(), it->size());
138  ext_comm->communities.push_back(value);
139  }
140  }
141 
142  LargeCommunitySpec *large_comm = new LargeCommunitySpec;
143  if (attr->large_community() && attr->large_community()->communities().size()) {
144  uint16_t tag_index = 0;
146  attr->large_community()->communities();
147  uint64_t tid64;
148  uint32_t asn, tid, value;
149  for (LargeCommunity::LargeCommunityList::const_iterator it = v.begin();
150  it != v.end(); ++it) {
151  TagLC tag_lc {*it};
152  tid64 = tag_lc.tag();
153  if ((tid64 & 0xFFFF0000) == 0) {
154  asn = tag_lc.as_number();
155  tid = ((tid64 & 0xFFFF00000000) >> 16) |
156  (tid64 & 0xFFFF);
157  if (asn <= AS2_MAX) {
158  Tag tag {as2_t(asn), tid};
159  ext_comm->communities.push_back(tag.GetExtCommunityValue());
160  } else {
161  Tag4ByteAs tag4 {asn, tag_index};
162  Tag tag {tag_index, tid};
163  ext_comm->communities.push_back(tag.GetExtCommunityValue());
164  ext_comm->communities.push_back(tag4.GetExtCommunityValue());
165  tag_index++;
166  }
167  continue;
168  }
169 
170  for (int i = 0; i < 3; i++) {
171  value = get_value(it->data() + 4*i, 4);
172  large_comm->communities.push_back(value);
173  }
174  }
175  update.path_attributes.push_back(large_comm);
176  }
177  if (ext_comm->EncodeLength()) {
178  update.path_attributes.push_back(ext_comm);
179  }
180 
181  if (attr->origin_vn_path() && attr->origin_vn_path()->origin_vns().size()) {
182  OriginVnPathSpec *ovnpath_spec = new OriginVnPathSpec;
183  const OriginVnPath::OriginVnList &v =
184  attr->origin_vn_path()->origin_vns();
185  for (OriginVnPath::OriginVnList::const_iterator it = v.begin();
186  it != v.end(); ++it) {
187  uint64_t value = get_value(it->data(), it->size());
188  ovnpath_spec->origin_vns.push_back(value);
189  }
190  update.path_attributes.push_back(ovnpath_spec);
191  }
192 
193  if (attr->pmsi_tunnel()) {
194  PmsiTunnelSpec *pmsi_spec =
195  new PmsiTunnelSpec(attr->pmsi_tunnel()->pmsi_tunnel());
196  update.path_attributes.push_back(pmsi_spec);
197  }
198 
199  std::vector<uint8_t> nh;
200 
201  route->BuildBgpProtoNextHop(nh, attr->nexthop());
202 
203  BgpMpNlri *nlri = new BgpMpNlri(
205  BgpAf::FamilyToSafi(family), nh);
206  update.path_attributes.push_back(nlri);
207 
208  BgpProtoPrefix *prefix = new BgpProtoPrefix;
209  const uint32_t label = (family == Address::INET) ? 0 : roattr->label();
210  route->BuildProtoPrefix(prefix, attr, label, roattr->l3_label());
211  nlri->nlri.push_back(prefix);
212 
213  int result = BgpProto::Encode(&update, data_, sizeof(data_),
214  &encode_offsets_, ribout->as4_supported());
215  if (result <= 0) {
216  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"));
220  return false;
221  }
222 
224  datalen_ = result;
225  return true;
226 }
227 
228 bool BgpMessage::StartUnreach(const BgpRoute *route) {
229  BgpProto::Update update;
230  Address::Family family = table_->family();
231 
232  BgpMpNlri *nlri =
234  BgpAf::FamilyToAfi(family), BgpAf::FamilyToSafi(family));
235  update.path_attributes.push_back(nlri);
236 
237  BgpProtoPrefix *prefix = new BgpProtoPrefix;
238  route->BuildProtoPrefix(prefix);
239  nlri->nlri.push_back(prefix);
240 
241  int result =
242  BgpProto::Encode(&update, data_, sizeof(data_), &encode_offsets_);
243  if (result <= 0) {
244  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"));
248  return false;
249  }
250 
252  datalen_ = result;
253  return true;
254 }
255 
257  Message::Reset();
258  table_ = NULL;
260  datalen_ = 0;
261 }
262 
263 bool BgpMessage::Start(const RibOut *ribout, bool cache_repr,
264  const RibOutAttr *roattr, const BgpRoute *route) {
265  Reset();
266  table_ = ribout->table();
267 
268  if (roattr->IsReachable()) {
269  return StartReach(ribout, roattr, route);
270  } else {
271  return StartUnreach(route);
272  }
273 }
274 
275 bool BgpMessage::UpdateLength(const char *tag, int size, int delta) {
276  int offset = encode_offsets_.FindOffset(tag);
277  if (offset < 0) {
278  return false;
279  }
280  int value = get_value(&data_[offset], size);
281  value += delta;
282  put_value(&data_[offset], size, value);
283  return true;
284 }
285 
286 bool BgpMessage::AddRoute(const BgpRoute *route, const RibOutAttr *roattr) {
287  uint8_t *data = data_ + datalen_;
288  size_t size = sizeof(data_) - datalen_;
289  Address::Family family = table_->family();
290 
291  BgpMpNlri nlri;
292  nlri.afi = BgpAf::FamilyToAfi(family);
293  nlri.safi = BgpAf::FamilyToSafi(family);
294  BgpProtoPrefix *prefix = new BgpProtoPrefix;
295  if (roattr) {
296  const uint32_t label = (family == Address::INET) ? 0 : roattr->label();
297  route->BuildProtoPrefix(prefix, roattr->attr(), label);
298  } else {
299  route->BuildProtoPrefix(prefix);
300  }
301  nlri.nlri.push_back(prefix);
302 
303  int result = BgpProto::Encode(&nlri, data, size);
304  if (result <= 0) {
305  return false;
306  }
307 
308  datalen_ += result;
309  if (roattr->IsReachable()) {
311  } else {
313  }
314 
315  if (!UpdateLength("BgpMsgLength", 2, result)) {
316  assert(false);
317  return false;
318  }
319 
320  if (!UpdateLength("BgpPathAttribute", 2, result)) {
321  assert(false);
322  return false;
323  }
324 
325  if (!UpdateLength("MpReachUnreachNlri", 2, result)) {
326  assert(false);
327  return false;
328  }
329 
330  return true;
331 }
332 
334 }
335 
336 const uint8_t *BgpMessage::GetData(IPeerUpdate *peer, size_t *lenp,
337  const string **msg_str, string *temp) {
338  *lenp = datalen_;
339  return data_;
340 }
341 
343  return new BgpMessage;
344 }
345 
347 : MessageBuilder() {
348 }
#define AS_TRANS
Definition: bgp_common.h:24
#define AS2_MAX
Definition: bgp_common.h:25
uint16_t as2_t
Definition: bgp_common.h:23
#define BGP_LOG_FLAG_ALL
Definition: bgp_log.h:45
#define BGP_LOG_WARNING_STR(obj, flags, arg)
Definition: bgp_log.h:108
Family
Definition: address.h:25
@ INET
Definition: address.h:27
const As4PathSpec & path() const
Definition: bgp_aspath.h:444
const AsPath4ByteSpec & path() const
Definition: bgp_aspath.h:286
const AsPathSpec & path() const
Definition: bgp_aspath.h:128
@ Unicast
Definition: bgp_af.h:26
static Afi FamilyToAfi(Address::Family family)
Definition: bgp_af.cc:160
static Safi FamilyToSafi(Address::Family family)
Definition: bgp_af.cc:189
@ IPv4
Definition: bgp_af.h:20
uint32_t local_pref() const
Definition: bgp_attr.h:892
const LargeCommunity * large_community() const
Definition: bgp_attr.h:919
const AsPath * as_path() const
Definition: bgp_attr.h:902
const IpAddress & aggregator_adderess() const
Definition: bgp_attr.h:897
const EdgeForwarding * edge_forwarding() const
Definition: bgp_attr.h:927
const ClusterList * cluster_list() const
Definition: bgp_attr.h:910
const OriginVnPath * origin_vn_path() const
Definition: bgp_attr.h:922
uint32_t med() const
Definition: bgp_attr.h:891
const Community * community() const
Definition: bgp_attr.h:917
as_t aggregator_as_num() const
Definition: bgp_attr.h:894
const IpAddress & nexthop() const
Definition: bgp_attr.h:889
const EdgeDiscovery * edge_discovery() const
Definition: bgp_attr.h:924
const ExtCommunity * ext_community() const
Definition: bgp_attr.h:918
const AsPath4Byte * aspath_4byte() const
Definition: bgp_attr.h:905
const Ip4Address & originator_id() const
Definition: bgp_attr.h:898
BgpAttrOrigin::OriginType origin() const
Definition: bgp_attr.h:884
const PmsiTunnel * pmsi_tunnel() const
Definition: bgp_attr.h:923
const As4Path * as4_path() const
Definition: bgp_attr.h:914
bool atomic_aggregate() const
Definition: bgp_attr.h:893
virtual Message * Create() const
virtual ~BgpMessage()
virtual bool Start(const RibOut *ribout, bool cache_routes, const RibOutAttr *roattr, const BgpRoute *route)
uint8_t data_[BgpProto::kMaxMessageSize]
virtual void Reset()
virtual const uint8_t * GetData(IPeerUpdate *peer, size_t *lenp, const std::string **msg_str, std::string *temp)
bool StartReach(const RibOut *ribout, const RibOutAttr *roattr, const BgpRoute *route)
const BgpTable * table_
bool UpdateLength(const char *tag, int size, int delta)
virtual bool AddRoute(const BgpRoute *route, const RibOutAttr *roattr)
EncodeOffsets encode_offsets_
bool StartUnreach(const BgpRoute *route)
virtual void Finish()
static int Encode(const BgpMessage *msg, uint8_t *data, size_t size, EncodeOffsets *offsets=NULL, bool as4=false)
Definition: bgp_proto.cc:2136
virtual void BuildProtoPrefix(BgpProtoPrefix *prefix, const BgpAttr *attr=NULL, uint32_t label=0, uint32_t l3_label=0) const
Definition: bgp_route.h:68
virtual void BuildBgpProtoNextHop(std::vector< uint8_t > &nh, IpAddress nexthop) const
Definition: bgp_route.h:73
void increment_message_build_error() const
Definition: bgp_server.h:277
BgpServer * server()
Definition: bgp_table.cc:88
virtual Address::Family family() const =0
const ClusterListSpec & cluster_list() const
Definition: bgp_attr.h:230
const std::vector< uint32_t > & communities() const
Definition: community.h:68
virtual std::string ToString() const =0
const std::string & name() const
Definition: db_table.h:111
const EdgeDiscoverySpec & edge_discovery() const
Definition: bgp_attr.h:471
const EdgeForwardingSpec & edge_forwarding() const
Definition: bgp_attr.h:578
void ClearOffsets()
Definition: parse_object.h:98
std::vector< uint64_t > communities
Definition: community.h:146
virtual size_t EncodeLength() const
Definition: community.cc:267
std::vector< ExtCommunityValue > ExtCommunityList
Definition: community.h:156
const ExtCommunityList & communities() const
Definition: community.h:183
This class encapsulates the wire-format representation of a BGP Large Community attribute and provide...
Definition: community.h:509
std::vector< uint32_t > communities
Vector of community values. Each Large Community value consists of three 4-byte fields (12 bytes tota...
Definition: community.h:541
const LargeCommunityList & communities() const
Get the list of Large Community values.
Definition: community.h:595
std::vector< LargeCommunityValue > LargeCommunityList
A list (vector) of LargeCommunityValue items.
Definition: community.h:563
uint64_t num_reach_route_
virtual void Reset()
uint64_t num_unreach_route_
std::vector< OriginVnValue > OriginVnList
const OriginVnList & origin_vns() const
const PmsiTunnelSpec & pmsi_tunnel() const
Definition: bgp_attr.h:374
const BgpAttr * attr() const
Definition: bgp_ribout.h:99
bool IsReachable() const
Definition: bgp_ribout.h:96
uint32_t l3_label() const
Definition: bgp_ribout.h:120
uint32_t label() const
Definition: bgp_ribout.h:117
BgpProto::BgpPeerType peer_type() const
Definition: bgp_ribout.h:322
BgpTable * table()
Definition: bgp_ribout.h:306
bool as4_supported() const
Definition: bgp_ribout.h:327
Represents a single BGP Large Community tag.
uint64_t tag() const
Returns the tag id.
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:56
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40
uint8_t safi
Definition: bgp_attr.h:309
uint16_t afi
Definition: bgp_attr.h:308
std::vector< BgpProtoPrefix * > nlri
Definition: bgp_attr.h:312
std::vector< BgpAttribute * > path_attributes
Definition: bgp_proto.h:436
std::vector< uint32_t > communities
Definition: community.h:36
std::vector< uint64_t > origin_vns