blob: 860569bc20bdd187a95cc63f731f95d0e4cdab2d [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
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#include <pthread.h>
47
48#include <libmemif.h>
49#include <icmp_proto.h>
50
51#define APP_NAME "ICMP_Responder"
52#define IF_NAME "memif_connection"
53
54
55#ifdef ICMP_DBG
56#define DBG(...) do { \
57 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \
58 printf (__VA_ARGS__); \
59 printf ("\n"); \
60 } while (0)
61#else
62#define DBG(...)
63#endif
64
65#define INFO(...) do { \
66 printf ("INFO: "__VA_ARGS__); \
67 printf ("\n"); \
68 } while (0)
69
70/* maximum tx/rx memif buffers */
71#define MAX_MEMIF_BUFS 256
72#define MAX_CONNS 50
73#define MAX_QUEUES 2
74#define MAX_THREADS ((MAX_CONNS) * (MAX_QUEUES))
75
76int main_epfd;
77
78typedef struct
79{
80 /* thread id */
81 uint8_t id;
82 /* memif connection index */
83 uint16_t index;
84 /* id of queue to be handled by thread */
85 uint8_t qid;
86 uint8_t isRunning;
87
88 uint16_t rx_buf_num;
89 uint16_t tx_buf_num;
90 memif_buffer_t *rx_bufs;
91 memif_buffer_t *tx_bufs;
92} memif_thread_data_t;
93
94typedef struct
95{
96 uint16_t index;
97 /* memif conenction handle */
98 memif_conn_handle_t conn;
99 /* interface ip address */
100 uint8_t ip_addr[4];
101 /* inform pthread about connection termination */
102 uint8_t pending_del;
103} memif_connection_t;
104
105memif_connection_t memif_connection[MAX_CONNS];
106long ctx[MAX_CONNS];
107
108/* thread data specific for each thread */
109memif_thread_data_t thread_data[MAX_THREADS];
110pthread_t thread[MAX_THREADS];
111
112void
113user_signal_handler (int sig)
114{
115 sig = sig;
116}
117
118static void
119print_memif_details ()
120{
121 memif_details_t md;
122 ssize_t buflen;
123 char *buf;
124 int err, i, e, ti;
125 buflen = 2048;
126 buf = malloc (buflen);
127 printf ("MEMIF DETAILS\n");
128 printf ("==============================\n");
129 for (i = 0; i < MAX_CONNS; i++)
130 {
131 memif_connection_t *c = &memif_connection[i];
132
133 memset (&md, 0, sizeof (md));
134 memset (buf, 0, buflen);
135
136 err = memif_get_details (c->conn, &md, buf, buflen);
137 if (err != MEMIF_ERR_SUCCESS)
138 {
139 if (err != MEMIF_ERR_NOCONN)
140 INFO ("%s", memif_strerror (err));
141 continue;
142 }
143
144 printf ("interface index: %d\n", i);
145
146 printf ("\tinterface ip: %u.%u.%u.%u\n",
147 c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
148 printf ("\tinterface name: %s\n", (char *) md.if_name);
149 printf ("\tapp name: %s\n", (char *) md.inst_name);
150 printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
151 printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
152 printf ("\tid: %u\n", md.id);
153 printf ("\tsecret: %s\n", (char *) md.secret);
154 printf ("\trole: ");
155 if (md.role)
156 printf ("slave\n");
157 else
158 printf ("master\n");
159 printf ("\tmode: ");
160 switch (md.mode)
161 {
162 case 0:
163 printf ("ethernet\n");
164 break;
165 case 1:
166 printf ("ip\n");
167 break;
168 case 2:
169 printf ("punt/inject\n");
170 break;
171 default:
172 printf ("unknown\n");
173 break;
174 }
175 printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
176 printf ("\trx queues:\n");
177 for (e = 0; e < md.rx_queues_num; e++)
178 {
179 ti = (i * MAX_QUEUES) + e;
180 printf ("\tqueue id: %u\n", md.rx_queues[e].qid);
181 printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
182 printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
183 printf ("\t\tthread id: %u\n", thread_data[ti].id);
184 printf ("\t\tthread connection index: %u\n", thread_data[ti].index);
185 printf ("\t\tthread running: ");
186 if (thread_data[ti].isRunning)
187 printf ("yes\n");
188 else
189 printf ("no");
190 }
191 printf ("\ttx queues:\n");
192 for (e = 0; e < md.tx_queues_num; e++)
193 {
194 printf ("\tqueue id: %u\n", md.tx_queues[e].qid);
195 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
196 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
197 }
198 printf ("\tlink: ");
199 if (md.link_up_down)
200 printf ("up\n");
201 else
202 printf ("down\n");
203 }
204 free (buf);
205}
206
207int
208add_epoll_fd (int epfd, int fd, uint32_t events)
209{
210 if (fd < 0)
211 {
212 DBG ("invalid fd %d", fd);
213 return -1;
214 }
215 struct epoll_event evt;
216 memset (&evt, 0, sizeof (evt));
217 evt.events = events;
218 evt.data.fd = fd;
219 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
220 {
221 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
222 return -1;
223 }
224 DBG ("fd %d added to epoll", fd);
225 return 0;
226}
227
228int
229mod_epoll_fd (int epfd, int fd, uint32_t events)
230{
231 if (fd < 0)
232 {
233 DBG ("invalid fd %d", fd);
234 return -1;
235 }
236 struct epoll_event evt;
237 memset (&evt, 0, sizeof (evt));
238 evt.events = events;
239 evt.data.fd = fd;
240 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
241 {
242 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
243 return -1;
244 }
245 DBG ("fd %d moddified on epoll", fd);
246 return 0;
247}
248
249int
250del_epoll_fd (int epfd, int fd)
251{
252 if (fd < 0)
253 {
254 DBG ("invalid fd %d", fd);
255 return -1;
256 }
257 struct epoll_event evt;
258 memset (&evt, 0, sizeof (evt));
259 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
260 {
261 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
262 return -1;
263 }
264 DBG ("fd %d removed from epoll", fd);
265 return 0;
266}
267
268void *
269memif_rx_poll (void *ptr)
270{
271 memif_thread_data_t *data = (memif_thread_data_t *) ptr;
272 memif_connection_t *c = &memif_connection[data->index];
273 int err;
274 uint16_t rx = 0, tx = 0, fb = 0;
275
276 data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
277 data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
278 data->rx_buf_num = 0;
279 data->tx_buf_num = 0;
280
281 data->isRunning = 1;
282 INFO ("pthread id %u starts in polling mode", data->id);
283
284 while (1)
285 {
286 if (c->pending_del)
287 goto close;
288
289 /* receive data from shared memory buffers */
290 err =
291 memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS,
292 &rx);
293 if (err != MEMIF_ERR_SUCCESS)
294 {
295 INFO ("memif_rx_burst: %s", memif_strerror (err));
296 data->rx_buf_num += rx;
297 goto error;
298 }
299 data->rx_buf_num += rx;
300 if (rx == 0)
301 {
302 continue;
303 }
304
305 DBG ("thread id: %u", data->id);
306
307 DBG ("received %d buffers. %u/%u alloc/free buffers",
308 rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
309
310 err =
311 memif_buffer_alloc (c->conn, data->qid, data->tx_bufs,
312 data->rx_buf_num, &tx);
313 if (err != MEMIF_ERR_SUCCESS)
314 {
315 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
316 data->tx_buf_num += tx;
317 goto error;
318 }
319 data->tx_buf_num += tx;
320 DBG ("allocated %d/%d buffers, %u free buffers",
321 tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num);
322
323 int i;
324 for (i = 0; i < rx; i++)
325 {
326 resolve_packet ((void *) (data->rx_bufs + i)->data,
327 (data->rx_bufs + i)->data_len,
328 (void *) (data->tx_bufs + i)->data,
329 &(data->tx_bufs + i)->data_len, c->ip_addr);
330 }
331
332 /* mark memif buffers and shared memory buffers as free */
333 err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
334 if (err != MEMIF_ERR_SUCCESS)
335 INFO ("memif_buffer_free: %s", memif_strerror (err));
336 data->rx_buf_num -= fb;
337
338 DBG ("freed %d buffers. %u/%u alloc/free buffers",
339 fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
340
341 err =
342 memif_tx_burst (c->conn, data->qid, data->tx_bufs, data->tx_buf_num,
343 &tx);
344 if (err != MEMIF_ERR_SUCCESS)
345 {
346 INFO ("memif_tx_burst: %s", memif_strerror (err));
347 goto error;
348 }
349 DBG ("tx: %d/%u", tx, data->tx_buf_num);
350 data->tx_buf_num -= tx;
351 }
352
353error:
354 INFO ("thread %u error!", data->id);
355 goto close;
356
357close:
358 err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
359 if (err != MEMIF_ERR_SUCCESS)
360 INFO ("memif_buffer_free: %s", memif_strerror (err));
361 data->rx_buf_num -= fb;
362 DBG ("freed %d buffers. %u/%u alloc/free buffers",
363 fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
364 free (data->rx_bufs);
365 free (data->tx_bufs);
366 data->isRunning = 0;
367 INFO ("pthread id %u exit", data->id);
368 pthread_exit (NULL);
369}
370
371void *
372memif_rx_interrupt (void *ptr)
373{
374 memif_thread_data_t *data = (memif_thread_data_t *) ptr;
375 memif_connection_t *c = &memif_connection[data->index];
376 int err;
377 uint16_t rx = 0, tx = 0, fb = 0;
378 struct epoll_event evt, *e;
379 int en = 0;
380 uint32_t events = 0;
381 sigset_t sigset;
382
383 signal (SIGUSR1, user_signal_handler);
384
385 data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
386 data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
387 data->rx_buf_num = 0;
388 data->tx_buf_num = 0;
389
390 data->isRunning = 1;
391 INFO ("pthread id %u starts in interrupt mode", data->id);
392 int thread_epfd = epoll_create (1);
393
394 /* get interrupt queue id */
395 int fd = -1;
396 err = memif_get_queue_efd (c->conn, data->qid, &fd);
397 if (err != MEMIF_ERR_SUCCESS)
398 {
399 INFO ("memif_get_queue_efd: %s", memif_strerror (err));
400 goto error;
401 }
402 add_epoll_fd (thread_epfd, fd, EPOLLIN);
403
404 while (1)
405 {
406 memset (&evt, 0, sizeof (evt));
407 evt.events = EPOLLIN | EPOLLOUT;
408 sigemptyset (&sigset);
409 en = epoll_pwait (thread_epfd, &evt, 1, -1, &sigset);
410 if (en < 0)
411 {
412 if (errno == EINTR)
413 goto close;
414 DBG ("epoll_pwait: %s", strerror (errno));
415 goto error;
416 }
417 else if (en > 0)
418 {
419 /* receive data from shared memory buffers */
420 err =
421 memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS,
422 &rx);
423 if (err != MEMIF_ERR_SUCCESS)
424 {
425 INFO ("memif_rx_burst: %s", memif_strerror (err));
426 data->rx_buf_num += rx;
427 goto error;
428 }
429 data->rx_buf_num += rx;
430 if (rx == 0)
431 {
432 continue;
433 }
434
435 DBG ("thread id: %u", data->id);
436
437 DBG ("received %d buffers. %u/%u alloc/free buffers",
438 rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
439
440 err =
441 memif_buffer_alloc (c->conn, data->qid, data->tx_bufs,
442 data->rx_buf_num, &tx);
443 if (err != MEMIF_ERR_SUCCESS)
444 {
445 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
446 data->tx_buf_num += tx;
447 goto error;
448 }
449 data->tx_buf_num += tx;
450 DBG ("allocated %d/%d buffers, %u free buffers",
451 tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num);
452
453 int i;
454 for (i = 0; i < rx; i++)
455 {
456 resolve_packet ((void *) (data->rx_bufs + i)->data,
457 (data->rx_bufs + i)->data_len,
458 (void *) (data->tx_bufs + i)->data,
459 &(data->tx_bufs + i)->data_len, c->ip_addr);
460 }
461
462 /* mark memif buffers and shared memory buffers as free */
463 err =
464 memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
465 if (err != MEMIF_ERR_SUCCESS)
466 INFO ("memif_buffer_free: %s", memif_strerror (err));
467 data->rx_buf_num -= fb;
468
469 DBG ("freed %d buffers. %u/%u alloc/free buffers",
470 fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
471
472 err =
473 memif_tx_burst (c->conn, data->qid, data->tx_bufs,
474 data->tx_buf_num, &tx);
475 if (err != MEMIF_ERR_SUCCESS)
476 {
477 INFO ("memif_tx_burst: %s", memif_strerror (err));
478 goto error;
479 }
480 DBG ("tx: %d/%u", tx, data->tx_buf_num);
481 data->tx_buf_num -= tx;
482 }
483 }
484
485error:
486 INFO ("thread %u error!", data->id);
487 goto close;
488
489close:
490 err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
491 if (err != MEMIF_ERR_SUCCESS)
492 INFO ("memif_buffer_free: %s", memif_strerror (err));
493 data->rx_buf_num -= fb;
494 DBG ("freed %d buffers. %u/%u alloc/free buffers",
495 fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
496 free (data->rx_bufs);
497 free (data->tx_bufs);
498 data->isRunning = 0;
499 INFO ("pthread id %u exit", data->id);
500 pthread_exit (NULL);
501
502}
503
504/* informs user about connected status. private_ctx is used by user to identify connection
505 (multiple connections WIP) */
506int
507on_connect (memif_conn_handle_t conn, void *private_ctx)
508{
509 long index = (*(long *) private_ctx);
510 int err, i, ti;
511 INFO ("memif connected! index %ld", index);
512 memif_connection_t *c = &memif_connection[index];
513 c->pending_del = 0;
514
515 for (i = 0; i < MAX_QUEUES; i++)
516 {
517 err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, i);
518 if (err != MEMIF_ERR_SUCCESS)
519 INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), i);
520 else
521 {
522 ti = (index * MAX_QUEUES) + i;
523 if (thread_data[ti].isRunning)
524 {
525 INFO ("thread id: %d already running!", ti);
526 continue;
527 }
528 thread_data[ti].index = index;
529 thread_data[ti].qid = i;
530 thread_data[ti].id = ti;
531 if ((i % 2) == 0)
532 pthread_create (&thread[ti],
533 NULL, memif_rx_poll, (void *) &thread_data[ti]);
534 else
535 pthread_create (&thread[ti],
536 NULL, memif_rx_interrupt,
537 (void *) &thread_data[ti]);
538 }
539
540 }
541 return 0;
542}
543
544/* informs user about disconnected status. private_ctx is used by user to identify connection
545 (multiple connections WIP) */
546int
547on_disconnect (memif_conn_handle_t conn, void *private_ctx)
548{
549 void *ptr;
550 long index = (*(long *) private_ctx);
551 memif_connection_t *c = &memif_connection[index];
552 int i, ti;
553 INFO ("memif disconnected!");
554 /* inform thread in polling mode about memif disconenction */
555 c->pending_del = 1;
556 for (i = 0; i < MAX_QUEUES; i++)
557 {
558 ti = (index * MAX_QUEUES) + i;
559 if (!thread_data[ti].isRunning)
560 continue;
561 if ((i % 2) != 0)
562 pthread_kill (thread[ti], SIGUSR1); /* interrupt thread in interrupt mode */
563 pthread_join (thread[ti], &ptr);
564 }
565 return 0;
566}
567
568/* user needs to watch new fd or stop watching fd that is about to be closed.
569 control fd will be modified during connection establishment to minimize CPU usage */
570int
571control_fd_update (int fd, uint8_t events)
572{
573 /* convert memif event definitions to epoll events */
574 if (events & MEMIF_FD_EVENT_DEL)
575 return del_epoll_fd (main_epfd, fd);
576
577 uint32_t evt = 0;
578 if (events & MEMIF_FD_EVENT_READ)
579 evt |= EPOLLIN;
580 if (events & MEMIF_FD_EVENT_WRITE)
581 evt |= EPOLLOUT;
582
583 if (events & MEMIF_FD_EVENT_MOD)
584 return mod_epoll_fd (main_epfd, fd, evt);
585
586 return add_epoll_fd (main_epfd, fd, evt);
587}
588
589int
590icmpr_memif_create (long index)
591{
592 if (index >= MAX_CONNS)
593 {
594 INFO ("connection array overflow");
595 return 0;
596 }
597 if (index < 0)
598 {
599 INFO ("don't even try...");
600 return 0;
601 }
602 memif_connection_t *c = &memif_connection[index];
603
604 /* setting memif connection arguments */
605 memif_conn_args_t args;
606 int fd = -1;
607 memset (&args, 0, sizeof (args));
608 args.is_master = 0;
609 args.log2_ring_size = 10;
610 args.buffer_size = 2048;
611 args.num_s2m_rings = 2;
612 args.num_m2s_rings = 2;
613 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
614 strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME));
615 args.mode = 0;
616 /* socket filename is not specified, because this app is supposed to
617 connect to VPP over memif. so default socket filename will be used */
618 /* default socketfile = /run/vpp/memif.sock */
619
620 args.interface_id = index;
621 /* last argument for memif_create (void * private_ctx) is used by user
622 to identify connection. this context is returned with callbacks */
623 int err = memif_create (&c->conn,
624 &args, on_connect, on_disconnect, NULL,
625 &ctx[index]);
626 if (err != MEMIF_ERR_SUCCESS)
627 {
628 INFO ("memif_create: %s", memif_strerror (err));
629 return 0;
630 }
631
632 c->index = index;
633
634 c->ip_addr[0] = 192;
635 c->ip_addr[1] = 168;
636 c->ip_addr[2] = c->index + 1;
637 c->ip_addr[3] = 2;
638 return 0;
639}
640
641int
642icmpr_memif_delete (long index)
643{
644 if (index >= MAX_CONNS)
645 {
646 INFO ("connection array overflow");
647 return 0;
648 }
649 if (index < 0)
650 {
651 INFO ("don't even try...");
652 return 0;
653 }
654 memif_connection_t *c = &memif_connection[index];
655
656 int err;
657 /* disconenct then delete memif connection */
658 err = memif_delete (&c->conn);
659 if (err != MEMIF_ERR_SUCCESS)
660 INFO ("memif_delete: %s", memif_strerror (err));
661 return 0;
662}
663
664void
665print_help ()
666{
667 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
668#ifdef ICMP_DBG
669 printf (" (debug)");
670#endif
671 printf ("\n");
672 printf ("==============================\n");
673 printf ("libmemif version: %s", LIBMEMIF_VERSION);
674#ifdef MEMIF_DBG
675 printf (" (debug)");
676#endif
677 printf ("\n");
678 printf ("memif version: %d\n", MEMIF_VERSION);
679 printf ("commands:\n");
680 printf ("\thelp - prints this help\n");
681 printf ("\texit - exit app\n");
682 printf ("\tconn <index> - create memif (slave-mode)\n");
683 printf ("\tdel <index> - delete memif\n");
684 printf ("\tshow - show connection details\n");
685 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
686}
687
688int
689icmpr_free ()
690{
691 /* application cleanup */
692 int err;
693 long i;
694 for (i = 0; i < MAX_CONNS; i++)
695 {
696 memif_connection_t *c = &memif_connection[i];
697 if (c->conn)
698 icmpr_memif_delete (i);
699 }
700
701 err = memif_cleanup ();
702 if (err != MEMIF_ERR_SUCCESS)
703 INFO ("memif_delete: %s", memif_strerror (err));
704
705 return 0;
706}
707
708int
709icmpr_set_ip (long index, char *ip)
710{
711 if (index >= MAX_CONNS)
712 {
713 INFO ("connection array overflow");
714 return 0;
715 }
716 if (index < 0)
717 {
718 INFO ("don't even try...");
719 return 0;
720 }
721 memif_connection_t *c = &memif_connection[index];
722 if (c->conn == NULL)
723 {
724 INFO ("no connection at index %ld", index);
725 return 0;
726 }
727
728 char *end;
729 char *ui;
730 uint8_t tmp[4];
731 ui = strtok (ip, ".");
732 if (ui == NULL)
733 goto error;
734 tmp[0] = strtol (ui, &end, 10);
735
736 ui = strtok (NULL, ".");
737 if (ui == NULL)
738 goto error;
739 tmp[1] = strtol (ui, &end, 10);
740
741 ui = strtok (NULL, ".");
742 if (ui == NULL)
743 goto error;
744 tmp[2] = strtol (ui, &end, 10);
745
746 ui = strtok (NULL, ".");
747 if (ui == NULL)
748 goto error;
749 tmp[3] = strtol (ui, &end, 10);
750
751 c->ip_addr[0] = tmp[0];
752 c->ip_addr[1] = tmp[1];
753 c->ip_addr[2] = tmp[2];
754 c->ip_addr[3] = tmp[3];
755
756 INFO ("memif %ld ip address set to %u.%u.%u.%u",
757 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
758
759 return 0;
760
761error:
762 INFO ("invalid ip address");
763 return 0;
764}
765
766
767int
768user_input_handler ()
769{
770 int i;
771 char *in = (char *) malloc (256);
772 char *ui = fgets (in, 256, stdin);
773 char *end;
774 if (in[0] == '\n')
775 goto done;
776 ui = strtok (in, " ");
777 if (strncmp (ui, "exit", 4) == 0)
778 {
779 free (in);
780 icmpr_free ();
781 exit (EXIT_SUCCESS);
782 }
783 else if (strncmp (ui, "help", 4) == 0)
784 {
785 print_help ();
786 goto done;
787 }
788 else if (strncmp (ui, "conn", 4) == 0)
789 {
790 ui = strtok (NULL, " ");
791 if (ui != NULL)
792 icmpr_memif_create (strtol (ui, &end, 10));
793 else
794 INFO ("expected id");
795 goto done;
796 }
797 else if (strncmp (ui, "del", 3) == 0)
798 {
799 ui = strtok (NULL, " ");
800 if (ui != NULL)
801 icmpr_memif_delete (strtol (ui, &end, 10));
802 else
803 INFO ("expected id");
804 goto done;
805 }
806 else if (strncmp (ui, "show", 4) == 0)
807 {
808 print_memif_details ();
809 goto done;
810 }
811 else if (strncmp (ui, "ip-set", 6) == 0)
812 {
813 ui = strtok (NULL, " ");
814 if (ui != NULL)
815 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
816 else
817 INFO ("expected id");
818 goto done;
819 }
820 else
821 {
822 DBG ("unknown command: %s", ui);
823 goto done;
824 }
825
826 return 0;
827done:
828 free (in);
829 return 0;
830}
831
832int
833poll_event (int timeout)
834{
835 struct epoll_event evt, *e;
836 int app_err = 0, memif_err = 0, en = 0;
837 int tmp, nfd;
838 uint32_t events = 0;
839 memset (&evt, 0, sizeof (evt));
840 evt.events = EPOLLIN | EPOLLOUT;
841 sigset_t sigset;
842 sigemptyset (&sigset);
843 en = epoll_pwait (main_epfd, &evt, 1, timeout, &sigset);
844 if (en < 0)
845 {
846 DBG ("epoll_pwait: %s", strerror (errno));
847 return -1;
848 }
849 if (en > 0)
850 {
851 /* this app does not use any other file descriptors than stds and memif control fds */
852 if (evt.data.fd > 2)
853 {
854 /* event of memif control fd */
855 /* convert epolle events to memif events */
856 if (evt.events & EPOLLIN)
857 events |= MEMIF_FD_EVENT_READ;
858 if (evt.events & EPOLLOUT)
859 events |= MEMIF_FD_EVENT_WRITE;
860 if (evt.events & EPOLLERR)
861 events |= MEMIF_FD_EVENT_ERROR;
862 memif_err = memif_control_fd_handler (evt.data.fd, events);
863 if (memif_err != MEMIF_ERR_SUCCESS)
864 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
865 }
866 else if (evt.data.fd == 0)
867 {
868 app_err = user_input_handler ();
869 }
870 else
871 {
872 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
873 }
874 }
875
876 if ((app_err < 0) || (memif_err < 0))
877 {
878 if (app_err < 0)
879 DBG ("user input handler error");
880 if (memif_err < 0)
881 DBG ("memif control fd handler error");
882 return -1;
883 }
884
885 return 0;
886}
887
888int
889main ()
890{
891 main_epfd = epoll_create (1);
892 add_epoll_fd (main_epfd, 0, EPOLLIN);
893
894 /* initialize memory interface */
895 int err, i;
896 /* if valid callback is passed as argument, fd event polling will be done by user
897 all file descriptors and events will be passed to user in this callback */
898 /* if callback is set to NULL libmemif will handle fd event polling */
899 err = memif_init (control_fd_update, APP_NAME);
900 if (err != MEMIF_ERR_SUCCESS)
901 INFO ("memif_init: %s", memif_strerror (err));
902
903 for (i = 0; i < MAX_CONNS; i++)
904 {
905 memif_connection[i].conn = NULL;
906 ctx[i] = i;
907 }
908
909 memset (&thread_data, 0, sizeof (memif_thread_data_t) * MAX_THREADS);
910
911 print_help ();
912
913 /* main loop */
914 while (1)
915 {
916 if (poll_event (-1) < 0)
917 {
918 DBG ("poll_event error!");
919 }
920 }
921}