OpenSDN source code
ermvpn_route.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 <algorithm>
9 #include <string>
10 #include <vector>
11 
12 #include "base/string_util.h"
14 
15 using std::copy;
16 using std::string;
17 using std::vector;
18 
19 // BgpProtoPrefix format for erm-vpn prefix.
20 //
21 // +------------------------------------+
22 // | RD (8 octets) |
23 // +------------------------------------+
24 // | Router-Id (4 octets) |
25 // +------------------------------------+
26 // | Multicast Source length (1 octet) |
27 // +------------------------------------+
28 // | Multicast Source (4) |
29 // +------------------------------------+
30 // | Multicast Group length (1 octet) |
31 // +------------------------------------+
32 // | Multicast Group (4) |
33 // +------------------------------------+
34 
36 }
37 
39  const Ip4Address &group, const Ip4Address &source)
40  : type_(type), rd_(rd), group_(group), source_(source) {
41 }
42 
44  const Ip4Address &router_id,
45  const Ip4Address &group, const Ip4Address &source)
46  : type_(type), rd_(rd), router_id_(router_id),
47  group_(group), source_(source) {
48 }
49 
51  ErmVpnPrefix *prefix) {
52  size_t rd_size = RouteDistinguisher::kSize;
53  size_t rtid_size = Address::kMaxV4Bytes;
54  size_t nlri_size = proto_prefix.prefix.size();
55  size_t expected_nlri_size =
56  rd_size + rtid_size + 2 * (Address::kMaxV4Bytes + 1);
57 
58  if (!IsValidForBgp(proto_prefix.type))
59  return -1;
60  if (nlri_size != expected_nlri_size)
61  return -1;
62 
63  prefix->type_ = proto_prefix.type;
64  size_t rd_offset = 0;
65  prefix->rd_ = RouteDistinguisher(&proto_prefix.prefix[rd_offset]);
66  size_t rtid_offset = rd_offset + rd_size;
67  prefix->router_id_ =
68  Ip4Address(get_value(&proto_prefix.prefix[rtid_offset], rtid_size));
69 
70  size_t source_offset = rtid_offset + rtid_size + 1;
71  if (proto_prefix.prefix[source_offset - 1] != Address::kMaxV4PrefixLen)
72  return -1;
73  prefix->source_ = Ip4Address(
74  get_value(&proto_prefix.prefix[source_offset], Address::kMaxV4Bytes));
75 
76  size_t group_offset = source_offset + Address::kMaxV4Bytes + 1;
77  if (proto_prefix.prefix[group_offset - 1] != Address::kMaxV4PrefixLen)
78  return -1;
79  prefix->group_ = Ip4Address(
80  get_value(&proto_prefix.prefix[group_offset], Address::kMaxV4Bytes));
81 
82  return 0;
83 }
84 
86  const BgpProtoPrefix &proto_prefix,
87  const BgpAttr *attr,
88  const Address::Family family,
89  ErmVpnPrefix *prefix,
90  BgpAttrPtr *new_attr, uint32_t *label,
91  uint32_t *l3_label) {
92  return FromProtoPrefix(proto_prefix, prefix);
93 }
94 
96  assert(IsValidForBgp(type_));
97 
98  size_t rd_size = RouteDistinguisher::kSize;
99  size_t rtid_size = Address::kMaxV4Bytes;
100 
101  proto_prefix->type = type_;
102  proto_prefix->prefix.clear();
103  proto_prefix->prefixlen =
104  (rd_size + rtid_size + 2 * (1 + Address::kMaxV4Bytes)) * 8;
105  proto_prefix->prefix.resize(proto_prefix->prefixlen / 8, 0);
106 
107  size_t rd_offset = 0;
108  copy(rd_.GetData(), rd_.GetData() + rd_size,
109  proto_prefix->prefix.begin() + rd_offset);
110 
111  size_t rtid_offset = rd_offset + rd_size;
112  const Ip4Address::bytes_type &rtid_bytes = router_id_.to_bytes();
113  copy(rtid_bytes.begin(), rtid_bytes.begin() + Address::kMaxV4Bytes,
114  proto_prefix->prefix.begin() + rtid_offset);
115 
116  size_t source_offset = rtid_offset + rtid_size + 1;
117  proto_prefix->prefix[source_offset - 1] = Address::kMaxV4PrefixLen;
118  const Ip4Address::bytes_type &source_bytes = source_.to_bytes();
119  copy(source_bytes.begin(), source_bytes.begin() + Address::kMaxV4Bytes,
120  proto_prefix->prefix.begin() + source_offset);
121 
122  size_t group_offset = source_offset + Address::kMaxV4Bytes + 1;
123  proto_prefix->prefix[group_offset - 1] = Address::kMaxV4PrefixLen;
124  const Ip4Address::bytes_type &group_bytes = group_.to_bytes();
125  copy(group_bytes.begin(), group_bytes.begin() + Address::kMaxV4Bytes,
126  proto_prefix->prefix.begin() + group_offset);
127 }
128 
130  boost::system::error_code *errorp) {
131  ErmVpnPrefix prefix, null_prefix;
132  string temp_str;
133 
134  // Look for Type.
135  size_t pos1 = str.find('-');
136  if (pos1 == string::npos) {
137  if (errorp != NULL) {
138  *errorp = make_error_code(boost::system::errc::invalid_argument);
139  }
140  return null_prefix;
141  }
142  temp_str = str.substr(0, pos1);
143  stringToInteger(temp_str, prefix.type_);
144  if (!IsValid(prefix.type_)) {
145  if (errorp != NULL) {
146  *errorp = make_error_code(boost::system::errc::invalid_argument);
147  }
148  return null_prefix;
149  }
150 
151  // Look for RD.
152  size_t pos2 = str.find('-', pos1 + 1);
153  if (pos2 == string::npos) {
154  if (errorp != NULL) {
155  *errorp = make_error_code(boost::system::errc::invalid_argument);
156  }
157  return null_prefix;
158  }
159  temp_str = str.substr(pos1 + 1, pos2 - pos1 - 1);
160  boost::system::error_code rd_err;
161  prefix.rd_ = RouteDistinguisher::FromString(temp_str, &rd_err);
162  if (rd_err.failed()) {
163  if (errorp != NULL) {
164  *errorp = rd_err;
165  }
166  return null_prefix;
167  }
168 
169  // Look for router-id.
170  size_t pos3 = str.find(',', pos2 + 1);
171  if (pos3 == string::npos) {
172  if (errorp != NULL) {
173  *errorp = make_error_code(boost::system::errc::invalid_argument);
174  }
175  return null_prefix;
176  }
177  temp_str = str.substr(pos2 + 1, pos3 - pos2 - 1);
178  boost::system::error_code rtid_err;
179  prefix.router_id_ = Ip4Address::from_string(temp_str, rtid_err);
180  if (rtid_err.failed()) {
181  if (errorp != NULL) {
182  *errorp = rtid_err;
183  }
184  return null_prefix;
185  }
186 
187  // Look for group.
188  size_t pos4 = str.find(',', pos3 + 1);
189  if (pos4 == string::npos) {
190  if (errorp != NULL) {
191  *errorp = make_error_code(boost::system::errc::invalid_argument);
192  }
193  return null_prefix;
194  }
195  temp_str = str.substr(pos3 + 1, pos4 - pos3 - 1);
196  boost::system::error_code group_err;
197  prefix.group_ = Ip4Address::from_string(temp_str, group_err);
198  if (group_err.failed()) {
199  if (errorp != NULL) {
200  *errorp = group_err;
201  }
202  return null_prefix;
203  }
204 
205  // Rest is source.
206  temp_str = str.substr(pos4 + 1, string::npos);
207  boost::system::error_code source_err;
208  prefix.source_ = Ip4Address::from_string(temp_str, source_err);
209  if (source_err.failed()) {
210  if (errorp != NULL) {
211  *errorp = source_err;
212  }
213  return null_prefix;
214  }
215 
216  return prefix;
217 }
218 
219 string ErmVpnPrefix::ToString() const {
220  string repr = integerToString(type_);
221  repr += "-" + rd_.ToString();
222  repr += "-" + router_id_.to_string();
223  repr += "," + group_.to_string();
224  repr += "," + source_.to_string();
225  return repr;
226 }
227 
229  assert(type_ == 0);
230  string repr = rd_.ToString();
231  repr += ":" + group_.to_string();
232  repr += "," + source_.to_string();
233  return repr;
234 }
235 
237  return (type == LocalTreeRoute || type == GlobalTreeRoute);
238 }
239 
241  return (type == NativeRoute || IsValidForBgp(type));
242 }
243 
244 bool ErmVpnPrefix::operator==(const ErmVpnPrefix &rhs) const {
245  return (
246  type_ == rhs.type_ &&
247  rd_ == rhs.rd_ &&
248  router_id_ == rhs.router_id_ &&
249  group_ == rhs.group_ &&
250  source_ == rhs.source_);
251 }
252 
253 ErmVpnRoute::ErmVpnRoute(const ErmVpnPrefix &prefix) : prefix_(prefix) {
254 }
255 
256 int ErmVpnRoute::CompareTo(const Route &rhs) const {
257  const ErmVpnRoute &other = static_cast<const ErmVpnRoute &>(rhs);
259  KEY_COMPARE(prefix_.group(), other.prefix_.group());
260  KEY_COMPARE(prefix_.type(), other.prefix_.type());
261  KEY_COMPARE(
264  return 0;
265 }
266 
267 string ErmVpnRoute::ToString() const {
268  return prefix_.ToString();
269 }
270 
272  if (xmpp_id_str_.empty())
274  return xmpp_id_str_;
275 }
276 
277 bool ErmVpnRoute::IsValid() const {
278  if (!BgpRoute::IsValid())
279  return false;
280 
281  const BgpAttr *attr = BestPath()->GetAttr();
282  switch (prefix_.type()) {
284  return attr->label_block().get() != NULL;
286  return attr->edge_discovery() != NULL;
288  return attr->edge_forwarding() != NULL;
290  break;
291  }
292 
293  return false;
294 }
295 
296 void ErmVpnRoute::SetKey(const DBRequestKey *reqkey) {
297  const ErmVpnTable::RequestKey *key =
298  static_cast<const ErmVpnTable::RequestKey *>(reqkey);
299  prefix_ = key->prefix;
300 }
301 
303  const BgpAttr *attr, uint32_t label, uint32_t l3_label) const {
304  prefix_.BuildProtoPrefix(prefix);
305 }
306 
308  vector<uint8_t> &nh, IpAddress nexthop) const {
309  nh.resize(4);
310  const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes();
311  copy(addr_bytes.begin(), addr_bytes.end(), nh.begin());
312 }
313 
316  key = new ErmVpnTable::RequestKey(GetPrefix(), NULL);
317  return KeyPtr(key);
318 }
319 
320 const std::string ErmVpnPrefix::GetType() const {
321  switch (type_) {
322  case NativeRoute:
323  return "NativeRoute";
324  case LocalTreeRoute:
325  return "LocalTreeRoute";
326  case GlobalTreeRoute:
327  return "GlobalTreeRoute";
328  case Invalid:
329  return "Invalid";
330  }
331  return "";
332 }
333 
334 const std::string ErmVpnRoute::GetType() const {
335  return GetPrefix().GetType();
336 }
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:999
static const uint8_t kMaxV4PrefixLen
Definition: address.h:21
Family
Definition: address.h:25
static const uint8_t kMaxV4Bytes
Definition: address.h:20
const EdgeForwarding * edge_forwarding() const
Definition: bgp_attr.h:927
LabelBlockPtr label_block() const
Definition: bgp_attr.h:930
const EdgeDiscovery * edge_discovery() const
Definition: bgp_attr.h:924
const BgpAttr * GetAttr() const
Definition: bgp_path.h:88
const BgpPath * BestPath() const
Definition: bgp_route.cc:48
virtual bool IsValid() const
Definition: bgp_route.cc:340
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
Ip4Address group_
Definition: ermvpn_route.h:70
Ip4Address source() const
Definition: ermvpn_route.h:61
static bool IsValid(uint8_t type)
Ip4Address source_
Definition: ermvpn_route.h:71
const std::string GetType() const
RouteDistinguisher rd_
Definition: ermvpn_route.h:68
static bool IsValidForBgp(uint8_t type)
uint8_t type() const
Definition: ermvpn_route.h:57
Ip4Address router_id() const
Definition: ermvpn_route.h:59
static ErmVpnPrefix FromString(const std::string &str, boost::system::error_code *errorp=NULL)
uint8_t type_
Definition: ermvpn_route.h:67
std::string ToXmppIdString() const
Ip4Address router_id_
Definition: ermvpn_route.h:69
const RouteDistinguisher & route_distinguisher() const
Definition: ermvpn_route.h:58
Ip4Address group() const
Definition: ermvpn_route.h:60
static int FromProtoPrefix(const BgpProtoPrefix &proto_prefix, ErmVpnPrefix *prefix)
Definition: ermvpn_route.cc:50
void BuildProtoPrefix(BgpProtoPrefix *prefix) const
Definition: ermvpn_route.cc:95
std::string ToString() const
bool operator==(const ErmVpnPrefix &rhs) const
virtual std::string ToXmppIdString() const
virtual std::string ToString() const
std::string xmpp_id_str_
Definition: ermvpn_route.h:103
ErmVpnRoute(const ErmVpnPrefix &prefix)
virtual void BuildProtoPrefix(BgpProtoPrefix *prefix, const BgpAttr *attr=NULL, uint32_t label=0, uint32_t l3_label=0) const
ErmVpnPrefix prefix_
Definition: ermvpn_route.h:102
virtual void SetKey(const DBRequestKey *reqkey)
virtual bool IsValid() const
virtual void BuildBgpProtoNextHop(std::vector< uint8_t > &nh, IpAddress nexthop) const
virtual int CompareTo(const Route &rhs) const
const ErmVpnPrefix & GetPrefix() const
Definition: ermvpn_route.h:82
virtual KeyPtr GetDBRequestKey() const
const std::string GetType() const
static RouteDistinguisher FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: rd.cc:84
static const size_t kSize
Definition: rd.h:14
const uint8_t * GetData() const
Definition: rd.h:60
std::string ToString() const
Definition: rd.cc:57
Definition: route.h:15
uint8_t type
Definition: load_balance.h:2
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40
bool stringToInteger(const std::string &str, NumberType &num)
Definition: string_util.h:72
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
std::vector< uint8_t > prefix
#define KEY_COMPARE(x, y)
Definition: util.h:71