blob: 5175de7dd0fe2827e6d2e4dfba04812a8eb22b57 [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
152 for a in addrs:
Neale Ranns097fa662018-05-01 05:17:55 -0700153 if a.sw_if_index == sw_if_index and \
154 a.prefix == prefix:
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700155 return True
156 return False
157
158
Neale Ranns15002542017-09-10 04:39:11 -0700159class VppIpTable(VppObject):
160
161 def __init__(self,
162 test,
163 table_id,
164 is_ip6=0):
165 self._test = test
166 self.table_id = table_id
167 self.is_ip6 = is_ip6
168
169 def add_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100170 self._test.vapi.ip_table_add_del(is_ipv6=self.is_ip6, is_add=1,
171 table_id=self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700172 self._test.registry.register(self, self._test.logger)
173
174 def remove_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100175 self._test.vapi.ip_table_add_del(is_ipv6=self.is_ip6, is_add=0,
176 table_id=self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700177
178 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700179 if self.table_id == 0:
180 # the default table always exists
181 return False
Neale Ranns15002542017-09-10 04:39:11 -0700182 # find the default route
183 return find_route(self._test,
184 "::" if self.is_ip6 else "0.0.0.0",
185 0,
Neale Ranns097fa662018-05-01 05:17:55 -0700186 self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700187
Neale Ranns15002542017-09-10 04:39:11 -0700188 def object_id(self):
189 return ("table-%s-%d" %
190 ("v6" if self.is_ip6 == 1 else "v4",
191 self.table_id))
192
193
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700194class VppIpInterfaceAddress(VppObject):
195
196 def __init__(self, test, intf, addr, len):
197 self._test = test
198 self.intf = intf
199 self.prefix = VppIpPrefix(addr, len)
200
201 def add_vpp_config(self):
202 self._test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000203 sw_if_index=self.intf.sw_if_index, address=self.prefix.bytes,
204 address_length=self.prefix.length, is_ipv6=self.prefix.is_ip6,
Ole Troan9a475372019-03-05 16:58:24 +0100205 is_add=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700206 self._test.registry.register(self, self._test.logger)
207
208 def remove_vpp_config(self):
209 self._test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000210 sw_if_index=self.intf.sw_if_index, address=self.prefix.bytes,
211 address_length=self.prefix.length, is_ipv6=self.prefix.is_ip6,
Ole Troan9a475372019-03-05 16:58:24 +0100212 is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700213
214 def query_vpp_config(self):
215 return fib_interface_ip_prefix(self._test,
216 self.prefix.address,
217 self.prefix.length,
218 self.intf.sw_if_index)
219
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700220 def object_id(self):
221 return "interface-ip-%s-%s" % (self.intf, self.prefix)
222
223
224class VppIpInterfaceBind(VppObject):
225
226 def __init__(self, test, intf, table):
227 self._test = test
228 self.intf = intf
229 self.table = table
230
231 def add_vpp_config(self):
232 if self.table.is_ip6:
233 self.intf.set_table_ip6(self.table.table_id)
234 else:
235 self.intf.set_table_ip4(self.table.table_id)
236 self._test.registry.register(self, self._test.logger)
237
238 def remove_vpp_config(self):
239 if 0 == self.table.table_id:
240 return
241 if self.table.is_ip6:
242 self.intf.set_table_ip6(0)
243 else:
244 self.intf.set_table_ip4(0)
245
246 def query_vpp_config(self):
247 if 0 == self.table.table_id:
248 return False
249 return self._test.vapi.sw_interface_get_table(
250 self.intf.sw_if_index,
251 self.table.is_ip6).vrf_id == self.table.table_id
252
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700253 def object_id(self):
254 return "interface-bind-%s-%s" % (self.intf, self.table)
255
256
Neale Ranns31ed7442018-02-23 05:29:09 -0800257class VppMplsLabel(object):
258 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
259 self.value = value
260 self.mode = mode
261 self.ttl = ttl
262 self.exp = exp
263
264 def encode(self):
265 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
266 return {'label': self.value,
267 'ttl': self.ttl,
268 'exp': self.exp,
269 'is_uniform': is_uniform}
270
Neale Ranns775f73c2018-12-20 03:01:49 -0800271 def __eq__(self, other):
272 if isinstance(other, self.__class__):
273 return (self.value == other.value and
274 self.ttl == other.ttl and
275 self.exp == other.exp and
276 self.mode == other.mode)
277 elif hasattr(other, 'label'):
278 return (self.value == other.label and
279 self.ttl == other.ttl and
280 self.exp == other.exp and
281 (self.mode == MplsLspMode.UNIFORM) == other.is_uniform)
282 else:
283 return False
284
285 def __ne__(self, other):
286 return not (self == other)
287
Neale Ranns31ed7442018-02-23 05:29:09 -0800288
Neale Ranns097fa662018-05-01 05:17:55 -0700289class VppFibPathNextHop(object):
290 def __init__(self, addr,
291 via_label=MPLS_LABEL_INVALID,
292 next_hop_id=INVALID_INDEX):
293 self.addr = VppIpAddressUnion(addr)
294 self.via_label = via_label
295 self.obj_id = next_hop_id
296
297 def encode(self):
298 if self.via_label is not MPLS_LABEL_INVALID:
299 return {'via_label': self.via_label}
300 if self.obj_id is not INVALID_INDEX:
301 return {'obj_id': self.obj_id}
302 else:
303 return {'address': self.addr.encode()}
304
305 def proto(self):
306 if self.via_label is MPLS_LABEL_INVALID:
307 return address_proto(self.addr)
308 else:
309 return FibPathProto.FIB_PATH_NH_PROTO_MPLS
310
311 def __eq__(self, other):
312 if not isinstance(other, self.__class__):
313 # try the other instance's __eq__.
314 return NotImplemented
315 return (self.addr == other.addr and
316 self.via_label == other.via_label and
317 self.obj_id == other.obj_id)
318
319
Neale Ranns5a8123b2017-01-26 01:18:23 -0800320class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000321
Klement Sekerada505f62017-01-04 12:58:53 +0100322 def __init__(
323 self,
324 nh_addr,
325 nh_sw_if_index,
326 nh_table_id=0,
327 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800328 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800329 rpf_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700330 next_hop_id=INVALID_INDEX,
331 proto=None,
332 flags=FibPathFlags.FIB_PATH_FLAG_NONE,
333 type=FibPathType.FIB_PATH_TYPE_NORMAL):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000334 self.nh_itf = nh_sw_if_index
335 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000336 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800337 self.weight = 1
338 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700339 self.proto = proto
340 self.flags = flags
341 self.type = type
342 self.nh = VppFibPathNextHop(nh_addr, nh_via_label, next_hop_id)
343 if proto is None:
344 self.proto = self.nh.proto()
Neale Ranns31426c62017-05-24 10:32:58 -0700345 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700346 self.proto = proto
Neale Ranns810086d2017-11-05 16:26:46 -0800347 self.next_hop_id = next_hop_id
Neale Ranns177bbdc2016-11-15 09:46:51 +0000348
Neale Ranns097fa662018-05-01 05:17:55 -0700349 def encode_labels(self):
Neale Ranns31ed7442018-02-23 05:29:09 -0800350 lstack = []
351 for l in self.nh_labels:
352 if type(l) == VppMplsLabel:
353 lstack.append(l.encode())
354 else:
355 lstack.append({'label': l,
356 'ttl': 255})
Neale Ranns097fa662018-05-01 05:17:55 -0700357 while (len(lstack) < 16):
358 lstack.append({})
359
Neale Ranns31ed7442018-02-23 05:29:09 -0800360 return lstack
361
Neale Ranns097fa662018-05-01 05:17:55 -0700362 def encode(self):
363 return {'weight': 1,
Neale Ranns2303cb12018-02-21 04:57:17 -0800364 'preference': 0,
365 'table_id': self.nh_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700366 'nh': self.nh.encode(),
Neale Ranns2303cb12018-02-21 04:57:17 -0800367 'next_hop_id': self.next_hop_id,
368 'sw_if_index': self.nh_itf,
Neale Ranns097fa662018-05-01 05:17:55 -0700369 'rpf_id': self.rpf_id,
370 'proto': self.proto,
371 'type': self.type,
372 'flags': self.flags,
Neale Ranns2303cb12018-02-21 04:57:17 -0800373 'n_labels': len(self.nh_labels),
Neale Ranns097fa662018-05-01 05:17:55 -0700374 'label_stack': self.encode_labels()}
Neale Ranns2303cb12018-02-21 04:57:17 -0800375
Neale Rannsef90ed02018-09-13 08:45:12 -0700376 def __eq__(self, other):
Neale Ranns775f73c2018-12-20 03:01:49 -0800377 if isinstance(other, self.__class__):
Neale Ranns097fa662018-05-01 05:17:55 -0700378 return self.nh == other.nh
Neale Ranns775f73c2018-12-20 03:01:49 -0800379 elif hasattr(other, 'sw_if_index'):
380 # vl_api_fib_path_t
381 if (len(self.nh_labels) != other.n_labels):
382 return False
383 for i in range(len(self.nh_labels)):
384 if (self.nh_labels[i] != other.label_stack[i]):
385 return False
386 return self.nh_itf == other.sw_if_index
387 else:
388 return False
389
390 def __ne__(self, other):
391 return not (self == other)
Neale Rannsef90ed02018-09-13 08:45:12 -0700392
Neale Ranns177bbdc2016-11-15 09:46:51 +0000393
Neale Ranns5a8123b2017-01-26 01:18:23 -0800394class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000395
Neale Rannsd792d9c2017-10-21 10:53:20 -0700396 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700397 nh=None,
Neale Ranns097fa662018-05-01 05:17:55 -0700398 proto=FibPathProto.FIB_PATH_NH_PROTO_IP4,
399 type=FibPathType.FIB_PATH_TYPE_NORMAL,
400 bier_imp=INVALID_INDEX):
Neale Rannse821ab12017-06-01 07:45:05 -0700401 if not nh:
Neale Ranns097fa662018-05-01 05:17:55 -0700402 nh = "::" if proto is FibPathProto.FIB_PATH_NH_PROTO_IP6 \
403 else "0.0.0.0"
Neale Rannse821ab12017-06-01 07:45:05 -0700404 super(VppMRoutePath, self).__init__(nh,
405 nh_sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700406 proto=proto,
407 type=type,
408 next_hop_id=bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +0000409 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700410 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000411
Neale Ranns097fa662018-05-01 05:17:55 -0700412 def encode(self):
413 return {'path': super(VppMRoutePath, self).encode(),
414 'itf_flags': self.nh_i_flags}
415
Neale Ranns32e1c012016-11-22 17:07:28 +0000416
Neale Ranns5a8123b2017-01-26 01:18:23 -0800417class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000418 """
419 IP Route
420 """
421
422 def __init__(self, test, dest_addr,
Neale Ranns097fa662018-05-01 05:17:55 -0700423 dest_addr_len, paths, table_id=0, register=True):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000424 self._test = test
425 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000426 self.table_id = table_id
Neale Ranns097fa662018-05-01 05:17:55 -0700427 self.prefix = VppIpPrefix(dest_addr, dest_addr_len)
428 self.register = register
Neale Ranns177bbdc2016-11-15 09:46:51 +0000429
Neale Ranns097fa662018-05-01 05:17:55 -0700430 self.encoded_paths = []
431 for path in self.paths:
432 self.encoded_paths.append(path.encode())
433
434 def __eq__(self, other):
435 if self.table_id == other.table_id and \
436 self.prefix == other.prefix:
437 return True
438 return False
439
440 def modify(self, paths):
Neale Ranns69b7aa42017-03-10 03:04:12 -0800441 self.paths = paths
Neale Ranns097fa662018-05-01 05:17:55 -0700442 self.encoded_paths = []
443 for path in self.paths:
444 self.encoded_paths.append(path.encode())
445
446 self._test.vapi.ip_route_add_del(route={'table_id': self.table_id,
447 'prefix': self.prefix.encode(),
448 'n_paths': len(
449 self.encoded_paths),
450 'paths': self.encoded_paths,
451 },
452 is_add=1,
453 is_multipath=0)
Neale Ranns69b7aa42017-03-10 03:04:12 -0800454
Neale Ranns177bbdc2016-11-15 09:46:51 +0000455 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700456 r = self._test.vapi.ip_route_add_del(
457 route={'table_id': self.table_id,
458 'prefix': self.prefix.encode(),
459 'n_paths': len(self.encoded_paths),
460 'paths': self.encoded_paths,
461 },
462 is_add=1,
463 is_multipath=0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700464 self.stats_index = r.stats_index
Neale Ranns097fa662018-05-01 05:17:55 -0700465 if self.register:
466 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000467
468 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700469 self._test.vapi.ip_route_add_del(route={'table_id': self.table_id,
470 'prefix': self.prefix.encode(),
471 'n_paths': len(
472 self.encoded_paths),
473 'paths': self.encoded_paths,
474 },
475 is_add=0,
476 is_multipath=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000477
Neale Ranns5a8123b2017-01-26 01:18:23 -0800478 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800479 return find_route(self._test,
Neale Ranns097fa662018-05-01 05:17:55 -0700480 self.prefix.address,
481 self.prefix.len,
482 self.table_id)
Neale Rannsad422ed2016-11-02 14:20:04 +0000483
Neale Ranns5a8123b2017-01-26 01:18:23 -0800484 def object_id(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800485 return ("%d:%s/%d"
486 % (self.table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700487 self.prefix.address,
488 self.prefix.len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800489
Neale Ranns008dbe12018-09-07 09:32:36 -0700490 def get_stats_to(self):
491 c = self._test.statistics.get_counter("/net/route/to")
492 return c[0][self.stats_index]
493
494 def get_stats_via(self):
495 c = self._test.statistics.get_counter("/net/route/via")
496 return c[0][self.stats_index]
497
Neale Ranns5a8123b2017-01-26 01:18:23 -0800498
499class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000500 """
501 IP Multicast Route
502 """
503
504 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800505 grp_addr_len, e_flags, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700506 rpf_id=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000507 self._test = test
508 self.paths = paths
Neale Ranns32e1c012016-11-22 17:07:28 +0000509 self.table_id = table_id
510 self.e_flags = e_flags
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800511 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000512
Neale Ranns097fa662018-05-01 05:17:55 -0700513 self.prefix = VppIpMPrefix(src_addr, grp_addr, grp_addr_len)
514 self.encoded_paths = []
515 for path in self.paths:
516 self.encoded_paths.append(path.encode())
Neale Ranns32e1c012016-11-22 17:07:28 +0000517
518 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700519 r = self._test.vapi.ip_mroute_add_del(self.table_id,
520 self.prefix.encode(),
521 self.e_flags,
522 self.rpf_id,
523 self.encoded_paths,
524 is_add=1)
525 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800526 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000527
528 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700529 self._test.vapi.ip_mroute_add_del(self.table_id,
530 self.prefix.encode(),
531 self.e_flags,
532 self.rpf_id,
533 self.encoded_paths,
534 is_add=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000535
536 def update_entry_flags(self, flags):
537 self.e_flags = flags
Neale Ranns097fa662018-05-01 05:17:55 -0700538 self._test.vapi.ip_mroute_add_del(self.table_id,
539 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000540 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700541 self.rpf_id,
542 [],
543 is_add=1)
Neale Ranns32e1c012016-11-22 17:07:28 +0000544
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800545 def update_rpf_id(self, rpf_id):
546 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700547 self._test.vapi.ip_mroute_add_del(self.table_id,
548 self.prefix.encode(),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800549 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700550 self.rpf_id,
551 [],
552 is_add=1)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800553
Neale Ranns32e1c012016-11-22 17:07:28 +0000554 def update_path_flags(self, itf, flags):
Neale Ranns097fa662018-05-01 05:17:55 -0700555 for p in range(len(self.paths)):
556 if self.paths[p].nh_itf == itf:
557 self.paths[p].nh_i_flags = flags
558 self.encoded_paths[p] = self.paths[p].encode()
559 break
560
561 self._test.vapi.ip_mroute_add_del(self.table_id,
562 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000563 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700564 self.rpf_id,
565 [self.encoded_paths[p]],
566 is_add=1,
567 is_multipath=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000568
Neale Ranns5a8123b2017-01-26 01:18:23 -0800569 def query_vpp_config(self):
Neale Ranns947ea622018-06-07 23:48:20 -0700570 return find_mroute(self._test,
Neale Ranns097fa662018-05-01 05:17:55 -0700571 self.prefix.gaddr,
572 self.prefix.saddr,
573 self.prefix.length,
574 self.table_id)
Neale Ranns32e1c012016-11-22 17:07:28 +0000575
Neale Ranns5a8123b2017-01-26 01:18:23 -0800576 def object_id(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700577 return ("%d:(%s,%s/%d)" % (self.table_id,
578 self.prefix.saddr,
579 self.prefix.gaddr,
580 self.prefix.length))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800581
Neale Ranns28c142e2018-09-07 09:37:07 -0700582 def get_stats(self):
583 c = self._test.statistics.get_counter("/net/mroute")
584 return c[0][self.stats_index]
585
Neale Ranns5a8123b2017-01-26 01:18:23 -0800586
587class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000588 def __init__(self, test, route, interface, packet):
589 self.route = route
590 self.interface = interface
591 self.packet = packet
592 self.test = test
593
594 def compare(self, signal):
595 self.test.assertEqual(self.interface, signal.sw_if_index)
596 self.test.assertEqual(self.route.table_id, signal.table_id)
Neale Ranns097fa662018-05-01 05:17:55 -0700597 self.test.assertEqual(self.route.prefix, signal.prefix)
Neale Ranns32e1c012016-11-22 17:07:28 +0000598
599
Neale Ranns5a8123b2017-01-26 01:18:23 -0800600class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000601 """
602 MPLS to IP Binding
603 """
604
Neale Ranns5a8123b2017-01-26 01:18:23 -0800605 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700606 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000607 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000608 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700609 self.dest_addr = dest_addr
Neale Ranns097fa662018-05-01 05:17:55 -0700610 self.ip_addr = ip_address(text_type(dest_addr))
Neale Rannsad422ed2016-11-02 14:20:04 +0000611 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800612 self.table_id = table_id
613 self.ip_table_id = ip_table_id
Neale Ranns097fa662018-05-01 05:17:55 -0700614 self.prefix = VppIpPrefix(dest_addr, dest_addr_len)
Neale Rannsad422ed2016-11-02 14:20:04 +0000615
616 def add_vpp_config(self):
617 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Ranns097fa662018-05-01 05:17:55 -0700618 self.prefix.encode(),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800619 table_id=self.table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700620 ip_table_id=self.ip_table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800621 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000622
623 def remove_vpp_config(self):
624 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Ranns097fa662018-05-01 05:17:55 -0700625 self.prefix.encode(),
Neale Rannsf12a83f2017-04-18 09:09:40 -0700626 table_id=self.table_id,
627 ip_table_id=self.ip_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700628 is_bind=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000629
Neale Ranns5a8123b2017-01-26 01:18:23 -0800630 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700631 dump = self._test.vapi.mpls_route_dump(self.table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800632 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700633 if self.local_label == e.mr_route.mr_label \
634 and self.table_id == e.mr_route.mr_table_id:
Neale Ranns5a8123b2017-01-26 01:18:23 -0800635 return True
636 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000637
Neale Ranns5a8123b2017-01-26 01:18:23 -0800638 def object_id(self):
639 return ("%d:%s binds %d:%s/%d"
640 % (self.table_id,
641 self.local_label,
642 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700643 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800644 self.dest_addr_len))
645
646
Neale Ranns15002542017-09-10 04:39:11 -0700647class VppMplsTable(VppObject):
648
649 def __init__(self,
650 test,
651 table_id):
652 self._test = test
653 self.table_id = table_id
654
655 def add_vpp_config(self):
656 self._test.vapi.mpls_table_add_del(
657 self.table_id,
658 is_add=1)
659 self._test.registry.register(self, self._test.logger)
660
661 def remove_vpp_config(self):
662 self._test.vapi.mpls_table_add_del(
663 self.table_id,
664 is_add=0)
665
666 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700667 dump = self._test.vapi.mpls_table_dump()
668 for d in dump:
669 if d.mt_table.mt_table_id == self.table_id:
670 return True
Neale Ranns15002542017-09-10 04:39:11 -0700671 return False
672
Neale Ranns15002542017-09-10 04:39:11 -0700673 def object_id(self):
674 return ("table-mpls-%d" % (self.table_id))
675
676
Neale Ranns5a8123b2017-01-26 01:18:23 -0800677class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000678 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800679 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000680 """
681
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800682 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700683 is_multicast=0,
684 eos_proto=FibPathProto.FIB_PATH_NH_PROTO_IP4):
Neale Rannsad422ed2016-11-02 14:20:04 +0000685 self._test = test
686 self.paths = paths
687 self.local_label = local_label
688 self.eos_bit = eos_bit
Neale Ranns097fa662018-05-01 05:17:55 -0700689 self.eos_proto = eos_proto
Neale Rannsad422ed2016-11-02 14:20:04 +0000690 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800691 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000692
693 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700694 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000695 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700696 paths.append(path.encode())
Neale Ranns31ed7442018-02-23 05:29:09 -0800697
Neale Ranns097fa662018-05-01 05:17:55 -0700698 r = self._test.vapi.mpls_route_add_del(self.table_id,
699 self.local_label,
700 self.eos_bit,
701 self.eos_proto,
702 self.is_multicast,
703 paths, 1, 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700704 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800705 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000706
707 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700708 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000709 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700710 paths.append(path.encode())
711
712 self._test.vapi.mpls_route_add_del(self.table_id,
713 self.local_label,
714 self.eos_bit,
715 self.eos_proto,
716 self.is_multicast,
717 paths, 0, 0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800718
719 def query_vpp_config(self):
Neale Ranns775f73c2018-12-20 03:01:49 -0800720 return find_mpls_route(self._test, self.table_id,
721 self.local_label, self.eos_bit)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800722
Neale Ranns5a8123b2017-01-26 01:18:23 -0800723 def object_id(self):
724 return ("%d:%s/%d"
725 % (self.table_id,
726 self.local_label,
Ole Troan9a475372019-03-05 16:58:24 +0100727 20 + self.eos_bit))
Neale Ranns008dbe12018-09-07 09:32:36 -0700728
729 def get_stats_to(self):
730 c = self._test.statistics.get_counter("/net/route/to")
731 return c[0][self.stats_index]
732
733 def get_stats_via(self):
734 c = self._test.statistics.get_counter("/net/route/via")
735 return c[0][self.stats_index]