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/src/libmemif.h | 442 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 442 insertions(+) create mode 100644 extras/libmemif/src/libmemif.h (limited to 'extras/libmemif/src/libmemif.h') diff --git a/extras/libmemif/src/libmemif.h b/extras/libmemif/src/libmemif.h new file mode 100644 index 00000000..3732be68 --- /dev/null +++ b/extras/libmemif/src/libmemif.h @@ -0,0 +1,442 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + +/** @file */ + +#ifndef _LIBMEMIF_H_ +#define _LIBMEMIF_H_ + +/** Libmemif version. */ +#define LIBMEMIF_VERSION "1.0" +/** Default name of application using libmemif. */ +#define MEMIF_DEFAULT_APP_NAME "libmemif-app" + +#include + +#include + +/*! Error codes */ +typedef enum +{ + MEMIF_ERR_SUCCESS = 0, /*!< success */ +/* SYSCALL ERRORS */ + MEMIF_ERR_SYSCALL, /*!< other syscall error */ + MEMIF_ERR_ACCES, /*!< permission denied */ + MEMIF_ERR_NO_FILE, /*!< file does not exist */ + MEMIF_ERR_FILE_LIMIT, /*!< system open file limit */ + MEMIF_ERR_PROC_FILE_LIMIT, /*!< process open file limit */ + MEMIF_ERR_ALREADY, /*!< connection already requested */ + MEMIF_ERR_AGAIN, /*!< fd is not socket, or operation would block */ + MEMIF_ERR_BAD_FD, /*!< invalid fd */ + MEMIF_ERR_NOMEM, /*!< out of memory */ +/* LIBMEMIF ERRORS */ + MEMIF_ERR_INVAL_ARG, /*!< invalid argument */ + MEMIF_ERR_NOCONN, /*!< handle points to no connection */ + MEMIF_ERR_CONN, /*!< handle points to existing connection */ + MEMIF_ERR_CB_FDUPDATE, /*!< user defined callback memif_control_fd_update_t error */ + MEMIF_ERR_FILE_NOT_SOCK, /*!< file specified by socket filename + exists, but it's not socket */ + MEMIF_ERR_NO_SHMFD, /*!< missing shm fd */ + MEMIF_ERR_COOKIE, /*!< wrong cookie on ring */ + MEMIF_ERR_NOBUF_RING, /*!< ring buffer full */ + MEMIF_ERR_NOBUF, /*!< not enough memif buffers */ + MEMIF_ERR_NOBUF_DET, /*!< memif details needs larger buffer */ + MEMIF_ERR_INT_WRITE, /*!< send interrupt error */ + MEMIF_ERR_MFMSG, /*!< malformed msg received */ + MEMIF_ERR_QID, /*!< invalid queue id */ +/* MEMIF PROTO ERRORS */ + MEMIF_ERR_PROTO, /*!< incompatible protocol version */ + MEMIF_ERR_ID, /*!< unmatched interface id */ + MEMIF_ERR_ACCSLAVE, /*!< slave cannot accept connection requests */ + MEMIF_ERR_ALRCONN, /*!< memif is already connected */ + MEMIF_ERR_MODE, /*!< mode mismatch */ + MEMIF_ERR_SECRET, /*!< secret mismatch */ + MEMIF_ERR_NOSECRET, /*!< secret required */ + MEMIF_ERR_MAXREG, /*!< max region limit reached */ + MEMIF_ERR_MAXRING, /*!< max ring limit reached */ + MEMIF_ERR_NO_INTFD, /*!< missing interrupt fd */ + MEMIF_ERR_DISCONNECT, /*!< disconenct received */ + MEMIF_ERR_DISCONNECTED, /*!< peer interface disconnected */ + MEMIF_ERR_UNKNOWN_MSG, /*!< unknown message type */ +} memif_err_t; + +/** + * @defgroup MEMIF_FD_EVENT Types of events that need to be watched for specific fd. + * + * @{ + */ + +/** user needs to set events that occured on fd and pass them to memif_control_fd_handler */ +#define MEMIF_FD_EVENT_READ (1 << 0) +#define MEMIF_FD_EVENT_WRITE (1 << 1) +/** inform libmemif that error occured on fd */ +#define MEMIF_FD_EVENT_ERROR (1 << 2) +/** if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) */ +#define MEMIF_FD_EVENT_DEL (1 << 3) +/** update events */ +#define MEMIF_FD_EVENT_MOD (1 << 4) +/** @} */ + +/** *brief Memif connection handle + pointer of type void, pointing to internal structure +*/ +typedef void *memif_conn_handle_t; +/** + * @defgroup CALLBACKS Callback functions definitions + * + * @{ + */ + +/** \brief Memif control file descriptor update (callback function) + @param fd - new file descriptor to watch + @param events - event type(s) to watch for + + This callback is called when there is new fd to watch for events on + or if fd is about to be closed (user mey want to stop watching for events on this fd). +*/ +typedef int (memif_control_fd_update_t) (int fd, uint8_t events); + +/** \brief Memif connection status update (callback function) + @param conn - memif connection handle + @param private_ctx - private context + + Informs user about connection status connected/disconnected. + On connected -> start watching for events on interrupt fd (optional). +*/ +typedef int (memif_connection_update_t) (memif_conn_handle_t conn, + void *private_ctx); + +/** \brief Memif interrupt occured (callback function) + @param conn - memif connection handle + @param private_ctx - private context + @param qid - queue id on which interrupt occured + + Called when event is received on interrupt fd. +*/ +typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx, + uint16_t qid); +/** @} */ + +/** + * @defgroup ARGS_N_BUFS Connection arguments and buffers + * + * @{ + */ + +/** \brief Memif connection arguments + @param socket_filename - socket filename + @param secret - otional parameter used as interface autenthication + @param num_s2m_rings - number of slave to master rings + @param num_m2s_rings - number of master to slave rings + @param buffer_size - size of buffer in shared memory + @param log2_ring_size - logarithm base 2 of ring size + @param is_master - 0 == master, 1 == slave + @param interface_id - id used to identify peer connection + @param interface_name - interface name + @param instance_name - application name + @param mode - 0 == ethernet, 1 == ip , 2 == punt/inject +*/ +typedef struct +{ + uint8_t *socket_filename; /*!< default = /run/vpp/memif.sock */ + uint8_t secret[24]; /*!< optional (interface authentication) */ + + uint8_t num_s2m_rings; /*!< default = 1 */ + uint8_t num_m2s_rings; /*!< default = 1 */ + uint16_t buffer_size; /*!< default = 2048 */ + memif_log2_ring_size_t log2_ring_size; /*!< default = 10 (1024) */ + uint8_t is_master; + + memif_interface_id_t interface_id; + uint8_t interface_name[32]; + uint8_t instance_name[32]; + memif_interface_mode_t mode:8; +} memif_conn_args_t; + +/*! memif receive mode */ +typedef enum +{ + MEMIF_RX_MODE_INTERRUPT = 0, /*!< interrupt mode */ + MEMIF_RX_MODE_POLLING /*!< polling mode */ +} memif_rx_mode_t; + +/** \brief Memif buffer + @param desc_index - ring descriptor index + @param buffer_len - shared meory buffer length + @param data_len - data length + @param data - pointer to shared memory data +*/ +typedef struct +{ + uint16_t desc_index; + uint32_t buffer_len; + uint32_t data_len; + void *data; +} memif_buffer_t; +/** @} */ + +/** + * @defgroup MEMIF_DETAILS Memif details structs + * + * @{ + */ + +/** \brief Memif queue details + @param qid - queue id + @param ring_size - size of ring buffer in sharem memory + @param buffer_size - buffer size on sharem memory +*/ +typedef struct +{ + uint8_t qid; + uint32_t ring_size; + uint16_t buffer_size; + /* add ring information */ +} memif_queue_details_t; + +/** \brief Memif details + @param if_name - interface name + @param inst_name - application name + @param remote_if_name - peer interface name + @param remote_inst_name - peer application name + @param id - connection id + @param secret - secret + @param role - 0 = master, 1 = slave + @param mode - 0 = ethernet, 1 = ip , 2 = punt/inject + @param socket_filename = socket filename + @param rx_queues_num - number of receive queues + @param tx_queues_num - number of transmit queues + @param rx_queues - struct containing receive queue details + @param tx_queues - struct containing transmit queue details + @param link_up_down - 1 = up (connected), 2 = down (disconnected) +*/ +typedef struct +{ + uint8_t *if_name; + uint8_t *inst_name; + uint8_t *remote_if_name; + uint8_t *remote_inst_name; + + uint32_t id; + uint8_t *secret; /* optional */ + uint8_t role; /* 0 = master, 1 = slave */ + uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */ + uint8_t *socket_filename; + uint8_t rx_queues_num; + uint8_t tx_queues_num; + memif_queue_details_t *rx_queues; + memif_queue_details_t *tx_queues; + + uint8_t link_up_down; /* 1 = up, 0 = down */ +} memif_details_t; +/** @} */ + +/** + * @defgroup API_CALLS Api calls + * + * @{ + */ + +/** \biref Memif get queue event file descriptor + @param conn - memif connection handle + @param qid - queue id + @param[out] fd - returns event file descriptor + + \return memif_err_t +*/ + +int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *fd); + +/** \brief Memif set rx mode + @param conn - memif connection handle + @param rx_mode - receive mode + @param qid - queue id + + \return memif_err_t +*/ +int memif_set_rx_mode (memif_conn_handle_t conn, memif_rx_mode_t rx_mode, + uint16_t qid); + +/** \brief Memif strerror + @param err_code - error code + + Converts error code to error message. + + \return Error string +*/ +char *memif_strerror (int err_code); + +/** \brief Memif get details + @param conn - memif conenction handle + @param md - pointer to memif details struct + @param buf - buffer containing details strings + @param buflen - length of buffer + + \return memif_err_t +*/ +int memif_get_details (memif_conn_handle_t conn, memif_details_t * md, + char *buf, ssize_t buflen); + +/** \brief Memif initialization + @param on_control_fd_update - if control fd updates inform user to watch new fd + @param app_name - application name + + if param on_control_fd_update is set to NULL, + libmemif will handle file descriptor event polling + if a valid callback is set, file descriptor event polling needs to be done by + user application, all file descriptors and event types will be passed in + this callback to user application + + Initialize internal libmemif structures. Create timerfd (used to periodically request connection by + disconnected memifs in slave mode, with no additional API call). This fd is passed to user with memif_control_fd_update_t + timer is inactive at this state. It activates with if there is at least one memif in slave mode. + + \return memif_err_t +*/ +int memif_init (memif_control_fd_update_t * on_control_fd_update, + char *app_name); + +/** \brief Memif cleanup + + Free libmemif internal allocations. + + \return 0 +*/ +int memif_cleanup (); + +/** \brief Memory interface create function + @param conn - connection handle for user app + @param args - memory interface connection arguments + @param on_connect - inform user about connected status + @param on_disconnect - inform user about disconnected status + @param on_interrupt - informs user about interrupt, if set to null user will not be notified about interrupt, user can use memif_get_queue_efd call to get interrupt fd to poll for events + @param private_ctx - private contex passed back to user with callback + + Creates memory interface. + + SLAVE-MODE - + Start timer that will send events to timerfd. If this fd is passed to memif_control_fd_handler + every disconnected memif in slave mode will send connection request. + On success new fd is passed to user with memif_control_fd_update_t. + + MASTER-MODE - + Create listener socket and pass fd to user with memif_cntrol_fd_update_t. + If this fd is passed to memif_control_fd_handler accept will be called and + new fd will be passed to user with memif_control_fd_update_t. + + + \return memif_err_t +*/ +int memif_create (memif_conn_handle_t * conn, 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); + +/** \brief Memif control file descriptor handler + @param fd - file descriptor on which the event occured + @param events - event type(s) that occured + + If event occures on any control fd, call memif_control_fd_handler. + Internal - lib will "identify" fd (timerfd, lsitener, control) and handle event accordingly. + + FD-TYPE - + TIMERFD - + Every disconnected memif in slave mode will request connection. + LISTENER or CONTROL - + Handle socket messaging (internal connection establishment). + INTERRUPT - + Call on_interrupt callback (if set). + + \return memif_err_t + +*/ +int memif_control_fd_handler (int fd, uint8_t events); + +/** \brief Memif delete + @param conn - pointer to memif connection handle + + + disconnect session (free queues and regions, close file descriptors, unmap shared memory) + set connection handle to NULL, to avoid possible double free + + \return memif_err_t +*/ +int memif_delete (memif_conn_handle_t * conn); + +/** \brief Memif buffer alloc + @param conn - memif conenction handle + @param qid - number indentifying queue + @param bufs - memif buffers + @param count - number of memif buffers to allocate + @param count_out - returns number of allocated buffers + + \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); + +/** \brief Memif buffer free + @param conn - memif conenction handle + @param qid - number indentifying queue + @param bufs - memif buffers + @param count - number of memif buffers to free + @param count_out - returns number of freed buffers + + \return memif_err_t +*/ +int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t * bufs, uint16_t count, + uint16_t * count_out); + +/** \brief Memif transmit buffer burst + @param conn - memif conenction handle + @param qid - number indentifying queue + @param bufs - memif buffers + @param count - number of memif buffers to transmit + @param tx - returns number of transmitted buffers + + \return memif_err_t +*/ +int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t * bufs, uint16_t count, uint16_t * tx); + +/** \brief Memif receive buffer burst + @param conn - memif conenction handle + @param qid - number indentifying queue + @param bufs - memif buffers + @param count - number of memif buffers to receive + @param rx - returns number of received buffers + + \return memif_err_t +*/ +int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t * bufs, uint16_t count, uint16_t * rx); + +/** \brief Memif poll event + @param timeout - timeout in seconds + + Passive event polling - + timeout = 0 - dont wait for event, check event queue if there is an event and return. + timeout = -1 - wait until event + + \return memif_err_t +*/ +int memif_poll_event (int timeout); +/** @} */ + +#endif /* _LIBMEMIF_H_ */ -- 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/src/libmemif.h') 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