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