blob: e4697beb56fbafd8e49700dd9e5f9c5b9c4cf14b [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);
97 ck_assert_int_gt (memif_epfd, -1);
98
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);
151 ck_assert_str_eq ((char *)c->args.socket_filename, SOCKET_FILENAME);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200152
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 =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200179 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100180 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200181 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
182
183 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200184
185 if ((err = memif_create (&conn, &args, on_connect,
186 on_disconnect, on_interrupt,
187 NULL)) != MEMIF_ERR_SUCCESS)
188 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
189
190 memif_connection_t *c = (memif_connection_t *) conn;
191
192 ck_assert_ptr_ne (c, NULL);
193
194 ck_assert_uint_eq (c->args.interface_id, args.interface_id);
195 ck_assert_uint_eq (c->args.is_master, args.is_master);
196 ck_assert_uint_eq (c->args.mode, args.mode);
197
198 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
199 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
200 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
201 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
202
203 ck_assert_ptr_eq (c->msg_queue, NULL);
204 ck_assert_ptr_eq (c->regions, NULL);
205 ck_assert_ptr_eq (c->tx_queues, NULL);
206 ck_assert_ptr_eq (c->rx_queues, NULL);
207
208 ck_assert_int_eq (c->fd, -1);
209
210 ck_assert_ptr_ne (c->on_connect, NULL);
211 ck_assert_ptr_ne (c->on_disconnect, NULL);
212 ck_assert_ptr_ne (c->on_interrupt, NULL);
213
msardara8f554b72018-12-11 18:36:55 +0100214 ck_assert_str_eq ((char *)c->args.interface_name, (char *)args.interface_name);
215 ck_assert_str_eq ((char *)c->args.socket_filename, SOCKET_FILENAME);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200216
217 struct stat file_stat;
218
219 rv = stat (SOCKET_FILENAME, &file_stat);
220 ck_assert_int_eq (rv, 0);
221
222 ck_assert (S_ISSOCK (file_stat.st_mode));
223
224 memif_delete (&conn);
225 ck_assert_ptr_eq (conn, NULL);
226}
227
228END_TEST
229START_TEST (test_create_mult)
230{
231 int err;
232 memif_conn_handle_t conn = NULL;
233 memif_conn_handle_t conn1 = NULL;
234 memif_conn_args_t args;
235 memset (&args, 0, sizeof (args));
236
237 libmemif_main_t *lm = &libmemif_main;
238
239 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200240 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100241 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200242 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
243
244 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200245
246 if ((err = memif_create (&conn, &args, on_connect,
247 on_disconnect, on_interrupt,
248 NULL)) != MEMIF_ERR_SUCCESS)
249 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
250
251 args.interface_id = 1;
252
253 if ((err = memif_create (&conn1, &args, on_connect,
254 on_disconnect, on_interrupt,
255 NULL)) != MEMIF_ERR_SUCCESS)
256 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
257
258 memif_connection_t *c = (memif_connection_t *) conn;
259 memif_connection_t *c1 = (memif_connection_t *) conn1;
260
261 ck_assert_ptr_ne (c, NULL);
262 ck_assert_ptr_ne (c1, NULL);
263
264 ck_assert_uint_eq (c->args.interface_id, 0);
265 ck_assert_uint_eq (c->args.is_master, args.is_master);
266 ck_assert_uint_eq (c->args.mode, args.mode);
267 ck_assert_uint_eq (c1->args.interface_id, 1);
268 ck_assert_uint_eq (c1->args.is_master, args.is_master);
269 ck_assert_uint_eq (c1->args.mode, args.mode);
270
271 ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
272 ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
273 ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
274 ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
275 ck_assert_uint_eq (c1->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES);
276 ck_assert_uint_eq (c1->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES);
277 ck_assert_uint_eq (c1->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE);
278 ck_assert_uint_eq (c1->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE);
279
280 ck_assert_ptr_eq (c->msg_queue, NULL);
281 ck_assert_ptr_eq (c->regions, NULL);
282 ck_assert_ptr_eq (c->tx_queues, NULL);
283 ck_assert_ptr_eq (c->rx_queues, NULL);
284 ck_assert_ptr_eq (c1->msg_queue, NULL);
285 ck_assert_ptr_eq (c1->regions, NULL);
286 ck_assert_ptr_eq (c1->tx_queues, NULL);
287 ck_assert_ptr_eq (c1->rx_queues, NULL);
288
289 ck_assert_int_eq (c->fd, -1);
290 ck_assert_int_eq (c1->fd, -1);
291
292 ck_assert_ptr_ne (c->on_connect, NULL);
293 ck_assert_ptr_ne (c->on_disconnect, NULL);
294 ck_assert_ptr_ne (c->on_interrupt, NULL);
295 ck_assert_ptr_ne (c1->on_connect, NULL);
296 ck_assert_ptr_ne (c1->on_disconnect, NULL);
297 ck_assert_ptr_ne (c1->on_interrupt, NULL);
298
msardara8f554b72018-12-11 18:36:55 +0100299 ck_assert_str_eq ((char *)c->args.interface_name, (char *)args.interface_name);
300 ck_assert_str_eq ((char *)c->args.socket_filename, SOCKET_FILENAME);
301 ck_assert_str_eq ((char *)c1->args.interface_name, (char *)args.interface_name);
302 ck_assert_str_eq ((char *)c1->args.socket_filename, SOCKET_FILENAME);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200303
304 struct itimerspec timer;
305 timerfd_gettime (lm->timerfd, &timer);
306
307 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
308 "timerfd not armed!");
309
310 if (lm->timerfd > 0)
311 close (lm->timerfd);
312 lm->timerfd = -1;
313
314 memif_delete (&conn);
315 ck_assert_ptr_eq (conn, NULL);
316}
317
318END_TEST
319START_TEST (test_control_fd_handler)
320{
321 int err;
322 ready_called = 0;
323 memif_conn_handle_t conn = NULL;
324 memif_conn_args_t args;
325 memset (&args, 0, sizeof (args));
326 args.num_s2m_rings = 2;
327 args.num_m2s_rings = 2;
328
329 libmemif_main_t *lm = &libmemif_main;
330
331 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200332 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100333 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200334 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
335
336 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200337
338 if ((err = memif_create (&conn, &args, on_connect,
339 on_disconnect, on_interrupt,
340 NULL)) != MEMIF_ERR_SUCCESS)
341 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
342
343 memif_connection_t *c = (memif_connection_t *) conn;
344
345 if ((err =
346 memif_control_fd_handler (lm->timerfd,
347 MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS)
348 ck_assert_msg (err == MEMIF_ERR_NO_FILE, "err code: %u, err msg: %s", err,
349 memif_strerror (err));
350
351 register_fd_ready_fn (c, read_fn, write_fn, error_fn);
352 c->fd = 69;
353 lm->control_list[0].key = c->fd;
354 lm->control_list[0].data_struct = c;
355
356 if ((err =
357 memif_control_fd_handler (c->fd,
358 MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS)
359 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
360
361 ck_assert (ready_called & read_call);
362
363 if ((err =
364 memif_control_fd_handler (c->fd,
365 MEMIF_FD_EVENT_WRITE)) != MEMIF_ERR_SUCCESS)
366 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
367
368 ck_assert (ready_called & write_call);
369
370 if ((err =
371 memif_control_fd_handler (c->fd,
372 MEMIF_FD_EVENT_ERROR)) != MEMIF_ERR_SUCCESS)
373 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
374
375 ck_assert (ready_called & error_call);
376
377 if (lm->timerfd > 0)
378 close (lm->timerfd);
379 lm->timerfd = -1;
380
381 memif_delete (&conn);
382 ck_assert_ptr_eq (conn, NULL);
383}
384
385END_TEST
386START_TEST (test_buffer_alloc)
387{
388 int err, i;
389 uint8_t qid;
390 uint16_t buf;
391 memif_buffer_t *bufs;
392 uint16_t max_buf = 10;
393 ready_called = 0;
394 memif_conn_handle_t conn = NULL;
395 memif_conn_args_t args;
396 memset (&args, 0, sizeof (args));
397 args.num_s2m_rings = 2;
398 args.num_m2s_rings = 2;
399
400 libmemif_main_t *lm = &libmemif_main;
401
402 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200403 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100404 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200405 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
406
407 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200408
409 if ((err = memif_create (&conn, &args, on_connect,
410 on_disconnect, on_interrupt,
411 NULL)) != MEMIF_ERR_SUCCESS)
412 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
413
414 memif_connection_t *c = (memif_connection_t *) conn;
415
416 c->run_args.num_s2m_rings = 2;
417 c->run_args.num_m2s_rings = 2;
418 c->run_args.log2_ring_size = 10;
419 c->run_args.buffer_size = 2048;
420
421 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
422 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
423
424 c->fd = 69;
425
426 /* test buffer allocation qid 0 (positive) */
427
428 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
429
430 qid = 0;
431 if ((err =
432 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200433 &buf,
434 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200435 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
436
437 ck_assert_uint_eq (buf, max_buf);
438 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200439 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200440
441 /* test buffer allocation qid 1 (positive) */
442 free (bufs);
443 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
444
445 qid = 1;
446 if ((err =
447 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200448 &buf,
449 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200450 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
451
452 ck_assert_uint_eq (buf, max_buf);
453 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200454 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200455
456 /* test buffer allocation qid 2 (negative) */
457
458 free (bufs);
459 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
460
461 qid = 2;
462 if ((err =
463 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200464 &buf,
465 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200466 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
467 memif_strerror (err));
468
469 if (lm->timerfd > 0)
470 close (lm->timerfd);
471 lm->timerfd = -1;
472
473 free (bufs);
474 bufs = NULL;
475
476 memif_delete (&conn);
477 ck_assert_ptr_eq (conn, NULL);
478}
479
480END_TEST
481START_TEST (test_tx_burst)
482{
483 int err, i;
484 uint16_t max_buf = 10, buf, tx;
485 uint8_t qid;
486 memif_buffer_t *bufs;
487 ready_called = 0;
488 memif_conn_handle_t conn = NULL;
489 memif_conn_args_t args;
490 memset (&args, 0, sizeof (args));
491 args.num_s2m_rings = 2;
492 args.num_m2s_rings = 2;
493
494 libmemif_main_t *lm = &libmemif_main;
495
496 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200497 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100498 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200499 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
500
501 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200502
503 if ((err = memif_create (&conn, &args, on_connect,
504 on_disconnect, on_interrupt,
505 NULL)) != MEMIF_ERR_SUCCESS)
506 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
507
508 memif_connection_t *c = (memif_connection_t *) conn;
509
510 c->run_args.num_s2m_rings = 2;
511 c->run_args.num_m2s_rings = 2;
512 c->run_args.log2_ring_size = 10;
513 c->run_args.buffer_size = 2048;
514
515 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
516 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
517
518 c->fd = 69;
519
520 /* test transmit qid 0 (positive) */
521
522 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
523 qid = 0;
524 if ((err =
525 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200526 &buf,
527 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200528 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
529
530 ck_assert_uint_eq (buf, max_buf);
531 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200532 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200533
534 if ((err =
535 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
536 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
537
538 ck_assert_uint_eq (tx, max_buf);
539 for (i = 0; i < max_buf; i++)
540 ck_assert_ptr_eq (bufs[i].data, NULL);
541
542 /* test transmit qid 1 (positive) */
543 free (bufs);
544 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
545 qid = 1;
546 if ((err =
547 memif_buffer_alloc (conn, qid, bufs, max_buf,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200548 &buf,
549 MEMIF_DEFAULT_BUFFER_SIZE)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200550 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
551
552 ck_assert_uint_eq (buf, max_buf);
553 for (i = 0; i < max_buf; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200554 ck_assert_uint_eq (bufs[i].len, MEMIF_DEFAULT_BUFFER_SIZE);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200555
556 if ((err =
557 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
558 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
559
560 ck_assert_uint_eq (tx, max_buf);
561 for (i = 0; i < max_buf; i++)
562 ck_assert_ptr_eq (bufs[i].data, NULL);
563
564 /* test transmit qid 2 (negative) */
565 free (bufs);
566 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
567 qid = 2;
568 if ((err =
569 memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS)
570 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
571 memif_strerror (err));
572
573 if (lm->timerfd > 0)
574 close (lm->timerfd);
575 lm->timerfd = -1;
576 free (bufs);
577 bufs = NULL;
578
579 memif_delete (&conn);
580 ck_assert_ptr_eq (conn, NULL);
581}
582
583END_TEST
584START_TEST (test_rx_burst)
585{
586 int err, i;
587 uint16_t max_buf = 10, buf, rx;
588 uint8_t qid;
589 memif_buffer_t *bufs;
590 memif_queue_t *mq;
591 memif_ring_t *ring;
592 ready_called = 0;
593 memif_conn_handle_t conn = NULL;
594 memif_conn_args_t args;
595 memset (&args, 0, sizeof (args));
596 args.num_s2m_rings = 2;
597 args.num_m2s_rings = 2;
598
599 libmemif_main_t *lm = &libmemif_main;
600
601 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200602 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100603 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200604 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
605
606 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200607
608 if ((err = memif_create (&conn, &args, on_connect,
609 on_disconnect, on_interrupt,
610 NULL)) != MEMIF_ERR_SUCCESS)
611 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
612
613 memif_connection_t *c = (memif_connection_t *) conn;
614
615 c->run_args.num_s2m_rings = 2;
616 c->run_args.num_m2s_rings = 2;
617 c->run_args.log2_ring_size = 10;
618 c->run_args.buffer_size = 2048;
619
620 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
621 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
622
623 c->fd = 69;
624
625 /* test receive qid 0 (positive) */
626 qid = 0;
627 mq = &c->rx_queues[qid];
628 ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200629 ring->tail += max_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200630
631 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
632
633 if ((err =
634 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
635 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
636
637 ck_assert_uint_eq (rx, max_buf);
638 for (i = 0; i < max_buf; i++)
639 ck_assert_ptr_ne (bufs[i].data, NULL);
640
641 /* test receive qid 1 (positive) */
642 qid = 1;
643 mq = &c->rx_queues[qid];
644 ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200645 ring->tail += max_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200646
647 free (bufs);
648 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
649
650 if ((err =
651 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
652 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
653
654 ck_assert_uint_eq (rx, max_buf);
655 for (i = 0; i < max_buf; i++)
656 ck_assert_ptr_ne (bufs[i].data, NULL);
657
658 /* test receive qid 2 (negative) */
659 free (bufs);
660 bufs = malloc (sizeof (memif_buffer_t) * max_buf);
661
662 if ((err =
663 memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS)
664 ck_assert_msg (err == MEMIF_ERR_QID, "err code: %u, err msg: %s", err,
665 memif_strerror (err));
666
667 if (lm->timerfd > 0)
668 close (lm->timerfd);
669 lm->timerfd = -1;
670 free (bufs);
671 bufs = NULL;
672
673 memif_delete (&conn);
674 ck_assert_ptr_eq (conn, NULL);
675}
676
677END_TEST
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200678START_TEST (test_get_details)
679{
680 int err, i;
681 ready_called = 0;
682 memif_conn_handle_t conn = NULL;
683 memif_conn_args_t args;
684 memset (&args, 0, sizeof (args));
685 args.num_s2m_rings = 2;
686 args.num_m2s_rings = 2;
687
688 libmemif_main_t *lm = &libmemif_main;
689
690 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200691 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100692 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200693 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
694
695 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200696
697 if ((err = memif_create (&conn, &args, on_connect,
698 on_disconnect, on_interrupt,
699 NULL)) != MEMIF_ERR_SUCCESS)
700 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
701
702 memif_connection_t *c = (memif_connection_t *) conn;
703
704 c->run_args.num_s2m_rings = 2;
705 c->run_args.num_m2s_rings = 2;
706 c->run_args.log2_ring_size = 10;
707 c->run_args.buffer_size = 2048;
708
709 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
710 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
711
712 memif_details_t md;
713 memset (&md, 0, sizeof (md));
714 ssize_t buflen = 2048;
715 char *buf = malloc (buflen);
716 memset (buf, 0, buflen);
717
718 if ((err = memif_get_details (conn, &md, buf, buflen)) != MEMIF_ERR_SUCCESS)
719 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
720
msardara8f554b72018-12-11 18:36:55 +0100721 ck_assert_str_eq ((char *)md.if_name, (char *)c->args.interface_name);
722 ck_assert_str_eq ((char *)md.remote_if_name, (char *)c->remote_if_name);
723 ck_assert_str_eq ((char *)md.remote_inst_name, (char *)c->remote_name);
724 ck_assert_str_eq ((char *)md.secret, (char *)c->args.secret);
725 ck_assert_str_eq ((char *)md.socket_filename, (char *)c->args.socket_filename);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200726
727 ck_assert_uint_eq (md.id, c->args.interface_id);
728 ck_assert_uint_ne (md.role, c->args.is_master);
729 ck_assert_uint_eq (md.mode, c->args.mode);
730 for (i = 0; i < md.rx_queues_num; i++)
731 {
732 ck_assert_uint_eq (md.rx_queues[i].qid, i);
733 ck_assert_uint_eq (md.rx_queues[i].ring_size,
734 (1 << c->args.log2_ring_size));
735 ck_assert_uint_eq (md.rx_queues[i].buffer_size, c->args.buffer_size);
736 }
737 for (i = 0; i < md.tx_queues_num; i++)
738 {
739 ck_assert_uint_eq (md.tx_queues[i].qid, i);
740 ck_assert_uint_eq (md.tx_queues[i].ring_size,
741 (1 << c->args.log2_ring_size));
742 ck_assert_uint_eq (md.tx_queues[i].buffer_size, c->args.buffer_size);
743 }
744 ck_assert_uint_eq (md.link_up_down, 0);
745
746 if (lm->timerfd > 0)
747 close (lm->timerfd);
748 lm->timerfd = -1;
749
750 memif_delete (&conn);
751 ck_assert_ptr_eq (conn, NULL);
752}
753
754END_TEST
755START_TEST (test_init_regions_and_queues)
756{
757 int err;
758 ready_called = 0;
759 memif_conn_handle_t conn = NULL;
760 memif_conn_args_t args;
761 memset (&args, 0, sizeof (args));
762 args.num_s2m_rings = 2;
763 args.num_m2s_rings = 2;
764
765 libmemif_main_t *lm = &libmemif_main;
766
767 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200768 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100769 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200770 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
771
772 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200773
774 if ((err = memif_create (&conn, &args, on_connect,
775 on_disconnect, on_interrupt,
776 NULL)) != MEMIF_ERR_SUCCESS)
777 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
778
779 memif_connection_t *c = (memif_connection_t *) conn;
780
781 c->run_args.num_s2m_rings = 2;
782 c->run_args.num_m2s_rings = 2;
783 c->run_args.log2_ring_size = 10;
784 c->run_args.buffer_size = 2048;
785
786 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
787 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
788
789 ck_assert_ptr_ne (c->regions, NULL);
790 ck_assert_ptr_ne (c->tx_queues, NULL);
791 ck_assert_ptr_ne (c->rx_queues, NULL);
792
msardara8f554b72018-12-11 18:36:55 +0100793 ck_assert_ptr_ne (c->regions->addr, NULL);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200794 ck_assert_ptr_ne (c->tx_queues->ring, NULL);
795 ck_assert_ptr_ne (c->rx_queues->ring, NULL);
796
797 ck_assert_int_ne (c->regions->fd, -1);
798 ck_assert_uint_eq (c->tx_queues->ring->cookie, MEMIF_COOKIE);
799 ck_assert_uint_eq (c->rx_queues->ring->cookie, MEMIF_COOKIE);
800
801 if (lm->timerfd > 0)
802 close (lm->timerfd);
803 lm->timerfd = -1;
804
805 memif_delete (&conn);
806 ck_assert_ptr_eq (conn, NULL);
807}
808
809END_TEST
810START_TEST (test_connect1)
811{
812 int err;
813 ready_called = 0;
814 memif_conn_handle_t conn = NULL;
815 memif_conn_args_t args;
816 memset (&args, 0, sizeof (args));
817 args.num_s2m_rings = 2;
818 args.num_m2s_rings = 2;
819
820 libmemif_main_t *lm = &libmemif_main;
821
822 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200823 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100824 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200825 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
826
827 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200828
829 if ((err = memif_create (&conn, &args, on_connect,
830 on_disconnect, on_interrupt,
831 NULL)) != MEMIF_ERR_SUCCESS)
832 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
833
834 memif_connection_t *c = (memif_connection_t *) conn;
835
836 c->run_args.num_s2m_rings = 2;
837 c->run_args.num_m2s_rings = 2;
838 c->run_args.log2_ring_size = 10;
839 c->run_args.buffer_size = 2048;
840
841 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
842 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
843
844 if ((err = memif_connect1 (c)) != MEMIF_ERR_SUCCESS)
845 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
846
847 if (lm->timerfd > 0)
848 close (lm->timerfd);
849 lm->timerfd = -1;
850
851 memif_delete (&conn);
852 ck_assert_ptr_eq (conn, NULL);
853}
854
855END_TEST
856START_TEST (test_disconnect_internal)
857{
858 int err;
859 ready_called = 0;
860 memif_conn_handle_t conn = NULL;
861 memif_conn_args_t args;
862 memset (&args, 0, sizeof (args));
863 args.num_s2m_rings = 2;
864 args.num_m2s_rings = 2;
865
866 libmemif_main_t *lm = &libmemif_main;
867
868 if ((err =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200869 memif_init (control_fd_update, TEST_APP_NAME, NULL,
msardara8f554b72018-12-11 18:36:55 +0100870 NULL, NULL)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200871 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
872
873 strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200874
875 if ((err = memif_create (&conn, &args, on_connect,
876 on_disconnect, on_interrupt,
877 NULL)) != MEMIF_ERR_SUCCESS)
878 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
879
880 memif_connection_t *c = (memif_connection_t *) conn;
881
882 c->run_args.num_s2m_rings = 2;
883 c->run_args.num_m2s_rings = 2;
884 c->run_args.log2_ring_size = 10;
885 c->run_args.buffer_size = 2048;
886
887 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
888 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
889
890 if ((err = memif_disconnect_internal (c)) != MEMIF_ERR_SUCCESS)
891 ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
892
893 ck_assert_int_eq (c->fd, -1);
894
895 ck_assert_ptr_eq (c->tx_queues, NULL);
896 ck_assert_ptr_eq (c->rx_queues, NULL);
897 ck_assert_ptr_eq (c->regions, NULL);
898 ck_assert_ptr_eq (c->msg_queue, NULL);
899
900 struct itimerspec timer;
901 timerfd_gettime (lm->timerfd, &timer);
902
903 ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec,
904 "timerfd not armed!");
905
906 if (lm->timerfd > 0)
907 close (lm->timerfd);
908 lm->timerfd = -1;
909
910 memif_delete (&conn);
911 ck_assert_ptr_eq (conn, NULL);
912}
913
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200914END_TEST Suite *
915main_suite ()
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200916{
917 Suite *s;
918
919 TCase *tc_api;
920 TCase *tc_internal;
921
922 /* create main test suite */
923 s = suite_create ("Libmemif main");
924
925 /* create api test case */
926 tc_api = tcase_create ("Api calls");
927 /* add tests to test case */
928 tcase_add_test (tc_api, test_init);
929 tcase_add_test (tc_api, test_init_epoll);
930 tcase_add_test (tc_api, test_create);
931 tcase_add_test (tc_api, test_create_master);
932 tcase_add_test (tc_api, test_create_mult);
933 tcase_add_test (tc_api, test_control_fd_handler);
934 tcase_add_test (tc_api, test_buffer_alloc);
935 tcase_add_test (tc_api, test_tx_burst);
936 tcase_add_test (tc_api, test_rx_burst);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200937 tcase_add_test (tc_api, test_get_details);
938
939 /* create internal test case */
940 tc_internal = tcase_create ("Internal");
941 /* add tests to test case */
942 tcase_add_test (tc_internal, test_init_regions_and_queues);
943 tcase_add_test (tc_internal, test_connect1);
944 tcase_add_test (tc_internal, test_disconnect_internal);
945
946 /* add test cases to test suite */
947 suite_add_tcase (s, tc_api);
948 suite_add_tcase (s, tc_internal);
949
950 /* return main test suite to test runner */
951 return s;
952}