5 from framework import VppTestCase, VppTestRunner
6 from vpp_object import VppObject
7 from vpp_neighbor import VppNeighbor
8 from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable
12 from vpp_papi_provider import L2_PORT_TYPE
14 from scapy.packet import Raw
15 from scapy.layers.l2 import Ether, ARP
16 from scapy.layers.inet import IP, UDP
17 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6NDOptSrcLLAddr, \
19 from scapy.utils6 import in6_getnsma, in6_getnsmac
21 from socket import AF_INET, AF_INET6
22 from scapy.utils import inet_pton, inet_ntop
23 from util import mactobinary
26 def find_gbp_endpoint(test, sw_if_index, ip=None, mac=None):
27 vip = VppIpAddress(ip)
29 eps = test.vapi.gbp_endpoint_dump()
31 if ep.endpoint.sw_if_index != sw_if_index:
33 for eip in ep.endpoint.ips:
39 class VppGbpEndpoint(VppObject):
46 return mactobinary(self.itf.remote_mac)
50 return self.itf.remote_mac
70 return [self.ip4, self.ip6]
74 return [self.fip4, self.fip6]
76 def __init__(self, test, itf, epg, recirc, ip4, fip4, ip6, fip6):
82 self._ip4 = VppIpAddress(ip4)
83 self._fip4 = VppIpAddress(fip4)
84 self._ip6 = VppIpAddress(ip6)
85 self._fip6 = VppIpAddress(fip6)
87 self.vmac = VppMacAddress(self.itf.remote_mac)
89 def add_vpp_config(self):
90 res = self._test.vapi.gbp_endpoint_add(
92 [self.ip4.encode(), self.ip6.encode()],
95 self.handle = res.handle
96 self._test.registry.register(self, self._test.logger)
98 def remove_vpp_config(self):
99 self._test.vapi.gbp_endpoint_del(self.handle)
102 return self.object_id()
105 return "gbp-endpoint;[%d:%s:%d]" % (self.itf.sw_if_index,
109 def query_vpp_config(self):
110 return find_gbp_endpoint(self._test,
111 self.itf.sw_if_index,
115 class VppGbpRecirc(VppObject):
117 GBP Recirculation Interface
120 def __init__(self, test, epg, recirc, is_ext=False):
126 def add_vpp_config(self):
127 self._test.vapi.gbp_recirc_add_del(
129 self.recirc.sw_if_index,
132 self._test.registry.register(self, self._test.logger)
134 def remove_vpp_config(self):
135 self._test.vapi.gbp_recirc_add_del(
137 self.recirc.sw_if_index,
142 return self.object_id()
145 return "gbp-recirc;[%d]" % (self.recirc.sw_if_index)
147 def query_vpp_config(self):
148 rs = self._test.vapi.gbp_recirc_dump()
150 if r.recirc.sw_if_index == self.recirc.sw_if_index:
155 class VppGbpSubnet(VppObject):
160 def __init__(self, test, table_id, address, address_len,
162 sw_if_index=None, epg=None):
164 self.table_id = table_id
165 self.prefix = VppIpPrefix(address, address_len)
166 self.is_internal = is_internal
167 self.sw_if_index = sw_if_index
170 def add_vpp_config(self):
171 self._test.vapi.gbp_subnet_add_del(
175 self.prefix.encode(),
176 sw_if_index=self.sw_if_index if self.sw_if_index else 0xffffffff,
177 epg_id=self.epg if self.epg else 0xffff)
178 self._test.registry.register(self, self._test.logger)
180 def remove_vpp_config(self):
181 self._test.vapi.gbp_subnet_add_del(
185 self.prefix.encode())
188 return self.object_id()
191 return "gbp-subnet;[%d-%s]" % (self.table_id,
194 def query_vpp_config(self):
195 ss = self._test.vapi.gbp_subnet_dump()
197 if s.subnet.table_id == self.table_id and \
198 s.subnet.prefix == self.prefix:
203 class VppGbpEndpointGroup(VppObject):
208 def __init__(self, test, epg, rd, bd, uplink,
209 bvi, bvi_ip4, bvi_ip6=None):
213 self.bvi_ip4 = VppIpAddress(bvi_ip4)
214 self.bvi_ip6 = VppIpAddress(bvi_ip6)
219 def add_vpp_config(self):
220 self._test.vapi.gbp_endpoint_group_add_del(
226 self.uplink.sw_if_index)
227 self._test.registry.register(self, self._test.logger)
229 def remove_vpp_config(self):
230 self._test.vapi.gbp_endpoint_group_add_del(
236 self.uplink.sw_if_index)
239 return self.object_id()
242 return "gbp-endpoint-group;[%d]" % (self.epg)
244 def query_vpp_config(self):
245 epgs = self._test.vapi.gbp_endpoint_group_dump()
247 if epg.epg.epg_id == self.epg:
252 class VppGbpContract(VppObject):
257 def __init__(self, test, src_epg, dst_epg, acl_index):
259 self.acl_index = acl_index
260 self.src_epg = src_epg
261 self.dst_epg = dst_epg
263 def add_vpp_config(self):
264 self._test.vapi.gbp_contract_add_del(
269 self._test.registry.register(self, self._test.logger)
271 def remove_vpp_config(self):
272 self._test.vapi.gbp_contract_add_del(
279 return self.object_id()
282 return "gbp-contract;[%d:%s:%d]" % (self.src_epg,
286 def query_vpp_config(self):
287 cs = self._test.vapi.gbp_contract_dump()
289 if c.contract.src_epg == self.src_epg \
290 and c.contract.dst_epg == self.dst_epg:
295 class VppGbpAcl(VppObject):
300 def __init__(self, test):
302 self.acl_index = 4294967295
304 def create_rule(self, is_ipv6=0, permit_deny=0, proto=-1,
305 s_prefix=0, s_ip='\x00\x00\x00\x00', sport_from=0,
306 sport_to=65535, d_prefix=0, d_ip='\x00\x00\x00\x00',
307 dport_from=0, dport_to=65535):
308 if proto == -1 or proto == 0:
311 elif proto == 1 or proto == 58:
314 rule = ({'is_permit': permit_deny, 'is_ipv6': is_ipv6, 'proto': proto,
315 'srcport_or_icmptype_first': sport_from,
316 'srcport_or_icmptype_last': sport_to,
317 'src_ip_prefix_len': s_prefix,
319 'dstport_or_icmpcode_first': dport_from,
320 'dstport_or_icmpcode_last': dport_to,
321 'dst_ip_prefix_len': d_prefix,
322 'dst_ip_addr': d_ip})
325 def add_vpp_config(self, rules):
327 reply = self._test.vapi.acl_add_replace(self.acl_index,
330 self.acl_index = reply.acl_index
331 return self.acl_index
333 def remove_vpp_config(self):
334 self._test.vapi.acl_del(self.acl_index)
337 return self.object_id()
340 return "gbp-acl;[%d]" % (self.acl_index)
342 def query_vpp_config(self):
343 cs = self._test.vapi.acl_dump()
345 if c.acl_index == self.acl_index:
350 class TestGBP(VppTestCase):
351 """ GBP Test Case """
354 super(TestGBP, self).setUp()
356 self.create_pg_interfaces(range(9))
357 self.create_loopback_interfaces(9)
359 self.router_mac = VppMacAddress("00:11:22:33:44:55")
361 for i in self.pg_interfaces:
363 for i in self.lo_interfaces:
365 self.vapi.sw_interface_set_mac_address(
367 self.router_mac.bytes)
370 for i in self.pg_interfaces:
373 super(TestGBP, self).tearDown()
375 def send_and_expect_bridged(self, src, tx, dst):
376 rx = self.send_and_expect(src, tx, dst)
379 self.assertEqual(r[Ether].src, tx[0][Ether].src)
380 self.assertEqual(r[Ether].dst, tx[0][Ether].dst)
381 self.assertEqual(r[IP].src, tx[0][IP].src)
382 self.assertEqual(r[IP].dst, tx[0][IP].dst)
385 def send_and_expect_bridged6(self, src, tx, dst):
386 rx = self.send_and_expect(src, tx, dst)
389 self.assertEqual(r[Ether].src, tx[0][Ether].src)
390 self.assertEqual(r[Ether].dst, tx[0][Ether].dst)
391 self.assertEqual(r[IPv6].src, tx[0][IPv6].src)
392 self.assertEqual(r[IPv6].dst, tx[0][IPv6].dst)
395 def send_and_expect_routed(self, src, tx, dst, src_mac):
396 rx = self.send_and_expect(src, tx, dst)
399 self.assertEqual(r[Ether].src, src_mac)
400 self.assertEqual(r[Ether].dst, dst.remote_mac)
401 self.assertEqual(r[IP].src, tx[0][IP].src)
402 self.assertEqual(r[IP].dst, tx[0][IP].dst)
405 def send_and_expect_natted(self, src, tx, dst, src_ip):
406 rx = self.send_and_expect(src, tx, dst)
409 self.assertEqual(r[Ether].src, tx[0][Ether].src)
410 self.assertEqual(r[Ether].dst, tx[0][Ether].dst)
411 self.assertEqual(r[IP].src, src_ip)
412 self.assertEqual(r[IP].dst, tx[0][IP].dst)
415 def send_and_expect_natted6(self, src, tx, dst, src_ip):
416 rx = self.send_and_expect(src, tx, dst)
419 self.assertEqual(r[Ether].src, tx[0][Ether].src)
420 self.assertEqual(r[Ether].dst, tx[0][Ether].dst)
421 self.assertEqual(r[IPv6].src, src_ip)
422 self.assertEqual(r[IPv6].dst, tx[0][IPv6].dst)
425 def send_and_expect_unnatted(self, src, tx, dst, dst_ip):
426 rx = self.send_and_expect(src, tx, dst)
429 self.assertEqual(r[Ether].src, tx[0][Ether].src)
430 self.assertEqual(r[Ether].dst, tx[0][Ether].dst)
431 self.assertEqual(r[IP].dst, dst_ip)
432 self.assertEqual(r[IP].src, tx[0][IP].src)
435 def send_and_expect_unnatted6(self, src, tx, dst, dst_ip):
436 rx = self.send_and_expect(src, tx, dst)
439 self.assertEqual(r[Ether].src, tx[0][Ether].src)
440 self.assertEqual(r[Ether].dst, tx[0][Ether].dst)
441 self.assertEqual(r[IPv6].dst, dst_ip)
442 self.assertEqual(r[IPv6].src, tx[0][IPv6].src)
445 def send_and_expect_double_natted(self, src, tx, dst, src_ip, dst_ip):
446 rx = self.send_and_expect(src, tx, dst)
449 self.assertEqual(r[Ether].src, self.router_mac.address)
450 self.assertEqual(r[Ether].dst, dst.remote_mac)
451 self.assertEqual(r[IP].dst, dst_ip)
452 self.assertEqual(r[IP].src, src_ip)
455 def send_and_expect_double_natted6(self, src, tx, dst, src_ip, dst_ip):
456 rx = self.send_and_expect(src, tx, dst)
459 self.assertEqual(r[Ether].src, self.router_mac.address)
460 self.assertEqual(r[Ether].dst, dst.remote_mac)
461 self.assertEqual(r[IPv6].dst, dst_ip)
462 self.assertEqual(r[IPv6].src, src_ip)
466 """ Group Based Policy """
468 nat_table = VppIpTable(self, 20)
469 nat_table.add_vpp_config()
470 nat_table = VppIpTable(self, 20, is_ip6=True)
471 nat_table.add_vpp_config()
476 self.vapi.bridge_domain_add_del(1, flood=1, uu_flood=1, forward=1,
477 learn=0, arp_term=1, is_add=1)
478 self.vapi.bridge_domain_add_del(2, flood=1, uu_flood=1, forward=1,
479 learn=0, arp_term=1, is_add=1)
480 self.vapi.bridge_domain_add_del(20, flood=1, uu_flood=1, forward=1,
481 learn=0, arp_term=1, is_add=1)
484 # 3 EPGs, 2 of which share a BD.
485 # 2 NAT EPGs, one for floating-IP subnets, the other for internet
487 epgs = [VppGbpEndpointGroup(self, 220, 0, 1, self.pg4,
491 VppGbpEndpointGroup(self, 221, 0, 1, self.pg5,
495 VppGbpEndpointGroup(self, 222, 0, 2, self.pg6,
499 VppGbpEndpointGroup(self, 333, 20, 20, self.pg7,
503 VppGbpEndpointGroup(self, 444, 20, 20, self.pg8,
507 recircs = [VppGbpRecirc(self, epgs[0],
509 VppGbpRecirc(self, epgs[1],
511 VppGbpRecirc(self, epgs[2],
513 VppGbpRecirc(self, epgs[3],
514 self.loop6, is_ext=True),
515 VppGbpRecirc(self, epgs[4],
516 self.loop8, is_ext=True)]
519 recirc_nat = recircs[3]
522 # 4 end-points, 2 in the same subnet, 3 in the same BD
524 eps = [VppGbpEndpoint(self, self.pg0,
526 "10.0.0.1", "11.0.0.1",
527 "2001:10::1", "3001::1"),
528 VppGbpEndpoint(self, self.pg1,
530 "10.0.0.2", "11.0.0.2",
531 "2001:10::2", "3001::2"),
532 VppGbpEndpoint(self, self.pg2,
534 "10.0.1.1", "11.0.0.3",
535 "2001:10:1::1", "3001::3"),
536 VppGbpEndpoint(self, self.pg3,
538 "10.0.2.1", "11.0.0.4",
539 "2001:10:2::1", "3001::4")]
542 # Config related to each of the EPGs
545 # IP config on the BVI interfaces
546 if epg != epgs[1] and epg != epgs[4]:
547 epg.bvi.set_table_ip4(epg.rd)
548 epg.bvi.set_table_ip6(epg.rd)
550 # The BVIs are NAT inside interfaces
551 self.vapi.nat44_interface_add_del_feature(epg.bvi.sw_if_index,
554 self.vapi.nat66_add_del_interface(epg.bvi.sw_if_index,
558 self.vapi.sw_interface_add_del_address(epg.bvi.sw_if_index,
561 self.vapi.sw_interface_add_del_address(epg.bvi.sw_if_index,
566 # EPG uplink interfaces in the BD
567 epg.uplink.set_table_ip4(epg.rd)
568 epg.uplink.set_table_ip6(epg.rd)
569 self.vapi.sw_interface_set_l2_bridge(epg.uplink.sw_if_index,
572 # add the BD ARP termination entry for BVI IP
573 self.vapi.bd_ip_mac_add_del(bd_id=epg.bd,
574 mac=self.router_mac.encode(),
575 ip=epg.bvi_ip4.encode(),
578 self.vapi.bd_ip_mac_add_del(bd_id=epg.bd,
579 mac=self.router_mac.encode(),
580 ip=epg.bvi_ip6.encode(),
584 # epg[1] shares the same BVI to epg[0]
585 if epg != epgs[1] and epg != epgs[4]:
587 self.vapi.sw_interface_set_l2_bridge(
590 port_type=L2_PORT_TYPE.BVI)
593 self.vapi.l2fib_add_del(self.router_mac.address,
601 for recirc in recircs:
602 # EPG's ingress recirculation interface maps to its RD
603 recirc.recirc.set_table_ip4(recirc.epg.rd)
604 recirc.recirc.set_table_ip6(recirc.epg.rd)
606 # in the bridge to allow DVR. L2 emulation to punt to L3
607 self.vapi.sw_interface_set_l2_bridge(recirc.recirc.sw_if_index,
609 self.vapi.sw_interface_set_l2_emulation(
610 recirc.recirc.sw_if_index)
612 self.vapi.nat44_interface_add_del_feature(
613 recirc.recirc.sw_if_index,
616 self.vapi.nat66_add_del_interface(
617 recirc.recirc.sw_if_index,
621 recirc.add_vpp_config()
626 self.pg_enable_capture(self.pg_interfaces)
629 # routes to the endpoints. We need these since there are no
630 # adj-fibs due to the fact the the BVI address has /32 and
631 # the subnet is not attached.
633 for (ip, fip) in zip(ep.ips, ep.fips):
634 r = VppIpRoute(self, ip.address, ip.length,
635 [VppRoutePath(ip.address,
636 ep.epg.bvi.sw_if_index,
637 proto=ip.dpo_proto)],
643 # ARP entries for the endpoints
645 a = VppNeighbor(self,
646 ep.epg.bvi.sw_if_index,
653 # add the BD ARP termination entry
654 self.vapi.bd_ip_mac_add_del(bd_id=ep.epg.bd,
655 mac=ep.vmac.encode(),
660 # Add static mappings for each EP from the 10/8 to 11/8 network
662 self.vapi.nat44_add_del_static_mapping(ip.bytes,
667 self.vapi.nat66_add_del_static_mapping(ip.bytes,
671 # add each EP itf to the its BD
672 self.vapi.sw_interface_set_l2_bridge(ep.itf.sw_if_index,
676 self.vapi.l2fib_add_del(ep.mac,
684 self.logger.info(self.vapi.cli("sh gbp endpoint"))
686 # ... results in a Gratuitous ARP/ND on the EPG's uplink
687 rx = ep.epg.uplink.get_capture(len(ep.ips), timeout=0.2)
689 for ii, ip in enumerate(ep.ips):
693 self.assertTrue(p.haslayer(ICMPv6ND_NA))
694 self.assertEqual(p[ICMPv6ND_NA].tgt, ip.address)
696 self.assertTrue(p.haslayer(ARP))
697 self.assertEqual(p[ARP].psrc, ip.address)
698 self.assertEqual(p[ARP].pdst, ip.address)
700 # add the BD ARP termination entry for floating IP
702 self.vapi.bd_ip_mac_add_del(bd_id=epg_nat.bd,
703 mac=ep.vmac.encode(),
708 # floating IPs route via EPG recirc
709 r = VppIpRoute(self, fip.address, fip.length,
710 [VppRoutePath(fip.address,
711 ep.recirc.recirc.sw_if_index,
713 proto=fip.dpo_proto)],
719 # L2 FIB entries in the NAT EPG BD to bridge the packets from
720 # the outside direct to the internal EPG
721 self.vapi.l2fib_add_del(ep.mac,
723 ep.recirc.recirc.sw_if_index,
727 # ARP packets for unknown IP are flooded
729 pkt_arp = (Ether(dst="ff:ff:ff:ff:ff:ff",
730 src=self.pg0.remote_mac) /
732 hwdst="ff:ff:ff:ff:ff:ff",
733 hwsrc=self.pg0.remote_mac,
734 pdst=epgs[0].bvi_ip4.address,
737 self.send_and_expect(self.pg0, [pkt_arp], self.pg0)
740 # ARP/ND packets get a response
742 pkt_arp = (Ether(dst="ff:ff:ff:ff:ff:ff",
743 src=self.pg0.remote_mac) /
745 hwdst="ff:ff:ff:ff:ff:ff",
746 hwsrc=self.pg0.remote_mac,
747 pdst=epgs[0].bvi_ip4.address,
748 psrc=eps[0].ip4.address))
750 self.send_and_expect(self.pg0, [pkt_arp], self.pg0)
752 nsma = in6_getnsma(inet_pton(AF_INET6, eps[0].ip6.address))
753 d = inet_ntop(AF_INET6, nsma)
754 pkt_nd = (Ether(dst=in6_getnsmac(nsma)) /
755 IPv6(dst=d, src=eps[0].ip6.address) /
756 ICMPv6ND_NS(tgt=epgs[0].bvi_ip6.address) /
757 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
758 self.send_and_expect(self.pg0, [pkt_nd], self.pg0)
761 # broadcast packets are flooded
763 pkt_bcast = (Ether(dst="ff:ff:ff:ff:ff:ff",
764 src=self.pg0.remote_mac) /
765 IP(src=eps[0].ip4.address, dst="232.1.1.1") /
766 UDP(sport=1234, dport=1234) /
769 self.vapi.cli("clear trace")
770 self.pg0.add_stream(pkt_bcast)
772 self.pg_enable_capture(self.pg_interfaces)
775 rxd = eps[1].itf.get_capture(1)
776 self.assertEqual(rxd[0][Ether].dst, pkt_bcast[Ether].dst)
777 rxd = epgs[0].uplink.get_capture(1)
778 self.assertEqual(rxd[0][Ether].dst, pkt_bcast[Ether].dst)
781 # packets to non-local L3 destinations dropped
783 pkt_intra_epg_220_ip4 = (Ether(src=self.pg0.remote_mac,
784 dst=self.router_mac.address) /
785 IP(src=eps[0].ip4.address,
787 UDP(sport=1234, dport=1234) /
789 pkt_inter_epg_222_ip4 = (Ether(src=self.pg0.remote_mac,
790 dst=self.router_mac.address) /
791 IP(src=eps[0].ip4.address,
793 UDP(sport=1234, dport=1234) /
796 self.send_and_assert_no_replies(self.pg0, pkt_intra_epg_220_ip4 * 65)
798 pkt_inter_epg_222_ip6 = (Ether(src=self.pg0.remote_mac,
799 dst=self.router_mac.address) /
800 IPv6(src=eps[0].ip6.address,
802 UDP(sport=1234, dport=1234) /
804 self.send_and_assert_no_replies(self.pg0, pkt_inter_epg_222_ip6 * 65)
807 # Add the subnet routes
809 s41 = VppGbpSubnet(self, 0, "10.0.0.0", 24)
810 s42 = VppGbpSubnet(self, 0, "10.0.1.0", 24)
811 s43 = VppGbpSubnet(self, 0, "10.0.2.0", 24)
815 s61 = VppGbpSubnet(self, 0, "2001:10::1", 64)
816 s62 = VppGbpSubnet(self, 0, "2001:10:1::1", 64)
817 s63 = VppGbpSubnet(self, 0, "2001:10:2::1", 64)
822 self.send_and_expect_bridged(self.pg0,
823 pkt_intra_epg_220_ip4 * 65,
825 self.send_and_expect_bridged(self.pg3,
826 pkt_inter_epg_222_ip4 * 65,
828 self.send_and_expect_bridged6(self.pg3,
829 pkt_inter_epg_222_ip6 * 65,
832 self.logger.info(self.vapi.cli("sh ip fib 11.0.0.2"))
833 self.logger.info(self.vapi.cli("sh gbp endpoint-group"))
834 self.logger.info(self.vapi.cli("sh gbp endpoint"))
835 self.logger.info(self.vapi.cli("sh gbp recirc"))
836 self.logger.info(self.vapi.cli("sh int"))
837 self.logger.info(self.vapi.cli("sh int addr"))
838 self.logger.info(self.vapi.cli("sh int feat loop6"))
839 self.logger.info(self.vapi.cli("sh vlib graph ip4-gbp-src-classify"))
840 self.logger.info(self.vapi.cli("sh int feat loop3"))
843 # Packet destined to unknown unicast is sent on the epg uplink ...
845 pkt_intra_epg_220_to_uplink = (Ether(src=self.pg0.remote_mac,
846 dst="00:00:00:33:44:55") /
847 IP(src=eps[0].ip4.address,
849 UDP(sport=1234, dport=1234) /
852 self.send_and_expect_bridged(self.pg0,
853 pkt_intra_epg_220_to_uplink * 65,
855 # ... and nowhere else
856 self.pg1.get_capture(0, timeout=0.1)
857 self.pg1.assert_nothing_captured(remark="Flood onto other VMS")
859 pkt_intra_epg_221_to_uplink = (Ether(src=self.pg2.remote_mac,
860 dst="00:00:00:33:44:66") /
861 IP(src=eps[0].ip4.address,
863 UDP(sport=1234, dport=1234) /
866 self.send_and_expect_bridged(self.pg2,
867 pkt_intra_epg_221_to_uplink * 65,
871 # Packets from the uplink are forwarded in the absence of a contract
873 pkt_intra_epg_220_from_uplink = (Ether(src="00:00:00:33:44:55",
874 dst=self.pg0.remote_mac) /
875 IP(src=eps[0].ip4.address,
877 UDP(sport=1234, dport=1234) /
880 self.send_and_expect_bridged(self.pg4,
881 pkt_intra_epg_220_from_uplink * 65,
885 # in the absence of policy, endpoints in the same EPG
888 pkt_intra_epg = (Ether(src=self.pg0.remote_mac,
889 dst=self.pg1.remote_mac) /
890 IP(src=eps[0].ip4.address,
891 dst=eps[1].ip4.address) /
892 UDP(sport=1234, dport=1234) /
895 self.send_and_expect_bridged(self.pg0, pkt_intra_epg * 65, self.pg1)
898 # in the abscense of policy, endpoints in the different EPG
901 pkt_inter_epg_220_to_221 = (Ether(src=self.pg0.remote_mac,
902 dst=self.pg2.remote_mac) /
903 IP(src=eps[0].ip4.address,
904 dst=eps[2].ip4.address) /
905 UDP(sport=1234, dport=1234) /
907 pkt_inter_epg_221_to_220 = (Ether(src=self.pg2.remote_mac,
908 dst=self.pg0.remote_mac) /
909 IP(src=eps[2].ip4.address,
910 dst=eps[0].ip4.address) /
911 UDP(sport=1234, dport=1234) /
913 pkt_inter_epg_220_to_222 = (Ether(src=self.pg0.remote_mac,
914 dst=self.router_mac.address) /
915 IP(src=eps[0].ip4.address,
916 dst=eps[3].ip4.address) /
917 UDP(sport=1234, dport=1234) /
920 self.send_and_assert_no_replies(self.pg0,
921 pkt_inter_epg_220_to_221 * 65)
922 self.send_and_assert_no_replies(self.pg0,
923 pkt_inter_epg_220_to_222 * 65)
926 # A uni-directional contract from EPG 220 -> 221
928 acl = VppGbpAcl(self)
929 rule = acl.create_rule(permit_deny=1, proto=17)
930 rule2 = acl.create_rule(is_ipv6=1, permit_deny=1, proto=17)
931 acl_index = acl.add_vpp_config([rule, rule2])
932 c1 = VppGbpContract(self, 220, 221, acl_index)
935 self.send_and_expect_bridged(self.pg0,
936 pkt_inter_epg_220_to_221 * 65,
938 self.send_and_assert_no_replies(self.pg0,
939 pkt_inter_epg_220_to_222 * 65)
942 # contract for the return direction
944 c2 = VppGbpContract(self, 221, 220, acl_index)
947 self.send_and_expect_bridged(self.pg0,
948 pkt_inter_epg_220_to_221 * 65,
950 self.send_and_expect_bridged(self.pg2,
951 pkt_inter_epg_221_to_220 * 65,
955 # check that inter group is still disabled for the groups
956 # not in the contract.
958 self.send_and_assert_no_replies(self.pg0,
959 pkt_inter_epg_220_to_222 * 65)
962 # A uni-directional contract from EPG 220 -> 222 'L3 routed'
964 c3 = VppGbpContract(self, 220, 222, acl_index)
967 self.logger.info(self.vapi.cli("sh gbp contract"))
969 self.send_and_expect_routed(self.pg0,
970 pkt_inter_epg_220_to_222 * 65,
972 self.router_mac.address)
975 # remove both contracts, traffic stops in both directions
977 c2.remove_vpp_config()
978 c1.remove_vpp_config()
979 c3.remove_vpp_config()
980 acl.remove_vpp_config()
982 self.send_and_assert_no_replies(self.pg2,
983 pkt_inter_epg_221_to_220 * 65)
984 self.send_and_assert_no_replies(self.pg0,
985 pkt_inter_epg_220_to_221 * 65)
986 self.send_and_expect_bridged(self.pg0, pkt_intra_epg * 65, self.pg1)
989 # EPs to the outside world
992 # in the EP's RD an external subnet via the NAT EPG's recirc
993 se1 = VppGbpSubnet(self, 0, "0.0.0.0", 0,
995 sw_if_index=recirc_nat.recirc.sw_if_index,
998 se2 = VppGbpSubnet(self, 0, "11.0.0.0", 8,
1000 sw_if_index=recirc_nat.recirc.sw_if_index,
1002 se2.add_vpp_config()
1003 se16 = VppGbpSubnet(self, 0, "::", 0,
1005 sw_if_index=recirc_nat.recirc.sw_if_index,
1007 se16.add_vpp_config()
1008 # in the NAT RD an external subnet via the NAT EPG's uplink
1009 se3 = VppGbpSubnet(self, 20, "0.0.0.0", 0,
1011 sw_if_index=epg_nat.uplink.sw_if_index,
1013 se36 = VppGbpSubnet(self, 20, "::", 0,
1015 sw_if_index=epg_nat.uplink.sw_if_index,
1017 se4 = VppGbpSubnet(self, 20, "11.0.0.0", 8,
1019 sw_if_index=epg_nat.uplink.sw_if_index,
1021 se3.add_vpp_config()
1022 se36.add_vpp_config()
1023 se4.add_vpp_config()
1025 self.logger.info(self.vapi.cli("sh ip fib 0.0.0.0/0"))
1026 self.logger.info(self.vapi.cli("sh ip fib 11.0.0.1"))
1027 self.logger.info(self.vapi.cli("sh ip6 fib ::/0"))
1028 self.logger.info(self.vapi.cli("sh ip6 fib %s" %
1032 # From an EP to an outside addess: IN2OUT
1034 pkt_inter_epg_220_to_global = (Ether(src=self.pg0.remote_mac,
1035 dst=self.router_mac.address) /
1036 IP(src=eps[0].ip4.address,
1038 UDP(sport=1234, dport=1234) /
1042 self.send_and_assert_no_replies(self.pg0,
1043 pkt_inter_epg_220_to_global * 65)
1045 acl2 = VppGbpAcl(self)
1046 rule = acl2.create_rule(permit_deny=1, proto=17, sport_from=1234,
1047 sport_to=1234, dport_from=1234, dport_to=1234)
1048 rule2 = acl2.create_rule(is_ipv6=1, permit_deny=1, proto=17,
1049 sport_from=1234, sport_to=1234,
1050 dport_from=1234, dport_to=1234)
1052 acl_index2 = acl2.add_vpp_config([rule, rule2])
1053 c4 = VppGbpContract(self, 220, 333, acl_index2)
1056 self.send_and_expect_natted(self.pg0,
1057 pkt_inter_epg_220_to_global * 65,
1059 eps[0].fip4.address)
1061 pkt_inter_epg_220_to_global = (Ether(src=self.pg0.remote_mac,
1062 dst=self.router_mac.address) /
1063 IPv6(src=eps[0].ip6.address,
1065 UDP(sport=1234, dport=1234) /
1068 self.send_and_expect_natted6(self.pg0,
1069 pkt_inter_epg_220_to_global * 65,
1071 eps[0].fip6.address)
1074 # From a global address to an EP: OUT2IN
1076 pkt_inter_epg_220_from_global = (Ether(src=self.router_mac.address,
1077 dst=self.pg0.remote_mac) /
1078 IP(dst=eps[0].fip4.address,
1080 UDP(sport=1234, dport=1234) /
1083 self.send_and_assert_no_replies(self.pg7,
1084 pkt_inter_epg_220_from_global * 65)
1086 c5 = VppGbpContract(self, 333, 220, acl_index2)
1089 self.send_and_expect_unnatted(self.pg7,
1090 pkt_inter_epg_220_from_global * 65,
1094 pkt_inter_epg_220_from_global = (Ether(src=self.router_mac.address,
1095 dst=self.pg0.remote_mac) /
1096 IPv6(dst=eps[0].fip6.address,
1098 UDP(sport=1234, dport=1234) /
1101 self.send_and_expect_unnatted6(self.pg7,
1102 pkt_inter_epg_220_from_global * 65,
1107 # From a local VM to another local VM using resp. public addresses:
1110 pkt_intra_epg_220_global = (Ether(src=self.pg0.remote_mac,
1111 dst=self.router_mac.address) /
1112 IP(src=eps[0].ip4.address,
1113 dst=eps[1].fip4.address) /
1114 UDP(sport=1234, dport=1234) /
1117 self.send_and_expect_double_natted(eps[0].itf,
1118 pkt_intra_epg_220_global * 65,
1120 eps[0].fip4.address,
1123 pkt_intra_epg_220_global = (Ether(src=self.pg0.remote_mac,
1124 dst=self.router_mac.address) /
1125 IPv6(src=eps[0].ip6.address,
1126 dst=eps[1].fip6.address) /
1127 UDP(sport=1234, dport=1234) /
1130 self.send_and_expect_double_natted6(eps[0].itf,
1131 pkt_intra_epg_220_global * 65,
1133 eps[0].fip6.address,
1140 # del static mappings for each EP from the 10/8 to 11/8 network
1141 self.vapi.nat44_add_del_static_mapping(ep.ip4.bytes,
1146 self.vapi.nat66_add_del_static_mapping(ep.ip6.bytes,
1152 # IP config on the BVI interfaces
1153 self.vapi.sw_interface_add_del_address(epg.bvi.sw_if_index,
1157 self.vapi.sw_interface_add_del_address(epg.bvi.sw_if_index,
1162 self.logger.info(self.vapi.cli("sh int addr"))
1164 epg.uplink.set_table_ip4(0)
1165 epg.uplink.set_table_ip6(0)
1167 if epg != epgs[0] and epg != epgs[3]:
1168 epg.bvi.set_table_ip4(0)
1169 epg.bvi.set_table_ip6(0)
1171 self.vapi.nat44_interface_add_del_feature(epg.bvi.sw_if_index,
1174 self.vapi.nat66_add_del_interface(epg.bvi.sw_if_index,
1178 for recirc in recircs:
1179 recirc.recirc.set_table_ip4(0)
1180 recirc.recirc.set_table_ip6(0)
1182 self.vapi.nat44_interface_add_del_feature(
1183 recirc.recirc.sw_if_index,
1186 self.vapi.nat66_add_del_interface(
1187 recirc.recirc.sw_if_index,
1192 if __name__ == '__main__':
1193 unittest.main(testRunner=VppTestRunner)