blob: 45609e80786640ad791d1cc54721efec578241b1 [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
Neale Ranns5a8123b2017-01-26 01:18:23 -08007from vpp_object import *
Neale Rannsb3b2de72017-03-08 05:17:22 -08008from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
Neale Rannsc0a93142018-09-05 15:42:26 -07009from vpp_ip import *
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 Ranns93cc3ee2018-10-10 07:22:51 -070081def fib_interface_ip_prefix(test, address, length, sw_if_index):
82 vp = VppIpPrefix(address, length)
83 addrs = test.vapi.ip_address_dump(sw_if_index, is_ipv6=vp.is_ip6)
84
85 if vp.is_ip6:
86 n = 16
87 else:
88 n = 4
89
90 for a in addrs:
91 if a.prefix_length == length and \
92 a.sw_if_index == sw_if_index and \
93 a.ip[:n] == vp.bytes:
94 return True
95 return False
96
97
Neale Ranns15002542017-09-10 04:39:11 -070098class VppIpTable(VppObject):
99
100 def __init__(self,
101 test,
102 table_id,
103 is_ip6=0):
104 self._test = test
105 self.table_id = table_id
106 self.is_ip6 = is_ip6
107
108 def add_vpp_config(self):
109 self._test.vapi.ip_table_add_del(
110 self.table_id,
111 is_ipv6=self.is_ip6,
112 is_add=1)
113 self._test.registry.register(self, self._test.logger)
114
115 def remove_vpp_config(self):
116 self._test.vapi.ip_table_add_del(
117 self.table_id,
118 is_ipv6=self.is_ip6,
119 is_add=0)
120
121 def query_vpp_config(self):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700122 if self.table_id == 0:
123 # the default table always exists
124 return False
Neale Ranns15002542017-09-10 04:39:11 -0700125 # find the default route
126 return find_route(self._test,
127 "::" if self.is_ip6 else "0.0.0.0",
128 0,
129 self.table_id,
130 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
131
132 def __str__(self):
133 return self.object_id()
134
135 def object_id(self):
136 return ("table-%s-%d" %
137 ("v6" if self.is_ip6 == 1 else "v4",
138 self.table_id))
139
140
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700141class VppIpInterfaceAddress(VppObject):
142
143 def __init__(self, test, intf, addr, len):
144 self._test = test
145 self.intf = intf
146 self.prefix = VppIpPrefix(addr, len)
147
148 def add_vpp_config(self):
149 self._test.vapi.sw_interface_add_del_address(
150 self.intf.sw_if_index,
151 self.prefix.bytes,
152 self.prefix.length,
153 is_add=1,
154 is_ipv6=self.prefix.is_ip6)
155 self._test.registry.register(self, self._test.logger)
156
157 def remove_vpp_config(self):
158 self._test.vapi.sw_interface_add_del_address(
159 self.intf.sw_if_index,
160 self.prefix.bytes,
161 self.prefix.length,
162 is_add=0,
163 is_ipv6=self.prefix.is_ip6)
164
165 def query_vpp_config(self):
166 return fib_interface_ip_prefix(self._test,
167 self.prefix.address,
168 self.prefix.length,
169 self.intf.sw_if_index)
170
171 def __str__(self):
172 return self.object_id()
173
174 def object_id(self):
175 return "interface-ip-%s-%s" % (self.intf, self.prefix)
176
177
178class VppIpInterfaceBind(VppObject):
179
180 def __init__(self, test, intf, table):
181 self._test = test
182 self.intf = intf
183 self.table = table
184
185 def add_vpp_config(self):
186 if self.table.is_ip6:
187 self.intf.set_table_ip6(self.table.table_id)
188 else:
189 self.intf.set_table_ip4(self.table.table_id)
190 self._test.registry.register(self, self._test.logger)
191
192 def remove_vpp_config(self):
193 if 0 == self.table.table_id:
194 return
195 if self.table.is_ip6:
196 self.intf.set_table_ip6(0)
197 else:
198 self.intf.set_table_ip4(0)
199
200 def query_vpp_config(self):
201 if 0 == self.table.table_id:
202 return False
203 return self._test.vapi.sw_interface_get_table(
204 self.intf.sw_if_index,
205 self.table.is_ip6).vrf_id == self.table.table_id
206
207 def __str__(self):
208 return self.object_id()
209
210 def object_id(self):
211 return "interface-bind-%s-%s" % (self.intf, self.table)
212
213
Neale Ranns31ed7442018-02-23 05:29:09 -0800214class VppMplsLabel(object):
215 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
216 self.value = value
217 self.mode = mode
218 self.ttl = ttl
219 self.exp = exp
220
221 def encode(self):
222 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
223 return {'label': self.value,
224 'ttl': self.ttl,
225 'exp': self.exp,
226 'is_uniform': is_uniform}
227
228
Neale Ranns5a8123b2017-01-26 01:18:23 -0800229class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000230
Klement Sekerada505f62017-01-04 12:58:53 +0100231 def __init__(
232 self,
233 nh_addr,
234 nh_sw_if_index,
235 nh_table_id=0,
236 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800237 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800238 rpf_id=0,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700239 is_interface_rx=0,
240 is_resolve_host=0,
Neale Rannsda78f952017-05-24 09:15:43 -0700241 is_resolve_attached=0,
Neale Ranns054c03a2017-10-13 05:15:07 -0700242 is_source_lookup=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800243 is_udp_encap=0,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800244 is_dvr=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800245 next_hop_id=0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -0700246 proto=DpoProto.DPO_PROTO_IP4):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000247 self.nh_itf = nh_sw_if_index
248 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000249 self.nh_via_label = nh_via_label
250 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800251 self.weight = 1
252 self.rpf_id = rpf_id
Neale Rannsda78f952017-05-24 09:15:43 -0700253 self.proto = proto
254 if self.proto is DpoProto.DPO_PROTO_IP6:
255 self.nh_addr = inet_pton(AF_INET6, nh_addr)
256 elif self.proto is DpoProto.DPO_PROTO_IP4:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800257 self.nh_addr = inet_pton(AF_INET, nh_addr)
Neale Ranns31426c62017-05-24 10:32:58 -0700258 else:
Neale Rannsda78f952017-05-24 09:15:43 -0700259 self.nh_addr = inet_pton(AF_INET6, "::")
Neale Rannsf12a83f2017-04-18 09:09:40 -0700260 self.is_resolve_host = is_resolve_host
261 self.is_resolve_attached = is_resolve_attached
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800262 self.is_interface_rx = is_interface_rx
Neale Ranns054c03a2017-10-13 05:15:07 -0700263 self.is_source_lookup = is_source_lookup
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800264 self.is_rpf_id = 0
265 if rpf_id != 0:
266 self.is_rpf_id = 1
267 self.nh_itf = rpf_id
Neale Ranns810086d2017-11-05 16:26:46 -0800268 self.is_udp_encap = is_udp_encap
269 self.next_hop_id = next_hop_id
Neale Rannsf068c3e2018-01-03 04:18:48 -0800270 self.is_dvr = is_dvr
Neale Ranns177bbdc2016-11-15 09:46:51 +0000271
Neale Ranns31ed7442018-02-23 05:29:09 -0800272 def encode_labels(self):
273 lstack = []
274 for l in self.nh_labels:
275 if type(l) == VppMplsLabel:
276 lstack.append(l.encode())
277 else:
278 lstack.append({'label': l,
279 'ttl': 255})
280 return lstack
281
Neale Ranns2303cb12018-02-21 04:57:17 -0800282 def encode(self):
283 return {'next_hop': self.nh_addr,
284 'weight': 1,
285 'afi': 0,
286 'preference': 0,
287 'table_id': self.nh_table_id,
288 'next_hop_id': self.next_hop_id,
289 'sw_if_index': self.nh_itf,
290 'afi': self.proto,
291 'is_udp_encap': self.is_udp_encap,
292 'n_labels': len(self.nh_labels),
293 'label_stack': self.encode_labels()}
294
Neale Rannsef90ed02018-09-13 08:45:12 -0700295 def __eq__(self, other):
296 return self.nh_addr == other.nh_addr
297
Neale Ranns177bbdc2016-11-15 09:46:51 +0000298
Neale Ranns5a8123b2017-01-26 01:18:23 -0800299class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000300
Neale Rannsd792d9c2017-10-21 10:53:20 -0700301 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700302 nh=None,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700303 proto=DpoProto.DPO_PROTO_IP4,
304 bier_imp=0):
Neale Rannse821ab12017-06-01 07:45:05 -0700305 if not nh:
306 nh = "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0"
307 super(VppMRoutePath, self).__init__(nh,
308 nh_sw_if_index,
309 proto=proto)
Neale Ranns32e1c012016-11-22 17:07:28 +0000310 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700311 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000312
313
Neale Ranns5a8123b2017-01-26 01:18:23 -0800314class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000315 """
316 IP Route
317 """
318
319 def __init__(self, test, dest_addr,
Neale Ranns37be7362017-02-21 17:30:26 -0800320 dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0,
Neale Ranns3b93be52018-09-07 01:48:54 -0700321 is_unreach=0, is_prohibit=0, is_drop=0):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000322 self._test = test
323 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000324 self.dest_addr_len = dest_addr_len
325 self.table_id = table_id
Neale Rannsfca0c242017-01-13 07:57:46 -0800326 self.is_ip6 = is_ip6
327 self.is_local = is_local
Neale Ranns37be7362017-02-21 17:30:26 -0800328 self.is_unreach = is_unreach
329 self.is_prohibit = is_prohibit
Neale Ranns3b93be52018-09-07 01:48:54 -0700330 self.is_drop = is_drop
Neale Rannsb3b2de72017-03-08 05:17:22 -0800331 self.dest_addr_p = dest_addr
Neale Rannsfca0c242017-01-13 07:57:46 -0800332 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800333 self.dest_addr = inet_pton(AF_INET6, dest_addr)
Neale Rannsfca0c242017-01-13 07:57:46 -0800334 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800335 self.dest_addr = inet_pton(AF_INET, dest_addr)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000336
Neale Ranns69b7aa42017-03-10 03:04:12 -0800337 def modify(self, paths, is_local=0,
338 is_unreach=0, is_prohibit=0):
339 self.paths = paths
340 self.is_local = is_local
341 self.is_unreach = is_unreach
342 self.is_prohibit = is_prohibit
343
Neale Ranns177bbdc2016-11-15 09:46:51 +0000344 def add_vpp_config(self):
Neale Ranns3b93be52018-09-07 01:48:54 -0700345 if self.is_local or self.is_unreach or \
346 self.is_prohibit or self.is_drop:
Neale Ranns008dbe12018-09-07 09:32:36 -0700347 r = self._test.vapi.ip_add_del_route(
Klement Sekerada505f62017-01-04 12:58:53 +0100348 self.dest_addr,
349 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800350 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800351 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800352 is_local=self.is_local,
353 is_unreach=self.is_unreach,
354 is_prohibit=self.is_prohibit,
Neale Ranns3b93be52018-09-07 01:48:54 -0700355 is_drop=self.is_drop,
Klement Sekerada505f62017-01-04 12:58:53 +0100356 table_id=self.table_id,
Neale Rannsfca0c242017-01-13 07:57:46 -0800357 is_ipv6=self.is_ip6)
358 else:
359 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800360 lstack = path.encode_labels()
361
Neale Ranns008dbe12018-09-07 09:32:36 -0700362 r = self._test.vapi.ip_add_del_route(
Neale Rannsfca0c242017-01-13 07:57:46 -0800363 self.dest_addr,
364 self.dest_addr_len,
365 path.nh_addr,
366 path.nh_itf,
367 table_id=self.table_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800368 next_hop_out_label_stack=lstack,
369 next_hop_n_out_labels=len(lstack),
Neale Rannsfca0c242017-01-13 07:57:46 -0800370 next_hop_via_label=path.nh_via_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700371 next_hop_table_id=path.nh_table_id,
Neale Ranns810086d2017-11-05 16:26:46 -0800372 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700373 is_ipv6=self.is_ip6,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800374 is_dvr=path.is_dvr,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700375 is_resolve_host=path.is_resolve_host,
376 is_resolve_attached=path.is_resolve_attached,
Neale Ranns054c03a2017-10-13 05:15:07 -0700377 is_source_lookup=path.is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800378 is_udp_encap=path.is_udp_encap,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700379 is_multipath=1 if len(self.paths) > 1 else 0)
Neale Ranns008dbe12018-09-07 09:32:36 -0700380 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800381 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000382
383 def remove_vpp_config(self):
Neale Ranns3b93be52018-09-07 01:48:54 -0700384 if self.is_local or self.is_unreach or \
385 self.is_prohibit or self.is_drop:
Neale Rannsfca0c242017-01-13 07:57:46 -0800386 self._test.vapi.ip_add_del_route(
387 self.dest_addr,
388 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800389 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800390 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800391 is_local=self.is_local,
392 is_unreach=self.is_unreach,
393 is_prohibit=self.is_prohibit,
Neale Rannsfca0c242017-01-13 07:57:46 -0800394 is_add=0,
395 table_id=self.table_id,
396 is_ipv6=self.is_ip6)
397 else:
398 for path in self.paths:
Neale Rannsf12a83f2017-04-18 09:09:40 -0700399 self._test.vapi.ip_add_del_route(
400 self.dest_addr,
401 self.dest_addr_len,
402 path.nh_addr,
403 path.nh_itf,
404 table_id=self.table_id,
405 next_hop_table_id=path.nh_table_id,
406 next_hop_via_label=path.nh_via_label,
Neale Ranns810086d2017-11-05 16:26:46 -0800407 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700408 is_add=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800409 is_udp_encap=path.is_udp_encap,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800410 is_ipv6=self.is_ip6,
411 is_dvr=path.is_dvr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000412
Neale Ranns5a8123b2017-01-26 01:18:23 -0800413 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800414 return find_route(self._test,
415 self.dest_addr_p,
416 self.dest_addr_len,
417 self.table_id,
418 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Rannsad422ed2016-11-02 14:20:04 +0000419
Neale Ranns5a8123b2017-01-26 01:18:23 -0800420 def __str__(self):
421 return self.object_id()
422
423 def object_id(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800424 return ("%d:%s/%d"
425 % (self.table_id,
426 self.dest_addr_p,
427 self.dest_addr_len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800428
Neale Ranns008dbe12018-09-07 09:32:36 -0700429 def get_stats_to(self):
430 c = self._test.statistics.get_counter("/net/route/to")
431 return c[0][self.stats_index]
432
433 def get_stats_via(self):
434 c = self._test.statistics.get_counter("/net/route/via")
435 return c[0][self.stats_index]
436
Neale Ranns5a8123b2017-01-26 01:18:23 -0800437
438class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000439 """
440 IP Multicast Route
441 """
442
443 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800444 grp_addr_len, e_flags, paths, table_id=0,
445 rpf_id=0, is_ip6=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000446 self._test = test
447 self.paths = paths
448 self.grp_addr_len = grp_addr_len
449 self.table_id = table_id
450 self.e_flags = e_flags
451 self.is_ip6 = is_ip6
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800452 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000453
Neale Ranns947ea622018-06-07 23:48:20 -0700454 self.grp_addr_p = grp_addr
455 self.src_addr_p = src_addr
Neale Ranns32e1c012016-11-22 17:07:28 +0000456 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800457 self.grp_addr = inet_pton(AF_INET6, grp_addr)
458 self.src_addr = inet_pton(AF_INET6, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000459 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800460 self.grp_addr = inet_pton(AF_INET, grp_addr)
461 self.src_addr = inet_pton(AF_INET, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000462
463 def add_vpp_config(self):
464 for path in self.paths:
Neale Ranns28c142e2018-09-07 09:37:07 -0700465 r = self._test.vapi.ip_mroute_add_del(self.src_addr,
466 self.grp_addr,
467 self.grp_addr_len,
468 self.e_flags,
469 path.proto,
470 path.nh_itf,
471 path.nh_addr,
472 path.nh_i_flags,
473 bier_imp=path.bier_imp,
474 rpf_id=self.rpf_id,
475 table_id=self.table_id,
476 is_ipv6=self.is_ip6)
477 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800478 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000479
480 def remove_vpp_config(self):
481 for path in self.paths:
482 self._test.vapi.ip_mroute_add_del(self.src_addr,
483 self.grp_addr,
484 self.grp_addr_len,
485 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700486 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000487 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700488 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000489 path.nh_i_flags,
490 table_id=self.table_id,
Neale Rannseea537a2018-01-09 04:11:28 -0800491 bier_imp=path.bier_imp,
Neale Ranns32e1c012016-11-22 17:07:28 +0000492 is_add=0,
493 is_ipv6=self.is_ip6)
494
495 def update_entry_flags(self, flags):
496 self.e_flags = flags
497 self._test.vapi.ip_mroute_add_del(self.src_addr,
498 self.grp_addr,
499 self.grp_addr_len,
500 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700501 0,
Neale Ranns32e1c012016-11-22 17:07:28 +0000502 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700503 "",
Neale Ranns32e1c012016-11-22 17:07:28 +0000504 0,
505 table_id=self.table_id,
506 is_ipv6=self.is_ip6)
507
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800508 def update_rpf_id(self, rpf_id):
509 self.rpf_id = rpf_id
510 self._test.vapi.ip_mroute_add_del(self.src_addr,
511 self.grp_addr,
512 self.grp_addr_len,
513 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700514 0,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800515 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700516 "",
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800517 0,
518 rpf_id=self.rpf_id,
519 table_id=self.table_id,
520 is_ipv6=self.is_ip6)
521
Neale Ranns32e1c012016-11-22 17:07:28 +0000522 def update_path_flags(self, itf, flags):
523 for path in self.paths:
524 if path.nh_itf == itf:
525 path.nh_i_flags = flags
526 break
527 self._test.vapi.ip_mroute_add_del(self.src_addr,
528 self.grp_addr,
529 self.grp_addr_len,
530 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700531 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000532 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700533 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000534 path.nh_i_flags,
535 table_id=self.table_id,
536 is_ipv6=self.is_ip6)
537
Neale Ranns5a8123b2017-01-26 01:18:23 -0800538 def query_vpp_config(self):
Neale Ranns947ea622018-06-07 23:48:20 -0700539 return find_mroute(self._test,
540 self.grp_addr_p,
541 self.src_addr_p,
542 self.grp_addr_len,
543 self.table_id,
544 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Ranns32e1c012016-11-22 17:07:28 +0000545
Neale Ranns5a8123b2017-01-26 01:18:23 -0800546 def __str__(self):
547 return self.object_id()
548
549 def object_id(self):
550 if self.is_ip6:
551 return ("%d:(%s,%s/%d)"
552 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800553 inet_ntop(AF_INET6, self.src_addr),
554 inet_ntop(AF_INET6, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800555 self.grp_addr_len))
556 else:
557 return ("%d:(%s,%s/%d)"
558 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800559 inet_ntop(AF_INET, self.src_addr),
560 inet_ntop(AF_INET, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800561 self.grp_addr_len))
562
Neale Ranns28c142e2018-09-07 09:37:07 -0700563 def get_stats(self):
564 c = self._test.statistics.get_counter("/net/mroute")
565 return c[0][self.stats_index]
566
Neale Ranns5a8123b2017-01-26 01:18:23 -0800567
568class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000569 def __init__(self, test, route, interface, packet):
570 self.route = route
571 self.interface = interface
572 self.packet = packet
573 self.test = test
574
575 def compare(self, signal):
576 self.test.assertEqual(self.interface, signal.sw_if_index)
577 self.test.assertEqual(self.route.table_id, signal.table_id)
578 self.test.assertEqual(self.route.grp_addr_len,
579 signal.grp_address_len)
580 for i in range(self.route.grp_addr_len / 8):
581 self.test.assertEqual(self.route.grp_addr[i],
582 signal.grp_address[i])
583 if (self.route.grp_addr_len > 32):
584 for i in range(4):
585 self.test.assertEqual(self.route.src_addr[i],
586 signal.src_address[i])
587
588
Neale Ranns5a8123b2017-01-26 01:18:23 -0800589class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000590 """
591 MPLS to IP Binding
592 """
593
Neale Ranns5a8123b2017-01-26 01:18:23 -0800594 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700595 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000596 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000597 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700598 self.dest_addr = dest_addr
Neale Rannsad422ed2016-11-02 14:20:04 +0000599 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800600 self.table_id = table_id
601 self.ip_table_id = ip_table_id
Neale Rannsf12a83f2017-04-18 09:09:40 -0700602 self.is_ip6 = is_ip6
603 if is_ip6:
604 self.dest_addrn = inet_pton(AF_INET6, dest_addr)
605 else:
606 self.dest_addrn = inet_pton(AF_INET, dest_addr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000607
608 def add_vpp_config(self):
609 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700610 self.dest_addrn,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800611 self.dest_addr_len,
612 table_id=self.table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700613 ip_table_id=self.ip_table_id,
614 is_ip4=(self.is_ip6 == 0))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800615 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000616
617 def remove_vpp_config(self):
618 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700619 self.dest_addrn,
Neale Rannsad422ed2016-11-02 14:20:04 +0000620 self.dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700621 table_id=self.table_id,
622 ip_table_id=self.ip_table_id,
623 is_bind=0,
624 is_ip4=(self.is_ip6 == 0))
Neale Rannsad422ed2016-11-02 14:20:04 +0000625
Neale Ranns5a8123b2017-01-26 01:18:23 -0800626 def query_vpp_config(self):
627 dump = self._test.vapi.mpls_fib_dump()
628 for e in dump:
629 if self.local_label == e.label \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800630 and self.table_id == e.table_id:
631 return True
632 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000633
Neale Ranns5a8123b2017-01-26 01:18:23 -0800634 def __str__(self):
635 return self.object_id()
636
637 def object_id(self):
638 return ("%d:%s binds %d:%s/%d"
639 % (self.table_id,
640 self.local_label,
641 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700642 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800643 self.dest_addr_len))
644
645
Neale Ranns15002542017-09-10 04:39:11 -0700646class VppMplsTable(VppObject):
647
648 def __init__(self,
649 test,
650 table_id):
651 self._test = test
652 self.table_id = table_id
653
654 def add_vpp_config(self):
655 self._test.vapi.mpls_table_add_del(
656 self.table_id,
657 is_add=1)
658 self._test.registry.register(self, self._test.logger)
659
660 def remove_vpp_config(self):
661 self._test.vapi.mpls_table_add_del(
662 self.table_id,
663 is_add=0)
664
665 def query_vpp_config(self):
666 # find the default route
667 dump = self._test.vapi.mpls_fib_dump()
668 if len(dump):
669 return True
670 return False
671
672 def __str__(self):
673 return self.object_id()
674
675 def object_id(self):
676 return ("table-mpls-%d" % (self.table_id))
677
678
Neale Ranns5a8123b2017-01-26 01:18:23 -0800679class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000680 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800681 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000682 """
683
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800684 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
685 is_multicast=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000686 self._test = test
687 self.paths = paths
688 self.local_label = local_label
689 self.eos_bit = eos_bit
690 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800691 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000692
693 def add_vpp_config(self):
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800694 is_multipath = len(self.paths) > 1
Neale Rannsad422ed2016-11-02 14:20:04 +0000695 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800696 lstack = path.encode_labels()
697
Neale Ranns008dbe12018-09-07 09:32:36 -0700698 r = self._test.vapi.mpls_route_add_del(
Klement Sekerada505f62017-01-04 12:58:53 +0100699 self.local_label,
700 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700701 path.proto,
Klement Sekerada505f62017-01-04 12:58:53 +0100702 path.nh_addr,
703 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800704 is_multicast=self.is_multicast,
705 is_multipath=is_multipath,
Klement Sekerada505f62017-01-04 12:58:53 +0100706 table_id=self.table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800707 is_interface_rx=path.is_interface_rx,
708 is_rpf_id=path.is_rpf_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800709 next_hop_out_label_stack=lstack,
710 next_hop_n_out_labels=len(lstack),
Klement Sekerada505f62017-01-04 12:58:53 +0100711 next_hop_via_label=path.nh_via_label,
712 next_hop_table_id=path.nh_table_id)
Neale Ranns008dbe12018-09-07 09:32:36 -0700713 self.stats_index = r.stats_index
Neale Ranns5a8123b2017-01-26 01:18:23 -0800714 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000715
716 def remove_vpp_config(self):
717 for path in self.paths:
718 self._test.vapi.mpls_route_add_del(self.local_label,
719 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700720 path.proto,
Neale Rannsad422ed2016-11-02 14:20:04 +0000721 path.nh_addr,
722 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800723 is_rpf_id=path.is_rpf_id,
Neale Rannsad422ed2016-11-02 14:20:04 +0000724 table_id=self.table_id,
725 is_add=0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800726
727 def query_vpp_config(self):
728 dump = self._test.vapi.mpls_fib_dump()
729 for e in dump:
730 if self.local_label == e.label \
731 and self.eos_bit == e.eos_bit \
732 and self.table_id == e.table_id:
733 return True
734 return False
735
736 def __str__(self):
737 return self.object_id()
738
739 def object_id(self):
740 return ("%d:%s/%d"
741 % (self.table_id,
742 self.local_label,
743 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]