blob: d36c56761e3db41f7efe09812ba60781c28a6cb3 [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
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08007from vpp_object import VppObject
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02008from vpp_ip import DpoProto, INVALID_INDEX, VppIpAddressUnion, VppIpMPrefix
Neale Ranns59f71132020-04-08 12:19:38 +00009from ipaddress import ip_network, ip_address, IPv4Network, IPv6Network
Klement Sekera77c821c2022-04-14 16:29:07 +020010from vpp_papi_exceptions import UnexpectedApiReturnValueError
Neale Ranns177bbdc2016-11-15 09:46:51 +000011
Neale Rannsad422ed2016-11-02 14:20:04 +000012# from vnet/vnet/mpls/mpls_types.h
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020013MPLS_IETF_MAX_LABEL = 0xFFFFF
Neale Rannsad422ed2016-11-02 14:20:04 +000014MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
Neale Ranns177bbdc2016-11-15 09:46:51 +000015
Neale Ranns097fa662018-05-01 05:17:55 -070016try:
17 text_type = unicode
18except NameError:
19 text_type = str
20
Neale Ranns177bbdc2016-11-15 09:46:51 +000021
Neale Ranns097fa662018-05-01 05:17:55 -070022class FibPathProto:
23 FIB_PATH_NH_PROTO_IP4 = 0
24 FIB_PATH_NH_PROTO_IP6 = 1
25 FIB_PATH_NH_PROTO_MPLS = 2
26 FIB_PATH_NH_PROTO_ETHERNET = 3
27 FIB_PATH_NH_PROTO_BIER = 4
28 FIB_PATH_NH_PROTO_NSH = 5
29
30
31class FibPathType:
32 FIB_PATH_TYPE_NORMAL = 0
33 FIB_PATH_TYPE_LOCAL = 1
34 FIB_PATH_TYPE_DROP = 2
35 FIB_PATH_TYPE_UDP_ENCAP = 3
36 FIB_PATH_TYPE_BIER_IMP = 4
37 FIB_PATH_TYPE_ICMP_UNREACH = 5
38 FIB_PATH_TYPE_ICMP_PROHIBIT = 6
39 FIB_PATH_TYPE_SOURCE_LOOKUP = 7
40 FIB_PATH_TYPE_DVR = 8
41 FIB_PATH_TYPE_INTERFACE_RX = 9
42 FIB_PATH_TYPE_CLASSIFY = 10
43
44
45class FibPathFlags:
46 FIB_PATH_FLAG_NONE = 0
47 FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1
48 FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2
Neale Ranns1dbcf302019-07-19 11:44:53 +000049 FIB_PATH_FLAG_POP_PW_CW = 4
Neale Ranns097fa662018-05-01 05:17:55 -070050
51
Neale Ranns31ed7442018-02-23 05:29:09 -080052class MplsLspMode:
53 PIPE = 0
54 UNIFORM = 1
55
56
Neale Rannsefd7bc22019-11-11 08:32:34 +000057def mk_network(addr, len):
58 if ip_address(text_type(addr)).version == 4:
59 return IPv4Network("%s/%d" % (addr, len), strict=False)
60 else:
61 return IPv6Network("%s/%d" % (addr, len), strict=False)
62
63
Neale Ranns93cc3ee2018-10-10 07:22:51 -070064def ip_to_dpo_proto(addr):
Paul Vinciguerrabeded852019-03-01 10:35:55 -080065 if addr.version == 6:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070066 return DpoProto.DPO_PROTO_IP6
67 else:
68 return DpoProto.DPO_PROTO_IP4
69
70
Neale Ranns097fa662018-05-01 05:17:55 -070071def address_proto(ip_addr):
Ole Troan6e6ad642020-02-04 13:28:13 +010072 if ip_addr.ip_addr.version == 4:
Neale Ranns097fa662018-05-01 05:17:55 -070073 return FibPathProto.FIB_PATH_NH_PROTO_IP4
Neale Rannsb3b2de72017-03-08 05:17:22 -080074 else:
Neale Ranns097fa662018-05-01 05:17:55 -070075 return FibPathProto.FIB_PATH_NH_PROTO_IP6
Neale Rannsb3b2de72017-03-08 05:17:22 -080076
Neale Ranns097fa662018-05-01 05:17:55 -070077
Maxime Peimb0d43392024-04-09 09:08:00 +020078def find_route(
79 test, addr, len, table_id=0, sw_if_index=None, ignore_default_route=False
80):
Neale Rannsefd7bc22019-11-11 08:32:34 +000081 prefix = mk_network(addr, len)
Neale Ranns097fa662018-05-01 05:17:55 -070082
Ole Troan6e6ad642020-02-04 13:28:13 +010083 if 4 == prefix.version:
Neale Ranns097fa662018-05-01 05:17:55 -070084 routes = test.vapi.ip_route_dump(table_id, False)
Neale Ranns097fa662018-05-01 05:17:55 -070085 else:
86 routes = test.vapi.ip_route_dump(table_id, True)
Neale Ranns097fa662018-05-01 05:17:55 -070087
Neale Rannsb3b2de72017-03-08 05:17:22 -080088 for e in routes:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020089 if table_id == e.route.table_id and str(e.route.prefix) == str(prefix):
Neale Ranns59f71132020-04-08 12:19:38 +000090 if not sw_if_index:
Maxime Peimb0d43392024-04-09 09:08:00 +020091 # if the route is a default one of the table:
92 # 0.0.0.0/0, 0.0.0.0/32, 240.0.0.0/4, 255.255.255.255/32
93 return not (
94 ignore_default_route
95 and e.route.n_paths == 1
96 and e.route.paths[0].type == FibPathType.FIB_PATH_TYPE_DROP
97 )
Neale Ranns59f71132020-04-08 12:19:38 +000098 else:
99 # should be only one path if the user is looking
100 # for the interface the route is reachable through
101 if e.route.n_paths != 1:
102 return False
103 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200104 return e.route.paths[0].sw_if_index == sw_if_index
Neale Ranns59f71132020-04-08 12:19:38 +0000105
Neale Rannsb3b2de72017-03-08 05:17:22 -0800106 return False
107
108
Neale Ranns9db6ada2019-11-08 12:42:31 +0000109def find_route_in_dump(dump, route, table):
110 for r in dump:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200111 if table.table_id == r.route.table_id and route.prefix == r.route.prefix:
Neale Ranns9db6ada2019-11-08 12:42:31 +0000112 if len(route.paths) == r.route.n_paths:
113 return True
114 return False
115
116
117def find_mroute_in_dump(dump, route, table):
118 for r in dump:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 if table.table_id == r.route.table_id and route.prefix == r.route.prefix:
Neale Ranns9db6ada2019-11-08 12:42:31 +0000120 return True
121 return False
122
123
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124def find_mroute(test, grp_addr, src_addr, grp_addr_len, table_id=0):
125 ip_mprefix = VppIpMPrefix(text_type(src_addr), text_type(grp_addr), grp_addr_len)
Neale Ranns097fa662018-05-01 05:17:55 -0700126
Ole Troan6e6ad642020-02-04 13:28:13 +0100127 if 4 == ip_mprefix.version:
Neale Ranns097fa662018-05-01 05:17:55 -0700128 routes = test.vapi.ip_mroute_dump(table_id, False)
Neale Ranns947ea622018-06-07 23:48:20 -0700129 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700130 routes = test.vapi.ip_mroute_dump(table_id, True)
131
Neale Ranns947ea622018-06-07 23:48:20 -0700132 for e in routes:
Neale Ranns097fa662018-05-01 05:17:55 -0700133 if table_id == e.route.table_id and ip_mprefix == e.route.prefix:
Neale Ranns947ea622018-06-07 23:48:20 -0700134 return True
135 return False
136
137
Neale Ranns775f73c2018-12-20 03:01:49 -0800138def find_mpls_route(test, table_id, label, eos_bit, paths=None):
Neale Ranns097fa662018-05-01 05:17:55 -0700139 dump = test.vapi.mpls_route_dump(table_id)
Neale Ranns775f73c2018-12-20 03:01:49 -0800140 for e in dump:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200141 if (
142 label == e.mr_route.mr_label
143 and eos_bit == e.mr_route.mr_eos
144 and table_id == e.mr_route.mr_table_id
145 ):
Neale Ranns775f73c2018-12-20 03:01:49 -0800146 if not paths:
147 return True
148 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200149 if len(paths) != len(e.mr_route.mr_paths):
Neale Ranns775f73c2018-12-20 03:01:49 -0800150 return False
151 for i in range(len(paths)):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200152 if paths[i] != e.mr_route.mr_paths[i]:
Neale Ranns775f73c2018-12-20 03:01:49 -0800153 return False
154 return True
155 return False
156
157
Neale Rannsefd7bc22019-11-11 08:32:34 +0000158def fib_interface_ip_prefix(test, addr, len, sw_if_index):
159 # can't use python net here since we need the host bits in the prefix
160 prefix = "%s/%d" % (addr, len)
161 addrs = test.vapi.ip_address_dump(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200162 sw_if_index, is_ipv6=(6 == ip_address(addr).version)
163 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700164
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700165 for a in addrs:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200166 if a.sw_if_index == sw_if_index and str(a.prefix) == prefix:
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700167 return True
168 return False
169
170
Neale Ranns15002542017-09-10 04:39:11 -0700171class VppIpTable(VppObject):
Maxime Peimf13534e2024-04-05 14:16:39 +0200172 def __init__(self, test, table_id, is_ip6=0, register=True, name=""):
Neale Ranns15002542017-09-10 04:39:11 -0700173 self._test = test
Maxime Peimf13534e2024-04-05 14:16:39 +0200174 self.name = name
Neale Ranns15002542017-09-10 04:39:11 -0700175 self.table_id = table_id
176 self.is_ip6 = is_ip6
Neale Ranns256b67b2020-09-02 14:46:53 +0000177 self.register = register
Neale Ranns15002542017-09-10 04:39:11 -0700178
179 def add_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200180 self._test.vapi.ip_table_add_del(
Maxime Peimf13534e2024-04-05 14:16:39 +0200181 is_add=1,
182 table={"is_ip6": self.is_ip6, "table_id": self.table_id, "name": self.name},
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200183 )
Neale Ranns256b67b2020-09-02 14:46:53 +0000184 if self.register:
185 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000186 return self
Neale Ranns15002542017-09-10 04:39:11 -0700187
188 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200189 self._test.vapi.ip_table_add_del(
190 is_add=0, table={"is_ip6": self.is_ip6, "table_id": self.table_id}
191 )
Neale Ranns9db6ada2019-11-08 12:42:31 +0000192
193 def replace_begin(self):
194 self._test.vapi.ip_table_replace_begin(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200195 table={"is_ip6": self.is_ip6, "table_id": self.table_id}
196 )
Neale Ranns9db6ada2019-11-08 12:42:31 +0000197
198 def replace_end(self):
199 self._test.vapi.ip_table_replace_end(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200200 table={"is_ip6": self.is_ip6, "table_id": self.table_id}
201 )
Neale Ranns9db6ada2019-11-08 12:42:31 +0000202
203 def flush(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200204 self._test.vapi.ip_table_flush(
205 table={"is_ip6": self.is_ip6, "table_id": self.table_id}
206 )
Neale Ranns9db6ada2019-11-08 12:42:31 +0000207
208 def dump(self):
209 return self._test.vapi.ip_route_dump(self.table_id, self.is_ip6)
210
211 def mdump(self):
212 return self._test.vapi.ip_mroute_dump(self.table_id, self.is_ip6)
Neale Ranns15002542017-09-10 04:39:11 -0700213
214 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700215 if self.table_id == 0:
216 # the default table always exists
217 return False
Neale Ranns15002542017-09-10 04:39:11 -0700218 # find the default route
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200219 return find_route(
220 self._test, "::" if self.is_ip6 else "0.0.0.0", 0, self.table_id
221 )
Neale Ranns15002542017-09-10 04:39:11 -0700222
Neale Ranns15002542017-09-10 04:39:11 -0700223 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200224 return "table-%s-%d" % ("v6" if self.is_ip6 == 1 else "v4", self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700225
226
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700227class VppIpInterfaceAddress(VppObject):
Neale Ranns59f71132020-04-08 12:19:38 +0000228 def __init__(self, test, intf, addr, len, bind=None):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700229 self._test = test
230 self.intf = intf
Neale Rannsefd7bc22019-11-11 08:32:34 +0000231 self.addr = addr
232 self.len = len
233 self.prefix = "%s/%d" % (addr, len)
Neale Ranns59f71132020-04-08 12:19:38 +0000234 self.host_len = ip_network(self.prefix, strict=False).max_prefixlen
235 self.table_id = 0
236 if bind:
237 self.table_id = bind.table.table_id
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700238
239 def add_vpp_config(self):
240 self._test.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200241 sw_if_index=self.intf.sw_if_index, prefix=self.prefix, is_add=1
242 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700243 self._test.registry.register(self, self._test.logger)
Neale Ranns9efcee62019-11-26 19:30:08 +0000244 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700245
246 def remove_vpp_config(self):
247 self._test.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200248 sw_if_index=self.intf.sw_if_index, prefix=self.prefix, is_add=0
249 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700250
251 def query_vpp_config(self):
Neale Ranns59f71132020-04-08 12:19:38 +0000252 # search for the IP address mapping and the two expected
253 # FIB entries
254 v = ip_address(self.addr).version
255
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200256 if (v == 4 and self.len < 31) or (v == 6 and self.len < 127):
257 return (
258 fib_interface_ip_prefix(
259 self._test, self.addr, self.len, self.intf.sw_if_index
260 )
261 & find_route(
262 self._test,
263 self.addr,
264 self.len,
265 table_id=self.table_id,
266 sw_if_index=self.intf.sw_if_index,
267 )
268 & find_route(
269 self._test,
270 self.addr,
271 self.host_len,
272 table_id=self.table_id,
273 sw_if_index=self.intf.sw_if_index,
274 )
275 )
Neale Ranns59f71132020-04-08 12:19:38 +0000276 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200277 return fib_interface_ip_prefix(
278 self._test, self.addr, self.len, self.intf.sw_if_index
279 ) & find_route(
280 self._test,
281 self.addr,
282 self.host_len,
283 table_id=self.table_id,
284 sw_if_index=self.intf.sw_if_index,
285 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700286
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700287 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200288 return "interface-ip-%s-%d-%s" % (self.intf, self.table_id, self.prefix)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700289
290
Neale Rannsec40a7d2020-04-23 07:36:12 +0000291class VppIp6LinkLocalAddress(VppObject):
Neale Rannsec40a7d2020-04-23 07:36:12 +0000292 def __init__(self, test, intf, addr):
293 self._test = test
294 self.intf = intf
295 self.addr = addr
296
297 def add_vpp_config(self):
298 self._test.vapi.sw_interface_ip6_set_link_local_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200299 sw_if_index=self.intf.sw_if_index, ip=self.addr
300 )
Neale Rannsec40a7d2020-04-23 07:36:12 +0000301 self._test.registry.register(self, self._test.logger)
302 return self
303
304 def remove_vpp_config(self):
305 # link locals can't be removed, only changed
306 pass
307
308 def query_vpp_config(self):
309 # no API to query
310 return False
311
312 def object_id(self):
313 return "ip6-link-local-%s-%s" % (self.intf, self.addr)
314
315
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700316class VppIpInterfaceBind(VppObject):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700317 def __init__(self, test, intf, table):
318 self._test = test
319 self.intf = intf
320 self.table = table
321
322 def add_vpp_config(self):
323 if self.table.is_ip6:
324 self.intf.set_table_ip6(self.table.table_id)
325 else:
326 self.intf.set_table_ip4(self.table.table_id)
327 self._test.registry.register(self, self._test.logger)
Neale Ranns59f71132020-04-08 12:19:38 +0000328 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700329
330 def remove_vpp_config(self):
331 if 0 == self.table.table_id:
332 return
333 if self.table.is_ip6:
334 self.intf.set_table_ip6(0)
335 else:
336 self.intf.set_table_ip4(0)
337
338 def query_vpp_config(self):
339 if 0 == self.table.table_id:
340 return False
Klement Sekera77c821c2022-04-14 16:29:07 +0200341 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200342 return (
343 self._test.vapi.sw_interface_get_table(
344 self.intf.sw_if_index, self.table.is_ip6
345 ).vrf_id
346 == self.table.table_id
347 )
Klement Sekera77c821c2022-04-14 16:29:07 +0200348 except UnexpectedApiReturnValueError as e:
349 if e.retval == -2: # INVALID_SW_IF_INDEX
350 return False
351 raise
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700352
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700353 def object_id(self):
354 return "interface-bind-%s-%s" % (self.intf, self.table)
355
356
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500357class VppMplsLabel:
Neale Ranns31ed7442018-02-23 05:29:09 -0800358 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
359 self.value = value
360 self.mode = mode
361 self.ttl = ttl
362 self.exp = exp
363
364 def encode(self):
365 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200366 return {
367 "label": self.value,
368 "ttl": self.ttl,
369 "exp": self.exp,
370 "is_uniform": is_uniform,
371 }
Neale Ranns31ed7442018-02-23 05:29:09 -0800372
Neale Ranns775f73c2018-12-20 03:01:49 -0800373 def __eq__(self, other):
374 if isinstance(other, self.__class__):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200375 return (
376 self.value == other.value
377 and self.ttl == other.ttl
378 and self.exp == other.exp
379 and self.mode == other.mode
380 )
381 elif hasattr(other, "label"):
382 return (
383 self.value == other.label
384 and self.ttl == other.ttl
385 and self.exp == other.exp
386 and (self.mode == MplsLspMode.UNIFORM) == other.is_uniform
387 )
Neale Ranns775f73c2018-12-20 03:01:49 -0800388 else:
389 return False
390
391 def __ne__(self, other):
392 return not (self == other)
393
Neale Ranns31ed7442018-02-23 05:29:09 -0800394
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500395class VppFibPathNextHop:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200396 def __init__(self, addr, via_label=MPLS_LABEL_INVALID, next_hop_id=INVALID_INDEX):
Neale Ranns097fa662018-05-01 05:17:55 -0700397 self.addr = VppIpAddressUnion(addr)
398 self.via_label = via_label
399 self.obj_id = next_hop_id
400
401 def encode(self):
402 if self.via_label is not MPLS_LABEL_INVALID:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200403 return {"via_label": self.via_label}
Neale Ranns097fa662018-05-01 05:17:55 -0700404 if self.obj_id is not INVALID_INDEX:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200405 return {"obj_id": self.obj_id}
Neale Ranns097fa662018-05-01 05:17:55 -0700406 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200407 return {"address": self.addr.encode()}
Neale Ranns097fa662018-05-01 05:17:55 -0700408
409 def proto(self):
410 if self.via_label is MPLS_LABEL_INVALID:
411 return address_proto(self.addr)
412 else:
413 return FibPathProto.FIB_PATH_NH_PROTO_MPLS
414
415 def __eq__(self, other):
416 if not isinstance(other, self.__class__):
417 # try the other instance's __eq__.
418 return NotImplemented
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200419 return (
420 self.addr == other.addr
421 and self.via_label == other.via_label
422 and self.obj_id == other.obj_id
423 )
Neale Ranns097fa662018-05-01 05:17:55 -0700424
425
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500426class VppRoutePath:
Klement Sekerada505f62017-01-04 12:58:53 +0100427 def __init__(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200428 self,
429 nh_addr,
430 nh_sw_if_index,
431 nh_table_id=0,
432 labels=[],
433 nh_via_label=MPLS_LABEL_INVALID,
434 rpf_id=0,
435 next_hop_id=INVALID_INDEX,
436 proto=None,
437 flags=FibPathFlags.FIB_PATH_FLAG_NONE,
438 type=FibPathType.FIB_PATH_TYPE_NORMAL,
439 ):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000440 self.nh_itf = nh_sw_if_index
441 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000442 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800443 self.weight = 1
444 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700445 self.proto = proto
446 self.flags = flags
447 self.type = type
448 self.nh = VppFibPathNextHop(nh_addr, nh_via_label, next_hop_id)
449 if proto is None:
450 self.proto = self.nh.proto()
Neale Ranns31426c62017-05-24 10:32:58 -0700451 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700452 self.proto = proto
Neale Ranns810086d2017-11-05 16:26:46 -0800453 self.next_hop_id = next_hop_id
Neale Ranns177bbdc2016-11-15 09:46:51 +0000454
Neale Ranns097fa662018-05-01 05:17:55 -0700455 def encode_labels(self):
Neale Ranns31ed7442018-02-23 05:29:09 -0800456 lstack = []
457 for l in self.nh_labels:
458 if type(l) == VppMplsLabel:
459 lstack.append(l.encode())
460 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200461 lstack.append({"label": l, "ttl": 255})
462 while len(lstack) < 16:
Neale Ranns097fa662018-05-01 05:17:55 -0700463 lstack.append({})
464
Neale Ranns31ed7442018-02-23 05:29:09 -0800465 return lstack
466
Neale Ranns097fa662018-05-01 05:17:55 -0700467 def encode(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200468 return {
469 "weight": 1,
470 "preference": 0,
471 "table_id": self.nh_table_id,
472 "nh": self.nh.encode(),
473 "next_hop_id": self.next_hop_id,
474 "sw_if_index": self.nh_itf,
475 "rpf_id": self.rpf_id,
476 "proto": self.proto,
477 "type": self.type,
478 "flags": self.flags,
479 "n_labels": len(self.nh_labels),
480 "label_stack": self.encode_labels(),
481 }
Neale Ranns2303cb12018-02-21 04:57:17 -0800482
Neale Rannsef90ed02018-09-13 08:45:12 -0700483 def __eq__(self, other):
Neale Ranns775f73c2018-12-20 03:01:49 -0800484 if isinstance(other, self.__class__):
Neale Ranns097fa662018-05-01 05:17:55 -0700485 return self.nh == other.nh
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200486 elif hasattr(other, "sw_if_index"):
Neale Ranns775f73c2018-12-20 03:01:49 -0800487 # vl_api_fib_path_t
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200488 if len(self.nh_labels) != other.n_labels:
Neale Ranns775f73c2018-12-20 03:01:49 -0800489 return False
490 for i in range(len(self.nh_labels)):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200491 if self.nh_labels[i] != other.label_stack[i]:
Neale Ranns775f73c2018-12-20 03:01:49 -0800492 return False
493 return self.nh_itf == other.sw_if_index
494 else:
495 return False
496
497 def __ne__(self, other):
498 return not (self == other)
Neale Rannsef90ed02018-09-13 08:45:12 -0700499
Neale Ranns177bbdc2016-11-15 09:46:51 +0000500
Neale Ranns5a8123b2017-01-26 01:18:23 -0800501class VppMRoutePath(VppRoutePath):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200502 def __init__(
503 self,
504 nh_sw_if_index,
505 flags,
506 nh=None,
507 proto=FibPathProto.FIB_PATH_NH_PROTO_IP4,
508 type=FibPathType.FIB_PATH_TYPE_NORMAL,
509 bier_imp=INVALID_INDEX,
510 ):
Neale Rannse821ab12017-06-01 07:45:05 -0700511 if not nh:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200512 nh = "::" if proto is FibPathProto.FIB_PATH_NH_PROTO_IP6 else "0.0.0.0"
513 super(VppMRoutePath, self).__init__(
514 nh, nh_sw_if_index, proto=proto, type=type, next_hop_id=bier_imp
515 )
Neale Ranns32e1c012016-11-22 17:07:28 +0000516 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700517 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000518
Neale Ranns097fa662018-05-01 05:17:55 -0700519 def encode(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200520 return {
521 "path": super(VppMRoutePath, self).encode(),
522 "itf_flags": self.nh_i_flags,
523 }
Neale Ranns097fa662018-05-01 05:17:55 -0700524
Neale Ranns32e1c012016-11-22 17:07:28 +0000525
Neale Ranns5a8123b2017-01-26 01:18:23 -0800526class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000527 """
528 IP Route
529 """
530
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200531 def __init__(
532 self, test, dest_addr, dest_addr_len, paths, table_id=0, register=True
533 ):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000534 self._test = test
535 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000536 self.table_id = table_id
Neale Rannsefd7bc22019-11-11 08:32:34 +0000537 self.prefix = mk_network(dest_addr, dest_addr_len)
Neale Ranns097fa662018-05-01 05:17:55 -0700538 self.register = register
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400539 self.stats_index = None
Neale Rannsc2ac2352019-07-02 14:33:29 +0000540 self.modified = False
Neale Ranns177bbdc2016-11-15 09:46:51 +0000541
Neale Ranns097fa662018-05-01 05:17:55 -0700542 self.encoded_paths = []
543 for path in self.paths:
544 self.encoded_paths.append(path.encode())
545
546 def __eq__(self, other):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200547 if self.table_id == other.table_id and self.prefix == other.prefix:
Neale Ranns097fa662018-05-01 05:17:55 -0700548 return True
549 return False
550
551 def modify(self, paths):
Neale Ranns69b7aa42017-03-10 03:04:12 -0800552 self.paths = paths
Neale Ranns097fa662018-05-01 05:17:55 -0700553 self.encoded_paths = []
554 for path in self.paths:
555 self.encoded_paths.append(path.encode())
Neale Rannsc2ac2352019-07-02 14:33:29 +0000556 self.modified = True
Neale Ranns097fa662018-05-01 05:17:55 -0700557
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200558 self._test.vapi.ip_route_add_del(
559 route={
560 "table_id": self.table_id,
561 "prefix": self.prefix,
562 "n_paths": len(self.encoded_paths),
563 "paths": self.encoded_paths,
564 },
565 is_add=1,
566 is_multipath=0,
567 )
Neale Ranns69b7aa42017-03-10 03:04:12 -0800568
Neale Ranns177bbdc2016-11-15 09:46:51 +0000569 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700570 r = self._test.vapi.ip_route_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200571 route={
572 "table_id": self.table_id,
573 "prefix": self.prefix,
574 "n_paths": len(self.encoded_paths),
575 "paths": self.encoded_paths,
576 },
Neale Ranns097fa662018-05-01 05:17:55 -0700577 is_add=1,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200578 is_multipath=0,
579 )
Neale Ranns008dbe12018-09-07 09:32:36 -0700580 self.stats_index = r.stats_index
Neale Ranns097fa662018-05-01 05:17:55 -0700581 if self.register:
582 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000583 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000584
585 def remove_vpp_config(self):
Neale Rannsc2ac2352019-07-02 14:33:29 +0000586 # there's no need to issue different deletes for modified routes
587 # we do this only to test the two different ways to delete routes
588 # eiter by passing all the paths to remove and mutlipath=1 or
589 # passing no paths and multipath=0
590 if self.modified:
591 self._test.vapi.ip_route_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200592 route={
593 "table_id": self.table_id,
594 "prefix": self.prefix,
595 "n_paths": len(self.encoded_paths),
596 "paths": self.encoded_paths,
597 },
Neale Rannsc2ac2352019-07-02 14:33:29 +0000598 is_add=0,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200599 is_multipath=1,
600 )
Neale Rannsc2ac2352019-07-02 14:33:29 +0000601 else:
602 self._test.vapi.ip_route_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200603 route={"table_id": self.table_id, "prefix": self.prefix, "n_paths": 0},
Neale Rannsc2ac2352019-07-02 14:33:29 +0000604 is_add=0,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200605 is_multipath=0,
606 )
Neale Rannsad422ed2016-11-02 14:20:04 +0000607
Neale Ranns5a8123b2017-01-26 01:18:23 -0800608 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200609 return find_route(
610 self._test,
611 self.prefix.network_address,
612 self.prefix.prefixlen,
613 self.table_id,
Maxime Peimb0d43392024-04-09 09:08:00 +0200614 ignore_default_route=True,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200615 )
Neale Rannsad422ed2016-11-02 14:20:04 +0000616
Neale Ranns5a8123b2017-01-26 01:18:23 -0800617 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200618 return "%s:table-%d-%s" % (
619 "ip6-route" if self.prefix.version == 6 else "ip-route",
620 self.table_id,
621 self.prefix,
622 )
Neale Ranns5a8123b2017-01-26 01:18:23 -0800623
Neale Ranns008dbe12018-09-07 09:32:36 -0700624 def get_stats_to(self):
625 c = self._test.statistics.get_counter("/net/route/to")
626 return c[0][self.stats_index]
627
628 def get_stats_via(self):
629 c = self._test.statistics.get_counter("/net/route/via")
630 return c[0][self.stats_index]
631
Neale Ranns5a8123b2017-01-26 01:18:23 -0800632
Neale Ranns976b2592019-12-04 06:11:00 +0000633class VppIpRouteV2(VppObject):
634 """
635 IP Route V2
636 """
637
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200638 def __init__(
639 self, test, dest_addr, dest_addr_len, paths, table_id=0, register=True, src=0
640 ):
Neale Ranns976b2592019-12-04 06:11:00 +0000641 self._test = test
642 self.paths = paths
643 self.table_id = table_id
644 self.prefix = mk_network(dest_addr, dest_addr_len)
645 self.register = register
646 self.stats_index = None
647 self.modified = False
648 self.src = src
649
650 self.encoded_paths = []
651 for path in self.paths:
652 self.encoded_paths.append(path.encode())
653
654 def __eq__(self, other):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200655 if self.table_id == other.table_id and self.prefix == other.prefix:
Neale Ranns976b2592019-12-04 06:11:00 +0000656 return True
657 return False
658
659 def modify(self, paths):
660 self.paths = paths
661 self.encoded_paths = []
662 for path in self.paths:
663 self.encoded_paths.append(path.encode())
664 self.modified = True
665
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200666 self._test.vapi.ip_route_add_del_v2(
667 route={
668 "table_id": self.table_id,
669 "prefix": self.prefix,
670 "src": self.src,
671 "n_paths": len(self.encoded_paths),
672 "paths": self.encoded_paths,
673 },
674 is_add=1,
675 is_multipath=0,
676 )
Neale Ranns976b2592019-12-04 06:11:00 +0000677
678 def add_vpp_config(self):
679 r = self._test.vapi.ip_route_add_del_v2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200680 route={
681 "table_id": self.table_id,
682 "prefix": self.prefix,
683 "n_paths": len(self.encoded_paths),
684 "paths": self.encoded_paths,
685 "src": self.src,
686 },
Neale Ranns976b2592019-12-04 06:11:00 +0000687 is_add=1,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200688 is_multipath=0,
689 )
Neale Ranns976b2592019-12-04 06:11:00 +0000690 self.stats_index = r.stats_index
691 if self.register:
692 self._test.registry.register(self, self._test.logger)
693 return self
694
695 def remove_vpp_config(self):
696 # there's no need to issue different deletes for modified routes
697 # we do this only to test the two different ways to delete routes
698 # eiter by passing all the paths to remove and mutlipath=1 or
699 # passing no paths and multipath=0
700 if self.modified:
701 self._test.vapi.ip_route_add_del_v2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200702 route={
703 "table_id": self.table_id,
704 "prefix": self.prefix,
705 "src": self.src,
706 "n_paths": len(self.encoded_paths),
707 "paths": self.encoded_paths,
708 },
Neale Ranns976b2592019-12-04 06:11:00 +0000709 is_add=0,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200710 is_multipath=1,
711 )
Neale Ranns976b2592019-12-04 06:11:00 +0000712 else:
713 self._test.vapi.ip_route_add_del_v2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200714 route={
715 "table_id": self.table_id,
716 "prefix": self.prefix,
717 "src": self.src,
718 "n_paths": 0,
719 },
Neale Ranns976b2592019-12-04 06:11:00 +0000720 is_add=0,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200721 is_multipath=0,
722 )
Neale Ranns976b2592019-12-04 06:11:00 +0000723
724 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200725 return find_route(
726 self._test,
727 self.prefix.network_address,
728 self.prefix.prefixlen,
729 self.table_id,
Maxime Peimb0d43392024-04-09 09:08:00 +0200730 ignore_default_route=True,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200731 )
Neale Ranns976b2592019-12-04 06:11:00 +0000732
733 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200734 return "%s:table-%d-%s" % (
735 "ip6-route" if self.prefix.version == 6 else "ip-route",
736 self.table_id,
737 self.prefix,
738 )
Neale Ranns976b2592019-12-04 06:11:00 +0000739
740 def get_stats_to(self):
741 c = self._test.statistics.get_counter("/net/route/to")
742 return c[0][self.stats_index]
743
744 def get_stats_via(self):
745 c = self._test.statistics.get_counter("/net/route/via")
746 return c[0][self.stats_index]
747
748
Neale Ranns5a8123b2017-01-26 01:18:23 -0800749class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000750 """
751 IP Multicast Route
752 """
753
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200754 def __init__(
755 self,
756 test,
757 src_addr,
758 grp_addr,
759 grp_addr_len,
760 e_flags,
761 paths,
762 table_id=0,
763 rpf_id=0,
764 ):
Neale Ranns32e1c012016-11-22 17:07:28 +0000765 self._test = test
766 self.paths = paths
Neale Ranns32e1c012016-11-22 17:07:28 +0000767 self.table_id = table_id
768 self.e_flags = e_flags
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800769 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000770
Neale Ranns097fa662018-05-01 05:17:55 -0700771 self.prefix = VppIpMPrefix(src_addr, grp_addr, grp_addr_len)
772 self.encoded_paths = []
773 for path in self.paths:
774 self.encoded_paths.append(path.encode())
Neale Ranns32e1c012016-11-22 17:07:28 +0000775
Paul Vinciguerrae64e5ff2020-04-28 00:27:38 -0400776 def encode(self, paths=None):
777 _paths = self.encoded_paths if paths is None else paths
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200778 return {
779 "table_id": self.table_id,
780 "entry_flags": self.e_flags,
781 "rpf_id": self.rpf_id,
782 "prefix": self.prefix.encode(),
783 "n_paths": len(_paths),
784 "paths": _paths,
785 }
Paul Vinciguerrae64e5ff2020-04-28 00:27:38 -0400786
Neale Ranns32e1c012016-11-22 17:07:28 +0000787 def add_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200788 r = self._test.vapi.ip_mroute_add_del(
789 route=self.encode(), is_multipath=1, is_add=1
790 )
Neale Ranns097fa662018-05-01 05:17:55 -0700791 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800792 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000793 return self
Neale Ranns32e1c012016-11-22 17:07:28 +0000794
795 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200796 self._test.vapi.ip_mroute_add_del(route=self.encode(), is_multipath=1, is_add=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000797
798 def update_entry_flags(self, flags):
799 self.e_flags = flags
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200800 self._test.vapi.ip_mroute_add_del(
801 route=self.encode(paths=[]), is_multipath=1, is_add=1
802 )
Neale Ranns32e1c012016-11-22 17:07:28 +0000803
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800804 def update_rpf_id(self, rpf_id):
805 self.rpf_id = rpf_id
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200806 self._test.vapi.ip_mroute_add_del(
807 route=self.encode(paths=[]), is_multipath=1, is_add=1
808 )
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800809
Neale Ranns32e1c012016-11-22 17:07:28 +0000810 def update_path_flags(self, itf, flags):
Neale Ranns097fa662018-05-01 05:17:55 -0700811 for p in range(len(self.paths)):
812 if self.paths[p].nh_itf == itf:
813 self.paths[p].nh_i_flags = flags
Paul Vinciguerra6230b9d2020-04-28 11:23:31 -0400814 self.encoded_paths[p] = self.paths[p].encode()
815 break
Neale Ranns097fa662018-05-01 05:17:55 -0700816
Paul Vinciguerrae64e5ff2020-04-28 00:27:38 -0400817 self._test.vapi.ip_mroute_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200818 route=self.encode(paths=[self.encoded_paths[p]]), is_add=1, is_multipath=0
819 )
Neale Ranns32e1c012016-11-22 17:07:28 +0000820
Neale Ranns5a8123b2017-01-26 01:18:23 -0800821 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200822 return find_mroute(
823 self._test,
824 self.prefix.gaddr,
825 self.prefix.saddr,
826 self.prefix.length,
827 self.table_id,
828 )
Neale Ranns32e1c012016-11-22 17:07:28 +0000829
Neale Ranns5a8123b2017-01-26 01:18:23 -0800830 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200831 return "%d:(%s,%s/%d)" % (
832 self.table_id,
833 self.prefix.saddr,
834 self.prefix.gaddr,
835 self.prefix.length,
836 )
Neale Ranns5a8123b2017-01-26 01:18:23 -0800837
Neale Ranns28c142e2018-09-07 09:37:07 -0700838 def get_stats(self):
839 c = self._test.statistics.get_counter("/net/mroute")
840 return c[0][self.stats_index]
841
Neale Ranns5a8123b2017-01-26 01:18:23 -0800842
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500843class VppMFibSignal:
Neale Ranns32e1c012016-11-22 17:07:28 +0000844 def __init__(self, test, route, interface, packet):
845 self.route = route
846 self.interface = interface
847 self.packet = packet
848 self.test = test
849
850 def compare(self, signal):
851 self.test.assertEqual(self.interface, signal.sw_if_index)
852 self.test.assertEqual(self.route.table_id, signal.table_id)
Neale Ranns097fa662018-05-01 05:17:55 -0700853 self.test.assertEqual(self.route.prefix, signal.prefix)
Neale Ranns32e1c012016-11-22 17:07:28 +0000854
855
Neale Ranns5a8123b2017-01-26 01:18:23 -0800856class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000857 """
858 MPLS to IP Binding
859 """
860
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200861 def __init__(
862 self,
863 test,
864 local_label,
865 dest_addr,
866 dest_addr_len,
867 table_id=0,
868 ip_table_id=0,
869 is_ip6=0,
870 ):
Neale Rannsad422ed2016-11-02 14:20:04 +0000871 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000872 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700873 self.dest_addr = dest_addr
Neale Ranns097fa662018-05-01 05:17:55 -0700874 self.ip_addr = ip_address(text_type(dest_addr))
Neale Rannsad422ed2016-11-02 14:20:04 +0000875 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800876 self.table_id = table_id
877 self.ip_table_id = ip_table_id
Neale Rannsefd7bc22019-11-11 08:32:34 +0000878 self.prefix = mk_network(dest_addr, dest_addr_len)
Neale Rannsad422ed2016-11-02 14:20:04 +0000879
880 def add_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200881 self._test.vapi.mpls_ip_bind_unbind(
882 self.local_label,
883 self.prefix,
884 table_id=self.table_id,
885 ip_table_id=self.ip_table_id,
886 )
Neale Ranns5a8123b2017-01-26 01:18:23 -0800887 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000888
889 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200890 self._test.vapi.mpls_ip_bind_unbind(
891 self.local_label,
892 self.prefix,
893 table_id=self.table_id,
894 ip_table_id=self.ip_table_id,
895 is_bind=0,
896 )
Neale Rannsad422ed2016-11-02 14:20:04 +0000897
Neale Ranns5a8123b2017-01-26 01:18:23 -0800898 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700899 dump = self._test.vapi.mpls_route_dump(self.table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800900 for e in dump:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200901 if (
902 self.local_label == e.mr_route.mr_label
903 and self.table_id == e.mr_route.mr_table_id
904 ):
Neale Ranns5a8123b2017-01-26 01:18:23 -0800905 return True
906 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000907
Neale Ranns5a8123b2017-01-26 01:18:23 -0800908 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200909 return "%d:%s binds %d:%s/%d" % (
910 self.table_id,
911 self.local_label,
912 self.ip_table_id,
913 self.dest_addr,
914 self.dest_addr_len,
915 )
Neale Ranns5a8123b2017-01-26 01:18:23 -0800916
917
Neale Ranns15002542017-09-10 04:39:11 -0700918class VppMplsTable(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200919 def __init__(self, test, table_id):
Neale Ranns15002542017-09-10 04:39:11 -0700920 self._test = test
921 self.table_id = table_id
922
923 def add_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200924 self._test.vapi.mpls_table_add_del(self.table_id, is_add=1)
Neale Ranns15002542017-09-10 04:39:11 -0700925 self._test.registry.register(self, self._test.logger)
926
927 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200928 self._test.vapi.mpls_table_add_del(self.table_id, is_add=0)
Neale Ranns15002542017-09-10 04:39:11 -0700929
930 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700931 dump = self._test.vapi.mpls_table_dump()
932 for d in dump:
933 if d.mt_table.mt_table_id == self.table_id:
934 return True
Neale Ranns15002542017-09-10 04:39:11 -0700935 return False
936
Neale Ranns15002542017-09-10 04:39:11 -0700937 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200938 return "table-mpls-%d" % (self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700939
940
Neale Ranns5a8123b2017-01-26 01:18:23 -0800941class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000942 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800943 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000944 """
945
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200946 def __init__(
947 self,
948 test,
949 local_label,
950 eos_bit,
951 paths,
952 table_id=0,
953 is_multicast=0,
954 eos_proto=FibPathProto.FIB_PATH_NH_PROTO_IP4,
955 ):
Neale Rannsad422ed2016-11-02 14:20:04 +0000956 self._test = test
957 self.paths = paths
958 self.local_label = local_label
959 self.eos_bit = eos_bit
Neale Ranns097fa662018-05-01 05:17:55 -0700960 self.eos_proto = eos_proto
Neale Rannsad422ed2016-11-02 14:20:04 +0000961 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800962 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000963
964 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700965 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000966 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700967 paths.append(path.encode())
Neale Ranns31ed7442018-02-23 05:29:09 -0800968
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200969 r = self._test.vapi.mpls_route_add_del(
970 self.table_id,
971 self.local_label,
972 self.eos_bit,
973 self.eos_proto,
974 self.is_multicast,
975 paths,
976 1,
977 0,
978 )
Neale Ranns008dbe12018-09-07 09:32:36 -0700979 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800980 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000981
982 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700983 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000984 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700985 paths.append(path.encode())
986
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200987 self._test.vapi.mpls_route_add_del(
988 self.table_id,
989 self.local_label,
990 self.eos_bit,
991 self.eos_proto,
992 self.is_multicast,
993 paths,
994 0,
995 0,
996 )
Neale Ranns5a8123b2017-01-26 01:18:23 -0800997
998 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200999 return find_mpls_route(
1000 self._test, self.table_id, self.local_label, self.eos_bit
1001 )
Neale Ranns5a8123b2017-01-26 01:18:23 -08001002
Neale Ranns5a8123b2017-01-26 01:18:23 -08001003 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001004 return "mpls-route-%d:%s/%d" % (
1005 self.table_id,
1006 self.local_label,
1007 20 + self.eos_bit,
1008 )
Neale Ranns008dbe12018-09-07 09:32:36 -07001009
1010 def get_stats_to(self):
1011 c = self._test.statistics.get_counter("/net/route/to")
1012 return c[0][self.stats_index]
1013
1014 def get_stats_via(self):
1015 c = self._test.statistics.get_counter("/net/route/via")
1016 return c[0][self.stats_index]