blob: 67183d943c838e0f902b9b0cc3ace052e87df02e [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
Neale Rannsb3b2de72017-03-08 05:17:22 -08008from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
Neale Rannsefd7bc22019-11-11 08:32:34 +00009from vpp_ip import DpoProto, INVALID_INDEX, VppIpAddressUnion, \
Neale Ranns097fa662018-05-01 05:17:55 -070010 VppIpMPrefix
11from ipaddress import ip_address, IPv4Network, IPv6Network
Neale Ranns177bbdc2016-11-15 09:46:51 +000012
Neale Rannsad422ed2016-11-02 14:20:04 +000013# from vnet/vnet/mpls/mpls_types.h
14MPLS_IETF_MAX_LABEL = 0xfffff
15MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
Neale Ranns177bbdc2016-11-15 09:46:51 +000016
Neale Ranns097fa662018-05-01 05:17:55 -070017try:
18 text_type = unicode
19except NameError:
20 text_type = str
21
Neale Ranns177bbdc2016-11-15 09:46:51 +000022
Neale Ranns180279b2017-03-16 15:49:09 -040023class MRouteItfFlags:
24 MFIB_ITF_FLAG_NONE = 0
25 MFIB_ITF_FLAG_NEGATE_SIGNAL = 1
26 MFIB_ITF_FLAG_ACCEPT = 2
27 MFIB_ITF_FLAG_FORWARD = 4
28 MFIB_ITF_FLAG_SIGNAL_PRESENT = 8
29 MFIB_ITF_FLAG_INTERNAL_COPY = 16
30
31
32class MRouteEntryFlags:
33 MFIB_ENTRY_FLAG_NONE = 0
34 MFIB_ENTRY_FLAG_SIGNAL = 1
35 MFIB_ENTRY_FLAG_DROP = 2
36 MFIB_ENTRY_FLAG_CONNECTED = 4
37 MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8
38
39
Neale Ranns097fa662018-05-01 05:17:55 -070040class FibPathProto:
41 FIB_PATH_NH_PROTO_IP4 = 0
42 FIB_PATH_NH_PROTO_IP6 = 1
43 FIB_PATH_NH_PROTO_MPLS = 2
44 FIB_PATH_NH_PROTO_ETHERNET = 3
45 FIB_PATH_NH_PROTO_BIER = 4
46 FIB_PATH_NH_PROTO_NSH = 5
47
48
49class FibPathType:
50 FIB_PATH_TYPE_NORMAL = 0
51 FIB_PATH_TYPE_LOCAL = 1
52 FIB_PATH_TYPE_DROP = 2
53 FIB_PATH_TYPE_UDP_ENCAP = 3
54 FIB_PATH_TYPE_BIER_IMP = 4
55 FIB_PATH_TYPE_ICMP_UNREACH = 5
56 FIB_PATH_TYPE_ICMP_PROHIBIT = 6
57 FIB_PATH_TYPE_SOURCE_LOOKUP = 7
58 FIB_PATH_TYPE_DVR = 8
59 FIB_PATH_TYPE_INTERFACE_RX = 9
60 FIB_PATH_TYPE_CLASSIFY = 10
61
62
63class FibPathFlags:
64 FIB_PATH_FLAG_NONE = 0
65 FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1
66 FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2
Neale Ranns1dbcf302019-07-19 11:44:53 +000067 FIB_PATH_FLAG_POP_PW_CW = 4
Neale Ranns097fa662018-05-01 05:17:55 -070068
69
Neale Ranns31ed7442018-02-23 05:29:09 -080070class MplsLspMode:
71 PIPE = 0
72 UNIFORM = 1
73
74
Neale Rannsefd7bc22019-11-11 08:32:34 +000075def mk_network(addr, len):
76 if ip_address(text_type(addr)).version == 4:
77 return IPv4Network("%s/%d" % (addr, len), strict=False)
78 else:
79 return IPv6Network("%s/%d" % (addr, len), strict=False)
80
81
Neale Ranns93cc3ee2018-10-10 07:22:51 -070082def ip_to_dpo_proto(addr):
Paul Vinciguerrabeded852019-03-01 10:35:55 -080083 if addr.version == 6:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070084 return DpoProto.DPO_PROTO_IP6
85 else:
86 return DpoProto.DPO_PROTO_IP4
87
88
Neale Ranns097fa662018-05-01 05:17:55 -070089def address_proto(ip_addr):
Ole Troan6e6ad642020-02-04 13:28:13 +010090 if ip_addr.ip_addr.version == 4:
Neale Ranns097fa662018-05-01 05:17:55 -070091 return FibPathProto.FIB_PATH_NH_PROTO_IP4
Neale Rannsb3b2de72017-03-08 05:17:22 -080092 else:
Neale Ranns097fa662018-05-01 05:17:55 -070093 return FibPathProto.FIB_PATH_NH_PROTO_IP6
Neale Rannsb3b2de72017-03-08 05:17:22 -080094
Neale Ranns097fa662018-05-01 05:17:55 -070095
96def find_route(test, addr, len, table_id=0):
Neale Rannsefd7bc22019-11-11 08:32:34 +000097 prefix = mk_network(addr, len)
Neale Ranns097fa662018-05-01 05:17:55 -070098
Ole Troan6e6ad642020-02-04 13:28:13 +010099 if 4 == prefix.version:
Neale Ranns097fa662018-05-01 05:17:55 -0700100 routes = test.vapi.ip_route_dump(table_id, False)
Neale Ranns097fa662018-05-01 05:17:55 -0700101 else:
102 routes = test.vapi.ip_route_dump(table_id, True)
Neale Ranns097fa662018-05-01 05:17:55 -0700103
Neale Rannsb3b2de72017-03-08 05:17:22 -0800104 for e in routes:
Neale Ranns097fa662018-05-01 05:17:55 -0700105 if table_id == e.route.table_id \
Neale Rannsefd7bc22019-11-11 08:32:34 +0000106 and str(e.route.prefix) == str(prefix):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800107 return True
108 return False
109
110
Neale Ranns9db6ada2019-11-08 12:42:31 +0000111def find_route_in_dump(dump, route, table):
112 for r in dump:
113 if table.table_id == r.route.table_id \
114 and route.prefix == r.route.prefix:
115 if len(route.paths) == r.route.n_paths:
116 return True
117 return False
118
119
120def find_mroute_in_dump(dump, route, table):
121 for r in dump:
122 if table.table_id == r.route.table_id \
123 and route.prefix == r.route.prefix:
124 return True
125 return False
126
127
Neale Ranns947ea622018-06-07 23:48:20 -0700128def find_mroute(test, grp_addr, src_addr, grp_addr_len,
Neale Ranns097fa662018-05-01 05:17:55 -0700129 table_id=0):
130 ip_mprefix = VppIpMPrefix(text_type(src_addr),
131 text_type(grp_addr),
132 grp_addr_len)
133
Ole Troan6e6ad642020-02-04 13:28:13 +0100134 if 4 == ip_mprefix.version:
Neale Ranns097fa662018-05-01 05:17:55 -0700135 routes = test.vapi.ip_mroute_dump(table_id, False)
Neale Ranns947ea622018-06-07 23:48:20 -0700136 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700137 routes = test.vapi.ip_mroute_dump(table_id, True)
138
Neale Ranns947ea622018-06-07 23:48:20 -0700139 for e in routes:
Neale Ranns097fa662018-05-01 05:17:55 -0700140 if table_id == e.route.table_id and ip_mprefix == e.route.prefix:
Neale Ranns947ea622018-06-07 23:48:20 -0700141 return True
142 return False
143
144
Neale Ranns775f73c2018-12-20 03:01:49 -0800145def find_mpls_route(test, table_id, label, eos_bit, paths=None):
Neale Ranns097fa662018-05-01 05:17:55 -0700146 dump = test.vapi.mpls_route_dump(table_id)
Neale Ranns775f73c2018-12-20 03:01:49 -0800147 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700148 if label == e.mr_route.mr_label \
149 and eos_bit == e.mr_route.mr_eos \
150 and table_id == e.mr_route.mr_table_id:
Neale Ranns775f73c2018-12-20 03:01:49 -0800151 if not paths:
152 return True
153 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700154 if (len(paths) != len(e.mr_route.mr_paths)):
Neale Ranns775f73c2018-12-20 03:01:49 -0800155 return False
156 for i in range(len(paths)):
Neale Ranns097fa662018-05-01 05:17:55 -0700157 if (paths[i] != e.mr_route.mr_paths[i]):
Neale Ranns775f73c2018-12-20 03:01:49 -0800158 return False
159 return True
160 return False
161
162
Neale Rannsefd7bc22019-11-11 08:32:34 +0000163def fib_interface_ip_prefix(test, addr, len, sw_if_index):
164 # can't use python net here since we need the host bits in the prefix
165 prefix = "%s/%d" % (addr, len)
166 addrs = test.vapi.ip_address_dump(
167 sw_if_index,
168 is_ipv6=(6 == ip_address(addr).version))
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700169
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700170 for a in addrs:
Neale Ranns097fa662018-05-01 05:17:55 -0700171 if a.sw_if_index == sw_if_index and \
Neale Rannsefd7bc22019-11-11 08:32:34 +0000172 str(a.prefix) == prefix:
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700173 return True
174 return False
175
176
Neale Ranns15002542017-09-10 04:39:11 -0700177class VppIpTable(VppObject):
178
179 def __init__(self,
180 test,
181 table_id,
182 is_ip6=0):
183 self._test = test
184 self.table_id = table_id
185 self.is_ip6 = is_ip6
186
187 def add_vpp_config(self):
Neale Ranns9db6ada2019-11-08 12:42:31 +0000188 self._test.vapi.ip_table_add_del(is_add=1,
189 table={'is_ip6': self.is_ip6,
190 'table_id': self.table_id})
Neale Ranns15002542017-09-10 04:39:11 -0700191 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000192 return self
Neale Ranns15002542017-09-10 04:39:11 -0700193
194 def remove_vpp_config(self):
Neale Ranns9db6ada2019-11-08 12:42:31 +0000195 self._test.vapi.ip_table_add_del(is_add=0,
196 table={'is_ip6': self.is_ip6,
197 'table_id': self.table_id})
198
199 def replace_begin(self):
200 self._test.vapi.ip_table_replace_begin(
201 table={'is_ip6': self.is_ip6,
202 'table_id': self.table_id})
203
204 def replace_end(self):
205 self._test.vapi.ip_table_replace_end(
206 table={'is_ip6': self.is_ip6,
207 'table_id': self.table_id})
208
209 def flush(self):
210 self._test.vapi.ip_table_flush(table={'is_ip6': self.is_ip6,
211 'table_id': self.table_id})
212
213 def dump(self):
214 return self._test.vapi.ip_route_dump(self.table_id, self.is_ip6)
215
216 def mdump(self):
217 return self._test.vapi.ip_mroute_dump(self.table_id, self.is_ip6)
Neale Ranns15002542017-09-10 04:39:11 -0700218
219 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700220 if self.table_id == 0:
221 # the default table always exists
222 return False
Neale Ranns15002542017-09-10 04:39:11 -0700223 # find the default route
224 return find_route(self._test,
225 "::" if self.is_ip6 else "0.0.0.0",
226 0,
Neale Ranns097fa662018-05-01 05:17:55 -0700227 self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700228
Neale Ranns15002542017-09-10 04:39:11 -0700229 def object_id(self):
230 return ("table-%s-%d" %
231 ("v6" if self.is_ip6 == 1 else "v4",
232 self.table_id))
233
234
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700235class VppIpInterfaceAddress(VppObject):
236
237 def __init__(self, test, intf, addr, len):
238 self._test = test
239 self.intf = intf
Neale Rannsefd7bc22019-11-11 08:32:34 +0000240 self.addr = addr
241 self.len = len
242 self.prefix = "%s/%d" % (addr, len)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700243
244 def add_vpp_config(self):
245 self._test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000246 sw_if_index=self.intf.sw_if_index, prefix=self.prefix,
Ole Troan9a475372019-03-05 16:58:24 +0100247 is_add=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700248 self._test.registry.register(self, self._test.logger)
Neale Ranns9efcee62019-11-26 19:30:08 +0000249 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700250
251 def remove_vpp_config(self):
252 self._test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000253 sw_if_index=self.intf.sw_if_index, prefix=self.prefix,
Ole Troan9a475372019-03-05 16:58:24 +0100254 is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700255
256 def query_vpp_config(self):
257 return fib_interface_ip_prefix(self._test,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000258 self.addr,
259 self.len,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700260 self.intf.sw_if_index)
261
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700262 def object_id(self):
263 return "interface-ip-%s-%s" % (self.intf, self.prefix)
264
265
266class VppIpInterfaceBind(VppObject):
267
268 def __init__(self, test, intf, table):
269 self._test = test
270 self.intf = intf
271 self.table = table
272
273 def add_vpp_config(self):
274 if self.table.is_ip6:
275 self.intf.set_table_ip6(self.table.table_id)
276 else:
277 self.intf.set_table_ip4(self.table.table_id)
278 self._test.registry.register(self, self._test.logger)
279
280 def remove_vpp_config(self):
281 if 0 == self.table.table_id:
282 return
283 if self.table.is_ip6:
284 self.intf.set_table_ip6(0)
285 else:
286 self.intf.set_table_ip4(0)
287
288 def query_vpp_config(self):
289 if 0 == self.table.table_id:
290 return False
291 return self._test.vapi.sw_interface_get_table(
292 self.intf.sw_if_index,
293 self.table.is_ip6).vrf_id == self.table.table_id
294
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700295 def object_id(self):
296 return "interface-bind-%s-%s" % (self.intf, self.table)
297
298
Neale Ranns31ed7442018-02-23 05:29:09 -0800299class VppMplsLabel(object):
300 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
301 self.value = value
302 self.mode = mode
303 self.ttl = ttl
304 self.exp = exp
305
306 def encode(self):
307 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
308 return {'label': self.value,
309 'ttl': self.ttl,
310 'exp': self.exp,
311 'is_uniform': is_uniform}
312
Neale Ranns775f73c2018-12-20 03:01:49 -0800313 def __eq__(self, other):
314 if isinstance(other, self.__class__):
315 return (self.value == other.value and
316 self.ttl == other.ttl and
317 self.exp == other.exp and
318 self.mode == other.mode)
319 elif hasattr(other, 'label'):
320 return (self.value == other.label and
321 self.ttl == other.ttl and
322 self.exp == other.exp and
323 (self.mode == MplsLspMode.UNIFORM) == other.is_uniform)
324 else:
325 return False
326
327 def __ne__(self, other):
328 return not (self == other)
329
Neale Ranns31ed7442018-02-23 05:29:09 -0800330
Neale Ranns097fa662018-05-01 05:17:55 -0700331class VppFibPathNextHop(object):
332 def __init__(self, addr,
333 via_label=MPLS_LABEL_INVALID,
334 next_hop_id=INVALID_INDEX):
335 self.addr = VppIpAddressUnion(addr)
336 self.via_label = via_label
337 self.obj_id = next_hop_id
338
339 def encode(self):
340 if self.via_label is not MPLS_LABEL_INVALID:
341 return {'via_label': self.via_label}
342 if self.obj_id is not INVALID_INDEX:
343 return {'obj_id': self.obj_id}
344 else:
345 return {'address': self.addr.encode()}
346
347 def proto(self):
348 if self.via_label is MPLS_LABEL_INVALID:
349 return address_proto(self.addr)
350 else:
351 return FibPathProto.FIB_PATH_NH_PROTO_MPLS
352
353 def __eq__(self, other):
354 if not isinstance(other, self.__class__):
355 # try the other instance's __eq__.
356 return NotImplemented
357 return (self.addr == other.addr and
358 self.via_label == other.via_label and
359 self.obj_id == other.obj_id)
360
361
Neale Ranns5a8123b2017-01-26 01:18:23 -0800362class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000363
Klement Sekerada505f62017-01-04 12:58:53 +0100364 def __init__(
365 self,
366 nh_addr,
367 nh_sw_if_index,
368 nh_table_id=0,
369 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800370 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800371 rpf_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700372 next_hop_id=INVALID_INDEX,
373 proto=None,
374 flags=FibPathFlags.FIB_PATH_FLAG_NONE,
375 type=FibPathType.FIB_PATH_TYPE_NORMAL):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000376 self.nh_itf = nh_sw_if_index
377 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000378 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800379 self.weight = 1
380 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700381 self.proto = proto
382 self.flags = flags
383 self.type = type
384 self.nh = VppFibPathNextHop(nh_addr, nh_via_label, next_hop_id)
385 if proto is None:
386 self.proto = self.nh.proto()
Neale Ranns31426c62017-05-24 10:32:58 -0700387 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700388 self.proto = proto
Neale Ranns810086d2017-11-05 16:26:46 -0800389 self.next_hop_id = next_hop_id
Neale Ranns177bbdc2016-11-15 09:46:51 +0000390
Neale Ranns097fa662018-05-01 05:17:55 -0700391 def encode_labels(self):
Neale Ranns31ed7442018-02-23 05:29:09 -0800392 lstack = []
393 for l in self.nh_labels:
394 if type(l) == VppMplsLabel:
395 lstack.append(l.encode())
396 else:
397 lstack.append({'label': l,
398 'ttl': 255})
Neale Ranns097fa662018-05-01 05:17:55 -0700399 while (len(lstack) < 16):
400 lstack.append({})
401
Neale Ranns31ed7442018-02-23 05:29:09 -0800402 return lstack
403
Neale Ranns097fa662018-05-01 05:17:55 -0700404 def encode(self):
405 return {'weight': 1,
Neale Ranns2303cb12018-02-21 04:57:17 -0800406 'preference': 0,
407 'table_id': self.nh_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700408 'nh': self.nh.encode(),
Neale Ranns2303cb12018-02-21 04:57:17 -0800409 'next_hop_id': self.next_hop_id,
410 'sw_if_index': self.nh_itf,
Neale Ranns097fa662018-05-01 05:17:55 -0700411 'rpf_id': self.rpf_id,
412 'proto': self.proto,
413 'type': self.type,
414 'flags': self.flags,
Neale Ranns2303cb12018-02-21 04:57:17 -0800415 'n_labels': len(self.nh_labels),
Neale Ranns097fa662018-05-01 05:17:55 -0700416 'label_stack': self.encode_labels()}
Neale Ranns2303cb12018-02-21 04:57:17 -0800417
Neale Rannsef90ed02018-09-13 08:45:12 -0700418 def __eq__(self, other):
Neale Ranns775f73c2018-12-20 03:01:49 -0800419 if isinstance(other, self.__class__):
Neale Ranns097fa662018-05-01 05:17:55 -0700420 return self.nh == other.nh
Neale Ranns775f73c2018-12-20 03:01:49 -0800421 elif hasattr(other, 'sw_if_index'):
422 # vl_api_fib_path_t
423 if (len(self.nh_labels) != other.n_labels):
424 return False
425 for i in range(len(self.nh_labels)):
426 if (self.nh_labels[i] != other.label_stack[i]):
427 return False
428 return self.nh_itf == other.sw_if_index
429 else:
430 return False
431
432 def __ne__(self, other):
433 return not (self == other)
Neale Rannsef90ed02018-09-13 08:45:12 -0700434
Neale Ranns177bbdc2016-11-15 09:46:51 +0000435
Neale Ranns5a8123b2017-01-26 01:18:23 -0800436class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000437
Neale Rannsd792d9c2017-10-21 10:53:20 -0700438 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700439 nh=None,
Neale Ranns097fa662018-05-01 05:17:55 -0700440 proto=FibPathProto.FIB_PATH_NH_PROTO_IP4,
441 type=FibPathType.FIB_PATH_TYPE_NORMAL,
442 bier_imp=INVALID_INDEX):
Neale Rannse821ab12017-06-01 07:45:05 -0700443 if not nh:
Neale Ranns097fa662018-05-01 05:17:55 -0700444 nh = "::" if proto is FibPathProto.FIB_PATH_NH_PROTO_IP6 \
445 else "0.0.0.0"
Neale Rannse821ab12017-06-01 07:45:05 -0700446 super(VppMRoutePath, self).__init__(nh,
447 nh_sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700448 proto=proto,
449 type=type,
450 next_hop_id=bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +0000451 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700452 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000453
Neale Ranns097fa662018-05-01 05:17:55 -0700454 def encode(self):
455 return {'path': super(VppMRoutePath, self).encode(),
456 'itf_flags': self.nh_i_flags}
457
Neale Ranns32e1c012016-11-22 17:07:28 +0000458
Neale Ranns5a8123b2017-01-26 01:18:23 -0800459class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000460 """
461 IP Route
462 """
463
464 def __init__(self, test, dest_addr,
Neale Ranns097fa662018-05-01 05:17:55 -0700465 dest_addr_len, paths, table_id=0, register=True):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000466 self._test = test
467 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000468 self.table_id = table_id
Neale Rannsefd7bc22019-11-11 08:32:34 +0000469 self.prefix = mk_network(dest_addr, dest_addr_len)
Neale Ranns097fa662018-05-01 05:17:55 -0700470 self.register = register
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400471 self.stats_index = None
Neale Rannsc2ac2352019-07-02 14:33:29 +0000472 self.modified = False
Neale Ranns177bbdc2016-11-15 09:46:51 +0000473
Neale Ranns097fa662018-05-01 05:17:55 -0700474 self.encoded_paths = []
475 for path in self.paths:
476 self.encoded_paths.append(path.encode())
477
478 def __eq__(self, other):
479 if self.table_id == other.table_id and \
480 self.prefix == other.prefix:
481 return True
482 return False
483
484 def modify(self, paths):
Neale Ranns69b7aa42017-03-10 03:04:12 -0800485 self.paths = paths
Neale Ranns097fa662018-05-01 05:17:55 -0700486 self.encoded_paths = []
487 for path in self.paths:
488 self.encoded_paths.append(path.encode())
Neale Rannsc2ac2352019-07-02 14:33:29 +0000489 self.modified = True
Neale Ranns097fa662018-05-01 05:17:55 -0700490
491 self._test.vapi.ip_route_add_del(route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000492 'prefix': self.prefix,
Neale Ranns097fa662018-05-01 05:17:55 -0700493 'n_paths': len(
494 self.encoded_paths),
495 'paths': self.encoded_paths,
496 },
497 is_add=1,
498 is_multipath=0)
Neale Ranns69b7aa42017-03-10 03:04:12 -0800499
Neale Ranns177bbdc2016-11-15 09:46:51 +0000500 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700501 r = self._test.vapi.ip_route_add_del(
502 route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000503 'prefix': self.prefix,
Neale Ranns097fa662018-05-01 05:17:55 -0700504 'n_paths': len(self.encoded_paths),
505 'paths': self.encoded_paths,
506 },
507 is_add=1,
508 is_multipath=0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700509 self.stats_index = r.stats_index
Neale Ranns097fa662018-05-01 05:17:55 -0700510 if self.register:
511 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000512 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000513
514 def remove_vpp_config(self):
Neale Rannsc2ac2352019-07-02 14:33:29 +0000515 # there's no need to issue different deletes for modified routes
516 # we do this only to test the two different ways to delete routes
517 # eiter by passing all the paths to remove and mutlipath=1 or
518 # passing no paths and multipath=0
519 if self.modified:
520 self._test.vapi.ip_route_add_del(
521 route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000522 'prefix': self.prefix,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000523 'n_paths': len(
524 self.encoded_paths),
525 'paths': self.encoded_paths},
526 is_add=0,
527 is_multipath=1)
528 else:
529 self._test.vapi.ip_route_add_del(
530 route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000531 'prefix': self.prefix,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000532 'n_paths': 0},
533 is_add=0,
534 is_multipath=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000535
Neale Ranns5a8123b2017-01-26 01:18:23 -0800536 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800537 return find_route(self._test,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000538 self.prefix.network_address,
539 self.prefix.prefixlen,
Neale Ranns097fa662018-05-01 05:17:55 -0700540 self.table_id)
Neale Rannsad422ed2016-11-02 14:20:04 +0000541
Neale Ranns5a8123b2017-01-26 01:18:23 -0800542 def object_id(self):
Neale Rannsefd7bc22019-11-11 08:32:34 +0000543 return ("%s:table-%d-%s" % (
544 'ip6-route' if self.prefix.version == 6 else 'ip-route',
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400545 self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000546 self.prefix))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800547
Neale Ranns008dbe12018-09-07 09:32:36 -0700548 def get_stats_to(self):
549 c = self._test.statistics.get_counter("/net/route/to")
550 return c[0][self.stats_index]
551
552 def get_stats_via(self):
553 c = self._test.statistics.get_counter("/net/route/via")
554 return c[0][self.stats_index]
555
Neale Ranns5a8123b2017-01-26 01:18:23 -0800556
557class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000558 """
559 IP Multicast Route
560 """
561
562 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800563 grp_addr_len, e_flags, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700564 rpf_id=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000565 self._test = test
566 self.paths = paths
Neale Ranns32e1c012016-11-22 17:07:28 +0000567 self.table_id = table_id
568 self.e_flags = e_flags
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800569 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000570
Neale Ranns097fa662018-05-01 05:17:55 -0700571 self.prefix = VppIpMPrefix(src_addr, grp_addr, grp_addr_len)
572 self.encoded_paths = []
573 for path in self.paths:
574 self.encoded_paths.append(path.encode())
Neale Ranns32e1c012016-11-22 17:07:28 +0000575
576 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700577 r = self._test.vapi.ip_mroute_add_del(self.table_id,
578 self.prefix.encode(),
579 self.e_flags,
580 self.rpf_id,
581 self.encoded_paths,
582 is_add=1)
583 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800584 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000585 return self
Neale Ranns32e1c012016-11-22 17:07:28 +0000586
587 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700588 self._test.vapi.ip_mroute_add_del(self.table_id,
589 self.prefix.encode(),
590 self.e_flags,
591 self.rpf_id,
592 self.encoded_paths,
593 is_add=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000594
595 def update_entry_flags(self, flags):
596 self.e_flags = flags
Neale Ranns097fa662018-05-01 05:17:55 -0700597 self._test.vapi.ip_mroute_add_del(self.table_id,
598 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000599 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700600 self.rpf_id,
601 [],
602 is_add=1)
Neale Ranns32e1c012016-11-22 17:07:28 +0000603
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800604 def update_rpf_id(self, rpf_id):
605 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700606 self._test.vapi.ip_mroute_add_del(self.table_id,
607 self.prefix.encode(),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800608 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700609 self.rpf_id,
610 [],
611 is_add=1)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800612
Neale Ranns32e1c012016-11-22 17:07:28 +0000613 def update_path_flags(self, itf, flags):
Neale Ranns097fa662018-05-01 05:17:55 -0700614 for p in range(len(self.paths)):
615 if self.paths[p].nh_itf == itf:
616 self.paths[p].nh_i_flags = flags
617 self.encoded_paths[p] = self.paths[p].encode()
618 break
619
620 self._test.vapi.ip_mroute_add_del(self.table_id,
621 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000622 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700623 self.rpf_id,
624 [self.encoded_paths[p]],
625 is_add=1,
626 is_multipath=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000627
Neale Ranns5a8123b2017-01-26 01:18:23 -0800628 def query_vpp_config(self):
Neale Ranns947ea622018-06-07 23:48:20 -0700629 return find_mroute(self._test,
Neale Ranns097fa662018-05-01 05:17:55 -0700630 self.prefix.gaddr,
631 self.prefix.saddr,
632 self.prefix.length,
633 self.table_id)
Neale Ranns32e1c012016-11-22 17:07:28 +0000634
Neale Ranns5a8123b2017-01-26 01:18:23 -0800635 def object_id(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700636 return ("%d:(%s,%s/%d)" % (self.table_id,
637 self.prefix.saddr,
638 self.prefix.gaddr,
639 self.prefix.length))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800640
Neale Ranns28c142e2018-09-07 09:37:07 -0700641 def get_stats(self):
642 c = self._test.statistics.get_counter("/net/mroute")
643 return c[0][self.stats_index]
644
Neale Ranns5a8123b2017-01-26 01:18:23 -0800645
646class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000647 def __init__(self, test, route, interface, packet):
648 self.route = route
649 self.interface = interface
650 self.packet = packet
651 self.test = test
652
653 def compare(self, signal):
654 self.test.assertEqual(self.interface, signal.sw_if_index)
655 self.test.assertEqual(self.route.table_id, signal.table_id)
Neale Ranns097fa662018-05-01 05:17:55 -0700656 self.test.assertEqual(self.route.prefix, signal.prefix)
Neale Ranns32e1c012016-11-22 17:07:28 +0000657
658
Neale Ranns5a8123b2017-01-26 01:18:23 -0800659class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000660 """
661 MPLS to IP Binding
662 """
663
Neale Ranns5a8123b2017-01-26 01:18:23 -0800664 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700665 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000666 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000667 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700668 self.dest_addr = dest_addr
Neale Ranns097fa662018-05-01 05:17:55 -0700669 self.ip_addr = ip_address(text_type(dest_addr))
Neale Rannsad422ed2016-11-02 14:20:04 +0000670 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800671 self.table_id = table_id
672 self.ip_table_id = ip_table_id
Neale Rannsefd7bc22019-11-11 08:32:34 +0000673 self.prefix = mk_network(dest_addr, dest_addr_len)
Neale Rannsad422ed2016-11-02 14:20:04 +0000674
675 def add_vpp_config(self):
676 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000677 self.prefix,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800678 table_id=self.table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700679 ip_table_id=self.ip_table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800680 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000681
682 def remove_vpp_config(self):
683 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000684 self.prefix,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700685 table_id=self.table_id,
686 ip_table_id=self.ip_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700687 is_bind=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000688
Neale Ranns5a8123b2017-01-26 01:18:23 -0800689 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700690 dump = self._test.vapi.mpls_route_dump(self.table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800691 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700692 if self.local_label == e.mr_route.mr_label \
693 and self.table_id == e.mr_route.mr_table_id:
Neale Ranns5a8123b2017-01-26 01:18:23 -0800694 return True
695 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000696
Neale Ranns5a8123b2017-01-26 01:18:23 -0800697 def object_id(self):
698 return ("%d:%s binds %d:%s/%d"
699 % (self.table_id,
700 self.local_label,
701 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700702 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800703 self.dest_addr_len))
704
705
Neale Ranns15002542017-09-10 04:39:11 -0700706class VppMplsTable(VppObject):
707
708 def __init__(self,
709 test,
710 table_id):
711 self._test = test
712 self.table_id = table_id
713
714 def add_vpp_config(self):
715 self._test.vapi.mpls_table_add_del(
716 self.table_id,
717 is_add=1)
718 self._test.registry.register(self, self._test.logger)
719
720 def remove_vpp_config(self):
721 self._test.vapi.mpls_table_add_del(
722 self.table_id,
723 is_add=0)
724
725 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700726 dump = self._test.vapi.mpls_table_dump()
727 for d in dump:
728 if d.mt_table.mt_table_id == self.table_id:
729 return True
Neale Ranns15002542017-09-10 04:39:11 -0700730 return False
731
Neale Ranns15002542017-09-10 04:39:11 -0700732 def object_id(self):
733 return ("table-mpls-%d" % (self.table_id))
734
735
Neale Ranns5a8123b2017-01-26 01:18:23 -0800736class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000737 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800738 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000739 """
740
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800741 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700742 is_multicast=0,
743 eos_proto=FibPathProto.FIB_PATH_NH_PROTO_IP4):
Neale Rannsad422ed2016-11-02 14:20:04 +0000744 self._test = test
745 self.paths = paths
746 self.local_label = local_label
747 self.eos_bit = eos_bit
Neale Ranns097fa662018-05-01 05:17:55 -0700748 self.eos_proto = eos_proto
Neale Rannsad422ed2016-11-02 14:20:04 +0000749 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800750 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000751
752 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700753 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000754 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700755 paths.append(path.encode())
Neale Ranns31ed7442018-02-23 05:29:09 -0800756
Neale Ranns097fa662018-05-01 05:17:55 -0700757 r = self._test.vapi.mpls_route_add_del(self.table_id,
758 self.local_label,
759 self.eos_bit,
760 self.eos_proto,
761 self.is_multicast,
762 paths, 1, 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700763 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800764 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000765
766 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700767 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000768 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700769 paths.append(path.encode())
770
771 self._test.vapi.mpls_route_add_del(self.table_id,
772 self.local_label,
773 self.eos_bit,
774 self.eos_proto,
775 self.is_multicast,
776 paths, 0, 0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800777
778 def query_vpp_config(self):
Neale Ranns775f73c2018-12-20 03:01:49 -0800779 return find_mpls_route(self._test, self.table_id,
780 self.local_label, self.eos_bit)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800781
Neale Ranns5a8123b2017-01-26 01:18:23 -0800782 def object_id(self):
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400783 return ("mpls-route-%d:%s/%d"
Neale Ranns5a8123b2017-01-26 01:18:23 -0800784 % (self.table_id,
785 self.local_label,
Ole Troan9a475372019-03-05 16:58:24 +0100786 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]