blob: 8ad9961157f1f19d060a66c5ac1af6aad164d550 [file] [log] [blame]
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <unistd.h>
20
21#include <main_test.h>
22
23#include <memif_private.h>
24
25#define SOCKET_FILENAME "/run/vpp/memif.sock"
26
27uint8_t ready_called;
28#define read_call (1 << 0)
29#define write_call (1 << 1)
30#define error_call (1 << 2)
31
32int
33read_fn (memif_connection_t * c)
34{
35 ready_called |= read_call;
36 return 0;
37}
38
39int
40write_fn (memif_connection_t * c)
41{
42 ready_called |= write_call;
43 return 0;
44}
45
46int
47error_fn (memif_connection_t * c)
48{
49 ready_called |= error_call;
50 return 0;
51}
52
53static void
54register_fd_ready_fn (memif_connection_t * c,
55 memif_fn * read_fn, memif_fn * write_fn,
56 memif_fn * error_fn)
57{
58 c->read_fn = read_fn;
59 c->write_fn = write_fn;
60 c->error_fn = error_fn;
61}
62
63START_TEST (test_init)
64{
65 int err;
66
67 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020068 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +010069 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020070 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
71
72 libmemif_main_t *lm = &libmemif_main;
73
74 ck_assert_ptr_ne (lm, NULL);
75 ck_assert_ptr_ne (lm->control_fd_update, NULL);
76 ck_assert_int_gt (lm->timerfd, 2);
77
78 if (lm->timerfd > 0)
79 close (lm->timerfd);
80 lm->timerfd = -1;
81}
82
83END_TEST
84START_TEST (test_init_epoll)
85{
86 int err;
87
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020088 if ((err =
msardara8f554b72018-12-11 18:36:55 +010089 memif_init (NULL, TEST_APP_NAME, NULL, NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020090 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
91
92 libmemif_main_t *lm = &libmemif_main;
93
94 ck_assert_ptr_ne (lm, NULL);
95 ck_assert_ptr_ne (lm->control_fd_update, NULL);
96 ck_assert_int_gt (lm->timerfd, 2);
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +020097 ck_assert_int_gt (lm->epfd, -1);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020098
99 if (lm->timerfd > 0)
100 close (lm->timerfd);
101 lm->timerfd = -1;
102}
103
104END_TEST
105START_TEST (test_create)
106{
107 int err;
108 memif_conn_handle_t conn = NULL;
109 memif_conn_args_t args;
110 memset (&args, 0, sizeof (args));
111
112 libmemif_main_t *lm = &libmemif_main;
113
114 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200115 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100116 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200117 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
118
119 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200120
121 if ((err = memif_create (&conn, &args, on_connect,
122 on_disconnect, on_interrupt,
123 NULL)) != MEMIF_ERR_SUCCESS)
124 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
125
126 memif_connection_t *c = (memif_connection_t *) conn;
127
128 ck_assert_ptr_ne (c, NULL);
129
130 ck_assert_uint_eq (c->args.interface_id, args.interface_id);
131 ck_assert_uint_eq (c->args.is_master, args.is_master);
132 ck_assert_uint_eq (c->args.mode, args.mode);
133
134 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
135 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
136 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
137 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
138
139 ck_assert_ptr_eq (c->msg_queue, NULL);
140 ck_assert_ptr_eq (c->regions, NULL);
141 ck_assert_ptr_eq (c->tx_queues, NULL);
142 ck_assert_ptr_eq (c->rx_queues, NULL);
143
144 ck_assert_int_eq (c->fd, -1);
145
146 ck_assert_ptr_ne (c->on_connect, NULL);
147 ck_assert_ptr_ne (c->on_disconnect, NULL);
148 ck_assert_ptr_ne (c->on_interrupt, NULL);
149
msardara8f554b72018-12-11 18:36:55 +0100150 ck_assert_str_eq ((char *)c->args.interface_name, (char *)args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200151
152 struct itimerspec timer;
153 timerfd_gettime (lm->timerfd, &timer);
154
155 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
156 "timerfd not armed!");
157
158 if (lm->timerfd > 0)
159 close (lm->timerfd);
160 lm->timerfd = -1;
161
162 memif_delete (&conn);
163 ck_assert_ptr_eq (conn, NULL);
164}
165
166END_TEST
167START_TEST (test_create_master)
168{
169 int err, rv;
170 memif_conn_handle_t conn = NULL;
171 memif_conn_args_t args;
172 memset (&args, 0, sizeof (args));
173 args.is_master = 1;
174
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200175 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200176 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100177 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200178 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
179
180 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200181
182 if ((err = memif_create (&conn, &args, on_connect,
183 on_disconnect, on_interrupt,
184 NULL)) != MEMIF_ERR_SUCCESS)
185 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
186
187 memif_connection_t *c = (memif_connection_t *) conn;
188
189 ck_assert_ptr_ne (c, NULL);
190
191 ck_assert_uint_eq (c->args.interface_id, args.interface_id);
192 ck_assert_uint_eq (c->args.is_master, args.is_master);
193 ck_assert_uint_eq (c->args.mode, args.mode);
194
195 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
196 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
197 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
198 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
199
200 ck_assert_ptr_eq (c->msg_queue, NULL);
201 ck_assert_ptr_eq (c->regions, NULL);
202 ck_assert_ptr_eq (c->tx_queues, NULL);
203 ck_assert_ptr_eq (c->rx_queues, NULL);
204
205 ck_assert_int_eq (c->fd, -1);
206
207 ck_assert_ptr_ne (c->on_connect, NULL);
208 ck_assert_ptr_ne (c->on_disconnect, NULL);
209 ck_assert_ptr_ne (c->on_interrupt, NULL);
210
msardara8f554b72018-12-11 18:36:55 +0100211 ck_assert_str_eq ((char *)c->args.interface_name, (char *)args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200212
213 struct stat file_stat;
214
215 rv = stat (SOCKET_FILENAME, &file_stat);
216 ck_assert_int_eq (rv, 0);
217
218 ck_assert (S_ISSOCK (file_stat.st_mode));
219
220 memif_delete (&conn);
221 ck_assert_ptr_eq (conn, NULL);
222}
223
224END_TEST
225START_TEST (test_create_mult)
226{
227 int err;
228 memif_conn_handle_t conn = NULL;
229 memif_conn_handle_t conn1 = NULL;
230 memif_conn_args_t args;
231 memset (&args, 0, sizeof (args));
232
233 libmemif_main_t *lm = &libmemif_main;
234
235 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200236 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100237 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200238 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
239
240 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200241
242 if ((err = memif_create (&conn, &args, on_connect,
243 on_disconnect, on_interrupt,
244 NULL)) != MEMIF_ERR_SUCCESS)
245 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
246
247 args.interface_id = 1;
248
249 if ((err = memif_create (&conn1, &args, on_connect,
250 on_disconnect, on_interrupt,
251 NULL)) != MEMIF_ERR_SUCCESS)
252 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
253
254 memif_connection_t *c = (memif_connection_t *) conn;
255 memif_connection_t *c1 = (memif_connection_t *) conn1;
256
257 ck_assert_ptr_ne (c, NULL);
258 ck_assert_ptr_ne (c1, NULL);
259
260 ck_assert_uint_eq (c->args.interface_id, 0);
261 ck_assert_uint_eq (c->args.is_master, args.is_master);
262 ck_assert_uint_eq (c->args.mode, args.mode);
263 ck_assert_uint_eq (c1->args.interface_id, 1);
264 ck_assert_uint_eq (c1->args.is_master, args.is_master);
265 ck_assert_uint_eq (c1->args.mode, args.mode);
266
267 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
268 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
269 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
270 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
271 ck_assert_uint_eq (c1->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
272 ck_assert_uint_eq (c1->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
273 ck_assert_uint_eq (c1->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
274 ck_assert_uint_eq (c1->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
275
276 ck_assert_ptr_eq (c->msg_queue, NULL);
277 ck_assert_ptr_eq (c->regions, NULL);
278 ck_assert_ptr_eq (c->tx_queues, NULL);
279 ck_assert_ptr_eq (c->rx_queues, NULL);
280 ck_assert_ptr_eq (c1->msg_queue, NULL);
281 ck_assert_ptr_eq (c1->regions, NULL);
282 ck_assert_ptr_eq (c1->tx_queues, NULL);
283 ck_assert_ptr_eq (c1->rx_queues, NULL);
284
285 ck_assert_int_eq (c->fd, -1);
286 ck_assert_int_eq (c1->fd, -1);
287
288 ck_assert_ptr_ne (c->on_connect, NULL);
289 ck_assert_ptr_ne (c->on_disconnect, NULL);
290 ck_assert_ptr_ne (c->on_interrupt, NULL);
291 ck_assert_ptr_ne (c1->on_connect, NULL);
292 ck_assert_ptr_ne (c1->on_disconnect, NULL);
293 ck_assert_ptr_ne (c1->on_interrupt, NULL);
294
msardara8f554b72018-12-11 18:36:55 +0100295 ck_assert_str_eq ((char *)c->args.interface_name, (char *)args.interface_name);
msardara8f554b72018-12-11 18:36:55 +0100296 ck_assert_str_eq ((char *)c1->args.interface_name, (char *)args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200297
298 struct itimerspec timer;
299 timerfd_gettime (lm->timerfd, &timer);
300
301 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
302 "timerfd not armed!");
303
304 if (lm->timerfd > 0)
305 close (lm->timerfd);
306 lm->timerfd = -1;
307
308 memif_delete (&conn);
309 ck_assert_ptr_eq (conn, NULL);
310}
311
312END_TEST
313START_TEST (test_control_fd_handler)
314{
315 int err;
316 ready_called = 0;
317 memif_conn_handle_t conn = NULL;
318 memif_conn_args_t args;
319 memset (&args, 0, sizeof (args));
320 args.num_s2m_rings = 2;
321 args.num_m2s_rings = 2;
322
323 libmemif_main_t *lm = &libmemif_main;
324
325 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200326 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100327 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200328 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
329
330 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200331
332 if ((err = memif_create (&conn, &args, on_connect,
333 on_disconnect, on_interrupt,
334 NULL)) != MEMIF_ERR_SUCCESS)
335 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
336
337 memif_connection_t *c = (memif_connection_t *) conn;
338
339 if ((err =
340 memif_control_fd_handler (lm->timerfd,
341 MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS)
342 ck_assert_msg (err == MEMIF_ERR_NO_FILE, "err code: %u, err msg: %s", err,
343 memif_strerror (err));
344
345 register_fd_ready_fn (c, read_fn, write_fn, error_fn);
346 c->fd = 69;
347 lm->control_list[0].key = c->fd;
348 lm->control_list[0].data_struct = c;
349
350 if ((err =
351 memif_control_fd_handler (c->fd,
352 MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS)
353 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
354
355 ck_assert (ready_called & read_call);
356
357 if ((err =
358 memif_control_fd_handler (c->fd,
359 MEMIF_FD_EVENT_WRITE)) != MEMIF_ERR_SUCCESS)
360 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
361
362 ck_assert (ready_called & write_call);
363
364 if ((err =
365 memif_control_fd_handler (c->fd,
366 MEMIF_FD_EVENT_ERROR)) != MEMIF_ERR_SUCCESS)
367 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
368
369 ck_assert (ready_called & error_call);
370
371 if (lm->timerfd > 0)
372 close (lm->timerfd);
373 lm->timerfd = -1;
374
375 memif_delete (&conn);
376 ck_assert_ptr_eq (conn, NULL);
377}
378
379END_TEST
380START_TEST (test_buffer_alloc)
381{
382 int err, i;
383 uint8_t qid;
384 uint16_t buf;
385 memif_buffer_t *bufs;
386 uint16_t max_buf = 10;
387 ready_called = 0;
388 memif_conn_handle_t conn = NULL;
389 memif_conn_args_t args;
390 memset (&args, 0, sizeof (args));
391 args.num_s2m_rings = 2;
392 args.num_m2s_rings = 2;
393
394 libmemif_main_t *lm = &libmemif_main;
395
396 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200397 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100398 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200399 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
400
401 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200402
403 if ((err = memif_create (&conn, &args, on_connect,
404 on_disconnect, on_interrupt,
405 NULL)) != MEMIF_ERR_SUCCESS)
406 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
407
408 memif_connection_t *c = (memif_connection_t *) conn;
409
410 c->run_args.num_s2m_rings = 2;
411 c->run_args.num_m2s_rings = 2;
412 c->run_args.log2_ring_size = 10;
413 c->run_args.buffer_size = 2048;
414
415 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
416 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
417
418 c->fd = 69;
419
420 /* test buffer allocation qid 0 (positive) */
421
422 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
423
424 qid = 0;
425 if ((err =
426 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200427 &buf,
428 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200429 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
430
431 ck_assert_uint_eq (buf, max_buf);
432 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200433 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200434
435 /* test buffer allocation qid 1 (positive) */
436 free (bufs);
437 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
438
439 qid = 1;
440 if ((err =
441 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200442 &buf,
443 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200444 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
445
446 ck_assert_uint_eq (buf, max_buf);
447 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200448 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200449
450 /* test buffer allocation qid 2 (negative) */
451
452 free (bufs);
453 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
454
455 qid = 2;
456 if ((err =
457 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200458 &buf,
459 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200460 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
461 memif_strerror (err));
462
463 if (lm->timerfd > 0)
464 close (lm->timerfd);
465 lm->timerfd = -1;
466
467 free (bufs);
468 bufs = NULL;
469
470 memif_delete (&conn);
471 ck_assert_ptr_eq (conn, NULL);
472}
473
474END_TEST
475START_TEST (test_tx_burst)
476{
477 int err, i;
478 uint16_t max_buf = 10, buf, tx;
479 uint8_t qid;
480 memif_buffer_t *bufs;
481 ready_called = 0;
482 memif_conn_handle_t conn = NULL;
483 memif_conn_args_t args;
484 memset (&args, 0, sizeof (args));
485 args.num_s2m_rings = 2;
486 args.num_m2s_rings = 2;
487
488 libmemif_main_t *lm = &libmemif_main;
489
490 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200491 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100492 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200493 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
494
495 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200496
497 if ((err = memif_create (&conn, &args, on_connect,
498 on_disconnect, on_interrupt,
499 NULL)) != MEMIF_ERR_SUCCESS)
500 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
501
502 memif_connection_t *c = (memif_connection_t *) conn;
503
504 c->run_args.num_s2m_rings = 2;
505 c->run_args.num_m2s_rings = 2;
506 c->run_args.log2_ring_size = 10;
507 c->run_args.buffer_size = 2048;
508
509 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
510 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
511
512 c->fd = 69;
513
514 /* test transmit qid 0 (positive) */
515
516 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
517 qid = 0;
518 if ((err =
519 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200520 &buf,
521 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200522 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
523
524 ck_assert_uint_eq (buf, max_buf);
525 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200526 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200527
528 if ((err =
529 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
530 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
531
532 ck_assert_uint_eq (tx, max_buf);
533 for (i = 0; i < max_buf; i++)
534 ck_assert_ptr_eq (bufs[i].data, NULL);
535
536 /* test transmit qid 1 (positive) */
537 free (bufs);
538 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
539 qid = 1;
540 if ((err =
541 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200542 &buf,
543 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200544 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
545
546 ck_assert_uint_eq (buf, max_buf);
547 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200548 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200549
550 if ((err =
551 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
552 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
553
554 ck_assert_uint_eq (tx, max_buf);
555 for (i = 0; i < max_buf; i++)
556 ck_assert_ptr_eq (bufs[i].data, NULL);
557
558 /* test transmit qid 2 (negative) */
559 free (bufs);
560 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
561 qid = 2;
562 if ((err =
563 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
564 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
565 memif_strerror (err));
566
567 if (lm->timerfd > 0)
568 close (lm->timerfd);
569 lm->timerfd = -1;
570 free (bufs);
571 bufs = NULL;
572
573 memif_delete (&conn);
574 ck_assert_ptr_eq (conn, NULL);
575}
576
577END_TEST
578START_TEST (test_rx_burst)
579{
580 int err, i;
Benoît Ganne49ee6842019-04-30 11:50:46 +0200581 uint16_t max_buf = 10, rx;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200582 uint8_t qid;
583 memif_buffer_t *bufs;
584 memif_queue_t *mq;
585 memif_ring_t *ring;
586 ready_called = 0;
587 memif_conn_handle_t conn = NULL;
588 memif_conn_args_t args;
589 memset (&args, 0, sizeof (args));
590 args.num_s2m_rings = 2;
591 args.num_m2s_rings = 2;
592
593 libmemif_main_t *lm = &libmemif_main;
594
595 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200596 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100597 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200598 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
599
600 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200601
602 if ((err = memif_create (&conn, &args, on_connect,
603 on_disconnect, on_interrupt,
604 NULL)) != MEMIF_ERR_SUCCESS)
605 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
606
607 memif_connection_t *c = (memif_connection_t *) conn;
608
609 c->run_args.num_s2m_rings = 2;
610 c->run_args.num_m2s_rings = 2;
611 c->run_args.log2_ring_size = 10;
612 c->run_args.buffer_size = 2048;
613
614 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
615 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
616
617 c->fd = 69;
618
619 /* test receive qid 0 (positive) */
620 qid = 0;
621 mq = &c->rx_queues[qid];
622 ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200623 ring->tail += max_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200624
625 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
626
627 if ((err =
628 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
629 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
630
631 ck_assert_uint_eq (rx, max_buf);
632 for (i = 0; i < max_buf; i++)
633 ck_assert_ptr_ne (bufs[i].data, NULL);
634
635 /* test receive qid 1 (positive) */
636 qid = 1;
637 mq = &c->rx_queues[qid];
638 ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200639 ring->tail += max_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200640
641 free (bufs);
642 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
643
644 if ((err =
645 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
646 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
647
648 ck_assert_uint_eq (rx, max_buf);
649 for (i = 0; i < max_buf; i++)
650 ck_assert_ptr_ne (bufs[i].data, NULL);
651
652 /* test receive qid 2 (negative) */
653 free (bufs);
654 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
655
656 if ((err =
657 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
658 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
659 memif_strerror (err));
660
661 if (lm->timerfd > 0)
662 close (lm->timerfd);
663 lm->timerfd = -1;
664 free (bufs);
665 bufs = NULL;
666
667 memif_delete (&conn);
668 ck_assert_ptr_eq (conn, NULL);
669}
670
671END_TEST
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200672START_TEST (test_get_details)
673{
674 int err, i;
675 ready_called = 0;
676 memif_conn_handle_t conn = NULL;
677 memif_conn_args_t args;
678 memset (&args, 0, sizeof (args));
679 args.num_s2m_rings = 2;
680 args.num_m2s_rings = 2;
681
682 libmemif_main_t *lm = &libmemif_main;
683
684 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200685 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100686 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200687 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
688
689 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200690
691 if ((err = memif_create (&conn, &args, on_connect,
692 on_disconnect, on_interrupt,
693 NULL)) != MEMIF_ERR_SUCCESS)
694 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
695
696 memif_connection_t *c = (memif_connection_t *) conn;
697
698 c->run_args.num_s2m_rings = 2;
699 c->run_args.num_m2s_rings = 2;
700 c->run_args.log2_ring_size = 10;
701 c->run_args.buffer_size = 2048;
702
703 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
704 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
705
706 memif_details_t md;
707 memset (&md, 0, sizeof (md));
708 ssize_t buflen = 2048;
709 char *buf = malloc (buflen);
710 memset (buf, 0, buflen);
711
712 if ((err = memif_get_details (conn, &md, buf, buflen)) != MEMIF_ERR_SUCCESS)
713 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
714
msardara8f554b72018-12-11 18:36:55 +0100715 ck_assert_str_eq ((char *)md.if_name, (char *)c->args.interface_name);
716 ck_assert_str_eq ((char *)md.remote_if_name, (char *)c->remote_if_name);
717 ck_assert_str_eq ((char *)md.remote_inst_name, (char *)c->remote_name);
718 ck_assert_str_eq ((char *)md.secret, (char *)c->args.secret);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200719
720 ck_assert_uint_eq (md.id, c->args.interface_id);
721 ck_assert_uint_ne (md.role, c->args.is_master);
722 ck_assert_uint_eq (md.mode, c->args.mode);
723 for (i = 0; i < md.rx_queues_num; i++)
724 {
725 ck_assert_uint_eq (md.rx_queues[i].qid, i);
726 ck_assert_uint_eq (md.rx_queues[i].ring_size,
727 (1 << c->args.log2_ring_size));
728 ck_assert_uint_eq (md.rx_queues[i].buffer_size, c->args.buffer_size);
729 }
730 for (i = 0; i < md.tx_queues_num; i++)
731 {
732 ck_assert_uint_eq (md.tx_queues[i].qid, i);
733 ck_assert_uint_eq (md.tx_queues[i].ring_size,
734 (1 << c->args.log2_ring_size));
735 ck_assert_uint_eq (md.tx_queues[i].buffer_size, c->args.buffer_size);
736 }
737 ck_assert_uint_eq (md.link_up_down, 0);
738
739 if (lm->timerfd > 0)
740 close (lm->timerfd);
741 lm->timerfd = -1;
742
743 memif_delete (&conn);
744 ck_assert_ptr_eq (conn, NULL);
745}
746
747END_TEST
748START_TEST (test_init_regions_and_queues)
749{
750 int err;
751 ready_called = 0;
752 memif_conn_handle_t conn = NULL;
753 memif_conn_args_t args;
754 memset (&args, 0, sizeof (args));
755 args.num_s2m_rings = 2;
756 args.num_m2s_rings = 2;
757
758 libmemif_main_t *lm = &libmemif_main;
759
760 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200761 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100762 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200763 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
764
765 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200766
767 if ((err = memif_create (&conn, &args, on_connect,
768 on_disconnect, on_interrupt,
769 NULL)) != MEMIF_ERR_SUCCESS)
770 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
771
772 memif_connection_t *c = (memif_connection_t *) conn;
773
774 c->run_args.num_s2m_rings = 2;
775 c->run_args.num_m2s_rings = 2;
776 c->run_args.log2_ring_size = 10;
777 c->run_args.buffer_size = 2048;
778
779 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
780 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
781
782 ck_assert_ptr_ne (c->regions, NULL);
783 ck_assert_ptr_ne (c->tx_queues, NULL);
784 ck_assert_ptr_ne (c->rx_queues, NULL);
785
msardara8f554b72018-12-11 18:36:55 +0100786 ck_assert_ptr_ne (c->regions->addr, NULL);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200787 ck_assert_ptr_ne (c->tx_queues->ring, NULL);
788 ck_assert_ptr_ne (c->rx_queues->ring, NULL);
789
790 ck_assert_int_ne (c->regions->fd, -1);
791 ck_assert_uint_eq (c->tx_queues->ring->cookie, MEMIF_COOKIE);
792 ck_assert_uint_eq (c->rx_queues->ring->cookie, MEMIF_COOKIE);
793
794 if (lm->timerfd > 0)
795 close (lm->timerfd);
796 lm->timerfd = -1;
797
798 memif_delete (&conn);
799 ck_assert_ptr_eq (conn, NULL);
800}
801
802END_TEST
803START_TEST (test_connect1)
804{
805 int err;
806 ready_called = 0;
807 memif_conn_handle_t conn = NULL;
808 memif_conn_args_t args;
809 memset (&args, 0, sizeof (args));
810 args.num_s2m_rings = 2;
811 args.num_m2s_rings = 2;
812
813 libmemif_main_t *lm = &libmemif_main;
814
815 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200816 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100817 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200818 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
819
820 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200821
822 if ((err = memif_create (&conn, &args, on_connect,
823 on_disconnect, on_interrupt,
824 NULL)) != MEMIF_ERR_SUCCESS)
825 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
826
827 memif_connection_t *c = (memif_connection_t *) conn;
828
829 c->run_args.num_s2m_rings = 2;
830 c->run_args.num_m2s_rings = 2;
831 c->run_args.log2_ring_size = 10;
832 c->run_args.buffer_size = 2048;
833
834 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
835 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
836
837 if ((err = memif_connect1 (c)) != MEMIF_ERR_SUCCESS)
838 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
839
840 if (lm->timerfd > 0)
841 close (lm->timerfd);
842 lm->timerfd = -1;
843
844 memif_delete (&conn);
845 ck_assert_ptr_eq (conn, NULL);
846}
847
848END_TEST
849START_TEST (test_disconnect_internal)
850{
851 int err;
852 ready_called = 0;
853 memif_conn_handle_t conn = NULL;
854 memif_conn_args_t args;
855 memset (&args, 0, sizeof (args));
856 args.num_s2m_rings = 2;
857 args.num_m2s_rings = 2;
858
859 libmemif_main_t *lm = &libmemif_main;
860
861 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200862 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100863 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200864 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
865
866 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200867
868 if ((err = memif_create (&conn, &args, on_connect,
869 on_disconnect, on_interrupt,
870 NULL)) != MEMIF_ERR_SUCCESS)
871 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
872
873 memif_connection_t *c = (memif_connection_t *) conn;
874
875 c->run_args.num_s2m_rings = 2;
876 c->run_args.num_m2s_rings = 2;
877 c->run_args.log2_ring_size = 10;
878 c->run_args.buffer_size = 2048;
879
880 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
881 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
882
883 if ((err = memif_disconnect_internal (c)) != MEMIF_ERR_SUCCESS)
884 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
885
886 ck_assert_int_eq (c->fd, -1);
887
888 ck_assert_ptr_eq (c->tx_queues, NULL);
889 ck_assert_ptr_eq (c->rx_queues, NULL);
890 ck_assert_ptr_eq (c->regions, NULL);
891 ck_assert_ptr_eq (c->msg_queue, NULL);
892
893 struct itimerspec timer;
894 timerfd_gettime (lm->timerfd, &timer);
895
896 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
897 "timerfd not armed!");
898
899 if (lm->timerfd > 0)
900 close (lm->timerfd);
901 lm->timerfd = -1;
902
903 memif_delete (&conn);
904 ck_assert_ptr_eq (conn, NULL);
905}
906
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200907END_TEST Suite *
908main_suite ()
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200909{
910 Suite *s;
911
912 TCase *tc_api;
913 TCase *tc_internal;
914
915 /* create main test suite */
916 s = suite_create ("Libmemif main");
917
918 /* create api test case */
919 tc_api = tcase_create ("Api calls");
920 /* add tests to test case */
921 tcase_add_test (tc_api, test_init);
922 tcase_add_test (tc_api, test_init_epoll);
923 tcase_add_test (tc_api, test_create);
924 tcase_add_test (tc_api, test_create_master);
925 tcase_add_test (tc_api, test_create_mult);
926 tcase_add_test (tc_api, test_control_fd_handler);
927 tcase_add_test (tc_api, test_buffer_alloc);
928 tcase_add_test (tc_api, test_tx_burst);
929 tcase_add_test (tc_api, test_rx_burst);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200930 tcase_add_test (tc_api, test_get_details);
931
932 /* create internal test case */
933 tc_internal = tcase_create ("Internal");
934 /* add tests to test case */
935 tcase_add_test (tc_internal, test_init_regions_and_queues);
936 tcase_add_test (tc_internal, test_connect1);
937 tcase_add_test (tc_internal, test_disconnect_internal);
938
939 /* add test cases to test suite */
940 suite_add_tcase (s, tc_api);
941 suite_add_tcase (s, tc_internal);
942
943 /* return main test suite to test runner */
944 return s;
945}