blob: b3d12938d9984c7a34b6ce7159e2e4d99c667b32 [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 Rannsc0a93142018-09-05 15:42:26 -07009from vpp_ip import *
Neale Ranns177bbdc2016-11-15 09:46:51 +000010
Neale Rannsad422ed2016-11-02 14:20:04 +000011# from vnet/vnet/mpls/mpls_types.h
12MPLS_IETF_MAX_LABEL = 0xfffff
13MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
Neale Ranns177bbdc2016-11-15 09:46:51 +000014
Neale Ranns177bbdc2016-11-15 09:46:51 +000015
Neale Ranns180279b2017-03-16 15:49:09 -040016class MRouteItfFlags:
17 MFIB_ITF_FLAG_NONE = 0
18 MFIB_ITF_FLAG_NEGATE_SIGNAL = 1
19 MFIB_ITF_FLAG_ACCEPT = 2
20 MFIB_ITF_FLAG_FORWARD = 4
21 MFIB_ITF_FLAG_SIGNAL_PRESENT = 8
22 MFIB_ITF_FLAG_INTERNAL_COPY = 16
23
24
25class MRouteEntryFlags:
26 MFIB_ENTRY_FLAG_NONE = 0
27 MFIB_ENTRY_FLAG_SIGNAL = 1
28 MFIB_ENTRY_FLAG_DROP = 2
29 MFIB_ENTRY_FLAG_CONNECTED = 4
30 MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8
31
32
Neale Ranns31ed7442018-02-23 05:29:09 -080033class MplsLspMode:
34 PIPE = 0
35 UNIFORM = 1
36
37
Neale Ranns93cc3ee2018-10-10 07:22:51 -070038def ip_to_dpo_proto(addr):
39 if addr.version is 6:
40 return DpoProto.DPO_PROTO_IP6
41 else:
42 return DpoProto.DPO_PROTO_IP4
43
44
Neale Rannsb3b2de72017-03-08 05:17:22 -080045def find_route(test, ip_addr, len, table_id=0, inet=AF_INET):
46 if inet == AF_INET:
47 s = 4
48 routes = test.vapi.ip_fib_dump()
49 else:
50 s = 16
51 routes = test.vapi.ip6_fib_dump()
52
53 route_addr = inet_pton(inet, ip_addr)
54 for e in routes:
55 if route_addr == e.address[:s] \
56 and len == e.address_length \
57 and table_id == e.table_id:
58 return True
59 return False
60
61
Neale Ranns947ea622018-06-07 23:48:20 -070062def find_mroute(test, grp_addr, src_addr, grp_addr_len,
63 table_id=0, inet=AF_INET):
64 if inet == AF_INET:
65 s = 4
66 routes = test.vapi.ip_mfib_dump()
67 else:
68 s = 16
69 routes = test.vapi.ip6_mfib_dump()
70 gaddr = inet_pton(inet, grp_addr)
71 saddr = inet_pton(inet, src_addr)
72 for e in routes:
73 if gaddr == e.grp_address[:s] \
74 and grp_addr_len == e.address_length \
75 and saddr == e.src_address[:s] \
76 and table_id == e.table_id:
77 return True
78 return False
79
80
Neale Ranns775f73c2018-12-20 03:01:49 -080081def find_mpls_route(test, table_id, label, eos_bit, paths=None):
82 dump = test.vapi.mpls_fib_dump()
83 for e in dump:
84 if label == e.label \
85 and eos_bit == e.eos_bit \
86 and table_id == e.table_id:
87 if not paths:
88 return True
89 else:
90 if (len(paths) != len(e.path)):
91 return False
92 for i in range(len(paths)):
93 if (paths[i] != e.path[i]):
94 return False
95 return True
96 return False
97
98
Neale Ranns93cc3ee2018-10-10 07:22:51 -070099def fib_interface_ip_prefix(test, address, length, sw_if_index):
100 vp = VppIpPrefix(address, length)
101 addrs = test.vapi.ip_address_dump(sw_if_index, is_ipv6=vp.is_ip6)
102
103 if vp.is_ip6:
104 n = 16
105 else:
106 n = 4
107
108 for a in addrs:
109 if a.prefix_length == length and \
110 a.sw_if_index == sw_if_index and \
111 a.ip[:n] == vp.bytes:
112 return True
113 return False
114
115
Neale Ranns15002542017-09-10 04:39:11 -0700116class VppIpTable(VppObject):
117
118 def __init__(self,
119 test,
120 table_id,
121 is_ip6=0):
122 self._test = test
123 self.table_id = table_id
124 self.is_ip6 = is_ip6
125
126 def add_vpp_config(self):
127 self._test.vapi.ip_table_add_del(
128 self.table_id,
129 is_ipv6=self.is_ip6,
130 is_add=1)
131 self._test.registry.register(self, self._test.logger)
132
133 def remove_vpp_config(self):
134 self._test.vapi.ip_table_add_del(
135 self.table_id,
136 is_ipv6=self.is_ip6,
137 is_add=0)
138
139 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700140 if self.table_id == 0:
141 # the default table always exists
142 return False
Neale Ranns15002542017-09-10 04:39:11 -0700143 # find the default route
144 return find_route(self._test,
145 "::" if self.is_ip6 else "0.0.0.0",
146 0,
147 self.table_id,
148 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
149
150 def __str__(self):
151 return self.object_id()
152
153 def object_id(self):
154 return ("table-%s-%d" %
155 ("v6" if self.is_ip6 == 1 else "v4",
156 self.table_id))
157
158
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700159class VppIpInterfaceAddress(VppObject):
160
161 def __init__(self, test, intf, addr, len):
162 self._test = test
163 self.intf = intf
164 self.prefix = VppIpPrefix(addr, len)
165
166 def add_vpp_config(self):
167 self._test.vapi.sw_interface_add_del_address(
168 self.intf.sw_if_index,
169 self.prefix.bytes,
170 self.prefix.length,
171 is_add=1,
172 is_ipv6=self.prefix.is_ip6)
173 self._test.registry.register(self, self._test.logger)
174
175 def remove_vpp_config(self):
176 self._test.vapi.sw_interface_add_del_address(
177 self.intf.sw_if_index,
178 self.prefix.bytes,
179 self.prefix.length,
180 is_add=0,
181 is_ipv6=self.prefix.is_ip6)
182
183 def query_vpp_config(self):
184 return fib_interface_ip_prefix(self._test,
185 self.prefix.address,
186 self.prefix.length,
187 self.intf.sw_if_index)
188
189 def __str__(self):
190 return self.object_id()
191
192 def object_id(self):
193 return "interface-ip-%s-%s" % (self.intf, self.prefix)
194
195
196class VppIpInterfaceBind(VppObject):
197
198 def __init__(self, test, intf, table):
199 self._test = test
200 self.intf = intf
201 self.table = table
202
203 def add_vpp_config(self):
204 if self.table.is_ip6:
205 self.intf.set_table_ip6(self.table.table_id)
206 else:
207 self.intf.set_table_ip4(self.table.table_id)
208 self._test.registry.register(self, self._test.logger)
209
210 def remove_vpp_config(self):
211 if 0 == self.table.table_id:
212 return
213 if self.table.is_ip6:
214 self.intf.set_table_ip6(0)
215 else:
216 self.intf.set_table_ip4(0)
217
218 def query_vpp_config(self):
219 if 0 == self.table.table_id:
220 return False
221 return self._test.vapi.sw_interface_get_table(
222 self.intf.sw_if_index,
223 self.table.is_ip6).vrf_id == self.table.table_id
224
225 def __str__(self):
226 return self.object_id()
227
228 def object_id(self):
229 return "interface-bind-%s-%s" % (self.intf, self.table)
230
231
Neale Ranns31ed7442018-02-23 05:29:09 -0800232class VppMplsLabel(object):
233 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
234 self.value = value
235 self.mode = mode
236 self.ttl = ttl
237 self.exp = exp
238
239 def encode(self):
240 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
241 return {'label': self.value,
242 'ttl': self.ttl,
243 'exp': self.exp,
244 'is_uniform': is_uniform}
245
Neale Ranns775f73c2018-12-20 03:01:49 -0800246 def __eq__(self, other):
247 if isinstance(other, self.__class__):
248 return (self.value == other.value and
249 self.ttl == other.ttl and
250 self.exp == other.exp and
251 self.mode == other.mode)
252 elif hasattr(other, 'label'):
253 return (self.value == other.label and
254 self.ttl == other.ttl and
255 self.exp == other.exp and
256 (self.mode == MplsLspMode.UNIFORM) == other.is_uniform)
257 else:
258 return False
259
260 def __ne__(self, other):
261 return not (self == other)
262
Neale Ranns31ed7442018-02-23 05:29:09 -0800263
Neale Ranns5a8123b2017-01-26 01:18:23 -0800264class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000265
Klement Sekerada505f62017-01-04 12:58:53 +0100266 def __init__(
267 self,
268 nh_addr,
269 nh_sw_if_index,
270 nh_table_id=0,
271 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800272 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800273 rpf_id=0,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700274 is_interface_rx=0,
275 is_resolve_host=0,
Neale Rannsda78f952017-05-24 09:15:43 -0700276 is_resolve_attached=0,
Neale Ranns054c03a2017-10-13 05:15:07 -0700277 is_source_lookup=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800278 is_udp_encap=0,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800279 is_dvr=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800280 next_hop_id=0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -0700281 proto=DpoProto.DPO_PROTO_IP4):
Neale Ranns311124e2019-01-24 04:52:25 -0800282 self.proto = proto
Neale Ranns177bbdc2016-11-15 09:46:51 +0000283 self.nh_itf = nh_sw_if_index
284 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000285 self.nh_via_label = nh_via_label
286 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800287 self.weight = 1
288 self.rpf_id = rpf_id
Neale Rannsda78f952017-05-24 09:15:43 -0700289 if self.proto is DpoProto.DPO_PROTO_IP6:
290 self.nh_addr = inet_pton(AF_INET6, nh_addr)
291 elif self.proto is DpoProto.DPO_PROTO_IP4:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800292 self.nh_addr = inet_pton(AF_INET, nh_addr)
Neale Ranns31426c62017-05-24 10:32:58 -0700293 else:
Neale Rannsda78f952017-05-24 09:15:43 -0700294 self.nh_addr = inet_pton(AF_INET6, "::")
Neale Rannsf12a83f2017-04-18 09:09:40 -0700295 self.is_resolve_host = is_resolve_host
296 self.is_resolve_attached = is_resolve_attached
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800297 self.is_interface_rx = is_interface_rx
Neale Ranns054c03a2017-10-13 05:15:07 -0700298 self.is_source_lookup = is_source_lookup
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800299 self.is_rpf_id = 0
300 if rpf_id != 0:
301 self.is_rpf_id = 1
302 self.nh_itf = rpf_id
Neale Ranns810086d2017-11-05 16:26:46 -0800303 self.is_udp_encap = is_udp_encap
304 self.next_hop_id = next_hop_id
Neale Rannsf068c3e2018-01-03 04:18:48 -0800305 self.is_dvr = is_dvr
Neale Ranns177bbdc2016-11-15 09:46:51 +0000306
Neale Ranns31ed7442018-02-23 05:29:09 -0800307 def encode_labels(self):
308 lstack = []
309 for l in self.nh_labels:
310 if type(l) == VppMplsLabel:
311 lstack.append(l.encode())
312 else:
313 lstack.append({'label': l,
314 'ttl': 255})
315 return lstack
316
Neale Ranns2303cb12018-02-21 04:57:17 -0800317 def encode(self):
318 return {'next_hop': self.nh_addr,
319 'weight': 1,
320 'afi': 0,
321 'preference': 0,
322 'table_id': self.nh_table_id,
323 'next_hop_id': self.next_hop_id,
324 'sw_if_index': self.nh_itf,
325 'afi': self.proto,
326 'is_udp_encap': self.is_udp_encap,
327 'n_labels': len(self.nh_labels),
328 'label_stack': self.encode_labels()}
329
Neale Rannsef90ed02018-09-13 08:45:12 -0700330 def __eq__(self, other):
Neale Ranns775f73c2018-12-20 03:01:49 -0800331 if isinstance(other, self.__class__):
332 return self.nh_addr == other.nh_addr
333 elif hasattr(other, 'sw_if_index'):
334 # vl_api_fib_path_t
335 if (len(self.nh_labels) != other.n_labels):
336 return False
337 for i in range(len(self.nh_labels)):
338 if (self.nh_labels[i] != other.label_stack[i]):
339 return False
340 return self.nh_itf == other.sw_if_index
341 else:
342 return False
343
344 def __ne__(self, other):
345 return not (self == other)
Neale Rannsef90ed02018-09-13 08:45:12 -0700346
Neale Ranns177bbdc2016-11-15 09:46:51 +0000347
Neale Ranns5a8123b2017-01-26 01:18:23 -0800348class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000349
Neale Rannsd792d9c2017-10-21 10:53:20 -0700350 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700351 nh=None,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700352 proto=DpoProto.DPO_PROTO_IP4,
353 bier_imp=0):
Neale Rannse821ab12017-06-01 07:45:05 -0700354 if not nh:
355 nh = "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0"
356 super(VppMRoutePath, self).__init__(nh,
357 nh_sw_if_index,
358 proto=proto)
Neale Ranns32e1c012016-11-22 17:07:28 +0000359 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700360 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000361
362
Neale Ranns5a8123b2017-01-26 01:18:23 -0800363class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000364 """
365 IP Route
366 """
367
368 def __init__(self, test, dest_addr,
Neale Ranns37be7362017-02-21 17:30:26 -0800369 dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0,
Neale Ranns3b93be52018-09-07 01:48:54 -0700370 is_unreach=0, is_prohibit=0, is_drop=0):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000371 self._test = test
372 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000373 self.dest_addr_len = dest_addr_len
374 self.table_id = table_id
Neale Rannsfca0c242017-01-13 07:57:46 -0800375 self.is_ip6 = is_ip6
376 self.is_local = is_local
Neale Ranns37be7362017-02-21 17:30:26 -0800377 self.is_unreach = is_unreach
378 self.is_prohibit = is_prohibit
Neale Ranns3b93be52018-09-07 01:48:54 -0700379 self.is_drop = is_drop
Neale Rannsb3b2de72017-03-08 05:17:22 -0800380 self.dest_addr_p = dest_addr
Neale Rannsfca0c242017-01-13 07:57:46 -0800381 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800382 self.dest_addr = inet_pton(AF_INET6, dest_addr)
Neale Rannsfca0c242017-01-13 07:57:46 -0800383 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800384 self.dest_addr = inet_pton(AF_INET, dest_addr)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000385
Neale Ranns69b7aa42017-03-10 03:04:12 -0800386 def modify(self, paths, is_local=0,
387 is_unreach=0, is_prohibit=0):
388 self.paths = paths
389 self.is_local = is_local
390 self.is_unreach = is_unreach
391 self.is_prohibit = is_prohibit
392
Neale Ranns177bbdc2016-11-15 09:46:51 +0000393 def add_vpp_config(self):
Neale Ranns7425f922019-01-23 00:36:16 -0800394 if self.is_unreach or self.is_prohibit or self.is_drop:
Neale Ranns008dbe12018-09-07 09:32:36 -0700395 r = self._test.vapi.ip_add_del_route(
Klement Sekerada505f62017-01-04 12:58:53 +0100396 self.dest_addr,
397 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800398 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800399 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800400 is_local=self.is_local,
401 is_unreach=self.is_unreach,
402 is_prohibit=self.is_prohibit,
Neale Ranns3b93be52018-09-07 01:48:54 -0700403 is_drop=self.is_drop,
Klement Sekerada505f62017-01-04 12:58:53 +0100404 table_id=self.table_id,
Neale Rannsfca0c242017-01-13 07:57:46 -0800405 is_ipv6=self.is_ip6)
406 else:
407 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800408 lstack = path.encode_labels()
409
Neale Ranns008dbe12018-09-07 09:32:36 -0700410 r = self._test.vapi.ip_add_del_route(
Neale Rannsfca0c242017-01-13 07:57:46 -0800411 self.dest_addr,
412 self.dest_addr_len,
413 path.nh_addr,
414 path.nh_itf,
415 table_id=self.table_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800416 next_hop_out_label_stack=lstack,
417 next_hop_n_out_labels=len(lstack),
Neale Rannsfca0c242017-01-13 07:57:46 -0800418 next_hop_via_label=path.nh_via_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700419 next_hop_table_id=path.nh_table_id,
Neale Ranns810086d2017-11-05 16:26:46 -0800420 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700421 is_ipv6=self.is_ip6,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800422 is_dvr=path.is_dvr,
Neale Ranns7425f922019-01-23 00:36:16 -0800423 is_local=self.is_local,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700424 is_resolve_host=path.is_resolve_host,
425 is_resolve_attached=path.is_resolve_attached,
Neale Ranns054c03a2017-10-13 05:15:07 -0700426 is_source_lookup=path.is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800427 is_udp_encap=path.is_udp_encap,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700428 is_multipath=1 if len(self.paths) > 1 else 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700429 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800430 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000431
432 def remove_vpp_config(self):
Neale Ranns7425f922019-01-23 00:36:16 -0800433 if self.is_unreach or self.is_prohibit or self.is_drop:
Neale Rannsfca0c242017-01-13 07:57:46 -0800434 self._test.vapi.ip_add_del_route(
435 self.dest_addr,
436 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800437 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800438 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800439 is_local=self.is_local,
440 is_unreach=self.is_unreach,
441 is_prohibit=self.is_prohibit,
Neale Rannsfca0c242017-01-13 07:57:46 -0800442 is_add=0,
443 table_id=self.table_id,
444 is_ipv6=self.is_ip6)
445 else:
446 for path in self.paths:
Neale Rannsf12a83f2017-04-18 09:09:40 -0700447 self._test.vapi.ip_add_del_route(
448 self.dest_addr,
449 self.dest_addr_len,
450 path.nh_addr,
451 path.nh_itf,
452 table_id=self.table_id,
453 next_hop_table_id=path.nh_table_id,
454 next_hop_via_label=path.nh_via_label,
Neale Ranns810086d2017-11-05 16:26:46 -0800455 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700456 is_add=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800457 is_udp_encap=path.is_udp_encap,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800458 is_ipv6=self.is_ip6,
459 is_dvr=path.is_dvr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000460
Neale Ranns5a8123b2017-01-26 01:18:23 -0800461 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800462 return find_route(self._test,
463 self.dest_addr_p,
464 self.dest_addr_len,
465 self.table_id,
466 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Rannsad422ed2016-11-02 14:20:04 +0000467
Neale Ranns5a8123b2017-01-26 01:18:23 -0800468 def __str__(self):
469 return self.object_id()
470
471 def object_id(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800472 return ("%d:%s/%d"
473 % (self.table_id,
474 self.dest_addr_p,
475 self.dest_addr_len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800476
Neale Ranns008dbe12018-09-07 09:32:36 -0700477 def get_stats_to(self):
478 c = self._test.statistics.get_counter("/net/route/to")
479 return c[0][self.stats_index]
480
481 def get_stats_via(self):
482 c = self._test.statistics.get_counter("/net/route/via")
483 return c[0][self.stats_index]
484
Neale Ranns5a8123b2017-01-26 01:18:23 -0800485
486class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000487 """
488 IP Multicast Route
489 """
490
491 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800492 grp_addr_len, e_flags, paths, table_id=0,
493 rpf_id=0, is_ip6=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000494 self._test = test
495 self.paths = paths
496 self.grp_addr_len = grp_addr_len
497 self.table_id = table_id
498 self.e_flags = e_flags
499 self.is_ip6 = is_ip6
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800500 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000501
Neale Ranns947ea622018-06-07 23:48:20 -0700502 self.grp_addr_p = grp_addr
503 self.src_addr_p = src_addr
Neale Ranns32e1c012016-11-22 17:07:28 +0000504 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800505 self.grp_addr = inet_pton(AF_INET6, grp_addr)
506 self.src_addr = inet_pton(AF_INET6, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000507 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800508 self.grp_addr = inet_pton(AF_INET, grp_addr)
509 self.src_addr = inet_pton(AF_INET, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000510
511 def add_vpp_config(self):
512 for path in self.paths:
Neale Ranns28c142e2018-09-07 09:37:07 -0700513 r = self._test.vapi.ip_mroute_add_del(self.src_addr,
514 self.grp_addr,
515 self.grp_addr_len,
516 self.e_flags,
517 path.proto,
518 path.nh_itf,
519 path.nh_addr,
520 path.nh_i_flags,
521 bier_imp=path.bier_imp,
522 rpf_id=self.rpf_id,
523 table_id=self.table_id,
524 is_ipv6=self.is_ip6)
525 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800526 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000527
528 def remove_vpp_config(self):
529 for path in self.paths:
530 self._test.vapi.ip_mroute_add_del(self.src_addr,
531 self.grp_addr,
532 self.grp_addr_len,
533 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700534 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000535 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700536 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000537 path.nh_i_flags,
538 table_id=self.table_id,
Neale Rannseea537a2018-01-09 04:11:28 -0800539 bier_imp=path.bier_imp,
Neale Ranns32e1c012016-11-22 17:07:28 +0000540 is_add=0,
541 is_ipv6=self.is_ip6)
542
543 def update_entry_flags(self, flags):
544 self.e_flags = flags
545 self._test.vapi.ip_mroute_add_del(self.src_addr,
546 self.grp_addr,
547 self.grp_addr_len,
548 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700549 0,
Neale Ranns32e1c012016-11-22 17:07:28 +0000550 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700551 "",
Neale Ranns32e1c012016-11-22 17:07:28 +0000552 0,
553 table_id=self.table_id,
554 is_ipv6=self.is_ip6)
555
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800556 def update_rpf_id(self, rpf_id):
557 self.rpf_id = rpf_id
558 self._test.vapi.ip_mroute_add_del(self.src_addr,
559 self.grp_addr,
560 self.grp_addr_len,
561 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700562 0,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800563 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700564 "",
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800565 0,
566 rpf_id=self.rpf_id,
567 table_id=self.table_id,
568 is_ipv6=self.is_ip6)
569
Neale Ranns32e1c012016-11-22 17:07:28 +0000570 def update_path_flags(self, itf, flags):
571 for path in self.paths:
572 if path.nh_itf == itf:
573 path.nh_i_flags = flags
574 break
575 self._test.vapi.ip_mroute_add_del(self.src_addr,
576 self.grp_addr,
577 self.grp_addr_len,
578 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700579 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000580 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700581 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000582 path.nh_i_flags,
583 table_id=self.table_id,
584 is_ipv6=self.is_ip6)
585
Neale Ranns5a8123b2017-01-26 01:18:23 -0800586 def query_vpp_config(self):
Neale Ranns947ea622018-06-07 23:48:20 -0700587 return find_mroute(self._test,
588 self.grp_addr_p,
589 self.src_addr_p,
590 self.grp_addr_len,
591 self.table_id,
592 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Ranns32e1c012016-11-22 17:07:28 +0000593
Neale Ranns5a8123b2017-01-26 01:18:23 -0800594 def __str__(self):
595 return self.object_id()
596
597 def object_id(self):
598 if self.is_ip6:
599 return ("%d:(%s,%s/%d)"
600 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800601 inet_ntop(AF_INET6, self.src_addr),
602 inet_ntop(AF_INET6, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800603 self.grp_addr_len))
604 else:
605 return ("%d:(%s,%s/%d)"
606 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800607 inet_ntop(AF_INET, self.src_addr),
608 inet_ntop(AF_INET, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800609 self.grp_addr_len))
610
Neale Ranns28c142e2018-09-07 09:37:07 -0700611 def get_stats(self):
612 c = self._test.statistics.get_counter("/net/mroute")
613 return c[0][self.stats_index]
614
Neale Ranns5a8123b2017-01-26 01:18:23 -0800615
616class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000617 def __init__(self, test, route, interface, packet):
618 self.route = route
619 self.interface = interface
620 self.packet = packet
621 self.test = test
622
623 def compare(self, signal):
624 self.test.assertEqual(self.interface, signal.sw_if_index)
625 self.test.assertEqual(self.route.table_id, signal.table_id)
626 self.test.assertEqual(self.route.grp_addr_len,
627 signal.grp_address_len)
628 for i in range(self.route.grp_addr_len / 8):
629 self.test.assertEqual(self.route.grp_addr[i],
630 signal.grp_address[i])
631 if (self.route.grp_addr_len > 32):
632 for i in range(4):
633 self.test.assertEqual(self.route.src_addr[i],
634 signal.src_address[i])
635
636
Neale Ranns5a8123b2017-01-26 01:18:23 -0800637class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000638 """
639 MPLS to IP Binding
640 """
641
Neale Ranns5a8123b2017-01-26 01:18:23 -0800642 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700643 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000644 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000645 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700646 self.dest_addr = dest_addr
Neale Rannsad422ed2016-11-02 14:20:04 +0000647 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800648 self.table_id = table_id
649 self.ip_table_id = ip_table_id
Neale Rannsf12a83f2017-04-18 09:09:40 -0700650 self.is_ip6 = is_ip6
651 if is_ip6:
652 self.dest_addrn = inet_pton(AF_INET6, dest_addr)
653 else:
654 self.dest_addrn = inet_pton(AF_INET, dest_addr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000655
656 def add_vpp_config(self):
657 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700658 self.dest_addrn,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800659 self.dest_addr_len,
660 table_id=self.table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700661 ip_table_id=self.ip_table_id,
662 is_ip4=(self.is_ip6 == 0))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800663 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000664
665 def remove_vpp_config(self):
666 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700667 self.dest_addrn,
Neale Rannsad422ed2016-11-02 14:20:04 +0000668 self.dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700669 table_id=self.table_id,
670 ip_table_id=self.ip_table_id,
671 is_bind=0,
672 is_ip4=(self.is_ip6 == 0))
Neale Rannsad422ed2016-11-02 14:20:04 +0000673
Neale Ranns5a8123b2017-01-26 01:18:23 -0800674 def query_vpp_config(self):
675 dump = self._test.vapi.mpls_fib_dump()
676 for e in dump:
677 if self.local_label == e.label \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800678 and self.table_id == e.table_id:
679 return True
680 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000681
Neale Ranns5a8123b2017-01-26 01:18:23 -0800682 def __str__(self):
683 return self.object_id()
684
685 def object_id(self):
686 return ("%d:%s binds %d:%s/%d"
687 % (self.table_id,
688 self.local_label,
689 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700690 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800691 self.dest_addr_len))
692
693
Neale Ranns15002542017-09-10 04:39:11 -0700694class VppMplsTable(VppObject):
695
696 def __init__(self,
697 test,
698 table_id):
699 self._test = test
700 self.table_id = table_id
701
702 def add_vpp_config(self):
703 self._test.vapi.mpls_table_add_del(
704 self.table_id,
705 is_add=1)
706 self._test.registry.register(self, self._test.logger)
707
708 def remove_vpp_config(self):
709 self._test.vapi.mpls_table_add_del(
710 self.table_id,
711 is_add=0)
712
713 def query_vpp_config(self):
714 # find the default route
715 dump = self._test.vapi.mpls_fib_dump()
716 if len(dump):
717 return True
718 return False
719
720 def __str__(self):
721 return self.object_id()
722
723 def object_id(self):
724 return ("table-mpls-%d" % (self.table_id))
725
726
Neale Ranns5a8123b2017-01-26 01:18:23 -0800727class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000728 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800729 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000730 """
731
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800732 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
733 is_multicast=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000734 self._test = test
735 self.paths = paths
736 self.local_label = local_label
737 self.eos_bit = eos_bit
738 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800739 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000740
741 def add_vpp_config(self):
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800742 is_multipath = len(self.paths) > 1
Neale Rannsad422ed2016-11-02 14:20:04 +0000743 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800744 lstack = path.encode_labels()
745
Neale Ranns008dbe12018-09-07 09:32:36 -0700746 r = self._test.vapi.mpls_route_add_del(
Klement Sekerada505f62017-01-04 12:58:53 +0100747 self.local_label,
748 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700749 path.proto,
Klement Sekerada505f62017-01-04 12:58:53 +0100750 path.nh_addr,
751 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800752 is_multicast=self.is_multicast,
753 is_multipath=is_multipath,
Klement Sekerada505f62017-01-04 12:58:53 +0100754 table_id=self.table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800755 is_interface_rx=path.is_interface_rx,
756 is_rpf_id=path.is_rpf_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800757 next_hop_out_label_stack=lstack,
758 next_hop_n_out_labels=len(lstack),
Klement Sekerada505f62017-01-04 12:58:53 +0100759 next_hop_via_label=path.nh_via_label,
760 next_hop_table_id=path.nh_table_id)
Neale Ranns008dbe12018-09-07 09:32:36 -0700761 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800762 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000763
764 def remove_vpp_config(self):
765 for path in self.paths:
766 self._test.vapi.mpls_route_add_del(self.local_label,
767 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700768 path.proto,
Neale Rannsad422ed2016-11-02 14:20:04 +0000769 path.nh_addr,
770 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800771 is_rpf_id=path.is_rpf_id,
Neale Rannsad422ed2016-11-02 14:20:04 +0000772 table_id=self.table_id,
773 is_add=0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800774
775 def query_vpp_config(self):
Neale Ranns775f73c2018-12-20 03:01:49 -0800776 return find_mpls_route(self._test, self.table_id,
777 self.local_label, self.eos_bit)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800778
779 def __str__(self):
780 return self.object_id()
781
782 def object_id(self):
783 return ("%d:%s/%d"
784 % (self.table_id,
785 self.local_label,
786 20+self.eos_bit))
Neale Ranns008dbe12018-09-07 09:32:36 -0700787
788 def get_stats_to(self):
789 c = self._test.statistics.get_counter("/net/route/to")
790 return c[0][self.stats_index]
791
792 def get_stats_via(self):
793 c = self._test.statistics.get_counter("/net/route/via")
794 return c[0][self.stats_index]