blob: 8f9d51a7d2a075a86e72924a68b48abe0cfe037a [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
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 Ranns93cc3ee2018-10-10 07:22:51 -070075def ip_to_dpo_proto(addr):
Paul Vinciguerrabeded852019-03-01 10:35:55 -080076 if addr.version == 6:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070077 return DpoProto.DPO_PROTO_IP6
78 else:
79 return DpoProto.DPO_PROTO_IP4
80
81
Neale Ranns097fa662018-05-01 05:17:55 -070082def address_proto(ip_addr):
83 if ip_addr.ip_addr.version is 4:
84 return FibPathProto.FIB_PATH_NH_PROTO_IP4
Neale Rannsb3b2de72017-03-08 05:17:22 -080085 else:
Neale Ranns097fa662018-05-01 05:17:55 -070086 return FibPathProto.FIB_PATH_NH_PROTO_IP6
Neale Rannsb3b2de72017-03-08 05:17:22 -080087
Neale Ranns097fa662018-05-01 05:17:55 -070088
89def find_route(test, addr, len, table_id=0):
90 ip_addr = ip_address(text_type(addr))
91
92 if 4 is ip_addr.version:
93 routes = test.vapi.ip_route_dump(table_id, False)
94 prefix = IPv4Network("%s/%d" % (text_type(addr), len), strict=False)
95 else:
96 routes = test.vapi.ip_route_dump(table_id, True)
97 prefix = IPv6Network("%s/%d" % (text_type(addr), len), strict=False)
98
Neale Rannsb3b2de72017-03-08 05:17:22 -080099 for e in routes:
Neale Ranns097fa662018-05-01 05:17:55 -0700100 if table_id == e.route.table_id \
101 and prefix == e.route.prefix:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800102 return True
103 return False
104
105
Neale Ranns947ea622018-06-07 23:48:20 -0700106def find_mroute(test, grp_addr, src_addr, grp_addr_len,
Neale Ranns097fa662018-05-01 05:17:55 -0700107 table_id=0):
108 ip_mprefix = VppIpMPrefix(text_type(src_addr),
109 text_type(grp_addr),
110 grp_addr_len)
111
112 if 4 is ip_mprefix.version:
113 routes = test.vapi.ip_mroute_dump(table_id, False)
Neale Ranns947ea622018-06-07 23:48:20 -0700114 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700115 routes = test.vapi.ip_mroute_dump(table_id, True)
116
Neale Ranns947ea622018-06-07 23:48:20 -0700117 for e in routes:
Neale Ranns097fa662018-05-01 05:17:55 -0700118 if table_id == e.route.table_id and ip_mprefix == e.route.prefix:
Neale Ranns947ea622018-06-07 23:48:20 -0700119 return True
120 return False
121
122
Neale Ranns775f73c2018-12-20 03:01:49 -0800123def find_mpls_route(test, table_id, label, eos_bit, paths=None):
Neale Ranns097fa662018-05-01 05:17:55 -0700124 dump = test.vapi.mpls_route_dump(table_id)
Neale Ranns775f73c2018-12-20 03:01:49 -0800125 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700126 if label == e.mr_route.mr_label \
127 and eos_bit == e.mr_route.mr_eos \
128 and table_id == e.mr_route.mr_table_id:
Neale Ranns775f73c2018-12-20 03:01:49 -0800129 if not paths:
130 return True
131 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700132 if (len(paths) != len(e.mr_route.mr_paths)):
Neale Ranns775f73c2018-12-20 03:01:49 -0800133 return False
134 for i in range(len(paths)):
Neale Ranns097fa662018-05-01 05:17:55 -0700135 if (paths[i] != e.mr_route.mr_paths[i]):
Neale Ranns775f73c2018-12-20 03:01:49 -0800136 return False
137 return True
138 return False
139
140
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700141def fib_interface_ip_prefix(test, address, length, sw_if_index):
Neale Ranns097fa662018-05-01 05:17:55 -0700142 ip_addr = ip_address(text_type(address))
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700143
Neale Ranns097fa662018-05-01 05:17:55 -0700144 if 4 is ip_addr.version:
145 addrs = test.vapi.ip_address_dump(sw_if_index)
146 prefix = IPv4Network("%s/%d" % (text_type(address), length),
147 strict=False)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700148 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700149 addrs = test.vapi.ip_address_dump(sw_if_index, is_ipv6=1)
150 prefix = IPv6Network("%s/%d" % (text_type(address), length),
151 strict=False)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700152
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400153 # TODO: refactor this to VppIpPrefix.__eq__
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700154 for a in addrs:
Neale Ranns097fa662018-05-01 05:17:55 -0700155 if a.sw_if_index == sw_if_index and \
Ole Troan75761b92019-09-11 17:49:08 +0200156 a.prefix.network == prefix:
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700157 return True
158 return False
159
160
Neale Ranns15002542017-09-10 04:39:11 -0700161class VppIpTable(VppObject):
162
163 def __init__(self,
164 test,
165 table_id,
166 is_ip6=0):
167 self._test = test
168 self.table_id = table_id
169 self.is_ip6 = is_ip6
170
171 def add_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100172 self._test.vapi.ip_table_add_del(is_ipv6=self.is_ip6, is_add=1,
173 table_id=self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700174 self._test.registry.register(self, self._test.logger)
175
176 def remove_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100177 self._test.vapi.ip_table_add_del(is_ipv6=self.is_ip6, is_add=0,
178 table_id=self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700179
180 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700181 if self.table_id == 0:
182 # the default table always exists
183 return False
Neale Ranns15002542017-09-10 04:39:11 -0700184 # find the default route
185 return find_route(self._test,
186 "::" if self.is_ip6 else "0.0.0.0",
187 0,
Neale Ranns097fa662018-05-01 05:17:55 -0700188 self.table_id)
Neale Ranns15002542017-09-10 04:39:11 -0700189
Neale Ranns15002542017-09-10 04:39:11 -0700190 def object_id(self):
191 return ("table-%s-%d" %
192 ("v6" if self.is_ip6 == 1 else "v4",
193 self.table_id))
194
195
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700196class VppIpInterfaceAddress(VppObject):
197
198 def __init__(self, test, intf, addr, len):
199 self._test = test
200 self.intf = intf
201 self.prefix = VppIpPrefix(addr, len)
202
203 def add_vpp_config(self):
204 self._test.vapi.sw_interface_add_del_address(
Jakub Grajciar053204a2019-03-18 13:17:53 +0100205 sw_if_index=self.intf.sw_if_index, prefix=self.prefix.encode(),
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(
Jakub Grajciar053204a2019-03-18 13:17:53 +0100211 sw_if_index=self.intf.sw_if_index, prefix=self.prefix.encode(),
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
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400429 self.stats_index = None
Neale Rannsc2ac2352019-07-02 14:33:29 +0000430 self.modified = False
Neale Ranns177bbdc2016-11-15 09:46:51 +0000431
Neale Ranns097fa662018-05-01 05:17:55 -0700432 self.encoded_paths = []
433 for path in self.paths:
434 self.encoded_paths.append(path.encode())
435
436 def __eq__(self, other):
437 if self.table_id == other.table_id and \
438 self.prefix == other.prefix:
439 return True
440 return False
441
442 def modify(self, paths):
Neale Ranns69b7aa42017-03-10 03:04:12 -0800443 self.paths = paths
Neale Ranns097fa662018-05-01 05:17:55 -0700444 self.encoded_paths = []
445 for path in self.paths:
446 self.encoded_paths.append(path.encode())
Neale Rannsc2ac2352019-07-02 14:33:29 +0000447 self.modified = True
Neale Ranns097fa662018-05-01 05:17:55 -0700448
449 self._test.vapi.ip_route_add_del(route={'table_id': self.table_id,
450 'prefix': self.prefix.encode(),
451 'n_paths': len(
452 self.encoded_paths),
453 'paths': self.encoded_paths,
454 },
455 is_add=1,
456 is_multipath=0)
Neale Ranns69b7aa42017-03-10 03:04:12 -0800457
Neale Ranns177bbdc2016-11-15 09:46:51 +0000458 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700459 r = self._test.vapi.ip_route_add_del(
460 route={'table_id': self.table_id,
461 'prefix': self.prefix.encode(),
462 'n_paths': len(self.encoded_paths),
463 'paths': self.encoded_paths,
464 },
465 is_add=1,
466 is_multipath=0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700467 self.stats_index = r.stats_index
Neale Ranns097fa662018-05-01 05:17:55 -0700468 if self.register:
469 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000470
471 def remove_vpp_config(self):
Neale Rannsc2ac2352019-07-02 14:33:29 +0000472 # there's no need to issue different deletes for modified routes
473 # we do this only to test the two different ways to delete routes
474 # eiter by passing all the paths to remove and mutlipath=1 or
475 # passing no paths and multipath=0
476 if self.modified:
477 self._test.vapi.ip_route_add_del(
478 route={'table_id': self.table_id,
479 'prefix': self.prefix.encode(),
480 'n_paths': len(
481 self.encoded_paths),
482 'paths': self.encoded_paths},
483 is_add=0,
484 is_multipath=1)
485 else:
486 self._test.vapi.ip_route_add_del(
487 route={'table_id': self.table_id,
488 'prefix': self.prefix.encode(),
489 'n_paths': 0},
490 is_add=0,
491 is_multipath=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000492
Neale Ranns5a8123b2017-01-26 01:18:23 -0800493 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800494 return find_route(self._test,
Neale Ranns097fa662018-05-01 05:17:55 -0700495 self.prefix.address,
496 self.prefix.len,
497 self.table_id)
Neale Rannsad422ed2016-11-02 14:20:04 +0000498
Neale Ranns5a8123b2017-01-26 01:18:23 -0800499 def object_id(self):
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400500 return ("%s:table-%d-%s/%d" % (
501 'ip6-route' if self.prefix.addr.version == 6 else 'ip-route',
502 self.table_id,
503 self.prefix.address,
504 self.prefix.len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800505
Neale Ranns008dbe12018-09-07 09:32:36 -0700506 def get_stats_to(self):
507 c = self._test.statistics.get_counter("/net/route/to")
508 return c[0][self.stats_index]
509
510 def get_stats_via(self):
511 c = self._test.statistics.get_counter("/net/route/via")
512 return c[0][self.stats_index]
513
Neale Ranns5a8123b2017-01-26 01:18:23 -0800514
515class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000516 """
517 IP Multicast Route
518 """
519
520 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800521 grp_addr_len, e_flags, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700522 rpf_id=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000523 self._test = test
524 self.paths = paths
Neale Ranns32e1c012016-11-22 17:07:28 +0000525 self.table_id = table_id
526 self.e_flags = e_flags
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800527 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000528
Neale Ranns097fa662018-05-01 05:17:55 -0700529 self.prefix = VppIpMPrefix(src_addr, grp_addr, grp_addr_len)
530 self.encoded_paths = []
531 for path in self.paths:
532 self.encoded_paths.append(path.encode())
Neale Ranns32e1c012016-11-22 17:07:28 +0000533
534 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700535 r = self._test.vapi.ip_mroute_add_del(self.table_id,
536 self.prefix.encode(),
537 self.e_flags,
538 self.rpf_id,
539 self.encoded_paths,
540 is_add=1)
541 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800542 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000543
544 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700545 self._test.vapi.ip_mroute_add_del(self.table_id,
546 self.prefix.encode(),
547 self.e_flags,
548 self.rpf_id,
549 self.encoded_paths,
550 is_add=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000551
552 def update_entry_flags(self, flags):
553 self.e_flags = flags
Neale Ranns097fa662018-05-01 05:17:55 -0700554 self._test.vapi.ip_mroute_add_del(self.table_id,
555 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000556 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700557 self.rpf_id,
558 [],
559 is_add=1)
Neale Ranns32e1c012016-11-22 17:07:28 +0000560
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800561 def update_rpf_id(self, rpf_id):
562 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700563 self._test.vapi.ip_mroute_add_del(self.table_id,
564 self.prefix.encode(),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800565 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700566 self.rpf_id,
567 [],
568 is_add=1)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800569
Neale Ranns32e1c012016-11-22 17:07:28 +0000570 def update_path_flags(self, itf, flags):
Neale Ranns097fa662018-05-01 05:17:55 -0700571 for p in range(len(self.paths)):
572 if self.paths[p].nh_itf == itf:
573 self.paths[p].nh_i_flags = flags
574 self.encoded_paths[p] = self.paths[p].encode()
575 break
576
577 self._test.vapi.ip_mroute_add_del(self.table_id,
578 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000579 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700580 self.rpf_id,
581 [self.encoded_paths[p]],
582 is_add=1,
583 is_multipath=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000584
Neale Ranns5a8123b2017-01-26 01:18:23 -0800585 def query_vpp_config(self):
Neale Ranns947ea622018-06-07 23:48:20 -0700586 return find_mroute(self._test,
Neale Ranns097fa662018-05-01 05:17:55 -0700587 self.prefix.gaddr,
588 self.prefix.saddr,
589 self.prefix.length,
590 self.table_id)
Neale Ranns32e1c012016-11-22 17:07:28 +0000591
Neale Ranns5a8123b2017-01-26 01:18:23 -0800592 def object_id(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700593 return ("%d:(%s,%s/%d)" % (self.table_id,
594 self.prefix.saddr,
595 self.prefix.gaddr,
596 self.prefix.length))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800597
Neale Ranns28c142e2018-09-07 09:37:07 -0700598 def get_stats(self):
599 c = self._test.statistics.get_counter("/net/mroute")
600 return c[0][self.stats_index]
601
Neale Ranns5a8123b2017-01-26 01:18:23 -0800602
603class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000604 def __init__(self, test, route, interface, packet):
605 self.route = route
606 self.interface = interface
607 self.packet = packet
608 self.test = test
609
610 def compare(self, signal):
611 self.test.assertEqual(self.interface, signal.sw_if_index)
612 self.test.assertEqual(self.route.table_id, signal.table_id)
Neale Ranns097fa662018-05-01 05:17:55 -0700613 self.test.assertEqual(self.route.prefix, signal.prefix)
Neale Ranns32e1c012016-11-22 17:07:28 +0000614
615
Neale Ranns5a8123b2017-01-26 01:18:23 -0800616class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000617 """
618 MPLS to IP Binding
619 """
620
Neale Ranns5a8123b2017-01-26 01:18:23 -0800621 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700622 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000623 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000624 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700625 self.dest_addr = dest_addr
Neale Ranns097fa662018-05-01 05:17:55 -0700626 self.ip_addr = ip_address(text_type(dest_addr))
Neale Rannsad422ed2016-11-02 14:20:04 +0000627 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800628 self.table_id = table_id
629 self.ip_table_id = ip_table_id
Neale Ranns097fa662018-05-01 05:17:55 -0700630 self.prefix = VppIpPrefix(dest_addr, dest_addr_len)
Neale Rannsad422ed2016-11-02 14:20:04 +0000631
632 def add_vpp_config(self):
633 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Ranns097fa662018-05-01 05:17:55 -0700634 self.prefix.encode(),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800635 table_id=self.table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700636 ip_table_id=self.ip_table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800637 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000638
639 def remove_vpp_config(self):
640 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Ranns097fa662018-05-01 05:17:55 -0700641 self.prefix.encode(),
Neale Rannsf12a83f2017-04-18 09:09:40 -0700642 table_id=self.table_id,
643 ip_table_id=self.ip_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700644 is_bind=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000645
Neale Ranns5a8123b2017-01-26 01:18:23 -0800646 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700647 dump = self._test.vapi.mpls_route_dump(self.table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800648 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700649 if self.local_label == e.mr_route.mr_label \
650 and self.table_id == e.mr_route.mr_table_id:
Neale Ranns5a8123b2017-01-26 01:18:23 -0800651 return True
652 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000653
Neale Ranns5a8123b2017-01-26 01:18:23 -0800654 def object_id(self):
655 return ("%d:%s binds %d:%s/%d"
656 % (self.table_id,
657 self.local_label,
658 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700659 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800660 self.dest_addr_len))
661
662
Neale Ranns15002542017-09-10 04:39:11 -0700663class VppMplsTable(VppObject):
664
665 def __init__(self,
666 test,
667 table_id):
668 self._test = test
669 self.table_id = table_id
670
671 def add_vpp_config(self):
672 self._test.vapi.mpls_table_add_del(
673 self.table_id,
674 is_add=1)
675 self._test.registry.register(self, self._test.logger)
676
677 def remove_vpp_config(self):
678 self._test.vapi.mpls_table_add_del(
679 self.table_id,
680 is_add=0)
681
682 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700683 dump = self._test.vapi.mpls_table_dump()
684 for d in dump:
685 if d.mt_table.mt_table_id == self.table_id:
686 return True
Neale Ranns15002542017-09-10 04:39:11 -0700687 return False
688
Neale Ranns15002542017-09-10 04:39:11 -0700689 def object_id(self):
690 return ("table-mpls-%d" % (self.table_id))
691
692
Neale Ranns5a8123b2017-01-26 01:18:23 -0800693class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000694 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800695 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000696 """
697
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800698 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700699 is_multicast=0,
700 eos_proto=FibPathProto.FIB_PATH_NH_PROTO_IP4):
Neale Rannsad422ed2016-11-02 14:20:04 +0000701 self._test = test
702 self.paths = paths
703 self.local_label = local_label
704 self.eos_bit = eos_bit
Neale Ranns097fa662018-05-01 05:17:55 -0700705 self.eos_proto = eos_proto
Neale Rannsad422ed2016-11-02 14:20:04 +0000706 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800707 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000708
709 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700710 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000711 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700712 paths.append(path.encode())
Neale Ranns31ed7442018-02-23 05:29:09 -0800713
Neale Ranns097fa662018-05-01 05:17:55 -0700714 r = self._test.vapi.mpls_route_add_del(self.table_id,
715 self.local_label,
716 self.eos_bit,
717 self.eos_proto,
718 self.is_multicast,
719 paths, 1, 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700720 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800721 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000722
723 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700724 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000725 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700726 paths.append(path.encode())
727
728 self._test.vapi.mpls_route_add_del(self.table_id,
729 self.local_label,
730 self.eos_bit,
731 self.eos_proto,
732 self.is_multicast,
733 paths, 0, 0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800734
735 def query_vpp_config(self):
Neale Ranns775f73c2018-12-20 03:01:49 -0800736 return find_mpls_route(self._test, self.table_id,
737 self.local_label, self.eos_bit)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800738
Neale Ranns5a8123b2017-01-26 01:18:23 -0800739 def object_id(self):
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400740 return ("mpls-route-%d:%s/%d"
Neale Ranns5a8123b2017-01-26 01:18:23 -0800741 % (self.table_id,
742 self.local_label,
Ole Troan9a475372019-03-05 16:58:24 +0100743 20 + self.eos_bit))
Neale Ranns008dbe12018-09-07 09:32:36 -0700744
745 def get_stats_to(self):
746 c = self._test.statistics.get_counter("/net/route/to")
747 return c[0][self.stats_index]
748
749 def get_stats_via(self):
750 c = self._test.statistics.get_counter("/net/route/via")
751 return c[0][self.stats_index]