6 from framework import VppTestCase, VppTestRunner
7 from vpp_ip import DpoProto
8 from vpp_ip_route import VppIpRoute, VppRoutePath
10 from scapy.layers.l2 import Ether, Raw
11 from scapy.layers.inet import IP, UDP, ICMP
12 from scapy.layers.inet6 import IPv6
15 class TestMAP(VppTestCase):
19 super(TestMAP, self).setUp()
21 # create 2 pg interfaces
22 self.create_pg_interfaces(range(4))
24 # pg0 is 'inside' IPv4
27 self.pg0.resolve_arp()
29 # pg1 is 'outside' IPv6
32 self.pg1.generate_remote_hosts(4)
33 self.pg1.configure_ipv6_neighbors()
36 super(TestMAP, self).tearDown()
37 for i in self.pg_interfaces:
42 def send_and_assert_encapped(self, tx, ip6_src, ip6_dst, dmac=None):
44 dmac = self.pg1.remote_mac
46 self.pg0.add_stream(tx)
48 self.pg_enable_capture(self.pg_interfaces)
51 rx = self.pg1.get_capture(1)
54 self.assertEqual(rx[Ether].dst, dmac)
55 self.assertEqual(rx[IP].src, tx[IP].src)
56 self.assertEqual(rx[IPv6].src, ip6_src)
57 self.assertEqual(rx[IPv6].dst, ip6_dst)
63 # Add a route to the MAP-BR
67 map_route = VppIpRoute(self,
70 [VppRoutePath(self.pg1.remote_ip6,
72 proto=DpoProto.DPO_PROTO_IP6)],
74 map_route.add_vpp_config()
77 # Add a domain that maps from pg0 to pg1
79 map_dst = socket.inet_pton(socket.AF_INET6, map_br_pfx)
81 map_src_n = socket.inet_pton(socket.AF_INET6, map_src)
82 client_pfx = socket.inet_pton(socket.AF_INET, "192.168.0.0")
84 self.vapi.map_add_domain(map_dst,
92 # Fire in a v4 packet that will be encapped to the BR
94 v4 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
95 IP(src=self.pg0.remote_ip4, dst='192.168.1.1') /
96 UDP(sport=20000, dport=10000) /
99 self.send_and_assert_encapped(v4, map_src, "2001::c0a8:0:0")
102 # Fire in a V6 encapped packet.
103 # expect a decapped packet on the inside ip4 link
105 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
106 IPv6(dst=map_src, src="2001::1") /
107 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
108 UDP(sport=20000, dport=10000) /
111 self.pg1.add_stream(p)
113 self.pg_enable_capture(self.pg_interfaces)
116 rx = self.pg0.get_capture(1)
119 self.assertFalse(rx.haslayer(IPv6))
120 self.assertEqual(rx[IP].src, p[IP].src)
121 self.assertEqual(rx[IP].dst, p[IP].dst)
124 # Pre-resolve. No API for this!!
126 self.vapi.ppcli("map params pre-resolve ip6-nh 4001::1")
128 self.send_and_assert_no_replies(self.pg0, v4,
129 "resovled via default route")
132 # Add a route to 4001::1. Expect the encapped traffic to be
133 # sent via that routes next-hop
135 pre_res_route = VppIpRoute(
136 self, "4001::1", 128,
137 [VppRoutePath(self.pg1.remote_hosts[2].ip6,
138 self.pg1.sw_if_index,
139 proto=DpoProto.DPO_PROTO_IP6)],
141 pre_res_route.add_vpp_config()
143 self.send_and_assert_encapped(v4, map_src,
145 dmac=self.pg1.remote_hosts[2].mac)
148 # change the route to the pre-solved next-hop
150 pre_res_route.modify([VppRoutePath(self.pg1.remote_hosts[3].ip6,
151 self.pg1.sw_if_index,
152 proto=DpoProto.DPO_PROTO_IP6)])
153 pre_res_route.add_vpp_config()
155 self.send_and_assert_encapped(v4, map_src,
157 dmac=self.pg1.remote_hosts[3].mac)
160 # cleanup. The test infra's object registry will ensure
161 # the route is really gone and thus that the unresolve worked.
163 pre_res_route.remove_vpp_config()
164 self.vapi.ppcli("map params pre-resolve del ip6-nh 4001::1")
166 if __name__ == '__main__':
167 unittest.main(testRunner=VppTestRunner)