blob: aae5b3c1efbd36dff2ad8c0d29062ffbe423a4a9 [file] [log] [blame]
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08001import socket
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02002import unittest
3
4from scapy.layers.l2 import Ether
5from scapy.layers.inet import IP, ICMP
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -08006import six
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02007
8from framework import VppTestCase, VppTestRunner, running_extended_tests
9from remote_test import RemoteClass, RemoteVppTestCase
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -080010from vpp_memif import MEMIF_MODE, MEMIF_ROLE, remove_all_memif_vpp_config, \
11 VppSocketFilename, VppMemif
Neale Ranns097fa662018-05-01 05:17:55 -070012from vpp_ip_route import VppIpRoute, VppRoutePath
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020013
14
15class TestMemif(VppTestCase):
16 """ Memif Test Case """
17
18 @classmethod
19 def setUpClass(cls):
20 # fork new process before client connects to VPP
21 cls.remote_test = RemoteClass(RemoteVppTestCase)
22 cls.remote_test.start_remote()
23 cls.remote_test.set_request_timeout(10)
24 super(TestMemif, cls).setUpClass()
25 cls.remote_test.setUpClass(cls.tempdir)
26 cls.create_pg_interfaces(range(1))
27 for pg in cls.pg_interfaces:
28 pg.config_ip4()
29 pg.admin_up()
30 pg.resolve_arp()
31
32 @classmethod
33 def tearDownClass(cls):
34 cls.remote_test.tearDownClass()
35 cls.remote_test.quit_remote()
36 for pg in cls.pg_interfaces:
37 pg.unconfig_ip4()
38 pg.set_table_ip4(0)
39 pg.admin_down()
40 super(TestMemif, cls).tearDownClass()
41
42 def tearDown(self):
43 remove_all_memif_vpp_config(self)
44 remove_all_memif_vpp_config(self.remote_test)
45 super(TestMemif, self).tearDown()
46
47 def _check_socket_filename(self, dump, socket_id, filename):
48 for d in dump:
49 if (d.socket_id == socket_id) and (
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -080050 d.socket_filename.rstrip(b"\0") == filename):
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020051 return True
52 return False
53
54 def test_memif_socket_filename_add_del(self):
Neale Ranns097fa662018-05-01 05:17:55 -070055 """ Memif socket filename add/del """
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020056
57 # dump default socket filename
58 dump = self.vapi.memif_socket_filename_dump()
59 self.assertTrue(
60 self._check_socket_filename(
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -080061 dump, 0, b"%s/memif.sock" % six.ensure_binary(
62 self.tempdir, encoding='utf-8')))
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020063
64 memif_sockets = []
65 # existing path
66 memif_sockets.append(
67 VppSocketFilename(
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -080068 self, 1, b"%s/memif1.sock" % six.ensure_binary(
69 self.tempdir, encoding='utf-8')))
Jakub Grajciar99743912018-10-09 12:28:21 +020070 # default path (test tempdir)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020071 memif_sockets.append(
72 VppSocketFilename(
73 self,
74 2,
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -080075 b"memif2.sock",
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020076 add_default_folder=True))
77 # create new folder in default folder
78 memif_sockets.append(
79 VppSocketFilename(
80 self,
81 3,
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -080082 b"sock/memif3.sock",
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020083 add_default_folder=True))
84
85 for sock in memif_sockets:
86 sock.add_vpp_config()
87 dump = sock.query_vpp_config()
88 self.assertTrue(
89 self._check_socket_filename(
90 dump,
91 sock.socket_id,
92 sock.socket_filename))
93
94 for sock in memif_sockets:
95 sock.remove_vpp_config()
96
97 dump = self.vapi.memif_socket_filename_dump()
98 self.assertTrue(
99 self._check_socket_filename(
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800100 dump, 0, b"%s/memif.sock" % six.ensure_binary(
101 self.tempdir, encoding='utf-8')))
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200102
103 def _create_delete_test_one_interface(self, memif):
104 memif.add_vpp_config()
105
106 dump = memif.query_vpp_config()
107
108 self.assertTrue(dump)
109 self.assertEqual(dump.sw_if_index, memif.sw_if_index)
110 self.assertEqual(dump.role, memif.role)
111 self.assertEqual(dump.mode, memif.mode)
112 if (memif.socket_id is not None):
113 self.assertEqual(dump.socket_id, memif.socket_id)
114
115 memif.remove_vpp_config()
116
117 dump = memif.query_vpp_config()
118
119 self.assertFalse(dump)
120
121 def _connect_test_one_interface(self, memif):
122 self.assertTrue(memif.wait_for_link_up(5))
123 dump = memif.query_vpp_config()
124
125 if memif.role == MEMIF_ROLE.SLAVE:
126 self.assertEqual(dump.ring_size, memif.ring_size)
127 self.assertEqual(dump.buffer_size, memif.buffer_size)
128 else:
129 self.assertEqual(dump.ring_size, 1)
130 self.assertEqual(dump.buffer_size, 0)
131
132 def _connect_test_interface_pair(self, memif0, memif1):
133 memif0.add_vpp_config()
134 memif1.add_vpp_config()
135
136 memif0.admin_up()
137 memif1.admin_up()
138
139 self._connect_test_one_interface(memif0)
140 self._connect_test_one_interface(memif1)
141
142 memif0.remove_vpp_config()
143 memif1.remove_vpp_config()
144
145 def test_memif_create_delete(self):
146 """ Memif create/delete interface """
147
148 memif = VppMemif(self, MEMIF_ROLE.SLAVE, MEMIF_MODE.ETHERNET)
149 self._create_delete_test_one_interface(memif)
150 memif.role = MEMIF_ROLE.MASTER
151 self._create_delete_test_one_interface(memif)
152
153 def test_memif_create_custom_socket(self):
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800154 """ Memif create with non-default socket filename """
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200155
156 memif_sockets = []
157 # existing path
158 memif_sockets.append(
159 VppSocketFilename(
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800160 self, 1, b"%s/memif1.sock" % six.ensure_binary(
161 self.tempdir, encoding='utf-8')))
Jakub Grajciar99743912018-10-09 12:28:21 +0200162 # default path (test tempdir)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200163 memif_sockets.append(
164 VppSocketFilename(
165 self,
166 2,
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800167 b"memif2.sock",
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200168 add_default_folder=True))
169 # create new folder in default folder
170 memif_sockets.append(
171 VppSocketFilename(
172 self,
173 3,
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800174 b"sock/memif3.sock",
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200175 add_default_folder=True))
176
177 memif = VppMemif(self, MEMIF_ROLE.SLAVE, MEMIF_MODE.ETHERNET)
178
179 for sock in memif_sockets:
180 sock.add_vpp_config()
181 memif.socket_id = sock.socket_id
182 memif.role = MEMIF_ROLE.SLAVE
183 self._create_delete_test_one_interface(memif)
184 memif.role = MEMIF_ROLE.MASTER
185 self._create_delete_test_one_interface(memif)
186
187 def test_memif_connect(self):
188 """ Memif connect """
Jakub Grajciar99743912018-10-09 12:28:21 +0200189 memif = VppMemif(self, MEMIF_ROLE.SLAVE, MEMIF_MODE.ETHERNET,
190 ring_size=1024, buffer_size=2048)
191
192 remote_socket = VppSocketFilename(self.remote_test, 1,
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800193 b"%s/memif.sock" % six.ensure_binary(
194 self.tempdir, encoding='utf-8'))
Jakub Grajciar99743912018-10-09 12:28:21 +0200195 remote_socket.add_vpp_config()
196
197 remote_memif = VppMemif(self.remote_test, MEMIF_ROLE.MASTER,
198 MEMIF_MODE.ETHERNET, socket_id=1,
199 ring_size=1024, buffer_size=2048)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200200
201 self._connect_test_interface_pair(memif, remote_memif)
202
203 memif.role = MEMIF_ROLE.MASTER
204 remote_memif.role = MEMIF_ROLE.SLAVE
205
206 self._connect_test_interface_pair(memif, remote_memif)
207
208 def _create_icmp(self, pg, memif, num):
209 pkts = []
210 for i in range(num):
211 pkt = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000212 IP(src=pg.remote_ip4, dst=memif.ip4_addr) /
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200213 ICMP(id=memif.if_id, type='echo-request', seq=i))
214 pkts.append(pkt)
215 return pkts
216
217 def _verify_icmp(self, pg, memif, rx, seq):
218 ip = rx[IP]
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000219 self.assertEqual(ip.src, memif.ip4_addr)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200220 self.assertEqual(ip.dst, pg.remote_ip4)
221 self.assertEqual(ip.proto, 1)
222 icmp = rx[ICMP]
223 self.assertEqual(icmp.type, 0) # echo-reply
224 self.assertEqual(icmp.id, memif.if_id)
225 self.assertEqual(icmp.seq, seq)
226
227 def test_memif_ping(self):
228 """ Memif ping """
Neale Ranns097fa662018-05-01 05:17:55 -0700229
Jakub Grajciar99743912018-10-09 12:28:21 +0200230 memif = VppMemif(self, MEMIF_ROLE.SLAVE, MEMIF_MODE.ETHERNET)
231
232 remote_socket = VppSocketFilename(self.remote_test, 1,
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800233 b"%s/memif.sock" % six.ensure_binary(
234 self.tempdir, encoding='utf-8'))
Jakub Grajciar99743912018-10-09 12:28:21 +0200235 remote_socket.add_vpp_config()
236
237 remote_memif = VppMemif(self.remote_test, MEMIF_ROLE.MASTER,
238 MEMIF_MODE.ETHERNET, socket_id=1)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200239
240 memif.add_vpp_config()
241 memif.config_ip4()
242 memif.admin_up()
243
244 remote_memif.add_vpp_config()
245 remote_memif.config_ip4()
246 remote_memif.admin_up()
247
248 self.assertTrue(memif.wait_for_link_up(5))
249 self.assertTrue(remote_memif.wait_for_link_up(5))
250
251 # add routing to remote vpp
Jakub Grajciar7db35de2019-06-25 10:22:11 +0200252 route = VppIpRoute(self.remote_test, self.pg0._local_ip4_subnet, 24,
Paul Vinciguerrac118a222019-06-26 06:52:32 -0400253 [VppRoutePath(memif.ip4_addr, 0xffffffff)],
254 register=False)
Jakub Grajciar7db35de2019-06-25 10:22:11 +0200255
256 route.add_vpp_config()
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200257
258 # create ICMP echo-request from local pg to remote memif
259 packet_num = 10
260 pkts = self._create_icmp(self.pg0, remote_memif, packet_num)
261
262 self.pg0.add_stream(pkts)
263 self.pg_enable_capture(self.pg_interfaces)
264 self.pg_start()
265 capture = self.pg0.get_capture(packet_num, timeout=2)
266 seq = 0
267 for c in capture:
268 self._verify_icmp(self.pg0, remote_memif, c, seq)
269 seq += 1
270
Jakub Grajciar7db35de2019-06-25 10:22:11 +0200271 route.remove_vpp_config()
272
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200273
274if __name__ == '__main__':
275 unittest.main(testRunner=VppTestRunner)