4 object abstractions for representing IP routes in VPP
7 from vpp_object import *
8 from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
10 # from vnet/vnet/mpls/mpls_types.h
11 MPLS_IETF_MAX_LABEL = 0xfffff
12 MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
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
24 class 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
36 DPO_PROTO_ETHERNET = 3
46 def find_route(test, ip_addr, len, table_id=0, inet=AF_INET):
49 routes = test.vapi.ip_fib_dump()
52 routes = test.vapi.ip6_fib_dump()
54 route_addr = inet_pton(inet, ip_addr)
56 if route_addr == e.address[:s] \
57 and len == e.address_length \
58 and table_id == e.table_id:
63 class VppIpTable(VppObject):
70 self.table_id = table_id
73 def add_vpp_config(self):
74 self._test.vapi.ip_table_add_del(
78 self._test.registry.register(self, self._test.logger)
80 def remove_vpp_config(self):
81 self._test.vapi.ip_table_add_del(
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",
92 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
95 return self.object_id()
98 return ("table-%s-%d" %
99 ("v6" if self.is_ip6 == 1 else "v4",
103 class VppMplsLabel(object):
104 def __init__(self, value, mode=MplsLspMode.PIPE, ttl=64, exp=0):
111 is_uniform = 0 if self.mode is MplsLspMode.PIPE else 1
112 return {'label': self.value,
115 'is_uniform': is_uniform}
118 class VppRoutePath(object):
126 nh_via_label=MPLS_LABEL_INVALID,
130 is_resolve_attached=0,
134 next_hop_id=0xffffffff,
135 proto=DpoProto.DPO_PROTO_IP4):
136 self.nh_itf = nh_sw_if_index
137 self.nh_table_id = nh_table_id
138 self.nh_via_label = nh_via_label
139 self.nh_labels = labels
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:
146 self.nh_addr = inet_pton(AF_INET, nh_addr)
148 self.nh_addr = inet_pton(AF_INET6, "::")
149 self.is_resolve_host = is_resolve_host
150 self.is_resolve_attached = is_resolve_attached
151 self.is_interface_rx = is_interface_rx
152 self.is_source_lookup = is_source_lookup
157 self.is_udp_encap = is_udp_encap
158 self.next_hop_id = next_hop_id
161 def encode_labels(self):
163 for l in self.nh_labels:
164 if type(l) == VppMplsLabel:
165 lstack.append(l.encode())
167 lstack.append({'label': l,
172 return {'next_hop': self.nh_addr,
176 'table_id': self.nh_table_id,
177 'next_hop_id': self.next_hop_id,
178 'sw_if_index': self.nh_itf,
180 'is_udp_encap': self.is_udp_encap,
181 'n_labels': len(self.nh_labels),
182 'label_stack': self.encode_labels()}
185 class VppMRoutePath(VppRoutePath):
187 def __init__(self, nh_sw_if_index, flags,
189 proto=DpoProto.DPO_PROTO_IP4,
192 nh = "::" if proto is DpoProto.DPO_PROTO_IP6 else "0.0.0.0"
193 super(VppMRoutePath, self).__init__(nh,
196 self.nh_i_flags = flags
197 self.bier_imp = bier_imp
200 class VppIpRoute(VppObject):
205 def __init__(self, test, dest_addr,
206 dest_addr_len, paths, table_id=0, is_ip6=0, is_local=0,
207 is_unreach=0, is_prohibit=0):
210 self.dest_addr_len = dest_addr_len
211 self.table_id = table_id
213 self.is_local = is_local
214 self.is_unreach = is_unreach
215 self.is_prohibit = is_prohibit
216 self.dest_addr_p = dest_addr
218 self.dest_addr = inet_pton(AF_INET6, dest_addr)
220 self.dest_addr = inet_pton(AF_INET, dest_addr)
222 def modify(self, paths, is_local=0,
223 is_unreach=0, is_prohibit=0):
225 self.is_local = is_local
226 self.is_unreach = is_unreach
227 self.is_prohibit = is_prohibit
229 def add_vpp_config(self):
230 if self.is_local or self.is_unreach or self.is_prohibit:
231 self._test.vapi.ip_add_del_route(
234 inet_pton(AF_INET6, "::"),
236 is_local=self.is_local,
237 is_unreach=self.is_unreach,
238 is_prohibit=self.is_prohibit,
239 table_id=self.table_id,
242 for path in self.paths:
243 lstack = path.encode_labels()
245 self._test.vapi.ip_add_del_route(
250 table_id=self.table_id,
251 next_hop_out_label_stack=lstack,
252 next_hop_n_out_labels=len(lstack),
253 next_hop_via_label=path.nh_via_label,
254 next_hop_table_id=path.nh_table_id,
255 next_hop_id=path.next_hop_id,
258 is_resolve_host=path.is_resolve_host,
259 is_resolve_attached=path.is_resolve_attached,
260 is_source_lookup=path.is_source_lookup,
261 is_udp_encap=path.is_udp_encap,
262 is_multipath=1 if len(self.paths) > 1 else 0)
263 self._test.registry.register(self, self._test.logger)
265 def remove_vpp_config(self):
266 if self.is_local or self.is_unreach or self.is_prohibit:
267 self._test.vapi.ip_add_del_route(
270 inet_pton(AF_INET6, "::"),
272 is_local=self.is_local,
273 is_unreach=self.is_unreach,
274 is_prohibit=self.is_prohibit,
276 table_id=self.table_id,
279 for path in self.paths:
280 self._test.vapi.ip_add_del_route(
285 table_id=self.table_id,
286 next_hop_table_id=path.nh_table_id,
287 next_hop_via_label=path.nh_via_label,
288 next_hop_id=path.next_hop_id,
290 is_udp_encap=path.is_udp_encap,
294 def query_vpp_config(self):
295 return find_route(self._test,
299 inet=AF_INET6 if self.is_ip6 == 1 else AF_INET)
302 return self.object_id()
311 class VppIpMRoute(VppObject):
316 def __init__(self, test, src_addr, grp_addr,
317 grp_addr_len, e_flags, paths, table_id=0,
321 self.grp_addr_len = grp_addr_len
322 self.table_id = table_id
323 self.e_flags = e_flags
328 self.grp_addr = inet_pton(AF_INET6, grp_addr)
329 self.src_addr = inet_pton(AF_INET6, src_addr)
331 self.grp_addr = inet_pton(AF_INET, grp_addr)
332 self.src_addr = inet_pton(AF_INET, src_addr)
334 def add_vpp_config(self):
335 for path in self.paths:
336 self._test.vapi.ip_mroute_add_del(self.src_addr,
344 bier_imp=path.bier_imp,
346 table_id=self.table_id,
348 self._test.registry.register(self, self._test.logger)
350 def remove_vpp_config(self):
351 for path in self.paths:
352 self._test.vapi.ip_mroute_add_del(self.src_addr,
360 table_id=self.table_id,
361 bier_imp=path.bier_imp,
365 def update_entry_flags(self, flags):
367 self._test.vapi.ip_mroute_add_del(self.src_addr,
375 table_id=self.table_id,
378 def update_rpf_id(self, rpf_id):
380 self._test.vapi.ip_mroute_add_del(self.src_addr,
389 table_id=self.table_id,
392 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
397 self._test.vapi.ip_mroute_add_del(self.src_addr,
405 table_id=self.table_id,
408 def query_vpp_config(self):
410 dump = self._test.vapi.ip6_mfib_dump()
412 dump = self._test.vapi.ip_mfib_dump()
414 if self.grp_addr == e.grp_address \
415 and self.grp_addr_len == e.address_length \
416 and self.src_addr == e.src_address \
417 and self.table_id == e.table_id:
422 return self.object_id()
426 return ("%d:(%s,%s/%d)"
428 inet_ntop(AF_INET6, self.src_addr),
429 inet_ntop(AF_INET6, self.grp_addr),
432 return ("%d:(%s,%s/%d)"
434 inet_ntop(AF_INET, self.src_addr),
435 inet_ntop(AF_INET, self.grp_addr),
439 class VppMFibSignal(object):
440 def __init__(self, test, route, interface, packet):
442 self.interface = interface
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):
456 self.test.assertEqual(self.route.src_addr[i],
457 signal.src_address[i])
460 class VppMplsIpBind(VppObject):
465 def __init__(self, test, local_label, dest_addr, dest_addr_len,
466 table_id=0, ip_table_id=0, is_ip6=0):
468 self.dest_addr_len = dest_addr_len
469 self.dest_addr = dest_addr
470 self.local_label = local_label
471 self.table_id = table_id
472 self.ip_table_id = ip_table_id
475 self.dest_addrn = inet_pton(AF_INET6, dest_addr)
477 self.dest_addrn = inet_pton(AF_INET, dest_addr)
479 def add_vpp_config(self):
480 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
483 table_id=self.table_id,
484 ip_table_id=self.ip_table_id,
485 is_ip4=(self.is_ip6 == 0))
486 self._test.registry.register(self, self._test.logger)
488 def remove_vpp_config(self):
489 self._test.vapi.mpls_ip_bind_unbind(self.local_label,
492 table_id=self.table_id,
493 ip_table_id=self.ip_table_id,
495 is_ip4=(self.is_ip6 == 0))
497 def query_vpp_config(self):
498 dump = self._test.vapi.mpls_fib_dump()
500 if self.local_label == e.label \
501 and self.table_id == e.table_id:
506 return self.object_id()
509 return ("%d:%s binds %d:%s/%d"
517 class VppMplsTable(VppObject):
523 self.table_id = table_id
525 def add_vpp_config(self):
526 self._test.vapi.mpls_table_add_del(
529 self._test.registry.register(self, self._test.logger)
531 def remove_vpp_config(self):
532 self._test.vapi.mpls_table_add_del(
536 def query_vpp_config(self):
537 # find the default route
538 dump = self._test.vapi.mpls_fib_dump()
544 return self.object_id()
547 return ("table-mpls-%d" % (self.table_id))
550 class VppMplsRoute(VppObject):
555 def __init__(self, test, local_label, eos_bit, paths, table_id=0,
559 self.local_label = local_label
560 self.eos_bit = eos_bit
561 self.table_id = table_id
562 self.is_multicast = is_multicast
564 def add_vpp_config(self):
565 is_multipath = len(self.paths) > 1
566 for path in self.paths:
567 lstack = path.encode_labels()
569 self._test.vapi.mpls_route_add_del(
575 is_multicast=self.is_multicast,
576 is_multipath=is_multipath,
577 table_id=self.table_id,
578 is_interface_rx=path.is_interface_rx,
579 is_rpf_id=path.is_rpf_id,
580 next_hop_out_label_stack=lstack,
581 next_hop_n_out_labels=len(lstack),
582 next_hop_via_label=path.nh_via_label,
583 next_hop_table_id=path.nh_table_id)
584 self._test.registry.register(self, self._test.logger)
586 def remove_vpp_config(self):
587 for path in self.paths:
588 self._test.vapi.mpls_route_add_del(self.local_label,
593 is_rpf_id=path.is_rpf_id,
594 table_id=self.table_id,
597 def query_vpp_config(self):
598 dump = self._test.vapi.mpls_fib_dump()
600 if self.local_label == e.label \
601 and self.eos_bit == e.eos_bit \
602 and self.table_id == e.table_id:
607 return self.object_id()