blob: 031412cc2fa22c1845a073569597dbb750e4e5c1 [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):
90 if ip_addr.ip_addr.version is 4:
91 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
Neale Rannsefd7bc22019-11-11 08:32:34 +000099 if 4 is 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
134 if 4 is ip_mprefix.version:
135 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)
249
250 def remove_vpp_config(self):
251 self._test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000252 sw_if_index=self.intf.sw_if_index, prefix=self.prefix,
Ole Troan9a475372019-03-05 16:58:24 +0100253 is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700254
255 def query_vpp_config(self):
256 return fib_interface_ip_prefix(self._test,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000257 self.addr,
258 self.len,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700259 self.intf.sw_if_index)
260
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700261 def object_id(self):
262 return "interface-ip-%s-%s" % (self.intf, self.prefix)
263
264
265class VppIpInterfaceBind(VppObject):
266
267 def __init__(self, test, intf, table):
268 self._test = test
269 self.intf = intf
270 self.table = table
271
272 def add_vpp_config(self):
273 if self.table.is_ip6:
274 self.intf.set_table_ip6(self.table.table_id)
275 else:
276 self.intf.set_table_ip4(self.table.table_id)
277 self._test.registry.register(self, self._test.logger)
278
279 def remove_vpp_config(self):
280 if 0 == self.table.table_id:
281 return
282 if self.table.is_ip6:
283 self.intf.set_table_ip6(0)
284 else:
285 self.intf.set_table_ip4(0)
286
287 def query_vpp_config(self):
288 if 0 == self.table.table_id:
289 return False
290 return self._test.vapi.sw_interface_get_table(
291 self.intf.sw_if_index,
292 self.table.is_ip6).vrf_id == self.table.table_id
293
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700294 def object_id(self):
295 return "interface-bind-%s-%s" % (self.intf, self.table)
296
297
Neale Ranns31ed7442018-02-23 05:29:09 -0800298class VppMplsLabel(object):
299 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
300 self.value = value
301 self.mode = mode
302 self.ttl = ttl
303 self.exp = exp
304
305 def encode(self):
306 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
307 return {'label': self.value,
308 'ttl': self.ttl,
309 'exp': self.exp,
310 'is_uniform': is_uniform}
311
Neale Ranns775f73c2018-12-20 03:01:49 -0800312 def __eq__(self, other):
313 if isinstance(other, self.__class__):
314 return (self.value == other.value and
315 self.ttl == other.ttl and
316 self.exp == other.exp and
317 self.mode == other.mode)
318 elif hasattr(other, 'label'):
319 return (self.value == other.label and
320 self.ttl == other.ttl and
321 self.exp == other.exp and
322 (self.mode == MplsLspMode.UNIFORM) == other.is_uniform)
323 else:
324 return False
325
326 def __ne__(self, other):
327 return not (self == other)
328
Neale Ranns31ed7442018-02-23 05:29:09 -0800329
Neale Ranns097fa662018-05-01 05:17:55 -0700330class VppFibPathNextHop(object):
331 def __init__(self, addr,
332 via_label=MPLS_LABEL_INVALID,
333 next_hop_id=INVALID_INDEX):
334 self.addr = VppIpAddressUnion(addr)
335 self.via_label = via_label
336 self.obj_id = next_hop_id
337
338 def encode(self):
339 if self.via_label is not MPLS_LABEL_INVALID:
340 return {'via_label': self.via_label}
341 if self.obj_id is not INVALID_INDEX:
342 return {'obj_id': self.obj_id}
343 else:
344 return {'address': self.addr.encode()}
345
346 def proto(self):
347 if self.via_label is MPLS_LABEL_INVALID:
348 return address_proto(self.addr)
349 else:
350 return FibPathProto.FIB_PATH_NH_PROTO_MPLS
351
352 def __eq__(self, other):
353 if not isinstance(other, self.__class__):
354 # try the other instance's __eq__.
355 return NotImplemented
356 return (self.addr == other.addr and
357 self.via_label == other.via_label and
358 self.obj_id == other.obj_id)
359
360
Neale Ranns5a8123b2017-01-26 01:18:23 -0800361class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000362
Klement Sekerada505f62017-01-04 12:58:53 +0100363 def __init__(
364 self,
365 nh_addr,
366 nh_sw_if_index,
367 nh_table_id=0,
368 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800369 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800370 rpf_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700371 next_hop_id=INVALID_INDEX,
372 proto=None,
373 flags=FibPathFlags.FIB_PATH_FLAG_NONE,
374 type=FibPathType.FIB_PATH_TYPE_NORMAL):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000375 self.nh_itf = nh_sw_if_index
376 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000377 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800378 self.weight = 1
379 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700380 self.proto = proto
381 self.flags = flags
382 self.type = type
383 self.nh = VppFibPathNextHop(nh_addr, nh_via_label, next_hop_id)
384 if proto is None:
385 self.proto = self.nh.proto()
Neale Ranns31426c62017-05-24 10:32:58 -0700386 else:
Neale Ranns097fa662018-05-01 05:17:55 -0700387 self.proto = proto
Neale Ranns810086d2017-11-05 16:26:46 -0800388 self.next_hop_id = next_hop_id
Neale Ranns177bbdc2016-11-15 09:46:51 +0000389
Neale Ranns097fa662018-05-01 05:17:55 -0700390 def encode_labels(self):
Neale Ranns31ed7442018-02-23 05:29:09 -0800391 lstack = []
392 for l in self.nh_labels:
393 if type(l) == VppMplsLabel:
394 lstack.append(l.encode())
395 else:
396 lstack.append({'label': l,
397 'ttl': 255})
Neale Ranns097fa662018-05-01 05:17:55 -0700398 while (len(lstack) < 16):
399 lstack.append({})
400
Neale Ranns31ed7442018-02-23 05:29:09 -0800401 return lstack
402
Neale Ranns097fa662018-05-01 05:17:55 -0700403 def encode(self):
404 return {'weight': 1,
Neale Ranns2303cb12018-02-21 04:57:17 -0800405 'preference': 0,
406 'table_id': self.nh_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700407 'nh': self.nh.encode(),
Neale Ranns2303cb12018-02-21 04:57:17 -0800408 'next_hop_id': self.next_hop_id,
409 'sw_if_index': self.nh_itf,
Neale Ranns097fa662018-05-01 05:17:55 -0700410 'rpf_id': self.rpf_id,
411 'proto': self.proto,
412 'type': self.type,
413 'flags': self.flags,
Neale Ranns2303cb12018-02-21 04:57:17 -0800414 'n_labels': len(self.nh_labels),
Neale Ranns097fa662018-05-01 05:17:55 -0700415 'label_stack': self.encode_labels()}
Neale Ranns2303cb12018-02-21 04:57:17 -0800416
Neale Rannsef90ed02018-09-13 08:45:12 -0700417 def __eq__(self, other):
Neale Ranns775f73c2018-12-20 03:01:49 -0800418 if isinstance(other, self.__class__):
Neale Ranns097fa662018-05-01 05:17:55 -0700419 return self.nh == other.nh
Neale Ranns775f73c2018-12-20 03:01:49 -0800420 elif hasattr(other, 'sw_if_index'):
421 # vl_api_fib_path_t
422 if (len(self.nh_labels) != other.n_labels):
423 return False
424 for i in range(len(self.nh_labels)):
425 if (self.nh_labels[i] != other.label_stack[i]):
426 return False
427 return self.nh_itf == other.sw_if_index
428 else:
429 return False
430
431 def __ne__(self, other):
432 return not (self == other)
Neale Rannsef90ed02018-09-13 08:45:12 -0700433
Neale Ranns177bbdc2016-11-15 09:46:51 +0000434
Neale Ranns5a8123b2017-01-26 01:18:23 -0800435class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000436
Neale Rannsd792d9c2017-10-21 10:53:20 -0700437 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700438 nh=None,
Neale Ranns097fa662018-05-01 05:17:55 -0700439 proto=FibPathProto.FIB_PATH_NH_PROTO_IP4,
440 type=FibPathType.FIB_PATH_TYPE_NORMAL,
441 bier_imp=INVALID_INDEX):
Neale Rannse821ab12017-06-01 07:45:05 -0700442 if not nh:
Neale Ranns097fa662018-05-01 05:17:55 -0700443 nh = "::" if proto is FibPathProto.FIB_PATH_NH_PROTO_IP6 \
444 else "0.0.0.0"
Neale Rannse821ab12017-06-01 07:45:05 -0700445 super(VppMRoutePath, self).__init__(nh,
446 nh_sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700447 proto=proto,
448 type=type,
449 next_hop_id=bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +0000450 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700451 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000452
Neale Ranns097fa662018-05-01 05:17:55 -0700453 def encode(self):
454 return {'path': super(VppMRoutePath, self).encode(),
455 'itf_flags': self.nh_i_flags}
456
Neale Ranns32e1c012016-11-22 17:07:28 +0000457
Neale Ranns5a8123b2017-01-26 01:18:23 -0800458class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000459 """
460 IP Route
461 """
462
463 def __init__(self, test, dest_addr,
Neale Ranns097fa662018-05-01 05:17:55 -0700464 dest_addr_len, paths, table_id=0, register=True):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000465 self._test = test
466 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000467 self.table_id = table_id
Neale Rannsefd7bc22019-11-11 08:32:34 +0000468 self.prefix = mk_network(dest_addr, dest_addr_len)
Neale Ranns097fa662018-05-01 05:17:55 -0700469 self.register = register
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400470 self.stats_index = None
Neale Rannsc2ac2352019-07-02 14:33:29 +0000471 self.modified = False
Neale Ranns177bbdc2016-11-15 09:46:51 +0000472
Neale Ranns097fa662018-05-01 05:17:55 -0700473 self.encoded_paths = []
474 for path in self.paths:
475 self.encoded_paths.append(path.encode())
476
477 def __eq__(self, other):
478 if self.table_id == other.table_id and \
479 self.prefix == other.prefix:
480 return True
481 return False
482
483 def modify(self, paths):
Neale Ranns69b7aa42017-03-10 03:04:12 -0800484 self.paths = paths
Neale Ranns097fa662018-05-01 05:17:55 -0700485 self.encoded_paths = []
486 for path in self.paths:
487 self.encoded_paths.append(path.encode())
Neale Rannsc2ac2352019-07-02 14:33:29 +0000488 self.modified = True
Neale Ranns097fa662018-05-01 05:17:55 -0700489
490 self._test.vapi.ip_route_add_del(route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000491 'prefix': self.prefix,
Neale Ranns097fa662018-05-01 05:17:55 -0700492 'n_paths': len(
493 self.encoded_paths),
494 'paths': self.encoded_paths,
495 },
496 is_add=1,
497 is_multipath=0)
Neale Ranns69b7aa42017-03-10 03:04:12 -0800498
Neale Ranns177bbdc2016-11-15 09:46:51 +0000499 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700500 r = self._test.vapi.ip_route_add_del(
501 route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000502 'prefix': self.prefix,
Neale Ranns097fa662018-05-01 05:17:55 -0700503 'n_paths': len(self.encoded_paths),
504 'paths': self.encoded_paths,
505 },
506 is_add=1,
507 is_multipath=0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700508 self.stats_index = r.stats_index
Neale Ranns097fa662018-05-01 05:17:55 -0700509 if self.register:
510 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000511 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000512
513 def remove_vpp_config(self):
Neale Rannsc2ac2352019-07-02 14:33:29 +0000514 # there's no need to issue different deletes for modified routes
515 # we do this only to test the two different ways to delete routes
516 # eiter by passing all the paths to remove and mutlipath=1 or
517 # passing no paths and multipath=0
518 if self.modified:
519 self._test.vapi.ip_route_add_del(
520 route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000521 'prefix': self.prefix,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000522 'n_paths': len(
523 self.encoded_paths),
524 'paths': self.encoded_paths},
525 is_add=0,
526 is_multipath=1)
527 else:
528 self._test.vapi.ip_route_add_del(
529 route={'table_id': self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000530 'prefix': self.prefix,
Neale Rannsc2ac2352019-07-02 14:33:29 +0000531 'n_paths': 0},
532 is_add=0,
533 is_multipath=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000534
Neale Ranns5a8123b2017-01-26 01:18:23 -0800535 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800536 return find_route(self._test,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000537 self.prefix.network_address,
538 self.prefix.prefixlen,
Neale Ranns097fa662018-05-01 05:17:55 -0700539 self.table_id)
Neale Rannsad422ed2016-11-02 14:20:04 +0000540
Neale Ranns5a8123b2017-01-26 01:18:23 -0800541 def object_id(self):
Neale Rannsefd7bc22019-11-11 08:32:34 +0000542 return ("%s:table-%d-%s" % (
543 'ip6-route' if self.prefix.version == 6 else 'ip-route',
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400544 self.table_id,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000545 self.prefix))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800546
Neale Ranns008dbe12018-09-07 09:32:36 -0700547 def get_stats_to(self):
548 c = self._test.statistics.get_counter("/net/route/to")
549 return c[0][self.stats_index]
550
551 def get_stats_via(self):
552 c = self._test.statistics.get_counter("/net/route/via")
553 return c[0][self.stats_index]
554
Neale Ranns5a8123b2017-01-26 01:18:23 -0800555
556class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000557 """
558 IP Multicast Route
559 """
560
561 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800562 grp_addr_len, e_flags, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700563 rpf_id=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000564 self._test = test
565 self.paths = paths
Neale Ranns32e1c012016-11-22 17:07:28 +0000566 self.table_id = table_id
567 self.e_flags = e_flags
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800568 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000569
Neale Ranns097fa662018-05-01 05:17:55 -0700570 self.prefix = VppIpMPrefix(src_addr, grp_addr, grp_addr_len)
571 self.encoded_paths = []
572 for path in self.paths:
573 self.encoded_paths.append(path.encode())
Neale Ranns32e1c012016-11-22 17:07:28 +0000574
575 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700576 r = self._test.vapi.ip_mroute_add_del(self.table_id,
577 self.prefix.encode(),
578 self.e_flags,
579 self.rpf_id,
580 self.encoded_paths,
581 is_add=1)
582 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800583 self._test.registry.register(self, self._test.logger)
Neale Ranns9db6ada2019-11-08 12:42:31 +0000584 return self
Neale Ranns32e1c012016-11-22 17:07:28 +0000585
586 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700587 self._test.vapi.ip_mroute_add_del(self.table_id,
588 self.prefix.encode(),
589 self.e_flags,
590 self.rpf_id,
591 self.encoded_paths,
592 is_add=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000593
594 def update_entry_flags(self, flags):
595 self.e_flags = flags
Neale Ranns097fa662018-05-01 05:17:55 -0700596 self._test.vapi.ip_mroute_add_del(self.table_id,
597 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000598 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700599 self.rpf_id,
600 [],
601 is_add=1)
Neale Ranns32e1c012016-11-22 17:07:28 +0000602
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800603 def update_rpf_id(self, rpf_id):
604 self.rpf_id = rpf_id
Neale Ranns097fa662018-05-01 05:17:55 -0700605 self._test.vapi.ip_mroute_add_del(self.table_id,
606 self.prefix.encode(),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800607 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700608 self.rpf_id,
609 [],
610 is_add=1)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800611
Neale Ranns32e1c012016-11-22 17:07:28 +0000612 def update_path_flags(self, itf, flags):
Neale Ranns097fa662018-05-01 05:17:55 -0700613 for p in range(len(self.paths)):
614 if self.paths[p].nh_itf == itf:
615 self.paths[p].nh_i_flags = flags
616 self.encoded_paths[p] = self.paths[p].encode()
617 break
618
619 self._test.vapi.ip_mroute_add_del(self.table_id,
620 self.prefix.encode(),
Neale Ranns32e1c012016-11-22 17:07:28 +0000621 self.e_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700622 self.rpf_id,
623 [self.encoded_paths[p]],
624 is_add=1,
625 is_multipath=0)
Neale Ranns32e1c012016-11-22 17:07:28 +0000626
Neale Ranns5a8123b2017-01-26 01:18:23 -0800627 def query_vpp_config(self):
Neale Ranns947ea622018-06-07 23:48:20 -0700628 return find_mroute(self._test,
Neale Ranns097fa662018-05-01 05:17:55 -0700629 self.prefix.gaddr,
630 self.prefix.saddr,
631 self.prefix.length,
632 self.table_id)
Neale Ranns32e1c012016-11-22 17:07:28 +0000633
Neale Ranns5a8123b2017-01-26 01:18:23 -0800634 def object_id(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700635 return ("%d:(%s,%s/%d)" % (self.table_id,
636 self.prefix.saddr,
637 self.prefix.gaddr,
638 self.prefix.length))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800639
Neale Ranns28c142e2018-09-07 09:37:07 -0700640 def get_stats(self):
641 c = self._test.statistics.get_counter("/net/mroute")
642 return c[0][self.stats_index]
643
Neale Ranns5a8123b2017-01-26 01:18:23 -0800644
645class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000646 def __init__(self, test, route, interface, packet):
647 self.route = route
648 self.interface = interface
649 self.packet = packet
650 self.test = test
651
652 def compare(self, signal):
653 self.test.assertEqual(self.interface, signal.sw_if_index)
654 self.test.assertEqual(self.route.table_id, signal.table_id)
Neale Ranns097fa662018-05-01 05:17:55 -0700655 self.test.assertEqual(self.route.prefix, signal.prefix)
Neale Ranns32e1c012016-11-22 17:07:28 +0000656
657
Neale Ranns5a8123b2017-01-26 01:18:23 -0800658class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000659 """
660 MPLS to IP Binding
661 """
662
Neale Ranns5a8123b2017-01-26 01:18:23 -0800663 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700664 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000665 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000666 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700667 self.dest_addr = dest_addr
Neale Ranns097fa662018-05-01 05:17:55 -0700668 self.ip_addr = ip_address(text_type(dest_addr))
Neale Rannsad422ed2016-11-02 14:20:04 +0000669 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800670 self.table_id = table_id
671 self.ip_table_id = ip_table_id
Neale Rannsefd7bc22019-11-11 08:32:34 +0000672 self.prefix = mk_network(dest_addr, dest_addr_len)
Neale Rannsad422ed2016-11-02 14:20:04 +0000673
674 def add_vpp_config(self):
675 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000676 self.prefix,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800677 table_id=self.table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700678 ip_table_id=self.ip_table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800679 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000680
681 def remove_vpp_config(self):
682 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000683 self.prefix,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700684 table_id=self.table_id,
685 ip_table_id=self.ip_table_id,
Neale Ranns097fa662018-05-01 05:17:55 -0700686 is_bind=0)
Neale Rannsad422ed2016-11-02 14:20:04 +0000687
Neale Ranns5a8123b2017-01-26 01:18:23 -0800688 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700689 dump = self._test.vapi.mpls_route_dump(self.table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800690 for e in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700691 if self.local_label == e.mr_route.mr_label \
692 and self.table_id == e.mr_route.mr_table_id:
Neale Ranns5a8123b2017-01-26 01:18:23 -0800693 return True
694 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000695
Neale Ranns5a8123b2017-01-26 01:18:23 -0800696 def object_id(self):
697 return ("%d:%s binds %d:%s/%d"
698 % (self.table_id,
699 self.local_label,
700 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700701 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800702 self.dest_addr_len))
703
704
Neale Ranns15002542017-09-10 04:39:11 -0700705class VppMplsTable(VppObject):
706
707 def __init__(self,
708 test,
709 table_id):
710 self._test = test
711 self.table_id = table_id
712
713 def add_vpp_config(self):
714 self._test.vapi.mpls_table_add_del(
715 self.table_id,
716 is_add=1)
717 self._test.registry.register(self, self._test.logger)
718
719 def remove_vpp_config(self):
720 self._test.vapi.mpls_table_add_del(
721 self.table_id,
722 is_add=0)
723
724 def query_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700725 dump = self._test.vapi.mpls_table_dump()
726 for d in dump:
727 if d.mt_table.mt_table_id == self.table_id:
728 return True
Neale Ranns15002542017-09-10 04:39:11 -0700729 return False
730
Neale Ranns15002542017-09-10 04:39:11 -0700731 def object_id(self):
732 return ("table-mpls-%d" % (self.table_id))
733
734
Neale Ranns5a8123b2017-01-26 01:18:23 -0800735class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000736 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800737 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000738 """
739
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800740 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
Neale Ranns097fa662018-05-01 05:17:55 -0700741 is_multicast=0,
742 eos_proto=FibPathProto.FIB_PATH_NH_PROTO_IP4):
Neale Rannsad422ed2016-11-02 14:20:04 +0000743 self._test = test
744 self.paths = paths
745 self.local_label = local_label
746 self.eos_bit = eos_bit
Neale Ranns097fa662018-05-01 05:17:55 -0700747 self.eos_proto = eos_proto
Neale Rannsad422ed2016-11-02 14:20:04 +0000748 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800749 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000750
751 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700752 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000753 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700754 paths.append(path.encode())
Neale Ranns31ed7442018-02-23 05:29:09 -0800755
Neale Ranns097fa662018-05-01 05:17:55 -0700756 r = self._test.vapi.mpls_route_add_del(self.table_id,
757 self.local_label,
758 self.eos_bit,
759 self.eos_proto,
760 self.is_multicast,
761 paths, 1, 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700762 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800763 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000764
765 def remove_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -0700766 paths = []
Neale Rannsad422ed2016-11-02 14:20:04 +0000767 for path in self.paths:
Neale Ranns097fa662018-05-01 05:17:55 -0700768 paths.append(path.encode())
769
770 self._test.vapi.mpls_route_add_del(self.table_id,
771 self.local_label,
772 self.eos_bit,
773 self.eos_proto,
774 self.is_multicast,
775 paths, 0, 0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800776
777 def query_vpp_config(self):
Neale Ranns775f73c2018-12-20 03:01:49 -0800778 return find_mpls_route(self._test, self.table_id,
779 self.local_label, self.eos_bit)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800780
Neale Ranns5a8123b2017-01-26 01:18:23 -0800781 def object_id(self):
Paul Vinciguerra941da4a2019-06-18 07:57:53 -0400782 return ("mpls-route-%d:%s/%d"
Neale Ranns5a8123b2017-01-26 01:18:23 -0800783 % (self.table_id,
784 self.local_label,
Ole Troan9a475372019-03-05 16:58:24 +0100785 20 + self.eos_bit))
Neale Ranns008dbe12018-09-07 09:32:36 -0700786
787 def get_stats_to(self):
788 c = self._test.statistics.get_counter("/net/route/to")
789 return c[0][self.stats_index]
790
791 def get_stats_via(self):
792 c = self._test.statistics.get_counter("/net/route/via")
793 return c[0][self.stats_index]