blob: 6e52481782f321a815d2c850004c7c9d7e5d5ba1 [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
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08009from vpp_ip import DpoProto, VppIpPrefix
Neale Ranns177bbdc2016-11-15 09:46:51 +000010
Neale Rannsad422ed2016-11-02 14:20:04 +000011# from vnet/vnet/mpls/mpls_types.h
12MPLS_IETF_MAX_LABEL = 0xfffff
13MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
Neale Ranns177bbdc2016-11-15 09:46:51 +000014
Neale Ranns177bbdc2016-11-15 09:46:51 +000015
Neale Ranns180279b2017-03-16 15:49:09 -040016class MRouteItfFlags:
17 MFIB_ITF_FLAG_NONE = 0
18 MFIB_ITF_FLAG_NEGATE_SIGNAL = 1
19 MFIB_ITF_FLAG_ACCEPT = 2
20 MFIB_ITF_FLAG_FORWARD = 4
21 MFIB_ITF_FLAG_SIGNAL_PRESENT = 8
22 MFIB_ITF_FLAG_INTERNAL_COPY = 16
23
24
25class MRouteEntryFlags:
26 MFIB_ENTRY_FLAG_NONE = 0
27 MFIB_ENTRY_FLAG_SIGNAL = 1
28 MFIB_ENTRY_FLAG_DROP = 2
29 MFIB_ENTRY_FLAG_CONNECTED = 4
30 MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8
31
32
Neale Ranns31ed7442018-02-23 05:29:09 -080033class MplsLspMode:
34 PIPE = 0
35 UNIFORM = 1
36
37
Neale Ranns93cc3ee2018-10-10 07:22:51 -070038def ip_to_dpo_proto(addr):
39 if addr.version is 6:
40 return DpoProto.DPO_PROTO_IP6
41 else:
42 return DpoProto.DPO_PROTO_IP4
43
44
Neale Rannsb3b2de72017-03-08 05:17:22 -080045def find_route(test, ip_addr, len, table_id=0, inet=AF_INET):
46 if inet == AF_INET:
47 s = 4
48 routes = test.vapi.ip_fib_dump()
49 else:
50 s = 16
51 routes = test.vapi.ip6_fib_dump()
52
53 route_addr = inet_pton(inet, ip_addr)
54 for e in routes:
55 if route_addr == e.address[:s] \
56 and len == e.address_length \
57 and table_id == e.table_id:
58 return True
59 return False
60
61
Neale Ranns947ea622018-06-07 23:48:20 -070062def find_mroute(test, grp_addr, src_addr, grp_addr_len,
63 table_id=0, inet=AF_INET):
64 if inet == AF_INET:
65 s = 4
66 routes = test.vapi.ip_mfib_dump()
67 else:
68 s = 16
69 routes = test.vapi.ip6_mfib_dump()
70 gaddr = inet_pton(inet, grp_addr)
71 saddr = inet_pton(inet, src_addr)
72 for e in routes:
73 if gaddr == e.grp_address[:s] \
74 and grp_addr_len == e.address_length \
75 and saddr == e.src_address[:s] \
76 and table_id == e.table_id:
77 return True
78 return False
79
80
Neale Ranns775f73c2018-12-20 03:01:49 -080081def find_mpls_route(test, table_id, label, eos_bit, paths=None):
82 dump = test.vapi.mpls_fib_dump()
83 for e in dump:
84 if label == e.label \
85 and eos_bit == e.eos_bit \
86 and table_id == e.table_id:
87 if not paths:
88 return True
89 else:
90 if (len(paths) != len(e.path)):
91 return False
92 for i in range(len(paths)):
93 if (paths[i] != e.path[i]):
94 return False
95 return True
96 return False
97
98
Neale Ranns93cc3ee2018-10-10 07:22:51 -070099def fib_interface_ip_prefix(test, address, length, sw_if_index):
100 vp = VppIpPrefix(address, length)
101 addrs = test.vapi.ip_address_dump(sw_if_index, is_ipv6=vp.is_ip6)
102
103 if vp.is_ip6:
104 n = 16
105 else:
106 n = 4
107
108 for a in addrs:
109 if a.prefix_length == length and \
110 a.sw_if_index == sw_if_index and \
111 a.ip[:n] == vp.bytes:
112 return True
113 return False
114
115
Neale Ranns15002542017-09-10 04:39:11 -0700116class VppIpTable(VppObject):
117
118 def __init__(self,
119 test,
120 table_id,
121 is_ip6=0):
122 self._test = test
123 self.table_id = table_id
124 self.is_ip6 = is_ip6
125
126 def add_vpp_config(self):
127 self._test.vapi.ip_table_add_del(
128 self.table_id,
129 is_ipv6=self.is_ip6,
130 is_add=1)
131 self._test.registry.register(self, self._test.logger)
132
133 def remove_vpp_config(self):
134 self._test.vapi.ip_table_add_del(
135 self.table_id,
136 is_ipv6=self.is_ip6,
137 is_add=0)
138
139 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700140 if self.table_id == 0:
141 # the default table always exists
142 return False
Neale Ranns15002542017-09-10 04:39:11 -0700143 # find the default route
144 return find_route(self._test,
145 "::" if self.is_ip6 else "0.0.0.0",
146 0,
147 self.table_id,
148 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
149
150 def __str__(self):
151 return self.object_id()
152
153 def object_id(self):
154 return ("table-%s-%d" %
155 ("v6" if self.is_ip6 == 1 else "v4",
156 self.table_id))
157
158
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700159class VppIpInterfaceAddress(VppObject):
160
161 def __init__(self, test, intf, addr, len):
162 self._test = test
163 self.intf = intf
164 self.prefix = VppIpPrefix(addr, len)
165
166 def add_vpp_config(self):
167 self._test.vapi.sw_interface_add_del_address(
168 self.intf.sw_if_index,
169 self.prefix.bytes,
170 self.prefix.length,
171 is_add=1,
172 is_ipv6=self.prefix.is_ip6)
173 self._test.registry.register(self, self._test.logger)
174
175 def remove_vpp_config(self):
176 self._test.vapi.sw_interface_add_del_address(
177 self.intf.sw_if_index,
178 self.prefix.bytes,
179 self.prefix.length,
180 is_add=0,
181 is_ipv6=self.prefix.is_ip6)
182
183 def query_vpp_config(self):
184 return fib_interface_ip_prefix(self._test,
185 self.prefix.address,
186 self.prefix.length,
187 self.intf.sw_if_index)
188
189 def __str__(self):
190 return self.object_id()
191
192 def object_id(self):
193 return "interface-ip-%s-%s" % (self.intf, self.prefix)
194
195
196class VppIpInterfaceBind(VppObject):
197
198 def __init__(self, test, intf, table):
199 self._test = test
200 self.intf = intf
201 self.table = table
202
203 def add_vpp_config(self):
204 if self.table.is_ip6:
205 self.intf.set_table_ip6(self.table.table_id)
206 else:
207 self.intf.set_table_ip4(self.table.table_id)
208 self._test.registry.register(self, self._test.logger)
209
210 def remove_vpp_config(self):
211 if 0 == self.table.table_id:
212 return
213 if self.table.is_ip6:
214 self.intf.set_table_ip6(0)
215 else:
216 self.intf.set_table_ip4(0)
217
218 def query_vpp_config(self):
219 if 0 == self.table.table_id:
220 return False
221 return self._test.vapi.sw_interface_get_table(
222 self.intf.sw_if_index,
223 self.table.is_ip6).vrf_id == self.table.table_id
224
225 def __str__(self):
226 return self.object_id()
227
228 def object_id(self):
229 return "interface-bind-%s-%s" % (self.intf, self.table)
230
231
Neale Ranns31ed7442018-02-23 05:29:09 -0800232class VppMplsLabel(object):
233 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
234 self.value = value
235 self.mode = mode
236 self.ttl = ttl
237 self.exp = exp
238
239 def encode(self):
240 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
241 return {'label': self.value,
242 'ttl': self.ttl,
243 'exp': self.exp,
244 'is_uniform': is_uniform}
245
Neale Ranns775f73c2018-12-20 03:01:49 -0800246 def __eq__(self, other):
247 if isinstance(other, self.__class__):
248 return (self.value == other.value and
249 self.ttl == other.ttl and
250 self.exp == other.exp and
251 self.mode == other.mode)
252 elif hasattr(other, 'label'):
253 return (self.value == other.label and
254 self.ttl == other.ttl and
255 self.exp == other.exp and
256 (self.mode == MplsLspMode.UNIFORM) == other.is_uniform)
257 else:
258 return False
259
260 def __ne__(self, other):
261 return not (self == other)
262
Neale Ranns31ed7442018-02-23 05:29:09 -0800263
Neale Ranns5a8123b2017-01-26 01:18:23 -0800264class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000265
Klement Sekerada505f62017-01-04 12:58:53 +0100266 def __init__(
267 self,
268 nh_addr,
269 nh_sw_if_index,
270 nh_table_id=0,
271 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800272 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800273 rpf_id=0,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700274 is_interface_rx=0,
275 is_resolve_host=0,
Neale Rannsda78f952017-05-24 09:15:43 -0700276 is_resolve_attached=0,
Neale Ranns054c03a2017-10-13 05:15:07 -0700277 is_source_lookup=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800278 is_udp_encap=0,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800279 is_dvr=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800280 next_hop_id=0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -0700281 proto=DpoProto.DPO_PROTO_IP4):
Neale Ranns311124e2019-01-24 04:52:25 -0800282 self.proto = proto
Neale Ranns177bbdc2016-11-15 09:46:51 +0000283 self.nh_itf = nh_sw_if_index
284 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000285 self.nh_via_label = nh_via_label
286 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800287 self.weight = 1
288 self.rpf_id = rpf_id
Neale Rannsda78f952017-05-24 09:15:43 -0700289 if self.proto is DpoProto.DPO_PROTO_IP6:
290 self.nh_addr = inet_pton(AF_INET6, nh_addr)
291 elif self.proto is DpoProto.DPO_PROTO_IP4:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800292 self.nh_addr = inet_pton(AF_INET, nh_addr)
Neale Ranns31426c62017-05-24 10:32:58 -0700293 else:
Neale Rannsda78f952017-05-24 09:15:43 -0700294 self.nh_addr = inet_pton(AF_INET6, "::")
Neale Rannsf12a83f2017-04-18 09:09:40 -0700295 self.is_resolve_host = is_resolve_host
296 self.is_resolve_attached = is_resolve_attached
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800297 self.is_interface_rx = is_interface_rx
Neale Ranns054c03a2017-10-13 05:15:07 -0700298 self.is_source_lookup = is_source_lookup
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800299 self.is_rpf_id = 0
300 if rpf_id != 0:
301 self.is_rpf_id = 1
302 self.nh_itf = rpf_id
Neale Ranns810086d2017-11-05 16:26:46 -0800303 self.is_udp_encap = is_udp_encap
304 self.next_hop_id = next_hop_id
Neale Rannsf068c3e2018-01-03 04:18:48 -0800305 self.is_dvr = is_dvr
Neale Ranns177bbdc2016-11-15 09:46:51 +0000306
Neale Ranns31ed7442018-02-23 05:29:09 -0800307 def encode_labels(self):
308 lstack = []
309 for l in self.nh_labels:
310 if type(l) == VppMplsLabel:
311 lstack.append(l.encode())
312 else:
313 lstack.append({'label': l,
314 'ttl': 255})
315 return lstack
316
Neale Ranns2303cb12018-02-21 04:57:17 -0800317 def encode(self):
318 return {'next_hop': self.nh_addr,
319 'weight': 1,
Neale Ranns2303cb12018-02-21 04:57:17 -0800320 'preference': 0,
321 'table_id': self.nh_table_id,
322 'next_hop_id': self.next_hop_id,
323 'sw_if_index': self.nh_itf,
324 'afi': self.proto,
325 'is_udp_encap': self.is_udp_encap,
326 'n_labels': len(self.nh_labels),
327 'label_stack': self.encode_labels()}
328
Neale Rannsef90ed02018-09-13 08:45:12 -0700329 def __eq__(self, other):
Neale Ranns775f73c2018-12-20 03:01:49 -0800330 if isinstance(other, self.__class__):
331 return self.nh_addr == other.nh_addr
332 elif hasattr(other, 'sw_if_index'):
333 # vl_api_fib_path_t
334 if (len(self.nh_labels) != other.n_labels):
335 return False
336 for i in range(len(self.nh_labels)):
337 if (self.nh_labels[i] != other.label_stack[i]):
338 return False
339 return self.nh_itf == other.sw_if_index
340 else:
341 return False
342
343 def __ne__(self, other):
344 return not (self == other)
Neale Rannsef90ed02018-09-13 08:45:12 -0700345
Neale Ranns177bbdc2016-11-15 09:46:51 +0000346
Neale Ranns5a8123b2017-01-26 01:18:23 -0800347class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000348
Neale Rannsd792d9c2017-10-21 10:53:20 -0700349 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700350 nh=None,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700351 proto=DpoProto.DPO_PROTO_IP4,
352 bier_imp=0):
Neale Rannse821ab12017-06-01 07:45:05 -0700353 if not nh:
354 nh = "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0"
355 super(VppMRoutePath, self).__init__(nh,
356 nh_sw_if_index,
357 proto=proto)
Neale Ranns32e1c012016-11-22 17:07:28 +0000358 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700359 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000360
361
Neale Ranns5a8123b2017-01-26 01:18:23 -0800362class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000363 """
364 IP Route
365 """
366
367 def __init__(self, test, dest_addr,
Neale Ranns37be7362017-02-21 17:30:26 -0800368 dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0,
Neale Ranns3b93be52018-09-07 01:48:54 -0700369 is_unreach=0, is_prohibit=0, is_drop=0):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000370 self._test = test
371 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000372 self.dest_addr_len = dest_addr_len
373 self.table_id = table_id
Neale Rannsfca0c242017-01-13 07:57:46 -0800374 self.is_ip6 = is_ip6
375 self.is_local = is_local
Neale Ranns37be7362017-02-21 17:30:26 -0800376 self.is_unreach = is_unreach
377 self.is_prohibit = is_prohibit
Neale Ranns3b93be52018-09-07 01:48:54 -0700378 self.is_drop = is_drop
Neale Rannsb3b2de72017-03-08 05:17:22 -0800379 self.dest_addr_p = dest_addr
Neale Rannsfca0c242017-01-13 07:57:46 -0800380 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800381 self.dest_addr = inet_pton(AF_INET6, dest_addr)
Neale Rannsfca0c242017-01-13 07:57:46 -0800382 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800383 self.dest_addr = inet_pton(AF_INET, dest_addr)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000384
Neale Ranns69b7aa42017-03-10 03:04:12 -0800385 def modify(self, paths, is_local=0,
386 is_unreach=0, is_prohibit=0):
387 self.paths = paths
388 self.is_local = is_local
389 self.is_unreach = is_unreach
390 self.is_prohibit = is_prohibit
391
Neale Ranns177bbdc2016-11-15 09:46:51 +0000392 def add_vpp_config(self):
Neale Ranns7425f922019-01-23 00:36:16 -0800393 if self.is_unreach or self.is_prohibit or self.is_drop:
Neale Ranns008dbe12018-09-07 09:32:36 -0700394 r = self._test.vapi.ip_add_del_route(
Klement Sekerada505f62017-01-04 12:58:53 +0100395 self.dest_addr,
396 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800397 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800398 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800399 is_local=self.is_local,
400 is_unreach=self.is_unreach,
401 is_prohibit=self.is_prohibit,
Neale Ranns3b93be52018-09-07 01:48:54 -0700402 is_drop=self.is_drop,
Klement Sekerada505f62017-01-04 12:58:53 +0100403 table_id=self.table_id,
Neale Rannsfca0c242017-01-13 07:57:46 -0800404 is_ipv6=self.is_ip6)
405 else:
406 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800407 lstack = path.encode_labels()
408
Neale Ranns008dbe12018-09-07 09:32:36 -0700409 r = self._test.vapi.ip_add_del_route(
Neale Rannsfca0c242017-01-13 07:57:46 -0800410 self.dest_addr,
411 self.dest_addr_len,
412 path.nh_addr,
413 path.nh_itf,
414 table_id=self.table_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800415 next_hop_out_label_stack=lstack,
416 next_hop_n_out_labels=len(lstack),
Neale Rannsfca0c242017-01-13 07:57:46 -0800417 next_hop_via_label=path.nh_via_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700418 next_hop_table_id=path.nh_table_id,
Neale Ranns810086d2017-11-05 16:26:46 -0800419 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700420 is_ipv6=self.is_ip6,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800421 is_dvr=path.is_dvr,
Neale Ranns7425f922019-01-23 00:36:16 -0800422 is_local=self.is_local,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700423 is_resolve_host=path.is_resolve_host,
424 is_resolve_attached=path.is_resolve_attached,
Neale Ranns054c03a2017-10-13 05:15:07 -0700425 is_source_lookup=path.is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800426 is_udp_encap=path.is_udp_encap,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700427 is_multipath=1 if len(self.paths) > 1 else 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700428 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800429 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000430
431 def remove_vpp_config(self):
Neale Ranns7425f922019-01-23 00:36:16 -0800432 if self.is_unreach or self.is_prohibit or self.is_drop:
Neale Rannsfca0c242017-01-13 07:57:46 -0800433 self._test.vapi.ip_add_del_route(
434 self.dest_addr,
435 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800436 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800437 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800438 is_local=self.is_local,
439 is_unreach=self.is_unreach,
440 is_prohibit=self.is_prohibit,
Neale Rannsfca0c242017-01-13 07:57:46 -0800441 is_add=0,
442 table_id=self.table_id,
443 is_ipv6=self.is_ip6)
444 else:
445 for path in self.paths:
Neale Rannsf12a83f2017-04-18 09:09:40 -0700446 self._test.vapi.ip_add_del_route(
447 self.dest_addr,
448 self.dest_addr_len,
449 path.nh_addr,
450 path.nh_itf,
451 table_id=self.table_id,
452 next_hop_table_id=path.nh_table_id,
453 next_hop_via_label=path.nh_via_label,
Neale Ranns810086d2017-11-05 16:26:46 -0800454 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700455 is_add=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800456 is_udp_encap=path.is_udp_encap,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800457 is_ipv6=self.is_ip6,
458 is_dvr=path.is_dvr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000459
Neale Ranns5a8123b2017-01-26 01:18:23 -0800460 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800461 return find_route(self._test,
462 self.dest_addr_p,
463 self.dest_addr_len,
464 self.table_id,
465 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Rannsad422ed2016-11-02 14:20:04 +0000466
Neale Ranns5a8123b2017-01-26 01:18:23 -0800467 def __str__(self):
468 return self.object_id()
469
470 def object_id(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800471 return ("%d:%s/%d"
472 % (self.table_id,
473 self.dest_addr_p,
474 self.dest_addr_len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800475
Neale Ranns008dbe12018-09-07 09:32:36 -0700476 def get_stats_to(self):
477 c = self._test.statistics.get_counter("/net/route/to")
478 return c[0][self.stats_index]
479
480 def get_stats_via(self):
481 c = self._test.statistics.get_counter("/net/route/via")
482 return c[0][self.stats_index]
483
Neale Ranns5a8123b2017-01-26 01:18:23 -0800484
485class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000486 """
487 IP Multicast Route
488 """
489
490 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800491 grp_addr_len, e_flags, paths, table_id=0,
492 rpf_id=0, is_ip6=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000493 self._test = test
494 self.paths = paths
495 self.grp_addr_len = grp_addr_len
496 self.table_id = table_id
497 self.e_flags = e_flags
498 self.is_ip6 = is_ip6
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800499 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000500
Neale Ranns947ea622018-06-07 23:48:20 -0700501 self.grp_addr_p = grp_addr
502 self.src_addr_p = src_addr
Neale Ranns32e1c012016-11-22 17:07:28 +0000503 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800504 self.grp_addr = inet_pton(AF_INET6, grp_addr)
505 self.src_addr = inet_pton(AF_INET6, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000506 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800507 self.grp_addr = inet_pton(AF_INET, grp_addr)
508 self.src_addr = inet_pton(AF_INET, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000509
510 def add_vpp_config(self):
511 for path in self.paths:
Neale Ranns28c142e2018-09-07 09:37:07 -0700512 r = self._test.vapi.ip_mroute_add_del(self.src_addr,
513 self.grp_addr,
514 self.grp_addr_len,
515 self.e_flags,
516 path.proto,
517 path.nh_itf,
518 path.nh_addr,
519 path.nh_i_flags,
520 bier_imp=path.bier_imp,
521 rpf_id=self.rpf_id,
522 table_id=self.table_id,
523 is_ipv6=self.is_ip6)
524 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800525 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000526
527 def remove_vpp_config(self):
528 for path in self.paths:
529 self._test.vapi.ip_mroute_add_del(self.src_addr,
530 self.grp_addr,
531 self.grp_addr_len,
532 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700533 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000534 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700535 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000536 path.nh_i_flags,
537 table_id=self.table_id,
Neale Rannseea537a2018-01-09 04:11:28 -0800538 bier_imp=path.bier_imp,
Neale Ranns32e1c012016-11-22 17:07:28 +0000539 is_add=0,
540 is_ipv6=self.is_ip6)
541
542 def update_entry_flags(self, flags):
543 self.e_flags = flags
544 self._test.vapi.ip_mroute_add_del(self.src_addr,
545 self.grp_addr,
546 self.grp_addr_len,
547 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700548 0,
Neale Ranns32e1c012016-11-22 17:07:28 +0000549 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700550 "",
Neale Ranns32e1c012016-11-22 17:07:28 +0000551 0,
552 table_id=self.table_id,
553 is_ipv6=self.is_ip6)
554
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800555 def update_rpf_id(self, rpf_id):
556 self.rpf_id = rpf_id
557 self._test.vapi.ip_mroute_add_del(self.src_addr,
558 self.grp_addr,
559 self.grp_addr_len,
560 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700561 0,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800562 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700563 "",
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800564 0,
565 rpf_id=self.rpf_id,
566 table_id=self.table_id,
567 is_ipv6=self.is_ip6)
568
Neale Ranns32e1c012016-11-22 17:07:28 +0000569 def update_path_flags(self, itf, flags):
570 for path in self.paths:
571 if path.nh_itf == itf:
572 path.nh_i_flags = flags
573 break
574 self._test.vapi.ip_mroute_add_del(self.src_addr,
575 self.grp_addr,
576 self.grp_addr_len,
577 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700578 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000579 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700580 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000581 path.nh_i_flags,
582 table_id=self.table_id,
583 is_ipv6=self.is_ip6)
584
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,
587 self.grp_addr_p,
588 self.src_addr_p,
589 self.grp_addr_len,
590 self.table_id,
591 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Ranns32e1c012016-11-22 17:07:28 +0000592
Neale Ranns5a8123b2017-01-26 01:18:23 -0800593 def __str__(self):
594 return self.object_id()
595
596 def object_id(self):
597 if self.is_ip6:
598 return ("%d:(%s,%s/%d)"
599 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800600 inet_ntop(AF_INET6, self.src_addr),
601 inet_ntop(AF_INET6, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800602 self.grp_addr_len))
603 else:
604 return ("%d:(%s,%s/%d)"
605 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800606 inet_ntop(AF_INET, self.src_addr),
607 inet_ntop(AF_INET, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800608 self.grp_addr_len))
609
Neale Ranns28c142e2018-09-07 09:37:07 -0700610 def get_stats(self):
611 c = self._test.statistics.get_counter("/net/mroute")
612 return c[0][self.stats_index]
613
Neale Ranns5a8123b2017-01-26 01:18:23 -0800614
615class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000616 def __init__(self, test, route, interface, packet):
617 self.route = route
618 self.interface = interface
619 self.packet = packet
620 self.test = test
621
622 def compare(self, signal):
623 self.test.assertEqual(self.interface, signal.sw_if_index)
624 self.test.assertEqual(self.route.table_id, signal.table_id)
625 self.test.assertEqual(self.route.grp_addr_len,
626 signal.grp_address_len)
627 for i in range(self.route.grp_addr_len / 8):
628 self.test.assertEqual(self.route.grp_addr[i],
629 signal.grp_address[i])
630 if (self.route.grp_addr_len > 32):
631 for i in range(4):
632 self.test.assertEqual(self.route.src_addr[i],
633 signal.src_address[i])
634
635
Neale Ranns5a8123b2017-01-26 01:18:23 -0800636class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000637 """
638 MPLS to IP Binding
639 """
640
Neale Ranns5a8123b2017-01-26 01:18:23 -0800641 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700642 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000643 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000644 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700645 self.dest_addr = dest_addr
Neale Rannsad422ed2016-11-02 14:20:04 +0000646 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800647 self.table_id = table_id
648 self.ip_table_id = ip_table_id
Neale Rannsf12a83f2017-04-18 09:09:40 -0700649 self.is_ip6 = is_ip6
650 if is_ip6:
651 self.dest_addrn = inet_pton(AF_INET6, dest_addr)
652 else:
653 self.dest_addrn = inet_pton(AF_INET, dest_addr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000654
655 def add_vpp_config(self):
656 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700657 self.dest_addrn,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800658 self.dest_addr_len,
659 table_id=self.table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700660 ip_table_id=self.ip_table_id,
661 is_ip4=(self.is_ip6 == 0))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800662 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000663
664 def remove_vpp_config(self):
665 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700666 self.dest_addrn,
Neale Rannsad422ed2016-11-02 14:20:04 +0000667 self.dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700668 table_id=self.table_id,
669 ip_table_id=self.ip_table_id,
670 is_bind=0,
671 is_ip4=(self.is_ip6 == 0))
Neale Rannsad422ed2016-11-02 14:20:04 +0000672
Neale Ranns5a8123b2017-01-26 01:18:23 -0800673 def query_vpp_config(self):
674 dump = self._test.vapi.mpls_fib_dump()
675 for e in dump:
676 if self.local_label == e.label \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800677 and self.table_id == e.table_id:
678 return True
679 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000680
Neale Ranns5a8123b2017-01-26 01:18:23 -0800681 def __str__(self):
682 return self.object_id()
683
684 def object_id(self):
685 return ("%d:%s binds %d:%s/%d"
686 % (self.table_id,
687 self.local_label,
688 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700689 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800690 self.dest_addr_len))
691
692
Neale Ranns15002542017-09-10 04:39:11 -0700693class VppMplsTable(VppObject):
694
695 def __init__(self,
696 test,
697 table_id):
698 self._test = test
699 self.table_id = table_id
700
701 def add_vpp_config(self):
702 self._test.vapi.mpls_table_add_del(
703 self.table_id,
704 is_add=1)
705 self._test.registry.register(self, self._test.logger)
706
707 def remove_vpp_config(self):
708 self._test.vapi.mpls_table_add_del(
709 self.table_id,
710 is_add=0)
711
712 def query_vpp_config(self):
713 # find the default route
714 dump = self._test.vapi.mpls_fib_dump()
715 if len(dump):
716 return True
717 return False
718
719 def __str__(self):
720 return self.object_id()
721
722 def object_id(self):
723 return ("table-mpls-%d" % (self.table_id))
724
725
Neale Ranns5a8123b2017-01-26 01:18:23 -0800726class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000727 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800728 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000729 """
730
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800731 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
732 is_multicast=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000733 self._test = test
734 self.paths = paths
735 self.local_label = local_label
736 self.eos_bit = eos_bit
737 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800738 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000739
740 def add_vpp_config(self):
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800741 is_multipath = len(self.paths) > 1
Neale Rannsad422ed2016-11-02 14:20:04 +0000742 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800743 lstack = path.encode_labels()
744
Neale Ranns008dbe12018-09-07 09:32:36 -0700745 r = self._test.vapi.mpls_route_add_del(
Klement Sekerada505f62017-01-04 12:58:53 +0100746 self.local_label,
747 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700748 path.proto,
Klement Sekerada505f62017-01-04 12:58:53 +0100749 path.nh_addr,
750 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800751 is_multicast=self.is_multicast,
752 is_multipath=is_multipath,
Klement Sekerada505f62017-01-04 12:58:53 +0100753 table_id=self.table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800754 is_interface_rx=path.is_interface_rx,
755 is_rpf_id=path.is_rpf_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800756 next_hop_out_label_stack=lstack,
757 next_hop_n_out_labels=len(lstack),
Klement Sekerada505f62017-01-04 12:58:53 +0100758 next_hop_via_label=path.nh_via_label,
759 next_hop_table_id=path.nh_table_id)
Neale Ranns008dbe12018-09-07 09:32:36 -0700760 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800761 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000762
763 def remove_vpp_config(self):
764 for path in self.paths:
765 self._test.vapi.mpls_route_add_del(self.local_label,
766 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700767 path.proto,
Neale Rannsad422ed2016-11-02 14:20:04 +0000768 path.nh_addr,
769 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800770 is_rpf_id=path.is_rpf_id,
Neale Rannsad422ed2016-11-02 14:20:04 +0000771 table_id=self.table_id,
772 is_add=0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800773
774 def query_vpp_config(self):
Neale Ranns775f73c2018-12-20 03:01:49 -0800775 return find_mpls_route(self._test, self.table_id,
776 self.local_label, self.eos_bit)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800777
778 def __str__(self):
779 return self.object_id()
780
781 def object_id(self):
782 return ("%d:%s/%d"
783 % (self.table_id,
784 self.local_label,
785 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]