2 from socket import AF_INET, AF_INET6, inet_pton
4 from ipaddress import IPv4Network
6 from scapy.packet import Raw
7 from scapy.layers.l2 import Ether
8 from scapy.layers.inet import IP, UDP
10 from framework import VppTestCase, VppTestRunner
11 from vpp_interface import VppInterface
12 from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath
13 from vpp_acl import AclRule, VppAcl, VppAclInterface
18 class VppPipe(VppInterface):
25 return self.result.pipe_sw_if_index[1]
29 return self.result.pipe_sw_if_index[0]
31 def __init__(self, test, instance=0xffffffff):
32 super(VppPipe, self).__init__(test)
34 self.instance = instance
36 def add_vpp_config(self):
37 self.result = self._test.vapi.pipe_create(
38 0 if self.instance == 0xffffffff else 1,
40 self.set_sw_if_index(self.result.sw_if_index)
42 def remove_vpp_config(self):
43 self._test.vapi.pipe_delete(
44 self.result.sw_if_index)
47 return "pipe-%d" % (self._sw_if_index)
49 def query_vpp_config(self):
50 pipes = self._test.vapi.pipe_dump()
52 if p.sw_if_index == self.result.sw_if_index:
56 def set_unnumbered(self, ip_sw_if_index, is_east, is_add=True):
58 res = self._test.vapi.sw_interface_set_unnumbered(
59 ip_sw_if_index, self.east, is_add)
61 res = self._test.vapi.sw_interface_set_unnumbered(
62 ip_sw_if_index, self.west, is_add)
65 class TestPipe(VppTestCase):
70 super(TestPipe, cls).setUpClass()
73 def tearDownClass(cls):
74 super(TestPipe, cls).tearDownClass()
77 super(TestPipe, self).setUp()
79 self.create_pg_interfaces(range(4))
81 for i in self.pg_interfaces:
85 for i in self.pg_interfaces:
88 super(TestPipe, self).tearDown()
93 pipes = [VppPipe(self), VppPipe(self, 10)]
100 # L2 cross-connect pipe0 east with pg0 and west with pg1
102 self.vapi.sw_interface_set_l2_xconnect(self.pg0.sw_if_index,
105 self.vapi.sw_interface_set_l2_xconnect(pipes[0].east,
106 self.pg0.sw_if_index,
108 self.vapi.sw_interface_set_l2_xconnect(self.pg1.sw_if_index,
111 self.vapi.sw_interface_set_l2_xconnect(pipes[0].west,
112 self.pg1.sw_if_index,
115 # test bi-directional L2 flow pg0<->pg1
116 p = (Ether(src=self.pg0.remote_mac,
117 dst=self.pg1.remote_mac) /
120 UDP(sport=1234, dport=1234) /
123 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
124 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0)
127 # Attach ACL to ensure features are run on the pipe
129 rule_1 = AclRule(is_permit=0, proto=17,
130 src_prefix=IPv4Network("1.1.1.1/32"),
131 dst_prefix=IPv4Network("1.1.1.2/32"), ports=1234)
132 acl = VppAcl(self, rules=[rule_1])
135 # Apply the ACL on the pipe on output
136 acl_if_e = VppAclInterface(self, sw_if_index=pipes[0].east, n_input=0,
138 acl_if_e.add_vpp_config()
140 self.send_and_assert_no_replies(self.pg0, p * NUM_PKTS)
141 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0)
143 # remove from output and apply on input
144 acl_if_e.remove_vpp_config()
145 acl_if_w = VppAclInterface(self, sw_if_index=pipes[0].west, n_input=1,
147 acl_if_w.add_vpp_config()
149 self.send_and_assert_no_replies(self.pg0, p * NUM_PKTS)
150 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0)
152 acl_if_w.remove_vpp_config()
153 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
154 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0)
157 # L3 routes in two separate tables so a pipe can be used to L3
161 tables.append(VppIpTable(self, 1))
162 tables.append(VppIpTable(self, 2))
167 self.pg2.set_table_ip4(1)
168 self.pg2.config_ip4()
169 self.pg2.resolve_arp()
170 self.pg3.set_table_ip4(2)
171 self.pg3.config_ip4()
172 self.pg3.resolve_arp()
175 routes.append(VppIpRoute(self, "1.1.1.1", 32,
176 [VppRoutePath(self.pg3.remote_ip4,
177 self.pg3.sw_if_index)],
179 routes.append(VppIpRoute(self, "1.1.1.1", 32,
180 [VppRoutePath("0.0.0.0", pipes[1].east)],
182 routes.append(VppIpRoute(self, "1.1.1.2", 32,
183 [VppRoutePath("0.0.0.0", pipes[1].west)],
185 routes.append(VppIpRoute(self, "1.1.1.2", 32,
186 [VppRoutePath(self.pg2.remote_ip4,
187 self.pg2.sw_if_index)],
193 p_east = (Ether(src=self.pg2.remote_mac,
194 dst=self.pg2.local_mac) /
197 UDP(sport=1234, dport=1234) /
200 # bind the pipe ends to the correct tables
201 self.vapi.sw_interface_set_table(pipes[1].west, 0, 2)
202 self.vapi.sw_interface_set_table(pipes[1].east, 0, 1)
204 # IP is not enabled on the pipes at this point
205 self.send_and_assert_no_replies(self.pg2, p_east * NUM_PKTS)
207 # IP enable the Pipes by making them unnumbered
208 pipes[1].set_unnumbered(self.pg2.sw_if_index, True)
209 pipes[1].set_unnumbered(self.pg3.sw_if_index, False)
211 self.send_and_expect(self.pg2, p_east * NUM_PKTS, self.pg3)
213 # and the return path
214 p_west = (Ether(src=self.pg3.remote_mac,
215 dst=self.pg3.local_mac) /
218 UDP(sport=1234, dport=1234) /
220 self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2)
223 # Use ACLs to test features run on the Pipes
225 acl_if_e1 = VppAclInterface(self, sw_if_index=pipes[1].east, n_input=0,
227 acl_if_e1.add_vpp_config()
228 self.send_and_assert_no_replies(self.pg2, p_east * NUM_PKTS)
229 self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2)
231 # remove from output and apply on input
232 acl_if_e1.remove_vpp_config()
233 acl_if_w1 = VppAclInterface(self, sw_if_index=pipes[1].west, n_input=1,
235 acl_if_w1.add_vpp_config()
236 self.send_and_assert_no_replies(self.pg2, p_east * NUM_PKTS)
237 self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2)
238 acl_if_w1.remove_vpp_config()
240 self.send_and_expect(self.pg2, p_east * NUM_PKTS, self.pg3)
241 self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2)
243 # cleanup (so the tables delete)
244 self.pg2.unconfig_ip4()
245 self.pg2.set_table_ip4(0)
246 self.pg3.unconfig_ip4()
247 self.pg3.set_table_ip4(0)
248 self.vapi.sw_interface_set_table(pipes[1].west, 0, 0)
249 self.vapi.sw_interface_set_table(pipes[1].east, 0, 0)
252 if __name__ == '__main__':
253 unittest.main(testRunner=VppTestRunner)