blob: 9ac3948abf40c2f86a629759f97412a187f96854 [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
7from vpp_ip_route import VppIpRoute, VppRoutePath
8
9from scapy.layers.l2 import Ether, Raw
10from scapy.layers.inet import IP, UDP, ICMP
11from scapy.layers.inet6 import IPv6
12
13
14class TestMAP(VppTestCase):
15 """ MAP Test Case """
16
17 def setUp(self):
18 super(TestMAP, self).setUp()
19
20 # create 2 pg interfaces
21 self.create_pg_interfaces(range(4))
22
23 # pg0 is 'inside' IPv4
24 self.pg0.admin_up()
25 self.pg0.config_ip4()
26 self.pg0.resolve_arp()
27
28 # pg1 is 'outside' IPv6
29 self.pg1.admin_up()
30 self.pg1.config_ip6()
31 self.pg1.generate_remote_hosts(4)
32 self.pg1.configure_ipv6_neighbors()
33
34 def tearDown(self):
35 super(TestMAP, self).tearDown()
36 for i in self.pg_interfaces:
37 i.unconfig_ip4()
38 i.unconfig_ip6()
39 i.admin_down()
40
41 def send_and_assert_no_replies(self, intf, pkts, remark):
42 intf.add_stream(pkts)
43 self.pg_enable_capture(self.pg_interfaces)
44 self.pg_start()
45 for i in self.pg_interfaces:
46 i.assert_nothing_captured(remark=remark)
47
48 def send_and_assert_encapped(self, tx, ip6_src, ip6_dst, dmac=None):
49 if not dmac:
50 dmac = self.pg1.remote_mac
51
52 self.pg0.add_stream(tx)
53
54 self.pg_enable_capture(self.pg_interfaces)
55 self.pg_start()
56
57 rx = self.pg1.get_capture(1)
58 rx = rx[0]
59
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)
64
Neale Ranns80823802017-02-20 18:23:41 -080065 def test_map_e(self):
66 """ MAP-E """
67
68 #
69 # Add a route to the MAP-BR
70 #
71 map_br_pfx = "2001::"
72 map_br_pfx_len = 64
73 map_route = VppIpRoute(self,
74 map_br_pfx,
75 map_br_pfx_len,
76 [VppRoutePath(self.pg1.remote_ip6,
77 self.pg1.sw_if_index,
78 is_ip6=1)],
79 is_ip6=1)
80 map_route.add_vpp_config()
81
82 #
83 # Add a domain that maps from pg0 to pg1
84 #
85 map_dst = socket.inet_pton(socket.AF_INET6, map_br_pfx)
86 map_src = "3001::1"
87 map_src_n = socket.inet_pton(socket.AF_INET6, map_src)
88 client_pfx = socket.inet_pton(socket.AF_INET, "192.168.0.0")
89
90 self.vapi.map_add_domain(map_dst,
91 map_br_pfx_len,
92 map_src_n,
93 128,
94 client_pfx,
95 16)
96
97 #
98 # Fire in a v4 packet that will be encapped to the BR
99 #
100 v4 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
101 IP(src=self.pg0.remote_ip4, dst='192.168.1.1') /
102 UDP(sport=20000, dport=10000) /
103 Raw('\xa5' * 100))
104
105 self.send_and_assert_encapped(v4, map_src, "2001::c0a8:0:0")
106
107 #
108 # Fire in a V6 encapped packet.
109 # expect a decapped packet on the inside ip4 link
110 #
111 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
112 IPv6(dst=map_src, src="2001::1") /
113 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
114 UDP(sport=20000, dport=10000) /
115 Raw('\xa5' * 100))
116
117 self.pg1.add_stream(p)
118
119 self.pg_enable_capture(self.pg_interfaces)
120 self.pg_start()
121
122 rx = self.pg0.get_capture(1)
123 rx = rx[0]
124
125 self.assertFalse(rx.haslayer(IPv6))
126 self.assertEqual(rx[IP].src, p[IP].src)
127 self.assertEqual(rx[IP].dst, p[IP].dst)
128
129 #
130 # Pre-resolve. No API for this!!
131 #
132 self.vapi.ppcli("map params pre-resolve ip6-nh 4001::1")
133
134 self.send_and_assert_no_replies(self.pg0, v4,
135 "resovled via default route")
136
137 #
138 # Add a route to 4001::1. Expect the encapped traffic to be
139 # sent via that routes next-hop
140 #
141 pre_res_route = VppIpRoute(self,
142 "4001::1",
143 128,
144 [VppRoutePath(self.pg1.remote_hosts[2].ip6,
145 self.pg1.sw_if_index,
146 is_ip6=1)],
147 is_ip6=1)
148 pre_res_route.add_vpp_config()
149
150 self.send_and_assert_encapped(v4, map_src,
151 "2001::c0a8:0:0",
152 dmac=self.pg1.remote_hosts[2].mac)
153
154 #
155 # change the route to the pre-solved next-hop
156 #
Neale Ranns69b7aa42017-03-10 03:04:12 -0800157 pre_res_route.modify([VppRoutePath(self.pg1.remote_hosts[3].ip6,
158 self.pg1.sw_if_index,
159 is_ip6=1)])
160 pre_res_route.add_vpp_config()
Neale Ranns80823802017-02-20 18:23:41 -0800161
162 self.send_and_assert_encapped(v4, map_src,
163 "2001::c0a8:0:0",
164 dmac=self.pg1.remote_hosts[3].mac)
165
Neale Ranns69b7aa42017-03-10 03:04:12 -0800166 #
167 # cleanup. The test infra's object registry will ensure
168 # the route is really gone and thus that the unresolve worked.
169 #
170 pre_res_route.remove_vpp_config()
171 self.vapi.ppcli("map params pre-resolve del ip6-nh 4001::1")
172
Neale Ranns80823802017-02-20 18:23:41 -0800173if __name__ == '__main__':
174 unittest.main(testRunner=VppTestRunner)