blob: 8fe229986aa4be7de0a6f8b95126998785d181ba [file] [log] [blame]
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02001import unittest
2
3from scapy.layers.l2 import Ether
4from scapy.layers.inet import IP, ICMP
5
6from framework import VppTestCase, VppTestRunner, running_extended_tests
7from remote_test import RemoteClass, RemoteVppTestCase
8from vpp_memif import *
9
10
11class TestMemif(VppTestCase):
12 """ Memif Test Case """
13
14 @classmethod
15 def setUpClass(cls):
16 # fork new process before client connects to VPP
17 cls.remote_test = RemoteClass(RemoteVppTestCase)
18 cls.remote_test.start_remote()
19 cls.remote_test.set_request_timeout(10)
20 super(TestMemif, cls).setUpClass()
21 cls.remote_test.setUpClass(cls.tempdir)
22 cls.create_pg_interfaces(range(1))
23 for pg in cls.pg_interfaces:
24 pg.config_ip4()
25 pg.admin_up()
26 pg.resolve_arp()
27
28 @classmethod
29 def tearDownClass(cls):
30 cls.remote_test.tearDownClass()
31 cls.remote_test.quit_remote()
32 for pg in cls.pg_interfaces:
33 pg.unconfig_ip4()
34 pg.set_table_ip4(0)
35 pg.admin_down()
36 super(TestMemif, cls).tearDownClass()
37
38 def tearDown(self):
39 remove_all_memif_vpp_config(self)
40 remove_all_memif_vpp_config(self.remote_test)
41 super(TestMemif, self).tearDown()
42
43 def _check_socket_filename(self, dump, socket_id, filename):
44 for d in dump:
45 if (d.socket_id == socket_id) and (
46 d.socket_filename.rstrip("\0") == filename):
47 return True
48 return False
49
50 def test_memif_socket_filename_add_del(self):
51 """ Memif socket filenale add/del """
52
53 # dump default socket filename
54 dump = self.vapi.memif_socket_filename_dump()
55 self.assertTrue(
56 self._check_socket_filename(
57 dump, 0, "/run/vpp/memif.sock"))
58
59 memif_sockets = []
60 # existing path
61 memif_sockets.append(
62 VppSocketFilename(
63 self, 1, "/run/vpp/memif1.sock"))
64 # default path ("/run/vpp")
65 memif_sockets.append(
66 VppSocketFilename(
67 self,
68 2,
69 "memif2.sock",
70 add_default_folder=True))
71 # create new folder in default folder
72 memif_sockets.append(
73 VppSocketFilename(
74 self,
75 3,
76 "sock/memif3.sock",
77 add_default_folder=True))
78
79 for sock in memif_sockets:
80 sock.add_vpp_config()
81 dump = sock.query_vpp_config()
82 self.assertTrue(
83 self._check_socket_filename(
84 dump,
85 sock.socket_id,
86 sock.socket_filename))
87
88 for sock in memif_sockets:
89 sock.remove_vpp_config()
90
91 dump = self.vapi.memif_socket_filename_dump()
92 self.assertTrue(
93 self._check_socket_filename(
94 dump, 0, "/run/vpp/memif.sock"))
95
96 def _create_delete_test_one_interface(self, memif):
97 memif.add_vpp_config()
98
99 dump = memif.query_vpp_config()
100
101 self.assertTrue(dump)
102 self.assertEqual(dump.sw_if_index, memif.sw_if_index)
103 self.assertEqual(dump.role, memif.role)
104 self.assertEqual(dump.mode, memif.mode)
105 if (memif.socket_id is not None):
106 self.assertEqual(dump.socket_id, memif.socket_id)
107
108 memif.remove_vpp_config()
109
110 dump = memif.query_vpp_config()
111
112 self.assertFalse(dump)
113
114 def _connect_test_one_interface(self, memif):
115 self.assertTrue(memif.wait_for_link_up(5))
116 dump = memif.query_vpp_config()
117
118 if memif.role == MEMIF_ROLE.SLAVE:
119 self.assertEqual(dump.ring_size, memif.ring_size)
120 self.assertEqual(dump.buffer_size, memif.buffer_size)
121 else:
122 self.assertEqual(dump.ring_size, 1)
123 self.assertEqual(dump.buffer_size, 0)
124
125 def _connect_test_interface_pair(self, memif0, memif1):
126 memif0.add_vpp_config()
127 memif1.add_vpp_config()
128
129 memif0.admin_up()
130 memif1.admin_up()
131
132 self._connect_test_one_interface(memif0)
133 self._connect_test_one_interface(memif1)
134
135 memif0.remove_vpp_config()
136 memif1.remove_vpp_config()
137
138 def test_memif_create_delete(self):
139 """ Memif create/delete interface """
140
141 memif = VppMemif(self, MEMIF_ROLE.SLAVE, MEMIF_MODE.ETHERNET)
142 self._create_delete_test_one_interface(memif)
143 memif.role = MEMIF_ROLE.MASTER
144 self._create_delete_test_one_interface(memif)
145
146 def test_memif_create_custom_socket(self):
147 """ Memif create with non-default socket filname """
148
149 memif_sockets = []
150 # existing path
151 memif_sockets.append(
152 VppSocketFilename(
153 self, 1, "/run/vpp/memif1.sock"))
154 # default path ("/run/vpp")
155 memif_sockets.append(
156 VppSocketFilename(
157 self,
158 2,
159 "memif2.sock",
160 add_default_folder=True))
161 # create new folder in default folder
162 memif_sockets.append(
163 VppSocketFilename(
164 self,
165 3,
166 "sock/memif3.sock",
167 add_default_folder=True))
168
169 memif = VppMemif(self, MEMIF_ROLE.SLAVE, MEMIF_MODE.ETHERNET)
170
171 for sock in memif_sockets:
172 sock.add_vpp_config()
173 memif.socket_id = sock.socket_id
174 memif.role = MEMIF_ROLE.SLAVE
175 self._create_delete_test_one_interface(memif)
176 memif.role = MEMIF_ROLE.MASTER
177 self._create_delete_test_one_interface(memif)
178
179 def test_memif_connect(self):
180 """ Memif connect """
181 memif = VppMemif(
182 self,
183 MEMIF_ROLE.SLAVE,
184 MEMIF_MODE.ETHERNET,
185 ring_size=1024,
186 buffer_size=2048)
187 remote_memif = VppMemif(
188 self.remote_test,
189 MEMIF_ROLE.MASTER,
190 MEMIF_MODE.ETHERNET,
191 ring_size=1024,
192 buffer_size=2048)
193
194 self._connect_test_interface_pair(memif, remote_memif)
195
196 memif.role = MEMIF_ROLE.MASTER
197 remote_memif.role = MEMIF_ROLE.SLAVE
198
199 self._connect_test_interface_pair(memif, remote_memif)
200
201 def _create_icmp(self, pg, memif, num):
202 pkts = []
203 for i in range(num):
204 pkt = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
205 IP(src=pg.remote_ip4, dst=memif.ip4_addr) /
206 ICMP(id=memif.if_id, type='echo-request', seq=i))
207 pkts.append(pkt)
208 return pkts
209
210 def _verify_icmp(self, pg, memif, rx, seq):
211 ip = rx[IP]
212 self.assertEqual(ip.src, memif.ip4_addr)
213 self.assertEqual(ip.dst, pg.remote_ip4)
214 self.assertEqual(ip.proto, 1)
215 icmp = rx[ICMP]
216 self.assertEqual(icmp.type, 0) # echo-reply
217 self.assertEqual(icmp.id, memif.if_id)
218 self.assertEqual(icmp.seq, seq)
219
220 def test_memif_ping(self):
221 """ Memif ping """
222 memif = VppMemif(self, MEMIF_ROLE.MASTER, MEMIF_MODE.ETHERNET)
223 remote_memif = VppMemif(self.remote_test, MEMIF_ROLE.SLAVE,
224 MEMIF_MODE.ETHERNET)
225
226 memif.add_vpp_config()
227 memif.config_ip4()
228 memif.admin_up()
229
230 remote_memif.add_vpp_config()
231 remote_memif.config_ip4()
232 remote_memif.admin_up()
233
234 self.assertTrue(memif.wait_for_link_up(5))
235 self.assertTrue(remote_memif.wait_for_link_up(5))
236
237 # add routing to remote vpp
238 dst_addr = socket.inet_pton(socket.AF_INET, self.pg0._local_ip4_subnet)
239 dst_addr_len = 24
240 next_hop_addr = socket.inet_pton(socket.AF_INET, memif.ip4_addr)
241 self.remote_test.vapi.ip_add_del_route(
242 dst_addr, dst_addr_len, next_hop_addr)
243
244 # create ICMP echo-request from local pg to remote memif
245 packet_num = 10
246 pkts = self._create_icmp(self.pg0, remote_memif, packet_num)
247
248 self.pg0.add_stream(pkts)
249 self.pg_enable_capture(self.pg_interfaces)
250 self.pg_start()
251 capture = self.pg0.get_capture(packet_num, timeout=2)
252 seq = 0
253 for c in capture:
254 self._verify_icmp(self.pg0, remote_memif, c, seq)
255 seq += 1
256
257
258if __name__ == '__main__':
259 unittest.main(testRunner=VppTestRunner)