6 from framework import VppTestCase, VppTestRunner
7 from vpp_ip_route import VppIpRoute, VppRoutePath
9 from scapy.layers.l2 import Ether, Raw
10 from scapy.layers.inet import IP, UDP, ICMP
11 from scapy.layers.inet6 import IPv6
14 class TestMAP(VppTestCase):
18 super(TestMAP, self).setUp()
20 # create 2 pg interfaces
21 self.create_pg_interfaces(range(4))
23 # pg0 is 'inside' IPv4
26 self.pg0.resolve_arp()
28 # pg1 is 'outside' IPv6
31 self.pg1.generate_remote_hosts(4)
32 self.pg1.configure_ipv6_neighbors()
35 super(TestMAP, self).tearDown()
36 for i in self.pg_interfaces:
41 def send_and_assert_no_replies(self, intf, pkts, remark):
43 self.pg_enable_capture(self.pg_interfaces)
45 for i in self.pg_interfaces:
46 i.assert_nothing_captured(remark=remark)
48 def send_and_assert_encapped(self, tx, ip6_src, ip6_dst, dmac=None):
50 dmac = self.pg1.remote_mac
52 self.pg0.add_stream(tx)
54 self.pg_enable_capture(self.pg_interfaces)
57 rx = self.pg1.get_capture(1)
60 self.assertEqual(rx[Ether].dst, dmac)
61 self.assertEqual(rx[IP].src, tx[IP].src)
62 self.assertEqual(rx[IPv6].src, ip6_src)
63 self.assertEqual(rx[IPv6].dst, ip6_dst)
65 @unittest.skip("Doesn't Work")
70 # Add a route to the MAP-BR
74 map_route = VppIpRoute(self,
77 [VppRoutePath(self.pg1.remote_ip6,
81 map_route.add_vpp_config()
84 # Add a domain that maps from pg0 to pg1
86 map_dst = socket.inet_pton(socket.AF_INET6, map_br_pfx)
88 map_src_n = socket.inet_pton(socket.AF_INET6, map_src)
89 client_pfx = socket.inet_pton(socket.AF_INET, "192.168.0.0")
91 self.vapi.map_add_domain(map_dst,
99 # Fire in a v4 packet that will be encapped to the BR
101 v4 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
102 IP(src=self.pg0.remote_ip4, dst='192.168.1.1') /
103 UDP(sport=20000, dport=10000) /
106 self.send_and_assert_encapped(v4, map_src, "2001::c0a8:0:0")
109 # Fire in a V6 encapped packet.
110 # expect a decapped packet on the inside ip4 link
112 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
113 IPv6(dst=map_src, src="2001::1") /
114 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
115 UDP(sport=20000, dport=10000) /
118 self.pg1.add_stream(p)
120 self.pg_enable_capture(self.pg_interfaces)
123 rx = self.pg0.get_capture(1)
126 self.assertFalse(rx.haslayer(IPv6))
127 self.assertEqual(rx[IP].src, p[IP].src)
128 self.assertEqual(rx[IP].dst, p[IP].dst)
131 # Pre-resolve. No API for this!!
133 self.vapi.ppcli("map params pre-resolve ip6-nh 4001::1")
135 self.send_and_assert_no_replies(self.pg0, v4,
136 "resovled via default route")
139 # Add a route to 4001::1. Expect the encapped traffic to be
140 # sent via that routes next-hop
142 pre_res_route = VppIpRoute(self,
145 [VppRoutePath(self.pg1.remote_hosts[2].ip6,
146 self.pg1.sw_if_index,
149 pre_res_route.add_vpp_config()
151 self.send_and_assert_encapped(v4, map_src,
153 dmac=self.pg1.remote_hosts[2].mac)
156 # change the route to the pre-solved next-hop
158 pre_res_route1 = VppIpRoute(self,
161 [VppRoutePath(self.pg1.remote_hosts[3].ip6,
162 self.pg1.sw_if_index,
165 pre_res_route1.add_vpp_config()
167 self.send_and_assert_encapped(v4, map_src,
169 dmac=self.pg1.remote_hosts[3].mac)
171 if __name__ == '__main__':
172 unittest.main(testRunner=VppTestRunner)