blob: deb8acb69ff49936ed058b9d9ef9d392b0bd29fd [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 =
68 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
69 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
70
71 libmemif_main_t *lm = &libmemif_main;
72
73 ck_assert_ptr_ne (lm, NULL);
74 ck_assert_ptr_ne (lm->control_fd_update, NULL);
75 ck_assert_int_gt (lm->timerfd, 2);
76
77 if (lm->timerfd > 0)
78 close (lm->timerfd);
79 lm->timerfd = -1;
80}
81
82END_TEST
83START_TEST (test_init_epoll)
84{
85 int err;
86
87 if ((err = memif_init (NULL, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
88 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
89
90 libmemif_main_t *lm = &libmemif_main;
91
92 ck_assert_ptr_ne (lm, NULL);
93 ck_assert_ptr_ne (lm->control_fd_update, NULL);
94 ck_assert_int_gt (lm->timerfd, 2);
95 ck_assert_int_gt (memif_epfd, -1);
96
97 if (lm->timerfd > 0)
98 close (lm->timerfd);
99 lm->timerfd = -1;
100}
101
102END_TEST
103START_TEST (test_create)
104{
105 int err;
106 memif_conn_handle_t conn = NULL;
107 memif_conn_args_t args;
108 memset (&args, 0, sizeof (args));
109
110 libmemif_main_t *lm = &libmemif_main;
111
112 if ((err =
113 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
114 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
115
116 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
117 strncpy ((char *) args.instance_name, TEST_APP_NAME,
118 strlen (TEST_APP_NAME));
119
120 if ((err = memif_create (&conn, &args, on_connect,
121 on_disconnect, on_interrupt,
122 NULL)) != MEMIF_ERR_SUCCESS)
123 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
124
125 memif_connection_t *c = (memif_connection_t *) conn;
126
127 ck_assert_ptr_ne (c, NULL);
128
129 ck_assert_uint_eq (c->args.interface_id, args.interface_id);
130 ck_assert_uint_eq (c->args.is_master, args.is_master);
131 ck_assert_uint_eq (c->args.mode, args.mode);
132
133 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
134 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
135 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
136 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
137
138 ck_assert_ptr_eq (c->msg_queue, NULL);
139 ck_assert_ptr_eq (c->regions, NULL);
140 ck_assert_ptr_eq (c->tx_queues, NULL);
141 ck_assert_ptr_eq (c->rx_queues, NULL);
142
143 ck_assert_int_eq (c->fd, -1);
144
145 ck_assert_ptr_ne (c->on_connect, NULL);
146 ck_assert_ptr_ne (c->on_disconnect, NULL);
147 ck_assert_ptr_ne (c->on_interrupt, NULL);
148
149 ck_assert_str_eq (c->args.interface_name, args.interface_name);
150 ck_assert_str_eq (c->args.instance_name, args.instance_name);
151 ck_assert_str_eq (c->args.socket_filename, SOCKET_FILENAME);
152
153 struct itimerspec timer;
154 timerfd_gettime (lm->timerfd, &timer);
155
156 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
157 "timerfd not armed!");
158
159 if (lm->timerfd > 0)
160 close (lm->timerfd);
161 lm->timerfd = -1;
162
163 memif_delete (&conn);
164 ck_assert_ptr_eq (conn, NULL);
165}
166
167END_TEST
168START_TEST (test_create_master)
169{
170 int err, rv;
171 memif_conn_handle_t conn = NULL;
172 memif_conn_args_t args;
173 memset (&args, 0, sizeof (args));
174 args.is_master = 1;
175
176 libmemif_main_t *lm = &libmemif_main;
177
178 if ((err =
179 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
180 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
181
182 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
183 strncpy ((char *) args.instance_name, TEST_APP_NAME,
184 strlen (TEST_APP_NAME));
185
186 if ((err = memif_create (&conn, &args, on_connect,
187 on_disconnect, on_interrupt,
188 NULL)) != MEMIF_ERR_SUCCESS)
189 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
190
191 memif_connection_t *c = (memif_connection_t *) conn;
192
193 ck_assert_ptr_ne (c, NULL);
194
195 ck_assert_uint_eq (c->args.interface_id, args.interface_id);
196 ck_assert_uint_eq (c->args.is_master, args.is_master);
197 ck_assert_uint_eq (c->args.mode, args.mode);
198
199 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
200 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
201 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
202 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
203
204 ck_assert_ptr_eq (c->msg_queue, NULL);
205 ck_assert_ptr_eq (c->regions, NULL);
206 ck_assert_ptr_eq (c->tx_queues, NULL);
207 ck_assert_ptr_eq (c->rx_queues, NULL);
208
209 ck_assert_int_eq (c->fd, -1);
210
211 ck_assert_ptr_ne (c->on_connect, NULL);
212 ck_assert_ptr_ne (c->on_disconnect, NULL);
213 ck_assert_ptr_ne (c->on_interrupt, NULL);
214
215 ck_assert_str_eq (c->args.interface_name, args.interface_name);
216 ck_assert_str_eq (c->args.instance_name, args.instance_name);
217 ck_assert_str_eq (c->args.socket_filename, SOCKET_FILENAME);
218
219 struct stat file_stat;
220
221 rv = stat (SOCKET_FILENAME, &file_stat);
222 ck_assert_int_eq (rv, 0);
223
224 ck_assert (S_ISSOCK (file_stat.st_mode));
225
226 memif_delete (&conn);
227 ck_assert_ptr_eq (conn, NULL);
228}
229
230END_TEST
231START_TEST (test_create_mult)
232{
233 int err;
234 memif_conn_handle_t conn = NULL;
235 memif_conn_handle_t conn1 = NULL;
236 memif_conn_args_t args;
237 memset (&args, 0, sizeof (args));
238
239 libmemif_main_t *lm = &libmemif_main;
240
241 if ((err =
242 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
243 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
244
245 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
246 strncpy ((char *) args.instance_name, TEST_APP_NAME,
247 strlen (TEST_APP_NAME));
248
249 if ((err = memif_create (&conn, &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 args.interface_id = 1;
255
256 if ((err = memif_create (&conn1, &args, on_connect,
257 on_disconnect, on_interrupt,
258 NULL)) != MEMIF_ERR_SUCCESS)
259 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
260
261 memif_connection_t *c = (memif_connection_t *) conn;
262 memif_connection_t *c1 = (memif_connection_t *) conn1;
263
264 ck_assert_ptr_ne (c, NULL);
265 ck_assert_ptr_ne (c1, NULL);
266
267 ck_assert_uint_eq (c->args.interface_id, 0);
268 ck_assert_uint_eq (c->args.is_master, args.is_master);
269 ck_assert_uint_eq (c->args.mode, args.mode);
270 ck_assert_uint_eq (c1->args.interface_id, 1);
271 ck_assert_uint_eq (c1->args.is_master, args.is_master);
272 ck_assert_uint_eq (c1->args.mode, args.mode);
273
274 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
275 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
276 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
277 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
278 ck_assert_uint_eq (c1->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
279 ck_assert_uint_eq (c1->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
280 ck_assert_uint_eq (c1->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
281 ck_assert_uint_eq (c1->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
282
283 ck_assert_ptr_eq (c->msg_queue, NULL);
284 ck_assert_ptr_eq (c->regions, NULL);
285 ck_assert_ptr_eq (c->tx_queues, NULL);
286 ck_assert_ptr_eq (c->rx_queues, NULL);
287 ck_assert_ptr_eq (c1->msg_queue, NULL);
288 ck_assert_ptr_eq (c1->regions, NULL);
289 ck_assert_ptr_eq (c1->tx_queues, NULL);
290 ck_assert_ptr_eq (c1->rx_queues, NULL);
291
292 ck_assert_int_eq (c->fd, -1);
293 ck_assert_int_eq (c1->fd, -1);
294
295 ck_assert_ptr_ne (c->on_connect, NULL);
296 ck_assert_ptr_ne (c->on_disconnect, NULL);
297 ck_assert_ptr_ne (c->on_interrupt, NULL);
298 ck_assert_ptr_ne (c1->on_connect, NULL);
299 ck_assert_ptr_ne (c1->on_disconnect, NULL);
300 ck_assert_ptr_ne (c1->on_interrupt, NULL);
301
302 ck_assert_str_eq (c->args.interface_name, args.interface_name);
303 ck_assert_str_eq (c->args.instance_name, args.instance_name);
304 ck_assert_str_eq (c->args.socket_filename, SOCKET_FILENAME);
305 ck_assert_str_eq (c1->args.interface_name, args.interface_name);
306 ck_assert_str_eq (c1->args.instance_name, args.instance_name);
307 ck_assert_str_eq (c1->args.socket_filename, SOCKET_FILENAME);
308
309 struct itimerspec timer;
310 timerfd_gettime (lm->timerfd, &timer);
311
312 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
313 "timerfd not armed!");
314
315 if (lm->timerfd > 0)
316 close (lm->timerfd);
317 lm->timerfd = -1;
318
319 memif_delete (&conn);
320 ck_assert_ptr_eq (conn, NULL);
321}
322
323END_TEST
324START_TEST (test_control_fd_handler)
325{
326 int err;
327 ready_called = 0;
328 memif_conn_handle_t conn = NULL;
329 memif_conn_args_t args;
330 memset (&args, 0, sizeof (args));
331 args.num_s2m_rings = 2;
332 args.num_m2s_rings = 2;
333
334 libmemif_main_t *lm = &libmemif_main;
335
336 if ((err =
337 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
338 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
339
340 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
341 strncpy ((char *) args.instance_name, TEST_APP_NAME,
342 strlen (TEST_APP_NAME));
343
344 if ((err = memif_create (&conn, &args, on_connect,
345 on_disconnect, on_interrupt,
346 NULL)) != MEMIF_ERR_SUCCESS)
347 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
348
349 memif_connection_t *c = (memif_connection_t *) conn;
350
351 if ((err =
352 memif_control_fd_handler (lm->timerfd,
353 MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS)
354 ck_assert_msg (err == MEMIF_ERR_NO_FILE, "err code: %u, err msg: %s", err,
355 memif_strerror (err));
356
357 register_fd_ready_fn (c, read_fn, write_fn, error_fn);
358 c->fd = 69;
359 lm->control_list[0].key = c->fd;
360 lm->control_list[0].data_struct = c;
361
362 if ((err =
363 memif_control_fd_handler (c->fd,
364 MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS)
365 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
366
367 ck_assert (ready_called & read_call);
368
369 if ((err =
370 memif_control_fd_handler (c->fd,
371 MEMIF_FD_EVENT_WRITE)) != MEMIF_ERR_SUCCESS)
372 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
373
374 ck_assert (ready_called & write_call);
375
376 if ((err =
377 memif_control_fd_handler (c->fd,
378 MEMIF_FD_EVENT_ERROR)) != MEMIF_ERR_SUCCESS)
379 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
380
381 ck_assert (ready_called & error_call);
382
383 if (lm->timerfd > 0)
384 close (lm->timerfd);
385 lm->timerfd = -1;
386
387 memif_delete (&conn);
388 ck_assert_ptr_eq (conn, NULL);
389}
390
391END_TEST
392START_TEST (test_buffer_alloc)
393{
394 int err, i;
395 uint8_t qid;
396 uint16_t buf;
397 memif_buffer_t *bufs;
398 uint16_t max_buf = 10;
399 ready_called = 0;
400 memif_conn_handle_t conn = NULL;
401 memif_conn_args_t args;
402 memset (&args, 0, sizeof (args));
403 args.num_s2m_rings = 2;
404 args.num_m2s_rings = 2;
405
406 libmemif_main_t *lm = &libmemif_main;
407
408 if ((err =
409 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
410 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
411
412 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
413 strncpy ((char *) args.instance_name, TEST_APP_NAME,
414 strlen (TEST_APP_NAME));
415
416 if ((err = memif_create (&conn, &args, on_connect,
417 on_disconnect, on_interrupt,
418 NULL)) != MEMIF_ERR_SUCCESS)
419 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
420
421 memif_connection_t *c = (memif_connection_t *) conn;
422
423 c->run_args.num_s2m_rings = 2;
424 c->run_args.num_m2s_rings = 2;
425 c->run_args.log2_ring_size = 10;
426 c->run_args.buffer_size = 2048;
427
428 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
429 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
430
431 c->fd = 69;
432
433 /* test buffer allocation qid 0 (positive) */
434
435 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
436
437 qid = 0;
438 if ((err =
439 memif_buffer_alloc (conn, qid, bufs, max_buf,
440 &buf)) != MEMIF_ERR_SUCCESS)
441 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
442
443 ck_assert_uint_eq (buf, max_buf);
444 for (i = 0; i < max_buf; i++)
445 ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE);
446
447 /* test buffer allocation qid 1 (positive) */
448 free (bufs);
449 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
450
451 qid = 1;
452 if ((err =
453 memif_buffer_alloc (conn, qid, bufs, max_buf,
454 &buf)) != MEMIF_ERR_SUCCESS)
455 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
456
457 ck_assert_uint_eq (buf, max_buf);
458 for (i = 0; i < max_buf; i++)
459 ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE);
460
461 /* test buffer allocation qid 2 (negative) */
462
463 free (bufs);
464 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
465
466 qid = 2;
467 if ((err =
468 memif_buffer_alloc (conn, qid, bufs, max_buf,
469 &buf)) != MEMIF_ERR_SUCCESS)
470 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
471 memif_strerror (err));
472
473 if (lm->timerfd > 0)
474 close (lm->timerfd);
475 lm->timerfd = -1;
476
477 free (bufs);
478 bufs = NULL;
479
480 memif_delete (&conn);
481 ck_assert_ptr_eq (conn, NULL);
482}
483
484END_TEST
485START_TEST (test_tx_burst)
486{
487 int err, i;
488 uint16_t max_buf = 10, buf, tx;
489 uint8_t qid;
490 memif_buffer_t *bufs;
491 ready_called = 0;
492 memif_conn_handle_t conn = NULL;
493 memif_conn_args_t args;
494 memset (&args, 0, sizeof (args));
495 args.num_s2m_rings = 2;
496 args.num_m2s_rings = 2;
497
498 libmemif_main_t *lm = &libmemif_main;
499
500 if ((err =
501 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
502 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
503
504 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
505 strncpy ((char *) args.instance_name, TEST_APP_NAME,
506 strlen (TEST_APP_NAME));
507
508 if ((err = memif_create (&conn, &args, on_connect,
509 on_disconnect, on_interrupt,
510 NULL)) != MEMIF_ERR_SUCCESS)
511 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
512
513 memif_connection_t *c = (memif_connection_t *) conn;
514
515 c->run_args.num_s2m_rings = 2;
516 c->run_args.num_m2s_rings = 2;
517 c->run_args.log2_ring_size = 10;
518 c->run_args.buffer_size = 2048;
519
520 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
521 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
522
523 c->fd = 69;
524
525 /* test transmit qid 0 (positive) */
526
527 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
528 qid = 0;
529 if ((err =
530 memif_buffer_alloc (conn, qid, bufs, max_buf,
531 &buf)) != MEMIF_ERR_SUCCESS)
532 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
533
534 ck_assert_uint_eq (buf, max_buf);
535 for (i = 0; i < max_buf; i++)
536 ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE);
537
538 if ((err =
539 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
540 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
541
542 ck_assert_uint_eq (tx, max_buf);
543 for (i = 0; i < max_buf; i++)
544 ck_assert_ptr_eq (bufs[i].data, NULL);
545
546 /* test transmit qid 1 (positive) */
547 free (bufs);
548 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
549 qid = 1;
550 if ((err =
551 memif_buffer_alloc (conn, qid, bufs, max_buf,
552 &buf)) != MEMIF_ERR_SUCCESS)
553 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
554
555 ck_assert_uint_eq (buf, max_buf);
556 for (i = 0; i < max_buf; i++)
557 ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE);
558
559 if ((err =
560 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
561 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
562
563 ck_assert_uint_eq (tx, max_buf);
564 for (i = 0; i < max_buf; i++)
565 ck_assert_ptr_eq (bufs[i].data, NULL);
566
567 /* test transmit qid 2 (negative) */
568 free (bufs);
569 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
570 qid = 2;
571 if ((err =
572 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
573 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
574 memif_strerror (err));
575
576 if (lm->timerfd > 0)
577 close (lm->timerfd);
578 lm->timerfd = -1;
579 free (bufs);
580 bufs = NULL;
581
582 memif_delete (&conn);
583 ck_assert_ptr_eq (conn, NULL);
584}
585
586END_TEST
587START_TEST (test_rx_burst)
588{
589 int err, i;
590 uint16_t max_buf = 10, buf, rx;
591 uint8_t qid;
592 memif_buffer_t *bufs;
593 memif_queue_t *mq;
594 memif_ring_t *ring;
595 ready_called = 0;
596 memif_conn_handle_t conn = NULL;
597 memif_conn_args_t args;
598 memset (&args, 0, sizeof (args));
599 args.num_s2m_rings = 2;
600 args.num_m2s_rings = 2;
601
602 libmemif_main_t *lm = &libmemif_main;
603
604 if ((err =
605 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
606 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
607
608 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
609 strncpy ((char *) args.instance_name, TEST_APP_NAME,
610 strlen (TEST_APP_NAME));
611
612 if ((err = memif_create (&conn, &args, on_connect,
613 on_disconnect, on_interrupt,
614 NULL)) != MEMIF_ERR_SUCCESS)
615 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
616
617 memif_connection_t *c = (memif_connection_t *) conn;
618
619 c->run_args.num_s2m_rings = 2;
620 c->run_args.num_m2s_rings = 2;
621 c->run_args.log2_ring_size = 10;
622 c->run_args.buffer_size = 2048;
623
624 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
625 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
626
627 c->fd = 69;
628
629 /* test receive qid 0 (positive) */
630 qid = 0;
631 mq = &c->rx_queues[qid];
632 ring = mq->ring;
633 ring->head += max_buf;
634
635 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
636
637 if ((err =
638 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
639 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
640
641 ck_assert_uint_eq (rx, max_buf);
642 for (i = 0; i < max_buf; i++)
643 ck_assert_ptr_ne (bufs[i].data, NULL);
644
645 /* test receive qid 1 (positive) */
646 qid = 1;
647 mq = &c->rx_queues[qid];
648 ring = mq->ring;
649 ring->head += max_buf;
650
651 free (bufs);
652 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
653
654 if ((err =
655 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
656 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
657
658 ck_assert_uint_eq (rx, max_buf);
659 for (i = 0; i < max_buf; i++)
660 ck_assert_ptr_ne (bufs[i].data, NULL);
661
662 /* test receive qid 2 (negative) */
663 free (bufs);
664 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
665
666 if ((err =
667 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
668 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
669 memif_strerror (err));
670
671 if (lm->timerfd > 0)
672 close (lm->timerfd);
673 lm->timerfd = -1;
674 free (bufs);
675 bufs = NULL;
676
677 memif_delete (&conn);
678 ck_assert_ptr_eq (conn, NULL);
679}
680
681END_TEST
682START_TEST (test_buffer_free)
683{
684 int err, i;
685 uint16_t max_buf = 10, buf, rx;
686 uint8_t qid;
687 memif_buffer_t *bufs;
688 memif_queue_t *mq;
689 memif_ring_t *ring;
690 ready_called = 0;
691 memif_conn_handle_t conn = NULL;
692 memif_conn_args_t args;
693 memset (&args, 0, sizeof (args));
694 args.num_s2m_rings = 2;
695 args.num_m2s_rings = 2;
696
697 libmemif_main_t *lm = &libmemif_main;
698
699 if ((err =
700 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
701 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
702
703 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
704 strncpy ((char *) args.instance_name, TEST_APP_NAME,
705 strlen (TEST_APP_NAME));
706
707 if ((err = memif_create (&conn, &args, on_connect,
708 on_disconnect, on_interrupt,
709 NULL)) != MEMIF_ERR_SUCCESS)
710 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
711
712 memif_connection_t *c = (memif_connection_t *) conn;
713
714 c->run_args.num_s2m_rings = 2;
715 c->run_args.num_m2s_rings = 2;
716 c->run_args.log2_ring_size = 10;
717 c->run_args.buffer_size = 2048;
718
719 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
720 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
721
722 c->fd = 69;
723
724 /* test buffer free qid 0 (positive) */
725 qid = 0;
726 mq = &c->rx_queues[qid];
727 ring = mq->ring;
728 ring->head += 10;
729
730 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
731
732 if ((err =
733 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
734 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
735
736 ck_assert_uint_eq (rx, max_buf);
737 for (i = 0; i < max_buf; i++)
738 ck_assert_ptr_ne (bufs[i].data, NULL);
739
740 if ((err =
741 memif_buffer_free (conn, qid, bufs, max_buf,
742 &buf)) != MEMIF_ERR_SUCCESS)
743 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
744
745 ck_assert_uint_eq (buf, max_buf);
746 for (i = 0; i < max_buf; i++)
747 ck_assert_ptr_eq (bufs[i].data, NULL);
748 ck_assert_uint_eq (ring->head, ring->tail);
749
750 /* test buffer free qid 1 (positive) */
751 qid = 1;
752 mq = &c->rx_queues[qid];
753 ring = mq->ring;
754 ring->head += 10;
755
756 free (bufs);
757 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
758
759 if ((err =
760 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
761 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
762
763 ck_assert_uint_eq (rx, max_buf);
764 for (i = 0; i < max_buf; i++)
765 ck_assert_ptr_ne (bufs[i].data, NULL);
766
767 if ((err =
768 memif_buffer_free (conn, qid, bufs, max_buf,
769 &buf)) != MEMIF_ERR_SUCCESS)
770 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
771
772 ck_assert_uint_eq (buf, max_buf);
773 for (i = 0; i < max_buf; i++)
774 ck_assert_ptr_eq (bufs[i].data, NULL);
775 ck_assert_uint_eq (ring->head, ring->tail);
776
777
778 /* test buffer free qid 2 (negative) */
779 qid = 2;
780 free (bufs);
781 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
782
783 if ((err =
784 memif_buffer_free (conn, qid, bufs, max_buf,
785 &buf)) != MEMIF_ERR_SUCCESS)
786 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
787 memif_strerror (err));
788
789 if (lm->timerfd > 0)
790 close (lm->timerfd);
791 lm->timerfd = -1;
792 free (bufs);
793 bufs = NULL;
794
795 memif_delete (&conn);
796 ck_assert_ptr_eq (conn, NULL);
797}
798
799END_TEST
800START_TEST (test_get_details)
801{
802 int err, i;
803 ready_called = 0;
804 memif_conn_handle_t conn = NULL;
805 memif_conn_args_t args;
806 memset (&args, 0, sizeof (args));
807 args.num_s2m_rings = 2;
808 args.num_m2s_rings = 2;
809
810 libmemif_main_t *lm = &libmemif_main;
811
812 if ((err =
813 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
814 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
815
816 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
817 strncpy ((char *) args.instance_name, TEST_APP_NAME,
818 strlen (TEST_APP_NAME));
819
820 if ((err = memif_create (&conn, &args, on_connect,
821 on_disconnect, on_interrupt,
822 NULL)) != MEMIF_ERR_SUCCESS)
823 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
824
825 memif_connection_t *c = (memif_connection_t *) conn;
826
827 c->run_args.num_s2m_rings = 2;
828 c->run_args.num_m2s_rings = 2;
829 c->run_args.log2_ring_size = 10;
830 c->run_args.buffer_size = 2048;
831
832 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
833 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
834
835 memif_details_t md;
836 memset (&md, 0, sizeof (md));
837 ssize_t buflen = 2048;
838 char *buf = malloc (buflen);
839 memset (buf, 0, buflen);
840
841 if ((err = memif_get_details (conn, &md, buf, buflen)) != MEMIF_ERR_SUCCESS)
842 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
843
844 ck_assert_str_eq (md.if_name, c->args.interface_name);
845 ck_assert_str_eq (md.inst_name, c->args.instance_name);
846 ck_assert_str_eq (md.remote_if_name, c->remote_if_name);
847 ck_assert_str_eq (md.remote_inst_name, c->remote_name);
848 ck_assert_str_eq (md.secret, c->args.secret);
849 ck_assert_str_eq (md.socket_filename, c->args.socket_filename);
850
851 ck_assert_uint_eq (md.id, c->args.interface_id);
852 ck_assert_uint_ne (md.role, c->args.is_master);
853 ck_assert_uint_eq (md.mode, c->args.mode);
854 for (i = 0; i < md.rx_queues_num; i++)
855 {
856 ck_assert_uint_eq (md.rx_queues[i].qid, i);
857 ck_assert_uint_eq (md.rx_queues[i].ring_size,
858 (1 << c->args.log2_ring_size));
859 ck_assert_uint_eq (md.rx_queues[i].buffer_size, c->args.buffer_size);
860 }
861 for (i = 0; i < md.tx_queues_num; i++)
862 {
863 ck_assert_uint_eq (md.tx_queues[i].qid, i);
864 ck_assert_uint_eq (md.tx_queues[i].ring_size,
865 (1 << c->args.log2_ring_size));
866 ck_assert_uint_eq (md.tx_queues[i].buffer_size, c->args.buffer_size);
867 }
868 ck_assert_uint_eq (md.link_up_down, 0);
869
870 if (lm->timerfd > 0)
871 close (lm->timerfd);
872 lm->timerfd = -1;
873
874 memif_delete (&conn);
875 ck_assert_ptr_eq (conn, NULL);
876}
877
878END_TEST
879START_TEST (test_init_regions_and_queues)
880{
881 int err;
882 ready_called = 0;
883 memif_conn_handle_t conn = NULL;
884 memif_conn_args_t args;
885 memset (&args, 0, sizeof (args));
886 args.num_s2m_rings = 2;
887 args.num_m2s_rings = 2;
888
889 libmemif_main_t *lm = &libmemif_main;
890
891 if ((err =
892 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
893 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
894
895 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
896 strncpy ((char *) args.instance_name, TEST_APP_NAME,
897 strlen (TEST_APP_NAME));
898
899 if ((err = memif_create (&conn, &args, on_connect,
900 on_disconnect, on_interrupt,
901 NULL)) != MEMIF_ERR_SUCCESS)
902 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
903
904 memif_connection_t *c = (memif_connection_t *) conn;
905
906 c->run_args.num_s2m_rings = 2;
907 c->run_args.num_m2s_rings = 2;
908 c->run_args.log2_ring_size = 10;
909 c->run_args.buffer_size = 2048;
910
911 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
912 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
913
914 ck_assert_ptr_ne (c->regions, NULL);
915 ck_assert_ptr_ne (c->tx_queues, NULL);
916 ck_assert_ptr_ne (c->rx_queues, NULL);
917
918 ck_assert_ptr_ne (c->regions->shm, NULL);
919 ck_assert_ptr_ne (c->tx_queues->ring, NULL);
920 ck_assert_ptr_ne (c->rx_queues->ring, NULL);
921
922 ck_assert_int_ne (c->regions->fd, -1);
923 ck_assert_uint_eq (c->tx_queues->ring->cookie, MEMIF_COOKIE);
924 ck_assert_uint_eq (c->rx_queues->ring->cookie, MEMIF_COOKIE);
925
926 if (lm->timerfd > 0)
927 close (lm->timerfd);
928 lm->timerfd = -1;
929
930 memif_delete (&conn);
931 ck_assert_ptr_eq (conn, NULL);
932}
933
934END_TEST
935START_TEST (test_connect1)
936{
937 int err;
938 ready_called = 0;
939 memif_conn_handle_t conn = NULL;
940 memif_conn_args_t args;
941 memset (&args, 0, sizeof (args));
942 args.num_s2m_rings = 2;
943 args.num_m2s_rings = 2;
944
945 libmemif_main_t *lm = &libmemif_main;
946
947 if ((err =
948 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
949 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
950
951 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
952 strncpy ((char *) args.instance_name, TEST_APP_NAME,
953 strlen (TEST_APP_NAME));
954
955 if ((err = memif_create (&conn, &args, on_connect,
956 on_disconnect, on_interrupt,
957 NULL)) != MEMIF_ERR_SUCCESS)
958 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
959
960 memif_connection_t *c = (memif_connection_t *) conn;
961
962 c->run_args.num_s2m_rings = 2;
963 c->run_args.num_m2s_rings = 2;
964 c->run_args.log2_ring_size = 10;
965 c->run_args.buffer_size = 2048;
966
967 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
968 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
969
970 if ((err = memif_connect1 (c)) != MEMIF_ERR_SUCCESS)
971 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
972
973 if (lm->timerfd > 0)
974 close (lm->timerfd);
975 lm->timerfd = -1;
976
977 memif_delete (&conn);
978 ck_assert_ptr_eq (conn, NULL);
979}
980
981END_TEST
982START_TEST (test_disconnect_internal)
983{
984 int err;
985 ready_called = 0;
986 memif_conn_handle_t conn = NULL;
987 memif_conn_args_t args;
988 memset (&args, 0, sizeof (args));
989 args.num_s2m_rings = 2;
990 args.num_m2s_rings = 2;
991
992 libmemif_main_t *lm = &libmemif_main;
993
994 if ((err =
995 memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS)
996 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
997
998 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
999 strncpy ((char *) args.instance_name, TEST_APP_NAME,
1000 strlen (TEST_APP_NAME));
1001
1002 if ((err = memif_create (&conn, &args, on_connect,
1003 on_disconnect, on_interrupt,
1004 NULL)) != MEMIF_ERR_SUCCESS)
1005 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
1006
1007 memif_connection_t *c = (memif_connection_t *) conn;
1008
1009 c->run_args.num_s2m_rings = 2;
1010 c->run_args.num_m2s_rings = 2;
1011 c->run_args.log2_ring_size = 10;
1012 c->run_args.buffer_size = 2048;
1013
1014 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
1015 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
1016
1017 if ((err = memif_disconnect_internal (c)) != MEMIF_ERR_SUCCESS)
1018 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
1019
1020 ck_assert_int_eq (c->fd, -1);
1021
1022 ck_assert_ptr_eq (c->tx_queues, NULL);
1023 ck_assert_ptr_eq (c->rx_queues, NULL);
1024 ck_assert_ptr_eq (c->regions, NULL);
1025 ck_assert_ptr_eq (c->msg_queue, NULL);
1026
1027 struct itimerspec timer;
1028 timerfd_gettime (lm->timerfd, &timer);
1029
1030 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
1031 "timerfd not armed!");
1032
1033 if (lm->timerfd > 0)
1034 close (lm->timerfd);
1035 lm->timerfd = -1;
1036
1037 memif_delete (&conn);
1038 ck_assert_ptr_eq (conn, NULL);
1039}
1040
1041END_TEST Suite * main_suite ()
1042{
1043 Suite *s;
1044
1045 TCase *tc_api;
1046 TCase *tc_internal;
1047
1048 /* create main test suite */
1049 s = suite_create ("Libmemif main");
1050
1051 /* create api test case */
1052 tc_api = tcase_create ("Api calls");
1053 /* add tests to test case */
1054 tcase_add_test (tc_api, test_init);
1055 tcase_add_test (tc_api, test_init_epoll);
1056 tcase_add_test (tc_api, test_create);
1057 tcase_add_test (tc_api, test_create_master);
1058 tcase_add_test (tc_api, test_create_mult);
1059 tcase_add_test (tc_api, test_control_fd_handler);
1060 tcase_add_test (tc_api, test_buffer_alloc);
1061 tcase_add_test (tc_api, test_tx_burst);
1062 tcase_add_test (tc_api, test_rx_burst);
1063 tcase_add_test (tc_api, test_buffer_free);
1064 tcase_add_test (tc_api, test_get_details);
1065
1066 /* create internal test case */
1067 tc_internal = tcase_create ("Internal");
1068 /* add tests to test case */
1069 tcase_add_test (tc_internal, test_init_regions_and_queues);
1070 tcase_add_test (tc_internal, test_connect1);
1071 tcase_add_test (tc_internal, test_disconnect_internal);
1072
1073 /* add test cases to test suite */
1074 suite_add_tcase (s, tc_api);
1075 suite_add_tcase (s, tc_internal);
1076
1077 /* return main test suite to test runner */
1078 return s;
1079}