From 7c5c40db2a8d71a857ae63b6238cfac6e257da6d Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 30 Aug 2017 10:13:25 +0200 Subject: Shared memory packet interface (memif) library Change-Id: I5097462ae85acd705f19e92517c01094dba7565f Signed-off-by: Jakub Grajciar --- extras/libmemif/examples/icmp_responder-mt/main.c | 921 ++++++++++++++++++++++ 1 file changed, 921 insertions(+) create mode 100644 extras/libmemif/examples/icmp_responder-mt/main.c (limited to 'extras/libmemif/examples/icmp_responder-mt/main.c') diff --git a/extras/libmemif/examples/icmp_responder-mt/main.c b/extras/libmemif/examples/icmp_responder-mt/main.c new file mode 100644 index 00000000..860569bc --- /dev/null +++ b/extras/libmemif/examples/icmp_responder-mt/main.c @@ -0,0 +1,921 @@ +/* + *------------------------------------------------------------------ + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define APP_NAME "ICMP_Responder" +#define IF_NAME "memif_connection" + + +#ifdef ICMP_DBG +#define DBG(...) do { \ + printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ + printf (__VA_ARGS__); \ + printf ("\n"); \ + } while (0) +#else +#define DBG(...) +#endif + +#define INFO(...) do { \ + printf ("INFO: "__VA_ARGS__); \ + printf ("\n"); \ + } while (0) + +/* maximum tx/rx memif buffers */ +#define MAX_MEMIF_BUFS 256 +#define MAX_CONNS 50 +#define MAX_QUEUES 2 +#define MAX_THREADS ((MAX_CONNS) * (MAX_QUEUES)) + +int main_epfd; + +typedef struct +{ + /* thread id */ + uint8_t id; + /* memif connection index */ + uint16_t index; + /* id of queue to be handled by thread */ + uint8_t qid; + uint8_t isRunning; + + uint16_t rx_buf_num; + uint16_t tx_buf_num; + memif_buffer_t *rx_bufs; + memif_buffer_t *tx_bufs; +} memif_thread_data_t; + +typedef struct +{ + uint16_t index; + /* memif conenction handle */ + memif_conn_handle_t conn; + /* interface ip address */ + uint8_t ip_addr[4]; + /* inform pthread about connection termination */ + uint8_t pending_del; +} memif_connection_t; + +memif_connection_t memif_connection[MAX_CONNS]; +long ctx[MAX_CONNS]; + +/* thread data specific for each thread */ +memif_thread_data_t thread_data[MAX_THREADS]; +pthread_t thread[MAX_THREADS]; + +void +user_signal_handler (int sig) +{ + sig = sig; +} + +static void +print_memif_details () +{ + memif_details_t md; + ssize_t buflen; + char *buf; + int err, i, e, ti; + buflen = 2048; + buf = malloc (buflen); + printf ("MEMIF DETAILS\n"); + printf ("==============================\n"); + for (i = 0; i < MAX_CONNS; i++) + { + memif_connection_t *c = &memif_connection[i]; + + memset (&md, 0, sizeof (md)); + memset (buf, 0, buflen); + + err = memif_get_details (c->conn, &md, buf, buflen); + if (err != MEMIF_ERR_SUCCESS) + { + if (err != MEMIF_ERR_NOCONN) + INFO ("%s", memif_strerror (err)); + continue; + } + + printf ("interface index: %d\n", i); + + printf ("\tinterface ip: %u.%u.%u.%u\n", + c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]); + printf ("\tinterface name: %s\n", (char *) md.if_name); + printf ("\tapp name: %s\n", (char *) md.inst_name); + printf ("\tremote interface name: %s\n", (char *) md.remote_if_name); + printf ("\tremote app name: %s\n", (char *) md.remote_inst_name); + printf ("\tid: %u\n", md.id); + printf ("\tsecret: %s\n", (char *) md.secret); + printf ("\trole: "); + if (md.role) + printf ("slave\n"); + else + printf ("master\n"); + printf ("\tmode: "); + switch (md.mode) + { + case 0: + printf ("ethernet\n"); + break; + case 1: + printf ("ip\n"); + break; + case 2: + printf ("punt/inject\n"); + break; + default: + printf ("unknown\n"); + break; + } + printf ("\tsocket filename: %s\n", (char *) md.socket_filename); + printf ("\trx queues:\n"); + for (e = 0; e < md.rx_queues_num; e++) + { + ti = (i * MAX_QUEUES) + e; + printf ("\tqueue id: %u\n", md.rx_queues[e].qid); + printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size); + printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size); + printf ("\t\tthread id: %u\n", thread_data[ti].id); + printf ("\t\tthread connection index: %u\n", thread_data[ti].index); + printf ("\t\tthread running: "); + if (thread_data[ti].isRunning) + printf ("yes\n"); + else + printf ("no"); + } + printf ("\ttx queues:\n"); + for (e = 0; e < md.tx_queues_num; e++) + { + printf ("\tqueue id: %u\n", md.tx_queues[e].qid); + printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size); + printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size); + } + printf ("\tlink: "); + if (md.link_up_down) + printf ("up\n"); + else + printf ("down\n"); + } + free (buf); +} + +int +add_epoll_fd (int epfd, int fd, uint32_t events) +{ + if (fd < 0) + { + DBG ("invalid fd %d", fd); + return -1; + } + struct epoll_event evt; + memset (&evt, 0, sizeof (evt)); + evt.events = events; + evt.data.fd = fd; + if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0) + { + DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); + return -1; + } + DBG ("fd %d added to epoll", fd); + return 0; +} + +int +mod_epoll_fd (int epfd, int fd, uint32_t events) +{ + if (fd < 0) + { + DBG ("invalid fd %d", fd); + return -1; + } + struct epoll_event evt; + memset (&evt, 0, sizeof (evt)); + evt.events = events; + evt.data.fd = fd; + if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0) + { + DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); + return -1; + } + DBG ("fd %d moddified on epoll", fd); + return 0; +} + +int +del_epoll_fd (int epfd, int fd) +{ + if (fd < 0) + { + DBG ("invalid fd %d", fd); + return -1; + } + struct epoll_event evt; + memset (&evt, 0, sizeof (evt)); + if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0) + { + DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); + return -1; + } + DBG ("fd %d removed from epoll", fd); + return 0; +} + +void * +memif_rx_poll (void *ptr) +{ + memif_thread_data_t *data = (memif_thread_data_t *) ptr; + memif_connection_t *c = &memif_connection[data->index]; + int err; + uint16_t rx = 0, tx = 0, fb = 0; + + data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + data->rx_buf_num = 0; + data->tx_buf_num = 0; + + data->isRunning = 1; + INFO ("pthread id %u starts in polling mode", data->id); + + while (1) + { + if (c->pending_del) + goto close; + + /* receive data from shared memory buffers */ + err = + memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS, + &rx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_rx_burst: %s", memif_strerror (err)); + data->rx_buf_num += rx; + goto error; + } + data->rx_buf_num += rx; + if (rx == 0) + { + continue; + } + + DBG ("thread id: %u", data->id); + + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); + + err = + memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, + data->rx_buf_num, &tx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + data->tx_buf_num += tx; + goto error; + } + data->tx_buf_num += tx; + DBG ("allocated %d/%d buffers, %u free buffers", + tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num); + + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (data->rx_bufs + i)->data, + (data->rx_bufs + i)->data_len, + (void *) (data->tx_bufs + i)->data, + &(data->tx_bufs + i)->data_len, c->ip_addr); + } + + /* mark memif buffers and shared memory buffers as free */ + err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + data->rx_buf_num -= fb; + + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); + + err = + memif_tx_burst (c->conn, data->qid, data->tx_bufs, data->tx_buf_num, + &tx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_tx_burst: %s", memif_strerror (err)); + goto error; + } + DBG ("tx: %d/%u", tx, data->tx_buf_num); + data->tx_buf_num -= tx; + } + +error: + INFO ("thread %u error!", data->id); + goto close; + +close: + err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + data->rx_buf_num -= fb; + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); + free (data->rx_bufs); + free (data->tx_bufs); + data->isRunning = 0; + INFO ("pthread id %u exit", data->id); + pthread_exit (NULL); +} + +void * +memif_rx_interrupt (void *ptr) +{ + memif_thread_data_t *data = (memif_thread_data_t *) ptr; + memif_connection_t *c = &memif_connection[data->index]; + int err; + uint16_t rx = 0, tx = 0, fb = 0; + struct epoll_event evt, *e; + int en = 0; + uint32_t events = 0; + sigset_t sigset; + + signal (SIGUSR1, user_signal_handler); + + data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + data->rx_buf_num = 0; + data->tx_buf_num = 0; + + data->isRunning = 1; + INFO ("pthread id %u starts in interrupt mode", data->id); + int thread_epfd = epoll_create (1); + + /* get interrupt queue id */ + int fd = -1; + err = memif_get_queue_efd (c->conn, data->qid, &fd); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_get_queue_efd: %s", memif_strerror (err)); + goto error; + } + add_epoll_fd (thread_epfd, fd, EPOLLIN); + + while (1) + { + memset (&evt, 0, sizeof (evt)); + evt.events = EPOLLIN | EPOLLOUT; + sigemptyset (&sigset); + en = epoll_pwait (thread_epfd, &evt, 1, -1, &sigset); + if (en < 0) + { + if (errno == EINTR) + goto close; + DBG ("epoll_pwait: %s", strerror (errno)); + goto error; + } + else if (en > 0) + { + /* receive data from shared memory buffers */ + err = + memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS, + &rx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_rx_burst: %s", memif_strerror (err)); + data->rx_buf_num += rx; + goto error; + } + data->rx_buf_num += rx; + if (rx == 0) + { + continue; + } + + DBG ("thread id: %u", data->id); + + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); + + err = + memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, + data->rx_buf_num, &tx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + data->tx_buf_num += tx; + goto error; + } + data->tx_buf_num += tx; + DBG ("allocated %d/%d buffers, %u free buffers", + tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num); + + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (data->rx_bufs + i)->data, + (data->rx_bufs + i)->data_len, + (void *) (data->tx_bufs + i)->data, + &(data->tx_bufs + i)->data_len, c->ip_addr); + } + + /* mark memif buffers and shared memory buffers as free */ + err = + memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + data->rx_buf_num -= fb; + + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); + + err = + memif_tx_burst (c->conn, data->qid, data->tx_bufs, + data->tx_buf_num, &tx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_tx_burst: %s", memif_strerror (err)); + goto error; + } + DBG ("tx: %d/%u", tx, data->tx_buf_num); + data->tx_buf_num -= tx; + } + } + +error: + INFO ("thread %u error!", data->id); + goto close; + +close: + err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + data->rx_buf_num -= fb; + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); + free (data->rx_bufs); + free (data->tx_bufs); + data->isRunning = 0; + INFO ("pthread id %u exit", data->id); + pthread_exit (NULL); + +} + +/* informs user about connected status. private_ctx is used by user to identify connection + (multiple connections WIP) */ +int +on_connect (memif_conn_handle_t conn, void *private_ctx) +{ + long index = (*(long *) private_ctx); + int err, i, ti; + INFO ("memif connected! index %ld", index); + memif_connection_t *c = &memif_connection[index]; + c->pending_del = 0; + + for (i = 0; i < MAX_QUEUES; i++) + { + err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, i); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), i); + else + { + ti = (index * MAX_QUEUES) + i; + if (thread_data[ti].isRunning) + { + INFO ("thread id: %d already running!", ti); + continue; + } + thread_data[ti].index = index; + thread_data[ti].qid = i; + thread_data[ti].id = ti; + if ((i % 2) == 0) + pthread_create (&thread[ti], + NULL, memif_rx_poll, (void *) &thread_data[ti]); + else + pthread_create (&thread[ti], + NULL, memif_rx_interrupt, + (void *) &thread_data[ti]); + } + + } + return 0; +} + +/* informs user about disconnected status. private_ctx is used by user to identify connection + (multiple connections WIP) */ +int +on_disconnect (memif_conn_handle_t conn, void *private_ctx) +{ + void *ptr; + long index = (*(long *) private_ctx); + memif_connection_t *c = &memif_connection[index]; + int i, ti; + INFO ("memif disconnected!"); + /* inform thread in polling mode about memif disconenction */ + c->pending_del = 1; + for (i = 0; i < MAX_QUEUES; i++) + { + ti = (index * MAX_QUEUES) + i; + if (!thread_data[ti].isRunning) + continue; + if ((i % 2) != 0) + pthread_kill (thread[ti], SIGUSR1); /* interrupt thread in interrupt mode */ + pthread_join (thread[ti], &ptr); + } + return 0; +} + +/* user needs to watch new fd or stop watching fd that is about to be closed. + control fd will be modified during connection establishment to minimize CPU usage */ +int +control_fd_update (int fd, uint8_t events) +{ + /* convert memif event definitions to epoll events */ + if (events & MEMIF_FD_EVENT_DEL) + return del_epoll_fd (main_epfd, fd); + + uint32_t evt = 0; + if (events & MEMIF_FD_EVENT_READ) + evt |= EPOLLIN; + if (events & MEMIF_FD_EVENT_WRITE) + evt |= EPOLLOUT; + + if (events & MEMIF_FD_EVENT_MOD) + return mod_epoll_fd (main_epfd, fd, evt); + + return add_epoll_fd (main_epfd, fd, evt); +} + +int +icmpr_memif_create (long index) +{ + if (index >= MAX_CONNS) + { + INFO ("connection array overflow"); + return 0; + } + if (index < 0) + { + INFO ("don't even try..."); + return 0; + } + memif_connection_t *c = &memif_connection[index]; + + /* setting memif connection arguments */ + memif_conn_args_t args; + int fd = -1; + memset (&args, 0, sizeof (args)); + args.is_master = 0; + args.log2_ring_size = 10; + args.buffer_size = 2048; + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME)); + strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME)); + args.mode = 0; + /* socket filename is not specified, because this app is supposed to + connect to VPP over memif. so default socket filename will be used */ + /* default socketfile = /run/vpp/memif.sock */ + + args.interface_id = index; + /* last argument for memif_create (void * private_ctx) is used by user + to identify connection. this context is returned with callbacks */ + int err = memif_create (&c->conn, + &args, on_connect, on_disconnect, NULL, + &ctx[index]); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_create: %s", memif_strerror (err)); + return 0; + } + + c->index = index; + + c->ip_addr[0] = 192; + c->ip_addr[1] = 168; + c->ip_addr[2] = c->index + 1; + c->ip_addr[3] = 2; + return 0; +} + +int +icmpr_memif_delete (long index) +{ + if (index >= MAX_CONNS) + { + INFO ("connection array overflow"); + return 0; + } + if (index < 0) + { + INFO ("don't even try..."); + return 0; + } + memif_connection_t *c = &memif_connection[index]; + + int err; + /* disconenct then delete memif connection */ + err = memif_delete (&c->conn); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); + return 0; +} + +void +print_help () +{ + printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME); +#ifdef ICMP_DBG + printf (" (debug)"); +#endif + printf ("\n"); + printf ("==============================\n"); + printf ("libmemif version: %s", LIBMEMIF_VERSION); +#ifdef MEMIF_DBG + printf (" (debug)"); +#endif + printf ("\n"); + printf ("memif version: %d\n", MEMIF_VERSION); + printf ("commands:\n"); + printf ("\thelp - prints this help\n"); + printf ("\texit - exit app\n"); + printf ("\tconn - create memif (slave-mode)\n"); + printf ("\tdel - delete memif\n"); + printf ("\tshow - show connection details\n"); + printf ("\tip-set - set interface ip address\n"); +} + +int +icmpr_free () +{ + /* application cleanup */ + int err; + long i; + for (i = 0; i < MAX_CONNS; i++) + { + memif_connection_t *c = &memif_connection[i]; + if (c->conn) + icmpr_memif_delete (i); + } + + err = memif_cleanup (); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); + + return 0; +} + +int +icmpr_set_ip (long index, char *ip) +{ + if (index >= MAX_CONNS) + { + INFO ("connection array overflow"); + return 0; + } + if (index < 0) + { + INFO ("don't even try..."); + return 0; + } + memif_connection_t *c = &memif_connection[index]; + if (c->conn == NULL) + { + INFO ("no connection at index %ld", index); + return 0; + } + + char *end; + char *ui; + uint8_t tmp[4]; + ui = strtok (ip, "."); + if (ui == NULL) + goto error; + tmp[0] = strtol (ui, &end, 10); + + ui = strtok (NULL, "."); + if (ui == NULL) + goto error; + tmp[1] = strtol (ui, &end, 10); + + ui = strtok (NULL, "."); + if (ui == NULL) + goto error; + tmp[2] = strtol (ui, &end, 10); + + ui = strtok (NULL, "."); + if (ui == NULL) + goto error; + tmp[3] = strtol (ui, &end, 10); + + c->ip_addr[0] = tmp[0]; + c->ip_addr[1] = tmp[1]; + c->ip_addr[2] = tmp[2]; + c->ip_addr[3] = tmp[3]; + + INFO ("memif %ld ip address set to %u.%u.%u.%u", + index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]); + + return 0; + +error: + INFO ("invalid ip address"); + return 0; +} + + +int +user_input_handler () +{ + int i; + char *in = (char *) malloc (256); + char *ui = fgets (in, 256, stdin); + char *end; + if (in[0] == '\n') + goto done; + ui = strtok (in, " "); + if (strncmp (ui, "exit", 4) == 0) + { + free (in); + icmpr_free (); + exit (EXIT_SUCCESS); + } + else if (strncmp (ui, "help", 4) == 0) + { + print_help (); + goto done; + } + else if (strncmp (ui, "conn", 4) == 0) + { + ui = strtok (NULL, " "); + if (ui != NULL) + icmpr_memif_create (strtol (ui, &end, 10)); + else + INFO ("expected id"); + goto done; + } + else if (strncmp (ui, "del", 3) == 0) + { + ui = strtok (NULL, " "); + if (ui != NULL) + icmpr_memif_delete (strtol (ui, &end, 10)); + else + INFO ("expected id"); + goto done; + } + else if (strncmp (ui, "show", 4) == 0) + { + print_memif_details (); + goto done; + } + else if (strncmp (ui, "ip-set", 6) == 0) + { + ui = strtok (NULL, " "); + if (ui != NULL) + icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " ")); + else + INFO ("expected id"); + goto done; + } + else + { + DBG ("unknown command: %s", ui); + goto done; + } + + return 0; +done: + free (in); + return 0; +} + +int +poll_event (int timeout) +{ + struct epoll_event evt, *e; + int app_err = 0, memif_err = 0, en = 0; + int tmp, nfd; + uint32_t events = 0; + memset (&evt, 0, sizeof (evt)); + evt.events = EPOLLIN | EPOLLOUT; + sigset_t sigset; + sigemptyset (&sigset); + en = epoll_pwait (main_epfd, &evt, 1, timeout, &sigset); + if (en < 0) + { + DBG ("epoll_pwait: %s", strerror (errno)); + return -1; + } + if (en > 0) + { + /* this app does not use any other file descriptors than stds and memif control fds */ + if (evt.data.fd > 2) + { + /* event of memif control fd */ + /* convert epolle events to memif events */ + if (evt.events & EPOLLIN) + events |= MEMIF_FD_EVENT_READ; + if (evt.events & EPOLLOUT) + events |= MEMIF_FD_EVENT_WRITE; + if (evt.events & EPOLLERR) + events |= MEMIF_FD_EVENT_ERROR; + memif_err = memif_control_fd_handler (evt.data.fd, events); + if (memif_err != MEMIF_ERR_SUCCESS) + INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); + } + else if (evt.data.fd == 0) + { + app_err = user_input_handler (); + } + else + { + DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd); + } + } + + if ((app_err < 0) || (memif_err < 0)) + { + if (app_err < 0) + DBG ("user input handler error"); + if (memif_err < 0) + DBG ("memif control fd handler error"); + return -1; + } + + return 0; +} + +int +main () +{ + main_epfd = epoll_create (1); + add_epoll_fd (main_epfd, 0, EPOLLIN); + + /* initialize memory interface */ + int err, i; + /* if valid callback is passed as argument, fd event polling will be done by user + all file descriptors and events will be passed to user in this callback */ + /* if callback is set to NULL libmemif will handle fd event polling */ + err = memif_init (control_fd_update, APP_NAME); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_init: %s", memif_strerror (err)); + + for (i = 0; i < MAX_CONNS; i++) + { + memif_connection[i].conn = NULL; + ctx[i] = i; + } + + memset (&thread_data, 0, sizeof (memif_thread_data_t) * MAX_THREADS); + + print_help (); + + /* main loop */ + while (1) + { + if (poll_event (-1) < 0) + { + DBG ("poll_event error!"); + } + } +} -- cgit 1.2.3-korg From b467b2a02be6ea7bab1a4773523afe8a8e3cfd83 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 14 Sep 2017 14:12:10 +0200 Subject: libmemif: Jumbo frames support Change-Id: I2b316358dcd2de7168a860541bcca35c3dd44649 Signed-off-by: Jakub Grajciar --- extras/libmemif/Makefile.am | 9 + extras/libmemif/examples/README.md | 6 +- .../libmemif/examples/icmp_responder-epoll/main.c | 2 +- extras/libmemif/examples/icmp_responder-mt/main.c | 4 +- extras/libmemif/examples/icmp_responder/main.c | 2 +- extras/libmemif/src/libmemif.h | 3 +- extras/libmemif/src/main.c | 359 +++++++++++++++------ extras/libmemif/src/memif_private.h | 1 + extras/libmemif/src/socket.c | 2 - 9 files changed, 285 insertions(+), 103 deletions(-) (limited to 'extras/libmemif/examples/icmp_responder-mt/main.c') diff --git a/extras/libmemif/Makefile.am b/extras/libmemif/Makefile.am index 48e4bb89..1ff7e7e5 100644 --- a/extras/libmemif/Makefile.am +++ b/extras/libmemif/Makefile.am @@ -17,6 +17,9 @@ ACLOCAL_AMFLAGS = -I m4 AM_CPPFLAGS = -g -DMEMIF_DBG -DICMP_DBG +SRCS_C := $(shell find . -name "*.c" ) +SRCS_H := $(shell find . -name "*.h" ) + .PHONY: release release: $(MAKE) AM_CPPFLAGS="-O3" @@ -27,6 +30,12 @@ doc: doxygen doxygen.conf @echo Doxygen documentation built in docs directory. +.PHONY: fixstyle +fixstyle: + @echo Fixing code style... + indent $(SRCS_C) $(SRCS_H) + @echo Code style fixed! + # # unit_test # diff --git a/extras/libmemif/examples/README.md b/extras/libmemif/examples/README.md index bbd663b9..1375d27a 100644 --- a/extras/libmemif/examples/README.md +++ b/extras/libmemif/examples/README.md @@ -11,6 +11,6 @@ Current WORKDIR is set to root repository directory. Example apps can be run fro Example app | Description ------------|------------ -[icmpr](../examples/icmp_responder/main.c) | Simplest implementaion. Event polling is handled by libmemif. Single memif conenction in slave mode is created (id 0). Use Ctrl + C to exit app. -[icmpr-epoll](../examples/icmp_responder-epoll/main.c) (run in container by default) | Supports multiple connections and master mode. User can create/delete connections, set ip addresses, print connection information. [Example setup](ExampleSetup.md) contains instructions on basic connection use cases setups. -[icmpr-mt](../examples/icmp_responder-mt/main.c) | Multi-thread example, very similar to icmpr-epoll. Packets are handled in threads assigned to specific queues. Slave mode only. +[icmpr](../examples/icmp_responder/main.c) | Simplest implementaion. Event polling is handled by libmemif. Single memif conenction in slave mode is created (id 0). Use Ctrl + C to exit app. Memif receive mode: interrupt. +[icmpr-epoll](../examples/icmp_responder-epoll/main.c) (run in container by default) | Supports multiple connections and master mode. User can create/delete connections, set ip addresses, print connection information. [Example setup](ExampleSetup.md) contains instructions on basic connection use cases setups. Memif receive mode: interrupt. App provides functionality to disable interrupts for specified queue/s for testing purposes. Polling mode is not implemented in this example. +[icmpr-mt](../examples/icmp_responder-mt/main.c) | Multi-thread example, very similar to icmpr-epoll. Packets are handled in threads assigned to specific queues. Slave mode only. Memif receive mode: polling (memif_rx_poll function), interrupt (memif_rx_interrupt function). Receive modes differ per queue. diff --git a/extras/libmemif/examples/icmp_responder-epoll/main.c b/extras/libmemif/examples/icmp_responder-epoll/main.c index 4172785f..cff944f9 100644 --- a/extras/libmemif/examples/icmp_responder-epoll/main.c +++ b/extras/libmemif/examples/icmp_responder-epoll/main.c @@ -283,7 +283,7 @@ icmpr_buffer_alloc (long index, long n, uint16_t qid) int err; uint16_t r; /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */ - err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); + err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); diff --git a/extras/libmemif/examples/icmp_responder-mt/main.c b/extras/libmemif/examples/icmp_responder-mt/main.c index 860569bc..c47fc53d 100644 --- a/extras/libmemif/examples/icmp_responder-mt/main.c +++ b/extras/libmemif/examples/icmp_responder-mt/main.c @@ -309,7 +309,7 @@ memif_rx_poll (void *ptr) err = memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, - data->rx_buf_num, &tx); + data->rx_buf_num, &tx, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); @@ -439,7 +439,7 @@ memif_rx_interrupt (void *ptr) err = memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, - data->rx_buf_num, &tx); + data->rx_buf_num, &tx, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); diff --git a/extras/libmemif/examples/icmp_responder/main.c b/extras/libmemif/examples/icmp_responder/main.c index 5351b6b8..9e49771e 100644 --- a/extras/libmemif/examples/icmp_responder/main.c +++ b/extras/libmemif/examples/icmp_responder/main.c @@ -225,7 +225,7 @@ icmpr_buffer_alloc (long n, uint16_t qid) int err; uint16_t r; /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */ - err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); + err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); diff --git a/extras/libmemif/src/libmemif.h b/extras/libmemif/src/libmemif.h index 3732be68..a2d1a5e2 100644 --- a/extras/libmemif/src/libmemif.h +++ b/extras/libmemif/src/libmemif.h @@ -383,12 +383,13 @@ int memif_delete (memif_conn_handle_t * conn); @param bufs - memif buffers @param count - number of memif buffers to allocate @param count_out - returns number of allocated buffers + @param size - minimal buffer size, 0 = standard buffer size \return memif_err_t */ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t * bufs, uint16_t count, - uint16_t * count_out); + uint16_t * count_out, uint16_t size); /** \brief Memif buffer free @param conn - memif conenction handle diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c index d1b59eea..49bf50cb 100644 --- a/extras/libmemif/src/main.c +++ b/extras/libmemif/src/main.c @@ -512,8 +512,8 @@ memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, if (conn == NULL) return MEMIF_ERR_NOCONN; uint8_t num = - (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args. - num_m2s_rings; + (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; @@ -656,9 +656,9 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args, } DBG ("creating socket file"); ms = malloc (sizeof (memif_socket_t)); - ms->filename = malloc (strlen ((char *) conn->args. - socket_filename) + - sizeof (char)); + ms->filename = + malloc (strlen ((char *) conn->args.socket_filename) + + sizeof (char)); memset (ms->filename, 0, strlen ((char *) conn->args.socket_filename) + sizeof (char)); @@ -842,17 +842,20 @@ memif_control_fd_handler (int fd, uint8_t events) if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL) { num = - (((memif_connection_t *) e->data_struct)->args. - is_master) ? ((memif_connection_t *) e->data_struct)-> - run_args.num_s2m_rings : ((memif_connection_t *) e-> - data_struct)->run_args. - num_m2s_rings; + (((memif_connection_t *) e->data_struct)-> + args.is_master) ? ((memif_connection_t *) e-> + data_struct)->run_args. + num_s2m_rings : ((memif_connection_t *) e->data_struct)-> + run_args.num_m2s_rings; for (i = 0; i < num; i++) { - if (((memif_connection_t *) e->data_struct)->rx_queues[i]. - int_fd == fd) + if (((memif_connection_t *) e->data_struct)-> + rx_queues[i].int_fd == fd) { - ((memif_connection_t *) e->data_struct)->on_interrupt ((void *) e->data_struct, ((memif_connection_t *) e->data_struct)->private_ctx, i); + ((memif_connection_t *) e->data_struct)-> + on_interrupt ((void *) e->data_struct, + ((memif_connection_t *) e-> + data_struct)->private_ctx, i); return MEMIF_ERR_SUCCESS; } } @@ -879,24 +882,24 @@ memif_control_fd_handler (int fd, uint8_t events) if (events & MEMIF_FD_EVENT_READ) { err = - ((memif_connection_t *) e->data_struct)->read_fn (e-> - data_struct); + ((memif_connection_t *) e->data_struct)-> + read_fn (e->data_struct); if (err != MEMIF_ERR_SUCCESS) return err; } if (events & MEMIF_FD_EVENT_WRITE) { err = - ((memif_connection_t *) e->data_struct)->write_fn (e-> - data_struct); + ((memif_connection_t *) e->data_struct)-> + write_fn (e->data_struct); if (err != MEMIF_ERR_SUCCESS) return err; } if (events & MEMIF_FD_EVENT_ERROR) { err = - ((memif_connection_t *) e->data_struct)->error_fn (e-> - data_struct); + ((memif_connection_t *) e->data_struct)-> + error_fn (e->data_struct); if (err != MEMIF_ERR_SUCCESS) return err; } @@ -990,8 +993,8 @@ memif_disconnect_internal (memif_connection_t * c) if (c->tx_queues != NULL) { num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; for (i = 0; i < num; i++) { mq = &c->tx_queues[i]; @@ -1011,8 +1014,8 @@ memif_disconnect_internal (memif_connection_t * c) if (c->rx_queues != NULL) { num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; for (i = 0; i < num; i++) { mq = &c->rx_queues[i]; @@ -1164,8 +1167,8 @@ memif_connect1 (memif_connection_t * c) } num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; for (i = 0; i < num; i++) { mq = &c->tx_queues[i]; @@ -1182,8 +1185,8 @@ memif_connect1 (memif_connection_t * c) } } num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; for (i = 0; i < num; i++) { mq = &c->rx_queues[i]; @@ -1336,7 +1339,7 @@ memif_init_regions_and_queues (memif_connection_t * conn) int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t * bufs, uint16_t count, - uint16_t * count_out) + uint16_t * count_out, uint16_t size) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -1344,17 +1347,18 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; if (qid >= num) return MEMIF_ERR_QID; memif_queue_t *mq = &c->tx_queues[qid]; memif_ring_t *ring = mq->ring; memif_buffer_t *b0, *b1; + uint8_t chain_buf0, chain_buf1; uint16_t mask = (1 << mq->log2_ring_size) - 1; uint16_t s0, s1, ns; *count_out = 0; - int err = MEMIF_ERR_SUCCESS; /* 0 */ + int i, err = MEMIF_ERR_SUCCESS; /* 0 */ if (ring->tail != ring->head) { @@ -1374,39 +1378,86 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, while ((count > 2) && (ns > 2)) { s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; - s1 = (ring->head + mq->alloc_bufs + *count_out + 1) & mask; + chain_buf0 = size / ring->desc[s0].buffer_length; + if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0)) + chain_buf0++; + + if (chain_buf0 > ns) + break; + + s1 = (ring->head + mq->alloc_bufs + *count_out + chain_buf0) & mask; + chain_buf1 = size / ring->desc[s1].buffer_length; + if (((size % ring->desc[s1].buffer_length) != 0) || (size == 0)) + chain_buf1++; + + if ((chain_buf0 + chain_buf1) > ns) + break; b0 = (bufs + *count_out); b1 = (bufs + *count_out + 1); b0->desc_index = s0; b1->desc_index = s1; - b0->buffer_len = ring->desc[s0].buffer_length; - b1->buffer_len = ring->desc[s1].buffer_length; + b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0; + b1->buffer_len = ring->desc[s1].buffer_length * chain_buf1; /* TODO: support multiple regions -> ring descriptor contains region index */ b0->data = c->regions->shm + ring->desc[s0].offset; b1->data = c->regions->shm + ring->desc[s1].offset; + for (i = 0; i < (memif_min (chain_buf0, chain_buf1) - 1); i++) + { + ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + DBG ("allocating chained buffers"); + } + + if (chain_buf0 > chain_buf1) + { + for (; i < (chain_buf0 - 1); i++) + ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + } + else + { + for (; i < (chain_buf1 - 1); i++) + ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + } + + mq->alloc_bufs += chain_buf0 + chain_buf1; + DBG ("allocated ring slots %u, %u", s0, s1); count -= 2; - ns -= 2; + ns -= chain_buf0 + chain_buf1; *count_out += 2; } s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; b0 = (bufs + *count_out); + chain_buf0 = size / ring->desc[s0].buffer_length; + if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0)) + chain_buf0++; + + if (chain_buf0 > ns) + break; + b0->desc_index = s0; - b0->buffer_len = ring->desc[s0].buffer_length; + b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0; b0->data = c->regions->shm + ring->desc[s0].offset; + for (i = 0; i < (chain_buf0 - 1); i++) + { + ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + DBG ("allocating chained buffers"); + } + + mq->alloc_bufs += chain_buf0; + DBG ("allocated ring slot %u", s0); count--; - ns--; + ns -= chain_buf0; *count_out += 1; } - mq->alloc_bufs += *count_out; DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, mq->alloc_bufs); @@ -1430,8 +1481,8 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; libmemif_main_t *lm = &libmemif_main; @@ -1439,6 +1490,7 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, memif_ring_t *ring = mq->ring; uint16_t tail = ring->tail; uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint8_t chain_buf0, chain_buf1; memif_buffer_t *b0, *b1; *count_out = 0; @@ -1451,22 +1503,35 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, { b0 = (bufs + *count_out); b1 = (bufs + *count_out + 1); - tail = (b0->desc_index + 1) & mask; - tail = (b1->desc_index + 1) & mask; + chain_buf0 = + b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != + 0) + chain_buf0++; + chain_buf1 = + b1->buffer_len / ring->desc[b1->desc_index].buffer_length; + if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) != + 0) + chain_buf1++; + tail = (b0->desc_index + chain_buf0) & mask; + tail = (b1->desc_index + chain_buf1) & mask; b0->data = NULL; b1->data = NULL; count -= 2; *count_out += 2; - mq->alloc_bufs -= 2; + mq->alloc_bufs -= chain_buf0 + chain_buf1; } b0 = (bufs + *count_out); - tail = (b0->desc_index + 1) & mask; + chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0) + chain_buf0++; + tail = (b0->desc_index + chain_buf0) & mask; b0->data = NULL; count--; *count_out += 1; - mq->alloc_bufs--; + mq->alloc_bufs -= chain_buf0; } MEMIF_MEORY_BARRIER (); ring->tail = tail; @@ -1484,33 +1549,94 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; if (qid >= num) return MEMIF_ERR_QID; memif_queue_t *mq = &c->tx_queues[qid]; memif_ring_t *ring = mq->ring; uint16_t head = ring->head; uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint8_t chain_buf0, chain_buf1; *tx = 0; + uint16_t curr_buf = 0; memif_buffer_t *b0, *b1; + int i; while (count) { while (count > 2) { - b0 = (bufs + *tx); - b1 = (bufs + *tx + 1); - ring->desc[b0->desc_index].length = b0->data_len; - ring->desc[b1->desc_index].length = b1->data_len; + b0 = (bufs + curr_buf); + b1 = (bufs + curr_buf + 1); + chain_buf0 = + b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != + 0) + chain_buf0++; + + chain_buf1 = + b1->buffer_len / ring->desc[b1->desc_index].buffer_length; + if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) != + 0) + chain_buf1++; + + for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++) + { + ring->desc[(b0->desc_index + i) & mask].length = b0->data_len; + ring->desc[(b1->desc_index + i) & mask].length = b1->data_len; +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data + + ring->desc[(b0->desc_index + + i) & mask].buffer_length * + (chain_buf0 - 1), + ring->desc[(b0->desc_index + + i) & mask].buffer_length, DBG_TX_BUF); + print_bytes (b1->data + + ring->desc[(b1->desc_index + + i) & mask].buffer_length * + (chain_buf1 - 1), + ring->desc[(b1->desc_index + + i) & mask].buffer_length, DBG_TX_BUF); +#endif + } + if (chain_buf0 > chain_buf1) + { + for (; i < chain_buf0; i++) + { + ring->desc[(b0->desc_index + i) & mask].length = + b0->data_len; +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data + + ring->desc[(b0->desc_index + + i) & mask].buffer_length * + (chain_buf0 - 1), + ring->desc[(b0->desc_index + + i) & mask].buffer_length, + DBG_TX_BUF); +#endif + } + } + else + { + for (; i < chain_buf1; i++) + { + ring->desc[b1->desc_index + i].length = b1->data_len; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_TX_BUF); - print_bytes (b1->data, b1->data_len, DBG_TX_BUF); + print_bytes (b1->data + + ring->desc[(b1->desc_index + + i) & mask].buffer_length * + (chain_buf1 - 1), + ring->desc[(b1->desc_index + + i) & mask].buffer_length, + DBG_TX_BUF); #endif + } + } - head = (b0->desc_index + 1) & mask; - head = (b1->desc_index + 1) & mask; + head = (b0->desc_index + chain_buf0) & mask; + head = (b1->desc_index + chain_buf1) & mask; b0->data = NULL; b0->data_len = 0; @@ -1518,29 +1644,44 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b1->data_len = 0; count -= 2; - *tx += 2; + *tx += chain_buf0 + chain_buf1; + curr_buf += 2; } b0 = (bufs + *tx); - ring->desc[b0->desc_index].length = b0->data_len; + chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0) + chain_buf0++; + for (i = 0; i < chain_buf0; i++) + { + ring->desc[(b0->desc_index + i) & mask].length = b0->data_len; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_TX_BUF); + print_bytes (b0->data + + ring->desc[(b0->desc_index + i) & mask].buffer_length * + (chain_buf0 - 1), + ring->desc[(b0->desc_index + i) & mask].buffer_length, + DBG_TX_BUF); #endif + } - head = (b0->desc_index + 1) & mask; + head = (b0->desc_index + chain_buf0) & mask; b0->data = NULL; b0->data_len = 0; count--; - *tx += 1; + *tx += chain_buf0; + curr_buf++; } MEMIF_MEORY_BARRIER (); ring->head = head; mq->alloc_bufs -= *tx; + /* TODO: return num of buffers and packets */ + *tx = curr_buf; + if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) { uint64_t a = 1; @@ -1562,8 +1703,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; memif_queue_t *mq = &c->rx_queues[qid]; @@ -1572,7 +1713,9 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, uint16_t ns; uint16_t mask = (1 << mq->log2_ring_size) - 1; memif_buffer_t *b0, *b1; + uint16_t curr_buf = 0; *rx = 0; + int i; uint64_t b; ssize_t r = read (mq->int_fd, &b, sizeof (b)); @@ -1591,49 +1734,79 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, { while ((ns > 2) && (count > 2)) { - b0 = (bufs + *rx); - b1 = (bufs + *rx + 1); + b0 = (bufs + curr_buf); + b1 = (bufs + curr_buf + 1); b0->desc_index = mq->last_head; - b1->desc_index = mq->last_head + 1; - b0->data = memif_get_buffer (conn, ring, mq->last_head); - b1->data = memif_get_buffer (conn, ring, mq->last_head + 1); - b0->data_len = ring->desc[mq->last_head].length; - b1->data_len = ring->desc[mq->last_head + 1].length; - b0->buffer_len = ring->desc[mq->last_head].buffer_length; - b1->buffer_len = ring->desc[mq->last_head + 1].buffer_length; - + i = 0; + do + { + b0->data = memif_get_buffer (conn, ring, mq->last_head); + b0->data_len = ring->desc[mq->last_head].length; + b0->buffer_len = ring->desc[mq->last_head].buffer_length; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_RX_BUF); - print_bytes (b1->data, b1->data_len, DBG_RX_BUF); + print_bytes (b0->data + + ring->desc[b0->desc_index].buffer_length * i++, + ring->desc[b0->desc_index].buffer_length, + DBG_TX_BUF); #endif + mq->last_head = (mq->last_head + 1) & mask; + ns--; + *rx += 1; + } + while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT); - mq->last_head = (mq->last_head + 2) & mask; + b1->desc_index = mq->last_head; + i = 0; + do + { + b1->data = memif_get_buffer (conn, ring, mq->last_head); + b1->data_len = ring->desc[mq->last_head].length; + b1->buffer_len = ring->desc[mq->last_head].buffer_length; +#ifdef MEMIF_DBG_SHM + print_bytes (b1->data + + ring->desc[b1->desc_index].buffer_length * i++, + ring->desc[b1->desc_index].buffer_length, + DBG_TX_BUF); +#endif + mq->last_head = (mq->last_head + 1) & mask; + ns--; + *rx += 1; + } + while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT); - ns -= 2; count -= 2; - *rx += 2; + curr_buf += 2; } - b0 = (bufs + *rx); + b0 = (bufs + curr_buf); b0->desc_index = mq->last_head; - b0->data = memif_get_buffer (conn, ring, mq->last_head); - b0->data_len = ring->desc[mq->last_head].length; - b0->buffer_len = ring->desc[mq->last_head].buffer_length; - + i = 0; + do + { + b0->data = memif_get_buffer (conn, ring, mq->last_head); + b0->data_len = ring->desc[mq->last_head].length; + b0->buffer_len = ring->desc[mq->last_head].buffer_length; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_RX_BUF); + print_bytes (b0->data + + ring->desc[b0->desc_index].buffer_length * i++, + ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF); #endif + mq->last_head = (mq->last_head + 1) & mask; + ns--; + *rx += 1; + } + while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT); - mq->last_head = (mq->last_head + 1) & mask; - - ns--; count--; - *rx += 1; + curr_buf++; } mq->alloc_bufs += *rx; + /* TODO: return num of buffers and packets */ + *rx = curr_buf; + if (ns) { DBG ("not enough buffers!"); @@ -1722,8 +1895,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md, err = MEMIF_ERR_NOBUF_DET; md->rx_queues_num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; l1 = sizeof (memif_queue_details_t) * md->rx_queues_num; if (l0 + l1 <= buflen) @@ -1742,8 +1915,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md, } md->tx_queues_num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; l1 = sizeof (memif_queue_details_t) * md->tx_queues_num; if (l0 + l1 <= buflen) @@ -1776,8 +1949,8 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; diff --git a/extras/libmemif/src/memif_private.h b/extras/libmemif/src/memif_private.h index 51f3be66..83962bcf 100644 --- a/extras/libmemif/src/memif_private.h +++ b/extras/libmemif/src/memif_private.h @@ -44,6 +44,7 @@ #define MEMIF_MAX_FDS 512 +#define memif_min(a,b) (((a) < (b)) ? (a) : (b)) #ifdef MEMIF_DBG #define DBG(...) do { \ diff --git a/extras/libmemif/src/socket.c b/extras/libmemif/src/socket.c index 9c9b3a8d..ca24d929 100644 --- a/extras/libmemif/src/socket.c +++ b/extras/libmemif/src/socket.c @@ -33,8 +33,6 @@ #include #include -#define memif_min(a,b) ((a < b) ? (a) : (b)) - /* sends msg to socket */ static_fn int memif_msg_send (int fd, memif_msg_t * msg, int afd) -- cgit 1.2.3-korg