blob: 1a53492f1365ae234d31bb79ceb1f94b8bb131b4 [file] [log] [blame]
Neale Ranns80823802017-02-20 18:23:41 -08001#!/usr/bin/env python
2
3import unittest
4import socket
5
6from framework import VppTestCase, VppTestRunner
Neale Rannsc0a93142018-09-05 15:42:26 -07007from vpp_ip import DpoProto
8from vpp_ip_route import VppIpRoute, VppRoutePath
Neale Ranns80823802017-02-20 18:23:41 -08009
10from scapy.layers.l2 import Ether, Raw
11from scapy.layers.inet import IP, UDP, ICMP
12from scapy.layers.inet6 import IPv6
13
14
15class TestMAP(VppTestCase):
16 """ MAP Test Case """
17
18 def setUp(self):
19 super(TestMAP, self).setUp()
20
21 # create 2 pg interfaces
22 self.create_pg_interfaces(range(4))
23
24 # pg0 is 'inside' IPv4
25 self.pg0.admin_up()
26 self.pg0.config_ip4()
27 self.pg0.resolve_arp()
28
29 # pg1 is 'outside' IPv6
30 self.pg1.admin_up()
31 self.pg1.config_ip6()
32 self.pg1.generate_remote_hosts(4)
33 self.pg1.configure_ipv6_neighbors()
34
35 def tearDown(self):
36 super(TestMAP, self).tearDown()
37 for i in self.pg_interfaces:
38 i.unconfig_ip4()
39 i.unconfig_ip6()
40 i.admin_down()
41
Neale Ranns80823802017-02-20 18:23:41 -080042 def send_and_assert_encapped(self, tx, ip6_src, ip6_dst, dmac=None):
43 if not dmac:
44 dmac = self.pg1.remote_mac
45
46 self.pg0.add_stream(tx)
47
48 self.pg_enable_capture(self.pg_interfaces)
49 self.pg_start()
50
51 rx = self.pg1.get_capture(1)
52 rx = rx[0]
53
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)
58
Neale Ranns80823802017-02-20 18:23:41 -080059 def test_map_e(self):
60 """ MAP-E """
61
62 #
63 # Add a route to the MAP-BR
64 #
65 map_br_pfx = "2001::"
66 map_br_pfx_len = 64
67 map_route = VppIpRoute(self,
68 map_br_pfx,
69 map_br_pfx_len,
70 [VppRoutePath(self.pg1.remote_ip6,
71 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -070072 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns80823802017-02-20 18:23:41 -080073 is_ip6=1)
74 map_route.add_vpp_config()
75
76 #
77 # Add a domain that maps from pg0 to pg1
78 #
79 map_dst = socket.inet_pton(socket.AF_INET6, map_br_pfx)
80 map_src = "3001::1"
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")
83
84 self.vapi.map_add_domain(map_dst,
85 map_br_pfx_len,
86 map_src_n,
87 128,
88 client_pfx,
89 16)
90
91 #
92 # Fire in a v4 packet that will be encapped to the BR
93 #
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) /
97 Raw('\xa5' * 100))
98
99 self.send_and_assert_encapped(v4, map_src, "2001::c0a8:0:0")
100
101 #
102 # Fire in a V6 encapped packet.
103 # expect a decapped packet on the inside ip4 link
104 #
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) /
109 Raw('\xa5' * 100))
110
111 self.pg1.add_stream(p)
112
113 self.pg_enable_capture(self.pg_interfaces)
114 self.pg_start()
115
116 rx = self.pg0.get_capture(1)
117 rx = rx[0]
118
119 self.assertFalse(rx.haslayer(IPv6))
120 self.assertEqual(rx[IP].src, p[IP].src)
121 self.assertEqual(rx[IP].dst, p[IP].dst)
122
123 #
124 # Pre-resolve. No API for this!!
125 #
126 self.vapi.ppcli("map params pre-resolve ip6-nh 4001::1")
127
128 self.send_and_assert_no_replies(self.pg0, v4,
129 "resovled via default route")
130
131 #
132 # Add a route to 4001::1. Expect the encapped traffic to be
133 # sent via that routes next-hop
134 #
Neale Rannsda78f952017-05-24 09:15:43 -0700135 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)],
140 is_ip6=1)
Neale Ranns80823802017-02-20 18:23:41 -0800141 pre_res_route.add_vpp_config()
142
143 self.send_and_assert_encapped(v4, map_src,
144 "2001::c0a8:0:0",
145 dmac=self.pg1.remote_hosts[2].mac)
146
147 #
148 # change the route to the pre-solved next-hop
149 #
Neale Ranns69b7aa42017-03-10 03:04:12 -0800150 pre_res_route.modify([VppRoutePath(self.pg1.remote_hosts[3].ip6,
151 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700152 proto=DpoProto.DPO_PROTO_IP6)])
Neale Ranns69b7aa42017-03-10 03:04:12 -0800153 pre_res_route.add_vpp_config()
Neale Ranns80823802017-02-20 18:23:41 -0800154
155 self.send_and_assert_encapped(v4, map_src,
156 "2001::c0a8:0:0",
157 dmac=self.pg1.remote_hosts[3].mac)
158
Neale Ranns69b7aa42017-03-10 03:04:12 -0800159 #
160 # cleanup. The test infra's object registry will ensure
161 # the route is really gone and thus that the unresolve worked.
162 #
163 pre_res_route.remove_vpp_config()
164 self.vapi.ppcli("map params pre-resolve del ip6-nh 4001::1")
165
Neale Ranns80823802017-02-20 18:23:41 -0800166if __name__ == '__main__':
167 unittest.main(testRunner=VppTestRunner)