2c23bd04748fbd65696a228c91899cefd4fada54
[vpp.git] / test / test_pipe.py
1 #!/usr/bin/env python
2 from socket import AF_INET, AF_INET6, inet_pton
3
4 from framework import VppTestCase, VppTestRunner
5 from vpp_interface import VppInterface
6 from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath
7
8 from scapy.packet import Raw
9 from scapy.layers.l2 import Ether
10 from scapy.layers.inet import IP, UDP
11
12
13 class VppPipe(VppInterface):
14     """
15     VPP Pipe
16     """
17
18     @property
19     def east(self):
20         return self.result.pipe_sw_if_index[1]
21
22     @property
23     def west(self):
24         return self.result.pipe_sw_if_index[0]
25
26     def __init__(self, test, instance=0xffffffff):
27         super(VppPipe, self).__init__(test)
28         self._test = test
29         self.instance = instance
30
31     def add_vpp_config(self):
32         self.result = self._test.vapi.pipe_create(
33             0 if self.instance == 0xffffffff else 1,
34             self.instance)
35         self.set_sw_if_index(self.result.sw_if_index)
36
37     def remove_vpp_config(self):
38         self._test.vapi.pipe_delete(
39             self.result.sw_if_index)
40
41     def object_id(self):
42         return "pipe-%d" % (self._sw_if_index)
43
44     def query_vpp_config(self):
45         pipes = self._test.vapi.pipe_dump()
46         for p in pipes:
47             if p.sw_if_index == self.result.sw_if_index:
48                 return True
49         return False
50
51     def set_unnumbered(self, ip_sw_if_index, is_add=True):
52         res = self._test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
53                                                           self.east, is_add)
54         res = self._test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
55                                                           self.west, is_add)
56
57
58 class TestPipe(VppTestCase):
59     """ Pipes """
60
61     def setUp(self):
62         super(TestPipe, self).setUp()
63
64         self.create_pg_interfaces(range(4))
65
66         for i in self.pg_interfaces:
67             i.admin_up()
68
69     def tearDown(self):
70         for i in self.pg_interfaces:
71             i.admin_down()
72
73         super(TestPipe, self).tearDown()
74
75     def test_pipe(self):
76         """ Pipes """
77
78         pipes = [VppPipe(self), VppPipe(self, 10)]
79
80         for p in pipes:
81             p.add_vpp_config()
82             p.admin_up()
83
84         #
85         # L2 cross-connect pipe0 east with pg0 and west with pg1
86         #
87         self.vapi.sw_interface_set_l2_xconnect(self.pg0.sw_if_index,
88                                                pipes[0].east,
89                                                enable=1)
90         self.vapi.sw_interface_set_l2_xconnect(pipes[0].east,
91                                                self.pg0.sw_if_index,
92                                                enable=1)
93         self.vapi.sw_interface_set_l2_xconnect(self.pg1.sw_if_index,
94                                                pipes[0].west,
95                                                enable=1)
96         self.vapi.sw_interface_set_l2_xconnect(pipes[0].west,
97                                                self.pg1.sw_if_index,
98                                                enable=1)
99
100         # test bi-directional L2 flow pg0<->pg1
101         p = (Ether(src=self.pg0.remote_mac,
102                    dst=self.pg1.remote_mac) /
103              IP(src="1.1.1.1",
104                 dst="1.1.1.2") /
105              UDP(sport=1234, dport=1234) /
106              Raw('\xa5' * 100))
107
108         self.send_and_expect(self.pg0, p * 65, self.pg1)
109         self.send_and_expect(self.pg1, p * 65, self.pg0)
110
111         #
112         # Attach ACL to ensure features are run on the pipe
113         #
114         rule_1 = ({'is_permit': 0,
115                    'is_ipv6': 0,
116                    'proto': 17,
117                    'srcport_or_icmptype_first': 1234,
118                    'srcport_or_icmptype_last': 1234,
119                    'src_ip_prefix_len': 32,
120                    'src_ip_addr': inet_pton(AF_INET, "1.1.1.1"),
121                    'dstport_or_icmpcode_first': 1234,
122                    'dstport_or_icmpcode_last': 1234,
123                    'dst_ip_prefix_len': 32,
124                    'dst_ip_addr': inet_pton(AF_INET, "1.1.1.2")})
125         acl = self.vapi.acl_add_replace(acl_index=4294967295,
126                                         r=[rule_1])
127
128         # Apply the ACL on the pipe on output
129         self.vapi.acl_interface_set_acl_list(pipes[0].east,
130                                              0,
131                                              [acl.acl_index])
132         self.send_and_assert_no_replies(self.pg0, p * 65)
133         self.send_and_expect(self.pg1, p * 65, self.pg0)
134
135         # remove from output and apply on input
136         self.vapi.acl_interface_set_acl_list(pipes[0].east,
137                                              0,
138                                              [])
139         self.vapi.acl_interface_set_acl_list(pipes[0].west,
140                                              1,
141                                              [acl.acl_index])
142         self.send_and_assert_no_replies(self.pg0, p * 65)
143         self.send_and_expect(self.pg1, p * 65, self.pg0)
144         self.vapi.acl_interface_set_acl_list(pipes[0].west,
145                                              0,
146                                              [])
147         self.send_and_expect(self.pg0, p * 65, self.pg1)
148         self.send_and_expect(self.pg1, p * 65, self.pg0)
149
150         #
151         # L3 routes in two separate tables so a pipe can be used to L3
152         # x-connect
153         #
154         tables = []
155         tables.append(VppIpTable(self, 1))
156         tables.append(VppIpTable(self, 2))
157
158         for t in tables:
159             t.add_vpp_config()
160
161         self.pg2.set_table_ip4(1)
162         self.pg2.config_ip4()
163         self.pg2.resolve_arp()
164         self.pg3.set_table_ip4(2)
165         self.pg3.config_ip4()
166         self.pg3.resolve_arp()
167
168         routes = []
169         routes.append(VppIpRoute(self, "1.1.1.1", 32,
170                                  [VppRoutePath(self.pg3.remote_ip4,
171                                                self.pg3.sw_if_index)],
172                                  table_id=2))
173         routes.append(VppIpRoute(self, "1.1.1.1", 32,
174                                  [VppRoutePath("0.0.0.0", pipes[1].east)],
175                                  table_id=1))
176         routes.append(VppIpRoute(self, "1.1.1.2", 32,
177                                  [VppRoutePath("0.0.0.0", pipes[1].west)],
178                                  table_id=2))
179         routes.append(VppIpRoute(self, "1.1.1.2", 32,
180                                  [VppRoutePath(self.pg2.remote_ip4,
181                                                self.pg2.sw_if_index)],
182                                  table_id=1))
183
184         for r in routes:
185             r.add_vpp_config()
186
187         p_east = (Ether(src=self.pg2.remote_mac,
188                         dst=self.pg2.local_mac) /
189                   IP(src="1.1.1.2",
190                      dst="1.1.1.1") /
191                   UDP(sport=1234, dport=1234) /
192                   Raw('\xa5' * 100))
193
194         # bind the pipe ends to the correct tables
195         self.vapi.sw_interface_set_table(pipes[1].west, 0, 2)
196         self.vapi.sw_interface_set_table(pipes[1].east, 0, 1)
197
198         # IP is not enabled on the pipes at this point
199         self.send_and_assert_no_replies(self.pg2, p_east * 65)
200
201         # IP enable the Pipes by making them unnumbered
202         pipes[0].set_unnumbered(self.pg2.sw_if_index)
203         pipes[1].set_unnumbered(self.pg3.sw_if_index)
204
205         self.send_and_expect(self.pg2, p_east * 65, self.pg3)
206
207         # and the return path
208         p_west = (Ether(src=self.pg3.remote_mac,
209                         dst=self.pg3.local_mac) /
210                   IP(src="1.1.1.1",
211                      dst="1.1.1.2") /
212                   UDP(sport=1234, dport=1234) /
213                   Raw('\xa5' * 100))
214         self.send_and_expect(self.pg3, p_west * 65, self.pg2)
215
216         #
217         # Use ACLs to test features run on the Pipes
218         #
219         self.vapi.acl_interface_set_acl_list(pipes[1].east,
220                                              0,
221                                              [acl.acl_index])
222         self.send_and_assert_no_replies(self.pg2, p_east * 65)
223         self.send_and_expect(self.pg3, p_west * 65, self.pg2)
224
225         # remove from output and apply on input
226         self.vapi.acl_interface_set_acl_list(pipes[1].east,
227                                              0,
228                                              [])
229         self.vapi.acl_interface_set_acl_list(pipes[1].west,
230                                              1,
231                                              [acl.acl_index])
232         self.send_and_assert_no_replies(self.pg2, p_east * 65)
233         self.send_and_expect(self.pg3, p_west * 65, self.pg2)
234         self.vapi.acl_interface_set_acl_list(pipes[1].west,
235                                              0,
236                                              [])
237         self.send_and_expect(self.pg2, p_east * 65, self.pg3)
238         self.send_and_expect(self.pg3, p_west * 65, self.pg2)
239
240         # cleanup (so the tables delete)
241         self.pg2.unconfig_ip4()
242         self.pg2.set_table_ip4(0)
243         self.pg3.unconfig_ip4()
244         self.pg3.set_table_ip4(0)
245         self.vapi.sw_interface_set_table(pipes[1].west, 0, 0)
246         self.vapi.sw_interface_set_table(pipes[1].east, 0, 0)
247
248
249 if __name__ == '__main__':
250     unittest.main(testRunner=VppTestRunner)