blob: 3f871d7f23fd6b79ddffded1715219549888dc23 [file] [log] [blame]
Jakub Grajciar3744fc72018-03-29 13:15:10 +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
18#include <stdlib.h>
19#include <stdint.h>
20#include <net/if.h>
21#include <sys/types.h>
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <sys/socket.h>
25#include <sys/un.h>
26#include <sys/uio.h>
27#include <sys/mman.h>
28#include <sys/prctl.h>
29#include <inttypes.h>
30#include <string.h>
31#include <stdio.h>
32#include <netdb.h>
33#include <linux/ip.h>
34#include <linux/icmp.h>
35#include <arpa/inet.h>
36#include <stdlib.h>
37#include <netinet/if_ether.h>
38#include <net/if_arp.h>
39#include <asm/byteorder.h>
40#include <byteswap.h>
41#include <string.h>
42#include <sys/epoll.h>
43#include <errno.h>
44#include <unistd.h>
45#include <signal.h>
46
47#include <time.h>
48
49#include <libmemif.h>
50#include <icmp_proto.h>
51
52#define APP_NAME "ICMP_Responder"
53#define IF_NAME "memif_connection"
54
55#define HEADROOM 0x80 /* 128b */
56#define ENCAP 0x60
57
58#ifdef ICMP_DBG
59#define DBG(...) do { \
60 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \
61 printf (__VA_ARGS__); \
62 printf ("\n"); \
63 } while (0)
64#define LOG(...) do { \
65 if (enable_log) { \
66 dprintf (out_fd, __VA_ARGS__); \
67 dprintf (out_fd, "\n"); \
68 } \
69 } while (0)
70#define LOG_FILE "/tmp/memif_time_test.txt"
71#else
72#define DBG(...)
73#define LOG(...)
74#endif
75
76#define INFO(...) do { \
77 printf ("INFO: "__VA_ARGS__); \
78 printf ("\n"); \
79 } while (0)
80
81
82/* maximum tx/rx memif buffers */
83#define MAX_MEMIF_BUFS 256
84#define MAX_CONNS 50
85
86int epfd;
87int out_fd;
88uint8_t enable_log;
89
90typedef struct
91{
92 uint16_t index;
Paul Vinciguerra62237662020-05-29 23:03:06 -040093 /* memif connection handle */
Jakub Grajciar3744fc72018-03-29 13:15:10 +020094 memif_conn_handle_t conn;
95 /* buffers */
96 memif_buffer_t *bufs;
97 /* allocated tx buffers counter */
98 /* number of tx buffers pointing to shared memory */
99 uint16_t tx_buf_num;
Paul Vinciguerra62237662020-05-29 23:03:06 -0400100 /* allocated rx buffers counter */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200101 /* number of rx buffers pointing to shared memory */
102 uint16_t rx_buf_num;
103 /* interface ip address */
104 uint8_t ip_addr[4];
105 uint64_t tx_counter, rx_counter, tx_err_counter;
106 uint64_t t_sec, t_nsec;
107} memif_connection_t;
108
109typedef struct
110{
111 uint16_t index;
112 icmpr_flow_mode_t mode;
113 uint64_t packet_count;
114 uint16_t sequence;
115 uint64_t tx;
116 uint8_t ip_daddr[4];
117 uint8_t hw_daddr[6];
118 struct timespec *start, end;
119} icmpr_flow_t;
120
121memif_connection_t memif_connection[MAX_CONNS];
122long ctx[MAX_CONNS];
123icmpr_flow_t *flow;
124
125/* print details for all memif connections */
126static void
127print_memif_details ()
128{
129 memif_details_t md;
130 ssize_t buflen;
131 char *buf;
132 int err, i, e;
133 buflen = 2048;
134 buf = malloc (buflen);
135 printf ("MEMIF DETAILS\n");
136 printf ("==============================\n");
137 for (i = 0; i < MAX_CONNS; i++)
138 {
139 memif_connection_t *c = &memif_connection[i];
140
141 memset (&md, 0, sizeof (md));
142 memset (buf, 0, buflen);
143
144 err = memif_get_details (c->conn, &md, buf, buflen);
145 if (err != MEMIF_ERR_SUCCESS)
146 {
147 if (err != MEMIF_ERR_NOCONN)
148 INFO ("%s", memif_strerror (err));
149 continue;
150 }
151
152 printf ("interface index: %d\n", i);
153
154 printf ("\tinterface ip: %u.%u.%u.%u\n",
155 c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
156 printf ("\tinterface name: %s\n", (char *) md.if_name);
157 printf ("\tapp name: %s\n", (char *) md.inst_name);
158 printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
159 printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
160 printf ("\tid: %u\n", md.id);
161 printf ("\tsecret: %s\n", (char *) md.secret);
162 printf ("\trole: ");
163 if (md.role)
164 printf ("slave\n");
165 else
166 printf ("master\n");
167 printf ("\tmode: ");
168 switch (md.mode)
169 {
170 case 0:
171 printf ("ethernet\n");
172 break;
173 case 1:
174 printf ("ip\n");
175 break;
176 case 2:
177 printf ("punt/inject\n");
178 break;
179 default:
180 printf ("unknown\n");
181 break;
182 }
183 printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
184 printf ("\trx queues:\n");
185 for (e = 0; e < md.rx_queues_num; e++)
186 {
187 printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
188 printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
189 printf ("\t\tring rx mode: %s\n",
190 md.rx_queues[e].flags ? "polling" : "interrupt");
191 printf ("\t\tring head: %u\n", md.rx_queues[e].head);
192 printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
193 printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
194 }
195 printf ("\ttx queues:\n");
196 for (e = 0; e < md.tx_queues_num; e++)
197 {
198 printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
199 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
200 printf ("\t\tring rx mode: %s\n",
201 md.tx_queues[e].flags ? "polling" : "interrupt");
202 printf ("\t\tring head: %u\n", md.tx_queues[e].head);
203 printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
204 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
205 }
206 printf ("\tlink: ");
207 if (md.link_up_down)
208 printf ("up\n");
209 else
210 printf ("down\n");
211 }
212 free (buf);
213}
214
215int
216add_epoll_fd (int fd, uint32_t events)
217{
218 if (fd < 0)
219 {
220 DBG ("invalid fd %d", fd);
221 return -1;
222 }
223 struct epoll_event evt;
224 memset (&evt, 0, sizeof (evt));
225 evt.events = events;
226 evt.data.fd = fd;
227 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
228 {
229 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
230 return -1;
231 }
232 DBG ("fd %d added to epoll", fd);
233 return 0;
234}
235
236int
237mod_epoll_fd (int fd, uint32_t events)
238{
239 if (fd < 0)
240 {
241 DBG ("invalid fd %d", fd);
242 return -1;
243 }
244 struct epoll_event evt;
245 memset (&evt, 0, sizeof (evt));
246 evt.events = events;
247 evt.data.fd = fd;
248 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
249 {
250 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
251 return -1;
252 }
Paul Vinciguerra62237662020-05-29 23:03:06 -0400253 DBG ("fd %d modified on epoll", fd);
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200254 return 0;
255}
256
257int
258del_epoll_fd (int fd)
259{
260 if (fd < 0)
261 {
262 DBG ("invalid fd %d", fd);
263 return -1;
264 }
265 struct epoll_event evt;
266 memset (&evt, 0, sizeof (evt));
267 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
268 {
269 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
270 return -1;
271 }
272 DBG ("fd %d removed from epoll", fd);
273 return 0;
274}
275
276/* informs user about connected status. private_ctx is used by user to identify connection
277 (multiple connections WIP) */
278int
279on_connect (memif_conn_handle_t conn, void *private_ctx)
280{
281 INFO ("memif connected!");
282 memif_refill_queue (conn, 0, -1, HEADROOM);
283 enable_log = 1;
284 return 0;
285}
286
287/* informs user about disconnected status. private_ctx is used by user to identify connection
288 (multiple connections WIP) */
289int
290on_disconnect (memif_conn_handle_t conn, void *private_ctx)
291{
292 INFO ("memif disconnected!");
293 return 0;
294}
295
296/* user needs to watch new fd or stop watching fd that is about to be closed.
297 control fd will be modified during connection establishment to minimize CPU usage */
298int
Jakub Grajciar12df4972019-07-01 14:24:48 +0200299control_fd_update (int fd, uint8_t events, void *ctx)
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200300{
301 /* convert memif event definitions to epoll events */
302 if (events & MEMIF_FD_EVENT_DEL)
303 return del_epoll_fd (fd);
304
305 uint32_t evt = 0;
306 if (events & MEMIF_FD_EVENT_READ)
307 evt |= EPOLLIN;
308 if (events & MEMIF_FD_EVENT_WRITE)
309 evt |= EPOLLOUT;
310
311 if (events & MEMIF_FD_EVENT_MOD)
312 return mod_epoll_fd (fd, evt);
313
314 return add_epoll_fd (fd, evt);
315}
316
317/* called when event is polled on interrupt file descriptor.
318 there are packets in shared memory ready to be received */
319/* handle packet processing in rx buffer then enqueue this buffer to tx and transmit */
320int
321on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
322{
323 long index = *((long *) private_ctx);
324 memif_connection_t *c = &memif_connection[index];
325 if (c->index != index)
326 {
327 INFO ("invalid context: %ld/%u", index, c->index);
328 return 0;
329 }
330
331 int err = MEMIF_ERR_SUCCESS, ret_val;
332 uint16_t rx = 0, tx = 0;
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200333 int i = 0; /* rx buffer iterator */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200334
335 /* loop while there are packets in shm */
336 do
337 {
338 /* receive data from shared memory buffers (dequeue rx buffers) */
339 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
340 ret_val = err;
341 c->rx_counter += rx;
342 c->rx_buf_num += rx;
343 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
344 {
345 INFO ("memif_rx_burst: %s", memif_strerror (err));
346 goto error;
347 }
348
Paul Vinciguerra62237662020-05-29 23:03:06 -0400349 /* process buffers in place */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200350 for (i = 0; i < rx; i++)
351 {
352 resolve_packet2 ((void *) (c->bufs + i)->data,
353 &(c->bufs + i)->len, c->ip_addr);
354 }
355
Paul Vinciguerra62237662020-05-29 23:03:06 -0400356 /* enqueue processed buffers to tx ring */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200357 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, i, &tx);
358 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
359 {
360 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
361 goto error;
362 }
363 c->rx_buf_num -= tx;
364 c->tx_buf_num += tx;
365 c->tx_err_counter += i - tx;
366
367 /* mark memif buffers and shared memory buffers as free */
368 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
369 if (err != MEMIF_ERR_SUCCESS)
370 INFO ("memif_buffer_free: %s", memif_strerror (err));
371 c->rx_buf_num -= rx;
372
373 DBG ("freed %d buffers. %u/%u alloc/free buffers",
Jakub Grajciar12df4972019-07-01 14:24:48 +0200374 rx, rx, MAX_MEMIF_BUFS - rx);
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200375
376 /* transmit allocated buffers */
377 err = memif_tx_burst (c->conn, qid, c->bufs, rx, &tx);
378 if (err != MEMIF_ERR_SUCCESS)
379 {
380 INFO ("memif_tx_burst: %s", memif_strerror (err));
381 goto error;
382 }
383 c->tx_counter += tx;
384
385 }
386 while (ret_val == MEMIF_ERR_NOBUF);
387
388 return 0;
389
390error:
391 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
392 if (err != MEMIF_ERR_SUCCESS)
393 INFO ("memif_buffer_free: %s", memif_strerror (err));
394 c->rx_buf_num = 0;
395 DBG ("freed %d buffers. %u/%u alloc/free buffers",
Jakub Grajciar12df4972019-07-01 14:24:48 +0200396 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200397 return 0;
398}
399
Paul Vinciguerra62237662020-05-29 23:03:06 -0400400/* add ethernet encap to packet in rx buffer then enqueue this buffer to tx and transmit */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200401int
402on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
403{
404 long index = *((long *) private_ctx);
405 memif_connection_t *c = &memif_connection[index];
406 if (c->index != index)
407 {
408 INFO ("invalid context: %ld/%u", index, c->index);
409 return 0;
410 }
411
412 int err = MEMIF_ERR_SUCCESS, ret_val;
413 uint16_t rx = 0, tx = 0;
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200414 int i = 0; /* rx buffer iterator */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200415
416 /* loop while there are packets in shm */
417 do
418 {
419 /* receive data from shared memory buffers (dequeue rx buffers) */
420 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
421 ret_val = err;
422 c->rx_counter += rx;
423 c->rx_buf_num += rx;
424 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
425 {
426 INFO ("memif_rx_burst: %s", memif_strerror (err));
427 goto error;
428 }
429
430 /* process bufers in place */
431 for (i = 0; i < rx; i++)
432 {
433 resolve_packet3 (&c->bufs[i].data, &c->bufs[i].len, c->ip_addr);
434 }
435 /* enque processed buffers to tx ring */
436 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, rx, &tx);
437 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
438 {
439 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
440 goto error;
441 }
442 c->rx_buf_num -= tx;
443 c->tx_buf_num += tx;
444 c->tx_err_counter += i - tx;
445
446 /* mark memif buffers and shared memory buffers as free */
447 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
448 if (err != MEMIF_ERR_SUCCESS)
449 INFO ("memif_buffer_free: %s", memif_strerror (err));
450 c->rx_buf_num -= rx;
451
452 DBG ("freed %d buffers. %u/%u alloc/free buffers",
Jakub Grajciar12df4972019-07-01 14:24:48 +0200453 rx, rx, MAX_MEMIF_BUFS - rx);
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200454
455 /* transmit allocated buffers */
456 err = memif_tx_burst (c->conn, qid, c->bufs, i, &tx);
457 if (err != MEMIF_ERR_SUCCESS)
458 {
459 INFO ("memif_tx_burst: %s", memif_strerror (err));
460 goto error;
461 }
462 c->tx_counter += tx;
463
464 }
465 while (ret_val == MEMIF_ERR_NOBUF);
466
467 return 0;
468
469error:
470 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
471 if (err != MEMIF_ERR_SUCCESS)
472 INFO ("memif_buffer_free: %s", memif_strerror (err));
473 c->rx_buf_num = 0;
474 DBG ("freed %d buffers. %u/%u alloc/free buffers",
Jakub Grajciar12df4972019-07-01 14:24:48 +0200475 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200476 return 0;
477}
478
479/* called when event is polled on interrupt file descriptor.
480 there are packets in shared memory ready to be received */
481/* dev test modification: handle only ARP requests */
482int
483on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
484{
485 long index = *((long *) private_ctx);
486 memif_connection_t *c = &memif_connection[index];
487 if (c->index != index)
488 {
489 INFO ("invalid context: %ld/%u", index, c->index);
490 return 0;
491 }
492
493 int err = MEMIF_ERR_SUCCESS, ret_val;
494 int i;
495 uint16_t rx, tx;
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200496
497 do
498 {
499 /* receive data from shared memory buffers */
500 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
501 ret_val = err;
502 c->rx_buf_num += rx;
503 c->rx_counter += rx;
504 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
505 {
506 INFO ("memif_rx_burst: %s", memif_strerror (err));
507 goto error;
508 }
509
510 for (i = 0; i < rx; i++)
511 {
512 if (((struct ether_header *) (c->bufs + i)->data)->ether_type ==
513 0x0608)
514 {
515 /* process data in place */
516 resolve_packet2 ((void *) (c->bufs + i)->data,
517 &(c->bufs + i)->len, c->ip_addr);
518 /* enque buffer to tx ring */
519 memif_buffer_enq_tx (c->conn, qid, c->bufs, 1, &tx);
520 c->rx_buf_num -= tx;
521 memif_tx_burst (c->conn, qid, c->bufs, 1, &tx);
522 }
523 }
524
525 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
526 if (err != MEMIF_ERR_SUCCESS)
527 INFO ("memif_buffer_free: %s", memif_strerror (err));
528 c->rx_buf_num -= rx;
529
530 }
531 while (ret_val == MEMIF_ERR_NOBUF);
532
533 return 0;
534
535error:
536 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
537 if (err != MEMIF_ERR_SUCCESS)
538 INFO ("memif_buffer_free: %s", memif_strerror (err));
539 c->rx_buf_num = 0;
540 DBG ("freed %d buffers. %u/%u alloc/free buffers",
Jakub Grajciar12df4972019-07-01 14:24:48 +0200541 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200542 return 0;
543}
544
545int
546icmpr_memif_create (long index, long mode, char *s)
547{
548 if (index >= MAX_CONNS)
549 {
550 INFO ("connection array overflow");
551 return 0;
552 }
553 if (index < 0)
554 {
555 INFO ("don't even try...");
556 return 0;
557 }
558 memif_connection_t *c = &memif_connection[index];
559
560 /* setting memif connection arguments */
561 memif_conn_args_t args;
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200562 memset (&args, 0, sizeof (args));
563 args.is_master = mode;
564 args.log2_ring_size = 11;
565 args.buffer_size = 2048;
566 args.num_s2m_rings = 1;
567 args.num_m2s_rings = 1;
568 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
569 args.mode = 0;
570 /* socket filename is not specified, because this app is supposed to
571 connect to VPP over memif. so default socket filename will be used */
572 /* default socketfile = /run/vpp/memif.sock */
573
574 args.interface_id = index;
575 /* last argument for memif_create (void * private_ctx) is used by user
576 to identify connection. this context is returned with callbacks */
577 int err;
578 /* default interrupt */
579 if (s == NULL)
580 {
581 err = memif_create (&c->conn,
582 &args, on_connect, on_disconnect, on_interrupt,
583 &ctx[index]);
584 if (err != MEMIF_ERR_SUCCESS)
585 {
586 INFO ("memif_create: %s", memif_strerror (err));
587 return 0;
588 }
589 }
590 else
591 {
592 if (strncmp (s, "0", 1) == 0)
593 {
594 err = memif_create (&c->conn,
595 &args, on_connect, on_disconnect, on_interrupt0,
596 &ctx[index]);
597 if (err != MEMIF_ERR_SUCCESS)
598 {
599 INFO ("memif_create: %s", memif_strerror (err));
600 return 0;
601 }
602 }
603 else if (strncmp (s, "1", 1) == 0)
604 {
605 err = memif_create (&c->conn,
606 &args, on_connect, on_disconnect, on_interrupt1,
607 &ctx[index]);
608 if (err != MEMIF_ERR_SUCCESS)
609 {
610 INFO ("memif_create: %s", memif_strerror (err));
611 return 0;
612 }
613 }
614 else
615 {
616 INFO ("Unknown interrupt descriptor");
617 goto done;
618 }
619 }
620
621 c->index = index;
622 /* alloc memif buffers */
623 c->rx_buf_num = 0;
624 c->tx_buf_num = 0;
625 c->bufs =
626 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
627
628 c->ip_addr[0] = 192;
629 c->ip_addr[1] = 168;
630 c->ip_addr[2] = c->index + 1;
631 c->ip_addr[3] = 2;
632
633 c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
634
635done:
636 return 0;
637}
638
639int
640icmpr_memif_delete (long index)
641{
642 if (index >= MAX_CONNS)
643 {
644 INFO ("connection array overflow");
645 return 0;
646 }
647 if (index < 0)
648 {
649 INFO ("don't even try...");
650 return 0;
651 }
652 memif_connection_t *c = &memif_connection[index];
653
654 if (c->bufs)
655 free (c->bufs);
656 c->bufs = NULL;
657 c->tx_buf_num = 0;
658 c->rx_buf_num = 0;
659
660 int err;
Paul Vinciguerra62237662020-05-29 23:03:06 -0400661 /* disconnect then delete memif connection */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200662 err = memif_delete (&c->conn);
663 if (err != MEMIF_ERR_SUCCESS)
664 INFO ("memif_delete: %s", memif_strerror (err));
665 if (c->conn != NULL)
666 INFO ("memif delete fail");
667 return 0;
668}
669
670void
671print_help ()
672{
673 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
674#ifdef ICMP_DBG
675 printf (" (debug)");
676#endif
677 printf ("\n");
678 printf ("==============================\n");
679 printf ("libmemif version: %s", LIBMEMIF_VERSION);
680#ifdef MEMIF_DBG
681 printf (" (debug)");
682#endif
683 printf ("\n");
684 printf ("memif version: %d\n", memif_get_version ());
685 printf ("commands:\n");
686 printf ("\thelp - prints this help\n");
687 printf ("\texit - exit app\n");
688 printf
689 ("\tconn <index> <mode> [<interrupt-desc>] - create memif. index is also used as interface id, mode 0 = slave 1 = master, interrupt-desc none = default 0 = if ring is full wait 1 = handle only ARP requests\n");
690 printf ("\tdel <index> - delete memif\n");
691 printf ("\tshow - show connection details\n");
692 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
693 printf
694 ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
695 printf ("\tsh-count - print counters\n");
696 printf ("\tcl-count - clear counters\n");
697 printf
698 ("\tsend <index> <tx> <ip> <mac> - send icmp, ommit mac to transmit on ip layer\n");
699}
700
701int
702icmpr_free ()
703{
704 /* application cleanup */
705 int err;
706 long i;
707 if (out_fd > 0)
708 close (out_fd);
709 out_fd = -1;
710 for (i = 0; i < MAX_CONNS; i++)
711 {
712 memif_connection_t *c = &memif_connection[i];
713 if (c->conn)
714 icmpr_memif_delete (i);
715 }
716
717 err = memif_cleanup ();
718 if (err != MEMIF_ERR_SUCCESS)
719 INFO ("memif_delete: %s", memif_strerror (err));
720
721 return 0;
722}
723
724int
725icmpr_set_ip (long index, char *ip)
726{
727 if (index >= MAX_CONNS)
728 {
729 INFO ("connection array overflow");
730 return 0;
731 }
732 if (index < 0)
733 {
734 INFO ("don't even try...");
735 return 0;
736 }
737 memif_connection_t *c = &memif_connection[index];
738 if (c->conn == NULL)
739 {
740 INFO ("no connection at index %ld", index);
741 return 0;
742 }
743
744 char *end;
745 char *ui;
746 uint8_t tmp[4];
747 ui = strtok (ip, ".");
748 if (ui == NULL)
749 goto error;
750 tmp[0] = strtol (ui, &end, 10);
751
752 ui = strtok (NULL, ".");
753 if (ui == NULL)
754 goto error;
755 tmp[1] = strtol (ui, &end, 10);
756
757 ui = strtok (NULL, ".");
758 if (ui == NULL)
759 goto error;
760 tmp[2] = strtol (ui, &end, 10);
761
762 ui = strtok (NULL, ".");
763 if (ui == NULL)
764 goto error;
765 tmp[3] = strtol (ui, &end, 10);
766
767 c->ip_addr[0] = tmp[0];
768 c->ip_addr[1] = tmp[1];
769 c->ip_addr[2] = tmp[2];
770 c->ip_addr[3] = tmp[3];
771
772 INFO ("memif %ld ip address set to %u.%u.%u.%u",
773 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
774
775 return 0;
776
777error:
778 INFO ("invalid ip address");
779 return 0;
780}
781
782int
783icmpr_set_rx_mode (long index, long qid, char *mode)
784{
785 if (index >= MAX_CONNS)
786 {
787 INFO ("connection array overflow");
788 return 0;
789 }
790 if (index < 0)
791 {
792 INFO ("don't even try...");
793 return 0;
794 }
795 memif_connection_t *c = &memif_connection[index];
796
797 if (c->conn == NULL)
798 {
799 INFO ("no connection at index %ld", index);
800 return 0;
801 }
802
803 if (strncmp (mode, "interrupt", 9) == 0)
804 {
805 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
806 }
807
808 else if (strncmp (mode, "polling", 7) == 0)
809 {
810 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
811 }
812 else
813 INFO ("expected rx mode <interrupt|polling>");
814 return 0;
815}
816
817void
818icmpr_print_counters ()
819{
820 int i;
821 for (i = 0; i < MAX_CONNS; i++)
822 {
823 memif_connection_t *c = &memif_connection[i];
824 if (c->conn == NULL)
825 continue;
826 printf ("===============================\n");
827 printf ("interface index: %d\n", c->index);
828 printf ("\trx: %lu\n", c->rx_counter);
829 printf ("\ttx: %lu\n", c->tx_counter);
830 printf ("\ttx_err: %lu\n", c->tx_err_counter);
831 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
832 }
833}
834
835void
836icmpr_reset_counters ()
837{
838 int i;
839 for (i = 0; i < MAX_CONNS; i++)
840 {
841 memif_connection_t *c = &memif_connection[i];
842 if (c->conn == NULL)
843 continue;
844 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
845 c->rx_counter = 0;
846 }
847}
848
849void
850icmpr_send_proc ()
851{
852 memif_connection_t *c = &memif_connection[flow->index];
853 if (c->conn == NULL)
854 {
855 INFO ("No connection at index %d. Stopping flow...\n", flow->index);
856 goto stop_flow;
857 }
858 uint16_t tx, i;
859 int err = MEMIF_ERR_SUCCESS;
860
861 if (!flow->start)
862 {
863 flow->start = malloc (sizeof (struct timespec));
864 memset (flow->start, 0, sizeof (struct timespec));
865 timespec_get (flow->start, TIME_UTC);
866 }
867
868 i = 0;
869 err = memif_buffer_alloc (c->conn, 0, c->bufs,
870 MAX_MEMIF_BUFS >
871 flow->packet_count ? flow->packet_count :
872 MAX_MEMIF_BUFS, &tx, 64);
873 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
874 {
875 INFO ("memif_buffer_alloc: %s Stopping flow...\n",
876 memif_strerror (err));
877 goto stop_flow;
878 }
879 c->tx_buf_num += tx;
880
881 while (tx)
882 {
883 generate_packet2 ((void *) c->bufs[i].data,
884 &c->bufs[i].len, c->ip_addr,
885 flow->ip_daddr, flow->hw_daddr, (flow->sequence)++,
886 flow->mode);
887 i++;
888 tx--;
889 }
890 err = memif_tx_burst (c->conn, 0, c->bufs, i, &tx);
891 if (err != MEMIF_ERR_SUCCESS)
892 {
893 INFO ("memif_tx_burst: %s Stopping flow...\n", memif_strerror (err));
894 goto stop_flow;
895 }
896 c->tx_buf_num -= tx;
897 c->tx_counter += tx;
898 flow->tx += tx;
899 flow->packet_count -= tx;
900
901 if (flow->packet_count == 0)
902 {
903 timespec_get (&flow->end, TIME_UTC);
904 INFO ("Flow finished!");
905 INFO ("Flow length: %lu", flow->tx);
906 uint64_t t1 = flow->end.tv_sec - flow->start->tv_sec;
907 uint64_t t2;
908 if (flow->end.tv_nsec > flow->start->tv_nsec)
909 {
910 t2 = flow->end.tv_nsec - flow->start->tv_nsec;
911 }
912 else
913 {
914 t2 = flow->start->tv_nsec - flow->end.tv_nsec;
915 t1--;
916 }
917 c->t_sec = t1;
918 c->t_nsec = t2;
919 INFO ("Flow time: %lus %luns", t1, t2);
920 double tmp = t1;
921 tmp += t2 / 1e+9;
922 tmp = flow->tx / tmp;
923 INFO ("Average pps: %f", tmp);
924 INFO ("Stopping flow...");
925 goto stop_flow;
926 }
927
928 return;
929
930stop_flow:
931 if (flow)
932 {
933 if (flow->start)
934 free (flow->start);
935 free (flow);
936 }
937 flow = NULL;
938 return;
939}
940
941int
942icmpr_send (long index, long packet_num, char *input)
943{
944 if (flow)
945 {
946 printf ("only one flow allowed\n");
947 return 0;
948 }
949
950 memif_connection_t *c = &memif_connection[index];
951 char *end;
952 char *ui;
953 uint8_t tmp[6];
954 if (c->conn == NULL)
955 return -1;
956
957 flow = malloc (sizeof (icmpr_flow_t));
958 flow->index = index;
959 flow->packet_count = packet_num;
960 flow->sequence = 0;
961 flow->tx = 0;
962 flow->start = NULL;
963 memset (&flow->end, 0, sizeof (struct timespec));
964
965 INFO ("packet count: %lu", flow->packet_count);
966 printf ("%s\n", input);
967
968 ui = strtok (input, ".");
969 if (ui == NULL)
970 goto error;
971 tmp[0] = strtol (ui, &end, 10);
972
973 ui = strtok (NULL, ".");
974 if (ui == NULL)
975 goto error;
976 tmp[1] = strtol (ui, &end, 10);
977
978 ui = strtok (NULL, ".");
979 if (ui == NULL)
980 goto error;
981 tmp[2] = strtol (ui, &end, 10);
982
983 ui = strtok (NULL, ".");
984 if (ui == NULL)
985 goto error;
986 tmp[3] = strtol (ui, &end, 10);
987
988 flow->ip_daddr[0] = tmp[0];
989 flow->ip_daddr[1] = tmp[1];
990 flow->ip_daddr[2] = tmp[2];
991 flow->ip_daddr[3] = tmp[3];
992
993 ui = strtok (NULL, " ");
994 if (ui == NULL)
995 {
996 flow->mode = ICMPR_FLOW_MODE_IP;
997 return 0;
998 }
999
1000 ui = strtok (NULL, ":");
1001 if (ui == NULL)
1002 goto error;
1003 tmp[0] = strtol (ui, &end, 16);
1004 ui = strtok (NULL, ":");
1005 if (ui == NULL)
1006 goto error;
1007 tmp[1] = strtol (ui, &end, 16);
1008 ui = strtok (NULL, ":");
1009 if (ui == NULL)
1010 goto error;
1011 tmp[2] = strtol (ui, &end, 16);
1012 ui = strtok (NULL, ":");
1013 if (ui == NULL)
1014 goto error;
1015 tmp[3] = strtol (ui, &end, 16);
1016 ui = strtok (NULL, ":");
1017 if (ui == NULL)
1018 goto error;
1019 tmp[4] = strtol (ui, &end, 16);
1020 ui = strtok (NULL, ":");
1021 if (ui == NULL)
1022 goto error;
1023 tmp[5] = strtol (ui, &end, 16);
1024
1025 flow->hw_daddr[0] = tmp[0];
1026 flow->hw_daddr[1] = tmp[1];
1027 flow->hw_daddr[2] = tmp[2];
1028 flow->hw_daddr[3] = tmp[3];
1029 flow->hw_daddr[4] = tmp[4];
1030 flow->hw_daddr[5] = tmp[5];
1031
1032 flow->mode = ICMPR_FLOW_MODE_ETH;
1033
1034 return 0;
1035
1036error:
1037 INFO ("Invalid input\n");
1038 if (flow)
1039 free (flow);
1040 flow = NULL;
1041 return 0;
1042}
1043
1044int
1045user_input_handler ()
1046{
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001047 char *in = (char *) malloc (256);
1048 char *ui = fgets (in, 256, stdin);
1049 char *end;
1050 long a;
1051 if (in[0] == '\n')
1052 goto done;
1053 ui = strtok (in, " ");
1054 if (strncmp (ui, "exit", 4) == 0)
1055 {
1056 free (in);
1057 icmpr_free ();
1058 exit (EXIT_SUCCESS);
1059 }
1060 else if (strncmp (ui, "help", 4) == 0)
1061 {
1062 print_help ();
1063 goto done;
1064 }
1065 else if (strncmp (ui, "conn", 4) == 0)
1066 {
1067 ui = strtok (NULL, " ");
1068 if (ui != NULL)
1069 a = strtol (ui, &end, 10);
1070 else
1071 {
1072 INFO ("expected id");
1073 goto done;
1074 }
1075 ui = strtok (NULL, " ");
1076 if (ui != NULL)
1077 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1078 else
1079 INFO ("expected mode <0|1>");
1080 goto done;
1081 }
1082 else if (strncmp (ui, "del", 3) == 0)
1083 {
1084 ui = strtok (NULL, " ");
1085 if (ui != NULL)
1086 icmpr_memif_delete (strtol (ui, &end, 10));
1087 else
1088 INFO ("expected id");
1089 goto done;
1090 }
1091 else if (strncmp (ui, "show", 4) == 0)
1092 {
1093 print_memif_details ();
1094 goto done;
1095 }
1096 else if (strncmp (ui, "ip-set", 6) == 0)
1097 {
1098 ui = strtok (NULL, " ");
1099 if (ui != NULL)
1100 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
1101 else
1102 INFO ("expected id");
1103 goto done;
1104 }
1105 else if (strncmp (ui, "rx-mode", 7) == 0)
1106 {
1107 ui = strtok (NULL, " ");
1108 if (ui != NULL)
1109 a = strtol (ui, &end, 10);
1110 else
1111 {
1112 INFO ("expected id");
1113 goto done;
1114 }
1115 ui = strtok (NULL, " ");
1116 if (ui != NULL)
1117 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1118 else
1119 INFO ("expected qid");
1120 goto done;
1121 }
1122 else if (strncmp (ui, "sh-count", 8) == 0)
1123 {
1124 icmpr_print_counters ();
1125 }
1126 else if (strncmp (ui, "cl-count", 8) == 0)
1127 {
1128 icmpr_reset_counters ();
1129 }
1130 else if (strncmp (ui, "send", 4) == 0)
1131 {
1132 ui = strtok (NULL, " ");
1133 if (ui != NULL)
1134 a = strtol (ui, &end, 10);
1135 else
1136 {
1137 INFO ("expected id");
1138 goto done;
1139 }
1140 ui = strtok (NULL, " ");
1141 if (ui != NULL)
1142 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "));
1143 else
1144 INFO ("expected count");
1145 goto done;
1146 }
1147 else
1148 {
1149 INFO ("unknown command: %s", ui);
1150 goto done;
1151 }
1152
1153 return 0;
1154done:
1155 free (in);
1156 return 0;
1157}
1158
1159int
1160poll_event (int timeout)
1161{
BenoƮt Ganne49ee6842019-04-30 11:50:46 +02001162 struct epoll_event evt;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001163 int app_err = 0, memif_err = 0, en = 0;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001164 uint32_t events = 0;
1165 struct timespec start, end;
1166 memset (&evt, 0, sizeof (evt));
1167 evt.events = EPOLLIN | EPOLLOUT;
1168 sigset_t sigset;
1169 sigemptyset (&sigset);
1170 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
1171 /* id event polled */
1172 timespec_get (&start, TIME_UTC);
1173 if (en < 0)
1174 {
1175 DBG ("epoll_pwait: %s", strerror (errno));
1176 return -1;
1177 }
1178 if (en > 0)
1179 {
1180 /* this app does not use any other file descriptors than stds and memif control fds */
1181 if (evt.data.fd > 2)
1182 {
1183 /* event of memif control fd */
Paul Vinciguerra62237662020-05-29 23:03:06 -04001184 /* convert epoll events to memif events */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001185 if (evt.events & EPOLLIN)
1186 events |= MEMIF_FD_EVENT_READ;
1187 if (evt.events & EPOLLOUT)
1188 events |= MEMIF_FD_EVENT_WRITE;
1189 if (evt.events & EPOLLERR)
1190 events |= MEMIF_FD_EVENT_ERROR;
1191 memif_err = memif_control_fd_handler (evt.data.fd, events);
1192 if (memif_err != MEMIF_ERR_SUCCESS)
1193 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1194 }
1195 else if (evt.data.fd == 0)
1196 {
1197 app_err = user_input_handler ();
1198 }
1199 else
1200 {
1201 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1202 }
1203 }
1204
1205 timespec_get (&end, TIME_UTC);
1206 LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
1207
1208 if ((app_err < 0) || (memif_err < 0))
1209 {
1210 if (app_err < 0)
1211 DBG ("user input handler error");
1212 if (memif_err < 0)
1213 DBG ("memif control fd handler error");
1214 return -1;
1215 }
1216
1217 return 0;
1218}
1219
1220int
1221main ()
1222{
1223 epfd = epoll_create (1);
1224 add_epoll_fd (0, EPOLLIN);
1225
1226 flow = NULL;
1227
1228#ifdef LOG_FILE
1229 remove (LOG_FILE);
1230 enable_log = 0;
1231
1232 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1233 if (out_fd < 0)
1234 INFO ("Error opening log file: %s", strerror (errno));
1235#endif /* LOG_FILE */
1236
1237 /* initialize memory interface */
1238 int err, i;
1239 /* if valid callback is passed as argument, fd event polling will be done by user
1240 all file descriptors and events will be passed to user in this callback */
1241 /* if callback is set to NULL libmemif will handle fd event polling */
msardara8f554b72018-12-11 18:36:55 +01001242 err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001243 if (err != MEMIF_ERR_SUCCESS)
1244 {
1245 INFO ("memif_init: %s", memif_strerror (err));
1246 icmpr_free ();
1247 exit (-1);
1248 }
1249
1250 for (i = 0; i < MAX_CONNS; i++)
1251 {
1252 memif_connection[i].conn = NULL;
1253 ctx[i] = i;
1254 }
1255
1256 print_help ();
1257
1258 /* main loop */
1259 while (1)
1260 {
1261 if (poll_event (0) < 0)
1262 {
1263 DBG ("poll_event error!");
1264 }
1265 if (flow)
1266 {
1267 icmpr_send_proc ();
1268 }
1269 }
1270}