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