blob: aad33039bd8bafb197af2b8d7cdf076be6c5ddbc [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;
93 /* memif conenction handle */
94 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;
100 /* allcoated rx buffers counter */
101 /* 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 }
253 DBG ("fd %d moddified on epoll", fd);
254 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
299control_fd_update (int fd, uint8_t events)
300{
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;
333 uint16_t fb = 0;
334 int i = 0; /* rx buffer iterator */
335 int j = 0; /* tx bufferiterator */
336
337 /* loop while there are packets in shm */
338 do
339 {
340 /* receive data from shared memory buffers (dequeue rx buffers) */
341 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
342 ret_val = err;
343 c->rx_counter += rx;
344 c->rx_buf_num += rx;
345 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
346 {
347 INFO ("memif_rx_burst: %s", memif_strerror (err));
348 goto error;
349 }
350
351 /* process bufers in place */
352 for (i = 0; i < rx; i++)
353 {
354 resolve_packet2 ((void *) (c->bufs + i)->data,
355 &(c->bufs + i)->len, c->ip_addr);
356 }
357
358 /* enque processed buffers to tx ring */
359 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, i, &tx);
360 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
361 {
362 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
363 goto error;
364 }
365 c->rx_buf_num -= tx;
366 c->tx_buf_num += tx;
367 c->tx_err_counter += i - tx;
368
369 /* mark memif buffers and shared memory buffers as free */
370 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
371 if (err != MEMIF_ERR_SUCCESS)
372 INFO ("memif_buffer_free: %s", memif_strerror (err));
373 c->rx_buf_num -= rx;
374
375 DBG ("freed %d buffers. %u/%u alloc/free buffers",
376 fb, rx, MAX_MEMIF_BUFS - rx);
377
378 /* transmit allocated buffers */
379 err = memif_tx_burst (c->conn, qid, c->bufs, rx, &tx);
380 if (err != MEMIF_ERR_SUCCESS)
381 {
382 INFO ("memif_tx_burst: %s", memif_strerror (err));
383 goto error;
384 }
385 c->tx_counter += tx;
386
387 }
388 while (ret_val == MEMIF_ERR_NOBUF);
389
390 return 0;
391
392error:
393 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
394 if (err != MEMIF_ERR_SUCCESS)
395 INFO ("memif_buffer_free: %s", memif_strerror (err));
396 c->rx_buf_num = 0;
397 DBG ("freed %d buffers. %u/%u alloc/free buffers",
398 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
399 return 0;
400}
401
402/* add ethernet encap to packet in rx buffer then enqueue this buffer to tx and tranmit */
403int
404on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
405{
406 long index = *((long *) private_ctx);
407 memif_connection_t *c = &memif_connection[index];
408 if (c->index != index)
409 {
410 INFO ("invalid context: %ld/%u", index, c->index);
411 return 0;
412 }
413
414 int err = MEMIF_ERR_SUCCESS, ret_val;
415 uint16_t rx = 0, tx = 0;
416 uint16_t fb = 0;
417 int i = 0; /* rx buffer iterator */
418 int j = 0; /* tx bufferiterator */
419
420 /* loop while there are packets in shm */
421 do
422 {
423 /* receive data from shared memory buffers (dequeue rx buffers) */
424 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
425 ret_val = err;
426 c->rx_counter += rx;
427 c->rx_buf_num += rx;
428 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
429 {
430 INFO ("memif_rx_burst: %s", memif_strerror (err));
431 goto error;
432 }
433
434 /* process bufers in place */
435 for (i = 0; i < rx; i++)
436 {
437 resolve_packet3 (&c->bufs[i].data, &c->bufs[i].len, c->ip_addr);
438 }
439 /* enque processed buffers to tx ring */
440 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, rx, &tx);
441 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
442 {
443 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
444 goto error;
445 }
446 c->rx_buf_num -= tx;
447 c->tx_buf_num += tx;
448 c->tx_err_counter += i - tx;
449
450 /* mark memif buffers and shared memory buffers as free */
451 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
452 if (err != MEMIF_ERR_SUCCESS)
453 INFO ("memif_buffer_free: %s", memif_strerror (err));
454 c->rx_buf_num -= rx;
455
456 DBG ("freed %d buffers. %u/%u alloc/free buffers",
457 fb, rx, MAX_MEMIF_BUFS - rx);
458
459 /* transmit allocated buffers */
460 err = memif_tx_burst (c->conn, qid, c->bufs, i, &tx);
461 if (err != MEMIF_ERR_SUCCESS)
462 {
463 INFO ("memif_tx_burst: %s", memif_strerror (err));
464 goto error;
465 }
466 c->tx_counter += tx;
467
468 }
469 while (ret_val == MEMIF_ERR_NOBUF);
470
471 return 0;
472
473error:
474 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
475 if (err != MEMIF_ERR_SUCCESS)
476 INFO ("memif_buffer_free: %s", memif_strerror (err));
477 c->rx_buf_num = 0;
478 DBG ("freed %d buffers. %u/%u alloc/free buffers",
479 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
480 return 0;
481}
482
483/* called when event is polled on interrupt file descriptor.
484 there are packets in shared memory ready to be received */
485/* dev test modification: handle only ARP requests */
486int
487on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
488{
489 long index = *((long *) private_ctx);
490 memif_connection_t *c = &memif_connection[index];
491 if (c->index != index)
492 {
493 INFO ("invalid context: %ld/%u", index, c->index);
494 return 0;
495 }
496
497 int err = MEMIF_ERR_SUCCESS, ret_val;
498 int i;
499 uint16_t rx, tx;
500 uint16_t fb;
501 uint16_t pck_seq;
502
503 do
504 {
505 /* receive data from shared memory buffers */
506 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
507 ret_val = err;
508 c->rx_buf_num += rx;
509 c->rx_counter += rx;
510 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
511 {
512 INFO ("memif_rx_burst: %s", memif_strerror (err));
513 goto error;
514 }
515
516 for (i = 0; i < rx; i++)
517 {
518 if (((struct ether_header *) (c->bufs + i)->data)->ether_type ==
519 0x0608)
520 {
521 /* process data in place */
522 resolve_packet2 ((void *) (c->bufs + i)->data,
523 &(c->bufs + i)->len, c->ip_addr);
524 /* enque buffer to tx ring */
525 memif_buffer_enq_tx (c->conn, qid, c->bufs, 1, &tx);
526 c->rx_buf_num -= tx;
527 memif_tx_burst (c->conn, qid, c->bufs, 1, &tx);
528 }
529 }
530
531 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
532 if (err != MEMIF_ERR_SUCCESS)
533 INFO ("memif_buffer_free: %s", memif_strerror (err));
534 c->rx_buf_num -= rx;
535
536 }
537 while (ret_val == MEMIF_ERR_NOBUF);
538
539 return 0;
540
541error:
542 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
543 if (err != MEMIF_ERR_SUCCESS)
544 INFO ("memif_buffer_free: %s", memif_strerror (err));
545 c->rx_buf_num = 0;
546 DBG ("freed %d buffers. %u/%u alloc/free buffers",
547 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
548 return 0;
549}
550
551int
552icmpr_memif_create (long index, long mode, char *s)
553{
554 if (index >= MAX_CONNS)
555 {
556 INFO ("connection array overflow");
557 return 0;
558 }
559 if (index < 0)
560 {
561 INFO ("don't even try...");
562 return 0;
563 }
564 memif_connection_t *c = &memif_connection[index];
565
566 /* setting memif connection arguments */
567 memif_conn_args_t args;
568 int fd = -1;
569 memset (&args, 0, sizeof (args));
570 args.is_master = mode;
571 args.log2_ring_size = 11;
572 args.buffer_size = 2048;
573 args.num_s2m_rings = 1;
574 args.num_m2s_rings = 1;
575 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
576 args.mode = 0;
577 /* socket filename is not specified, because this app is supposed to
578 connect to VPP over memif. so default socket filename will be used */
579 /* default socketfile = /run/vpp/memif.sock */
580
581 args.interface_id = index;
582 /* last argument for memif_create (void * private_ctx) is used by user
583 to identify connection. this context is returned with callbacks */
584 int err;
585 /* default interrupt */
586 if (s == NULL)
587 {
588 err = memif_create (&c->conn,
589 &args, on_connect, on_disconnect, on_interrupt,
590 &ctx[index]);
591 if (err != MEMIF_ERR_SUCCESS)
592 {
593 INFO ("memif_create: %s", memif_strerror (err));
594 return 0;
595 }
596 }
597 else
598 {
599 if (strncmp (s, "0", 1) == 0)
600 {
601 err = memif_create (&c->conn,
602 &args, on_connect, on_disconnect, on_interrupt0,
603 &ctx[index]);
604 if (err != MEMIF_ERR_SUCCESS)
605 {
606 INFO ("memif_create: %s", memif_strerror (err));
607 return 0;
608 }
609 }
610 else if (strncmp (s, "1", 1) == 0)
611 {
612 err = memif_create (&c->conn,
613 &args, on_connect, on_disconnect, on_interrupt1,
614 &ctx[index]);
615 if (err != MEMIF_ERR_SUCCESS)
616 {
617 INFO ("memif_create: %s", memif_strerror (err));
618 return 0;
619 }
620 }
621 else
622 {
623 INFO ("Unknown interrupt descriptor");
624 goto done;
625 }
626 }
627
628 c->index = index;
629 /* alloc memif buffers */
630 c->rx_buf_num = 0;
631 c->tx_buf_num = 0;
632 c->bufs =
633 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
634
635 c->ip_addr[0] = 192;
636 c->ip_addr[1] = 168;
637 c->ip_addr[2] = c->index + 1;
638 c->ip_addr[3] = 2;
639
640 c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
641
642done:
643 return 0;
644}
645
646int
647icmpr_memif_delete (long index)
648{
649 if (index >= MAX_CONNS)
650 {
651 INFO ("connection array overflow");
652 return 0;
653 }
654 if (index < 0)
655 {
656 INFO ("don't even try...");
657 return 0;
658 }
659 memif_connection_t *c = &memif_connection[index];
660
661 if (c->bufs)
662 free (c->bufs);
663 c->bufs = NULL;
664 c->tx_buf_num = 0;
665 c->rx_buf_num = 0;
666
667 int err;
668 /* disconenct then delete memif connection */
669 err = memif_delete (&c->conn);
670 if (err != MEMIF_ERR_SUCCESS)
671 INFO ("memif_delete: %s", memif_strerror (err));
672 if (c->conn != NULL)
673 INFO ("memif delete fail");
674 return 0;
675}
676
677void
678print_help ()
679{
680 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
681#ifdef ICMP_DBG
682 printf (" (debug)");
683#endif
684 printf ("\n");
685 printf ("==============================\n");
686 printf ("libmemif version: %s", LIBMEMIF_VERSION);
687#ifdef MEMIF_DBG
688 printf (" (debug)");
689#endif
690 printf ("\n");
691 printf ("memif version: %d\n", memif_get_version ());
692 printf ("commands:\n");
693 printf ("\thelp - prints this help\n");
694 printf ("\texit - exit app\n");
695 printf
696 ("\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");
697 printf ("\tdel <index> - delete memif\n");
698 printf ("\tshow - show connection details\n");
699 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
700 printf
701 ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
702 printf ("\tsh-count - print counters\n");
703 printf ("\tcl-count - clear counters\n");
704 printf
705 ("\tsend <index> <tx> <ip> <mac> - send icmp, ommit mac to transmit on ip layer\n");
706}
707
708int
709icmpr_free ()
710{
711 /* application cleanup */
712 int err;
713 long i;
714 if (out_fd > 0)
715 close (out_fd);
716 out_fd = -1;
717 for (i = 0; i < MAX_CONNS; i++)
718 {
719 memif_connection_t *c = &memif_connection[i];
720 if (c->conn)
721 icmpr_memif_delete (i);
722 }
723
724 err = memif_cleanup ();
725 if (err != MEMIF_ERR_SUCCESS)
726 INFO ("memif_delete: %s", memif_strerror (err));
727
728 return 0;
729}
730
731int
732icmpr_set_ip (long index, char *ip)
733{
734 if (index >= MAX_CONNS)
735 {
736 INFO ("connection array overflow");
737 return 0;
738 }
739 if (index < 0)
740 {
741 INFO ("don't even try...");
742 return 0;
743 }
744 memif_connection_t *c = &memif_connection[index];
745 if (c->conn == NULL)
746 {
747 INFO ("no connection at index %ld", index);
748 return 0;
749 }
750
751 char *end;
752 char *ui;
753 uint8_t tmp[4];
754 ui = strtok (ip, ".");
755 if (ui == NULL)
756 goto error;
757 tmp[0] = strtol (ui, &end, 10);
758
759 ui = strtok (NULL, ".");
760 if (ui == NULL)
761 goto error;
762 tmp[1] = strtol (ui, &end, 10);
763
764 ui = strtok (NULL, ".");
765 if (ui == NULL)
766 goto error;
767 tmp[2] = strtol (ui, &end, 10);
768
769 ui = strtok (NULL, ".");
770 if (ui == NULL)
771 goto error;
772 tmp[3] = strtol (ui, &end, 10);
773
774 c->ip_addr[0] = tmp[0];
775 c->ip_addr[1] = tmp[1];
776 c->ip_addr[2] = tmp[2];
777 c->ip_addr[3] = tmp[3];
778
779 INFO ("memif %ld ip address set to %u.%u.%u.%u",
780 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
781
782 return 0;
783
784error:
785 INFO ("invalid ip address");
786 return 0;
787}
788
789int
790icmpr_set_rx_mode (long index, long qid, char *mode)
791{
792 if (index >= MAX_CONNS)
793 {
794 INFO ("connection array overflow");
795 return 0;
796 }
797 if (index < 0)
798 {
799 INFO ("don't even try...");
800 return 0;
801 }
802 memif_connection_t *c = &memif_connection[index];
803
804 if (c->conn == NULL)
805 {
806 INFO ("no connection at index %ld", index);
807 return 0;
808 }
809
810 if (strncmp (mode, "interrupt", 9) == 0)
811 {
812 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
813 }
814
815 else if (strncmp (mode, "polling", 7) == 0)
816 {
817 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
818 }
819 else
820 INFO ("expected rx mode <interrupt|polling>");
821 return 0;
822}
823
824void
825icmpr_print_counters ()
826{
827 int i;
828 for (i = 0; i < MAX_CONNS; i++)
829 {
830 memif_connection_t *c = &memif_connection[i];
831 if (c->conn == NULL)
832 continue;
833 printf ("===============================\n");
834 printf ("interface index: %d\n", c->index);
835 printf ("\trx: %lu\n", c->rx_counter);
836 printf ("\ttx: %lu\n", c->tx_counter);
837 printf ("\ttx_err: %lu\n", c->tx_err_counter);
838 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
839 }
840}
841
842void
843icmpr_reset_counters ()
844{
845 int i;
846 for (i = 0; i < MAX_CONNS; i++)
847 {
848 memif_connection_t *c = &memif_connection[i];
849 if (c->conn == NULL)
850 continue;
851 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
852 c->rx_counter = 0;
853 }
854}
855
856void
857icmpr_send_proc ()
858{
859 memif_connection_t *c = &memif_connection[flow->index];
860 if (c->conn == NULL)
861 {
862 INFO ("No connection at index %d. Stopping flow...\n", flow->index);
863 goto stop_flow;
864 }
865 uint16_t tx, i;
866 int err = MEMIF_ERR_SUCCESS;
867
868 if (!flow->start)
869 {
870 flow->start = malloc (sizeof (struct timespec));
871 memset (flow->start, 0, sizeof (struct timespec));
872 timespec_get (flow->start, TIME_UTC);
873 }
874
875 i = 0;
876 err = memif_buffer_alloc (c->conn, 0, c->bufs,
877 MAX_MEMIF_BUFS >
878 flow->packet_count ? flow->packet_count :
879 MAX_MEMIF_BUFS, &tx, 64);
880 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
881 {
882 INFO ("memif_buffer_alloc: %s Stopping flow...\n",
883 memif_strerror (err));
884 goto stop_flow;
885 }
886 c->tx_buf_num += tx;
887
888 while (tx)
889 {
890 generate_packet2 ((void *) c->bufs[i].data,
891 &c->bufs[i].len, c->ip_addr,
892 flow->ip_daddr, flow->hw_daddr, (flow->sequence)++,
893 flow->mode);
894 i++;
895 tx--;
896 }
897 err = memif_tx_burst (c->conn, 0, c->bufs, i, &tx);
898 if (err != MEMIF_ERR_SUCCESS)
899 {
900 INFO ("memif_tx_burst: %s Stopping flow...\n", memif_strerror (err));
901 goto stop_flow;
902 }
903 c->tx_buf_num -= tx;
904 c->tx_counter += tx;
905 flow->tx += tx;
906 flow->packet_count -= tx;
907
908 if (flow->packet_count == 0)
909 {
910 timespec_get (&flow->end, TIME_UTC);
911 INFO ("Flow finished!");
912 INFO ("Flow length: %lu", flow->tx);
913 uint64_t t1 = flow->end.tv_sec - flow->start->tv_sec;
914 uint64_t t2;
915 if (flow->end.tv_nsec > flow->start->tv_nsec)
916 {
917 t2 = flow->end.tv_nsec - flow->start->tv_nsec;
918 }
919 else
920 {
921 t2 = flow->start->tv_nsec - flow->end.tv_nsec;
922 t1--;
923 }
924 c->t_sec = t1;
925 c->t_nsec = t2;
926 INFO ("Flow time: %lus %luns", t1, t2);
927 double tmp = t1;
928 tmp += t2 / 1e+9;
929 tmp = flow->tx / tmp;
930 INFO ("Average pps: %f", tmp);
931 INFO ("Stopping flow...");
932 goto stop_flow;
933 }
934
935 return;
936
937stop_flow:
938 if (flow)
939 {
940 if (flow->start)
941 free (flow->start);
942 free (flow);
943 }
944 flow = NULL;
945 return;
946}
947
948int
949icmpr_send (long index, long packet_num, char *input)
950{
951 if (flow)
952 {
953 printf ("only one flow allowed\n");
954 return 0;
955 }
956
957 memif_connection_t *c = &memif_connection[index];
958 char *end;
959 char *ui;
960 uint8_t tmp[6];
961 if (c->conn == NULL)
962 return -1;
963
964 flow = malloc (sizeof (icmpr_flow_t));
965 flow->index = index;
966 flow->packet_count = packet_num;
967 flow->sequence = 0;
968 flow->tx = 0;
969 flow->start = NULL;
970 memset (&flow->end, 0, sizeof (struct timespec));
971
972 INFO ("packet count: %lu", flow->packet_count);
973 printf ("%s\n", input);
974
975 ui = strtok (input, ".");
976 if (ui == NULL)
977 goto error;
978 tmp[0] = strtol (ui, &end, 10);
979
980 ui = strtok (NULL, ".");
981 if (ui == NULL)
982 goto error;
983 tmp[1] = strtol (ui, &end, 10);
984
985 ui = strtok (NULL, ".");
986 if (ui == NULL)
987 goto error;
988 tmp[2] = strtol (ui, &end, 10);
989
990 ui = strtok (NULL, ".");
991 if (ui == NULL)
992 goto error;
993 tmp[3] = strtol (ui, &end, 10);
994
995 flow->ip_daddr[0] = tmp[0];
996 flow->ip_daddr[1] = tmp[1];
997 flow->ip_daddr[2] = tmp[2];
998 flow->ip_daddr[3] = tmp[3];
999
1000 ui = strtok (NULL, " ");
1001 if (ui == NULL)
1002 {
1003 flow->mode = ICMPR_FLOW_MODE_IP;
1004 return 0;
1005 }
1006
1007 ui = strtok (NULL, ":");
1008 if (ui == NULL)
1009 goto error;
1010 tmp[0] = strtol (ui, &end, 16);
1011 ui = strtok (NULL, ":");
1012 if (ui == NULL)
1013 goto error;
1014 tmp[1] = strtol (ui, &end, 16);
1015 ui = strtok (NULL, ":");
1016 if (ui == NULL)
1017 goto error;
1018 tmp[2] = strtol (ui, &end, 16);
1019 ui = strtok (NULL, ":");
1020 if (ui == NULL)
1021 goto error;
1022 tmp[3] = strtol (ui, &end, 16);
1023 ui = strtok (NULL, ":");
1024 if (ui == NULL)
1025 goto error;
1026 tmp[4] = strtol (ui, &end, 16);
1027 ui = strtok (NULL, ":");
1028 if (ui == NULL)
1029 goto error;
1030 tmp[5] = strtol (ui, &end, 16);
1031
1032 flow->hw_daddr[0] = tmp[0];
1033 flow->hw_daddr[1] = tmp[1];
1034 flow->hw_daddr[2] = tmp[2];
1035 flow->hw_daddr[3] = tmp[3];
1036 flow->hw_daddr[4] = tmp[4];
1037 flow->hw_daddr[5] = tmp[5];
1038
1039 flow->mode = ICMPR_FLOW_MODE_ETH;
1040
1041 return 0;
1042
1043error:
1044 INFO ("Invalid input\n");
1045 if (flow)
1046 free (flow);
1047 flow = NULL;
1048 return 0;
1049}
1050
1051int
1052user_input_handler ()
1053{
1054 int i;
1055 char *in = (char *) malloc (256);
1056 char *ui = fgets (in, 256, stdin);
1057 char *end;
1058 long a;
1059 if (in[0] == '\n')
1060 goto done;
1061 ui = strtok (in, " ");
1062 if (strncmp (ui, "exit", 4) == 0)
1063 {
1064 free (in);
1065 icmpr_free ();
1066 exit (EXIT_SUCCESS);
1067 }
1068 else if (strncmp (ui, "help", 4) == 0)
1069 {
1070 print_help ();
1071 goto done;
1072 }
1073 else if (strncmp (ui, "conn", 4) == 0)
1074 {
1075 ui = strtok (NULL, " ");
1076 if (ui != NULL)
1077 a = strtol (ui, &end, 10);
1078 else
1079 {
1080 INFO ("expected id");
1081 goto done;
1082 }
1083 ui = strtok (NULL, " ");
1084 if (ui != NULL)
1085 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1086 else
1087 INFO ("expected mode <0|1>");
1088 goto done;
1089 }
1090 else if (strncmp (ui, "del", 3) == 0)
1091 {
1092 ui = strtok (NULL, " ");
1093 if (ui != NULL)
1094 icmpr_memif_delete (strtol (ui, &end, 10));
1095 else
1096 INFO ("expected id");
1097 goto done;
1098 }
1099 else if (strncmp (ui, "show", 4) == 0)
1100 {
1101 print_memif_details ();
1102 goto done;
1103 }
1104 else if (strncmp (ui, "ip-set", 6) == 0)
1105 {
1106 ui = strtok (NULL, " ");
1107 if (ui != NULL)
1108 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
1109 else
1110 INFO ("expected id");
1111 goto done;
1112 }
1113 else if (strncmp (ui, "rx-mode", 7) == 0)
1114 {
1115 ui = strtok (NULL, " ");
1116 if (ui != NULL)
1117 a = strtol (ui, &end, 10);
1118 else
1119 {
1120 INFO ("expected id");
1121 goto done;
1122 }
1123 ui = strtok (NULL, " ");
1124 if (ui != NULL)
1125 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1126 else
1127 INFO ("expected qid");
1128 goto done;
1129 }
1130 else if (strncmp (ui, "sh-count", 8) == 0)
1131 {
1132 icmpr_print_counters ();
1133 }
1134 else if (strncmp (ui, "cl-count", 8) == 0)
1135 {
1136 icmpr_reset_counters ();
1137 }
1138 else if (strncmp (ui, "send", 4) == 0)
1139 {
1140 ui = strtok (NULL, " ");
1141 if (ui != NULL)
1142 a = strtol (ui, &end, 10);
1143 else
1144 {
1145 INFO ("expected id");
1146 goto done;
1147 }
1148 ui = strtok (NULL, " ");
1149 if (ui != NULL)
1150 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "));
1151 else
1152 INFO ("expected count");
1153 goto done;
1154 }
1155 else
1156 {
1157 INFO ("unknown command: %s", ui);
1158 goto done;
1159 }
1160
1161 return 0;
1162done:
1163 free (in);
1164 return 0;
1165}
1166
1167int
1168poll_event (int timeout)
1169{
1170 struct epoll_event evt, *e;
1171 int app_err = 0, memif_err = 0, en = 0;
1172 int tmp, nfd;
1173 uint32_t events = 0;
1174 struct timespec start, end;
1175 memset (&evt, 0, sizeof (evt));
1176 evt.events = EPOLLIN | EPOLLOUT;
1177 sigset_t sigset;
1178 sigemptyset (&sigset);
1179 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
1180 /* id event polled */
1181 timespec_get (&start, TIME_UTC);
1182 if (en < 0)
1183 {
1184 DBG ("epoll_pwait: %s", strerror (errno));
1185 return -1;
1186 }
1187 if (en > 0)
1188 {
1189 /* this app does not use any other file descriptors than stds and memif control fds */
1190 if (evt.data.fd > 2)
1191 {
1192 /* event of memif control fd */
1193 /* convert epolle events to memif events */
1194 if (evt.events & EPOLLIN)
1195 events |= MEMIF_FD_EVENT_READ;
1196 if (evt.events & EPOLLOUT)
1197 events |= MEMIF_FD_EVENT_WRITE;
1198 if (evt.events & EPOLLERR)
1199 events |= MEMIF_FD_EVENT_ERROR;
1200 memif_err = memif_control_fd_handler (evt.data.fd, events);
1201 if (memif_err != MEMIF_ERR_SUCCESS)
1202 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1203 }
1204 else if (evt.data.fd == 0)
1205 {
1206 app_err = user_input_handler ();
1207 }
1208 else
1209 {
1210 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1211 }
1212 }
1213
1214 timespec_get (&end, TIME_UTC);
1215 LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
1216
1217 if ((app_err < 0) || (memif_err < 0))
1218 {
1219 if (app_err < 0)
1220 DBG ("user input handler error");
1221 if (memif_err < 0)
1222 DBG ("memif control fd handler error");
1223 return -1;
1224 }
1225
1226 return 0;
1227}
1228
1229int
1230main ()
1231{
1232 epfd = epoll_create (1);
1233 add_epoll_fd (0, EPOLLIN);
1234
1235 flow = NULL;
1236
1237#ifdef LOG_FILE
1238 remove (LOG_FILE);
1239 enable_log = 0;
1240
1241 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1242 if (out_fd < 0)
1243 INFO ("Error opening log file: %s", strerror (errno));
1244#endif /* LOG_FILE */
1245
1246 /* initialize memory interface */
1247 int err, i;
1248 /* if valid callback is passed as argument, fd event polling will be done by user
1249 all file descriptors and events will be passed to user in this callback */
1250 /* if callback is set to NULL libmemif will handle fd event polling */
msardara8f554b72018-12-11 18:36:55 +01001251 err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001252 if (err != MEMIF_ERR_SUCCESS)
1253 {
1254 INFO ("memif_init: %s", memif_strerror (err));
1255 icmpr_free ();
1256 exit (-1);
1257 }
1258
1259 for (i = 0; i < MAX_CONNS; i++)
1260 {
1261 memif_connection[i].conn = NULL;
1262 ctx[i] = i;
1263 }
1264
1265 print_help ();
1266
1267 /* main loop */
1268 while (1)
1269 {
1270 if (poll_event (0) < 0)
1271 {
1272 DBG ("poll_event error!");
1273 }
1274 if (flow)
1275 {
1276 icmpr_send_proc ();
1277 }
1278 }
1279}