blob: 17a42fec706d2de9c0b24c6b38a6f44815504c4e [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 Ranns177bbdc2016-11-15 09:46:51 +00009
Neale Rannsad422ed2016-11-02 14:20:04 +000010# from vnet/vnet/mpls/mpls_types.h
11MPLS_IETF_MAX_LABEL = 0xfffff
12MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
Neale Ranns177bbdc2016-11-15 09:46:51 +000013
Neale Ranns177bbdc2016-11-15 09:46:51 +000014
Neale Ranns180279b2017-03-16 15:49:09 -040015class MRouteItfFlags:
16 MFIB_ITF_FLAG_NONE = 0
17 MFIB_ITF_FLAG_NEGATE_SIGNAL = 1
18 MFIB_ITF_FLAG_ACCEPT = 2
19 MFIB_ITF_FLAG_FORWARD = 4
20 MFIB_ITF_FLAG_SIGNAL_PRESENT = 8
21 MFIB_ITF_FLAG_INTERNAL_COPY = 16
22
23
24class MRouteEntryFlags:
25 MFIB_ENTRY_FLAG_NONE = 0
26 MFIB_ENTRY_FLAG_SIGNAL = 1
27 MFIB_ENTRY_FLAG_DROP = 2
28 MFIB_ENTRY_FLAG_CONNECTED = 4
29 MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8
30
31
Neale Rannsda78f952017-05-24 09:15:43 -070032class DpoProto:
33 DPO_PROTO_IP4 = 0
34 DPO_PROTO_IP6 = 1
35 DPO_PROTO_MPLS = 2
36 DPO_PROTO_ETHERNET = 3
Neale Rannsd792d9c2017-10-21 10:53:20 -070037 DPO_PROTO_BIER = 4
38 DPO_PROTO_NSH = 5
Neale Rannsda78f952017-05-24 09:15:43 -070039
40
Neale Ranns31ed7442018-02-23 05:29:09 -080041class MplsLspMode:
42 PIPE = 0
43 UNIFORM = 1
44
45
Neale Rannsb3b2de72017-03-08 05:17:22 -080046def find_route(test, ip_addr, len, table_id=0, inet=AF_INET):
47 if inet == AF_INET:
48 s = 4
49 routes = test.vapi.ip_fib_dump()
50 else:
51 s = 16
52 routes = test.vapi.ip6_fib_dump()
53
54 route_addr = inet_pton(inet, ip_addr)
55 for e in routes:
56 if route_addr == e.address[:s] \
57 and len == e.address_length \
58 and table_id == e.table_id:
59 return True
60 return False
61
62
Neale Ranns15002542017-09-10 04:39:11 -070063class VppIpTable(VppObject):
64
65 def __init__(self,
66 test,
67 table_id,
68 is_ip6=0):
69 self._test = test
70 self.table_id = table_id
71 self.is_ip6 = is_ip6
72
73 def add_vpp_config(self):
74 self._test.vapi.ip_table_add_del(
75 self.table_id,
76 is_ipv6=self.is_ip6,
77 is_add=1)
78 self._test.registry.register(self, self._test.logger)
79
80 def remove_vpp_config(self):
81 self._test.vapi.ip_table_add_del(
82 self.table_id,
83 is_ipv6=self.is_ip6,
84 is_add=0)
85
86 def query_vpp_config(self):
87 # find the default route
88 return find_route(self._test,
89 "::" if self.is_ip6 else "0.0.0.0",
90 0,
91 self.table_id,
92 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
93
94 def __str__(self):
95 return self.object_id()
96
97 def object_id(self):
98 return ("table-%s-%d" %
99 ("v6" if self.is_ip6 == 1 else "v4",
100 self.table_id))
101
102
Neale Ranns31ed7442018-02-23 05:29:09 -0800103class VppMplsLabel(object):
104 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
105 self.value = value
106 self.mode = mode
107 self.ttl = ttl
108 self.exp = exp
109
110 def encode(self):
111 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
112 return {'label': self.value,
113 'ttl': self.ttl,
114 'exp': self.exp,
115 'is_uniform': is_uniform}
116
117
Neale Ranns5a8123b2017-01-26 01:18:23 -0800118class VppRoutePath(object):
Neale Rannsad422ed2016-11-02 14:20:04 +0000119
Klement Sekerada505f62017-01-04 12:58:53 +0100120 def __init__(
121 self,
122 nh_addr,
123 nh_sw_if_index,
124 nh_table_id=0,
125 labels=[],
Neale Rannsfca0c242017-01-13 07:57:46 -0800126 nh_via_label=MPLS_LABEL_INVALID,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800127 rpf_id=0,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700128 is_interface_rx=0,
129 is_resolve_host=0,
Neale Rannsda78f952017-05-24 09:15:43 -0700130 is_resolve_attached=0,
Neale Ranns054c03a2017-10-13 05:15:07 -0700131 is_source_lookup=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800132 is_udp_encap=0,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800133 is_dvr=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800134 next_hop_id=0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -0700135 proto=DpoProto.DPO_PROTO_IP4):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000136 self.nh_itf = nh_sw_if_index
137 self.nh_table_id = nh_table_id
Neale Rannsad422ed2016-11-02 14:20:04 +0000138 self.nh_via_label = nh_via_label
139 self.nh_labels = labels
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800140 self.weight = 1
141 self.rpf_id = rpf_id
Neale Rannsda78f952017-05-24 09:15:43 -0700142 self.proto = proto
143 if self.proto is DpoProto.DPO_PROTO_IP6:
144 self.nh_addr = inet_pton(AF_INET6, nh_addr)
145 elif self.proto is DpoProto.DPO_PROTO_IP4:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800146 self.nh_addr = inet_pton(AF_INET, nh_addr)
Neale Ranns31426c62017-05-24 10:32:58 -0700147 else:
Neale Rannsda78f952017-05-24 09:15:43 -0700148 self.nh_addr = inet_pton(AF_INET6, "::")
Neale Rannsf12a83f2017-04-18 09:09:40 -0700149 self.is_resolve_host = is_resolve_host
150 self.is_resolve_attached = is_resolve_attached
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800151 self.is_interface_rx = is_interface_rx
Neale Ranns054c03a2017-10-13 05:15:07 -0700152 self.is_source_lookup = is_source_lookup
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800153 self.is_rpf_id = 0
154 if rpf_id != 0:
155 self.is_rpf_id = 1
156 self.nh_itf = rpf_id
Neale Ranns810086d2017-11-05 16:26:46 -0800157 self.is_udp_encap = is_udp_encap
158 self.next_hop_id = next_hop_id
Neale Rannsf068c3e2018-01-03 04:18:48 -0800159 self.is_dvr = is_dvr
Neale Ranns177bbdc2016-11-15 09:46:51 +0000160
Neale Ranns31ed7442018-02-23 05:29:09 -0800161 def encode_labels(self):
162 lstack = []
163 for l in self.nh_labels:
164 if type(l) == VppMplsLabel:
165 lstack.append(l.encode())
166 else:
167 lstack.append({'label': l,
168 'ttl': 255})
169 return lstack
170
Neale Ranns2303cb12018-02-21 04:57:17 -0800171 def encode(self):
172 return {'next_hop': self.nh_addr,
173 'weight': 1,
174 'afi': 0,
175 'preference': 0,
176 'table_id': self.nh_table_id,
177 'next_hop_id': self.next_hop_id,
178 'sw_if_index': self.nh_itf,
179 'afi': self.proto,
180 'is_udp_encap': self.is_udp_encap,
181 'n_labels': len(self.nh_labels),
182 'label_stack': self.encode_labels()}
183
Neale Ranns177bbdc2016-11-15 09:46:51 +0000184
Neale Ranns5a8123b2017-01-26 01:18:23 -0800185class VppMRoutePath(VppRoutePath):
Neale Ranns32e1c012016-11-22 17:07:28 +0000186
Neale Rannsd792d9c2017-10-21 10:53:20 -0700187 def __init__(self, nh_sw_if_index, flags,
Neale Rannse821ab12017-06-01 07:45:05 -0700188 nh=None,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700189 proto=DpoProto.DPO_PROTO_IP4,
190 bier_imp=0):
Neale Rannse821ab12017-06-01 07:45:05 -0700191 if not nh:
192 nh = "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0"
193 super(VppMRoutePath, self).__init__(nh,
194 nh_sw_if_index,
195 proto=proto)
Neale Ranns32e1c012016-11-22 17:07:28 +0000196 self.nh_i_flags = flags
Neale Rannsd792d9c2017-10-21 10:53:20 -0700197 self.bier_imp = bier_imp
Neale Ranns32e1c012016-11-22 17:07:28 +0000198
199
Neale Ranns5a8123b2017-01-26 01:18:23 -0800200class VppIpRoute(VppObject):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000201 """
202 IP Route
203 """
204
205 def __init__(self, test, dest_addr,
Neale Ranns37be7362017-02-21 17:30:26 -0800206 dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0,
207 is_unreach=0, is_prohibit=0):
Neale Ranns177bbdc2016-11-15 09:46:51 +0000208 self._test = test
209 self.paths = paths
Neale Ranns177bbdc2016-11-15 09:46:51 +0000210 self.dest_addr_len = dest_addr_len
211 self.table_id = table_id
Neale Rannsfca0c242017-01-13 07:57:46 -0800212 self.is_ip6 = is_ip6
213 self.is_local = is_local
Neale Ranns37be7362017-02-21 17:30:26 -0800214 self.is_unreach = is_unreach
215 self.is_prohibit = is_prohibit
Neale Rannsb3b2de72017-03-08 05:17:22 -0800216 self.dest_addr_p = dest_addr
Neale Rannsfca0c242017-01-13 07:57:46 -0800217 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800218 self.dest_addr = inet_pton(AF_INET6, dest_addr)
Neale Rannsfca0c242017-01-13 07:57:46 -0800219 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800220 self.dest_addr = inet_pton(AF_INET, dest_addr)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000221
Neale Ranns69b7aa42017-03-10 03:04:12 -0800222 def modify(self, paths, is_local=0,
223 is_unreach=0, is_prohibit=0):
224 self.paths = paths
225 self.is_local = is_local
226 self.is_unreach = is_unreach
227 self.is_prohibit = is_prohibit
228
Neale Ranns177bbdc2016-11-15 09:46:51 +0000229 def add_vpp_config(self):
Neale Ranns37be7362017-02-21 17:30:26 -0800230 if self.is_local or self.is_unreach or self.is_prohibit:
Klement Sekerada505f62017-01-04 12:58:53 +0100231 self._test.vapi.ip_add_del_route(
232 self.dest_addr,
233 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800234 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800235 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800236 is_local=self.is_local,
237 is_unreach=self.is_unreach,
238 is_prohibit=self.is_prohibit,
Klement Sekerada505f62017-01-04 12:58:53 +0100239 table_id=self.table_id,
Neale Rannsfca0c242017-01-13 07:57:46 -0800240 is_ipv6=self.is_ip6)
241 else:
242 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800243 lstack = path.encode_labels()
244
Neale Rannsfca0c242017-01-13 07:57:46 -0800245 self._test.vapi.ip_add_del_route(
246 self.dest_addr,
247 self.dest_addr_len,
248 path.nh_addr,
249 path.nh_itf,
250 table_id=self.table_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800251 next_hop_out_label_stack=lstack,
252 next_hop_n_out_labels=len(lstack),
Neale Rannsfca0c242017-01-13 07:57:46 -0800253 next_hop_via_label=path.nh_via_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700254 next_hop_table_id=path.nh_table_id,
Neale Ranns810086d2017-11-05 16:26:46 -0800255 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700256 is_ipv6=self.is_ip6,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800257 is_dvr=path.is_dvr,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700258 is_resolve_host=path.is_resolve_host,
259 is_resolve_attached=path.is_resolve_attached,
Neale Ranns054c03a2017-10-13 05:15:07 -0700260 is_source_lookup=path.is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800261 is_udp_encap=path.is_udp_encap,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700262 is_multipath=1 if len(self.paths) > 1 else 0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800263 self._test.registry.register(self, self._test.logger)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000264
265 def remove_vpp_config(self):
Neale Ranns37be7362017-02-21 17:30:26 -0800266 if self.is_local or self.is_unreach or self.is_prohibit:
Neale Rannsfca0c242017-01-13 07:57:46 -0800267 self._test.vapi.ip_add_del_route(
268 self.dest_addr,
269 self.dest_addr_len,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800270 inet_pton(AF_INET6, "::"),
Neale Rannsfca0c242017-01-13 07:57:46 -0800271 0xffffffff,
Neale Ranns37be7362017-02-21 17:30:26 -0800272 is_local=self.is_local,
273 is_unreach=self.is_unreach,
274 is_prohibit=self.is_prohibit,
Neale Rannsfca0c242017-01-13 07:57:46 -0800275 is_add=0,
276 table_id=self.table_id,
277 is_ipv6=self.is_ip6)
278 else:
279 for path in self.paths:
Neale Rannsf12a83f2017-04-18 09:09:40 -0700280 self._test.vapi.ip_add_del_route(
281 self.dest_addr,
282 self.dest_addr_len,
283 path.nh_addr,
284 path.nh_itf,
285 table_id=self.table_id,
286 next_hop_table_id=path.nh_table_id,
287 next_hop_via_label=path.nh_via_label,
Neale Ranns810086d2017-11-05 16:26:46 -0800288 next_hop_id=path.next_hop_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700289 is_add=0,
Neale Ranns810086d2017-11-05 16:26:46 -0800290 is_udp_encap=path.is_udp_encap,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800291 is_ipv6=self.is_ip6,
292 is_dvr=path.is_dvr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000293
Neale Ranns5a8123b2017-01-26 01:18:23 -0800294 def query_vpp_config(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800295 return find_route(self._test,
296 self.dest_addr_p,
297 self.dest_addr_len,
298 self.table_id,
299 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
Neale Rannsad422ed2016-11-02 14:20:04 +0000300
Neale Ranns5a8123b2017-01-26 01:18:23 -0800301 def __str__(self):
302 return self.object_id()
303
304 def object_id(self):
Neale Rannsb3b2de72017-03-08 05:17:22 -0800305 return ("%d:%s/%d"
306 % (self.table_id,
307 self.dest_addr_p,
308 self.dest_addr_len))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800309
310
311class VppIpMRoute(VppObject):
Neale Ranns32e1c012016-11-22 17:07:28 +0000312 """
313 IP Multicast Route
314 """
315
316 def __init__(self, test, src_addr, grp_addr,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800317 grp_addr_len, e_flags, paths, table_id=0,
318 rpf_id=0, is_ip6=0):
Neale Ranns32e1c012016-11-22 17:07:28 +0000319 self._test = test
320 self.paths = paths
321 self.grp_addr_len = grp_addr_len
322 self.table_id = table_id
323 self.e_flags = e_flags
324 self.is_ip6 = is_ip6
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800325 self.rpf_id = rpf_id
Neale Ranns32e1c012016-11-22 17:07:28 +0000326
327 if is_ip6:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800328 self.grp_addr = inet_pton(AF_INET6, grp_addr)
329 self.src_addr = inet_pton(AF_INET6, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000330 else:
Neale Rannsb3b2de72017-03-08 05:17:22 -0800331 self.grp_addr = inet_pton(AF_INET, grp_addr)
332 self.src_addr = inet_pton(AF_INET, src_addr)
Neale Ranns32e1c012016-11-22 17:07:28 +0000333
334 def add_vpp_config(self):
335 for path in self.paths:
336 self._test.vapi.ip_mroute_add_del(self.src_addr,
337 self.grp_addr,
338 self.grp_addr_len,
339 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700340 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000341 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700342 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000343 path.nh_i_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700344 bier_imp=path.bier_imp,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800345 rpf_id=self.rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +0000346 table_id=self.table_id,
347 is_ipv6=self.is_ip6)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800348 self._test.registry.register(self, self._test.logger)
Neale Ranns32e1c012016-11-22 17:07:28 +0000349
350 def remove_vpp_config(self):
351 for path in self.paths:
352 self._test.vapi.ip_mroute_add_del(self.src_addr,
353 self.grp_addr,
354 self.grp_addr_len,
355 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700356 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000357 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700358 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000359 path.nh_i_flags,
360 table_id=self.table_id,
Neale Rannseea537a2018-01-09 04:11:28 -0800361 bier_imp=path.bier_imp,
Neale Ranns32e1c012016-11-22 17:07:28 +0000362 is_add=0,
363 is_ipv6=self.is_ip6)
364
365 def update_entry_flags(self, flags):
366 self.e_flags = flags
367 self._test.vapi.ip_mroute_add_del(self.src_addr,
368 self.grp_addr,
369 self.grp_addr_len,
370 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700371 0,
Neale Ranns32e1c012016-11-22 17:07:28 +0000372 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700373 "",
Neale Ranns32e1c012016-11-22 17:07:28 +0000374 0,
375 table_id=self.table_id,
376 is_ipv6=self.is_ip6)
377
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800378 def update_rpf_id(self, rpf_id):
379 self.rpf_id = rpf_id
380 self._test.vapi.ip_mroute_add_del(self.src_addr,
381 self.grp_addr,
382 self.grp_addr_len,
383 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700384 0,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800385 0xffffffff,
Neale Rannse821ab12017-06-01 07:45:05 -0700386 "",
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800387 0,
388 rpf_id=self.rpf_id,
389 table_id=self.table_id,
390 is_ipv6=self.is_ip6)
391
Neale Ranns32e1c012016-11-22 17:07:28 +0000392 def update_path_flags(self, itf, flags):
393 for path in self.paths:
394 if path.nh_itf == itf:
395 path.nh_i_flags = flags
396 break
397 self._test.vapi.ip_mroute_add_del(self.src_addr,
398 self.grp_addr,
399 self.grp_addr_len,
400 self.e_flags,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700401 path.proto,
Neale Ranns32e1c012016-11-22 17:07:28 +0000402 path.nh_itf,
Neale Rannse821ab12017-06-01 07:45:05 -0700403 path.nh_addr,
Neale Ranns32e1c012016-11-22 17:07:28 +0000404 path.nh_i_flags,
405 table_id=self.table_id,
406 is_ipv6=self.is_ip6)
407
Neale Ranns5a8123b2017-01-26 01:18:23 -0800408 def query_vpp_config(self):
Neale Rannsd792d9c2017-10-21 10:53:20 -0700409 if self.is_ip6:
410 dump = self._test.vapi.ip6_mfib_dump()
411 else:
412 dump = self._test.vapi.ip_mfib_dump()
Neale Ranns5a8123b2017-01-26 01:18:23 -0800413 for e in dump:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700414 if self.grp_addr == e.grp_address \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800415 and self.grp_addr_len == e.address_length \
Neale Rannsd792d9c2017-10-21 10:53:20 -0700416 and self.src_addr == e.src_address \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800417 and self.table_id == e.table_id:
418 return True
419 return False
Neale Ranns32e1c012016-11-22 17:07:28 +0000420
Neale Ranns5a8123b2017-01-26 01:18:23 -0800421 def __str__(self):
422 return self.object_id()
423
424 def object_id(self):
425 if self.is_ip6:
426 return ("%d:(%s,%s/%d)"
427 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800428 inet_ntop(AF_INET6, self.src_addr),
429 inet_ntop(AF_INET6, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800430 self.grp_addr_len))
431 else:
432 return ("%d:(%s,%s/%d)"
433 % (self.table_id,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800434 inet_ntop(AF_INET, self.src_addr),
435 inet_ntop(AF_INET, self.grp_addr),
Neale Ranns5a8123b2017-01-26 01:18:23 -0800436 self.grp_addr_len))
437
438
439class VppMFibSignal(object):
Neale Ranns32e1c012016-11-22 17:07:28 +0000440 def __init__(self, test, route, interface, packet):
441 self.route = route
442 self.interface = interface
443 self.packet = packet
444 self.test = test
445
446 def compare(self, signal):
447 self.test.assertEqual(self.interface, signal.sw_if_index)
448 self.test.assertEqual(self.route.table_id, signal.table_id)
449 self.test.assertEqual(self.route.grp_addr_len,
450 signal.grp_address_len)
451 for i in range(self.route.grp_addr_len / 8):
452 self.test.assertEqual(self.route.grp_addr[i],
453 signal.grp_address[i])
454 if (self.route.grp_addr_len > 32):
455 for i in range(4):
456 self.test.assertEqual(self.route.src_addr[i],
457 signal.src_address[i])
458
459
Neale Ranns5a8123b2017-01-26 01:18:23 -0800460class VppMplsIpBind(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000461 """
462 MPLS to IP Binding
463 """
464
Neale Ranns5a8123b2017-01-26 01:18:23 -0800465 def __init__(self, test, local_label, dest_addr, dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700466 table_id=0, ip_table_id=0, is_ip6=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000467 self._test = test
Neale Rannsad422ed2016-11-02 14:20:04 +0000468 self.dest_addr_len = dest_addr_len
Neale Rannsf12a83f2017-04-18 09:09:40 -0700469 self.dest_addr = dest_addr
Neale Rannsad422ed2016-11-02 14:20:04 +0000470 self.local_label = local_label
Neale Ranns5a8123b2017-01-26 01:18:23 -0800471 self.table_id = table_id
472 self.ip_table_id = ip_table_id
Neale Rannsf12a83f2017-04-18 09:09:40 -0700473 self.is_ip6 = is_ip6
474 if is_ip6:
475 self.dest_addrn = inet_pton(AF_INET6, dest_addr)
476 else:
477 self.dest_addrn = inet_pton(AF_INET, dest_addr)
Neale Rannsad422ed2016-11-02 14:20:04 +0000478
479 def add_vpp_config(self):
480 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700481 self.dest_addrn,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800482 self.dest_addr_len,
483 table_id=self.table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700484 ip_table_id=self.ip_table_id,
485 is_ip4=(self.is_ip6 == 0))
Neale Ranns5a8123b2017-01-26 01:18:23 -0800486 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000487
488 def remove_vpp_config(self):
489 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700490 self.dest_addrn,
Neale Rannsad422ed2016-11-02 14:20:04 +0000491 self.dest_addr_len,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700492 table_id=self.table_id,
493 ip_table_id=self.ip_table_id,
494 is_bind=0,
495 is_ip4=(self.is_ip6 == 0))
Neale Rannsad422ed2016-11-02 14:20:04 +0000496
Neale Ranns5a8123b2017-01-26 01:18:23 -0800497 def query_vpp_config(self):
498 dump = self._test.vapi.mpls_fib_dump()
499 for e in dump:
500 if self.local_label == e.label \
Neale Ranns5a8123b2017-01-26 01:18:23 -0800501 and self.table_id == e.table_id:
502 return True
503 return False
Neale Rannsad422ed2016-11-02 14:20:04 +0000504
Neale Ranns5a8123b2017-01-26 01:18:23 -0800505 def __str__(self):
506 return self.object_id()
507
508 def object_id(self):
509 return ("%d:%s binds %d:%s/%d"
510 % (self.table_id,
511 self.local_label,
512 self.ip_table_id,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700513 self.dest_addr,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800514 self.dest_addr_len))
515
516
Neale Ranns15002542017-09-10 04:39:11 -0700517class VppMplsTable(VppObject):
518
519 def __init__(self,
520 test,
521 table_id):
522 self._test = test
523 self.table_id = table_id
524
525 def add_vpp_config(self):
526 self._test.vapi.mpls_table_add_del(
527 self.table_id,
528 is_add=1)
529 self._test.registry.register(self, self._test.logger)
530
531 def remove_vpp_config(self):
532 self._test.vapi.mpls_table_add_del(
533 self.table_id,
534 is_add=0)
535
536 def query_vpp_config(self):
537 # find the default route
538 dump = self._test.vapi.mpls_fib_dump()
539 if len(dump):
540 return True
541 return False
542
543 def __str__(self):
544 return self.object_id()
545
546 def object_id(self):
547 return ("table-mpls-%d" % (self.table_id))
548
549
Neale Ranns5a8123b2017-01-26 01:18:23 -0800550class VppMplsRoute(VppObject):
Neale Rannsad422ed2016-11-02 14:20:04 +0000551 """
Neale Ranns5a8123b2017-01-26 01:18:23 -0800552 MPLS Route/LSP
Neale Rannsad422ed2016-11-02 14:20:04 +0000553 """
554
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800555 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
556 is_multicast=0):
Neale Rannsad422ed2016-11-02 14:20:04 +0000557 self._test = test
558 self.paths = paths
559 self.local_label = local_label
560 self.eos_bit = eos_bit
561 self.table_id = table_id
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800562 self.is_multicast = is_multicast
Neale Rannsad422ed2016-11-02 14:20:04 +0000563
564 def add_vpp_config(self):
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800565 is_multipath = len(self.paths) > 1
Neale Rannsad422ed2016-11-02 14:20:04 +0000566 for path in self.paths:
Neale Ranns31ed7442018-02-23 05:29:09 -0800567 lstack = path.encode_labels()
568
Klement Sekerada505f62017-01-04 12:58:53 +0100569 self._test.vapi.mpls_route_add_del(
570 self.local_label,
571 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700572 path.proto,
Klement Sekerada505f62017-01-04 12:58:53 +0100573 path.nh_addr,
574 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800575 is_multicast=self.is_multicast,
576 is_multipath=is_multipath,
Klement Sekerada505f62017-01-04 12:58:53 +0100577 table_id=self.table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800578 is_interface_rx=path.is_interface_rx,
579 is_rpf_id=path.is_rpf_id,
Neale Ranns31ed7442018-02-23 05:29:09 -0800580 next_hop_out_label_stack=lstack,
581 next_hop_n_out_labels=len(lstack),
Klement Sekerada505f62017-01-04 12:58:53 +0100582 next_hop_via_label=path.nh_via_label,
583 next_hop_table_id=path.nh_table_id)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800584 self._test.registry.register(self, self._test.logger)
Neale Rannsad422ed2016-11-02 14:20:04 +0000585
586 def remove_vpp_config(self):
587 for path in self.paths:
588 self._test.vapi.mpls_route_add_del(self.local_label,
589 self.eos_bit,
Neale Rannsda78f952017-05-24 09:15:43 -0700590 path.proto,
Neale Rannsad422ed2016-11-02 14:20:04 +0000591 path.nh_addr,
592 path.nh_itf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800593 is_rpf_id=path.is_rpf_id,
Neale Rannsad422ed2016-11-02 14:20:04 +0000594 table_id=self.table_id,
595 is_add=0)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800596
597 def query_vpp_config(self):
598 dump = self._test.vapi.mpls_fib_dump()
599 for e in dump:
600 if self.local_label == e.label \
601 and self.eos_bit == e.eos_bit \
602 and self.table_id == e.table_id:
603 return True
604 return False
605
606 def __str__(self):
607 return self.object_id()
608
609 def object_id(self):
610 return ("%d:%s/%d"
611 % (self.table_id,
612 self.local_label,
613 20+self.eos_bit))