blob: 39b2b1ae1e84c6d6cdfd3bbd8871b7dc64dfdeff [file] [log] [blame]
Neale Ranns177bbdc2016-11-15 09:46:51 +00001"""
2 IP Routes
3
4 object abstractions for representing IP routes in VPP
5"""
6
Neale Ranns5a8123b2017-01-26 01:18:23 -08007from vpp_object import *
Neale Rannsb3b2de72017-03-08 05:17:22 -08008from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
Neale Ranns177bbdc2016-11-15 09:46:51 +00009
Neale Rannsad422ed2016-11-02 14:20:04 +000010# from vnet/vnet/mpls/mpls_types.h
11MPLS_IETF_MAX_LABEL = 0xfffff
12MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
Neale Ranns177bbdc2016-11-15 09:46:51 +000013
Neale Ranns177bbdc2016-11-15 09:46:51 +000014
Neale Ranns180279b2017-03-16 15:49:09 -040015class MRouteItfFlags:
16 MFIB_ITF_FLAG_NONE = 0
17 MFIB_ITF_FLAG_NEGATE_SIGNAL = 1
18 MFIB_ITF_FLAG_ACCEPT = 2
19 MFIB_ITF_FLAG_FORWARD = 4
20 MFIB_ITF_FLAG_SIGNAL_PRESENT = 8
21 MFIB_ITF_FLAG_INTERNAL_COPY = 16
22
23
24class MRouteEntryFlags:
25 MFIB_ENTRY_FLAG_NONE = 0
26 MFIB_ENTRY_FLAG_SIGNAL = 1
27 MFIB_ENTRY_FLAG_DROP = 2
28 MFIB_ENTRY_FLAG_CONNECTED = 4
29 MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8
30
31
Neale Rannsda78f952017-05-24 09:15:43 -070032class DpoProto:
33 DPO_PROTO_IP4 = 0
34 DPO_PROTO_IP6 = 1
35 DPO_PROTO_MPLS = 2
36 DPO_PROTO_ETHERNET = 3
Neale Rannsd792d9c2017-10-21 10:53:20 -070037 DPO_PROTO_BIER = 4
38 DPO_PROTO_NSH = 5
Neale Rannsda78f952017-05-24 09:15:43 -070039
40
Neale Ranns31ed7442018-02-23 05:29:09 -080041class MplsLspMode:
42 PIPE = 0
43 UNIFORM = 1
44
45
Neale Rannsb3b2de72017-03-08 05:17:22 -080046def find_route(test, ip_addr, len, table_id=0, inet=AF_INET):
47 if inet == AF_INET:
48 s = 4
49 routes = test.vapi.ip_fib_dump()
50 else:
51 s = 16
52 routes = test.vapi.ip6_fib_dump()
53
54 route_addr = inet_pton(inet, ip_addr)
55 for e in routes:
56 if route_addr == e.address[:s] \
57 and len == e.address_length \
58 and table_id == e.table_id:
59 return True
60 return False
61
62
Neale Ranns15002542017-09-10 04:39:11 -070063class VppIpTable(VppObject):
64
65 def __init__(self,
66 test,
67 table_id,
68 is_ip6=0):
69 self._test = test
70 self.table_id = table_id
71 self.is_ip6 = is_ip6
72
73 def add_vpp_config(self):
74 self._test.vapi.ip_table_add_del(
75 self.table_id,
76 is_ipv6=self.is_ip6,
77 is_add=1)
78 self._test.registry.register(self, self._test.logger)
79
80 def remove_vpp_config(self):
81 self._test.vapi.ip_table_add_del(
82 self.table_id,
83 is_ipv6=self.is_ip6,
84 is_add=0)
85
86 def query_vpp_config(self):
87 # find the default route
88 return find_route(self._test,
89 "::" if self.is_ip6 else "0.0.0.0",
90 0,
91 self.table_id,
92 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
93
94 def __str__(self):
95 return self.object_id()
96
97 def object_id(self):
98 return ("table-%s-%d" %
99 ("v6" if self.is_ip6 == 1 else "v4",
100 self.table_id))
101
102
Neale Ranns31ed7442018-02-23 05:29:09 -0800103class VppMplsLabel(object):
104 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
105 self.value = value
106 self.mode = mode
107 self.ttl = ttl
108 self.exp = exp
109
110 def encode(self):
111 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
112 return {'label': self.value,
113 'ttl': self.ttl,
114 'exp': self.exp,
115 'is_uniform': is_uniform}
116
117
Neale Ranns5a8123b2017-01-26 01:18:23 -0800118class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000119
Klement Sekerada505f62017-01-04 12:58:53 +0100120 def __init__(
121 self,
122 nh_addr,
123 nh_sw_if_index,
124 nh_table_id=0,
125 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800126 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800127 rpf_id=0,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700128 is_interface_rx=0,
129 is_resolve_host=0,
Neale Rannsda78f952017-05-24 09:15:43 -0700130 is_resolve_attached=0,
Neale Ranns054c03a2017-10-13 05:15:07 -0700131 is_source_lookup=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800132 is_udp_encap=0,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800133 is_dvr=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800134 next_hop_id=0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -0700135 proto=DpoProto.DPO_PROTO_IP4):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000136 self.nh_itf = nh_sw_if_index
137 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000138 self.nh_via_label = nh_via_label
139 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800140 self.weight = 1
141 self.rpf_id = rpf_id
Neale Rannsda78f952017-05-24 09:15:43 -0700142 self.proto = proto
143 if self.proto is DpoProto.DPO_PROTO_IP6:
144 self.nh_addr = inet_pton(AF_INET6, nh_addr)
145 elif self.proto is DpoProto.DPO_PROTO_IP4:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800146 self.nh_addr = inet_pton(AF_INET, nh_addr)
Neale Ranns31426c62017-05-24 10:32:58 -0700147 else:
Neale Rannsda78f952017-05-24 09:15:43 -0700148 self.nh_addr = inet_pton(AF_INET6, "::")
Neale Rannsf12a83f2017-04-18 09:09:40 -0700149 self.is_resolve_host = is_resolve_host
150 self.is_resolve_attached = is_resolve_attached
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800151 self.is_interface_rx = is_interface_rx
Neale Ranns054c03a2017-10-13 05:15:07 -0700152 self.is_source_lookup = is_source_lookup
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800153 self.is_rpf_id = 0
154 if rpf_id != 0:
155 self.is_rpf_id = 1
156 self.nh_itf = rpf_id
Neale Ranns810086d2017-11-05 16:26:46 -0800157 self.is_udp_encap = is_udp_encap
158 self.next_hop_id = next_hop_id
Neale Rannsf068c3e2018-01-03 04:18:48 -0800159 self.is_dvr = is_dvr
Neale Ranns177bbdc2016-11-15 09:46:51 +0000160
Neale Ranns31ed7442018-02-23 05:29:09 -0800161 def encode_labels(self):
162 lstack = []
163 for l in self.nh_labels:
164 if type(l) == VppMplsLabel:
165 lstack.append(l.encode())
166 else:
167 lstack.append({'label': l,
168 'ttl': 255})
169 return lstack
170
Neale Ranns2303cb12018-02-21 04:57:17 -0800171 def encode(self):
172 return {'next_hop': self.nh_addr,
173 'weight': 1,
174 'afi': 0,
175 'preference': 0,
176 'table_id': self.nh_table_id,
177 'next_hop_id': self.next_hop_id,
178 'sw_if_index': self.nh_itf,
179 'afi': self.proto,
180 'is_udp_encap': self.is_udp_encap,
181 'n_labels': len(self.nh_labels),
182 'label_stack': self.encode_labels()}
183
Neale Ranns177bbdc2016-11-15 09:46:51 +0000184
Neale Ranns5a8123b2017-01-26 01:18:23 -0800185class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000186
Neale Rannsd792d9c2017-10-21 10:53:20 -0700187 def __init__(self, nh_sw_if_index, flags,
188 proto=DpoProto.DPO_PROTO_IP4,
189 bier_imp=0):
190 super(VppMRoutePath, self).__init__(
191 "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0",
192 nh_sw_if_index,
193 proto=proto)
Neale Ranns32e1c012016-11-22 17:07:28 +0000194 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700195 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000196
197
Neale Ranns5a8123b2017-01-26 01:18:23 -0800198class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000199 """
200 IP Route
201 """
202
203 def __init__(self, test, dest_addr,
Neale Ranns37be7362017-02-21 17:30:26 -0800204 dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0,
205 is_unreach=0, is_prohibit=0):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000206 self._test = test
207 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000208 self.dest_addr_len = dest_addr_len
209 self.table_id = table_id
Neale Rannsfca0c242017-01-13 07:57:46 -0800210 self.is_ip6 = is_ip6
211 self.is_local = is_local
Neale Ranns37be7362017-02-21 17:30:26 -0800212 self.is_unreach = is_unreach
213 self.is_prohibit = is_prohibit
Neale Rannsb3b2de72017-03-08 05:17:22 -0800214 self.dest_addr_p = dest_addr
Neale Rannsfca0c242017-01-13 07:57:46 -0800215 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800216 self.dest_addr = inet_pton(AF_INET6, dest_addr)
Neale Rannsfca0c242017-01-13 07:57:46 -0800217 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800218 self.dest_addr = inet_pton(AF_INET, dest_addr)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000219
Neale Ranns69b7aa42017-03-10 03:04:12 -0800220 def modify(self, paths, is_local=0,
221 is_unreach=0, is_prohibit=0):
222 self.paths = paths
223 self.is_local = is_local
224 self.is_unreach = is_unreach
225 self.is_prohibit = is_prohibit
226
Neale Ranns177bbdc2016-11-15 09:46:51 +0000227 def add_vpp_config(self):
Neale Ranns37be7362017-02-21 17:30:26 -0800228 if self.is_local or self.is_unreach or self.is_prohibit:
Klement Sekerada505f62017-01-04 12:58:53 +0100229 self._test.vapi.ip_add_del_route(
230 self.dest_addr,
231 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800232 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800233 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800234 is_local=self.is_local,
235 is_unreach=self.is_unreach,
236 is_prohibit=self.is_prohibit,
Klement Sekerada505f62017-01-04 12:58:53 +0100237 table_id=self.table_id,
Neale Rannsfca0c242017-01-13 07:57:46 -0800238 is_ipv6=self.is_ip6)
239 else:
240 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800241 lstack = path.encode_labels()
242
Neale Rannsfca0c242017-01-13 07:57:46 -0800243 self._test.vapi.ip_add_del_route(
244 self.dest_addr,
245 self.dest_addr_len,
246 path.nh_addr,
247 path.nh_itf,
248 table_id=self.table_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800249 next_hop_out_label_stack=lstack,
250 next_hop_n_out_labels=len(lstack),
Neale Rannsfca0c242017-01-13 07:57:46 -0800251 next_hop_via_label=path.nh_via_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700252 next_hop_table_id=path.nh_table_id,
Neale Ranns810086d2017-11-05 16:26:46 -0800253 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700254 is_ipv6=self.is_ip6,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800255 is_dvr=path.is_dvr,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700256 is_resolve_host=path.is_resolve_host,
257 is_resolve_attached=path.is_resolve_attached,
Neale Ranns054c03a2017-10-13 05:15:07 -0700258 is_source_lookup=path.is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800259 is_udp_encap=path.is_udp_encap,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700260 is_multipath=1 if len(self.paths) > 1 else 0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800261 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000262
263 def remove_vpp_config(self):
Neale Ranns37be7362017-02-21 17:30:26 -0800264 if self.is_local or self.is_unreach or self.is_prohibit:
Neale Rannsfca0c242017-01-13 07:57:46 -0800265 self._test.vapi.ip_add_del_route(
266 self.dest_addr,
267 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800268 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800269 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800270 is_local=self.is_local,
271 is_unreach=self.is_unreach,
272 is_prohibit=self.is_prohibit,
Neale Rannsfca0c242017-01-13 07:57:46 -0800273 is_add=0,
274 table_id=self.table_id,
275 is_ipv6=self.is_ip6)
276 else:
277 for path in self.paths:
Neale Rannsf12a83f2017-04-18 09:09:40 -0700278 self._test.vapi.ip_add_del_route(
279 self.dest_addr,
280 self.dest_addr_len,
281 path.nh_addr,
282 path.nh_itf,
283 table_id=self.table_id,
284 next_hop_table_id=path.nh_table_id,
285 next_hop_via_label=path.nh_via_label,
Neale Ranns810086d2017-11-05 16:26:46 -0800286 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700287 is_add=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800288 is_udp_encap=path.is_udp_encap,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800289 is_ipv6=self.is_ip6,
290 is_dvr=path.is_dvr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000291
Neale Ranns5a8123b2017-01-26 01:18:23 -0800292 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800293 return find_route(self._test,
294 self.dest_addr_p,
295 self.dest_addr_len,
296 self.table_id,
297 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Rannsad422ed2016-11-02 14:20:04 +0000298
Neale Ranns5a8123b2017-01-26 01:18:23 -0800299 def __str__(self):
300 return self.object_id()
301
302 def object_id(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800303 return ("%d:%s/%d"
304 % (self.table_id,
305 self.dest_addr_p,
306 self.dest_addr_len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800307
308
309class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000310 """
311 IP Multicast Route
312 """
313
314 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800315 grp_addr_len, e_flags, paths, table_id=0,
316 rpf_id=0, is_ip6=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000317 self._test = test
318 self.paths = paths
319 self.grp_addr_len = grp_addr_len
320 self.table_id = table_id
321 self.e_flags = e_flags
322 self.is_ip6 = is_ip6
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800323 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000324
325 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800326 self.grp_addr = inet_pton(AF_INET6, grp_addr)
327 self.src_addr = inet_pton(AF_INET6, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000328 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800329 self.grp_addr = inet_pton(AF_INET, grp_addr)
330 self.src_addr = inet_pton(AF_INET, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000331
332 def add_vpp_config(self):
333 for path in self.paths:
334 self._test.vapi.ip_mroute_add_del(self.src_addr,
335 self.grp_addr,
336 self.grp_addr_len,
337 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700338 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000339 path.nh_itf,
340 path.nh_i_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700341 bier_imp=path.bier_imp,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800342 rpf_id=self.rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +0000343 table_id=self.table_id,
344 is_ipv6=self.is_ip6)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800345 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000346
347 def remove_vpp_config(self):
348 for path in self.paths:
349 self._test.vapi.ip_mroute_add_del(self.src_addr,
350 self.grp_addr,
351 self.grp_addr_len,
352 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700353 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000354 path.nh_itf,
355 path.nh_i_flags,
356 table_id=self.table_id,
Neale Rannseea537a2018-01-09 04:11:28 -0800357 bier_imp=path.bier_imp,
Neale Ranns32e1c012016-11-22 17:07:28 +0000358 is_add=0,
359 is_ipv6=self.is_ip6)
360
361 def update_entry_flags(self, flags):
362 self.e_flags = flags
363 self._test.vapi.ip_mroute_add_del(self.src_addr,
364 self.grp_addr,
365 self.grp_addr_len,
366 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700367 0,
Neale Ranns32e1c012016-11-22 17:07:28 +0000368 0xffffffff,
369 0,
370 table_id=self.table_id,
371 is_ipv6=self.is_ip6)
372
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800373 def update_rpf_id(self, rpf_id):
374 self.rpf_id = rpf_id
375 self._test.vapi.ip_mroute_add_del(self.src_addr,
376 self.grp_addr,
377 self.grp_addr_len,
378 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700379 0,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800380 0xffffffff,
381 0,
382 rpf_id=self.rpf_id,
383 table_id=self.table_id,
384 is_ipv6=self.is_ip6)
385
Neale Ranns32e1c012016-11-22 17:07:28 +0000386 def update_path_flags(self, itf, flags):
387 for path in self.paths:
388 if path.nh_itf == itf:
389 path.nh_i_flags = flags
390 break
391 self._test.vapi.ip_mroute_add_del(self.src_addr,
392 self.grp_addr,
393 self.grp_addr_len,
394 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700395 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000396 path.nh_itf,
397 path.nh_i_flags,
398 table_id=self.table_id,
399 is_ipv6=self.is_ip6)
400
Neale Ranns5a8123b2017-01-26 01:18:23 -0800401 def query_vpp_config(self):
Neale Rannsd792d9c2017-10-21 10:53:20 -0700402 if self.is_ip6:
403 dump = self._test.vapi.ip6_mfib_dump()
404 else:
405 dump = self._test.vapi.ip_mfib_dump()
Neale Ranns5a8123b2017-01-26 01:18:23 -0800406 for e in dump:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700407 if self.grp_addr == e.grp_address \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800408 and self.grp_addr_len == e.address_length \
Neale Rannsd792d9c2017-10-21 10:53:20 -0700409 and self.src_addr == e.src_address \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800410 and self.table_id == e.table_id:
411 return True
412 return False
Neale Ranns32e1c012016-11-22 17:07:28 +0000413
Neale Ranns5a8123b2017-01-26 01:18:23 -0800414 def __str__(self):
415 return self.object_id()
416
417 def object_id(self):
418 if self.is_ip6:
419 return ("%d:(%s,%s/%d)"
420 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800421 inet_ntop(AF_INET6, self.src_addr),
422 inet_ntop(AF_INET6, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800423 self.grp_addr_len))
424 else:
425 return ("%d:(%s,%s/%d)"
426 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800427 inet_ntop(AF_INET, self.src_addr),
428 inet_ntop(AF_INET, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800429 self.grp_addr_len))
430
431
432class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000433 def __init__(self, test, route, interface, packet):
434 self.route = route
435 self.interface = interface
436 self.packet = packet
437 self.test = test
438
439 def compare(self, signal):
440 self.test.assertEqual(self.interface, signal.sw_if_index)
441 self.test.assertEqual(self.route.table_id, signal.table_id)
442 self.test.assertEqual(self.route.grp_addr_len,
443 signal.grp_address_len)
444 for i in range(self.route.grp_addr_len / 8):
445 self.test.assertEqual(self.route.grp_addr[i],
446 signal.grp_address[i])
447 if (self.route.grp_addr_len > 32):
448 for i in range(4):
449 self.test.assertEqual(self.route.src_addr[i],
450 signal.src_address[i])
451
452
Neale Ranns5a8123b2017-01-26 01:18:23 -0800453class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000454 """
455 MPLS to IP Binding
456 """
457
Neale Ranns5a8123b2017-01-26 01:18:23 -0800458 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700459 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000460 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000461 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700462 self.dest_addr = dest_addr
Neale Rannsad422ed2016-11-02 14:20:04 +0000463 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800464 self.table_id = table_id
465 self.ip_table_id = ip_table_id
Neale Rannsf12a83f2017-04-18 09:09:40 -0700466 self.is_ip6 = is_ip6
467 if is_ip6:
468 self.dest_addrn = inet_pton(AF_INET6, dest_addr)
469 else:
470 self.dest_addrn = inet_pton(AF_INET, dest_addr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000471
472 def add_vpp_config(self):
473 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700474 self.dest_addrn,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800475 self.dest_addr_len,
476 table_id=self.table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700477 ip_table_id=self.ip_table_id,
478 is_ip4=(self.is_ip6 == 0))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800479 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000480
481 def remove_vpp_config(self):
482 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700483 self.dest_addrn,
Neale Rannsad422ed2016-11-02 14:20:04 +0000484 self.dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700485 table_id=self.table_id,
486 ip_table_id=self.ip_table_id,
487 is_bind=0,
488 is_ip4=(self.is_ip6 == 0))
Neale Rannsad422ed2016-11-02 14:20:04 +0000489
Neale Ranns5a8123b2017-01-26 01:18:23 -0800490 def query_vpp_config(self):
491 dump = self._test.vapi.mpls_fib_dump()
492 for e in dump:
493 if self.local_label == e.label \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800494 and self.table_id == e.table_id:
495 return True
496 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000497
Neale Ranns5a8123b2017-01-26 01:18:23 -0800498 def __str__(self):
499 return self.object_id()
500
501 def object_id(self):
502 return ("%d:%s binds %d:%s/%d"
503 % (self.table_id,
504 self.local_label,
505 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700506 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800507 self.dest_addr_len))
508
509
Neale Ranns15002542017-09-10 04:39:11 -0700510class VppMplsTable(VppObject):
511
512 def __init__(self,
513 test,
514 table_id):
515 self._test = test
516 self.table_id = table_id
517
518 def add_vpp_config(self):
519 self._test.vapi.mpls_table_add_del(
520 self.table_id,
521 is_add=1)
522 self._test.registry.register(self, self._test.logger)
523
524 def remove_vpp_config(self):
525 self._test.vapi.mpls_table_add_del(
526 self.table_id,
527 is_add=0)
528
529 def query_vpp_config(self):
530 # find the default route
531 dump = self._test.vapi.mpls_fib_dump()
532 if len(dump):
533 return True
534 return False
535
536 def __str__(self):
537 return self.object_id()
538
539 def object_id(self):
540 return ("table-mpls-%d" % (self.table_id))
541
542
Neale Ranns5a8123b2017-01-26 01:18:23 -0800543class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000544 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800545 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000546 """
547
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800548 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
549 is_multicast=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000550 self._test = test
551 self.paths = paths
552 self.local_label = local_label
553 self.eos_bit = eos_bit
554 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800555 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000556
557 def add_vpp_config(self):
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800558 is_multipath = len(self.paths) > 1
Neale Rannsad422ed2016-11-02 14:20:04 +0000559 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800560 lstack = path.encode_labels()
561
Klement Sekerada505f62017-01-04 12:58:53 +0100562 self._test.vapi.mpls_route_add_del(
563 self.local_label,
564 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700565 path.proto,
Klement Sekerada505f62017-01-04 12:58:53 +0100566 path.nh_addr,
567 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800568 is_multicast=self.is_multicast,
569 is_multipath=is_multipath,
Klement Sekerada505f62017-01-04 12:58:53 +0100570 table_id=self.table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800571 is_interface_rx=path.is_interface_rx,
572 is_rpf_id=path.is_rpf_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800573 next_hop_out_label_stack=lstack,
574 next_hop_n_out_labels=len(lstack),
Klement Sekerada505f62017-01-04 12:58:53 +0100575 next_hop_via_label=path.nh_via_label,
576 next_hop_table_id=path.nh_table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800577 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000578
579 def remove_vpp_config(self):
580 for path in self.paths:
581 self._test.vapi.mpls_route_add_del(self.local_label,
582 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700583 path.proto,
Neale Rannsad422ed2016-11-02 14:20:04 +0000584 path.nh_addr,
585 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800586 is_rpf_id=path.is_rpf_id,
Neale Rannsad422ed2016-11-02 14:20:04 +0000587 table_id=self.table_id,
588 is_add=0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800589
590 def query_vpp_config(self):
591 dump = self._test.vapi.mpls_fib_dump()
592 for e in dump:
593 if self.local_label == e.label \
594 and self.eos_bit == e.eos_bit \
595 and self.table_id == e.table_id:
596 return True
597 return False
598
599 def __str__(self):
600 return self.object_id()
601
602 def object_id(self):
603 return ("%d:%s/%d"
604 % (self.table_id,
605 self.local_label,
606 20+self.eos_bit))