test: consolidate the multiple versions of send_and_*
[vpp.git] / test / test_map.py
1 #!/usr/bin/env python
2
3 import unittest
4 import socket
5
6 from framework import VppTestCase, VppTestRunner
7 from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto
8
9 from scapy.layers.l2 import Ether, Raw
10 from scapy.layers.inet import IP, UDP, ICMP
11 from scapy.layers.inet6 import IPv6
12
13
14 class 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_encapped(self, tx, ip6_src, ip6_dst, dmac=None):
42         if not dmac:
43             dmac = self.pg1.remote_mac
44
45         self.pg0.add_stream(tx)
46
47         self.pg_enable_capture(self.pg_interfaces)
48         self.pg_start()
49
50         rx = self.pg1.get_capture(1)
51         rx = rx[0]
52
53         self.assertEqual(rx[Ether].dst, dmac)
54         self.assertEqual(rx[IP].src, tx[IP].src)
55         self.assertEqual(rx[IPv6].src, ip6_src)
56         self.assertEqual(rx[IPv6].dst, ip6_dst)
57
58     def test_map_e(self):
59         """ MAP-E """
60
61         #
62         # Add a route to the MAP-BR
63         #
64         map_br_pfx = "2001::"
65         map_br_pfx_len = 64
66         map_route = VppIpRoute(self,
67                                map_br_pfx,
68                                map_br_pfx_len,
69                                [VppRoutePath(self.pg1.remote_ip6,
70                                              self.pg1.sw_if_index,
71                                              proto=DpoProto.DPO_PROTO_IP6)],
72                                is_ip6=1)
73         map_route.add_vpp_config()
74
75         #
76         # Add a domain that maps from pg0 to pg1
77         #
78         map_dst = socket.inet_pton(socket.AF_INET6, map_br_pfx)
79         map_src = "3001::1"
80         map_src_n = socket.inet_pton(socket.AF_INET6, map_src)
81         client_pfx = socket.inet_pton(socket.AF_INET, "192.168.0.0")
82
83         self.vapi.map_add_domain(map_dst,
84                                  map_br_pfx_len,
85                                  map_src_n,
86                                  128,
87                                  client_pfx,
88                                  16)
89
90         #
91         # Fire in a v4 packet that will be encapped to the BR
92         #
93         v4 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
94               IP(src=self.pg0.remote_ip4, dst='192.168.1.1') /
95               UDP(sport=20000, dport=10000) /
96               Raw('\xa5' * 100))
97
98         self.send_and_assert_encapped(v4, map_src, "2001::c0a8:0:0")
99
100         #
101         # Fire in a V6 encapped packet.
102         #  expect a decapped packet on the inside ip4 link
103         #
104         p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
105              IPv6(dst=map_src, src="2001::1") /
106              IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
107              UDP(sport=20000, dport=10000) /
108              Raw('\xa5' * 100))
109
110         self.pg1.add_stream(p)
111
112         self.pg_enable_capture(self.pg_interfaces)
113         self.pg_start()
114
115         rx = self.pg0.get_capture(1)
116         rx = rx[0]
117
118         self.assertFalse(rx.haslayer(IPv6))
119         self.assertEqual(rx[IP].src, p[IP].src)
120         self.assertEqual(rx[IP].dst, p[IP].dst)
121
122         #
123         # Pre-resolve. No API for this!!
124         #
125         self.vapi.ppcli("map params pre-resolve ip6-nh 4001::1")
126
127         self.send_and_assert_no_replies(self.pg0, v4,
128                                         "resovled via default route")
129
130         #
131         # Add a route to 4001::1. Expect the encapped traffic to be
132         # sent via that routes next-hop
133         #
134         pre_res_route = VppIpRoute(
135             self, "4001::1", 128,
136             [VppRoutePath(self.pg1.remote_hosts[2].ip6,
137                           self.pg1.sw_if_index,
138                           proto=DpoProto.DPO_PROTO_IP6)],
139             is_ip6=1)
140         pre_res_route.add_vpp_config()
141
142         self.send_and_assert_encapped(v4, map_src,
143                                       "2001::c0a8:0:0",
144                                       dmac=self.pg1.remote_hosts[2].mac)
145
146         #
147         # change the route to the pre-solved next-hop
148         #
149         pre_res_route.modify([VppRoutePath(self.pg1.remote_hosts[3].ip6,
150                                            self.pg1.sw_if_index,
151                                            proto=DpoProto.DPO_PROTO_IP6)])
152         pre_res_route.add_vpp_config()
153
154         self.send_and_assert_encapped(v4, map_src,
155                                       "2001::c0a8:0:0",
156                                       dmac=self.pg1.remote_hosts[3].mac)
157
158         #
159         # cleanup. The test infra's object registry will ensure
160         # the route is really gone and thus that the unresolve worked.
161         #
162         pre_res_route.remove_vpp_config()
163         self.vapi.ppcli("map params pre-resolve del ip6-nh 4001::1")
164
165 if __name__ == '__main__':
166     unittest.main(testRunner=VppTestRunner)