28 #include <sys/epoll.h> 29 #include <sys/eventfd.h> 35 #define APP_NAME "ICMP_Responder_mt_v3.1" 36 #define IF_NAME "memif_connection" 39 #define DBG(...) do { \ 40 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ 41 printf (__VA_ARGS__); \ 48 #define ICMPR_BUFFER_LENGTH 32 49 #define ICMPR_SOCKET_FILENAME_LEN 256 50 #define ICMPR_MEMIF_BUFFER_NUM 256 53 {
"threads", required_argument, 0,
't'},
54 {
"if_num", required_argument, 0,
'i'}
93 DBG (
"invalid fd %d", fd);
96 struct epoll_event evt;
97 memset (&evt, 0,
sizeof (evt));
100 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
102 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
105 DBG (
"fd %d added to epoll", fd);
114 DBG (
"invalid fd %d", fd);
117 struct epoll_event evt;
118 memset (&evt, 0,
sizeof (evt));
121 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
123 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
126 DBG (
"fd %d moddified on epoll", fd);
135 DBG (
"invalid fd %d", fd);
138 struct epoll_event evt;
139 memset (&evt, 0,
sizeof (evt));
140 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
142 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
145 DBG (
"fd %d removed from epoll", fd);
181 while (i < ptd->if_num && ptd->
conns[i].
handle != conn)
186 DBG (
"Connected: %u", c->
id);
200 while (i < ptd->if_num && ptd->
conns[i].
handle != conn)
205 DBG (
"Disconnected: %u", c->
id);
224 while (i < ptd->if_num && ptd->
conns[i].
handle != conn)
229 err =
memif_rx_burst (conn, qid, mbufs, ICMPR_MEMIF_BUFFER_NUM, &rx);
237 for (i = 0; i < rx; i++)
274 struct epoll_event evt;
277 memset (&evt, 0,
sizeof (evt));
278 evt.events = EPOLLIN | EPOLLOUT;
280 en = epoll_pwait (epfd, &evt, 1, timeout, NULL);
283 printf (
"epoll_pwait: %s\n", strerror (errno));
290 if (evt.data.fd == pcfd)
293 if (evt.events & EPOLLIN)
295 if (evt.events & EPOLLOUT)
297 if (evt.events & EPOLLERR)
316 ptd->
epfd = epoll_create (1);
319 if (ptd->
conns == NULL)
321 printf (
"%s\n", strerror (errno));
340 socket_filename[strlen (socket_filename)] =
'0' + ptd->
index;
341 strncpy (socket_filename + strlen (socket_filename),
".sock", 5);
342 DBG (
"socket_filename: %s", socket_filename);
345 socket_filename, ptd);
348 printf (
"memif_per_thread_create_socket: %s\n",
memif_strerror (rv));
353 for (i = 0; i < ptd->
if_num; i++)
360 memset (&args, 0,
sizeof (args));
378 ptd->
pcfd = eventfd (0, EFD_NONBLOCK);
381 printf (
"eventfd: %s\n", strerror (errno));
386 printf (
"Failed to add poll cancel fd to epfd.");
400 for (i = 0; i < ptd->
if_num; i++)
417 (
"exit - Exits the application.\nhelp - Print this help.\nshow - Show memif interfaces\n");
427 printf (
"%u Threads %u Memifs (per thread)\n", im->
threads,
429 printf (
"=================================\n");
431 for (i = 0; i < im->
threads; i++)
437 printf (
"\tMemif id %u\n\t%s\n", im->
ptd[i].
conns[j].
id,
448 int option_index = 0;
461 getopt_long (argc, argv,
"t:i:",
options, &option_index)) != (-1))
466 im->
threads = strtoul (optarg, NULL, 10);
479 printf (
"threads < 1\n");
485 printf (
"if_num < 1\n");
493 printf (
"%s\n", strerror (errno));
499 printf (
"%s\n", strerror (errno));
504 for (i = 0; i < im->
threads; i++)
521 printf (
"%s\n", strerror (errno));
525 if (strncmp (buffer,
"exit", 4) == 0)
527 else if (strncmp (buffer,
"help", 4) == 0)
529 else if (strncmp (buffer,
"show", 4) == 0)
533 for (i = 0; i < im->
threads; i++)
536 rv = write (im->
ptd[i].
pcfd, &b, sizeof (b));
539 printf (
"Failed to cancel polling. %s\n", strerror (errno));
542 pthread_join (im->
pthread[i], NULL);
struct memif_connection * conns
#define ICMPR_SOCKET_FILENAME_LEN
int add_epoll_fd(int fd, uint32_t events)
struct per_thread_data * ptd
memif_socket_handle_t socket_handle
#define MEMIF_FD_EVENT_READ
user needs to set events that occured on fd and pass them to memif_control_fd_handler ...
Optimized string handling code, including c11-compliant "safe C library" variants.
struct per_thread_data * ptd
int resolve_packet2(void *pck, uint32_t *size, uint8_t ip_addr[4])
memif_socket_handle_t socket
memif_per_thread_main_handle_t pt_main
uint16_t per_thread_if_num
int poll_event(int timeout)
int memif_refill_queue(memif_conn_handle_t conn, uint16_t qid, uint16_t count, uint16_t headroom)
Memif refill ring.
memif_connection_update_t * on_disconnect
const char * memif_get_socket_filename(memif_socket_handle_t sock)
Get socket filename.
int control_fd_update(int fd, uint8_t events, void *ctx)
int memif_per_thread_init(memif_per_thread_main_handle_t *pt_main, void *private_ctx, memif_control_fd_update_t *on_control_fd_update, char *app_name, memif_alloc_t *memif_alloc, memif_realloc_t *memif_realloc, memif_free_t *memif_free)
Memif per thread initialization.
char * memif_strerror(int err_code)
Memif strerror.
#define ICMPR_BUFFER_LENGTH
memif_conn_handle_t handle
#define MEMIF_FD_EVENT_DEL
if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) ...
static void icmpr_print_help()
void * memif_socket_handle_t
Memif socket handle pointer of type void, pointing to internal structure.
int memif_buffer_enq_tx(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *count_out)
Memif buffer enq tx.
#define ICMPR_MEMIF_BUFFER_NUM
struct icmpr_main icmpr_main
int memif_per_thread_create_socket(memif_per_thread_main_handle_t pt_main, memif_socket_handle_t *sock, const char *filename, void *private_ctx)
Create memif socket.
int memif_tx_burst(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *tx)
Memif transmit buffer burst.
int memif_per_thread_control_fd_handler(memif_per_thread_main_handle_t pt_main, int fd, uint8_t events)
Memif per thread control file descriptor handler.
memif_connection_update_t * on_connect
int memif_create(memif_conn_handle_t *c, memif_conn_args_t *args, memif_connection_update_t *on_connect, memif_connection_update_t *on_disconnect, memif_interrupt_t *on_interrupt, void *private_ctx)
Memory interface create function.
void * memif_per_thread_main_handle_t
Memif per thread main handle Pointer of type void, pointing to internal structure.
void * memif_conn_handle_t
Memif connection handle pointer of type void, pointing to internal structure.
memif_interrupt_t * on_interrupt
int mod_epoll_fd(int fd, uint32_t events)
int memif_rx_burst(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *rx)
Memif receive buffer burst.
int memif_delete(memif_conn_handle_t *conn)
Memif delete.
int memif_per_thread_cleanup(memif_per_thread_main_handle_t *pt_main)
Memif per thread cleanup.
static struct option options[]
static void icmpr_show_memifs()
#define MEMIF_FD_EVENT_ERROR
inform libmemif that error occured on fd
#define MEMIF_FD_EVENT_MOD
update events
int main(int argc, char *argv[])
int memif_delete_socket(memif_socket_handle_t *sock)
Delete memif socket.
#define MEMIF_FD_EVENT_WRITE
Memif connection arguments.
static void * icmpr_thread_fn(void *data)