blob: 864a39aeb3479ab81d5690cc32fe4eca1f7ad73e [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 Ranns097fa662018-05-01 05:17:55 -07009from vpp_ip import DpoProto, VppIpPrefix, INVALID_INDEX, VppIpAddressUnion, \
10 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
67
68
Neale Ranns31ed7442018-02-23 05:29:09 -080069class MplsLspMode:
70 PIPE = 0
71 UNIFORM = 1
72
73
Neale Ranns93cc3ee2018-10-10 07:22:51 -070074def ip_to_dpo_proto(addr):
Paul Vinciguerrabeded852019-03-01 10:35:55 -080075 if addr.version == 6:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070076 return DpoProto.DPO_PROTO_IP6
77 else:
78 return DpoProto.DPO_PROTO_IP4
79
80
Neale Ranns097fa662018-05-01 05:17:55 -070081def address_proto(ip_addr):
82 if ip_addr.ip_addr.version is 4:
83 return FibPathProto.FIB_PATH_NH_PROTO_IP4
Neale Rannsb3b2de72017-03-08 05:17:22 -080084 else:
Neale Ranns097fa662018-05-01 05:17:55 -070085 return FibPathProto.FIB_PATH_NH_PROTO_IP6
Neale Rannsb3b2de72017-03-08 05:17:22 -080086
Neale Ranns097fa662018-05-01 05:17:55 -070087
88def find_route(test, addr, len, table_id=0):
89 ip_addr = ip_address(text_type(addr))
90
91 if 4 is ip_addr.version:
92 routes = test.vapi.ip_route_dump(table_id, False)
93 prefix = IPv4Network("%s/%d" % (text_type(addr), len), strict=False)
94 else:
95 routes = test.vapi.ip_route_dump(table_id, True)
96 prefix = IPv6Network("%s/%d" % (text_type(addr), len), strict=False)
97
Neale Rannsb3b2de72017-03-08 05:17:22 -080098 for e in routes:
Neale Ranns097fa662018-05-01 05:17:55 -070099 if table_id == e.route.table_id \
100 and prefix == e.route.prefix:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800101 return True
102 return False
103
104
Neale Ranns947ea622018-06-07 23:48:20 -0700105def find_mroute(test, grp_addr, src_addr, grp_addr_len,
Neale Ranns097fa662018-05-01 05:17:55 -0700106 table_id=0):
107 ip_mprefix = VppIpMPrefix(text_type(src_addr),
108 text_type(grp_addr),
109 grp_addr_len)
110
111 if 4 is ip_mprefix.version:
112 routes = test.vapi.ip_mroute_dump(table_id, False)
Neale Ranns947ea622018-06-07 23:48:20 -0700113 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700114 routes = test.vapi.ip_mroute_dump(table_id, True)
115
Neale Ranns947ea622018-06-07 23:48:20 -0700116 for e in routes:
Neale Ranns097fa662018-05-01 05:17:55 -0700117 if table_id == e.route.table_id and ip_mprefix == e.route.prefix:
Neale Ranns947ea622018-06-07 23:48:20 -0700118 return True
119 return False
120
121
Neale Ranns775f73c2018-12-20 03:01:49 -0800122def find_mpls_route(test, table_id, label, eos_bit, paths=None):
Neale Ranns097fa662018-05-01 05:17:55 -0700123 dump = test.vapi.mpls_route_dump(table_id)
Neale Ranns775f73c2018-12-20 03:01:49 -0800124 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700125 if label == e.mr_route.mr_label \
126 and eos_bit == e.mr_route.mr_eos \
127 and table_id == e.mr_route.mr_table_id:
Neale Ranns775f73c2018-12-20 03:01:49 -0800128 if not paths:
129 return True
130 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700131 if (len(paths) != len(e.mr_route.mr_paths)):
Neale Ranns775f73c2018-12-20 03:01:49 -0800132 return False
133 for i in range(len(paths)):
Neale Ranns097fa662018-05-01 05:17:55 -0700134 if (paths[i] != e.mr_route.mr_paths[i]):
Neale Ranns775f73c2018-12-20 03:01:49 -0800135 return False
136 return True
137 return False
138
139
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700140def fib_interface_ip_prefix(test, address, length, sw_if_index):
Neale Ranns097fa662018-05-01 05:17:55 -0700141 ip_addr = ip_address(text_type(address))
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700142
Neale Ranns097fa662018-05-01 05:17:55 -0700143 if 4 is ip_addr.version:
144 addrs = test.vapi.ip_address_dump(sw_if_index)
145 prefix = IPv4Network("%s/%d" % (text_type(address), length),
146 strict=False)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700147 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700148 addrs = test.vapi.ip_address_dump(sw_if_index, is_ipv6=1)
149 prefix = IPv6Network("%s/%d" % (text_type(address), length),
150 strict=False)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700151
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400152 # TODO: refactor this to VppIpPrefix.__eq__
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700153 for a in addrs:
Neale Ranns097fa662018-05-01 05:17:55 -0700154 if a.sw_if_index == sw_if_index and \
155 a.prefix == prefix:
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700156 return True
157 return False
158
159
Neale Ranns15002542017-09-10 04:39:11 -0700160class VppIpTable(VppObject):
161
162 def __init__(self,
163 test,
164 table_id,
165 is_ip6=0):
166 self._test = test
167 self.table_id = table_id
168 self.is_ip6 = is_ip6
169
170 def add_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100171 self._test.vapi.ip_table_add_del(is_ipv6=self.is_ip6, is_add=1,
172 table_id=self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700173 self._test.registry.register(self, self._test.logger)
174
175 def remove_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100176 self._test.vapi.ip_table_add_del(is_ipv6=self.is_ip6, is_add=0,
177 table_id=self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700178
179 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700180 if self.table_id == 0:
181 # the default table always exists
182 return False
Neale Ranns15002542017-09-10 04:39:11 -0700183 # find the default route
184 return find_route(self._test,
185 "::" if self.is_ip6 else "0.0.0.0",
186 0,
Neale Ranns097fa662018-05-01 05:17:55 -0700187 self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700188
Neale Ranns15002542017-09-10 04:39:11 -0700189 def object_id(self):
190 return ("table-%s-%d" %
191 ("v6" if self.is_ip6 == 1 else "v4",
192 self.table_id))
193
194
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700195class VppIpInterfaceAddress(VppObject):
196
197 def __init__(self, test, intf, addr, len):
198 self._test = test
199 self.intf = intf
200 self.prefix = VppIpPrefix(addr, len)
201
202 def add_vpp_config(self):
203 self._test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000204 sw_if_index=self.intf.sw_if_index, address=self.prefix.bytes,
205 address_length=self.prefix.length, is_ipv6=self.prefix.is_ip6,
Ole Troan9a475372019-03-05 16:58:24 +0100206 is_add=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700207 self._test.registry.register(self, self._test.logger)
208
209 def remove_vpp_config(self):
210 self._test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000211 sw_if_index=self.intf.sw_if_index, address=self.prefix.bytes,
212 address_length=self.prefix.length, is_ipv6=self.prefix.is_ip6,
Ole Troan9a475372019-03-05 16:58:24 +0100213 is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700214
215 def query_vpp_config(self):
216 return fib_interface_ip_prefix(self._test,
217 self.prefix.address,
218 self.prefix.length,
219 self.intf.sw_if_index)
220
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700221 def object_id(self):
222 return "interface-ip-%s-%s" % (self.intf, self.prefix)
223
224
225class VppIpInterfaceBind(VppObject):
226
227 def __init__(self, test, intf, table):
228 self._test = test
229 self.intf = intf
230 self.table = table
231
232 def add_vpp_config(self):
233 if self.table.is_ip6:
234 self.intf.set_table_ip6(self.table.table_id)
235 else:
236 self.intf.set_table_ip4(self.table.table_id)
237 self._test.registry.register(self, self._test.logger)
238
239 def remove_vpp_config(self):
240 if 0 == self.table.table_id:
241 return
242 if self.table.is_ip6:
243 self.intf.set_table_ip6(0)
244 else:
245 self.intf.set_table_ip4(0)
246
247 def query_vpp_config(self):
248 if 0 == self.table.table_id:
249 return False
250 return self._test.vapi.sw_interface_get_table(
251 self.intf.sw_if_index,
252 self.table.is_ip6).vrf_id == self.table.table_id
253
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700254 def object_id(self):
255 return "interface-bind-%s-%s" % (self.intf, self.table)
256
257
Neale Ranns31ed7442018-02-23 05:29:09 -0800258class VppMplsLabel(object):
259 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
260 self.value = value
261 self.mode = mode
262 self.ttl = ttl
263 self.exp = exp
264
265 def encode(self):
266 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
267 return {'label': self.value,
268 'ttl': self.ttl,
269 'exp': self.exp,
270 'is_uniform': is_uniform}
271
Neale Ranns775f73c2018-12-20 03:01:49 -0800272 def __eq__(self, other):
273 if isinstance(other, self.__class__):
274 return (self.value == other.value and
275 self.ttl == other.ttl and
276 self.exp == other.exp and
277 self.mode == other.mode)
278 elif hasattr(other, 'label'):
279 return (self.value == other.label and
280 self.ttl == other.ttl and
281 self.exp == other.exp and
282 (self.mode == MplsLspMode.UNIFORM) == other.is_uniform)
283 else:
284 return False
285
286 def __ne__(self, other):
287 return not (self == other)
288
Neale Ranns31ed7442018-02-23 05:29:09 -0800289
Neale Ranns097fa662018-05-01 05:17:55 -0700290class VppFibPathNextHop(object):
291 def __init__(self, addr,
292 via_label=MPLS_LABEL_INVALID,
293 next_hop_id=INVALID_INDEX):
294 self.addr = VppIpAddressUnion(addr)
295 self.via_label = via_label
296 self.obj_id = next_hop_id
297
298 def encode(self):
299 if self.via_label is not MPLS_LABEL_INVALID:
300 return {'via_label': self.via_label}
301 if self.obj_id is not INVALID_INDEX:
302 return {'obj_id': self.obj_id}
303 else:
304 return {'address': self.addr.encode()}
305
306 def proto(self):
307 if self.via_label is MPLS_LABEL_INVALID:
308 return address_proto(self.addr)
309 else:
310 return FibPathProto.FIB_PATH_NH_PROTO_MPLS
311
312 def __eq__(self, other):
313 if not isinstance(other, self.__class__):
314 # try the other instance's __eq__.
315 return NotImplemented
316 return (self.addr == other.addr and
317 self.via_label == other.via_label and
318 self.obj_id == other.obj_id)
319
320
Neale Ranns5a8123b2017-01-26 01:18:23 -0800321class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000322
Klement Sekerada505f62017-01-04 12:58:53 +0100323 def __init__(
324 self,
325 nh_addr,
326 nh_sw_if_index,
327 nh_table_id=0,
328 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800329 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800330 rpf_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700331 next_hop_id=INVALID_INDEX,
332 proto=None,
333 flags=FibPathFlags.FIB_PATH_FLAG_NONE,
334 type=FibPathType.FIB_PATH_TYPE_NORMAL):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000335 self.nh_itf = nh_sw_if_index
336 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000337 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800338 self.weight = 1
339 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700340 self.proto = proto
341 self.flags = flags
342 self.type = type
343 self.nh = VppFibPathNextHop(nh_addr, nh_via_label, next_hop_id)
344 if proto is None:
345 self.proto = self.nh.proto()
Neale Ranns31426c62017-05-24 10:32:58 -0700346 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700347 self.proto = proto
Neale Ranns810086d2017-11-05 16:26:46 -0800348 self.next_hop_id = next_hop_id
Neale Ranns177bbdc2016-11-15 09:46:51 +0000349
Neale Ranns097fa662018-05-01 05:17:55 -0700350 def encode_labels(self):
Neale Ranns31ed7442018-02-23 05:29:09 -0800351 lstack = []
352 for l in self.nh_labels:
353 if type(l) == VppMplsLabel:
354 lstack.append(l.encode())
355 else:
356 lstack.append({'label': l,
357 'ttl': 255})
Neale Ranns097fa662018-05-01 05:17:55 -0700358 while (len(lstack) < 16):
359 lstack.append({})
360
Neale Ranns31ed7442018-02-23 05:29:09 -0800361 return lstack
362
Neale Ranns097fa662018-05-01 05:17:55 -0700363 def encode(self):
364 return {'weight': 1,
Neale Ranns2303cb12018-02-21 04:57:17 -0800365 'preference': 0,
366 'table_id': self.nh_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700367 'nh': self.nh.encode(),
Neale Ranns2303cb12018-02-21 04:57:17 -0800368 'next_hop_id': self.next_hop_id,
369 'sw_if_index': self.nh_itf,
Neale Ranns097fa662018-05-01 05:17:55 -0700370 'rpf_id': self.rpf_id,
371 'proto': self.proto,
372 'type': self.type,
373 'flags': self.flags,
Neale Ranns2303cb12018-02-21 04:57:17 -0800374 'n_labels': len(self.nh_labels),
Neale Ranns097fa662018-05-01 05:17:55 -0700375 'label_stack': self.encode_labels()}
Neale Ranns2303cb12018-02-21 04:57:17 -0800376
Neale Rannsef90ed02018-09-13 08:45:12 -0700377 def __eq__(self, other):
Neale Ranns775f73c2018-12-20 03:01:49 -0800378 if isinstance(other, self.__class__):
Neale Ranns097fa662018-05-01 05:17:55 -0700379 return self.nh == other.nh
Neale Ranns775f73c2018-12-20 03:01:49 -0800380 elif hasattr(other, 'sw_if_index'):
381 # vl_api_fib_path_t
382 if (len(self.nh_labels) != other.n_labels):
383 return False
384 for i in range(len(self.nh_labels)):
385 if (self.nh_labels[i] != other.label_stack[i]):
386 return False
387 return self.nh_itf == other.sw_if_index
388 else:
389 return False
390
391 def __ne__(self, other):
392 return not (self == other)
Neale Rannsef90ed02018-09-13 08:45:12 -0700393
Neale Ranns177bbdc2016-11-15 09:46:51 +0000394
Neale Ranns5a8123b2017-01-26 01:18:23 -0800395class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000396
Neale Rannsd792d9c2017-10-21 10:53:20 -0700397 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700398 nh=None,
Neale Ranns097fa662018-05-01 05:17:55 -0700399 proto=FibPathProto.FIB_PATH_NH_PROTO_IP4,
400 type=FibPathType.FIB_PATH_TYPE_NORMAL,
401 bier_imp=INVALID_INDEX):
Neale Rannse821ab12017-06-01 07:45:05 -0700402 if not nh:
Neale Ranns097fa662018-05-01 05:17:55 -0700403 nh = "::" if proto is FibPathProto.FIB_PATH_NH_PROTO_IP6 \
404 else "0.0.0.0"
Neale Rannse821ab12017-06-01 07:45:05 -0700405 super(VppMRoutePath, self).__init__(nh,
406 nh_sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700407 proto=proto,
408 type=type,
409 next_hop_id=bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +0000410 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700411 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000412
Neale Ranns097fa662018-05-01 05:17:55 -0700413 def encode(self):
414 return {'path': super(VppMRoutePath, self).encode(),
415 'itf_flags': self.nh_i_flags}
416
Neale Ranns32e1c012016-11-22 17:07:28 +0000417
Neale Ranns5a8123b2017-01-26 01:18:23 -0800418class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000419 """
420 IP Route
421 """
422
423 def __init__(self, test, dest_addr,
Neale Ranns097fa662018-05-01 05:17:55 -0700424 dest_addr_len, paths, table_id=0, register=True):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000425 self._test = test
426 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000427 self.table_id = table_id
Neale Ranns097fa662018-05-01 05:17:55 -0700428 self.prefix = VppIpPrefix(dest_addr, dest_addr_len)
429 self.register = register
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400430 self.stats_index = None
Neale Rannsc2ac2352019-07-02 14:33:29 +0000431 self.modified = False
Neale Ranns177bbdc2016-11-15 09:46:51 +0000432
Neale Ranns097fa662018-05-01 05:17:55 -0700433 self.encoded_paths = []
434 for path in self.paths:
435 self.encoded_paths.append(path.encode())
436
437 def __eq__(self, other):
438 if self.table_id == other.table_id and \
439 self.prefix == other.prefix:
440 return True
441 return False
442
443 def modify(self, paths):
Neale Ranns69b7aa42017-03-10 03:04:12 -0800444 self.paths = paths
Neale Ranns097fa662018-05-01 05:17:55 -0700445 self.encoded_paths = []
446 for path in self.paths:
447 self.encoded_paths.append(path.encode())
Neale Rannsc2ac2352019-07-02 14:33:29 +0000448 self.modified = True
Neale Ranns097fa662018-05-01 05:17:55 -0700449
450 self._test.vapi.ip_route_add_del(route={'table_id': self.table_id,
451 'prefix': self.prefix.encode(),
452 'n_paths': len(
453 self.encoded_paths),
454 'paths': self.encoded_paths,
455 },
456 is_add=1,
457 is_multipath=0)
Neale Ranns69b7aa42017-03-10 03:04:12 -0800458
Neale Ranns177bbdc2016-11-15 09:46:51 +0000459 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700460 r = self._test.vapi.ip_route_add_del(
461 route={'table_id': self.table_id,
462 'prefix': self.prefix.encode(),
463 'n_paths': len(self.encoded_paths),
464 'paths': self.encoded_paths,
465 },
466 is_add=1,
467 is_multipath=0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700468 self.stats_index = r.stats_index
Neale Ranns097fa662018-05-01 05:17:55 -0700469 if self.register:
470 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000471
472 def remove_vpp_config(self):
Neale Rannsc2ac2352019-07-02 14:33:29 +0000473 # there's no need to issue different deletes for modified routes
474 # we do this only to test the two different ways to delete routes
475 # eiter by passing all the paths to remove and mutlipath=1 or
476 # passing no paths and multipath=0
477 if self.modified:
478 self._test.vapi.ip_route_add_del(
479 route={'table_id': self.table_id,
480 'prefix': self.prefix.encode(),
481 'n_paths': len(
482 self.encoded_paths),
483 'paths': self.encoded_paths},
484 is_add=0,
485 is_multipath=1)
486 else:
487 self._test.vapi.ip_route_add_del(
488 route={'table_id': self.table_id,
489 'prefix': self.prefix.encode(),
490 'n_paths': 0},
491 is_add=0,
492 is_multipath=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000493
Neale Ranns5a8123b2017-01-26 01:18:23 -0800494 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800495 return find_route(self._test,
Neale Ranns097fa662018-05-01 05:17:55 -0700496 self.prefix.address,
497 self.prefix.len,
498 self.table_id)
Neale Rannsad422ed2016-11-02 14:20:04 +0000499
Neale Ranns5a8123b2017-01-26 01:18:23 -0800500 def object_id(self):
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400501 return ("%s:table-%d-%s/%d" % (
502 'ip6-route' if self.prefix.addr.version == 6 else 'ip-route',
503 self.table_id,
504 self.prefix.address,
505 self.prefix.len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800506
Neale Ranns008dbe12018-09-07 09:32:36 -0700507 def get_stats_to(self):
508 c = self._test.statistics.get_counter("/net/route/to")
509 return c[0][self.stats_index]
510
511 def get_stats_via(self):
512 c = self._test.statistics.get_counter("/net/route/via")
513 return c[0][self.stats_index]
514
Neale Ranns5a8123b2017-01-26 01:18:23 -0800515
516class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000517 """
518 IP Multicast Route
519 """
520
521 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800522 grp_addr_len, e_flags, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700523 rpf_id=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000524 self._test = test
525 self.paths = paths
Neale Ranns32e1c012016-11-22 17:07:28 +0000526 self.table_id = table_id
527 self.e_flags = e_flags
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800528 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000529
Neale Ranns097fa662018-05-01 05:17:55 -0700530 self.prefix = VppIpMPrefix(src_addr, grp_addr, grp_addr_len)
531 self.encoded_paths = []
532 for path in self.paths:
533 self.encoded_paths.append(path.encode())
Neale Ranns32e1c012016-11-22 17:07:28 +0000534
535 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700536 r = self._test.vapi.ip_mroute_add_del(self.table_id,
537 self.prefix.encode(),
538 self.e_flags,
539 self.rpf_id,
540 self.encoded_paths,
541 is_add=1)
542 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800543 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000544
545 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700546 self._test.vapi.ip_mroute_add_del(self.table_id,
547 self.prefix.encode(),
548 self.e_flags,
549 self.rpf_id,
550 self.encoded_paths,
551 is_add=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000552
553 def update_entry_flags(self, flags):
554 self.e_flags = flags
Neale Ranns097fa662018-05-01 05:17:55 -0700555 self._test.vapi.ip_mroute_add_del(self.table_id,
556 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000557 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700558 self.rpf_id,
559 [],
560 is_add=1)
Neale Ranns32e1c012016-11-22 17:07:28 +0000561
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800562 def update_rpf_id(self, rpf_id):
563 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700564 self._test.vapi.ip_mroute_add_del(self.table_id,
565 self.prefix.encode(),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800566 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700567 self.rpf_id,
568 [],
569 is_add=1)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800570
Neale Ranns32e1c012016-11-22 17:07:28 +0000571 def update_path_flags(self, itf, flags):
Neale Ranns097fa662018-05-01 05:17:55 -0700572 for p in range(len(self.paths)):
573 if self.paths[p].nh_itf == itf:
574 self.paths[p].nh_i_flags = flags
575 self.encoded_paths[p] = self.paths[p].encode()
576 break
577
578 self._test.vapi.ip_mroute_add_del(self.table_id,
579 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000580 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700581 self.rpf_id,
582 [self.encoded_paths[p]],
583 is_add=1,
584 is_multipath=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000585
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,
Neale Ranns097fa662018-05-01 05:17:55 -0700588 self.prefix.gaddr,
589 self.prefix.saddr,
590 self.prefix.length,
591 self.table_id)
Neale Ranns32e1c012016-11-22 17:07:28 +0000592
Neale Ranns5a8123b2017-01-26 01:18:23 -0800593 def object_id(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700594 return ("%d:(%s,%s/%d)" % (self.table_id,
595 self.prefix.saddr,
596 self.prefix.gaddr,
597 self.prefix.length))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800598
Neale Ranns28c142e2018-09-07 09:37:07 -0700599 def get_stats(self):
600 c = self._test.statistics.get_counter("/net/mroute")
601 return c[0][self.stats_index]
602
Neale Ranns5a8123b2017-01-26 01:18:23 -0800603
604class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000605 def __init__(self, test, route, interface, packet):
606 self.route = route
607 self.interface = interface
608 self.packet = packet
609 self.test = test
610
611 def compare(self, signal):
612 self.test.assertEqual(self.interface, signal.sw_if_index)
613 self.test.assertEqual(self.route.table_id, signal.table_id)
Neale Ranns097fa662018-05-01 05:17:55 -0700614 self.test.assertEqual(self.route.prefix, signal.prefix)
Neale Ranns32e1c012016-11-22 17:07:28 +0000615
616
Neale Ranns5a8123b2017-01-26 01:18:23 -0800617class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000618 """
619 MPLS to IP Binding
620 """
621
Neale Ranns5a8123b2017-01-26 01:18:23 -0800622 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700623 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000624 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000625 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700626 self.dest_addr = dest_addr
Neale Ranns097fa662018-05-01 05:17:55 -0700627 self.ip_addr = ip_address(text_type(dest_addr))
Neale Rannsad422ed2016-11-02 14:20:04 +0000628 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800629 self.table_id = table_id
630 self.ip_table_id = ip_table_id
Neale Ranns097fa662018-05-01 05:17:55 -0700631 self.prefix = VppIpPrefix(dest_addr, dest_addr_len)
Neale Rannsad422ed2016-11-02 14:20:04 +0000632
633 def add_vpp_config(self):
634 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Ranns097fa662018-05-01 05:17:55 -0700635 self.prefix.encode(),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800636 table_id=self.table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700637 ip_table_id=self.ip_table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800638 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000639
640 def remove_vpp_config(self):
641 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Ranns097fa662018-05-01 05:17:55 -0700642 self.prefix.encode(),
Neale Rannsf12a83f2017-04-18 09:09:40 -0700643 table_id=self.table_id,
644 ip_table_id=self.ip_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700645 is_bind=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000646
Neale Ranns5a8123b2017-01-26 01:18:23 -0800647 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700648 dump = self._test.vapi.mpls_route_dump(self.table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800649 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700650 if self.local_label == e.mr_route.mr_label \
651 and self.table_id == e.mr_route.mr_table_id:
Neale Ranns5a8123b2017-01-26 01:18:23 -0800652 return True
653 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000654
Neale Ranns5a8123b2017-01-26 01:18:23 -0800655 def object_id(self):
656 return ("%d:%s binds %d:%s/%d"
657 % (self.table_id,
658 self.local_label,
659 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700660 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800661 self.dest_addr_len))
662
663
Neale Ranns15002542017-09-10 04:39:11 -0700664class VppMplsTable(VppObject):
665
666 def __init__(self,
667 test,
668 table_id):
669 self._test = test
670 self.table_id = table_id
671
672 def add_vpp_config(self):
673 self._test.vapi.mpls_table_add_del(
674 self.table_id,
675 is_add=1)
676 self._test.registry.register(self, self._test.logger)
677
678 def remove_vpp_config(self):
679 self._test.vapi.mpls_table_add_del(
680 self.table_id,
681 is_add=0)
682
683 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700684 dump = self._test.vapi.mpls_table_dump()
685 for d in dump:
686 if d.mt_table.mt_table_id == self.table_id:
687 return True
Neale Ranns15002542017-09-10 04:39:11 -0700688 return False
689
Neale Ranns15002542017-09-10 04:39:11 -0700690 def object_id(self):
691 return ("table-mpls-%d" % (self.table_id))
692
693
Neale Ranns5a8123b2017-01-26 01:18:23 -0800694class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000695 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800696 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000697 """
698
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800699 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700700 is_multicast=0,
701 eos_proto=FibPathProto.FIB_PATH_NH_PROTO_IP4):
Neale Rannsad422ed2016-11-02 14:20:04 +0000702 self._test = test
703 self.paths = paths
704 self.local_label = local_label
705 self.eos_bit = eos_bit
Neale Ranns097fa662018-05-01 05:17:55 -0700706 self.eos_proto = eos_proto
Neale Rannsad422ed2016-11-02 14:20:04 +0000707 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800708 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000709
710 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700711 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000712 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700713 paths.append(path.encode())
Neale Ranns31ed7442018-02-23 05:29:09 -0800714
Neale Ranns097fa662018-05-01 05:17:55 -0700715 r = self._test.vapi.mpls_route_add_del(self.table_id,
716 self.local_label,
717 self.eos_bit,
718 self.eos_proto,
719 self.is_multicast,
720 paths, 1, 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700721 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800722 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000723
724 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700725 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000726 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700727 paths.append(path.encode())
728
729 self._test.vapi.mpls_route_add_del(self.table_id,
730 self.local_label,
731 self.eos_bit,
732 self.eos_proto,
733 self.is_multicast,
734 paths, 0, 0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800735
736 def query_vpp_config(self):
Neale Ranns775f73c2018-12-20 03:01:49 -0800737 return find_mpls_route(self._test, self.table_id,
738 self.local_label, self.eos_bit)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800739
Neale Ranns5a8123b2017-01-26 01:18:23 -0800740 def object_id(self):
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400741 return ("mpls-route-%d:%s/%d"
Neale Ranns5a8123b2017-01-26 01:18:23 -0800742 % (self.table_id,
743 self.local_label,
Ole Troan9a475372019-03-05 16:58:24 +0100744 20 + self.eos_bit))
Neale Ranns008dbe12018-09-07 09:32:36 -0700745
746 def get_stats_to(self):
747 c = self._test.statistics.get_counter("/net/route/to")
748 return c[0][self.stats_index]
749
750 def get_stats_via(self):
751 c = self._test.statistics.get_counter("/net/route/via")
752 return c[0][self.stats_index]