From 491a794239e15696b1fb6f7675588fe68ee13c88 Mon Sep 17 00:00:00 2001 From: JakubGrajciar Date: Wed, 21 Jun 2017 14:08:04 +0200 Subject: [PATCH 01/63] merge --- Makefile.am | 24 ++- examples/icmp_responder/main.c | 2 +- src/libmemif.h | 36 +++- src/main.c | 31 ++- src/memif.h | 18 +- src/memif_private.h | 175 +++++++++++++++ src/socket.c | 296 +++++++++++++++++++++++++ src/socket.h | 60 ++++++ test/socket_test.c | 379 +++++++++++++++++++++++++++++++++ test/socket_test.h | 27 +++ test/unit_test.c | 32 +++ test/unit_test.h | 60 ++++++ 12 files changed, 1126 insertions(+), 14 deletions(-) create mode 100644 src/memif_private.h create mode 100644 src/socket.c create mode 100644 src/socket.h create mode 100644 test/socket_test.c create mode 100644 test/socket_test.h create mode 100644 test/unit_test.c create mode 100644 test/unit_test.h diff --git a/Makefile.am b/Makefile.am index 88bf9d1..4fef3b2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -13,12 +13,21 @@ AUTOMAKE_OPTIONS = foreign subdir-objects +ACLOCAL_AMFLAGS = -I m4 + +# +# unit_test +# +unit_test_SOURCES = test/socket_test.c test/unit_test.c src/socket.c +# macro MEMIF_UNIT_TEST -> compile functions without static keyword +# and declare them in header files, so they can be called from unit tests +unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST + # # main lib # -libmemif_la_SOURCES = src/main.c -lib_LTLIBRARIES = libmemif.la -include_HEADERS = src/libmemif.h +libmemif_la_SOURCES = src/main.c src/socket.c +libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc # # ICMP responder example @@ -26,4 +35,13 @@ include_HEADERS = src/libmemif.h icmp_responder_SOURCES = examples/icmp_responder/main.c icmp_responder_LDADD = libmemif.la icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc + noinst_PROGRAMS = icmp_responder + +check_PROGRAMS = unit_test + +include_HEADERS = src/libmemif.h + +lib_LTLIBRARIES = libmemif.la + +TESTS = $(check_PROGRAMS) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 22271db..133be88 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -21,6 +21,6 @@ int main () { - printf ("foo is %u\n", foo (5)); + printf ("foo is nowhere to be found..."); } diff --git a/src/libmemif.h b/src/libmemif.h index 690bcb7..513c704 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -18,6 +18,40 @@ #ifndef _LIBMEMIF_H_ #define _LIBMEMIF_H_ -int foo (int x); +#include + +#include + +typedef void* memif_conn_handle_t; + +typedef struct +{ + uint8_t *socket_filename; + uint8_t secret[24]; + + uint8_t num_s2m_rings; + uint8_t num_m2s_rings; + uint16_t buffer_size; + uint16_t log2_ring_size; + uint8_t is_master; + + uint32_t interface_id; + uint8_t interface_name[32]; + uint8_t instance_name[32]; + memif_interface_mode_t mode:8; +} memif_conn_args_t; + +/* initialize memif connection. connect socket */ +int memif_connect (memif_conn_handle_t *conn, memif_conn_args_t * args); + +/* returns file descriptor for control channel. user can register fd for polling */ +/* on event user calls memif_control_handler () */ +int memif_get_control_fd (memif_conn_handle_t conn); + +/* handles control channel events (connect/disconnect) */ +int memif_control_fd_handler (memif_conn_handle_t conn, int fd); + +/* disconnect session (free memory, close file descriptors, unmap shared memory) */ +int memif_disconnect (memif_conn_handle_t conn); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index 7a1f463..5cb6b48 100644 --- a/src/main.c +++ b/src/main.c @@ -17,12 +17,35 @@ #include -#include "memif.h" -#include "libmemif.h" +/* memif protocol msg, ring and descriptor definitions */ +#include +/* memif api */ +#include +/* socket messaging functions */ +#include +/* private structs and functions */ +#include +int +memif_connect (memif_conn_handle_t *conn, memif_conn_args_t *args) +{ + return 0; +} -int foo (int x) +int +memif_get_control_fd (memif_conn_handle_t conn) { - return x + 1; + return 0; } +int +memif_control_fd_handler (memif_conn_handle_t conn, int fd) +{ + return 0; +} + +int +memif_disconnect (memif_conn_handle_t conn) +{ + return 0; +} diff --git a/src/memif.h b/src/memif.h index 30a27dc..11918ea 100644 --- a/src/memif.h +++ b/src/memif.h @@ -22,6 +22,11 @@ #define MEMIF_CACHELINE_SIZE 64 #endif +#define MEMIF_COOKIE 0x3E31F10 +#define MEMIF_VERSION_MAJOR 1 +#define MEMIF_VERSION_MINOR 0 +#define MEMIF_VERSION ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR) + /* * Type definitions */ @@ -53,9 +58,12 @@ typedef enum } memif_interface_mode_t; typedef uint16_t memif_region_index_t; +typedef uint64_t memif_region_offset_t; +typedef uint64_t memif_region_size_t; typedef uint16_t memif_ring_index_t; typedef uint32_t memif_interface_id_t; typedef uint16_t memif_version_t; +typedef uint8_t memif_log2_ring_size_t; /* * Socket messages @@ -69,7 +77,7 @@ typedef struct __attribute__ ((packed)) memif_region_index_t max_region; memif_ring_index_t max_m2s_ring; memif_ring_index_t max_s2m_ring; - uint8_t max_log2_ring_size; + memif_log2_ring_size_t max_log2_ring_size; } memif_msg_hello_t; typedef struct __attribute__ ((packed)) @@ -84,7 +92,7 @@ typedef struct __attribute__ ((packed)) typedef struct __attribute__ ((packed)) { memif_region_index_t index; - uint32_t size; + memif_region_size_t size; } memif_msg_add_region_t; typedef struct __attribute__ ((packed)) @@ -93,8 +101,8 @@ typedef struct __attribute__ ((packed)) #define MEMIF_MSG_ADD_RING_FLAG_S2M (1 << 0) memif_ring_index_t index; memif_region_index_t region; - uint32_t offset; - uint8_t log2_ring_size; + memif_region_offset_t offset; + memif_log2_ring_size_t log2_ring_size; } memif_msg_add_ring_t; typedef struct __attribute__ ((packed)) @@ -143,7 +151,7 @@ typedef struct __attribute__ ((packed)) uint32_t buffer_length; uint32_t length; uint8_t reserved[4]; - uint64_t offset; + memif_region_offset_t offset; uint64_t metadata; } memif_desc_t; diff --git a/src/memif_private.h b/src/memif_private.h new file mode 100644 index 0000000..4b00109 --- /dev/null +++ b/src/memif_private.h @@ -0,0 +1,175 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + + +#ifndef _MEMIF_PRIVATE_H_ +#define _MEMIF_PRIVATE_H_ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include + +#define MEMIF_DEFAULT_SOCKET_DIR "/run/vpp" +#define MEMIF_DEFAULT_SOCKET_FILENAME "memif.sock" +#define MEMIF_DEFAULT_RING_SIZE 1024 +#define MEMIF_DEFAULT_LOG2_RING_SIZE 10 +#define MEMIF_DEFAULT_RX_QUEUES 1 +#define MEMIF_DEFAULT_TX_QUEUES 1 +#define MEMIF_DEFAULT_BUFFER_SIZE 2048 + +#define MEMIF_MAX_M2S_RING 1 +#define MEMIF_MAX_S2M_RING 1 +#define MEMIF_MAX_REGION 255 +#define MEMIF_MAX_LOG2_RING_SIZE 14 + +#define MEMIF_MAX_FDS 512 + +#define MEMIF_DEBUG 1 + +#if MEMIF_DEBUG == 1 +#define DBG(...) do { \ + printf("MEMIF_DEBUG: %s:%d: ", __func__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) + +#define DBG_UNIX(...) do { \ + printf("MEMIF_DEBUG_UNIX: %s:%d: ", __func__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) + +#else +#define DBG(...) +#define DBG_UNIX(...) +#endif /* MEMIF_DEBUG */ + +#define error_return_unix(...) do { \ + DBG_UNIX(__VA_ARGS__); \ + return -1; \ + } while (0) +#define error_return(...) do { \ + DBG(__VA_ARGS__); \ + return -1; \ + } while (0) + +typedef struct +{ + void *shm; + uint32_t region_size; + int fd; +} memif_region_t; + +typedef struct +{ + memif_ring_t *ring; + uint8_t log2_ring_size; + uint8_t region; + uint32_t offset; + + uint16_t last_head; + uint16_t last_tail; + + int int_fd; + + uint64_t int_count; +} memif_queue_t; + +typedef struct memif_msg_queue_elt +{ + memif_msg_t msg; + int fd; + struct memif_msg_queue_elt *next; +} memif_msg_queue_elt_t; + +struct memif_connection; + +typedef struct memif_connection memif_connection_t; + +/* functions called by memif_control_fd_handler */ +typedef int (memif_fn) (memif_connection_t *conn); + +typedef struct memif_connection +{ + memif_conn_args_t args; + + int fd; + + memif_fn *write_fn, *read_fn, *error_fn; + + /* connection message queue */ + memif_msg_queue_elt_t *msg_queue; + + uint8_t remote_if_name[32]; + uint8_t remote_name[32]; + + memif_region_t *regions; + + memif_queue_t *rx_queues; + memif_queue_t *tx_queues; +} memif_connection_t; + +/* main.c */ + +/* if region doesn't contain shared memory, mmap region, check ring cookie */ +int memif_connect1 (memif_connection_t *c); + +/* memory map region, initalize rings and queues */ +int memif_init_regions_and_queues (memif_connection_t *c); + +#ifndef __NR_memfd_create +#if defined __x86_64__ +#define __NR_memfd_create 319 +#elif defined __arm__ +#define __NR_memfd_create 385 +#elif defined __aarch64__ +#define __NR_memfd_create 279 +#else +#error "__NR_memfd_create unknown for this architecture" +#endif +#endif + +static inline int +memfd_create (const char *name, unsigned int flags) +{ + return syscall (__NR_memfd_create, name, flags); +} + +static inline void * +memif_get_buffer (memif_connection_t *conn, memif_ring_t *ring, uint16_t index) +{ + return (conn->regions[ring->desc[index].region].shm + ring->desc[index].offset); +} + +#ifndef F_LINUX_SPECIFIC_BASE +#define F_LINUX_SPECIFIC_BASE 1024 +#endif +#define MFD_ALLOW_SEALING 0x0002U +#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) +#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) + +#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ +#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ +#define F_SEAL_GROW 0x0004 /* prevent file from growing */ +#define F_SEAL_WRITE 0x0008 /* prevent writes */ + +#endif /* _MEMIF_PRIVATE_H_ */ diff --git a/src/socket.c b/src/socket.c new file mode 100644 index 0000000..a70da81 --- /dev/null +++ b/src/socket.c @@ -0,0 +1,296 @@ +/* + *------------------------------------------------------------------ + * 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 + +#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) +{ + return 0; +} + +/* response from memif master - master is ready to handle next message */ +static_fn void +memif_msg_enq_ack (memif_connection_t *c) +{ + memif_msg_queue_elt_t *e = + (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + + e->msg.type = MEMIF_MSG_TYPE_ACK; + e->fd = -1; + e->next = NULL; + + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return; + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; +} + +static_fn int +memif_msg_send_hello (memif_connection_t *c) +{ + memif_msg_t msg = { 0 }; + memif_msg_hello_t *h = &msg.hello; + msg.type = MEMIF_MSG_TYPE_HELLO; + h->min_version = MEMIF_VERSION; + h->max_version = MEMIF_VERSION; + h->max_s2m_ring = MEMIF_MAX_M2S_RING; + h->max_m2s_ring = MEMIF_MAX_M2S_RING; + h->max_region = MEMIF_MAX_REGION; + h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE; + + strncpy ((char *) h->name, (char *) c->args.instance_name, + strlen ((char *) c->args.instance_name)); + + /* or initialize hello msg and return it to user to create own communication + (if there is use case) */ + /* msg hello is not enqueued but sent directly, + because it is the first msg to be sent */ + return memif_msg_send (c->fd, &msg, -1); +} + +/* send id and secret (optional) for interface identification */ +static_fn void +memif_msg_enq_init (memif_connection_t *c) +{ + memif_msg_queue_elt_t *e = + (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + + memif_msg_init_t *i = &e->msg.init; + + e->msg.type = MEMIF_MSG_TYPE_INIT; + e->fd = -1; + i->version = MEMIF_VERSION; + i->id = c->args.interface_id; + i->mode = c->args.mode; + + strncpy ((char *) i->name, (char *) c->args.instance_name, + strlen ((char *) c->args.instance_name)); + if (c->args.secret) + strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret)); + + e->next = NULL; + + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return; + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; +} + +static_fn int +memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) +{ + memif_msg_hello_t *h = &msg->hello; + + if (msg->hello.min_version > MEMIF_VERSION || + msg->hello.max_version < MEMIF_VERSION) + { + DBG ("incompatible protocol version"); + return -1; + } + /* there will probably be a nested struct c->run containing following variables + (this will be used to adjust shared memory information while keeping + configured values intact) */ + c->args.num_s2m_rings = memif_min (h->max_s2m_ring + 1, + c->args.num_s2m_rings); + c->args.num_m2s_rings = memif_min (h->max_m2s_ring + 1, + c->args.num_m2s_rings); + c->args.log2_ring_size = memif_min (h->max_log2_ring_size, + c->args.log2_ring_size); + strncpy ((char *) c->remote_name, (char *) h->name, strlen ((char *) h->name)); + + return 0; +} + +/* handle interface identification (id, secret (optional)) */ +static_fn int +memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) +{ + memif_msg_init_t *i = &msg->init; + if (i->version != MEMIF_VERSION) + { + DBG ("unsuported version"); + goto error; + } + if (c->args.interface_id != i->id) + { + DBG ("unmatched interface id"); + goto error; + } + + + if (!(c->args.is_master)) + { + DBG ("cannot connect to slave"); + goto error; + } + if (c->fd != -1) + { + DBG ("already connected"); + goto error; + } + if (i->mode != c->args.mode) + { + DBG ("mode mismatch"); + goto error; + } + + strncpy ((char *) c->remote_name, (char *) i->name, strlen ((char *) i->name)); + + if (c->args.secret) + { + int r; + if (i->secret) + { + if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret)) + { + DBG ("incorrect secret"); + return -1; + } + r = strncmp ((char *) i->secret, (char *) c->args.secret, + strlen ((char *) c->args.secret)); + if (r != 0) + { + DBG ("incorrect secret"); + return -1; + } + } + else + { + DBG ("secret required"); + return -1; + } + } + return 0; + +error: + /*memif_msg_send_disconnect (c);*/ + return -1; +} + +/* + * Slave + */ + +int +memif_slave_conn_fd_read_ready (memif_connection_t *c) +{ + /* calls memif_msg_receive to handle pending messages on socket */ + /* memif_msg_receive will call required functions based on "memif protocol" */ + return 0; +} + +/* get msg from msg queue buffer and send it to socket */ +int +memif_slave_conn_fd_write_ready (memif_connection_t *c) +{ + memif_msg_queue_elt_t *e = c->msg_queue; + c->msg_queue = c->msg_queue->next; + + int rv = 0; + /* TODO: check if write is possible */ + + rv = memif_msg_send (c->fd, &e->msg, e->fd); + free(e); + return rv; +} + +int +memif_slave_conn_fd_error (memif_connection_t *c) +{ + return 0; +} + +/* + * Master + */ + +int +memif_master_conn_fd_read_ready (memif_connection_t *c) +{ + return 0; +} + +int +memif_master_conn_fd_write_ready (memif_connection_t *c) +{ + return 0; +} + +int +memif_master_conn_fd_error (memif_connection_t *c) +{ + return 0; +} + +int +memif_conn_fd_accept_ready (memif_connection_t *c) +{ + int addr_len; + struct sockaddr_un client; + int conn_fd; + + addr_len = sizeof (client); + conn_fd = accept (c->fd, (struct sockaddr *) &client, (socklen_t *) &addr_len); + + if (conn_fd < 0) + { + DBG ("accept fd %d", c->fd); + return -1; + } + + c->read_fn = memif_master_conn_fd_read_ready; + c->write_fn = memif_master_conn_fd_write_ready; + c->error_fn = memif_master_conn_fd_error; + c->fd = conn_fd; + + int e = memif_msg_send_hello (c); + if (e < 0) + { + DBG ("memif msg send hello error!"); + return -1; + } + + return 0; +} diff --git a/src/socket.h b/src/socket.h new file mode 100644 index 0000000..c5288b2 --- /dev/null +++ b/src/socket.h @@ -0,0 +1,60 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + +#ifndef _SOCKET_H_ +#define _SOCKET_H + +#include + +/* socket.c */ + +int memif_slave_conn_fd_read_ready (memif_connection_t *c); + +int memif_slave_conn_fd_write_ready (memif_connection_t *c); + +int memif_slave_conn_fd_error (memif_connection_t *c); + +int memif_conn_fd_accept_ready (memif_connection_t *c); + +int memif_master_conn_fd_read_ready (memif_connection_t *c); + +int memif_master_conn_fd_write_ready (memif_connection_t *c); + +int memif_master_conn_fd_error (memif_connection_t *c); + +/* when compiling unit tests, compile functions without static keyword + and declare functions in header file */ +#ifdef MEMIF_UNIT_TEST +#define static_fn + +static_fn int memif_msg_send (int fd, memif_msg_t *msg, int afd); + +static_fn void memif_msg_enq_ack (memif_connection_t *c); + +static_fn int memif_msg_send_hello (memif_connection_t *c); + +static_fn void memif_msg_enq_init (memif_connection_t *c); + +static_fn int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg); + +static_fn int memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg); + +#else +#define static_fn static +#endif /* MEMIF_UNIT_TEST */ + +#endif /* _SOCKET_H_ */ diff --git a/test/socket_test.c b/test/socket_test.c new file mode 100644 index 0000000..a471687 --- /dev/null +++ b/test/socket_test.c @@ -0,0 +1,379 @@ +/* + *------------------------------------------------------------------ + * 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 + +static int +get_queue_len (memif_msg_queue_elt_t *q) +{ + int r = 0; + memif_msg_queue_elt_t *c = q; + while (c != NULL) + { + r++; + c = c->next; + } + return r; +} + +static int +test_msg_queue_add_pop () +{ + int rv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + + int i, len = 10; + + for (i = 0; i < len; i++) + { + if (i % 2) + memif_msg_enq_ack (&conn); + else + memif_msg_enq_init (&conn); + } + + if (len != get_queue_len (conn.msg_queue)) + { + ERROR("incorrect queue len"); + rv = -1; + } + + int pop = 6; + + for (i = 0; i < pop; i++) + { + if (i % 2) + { + if (conn.msg_queue->msg.type != MEMIF_MSG_TYPE_ACK) + { + ERROR ("incorrect msg type"); + rv = -1; + } + } + else + { + if (conn.msg_queue->msg.type != MEMIF_MSG_TYPE_INIT) + { + ERROR ("incorrect msg type"); + rv = -1; + } + } + memif_slave_conn_fd_write_ready (&conn); + } + + if ((len - pop) != get_queue_len (conn.msg_queue)) + { + ERROR("incorrect queue_len"); + rv = -1; + } + + return rv; + +} + +static int +test_msg_enq_ack () +{ + int rv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + + memif_msg_enq_ack (&conn); + memif_msg_queue_elt_t *e = conn.msg_queue; + + if (e->msg.type != MEMIF_MSG_TYPE_ACK) + { + ERROR ("incorrect msg type"); + rv = -1; + } + if (e->fd != -1) + { + ERROR ("incorrect file descriptor"); + rv = -1; + } + return rv; +} + + +static int +test_msg_enq_init () +{ + int rv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + + conn.args.interface_id = 69; + conn.args.mode = 0; + + strncpy ((char *)conn.args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + strncpy ((char *) conn.args.secret, TEST_SECRET, strlen (TEST_SECRET)); + + memif_msg_enq_init (&conn); + memif_msg_queue_elt_t *e = conn.msg_queue; + + if (e->msg.type != MEMIF_MSG_TYPE_INIT) + { + ERROR ("incorrect msg type"); + rv = -1; + } + if (e->fd != -1) + { + ERROR ("incorrect file descriptor"); + rv = -1; + } + memif_msg_init_t *i = &e->msg.init; + if (i->version != MEMIF_VERSION) + { + ERROR ("incorrect memif version"); + rv = -1; + } + if (i->id != conn.args.interface_id) + { + ERROR ("incorrect interface id"); + rv = -1; + } + if (i->mode != conn.args.mode) + { + ERROR ("incorrect mode"); + rv = -1; + } + if (strncmp ((char *) i->name, (char *) conn.args.instance_name, + strlen ((char *) i->name)) != 0) + { + ERROR ("incorrect interface name"); + rv = -1; + } + if (strncmp ((char *) i->secret, (char *) conn.args.secret, + strlen ((char *) i->secret)) != 0) + { + ERROR ("incorrect secret"); + rv = -1; + } + + return rv; +} + +static int +test_msg_receive_hello () +{ + int rv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + + memif_msg_t msg; + + memif_msg_hello_t *h = &msg.hello; + + msg.type = MEMIF_MSG_TYPE_HELLO; + + h->min_version = MEMIF_VERSION; + h->max_version = MEMIF_VERSION; + h->max_s2m_ring = 1; + h->max_m2s_ring = 1; + h->max_log2_ring_size = 14; + strncpy ((char *) h->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + + conn.args.num_s2m_rings = 4; + conn.args.num_m2s_rings = 6; + conn.args.log2_ring_size = 10; + + rv = memif_msg_receive_hello (&conn, &msg); + if (rv < 0) + { + ERROR ("memif protocol mismatch"); + return rv; + } + + if (conn.args.num_s2m_rings != 2) + { + ERROR ("incorrect number of slave to master rings"); + rv = -1; + } + if (conn.args.num_m2s_rings != 2) + { + ERROR ("incorrect number of master to slave rings"); + rv = -1; + } + if (conn.args.log2_ring_size != 10) + { + ERROR ("incorrect ring size"); + rv = -1; + } + if (strncmp ((char *) conn.remote_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0) + { + ERROR ("incorrect remote name"); + rv = -1; + } + + h->max_version = 9; + if (memif_msg_receive_hello (&conn, &msg) == 0) + { + ERROR ("no error on protocol mismatch"); + rv = -1; + } + + return rv; +} + +static int +test_msg_receive_init () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + + conn.args.interface_id = 69; + conn.args.is_master = 1; + conn.fd = -1; + conn.args.mode = 0; + memset (conn.args.secret, '\0', 24); + strncpy ((char *) conn.args.secret, TEST_SECRET, strlen (TEST_SECRET)); + + + memif_msg_t msg; + + memif_msg_init_t *i = &msg.init; + + msg.type = MEMIF_MSG_TYPE_INIT; + + i->version = MEMIF_VERSION; + i->id = 69; + i->mode = 0; + memset (i->name, '\0', 32); + memset (i->secret, '\0', 24); + strncpy ((char *) i->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); + + frv = memif_msg_receive_init (&conn, &msg); + if (frv < 0) + { + ERROR ("function error"); + rv = -1; + } + i->version = 9; + frv = memif_msg_receive_init (&conn, &msg); + if (frv == 0) + { + ERROR ("version mismatch (fn succes)"); + rv = -1; + } + i->version = MEMIF_VERSION; + i->id = 78; + frv = memif_msg_receive_init (&conn, &msg); + if (frv == 0) + { + ERROR ("id mismatch (fn success)"); + rv = -1; + } + i->id = 69; + i->mode = 1; + frv = memif_msg_receive_init (&conn, &msg); + if (frv == 0) + { + ERROR ("mode mismatch (fn success)"); + rv = -1; + } + i->mode = 0; + i->secret[0] = '\0'; + frv = memif_msg_receive_init (&conn, &msg); + if (frv == 0) + { + ERROR ("secret mismatch (fn success)"); + rv = -1; + } + strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); + conn.args.is_master = 0; + frv = memif_msg_receive_init (&conn, &msg); + if (frv == 0) + { + ERROR ("slave cannot accept connection (fn success)"); + rv = -1; + } + conn.args.is_master = 1; + conn.fd = 5; + frv = memif_msg_receive_init (&conn, &msg); + if (frv == 0) + { + ERROR ("already connected (fn success)"); + rv = -1; + } + return rv; +} + +int +test_socket (uint16_t *s, uint16_t *f) +{ + TEST_SET ("SOCKET.C"); + + if (test_msg_queue_add_pop () < 0) + { + (*f)++; + TEST_FAIL ("msg queue add/pop"); + } + else + { + (*s)++; + TEST_OK ("msg queue add/pop"); + } + + if (test_msg_enq_ack () < 0) + { + (*f)++; + TEST_FAIL ("msg enq ack"); + } + else + { + (*s)++; + TEST_OK ("msg enq ack"); + } + + if (test_msg_enq_init () < 0) + { + (*f)++; + TEST_FAIL ("msg enq init"); + } + else + { + (*s)++; + TEST_OK ("msg enq init"); + } + + if (test_msg_receive_hello () < 0) + { + (*f)++; + TEST_FAIL ("msg recv hello"); + } + else + { + (*s)++; + TEST_OK ("msg recv hello"); + } + + if (test_msg_receive_init () < 0) + { + (*f)++; + TEST_FAIL ("msg recv init"); + } + else + { + (*s)++; + TEST_OK ("msg recv init"); + } + + return 0; +} diff --git a/test/socket_test.h b/test/socket_test.h new file mode 100644 index 0000000..b55ac47 --- /dev/null +++ b/test/socket_test.h @@ -0,0 +1,27 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + +#ifndef _SOCKET_TEST_H_ +#define _SOCKET_TEST_H_ + +#include +#include +#include + +int test_socket (uint16_t *success, uint16_t *fail); + +#endif /* _SOCKET_TEST_H_ */ diff --git a/test/unit_test.c b/test/unit_test.c new file mode 100644 index 0000000..e2e3f79 --- /dev/null +++ b/test/unit_test.c @@ -0,0 +1,32 @@ +/* + *------------------------------------------------------------------ + * 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 + +int +main (void) +{ + uint16_t s = 0, f = 0; + + test_socket (&s, &f); + + INFO ("Success: %u Fail: %u", s, f); + + if (f != 0) + return -1; + return 0; +} diff --git a/test/unit_test.h b/test/unit_test.h new file mode 100644 index 0000000..712ba97 --- /dev/null +++ b/test/unit_test.h @@ -0,0 +1,60 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + +#ifndef _UNIT_TEST_H_ +#define _UNIT_TEST_H_ + +#include +#include + +#define RED "\x1b[31m" +#define GREEN "\x1b[32m" +#define YELLOW "\x1b[33m" +#define DEFAULT "\x1b[0m" + +#define INFO(...) do { \ + printf (DEFAULT"info: "__VA_ARGS__); \ + printf ("\n"); \ + } while (0) + +#define TEST_SET(...) do { \ + printf (YELLOW"TEST SET: "__VA_ARGS__); \ + printf (DEFAULT"\n"); \ + printf ("=============================================\n"); \ + } while (0) + +#define TEST_OK(...) do { \ + printf ("%-40s", DEFAULT"test: "__VA_ARGS__); \ + printf ("\t"GREEN"OK!"DEFAULT"\n"); \ + } while (0) + +#define TEST_FAIL(...) do { \ + printf ("%-40s", DEFAULT"test: "__VA_ARGS__); \ + printf ("\t"RED"FAIL!"DEFAULT"\n"); \ + } while (0) \ + +#define ERROR(...) do { \ + printf (RED"ERROR:"DEFAULT"%s:%d: ",__func__, __LINE__);\ + printf (__VA_ARGS__); \ + printf ("\n"); \ + } while (0) + +#define TEST_APP_NAME "unit_test_app" +#define TEST_IF_NAME "unit_test_if" +#define TEST_SECRET "psst" + +#endif /* _UNIT_TEST_H_ */ From 36b1d55f079a9bd70dc165b13eeb2793de0f3253 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 12:10:56 +0200 Subject: [PATCH 02/63] ODPM-378 --- src/socket.c | 126 +++++++++++++++++++++++++++++---------------------- src/socket.h | 33 ++++++++------ 2 files changed, 90 insertions(+), 69 deletions(-) diff --git a/src/socket.c b/src/socket.c index a70da81..d44664f 100644 --- a/src/socket.c +++ b/src/socket.c @@ -15,17 +15,25 @@ *------------------------------------------------------------------ */ +#define _GNU_SOURCE #include #include #include #include #include #include +#include +#include +#include +#include +#include +#include +#include #include #include -#define memif_min(a,b) a < b ? a : b +#define memif_min(a,b) ((a < b) ? (a) : (b)) /* sends msg to socket */ static_fn int @@ -43,8 +51,8 @@ memif_msg_enq_ack (memif_connection_t *c) e->msg.type = MEMIF_MSG_TYPE_ACK; e->fd = -1; - e->next = NULL; + e->next = NULL; if (c->msg_queue == NULL) { c->msg_queue = e; @@ -75,8 +83,6 @@ memif_msg_send_hello (memif_connection_t *c) strncpy ((char *) h->name, (char *) c->args.instance_name, strlen ((char *) c->args.instance_name)); - /* or initialize hello msg and return it to user to create own communication - (if there is use case) */ /* msg hello is not enqueued but sent directly, because it is the first msg to be sent */ return memif_msg_send (c->fd, &msg, -1); @@ -103,7 +109,6 @@ memif_msg_enq_init (memif_connection_t *c) strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret)); e->next = NULL; - if (c->msg_queue == NULL) { c->msg_queue = e; @@ -129,8 +134,8 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) DBG ("incompatible protocol version"); return -1; } - /* there will probably be a nested struct c->run containing following variables - (this will be used to adjust shared memory information while keeping + /* use nested struct c->run containing following variables? + (this would be used to adjust shared memory information while keeping configured values intact) */ c->args.num_s2m_rings = memif_min (h->max_s2m_ring + 1, c->args.num_s2m_rings); @@ -148,31 +153,37 @@ static_fn int memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) { memif_msg_init_t *i = &msg->init; + uint8_t err_string[96]; if (i->version != MEMIF_VERSION) { - DBG ("unsuported version"); + DBG ("MEMIF_VER_ERR"); + strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR)); goto error; } if (c->args.interface_id != i->id) { - DBG ("unmatched interface id"); + DBG ("MEMIF_ID_ERR"); + strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR)); goto error; } if (!(c->args.is_master)) { - DBG ("cannot connect to slave"); + DBG ("MEMIF_SLAVE_ERR"); + strncpy ((char *) err_string, MEMIF_SLAVE_ERR, strlen (MEMIF_SLAVE_ERR)); goto error; } if (c->fd != -1) { - DBG ("already connected"); + DBG ("MEMIF_CONN_ERR"); + strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR)); goto error; } if (i->mode != c->args.mode) { - DBG ("mode mismatch"); + DBG ("MEMIF_MODE_ERR"); + strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR)); goto error; } @@ -185,83 +196,84 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) { if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret)) { - DBG ("incorrect secret"); + DBG ("MEMIF_SECRET_ERR"); + strncpy ((char *) err_string, + MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); return -1; } r = strncmp ((char *) i->secret, (char *) c->args.secret, strlen ((char *) c->args.secret)); if (r != 0) { - DBG ("incorrect secret"); + DBG ("MEMIF_SECRET_ERR"); + strncpy ((char *) err_string, + MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); return -1; } } else { - DBG ("secret required"); + DBG ("MEMIF_NOSECRET_ERR"); + strncpy ((char *) err_string, + MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR)); return -1; } } return 0; error: - /*memif_msg_send_disconnect (c);*/ + memif_msg_send_disconnect (c, err_string, 1); return -1; } -/* - * Slave - */ - -int -memif_slave_conn_fd_read_ready (memif_connection_t *c) +static_fn int +memif_msg_receive (memif_connection_t *c) { - /* calls memif_msg_receive to handle pending messages on socket */ - /* memif_msg_receive will call required functions based on "memif protocol" */ return 0; } -/* get msg from msg queue buffer and send it to socket */ -int -memif_slave_conn_fd_write_ready (memif_connection_t *c) -{ - memif_msg_queue_elt_t *e = c->msg_queue; - c->msg_queue = c->msg_queue->next; - - int rv = 0; - /* TODO: check if write is possible */ - - rv = memif_msg_send (c->fd, &e->msg, e->fd); - free(e); - return rv; -} - int -memif_slave_conn_fd_error (memif_connection_t *c) +memif_conn_fd_error (memif_connection_t *c) { + DBG ("connection fd error"); + strncpy ((char *) c->remote_disconnect_string, "connection fd error", + 19); + memif_disconnect_internal (c); return 0; } -/* - * Master - */ - +/* calls memif_msg_receive to handle pending messages on socket */ int -memif_master_conn_fd_read_ready (memif_connection_t *c) +memif_conn_fd_read_ready (memif_connection_t *c) { + int rv = 0; + rv = memif_msg_receive (c); + if (rv < 0) + { + memif_disconnect_internal (c); + } return 0; } +/* get msg from msg queue buffer and send it to socket */ int -memif_master_conn_fd_write_ready (memif_connection_t *c) +memif_conn_fd_write_ready (memif_connection_t *c) { - return 0; -} + int rv = 0; + if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0) + return rv; -int -memif_master_conn_fd_error (memif_connection_t *c) -{ - return 0; + memif_msg_queue_elt_t *e = c->msg_queue; + if (e == NULL) + return rv; + + c->msg_queue = c->msg_queue->next; + + c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE; + + rv = memif_msg_send (c->fd, &e->msg, e->fd); + free(e); + return rv; } int @@ -271,6 +283,8 @@ memif_conn_fd_accept_ready (memif_connection_t *c) struct sockaddr_un client; int conn_fd; + DBG ("accept called"); + addr_len = sizeof (client); conn_fd = accept (c->fd, (struct sockaddr *) &client, (socklen_t *) &addr_len); @@ -279,10 +293,12 @@ memif_conn_fd_accept_ready (memif_connection_t *c) DBG ("accept fd %d", c->fd); return -1; } + DBG ("accept fd %d", c->fd); + DBG ("conn fd %d", conn_fd); - c->read_fn = memif_master_conn_fd_read_ready; - c->write_fn = memif_master_conn_fd_write_ready; - c->error_fn = memif_master_conn_fd_error; + c->read_fn = memif_conn_fd_read_ready; + c->write_fn = memif_conn_fd_write_ready; + c->error_fn = memif_conn_fd_error; c->fd = conn_fd; int e = memif_msg_send_hello (c); diff --git a/src/socket.h b/src/socket.h index c5288b2..c999385 100644 --- a/src/socket.h +++ b/src/socket.h @@ -20,38 +20,43 @@ #include +/* interface identification errors */ +#define MEMIF_VER_ERR "incompatible version" +#define MEMIF_ID_ERR "unmatched interface id" +#define MEMIF_SLAVE_ERR "cannot connect to salve" +#define MEMIF_CONN_ERR "already connected" +#define MEMIF_MODE_ERR "mode mismatch" +#define MEMIF_SECRET_ERR "incorrect secret" +#define MEMIF_NOSECRET_ERR "secret required" + /* socket.c */ -int memif_slave_conn_fd_read_ready (memif_connection_t *c); +int memif_conn_fd_read_ready (memif_connection_t *c); -int memif_slave_conn_fd_write_ready (memif_connection_t *c); +int memif_conn_fd_write_ready (memif_connection_t *c); -int memif_slave_conn_fd_error (memif_connection_t *c); +int memif_conn_fd_error (memif_connection_t *c); int memif_conn_fd_accept_ready (memif_connection_t *c); -int memif_master_conn_fd_read_ready (memif_connection_t *c); - -int memif_master_conn_fd_write_ready (memif_connection_t *c); - -int memif_master_conn_fd_error (memif_connection_t *c); +int memif_msg_send_disconnect (memif_connection_t *c, uint8_t *err_string, uint32_t err_code); /* when compiling unit tests, compile functions without static keyword and declare functions in header file */ #ifdef MEMIF_UNIT_TEST #define static_fn -static_fn int memif_msg_send (int fd, memif_msg_t *msg, int afd); +int memif_msg_send (int fd, memif_msg_t *msg, int afd); -static_fn void memif_msg_enq_ack (memif_connection_t *c); +void memif_msg_enq_ack (memif_connection_t *c); -static_fn int memif_msg_send_hello (memif_connection_t *c); +int memif_msg_send_hello (memif_connection_t *c); -static_fn void memif_msg_enq_init (memif_connection_t *c); +void memif_msg_enq_init (memif_connection_t *c); -static_fn int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg); +int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg); -static_fn int memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg); +int memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg); #else #define static_fn static From 6f561c31dc29c48a670596f9c4332c239129b5c1 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 12:13:36 +0200 Subject: [PATCH 03/63] ODPM-381 --- src/socket.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/socket.h | 5 +++++ 2 files changed, 64 insertions(+) diff --git a/src/socket.c b/src/socket.c index d44664f..2e7cbd3 100644 --- a/src/socket.c +++ b/src/socket.c @@ -123,6 +123,42 @@ memif_msg_enq_init (memif_connection_t *c) cur->next = e; } +/* send information about region specified by region_index */ +static_fn int +memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) +{ + if (c->regions == NULL) + error_return ("no regions initialized"); + + /* TODO: support multiple regions */ + memif_region_t *mr = c->regions; + + memif_msg_queue_elt_t *e = + (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + + memif_msg_add_region_t *ar = &e->msg.add_region; + + e->msg.type = MEMIF_MSG_TYPE_ADD_REGION; + e->fd = mr->fd; + ar->index = region_index; + ar->size = mr->region_size; + + e->next = NULL; + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return 0; + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + return 0; +} + static_fn int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) { @@ -226,6 +262,29 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) return -1; } +/* receive region information and add new region to connection (if possible) */ +static_fn int +memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) +{ + memif_msg_add_region_t *ar = &msg->add_region; + memif_region_t *mr; + if (fd < 0) + error_return ("missing memory region fd"); + + if (ar->index > MEMIF_MAX_REGION) + error_return ("maximum region limit reached"); + + mr = (memif_region_t *) malloc (sizeof (memif_region_t )); + mr->fd = fd; + mr->region_size = ar->size; + mr->shm = NULL; + + /* TODO: support multiple regions */ + c->regions = mr; + + return 0; +} + static_fn int memif_msg_receive (memif_connection_t *c) { diff --git a/src/socket.h b/src/socket.h index c999385..575590a 100644 --- a/src/socket.h +++ b/src/socket.h @@ -54,10 +54,15 @@ int memif_msg_send_hello (memif_connection_t *c); void memif_msg_enq_init (memif_connection_t *c); +int memif_msg_enq_add_region (memif_connection_t *c, uint8_t region); + int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg); int memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg); +int memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd); + + #else #define static_fn static #endif /* MEMIF_UNIT_TEST */ From 1118b94f22920876215e1a9ad8df24229ec7cf0a Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 12:15:57 +0200 Subject: [PATCH 04/63] ODPM-382 --- src/socket.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/socket.h | 3 ++ 2 files changed, 107 insertions(+) diff --git a/src/socket.c b/src/socket.c index 2e7cbd3..eded330 100644 --- a/src/socket.c +++ b/src/socket.c @@ -159,6 +159,46 @@ memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) return 0; } +/* send information about ring specified by direction (S2M | M2S) and index */ +static_fn int +memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) +{ + memif_msg_queue_elt_t *e = + (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + + memif_msg_add_ring_t *ar = &e->msg.add_ring; + + e->msg.type = MEMIF_MSG_TYPE_ADD_RING; + + /* TODO: support multiple rings */ + memif_queue_t *mq; + if (dir == MEMIF_RING_M2S) + mq = c->rx_queues; + else + mq = c->tx_queues; + + e->fd = mq->int_fd; + ar->index = index; + ar->offset = mq->offset; + ar->log2_ring_size = mq->log2_ring_size; + ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0; + + e->next = NULL; + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return 0; + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + return 0; +} + static_fn int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) { @@ -285,6 +325,70 @@ memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) return 0; } +/* receive ring information and add new ring to connection queue + (based on direction S2M | M2S) */ +static_fn int +memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) +{ + memif_msg_add_ring_t *ar = &msg->add_ring; + + memif_queue_t *mq; + + if (fd < 0) + error_return ("missing ring interrupt fd"); + + if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) + { + if (ar->index > MEMIF_MAX_S2M_RING) + error_return ("maximum ring limit reached"); + + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + mq->int_fd = fd; + mq->log2_ring_size = ar->log2_ring_size; + mq->region = ar->region; + mq->offset = ar->offset; + + /* TODO: support multiple rings */ + c->rx_queues = mq; + c->args.num_s2m_rings++; + } + else + { + if (ar->index > MEMIF_MAX_M2S_RING) + error_return ("maximum ring limit reached"); + + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + mq->int_fd = fd; + mq->log2_ring_size = ar->log2_ring_size; + mq->region = ar->region; + mq->offset = ar->offset; + + /* TODO: support multiple rings */ + c->tx_queues = mq; + c->args.num_m2s_rings++; + } + + return 0; +} + +/* slave -> master */ +static_fn int +memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) +{ + memif_msg_connect_t *cm = &msg->connect; + + int err = 0; + err = memif_connect1 (c); + if (err < 0) + return err; + + strncpy ((char *) c->remote_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + + c->on_connect ((void *) c, c->private_ctx); + + return 0; +} + static_fn int memif_msg_receive (memif_connection_t *c) { diff --git a/src/socket.h b/src/socket.h index 575590a..132ff6d 100644 --- a/src/socket.h +++ b/src/socket.h @@ -56,12 +56,15 @@ void memif_msg_enq_init (memif_connection_t *c); int memif_msg_enq_add_region (memif_connection_t *c, uint8_t region); +int memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir); + int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg); int memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg); int memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd); +int memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd); #else #define static_fn static From 69e0eab8c990a2a83b6c100147d070ac5efbf244 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 12:18:02 +0200 Subject: [PATCH 05/63] ODPM-380 --- src/socket.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/socket.h | 10 +++++ 2 files changed, 122 insertions(+) diff --git a/src/socket.c b/src/socket.c index eded330..b7c8719 100644 --- a/src/socket.c +++ b/src/socket.c @@ -39,6 +39,7 @@ static_fn int memif_msg_send (int fd, memif_msg_t *msg, int afd) { + return 0; } @@ -199,6 +200,86 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) return 0; } +/* used as connection request from slave */ +static_fn void +memif_msg_enq_connect (memif_connection_t *c) +{ + memif_msg_queue_elt_t *e = + (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + memif_msg_connect_t *cm = &e->msg.connect; + + e->msg.type = MEMIF_MSG_TYPE_CONNECT; + e->fd = -1; + strncpy ((char *) cm->if_name, (char *) c->args.interface_name, + strlen ((char *) c->args.interface_name)); + + e->next = NULL; + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return; + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + return; +} + +/* used as confirmation of connection by master */ +static_fn void +memif_msg_enq_connected (memif_connection_t *c) +{ + memif_msg_queue_elt_t *e = + (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + memif_msg_connected_t *cm = &e->msg.connected; + + e->msg.type = MEMIF_MSG_TYPE_CONNECTED; + e->fd = -1; + strncpy ((char *) cm->if_name, (char *) c->args.interface_name, + strlen ((char *) c->args.interface_name)); + + e->next = NULL; + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return; + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + return; +} + +/* immediately send disconnect msg */ + /* specifie protocol for disconnect msg err_code + so that it will be compatible with VPP? (header/doc) */ +int +memif_msg_send_disconnect (memif_connection_t *c, uint8_t *err_string, uint32_t err_code) +{ + memif_msg_t msg = { 0 }; + memif_msg_disconnect_t *d = &msg.disconnect; + + msg.type = MEMIF_MSG_TYPE_DISCONNECT; + d->code = err_code; + uint16_t l = strlen ((char *) err_string); + if (l > 96) + { + DBG ("Disconnect string too long. Sending first 96 characters."); + l = 96; + } + strncpy ((char *) d->string, (char *) err_string, l); + + return memif_msg_send (c->fd, &msg, -1); +} + static_fn int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) { @@ -389,6 +470,37 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) return 0; } +/* master -> slave */ +static_fn int +memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) +{ + memif_msg_connect_t *cm = &msg->connect; + + int err = 0; + err = memif_connect1 (c); + if (err < 0) + return err; + + strncpy ((char *) c->remote_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + + c->on_connect ((void *) c, c->private_ctx); + + return 0; +} + +static_fn int +memif_msg_receive_disconnect (memif_connection_t *c, memif_msg_t *msg) +{ + memif_msg_disconnect_t *d = &msg->disconnect; + + strncpy ((char *) c->remote_disconnect_string, (char *) d->string, + strlen ((char *) d->string)); + + /* on returning error, handle function will call memif_disconnect () */ + error_return ("disconnect received: %s, mode: %d", + c->remote_disconnect_string, c->args.mode); +} + static_fn int memif_msg_receive (memif_connection_t *c) { diff --git a/src/socket.h b/src/socket.h index 132ff6d..1280505 100644 --- a/src/socket.h +++ b/src/socket.h @@ -66,6 +66,16 @@ int memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int f int memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd); +void memif_msg_enq_connect (memif_connection_t *c); + +void memif_msg_enq_connected (memif_connection_t *c); + +int memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg); + +int memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg); + +int memif_msg_receive_disconnect (memif_connection_t *c, memif_msg_t *msg); + #else #define static_fn static #endif /* MEMIF_UNIT_TEST */ From 4ad7af76ef88f5ddab2a6d25d34b4a88efb36e73 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 12:19:00 +0200 Subject: [PATCH 06/63] ODPM-395 --- src/socket.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/src/socket.c b/src/socket.c index b7c8719..bd54d60 100644 --- a/src/socket.c +++ b/src/socket.c @@ -39,8 +39,33 @@ static_fn int memif_msg_send (int fd, memif_msg_t *msg, int afd) { + struct msghdr mh = { 0 }; + struct iovec iov[1]; + char ctl[CMSG_SPACE (sizeof (int))]; + int rv; - return 0; + iov[0].iov_base = (void *) msg; + iov[0].iov_len = sizeof (memif_msg_t); + mh.msg_iov = iov; + mh.msg_iovlen = 1; + + if (afd > 0) + { + struct cmsghdr *cmsg; + memset (&ctl, 0, sizeof (ctl)); + mh.msg_control = ctl; + mh.msg_controllen = sizeof (ctl); + cmsg = CMSG_FIRSTHDR (&mh); + cmsg->cmsg_len = CMSG_LEN (sizeof (int)); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + memcpy (CMSG_DATA (cmsg), &afd, sizeof (int)); + } + rv = sendmsg (fd, &mh, 0); + if (rv < 0) + error_return ("sendmsg: %s fd %d", strerror (errno), fd); + DBG ("Message type %u sent", msg->type); + return rv; } /* response from memif master - master is ready to handle next message */ @@ -504,6 +529,117 @@ memif_msg_receive_disconnect (memif_connection_t *c, memif_msg_t *msg) static_fn int memif_msg_receive (memif_connection_t *c) { + char ctl[CMSG_SPACE (sizeof (int)) + + CMSG_SPACE (sizeof (struct ucred))] = { 0 }; + struct msghdr mh = { 0 }; + struct iovec iov[1]; + memif_msg_t msg = { 0 }; + ssize_t size; + int err = 0; + int fd = -1; + int i = 0; + + iov[0].iov_base = (void *) &msg; + iov[0].iov_len = sizeof (memif_msg_t); + mh.msg_iov = iov; + mh.msg_iovlen = 1; + mh.msg_control = ctl; + mh.msg_controllen = sizeof (ctl); + + size = recvmsg (c->fd, &mh, 0); + if (size != sizeof (memif_msg_t)) + { + if (size == 0) + error_return ("disconnected"); + else + error_return ("malformed message received on fd %d", c->fd); + } + + struct ucred *cr = 0; + struct cmsghdr *cmsg; + + cmsg = CMSG_FIRSTHDR (&mh); + while (cmsg) + { + if (cmsg->cmsg_level == SOL_SOCKET) + { + if (cmsg->cmsg_type == SCM_CREDENTIALS) + { + cr = (struct ucred *) CMSG_DATA (cmsg); + } + else if (cmsg->cmsg_type == SCM_RIGHTS) + { + int *fdp = (int *) CMSG_DATA (cmsg); + fd = *fdp; + } + } + cmsg = CMSG_NXTHDR (&mh, cmsg); + } + + DBG ("Message type %u received", msg.type); + + switch (msg.type) + { + case MEMIF_MSG_TYPE_ACK: + break; + + case MEMIF_MSG_TYPE_HELLO: + if ((err = memif_msg_receive_hello (c, &msg)) < 0) + return err; + if ((err = memif_init_regions_and_queues (c)) < 0) + return err; + memif_msg_enq_init (c); + memif_msg_enq_add_region (c, 0); + /* TODO: support multiple rings */ + memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M); + memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S); + memif_msg_enq_connect (c); + break; + + case MEMIF_MSG_TYPE_INIT: + if ((err = memif_msg_receive_init (c, &msg)) < 0) + return err; + /* c->remote_pid = cr->pid */ + /* c->remote_uid = cr->uid */ + /* c->remote_gid = cr->gid */ + memif_msg_enq_ack (c); + break; + + case MEMIF_MSG_TYPE_ADD_REGION: + if ((err = memif_msg_receive_add_region (c, &msg, fd)) < 0) + return err; + memif_msg_enq_ack (c); + break; + + case MEMIF_MSG_TYPE_ADD_RING: + if ((err = memif_msg_receive_add_ring (c, &msg, fd)) < 0) + return err; + memif_msg_enq_ack (c); + break; + + case MEMIF_MSG_TYPE_CONNECT: + if ((err = memif_msg_receive_connect (c, &msg)) < 0) + return err; + memif_msg_enq_connected (c); + break; + + case MEMIF_MSG_TYPE_CONNECTED: + if ((err = memif_msg_receive_connected (c, &msg)) < 0) + return err; + break; + + case MEMIF_MSG_TYPE_DISCONNECT: + if ((err = memif_msg_receive_disconnect (c, &msg)) < 0) + return err; + break; + + default: + error_return ("unknown message type (0x%x)", msg.type); + break; + } + + /* can send msg */ + c->flags |= MEMIF_CONNECTION_FLAG_WRITE; return 0; } From 4763a83dc72a825f472edd0c9e05f61b8b07124c Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 13:16:41 +0200 Subject: [PATCH 07/63] tests --- Makefile.am | 10 +- test/socket_test.c | 458 ++++++++++++++++++++++++++++++++++++++++++++- test/unit_test.c | 3 + test/unit_test.h | 6 + 4 files changed, 468 insertions(+), 9 deletions(-) diff --git a/Makefile.am b/Makefile.am index 4fef3b2..08d21be 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,7 +18,11 @@ ACLOCAL_AMFLAGS = -I m4 # # unit_test # -unit_test_SOURCES = test/socket_test.c test/unit_test.c src/socket.c +unit_test_SOURCES = test/socket_test.c \ + test/main_test.c \ + test/unit_test.c \ + src/socket.c \ + src/main.c # macro MEMIF_UNIT_TEST -> compile functions without static keyword # and declare them in header files, so they can be called from unit tests unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST @@ -27,14 +31,14 @@ unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST # main lib # libmemif_la_SOURCES = src/main.c src/socket.c -libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc +libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -ggdb # # ICMP responder example # icmp_responder_SOURCES = examples/icmp_responder/main.c icmp_responder_LDADD = libmemif.la -icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc +icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -ggdb noinst_PROGRAMS = icmp_responder diff --git a/test/socket_test.c b/test/socket_test.c index a471687..17d13cf 100644 --- a/test/socket_test.c +++ b/test/socket_test.c @@ -15,8 +15,26 @@ *------------------------------------------------------------------ */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include +#define TEST_SOCK_DIR "/libmemif/memif.sock" + static int get_queue_len (memif_msg_queue_elt_t *q) { @@ -36,6 +54,7 @@ test_msg_queue_add_pop () int rv = 0; memif_connection_t conn; conn.msg_queue = NULL; + conn.fd = -1; int i, len = 10; @@ -73,7 +92,9 @@ test_msg_queue_add_pop () rv = -1; } } - memif_slave_conn_fd_write_ready (&conn); + conn.flags |= MEMIF_CONNECTION_FLAG_WRITE; + /* function will return -1 because no socket is created */ + memif_conn_fd_write_ready (&conn); } if ((len - pop) != get_queue_len (conn.msg_queue)) @@ -168,6 +189,118 @@ test_msg_enq_init () return rv; } +static int +test_msg_enq_add_region () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + conn.regions = (memif_region_t *) malloc (sizeof(memif_region_t)); + memif_region_t *mr = conn.regions; + mr->fd = 5; + mr->region_size = 2048; + mr->next = NULL; + uint8_t region_index = 0; + + frv = memif_msg_enq_add_region (&conn, region_index); + if (frv < 0) + { + ERROR ("function error"); + rv = -1; + } + memif_msg_queue_elt_t *e = conn.msg_queue; + if (e->msg.type != MEMIF_MSG_TYPE_ADD_REGION) + { + ERROR ("incorrect msg type"); + rv = -1; + } + if (e->fd != mr->fd) + { + ERROR ("file descriptor mismatch"); + rv = -1; + } + memif_msg_add_region_t *ar = &e->msg.add_region; + if (ar->index != region_index) + { + ERROR ("region index mismatch"); + rv = -1; + } + if (ar->size != mr->region_size) + { + ERROR ("region size mismatch"); + rv = -1; + } + + region_index = 9; + frv = memif_msg_enq_add_region (&conn, region_index); + if (frv == 0) + { + ERROR ("invalid region_index (success fn)"); + rv = -1; + } + region_index = 0; + + free (conn.regions); + conn.regions = NULL; + mr = NULL; + frv = memif_msg_enq_add_region (&conn, region_index); + if (frv == 0) + { + ERROR ("invalid region (success fn)"); + rv = -1; + } + + return rv; +} + +static int +test_msg_receive_add_region () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + conn.regions = NULL; + memif_msg_t msg; + msg.type = MEMIF_MSG_TYPE_ADD_REGION; + msg.add_region.size = 2048; + msg.add_region.index = 0; + + int fd = 5; + + frv = memif_msg_receive_add_region (&conn, &msg, fd); + if (frv < 0) + { + ERROR ("fuinction error"); + rv = -1; + } + + msg.add_region.index = 9; + frv = memif_msg_receive_add_region (&conn, &msg, fd); + if (frv == 0) + { + ERROR ("invalid region index (success fn)"); + rv = -1; + } + + memif_region_t *mr = conn.regions; + if (mr->fd != fd) + { + ERROR ("incorrect file descriptor"); + rv = -1; + } + if (mr->region_size != 2048) + { + ERROR ("incorrect region size"); + rv = -1; + } + if (mr->shm != NULL) + { + ERROR ("invalid shm"); + rv = -1; + } + + return rv; +} + static int test_msg_receive_hello () { @@ -315,6 +448,234 @@ test_msg_receive_init () return rv; } +static int +test_msg_enq_add_ring () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + conn.rx_queues = NULL; + conn.tx_queues = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + + memif_queue_t *mq = conn.tx_queues; + uint8_t dir = MEMIF_RING_S2M; + mq->int_fd = 5; + mq->offset = 0; + mq->log2_ring_size = 10; + + frv = memif_msg_enq_add_ring (&conn, 0, dir); + if (frv < 0) + { + ERROR ("function error"); + rv = -1; + } + memif_msg_queue_elt_t *e = conn.msg_queue; + if (e->msg.type != MEMIF_MSG_TYPE_ADD_RING) + { + ERROR ("incorrect msg type"); + rv = -1; + } + if (e->fd != mq->int_fd) + { + ERROR ("incorrect int fd"); + rv = -1; + } + memif_msg_add_ring_t *ar = &e->msg.add_ring; + if (ar->index != 0) + { + ERROR ("incorrect queue index"); + rv = -1; + } + if (ar->offset != mq->offset) + { + ERROR ("ring offset mismatch"); + rv = -1; + } + if (ar->log2_ring_size != mq->log2_ring_size) + { + ERROR ("ring size mismatch"); + rv = -1; + } + if ((ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) == 0) + { + ERROR ("incorrect ring flag"); + rv = -1; + } + + dir = MEMIF_RING_M2S; + frv = memif_msg_enq_add_ring (&conn, 0, dir); + if (frv == 0) + { + ERROR ("uninitialized queue (success fn)"); + rv = -1; + } + dir = MEMIF_RING_S2M; + frv = memif_msg_enq_add_ring (&conn, 9, dir); + if (frv == 0) + { + ERROR ("invalid queue index (success fn)"); + rv = -1; + } + + return rv; +} + +static int +test_msg_enq_connect () +{ + int rv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + + memif_msg_enq_connect (&conn); + memif_msg_queue_elt_t *e = conn.msg_queue; + if (e->msg.type != MEMIF_MSG_TYPE_CONNECT) + { + ERROR ("incorrect msg type"); + rv = -1; + } + if (e->fd != -1) + { + ERROR ("invalid file descriptor"); + rv = -1; + } + if (strncmp ((char *) e->msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0) + { + ERROR ("incorrect interface name"); + rv = -1; + } + return rv; +} + +static int +test_msg_enq_connected () +{ + int rv = 0; + memif_connection_t conn; + conn.msg_queue = NULL; + strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + + memif_msg_enq_connected (&conn); + memif_msg_queue_elt_t *e = conn.msg_queue; + if (e->msg.type != MEMIF_MSG_TYPE_CONNECTED) + { + ERROR ("incorrect msg type"); + rv = -1; + } + if (e->fd != -1) + { + ERROR ("invalid file descriptor"); + rv = -1; + } + if (strncmp ((char *) e->msg.connected.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0) + { + ERROR ("incorrect interface name"); + rv = -1; + } + return rv; +} + +static int +test_msg_send_disconnect () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + conn.fd = -1; + + /* only possible fail if memif_msg_send fails... */ + /* obsolete without socket */ + frv = memif_msg_send_disconnect (&conn); + if (frv < 0) + { + /*ERROR ("function error");*/ + /*rv = -1;*/ + rv = 0; + } + return rv; +} + +static int +test_msg_receive_connect () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + conn.regions = NULL; + conn.tx_queues = NULL; + conn.rx_queues = NULL; + memif_msg_t msg; + + msg.type = MEMIF_MSG_TYPE_CONNECT; + strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + frv = memif_msg_receive_connect (&conn, &msg); + if (frv < 0) + { + /* fail only if memif_connect1 fails + (obsolete test) */ + ERROR ("function error"); + rv = -1; + } + if (strncmp ((char *) conn.remote_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0 ) + { + ERROR ("incorrect remote interface name"); + rv = -1; + } + return rv; +} + +static int +test_msg_receive_connected () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + conn.regions = NULL; + conn.tx_queues = NULL; + conn.rx_queues = NULL; + memif_msg_t msg; + + msg.type = MEMIF_MSG_TYPE_CONNECTED; + strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + + frv = memif_msg_receive_connected (&conn, &msg); + if (frv < 0) + { + /* fail only if memif_connect1 fails + (obsolete test) */ + ERROR ("function error"); + rv = -1; + } + if (strncmp ((char *) conn.remote_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0 ) + { + ERROR ("incorrect remote interface name"); + rv = -1; + } + return rv; +} + +static int +test_msg_receive_disconnect () +{ + int rv = 0, frv = 0; + memif_connection_t conn; + memif_msg_t msg; + msg.type = MEMIF_MSG_TYPE_DISCONNECT; + strncpy ((char *) msg.disconnect.string, "DC", 2); + + frv = memif_msg_receive_disconnect (&conn, &msg); + if (frv == 0) + { + /* function can only return -1 */ + ERROR ("not possible"); + rv = -1; + } + if (strncmp ((char *) conn.remote_disconnect_string, "DC", 2) != 0) + { + ERROR ("incorrect disconnect string"); + rv = -1; + } + return rv; +} + int test_socket (uint16_t *s, uint16_t *f) { @@ -330,7 +691,6 @@ test_socket (uint16_t *s, uint16_t *f) (*s)++; TEST_OK ("msg queue add/pop"); } - if (test_msg_enq_ack () < 0) { (*f)++; @@ -341,7 +701,6 @@ test_socket (uint16_t *s, uint16_t *f) (*s)++; TEST_OK ("msg enq ack"); } - if (test_msg_enq_init () < 0) { (*f)++; @@ -352,7 +711,26 @@ test_socket (uint16_t *s, uint16_t *f) (*s)++; TEST_OK ("msg enq init"); } - + if (test_msg_enq_add_region () < 0) + { + (*f)++; + TEST_FAIL ("msg enq add region"); + } + else + { + (*s)++; + TEST_OK ("msg enq add region"); + } + if (test_msg_enq_add_ring () < 0) + { + (*f)++; + TEST_FAIL ("msg enq add ring"); + } + else + { + (*s)++; + TEST_OK ("msg enq add ring"); + } if (test_msg_receive_hello () < 0) { (*f)++; @@ -363,7 +741,6 @@ test_socket (uint16_t *s, uint16_t *f) (*s)++; TEST_OK ("msg recv hello"); } - if (test_msg_receive_init () < 0) { (*f)++; @@ -374,6 +751,75 @@ test_socket (uint16_t *s, uint16_t *f) (*s)++; TEST_OK ("msg recv init"); } - + if (test_msg_receive_add_region () < 0) + { + (*f)++; + TEST_FAIL ("msg recv add region"); + } + else + { + (*s)++; + TEST_OK ("msg recv add region"); + } + if (test_msg_enq_connect () < 0) + { + (*f)++; + TEST_FAIL ("msg enq connect"); + } + else + { + (*s)++; + TEST_OK ("msg enq connect"); + } + if (test_msg_enq_connected () < 0) + { + (*f)++; + TEST_FAIL ("msg enq connected"); + } + else + { + (*s)++; + TEST_OK ("msg enq connected"); + } + if (test_msg_send_disconnect () < 0) + { + (*f)++; + TEST_FAIL ("msg send disconnect"); + } + else + { + (*s)++; + TEST_OK ("msg send disconnect"); + } + if (test_msg_receive_connect () < 0) + { + (*f)++; + TEST_FAIL ("msg recv connect"); + } + else + { + (*s)++; + TEST_OK ("msg recv connect"); + } + if (test_msg_receive_connected () < 0) + { + (*f)++; + TEST_FAIL ("msg recv connected"); + } + else + { + (*s)++; + TEST_OK ("msg recv connected"); + } + if (test_msg_receive_disconnect () < 0) + { + (*f)++; + TEST_FAIL ("msg recv disconnect"); + } + else + { + (*s)++; + TEST_OK ("msg recv disconnect"); + } return 0; } diff --git a/test/unit_test.c b/test/unit_test.c index e2e3f79..f53cb2f 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -17,11 +17,14 @@ #include + int main (void) { uint16_t s = 0, f = 0; +/* test_main (&s, &f);*/ + test_socket (&s, &f); INFO ("Success: %u Fail: %u", s, f); diff --git a/test/unit_test.h b/test/unit_test.h index 712ba97..36ab77d 100644 --- a/test/unit_test.h +++ b/test/unit_test.h @@ -53,6 +53,12 @@ printf ("\n"); \ } while (0) +#define UNIX_ERROR(...) do { \ + printf (RED"UNIX ERROR:"DEFAULT"%s:%d: ",__func__, __LINE__);\ + printf (__VA_ARGS__); \ + printf ("\n"); \ + } while (0) + #define TEST_APP_NAME "unit_test_app" #define TEST_IF_NAME "unit_test_if" #define TEST_SECRET "psst" From 51896b880b696a08bb31fed220e0a7dd8bb329e5 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 13:18:39 +0200 Subject: [PATCH 08/63] ODPM-411 + ODPM-412 --- src/libmemif.h | 103 +++++++- src/main.c | 571 +++++++++++++++++++++++++++++++++++++++++++- src/memif_private.h | 19 +- 3 files changed, 670 insertions(+), 23 deletions(-) diff --git a/src/libmemif.h b/src/libmemif.h index 513c704..3787b5e 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -18,12 +18,43 @@ #ifndef _LIBMEMIF_H_ #define _LIBMEMIF_H_ +#define LIBMEMIF_VERSION "1.0" + #include #include +/* 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) + typedef void* memif_conn_handle_t; +/** \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 +*/ +typedef int (memif_connection_update_t) (memif_conn_handle_t conn, void *private_ctx); + typedef struct { uint8_t *socket_filename; @@ -32,26 +63,76 @@ typedef struct uint8_t num_s2m_rings; uint8_t num_m2s_rings; uint16_t buffer_size; - uint16_t log2_ring_size; + memif_log2_ring_size_t log2_ring_size; uint8_t is_master; - uint32_t interface_id; + 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; -/* initialize memif connection. connect socket */ -int memif_connect (memif_conn_handle_t *conn, memif_conn_args_t * args); +/** \brief Memif initialization + @param on_control_fd_update - if control fd updates inform user to watch new fd + + 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 +*/ +int memif_init (memif_control_fd_update_t *on_control_fd_update); + +/** \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 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 +*/ +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, 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 + call accept on this fd and pass new fd to user + control + handle socket messaging (internal connection establishment) +*/ +int memif_control_fd_handler (int fd, uint8_t events); + +/** \brief Memif get queue event file descriptor + @param conn - memif connection handle + @param qid - number identifying queue -/* returns file descriptor for control channel. user can register fd for polling */ -/* on event user calls memif_control_handler () */ -int memif_get_control_fd (memif_conn_handle_t conn); + return interrupt fd for memif queue specified by qid +*/ +int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid); -/* handles control channel events (connect/disconnect) */ -int memif_control_fd_handler (memif_conn_handle_t conn, int fd); +/** \brief Memif delete + @param conn - memif connection handle -/* disconnect session (free memory, close file descriptors, unmap shared memory) */ -int memif_disconnect (memif_conn_handle_t conn); + disconnect session (free queues and regions, close file descriptors, unmap shared memory) + set connection handle to NULL, to avoid possible double free +*/ +int memif_delete (memif_conn_handle_t *conn); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index 5cb6b48..833ba87 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,4 @@ /* - *------------------------------------------------------------------ - * 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: @@ -15,7 +13,33 @@ *------------------------------------------------------------------ */ -#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 /* memif protocol msg, ring and descriptor definitions */ #include @@ -26,26 +50,559 @@ /* private structs and functions */ #include +/* + * WIP + */ +typedef struct +{ + int fd; + uint16_t use_count; + uint8_t *filename; +} memif_socket_t; + +/* + * WIP + */ +typedef struct +{ + memif_control_fd_update_t *control_fd_update; + int timerfd; + struct itimerspec arm, disarm; + + /* TODO: update to arrays support multiple connections */ + memif_socket_t ms; + memif_connection_t *conn; +} libmemif_main_t; + +libmemif_main_t libmemif_main; + +static void +print_conn (memif_connection_t *c) +{ + printf ("MEMIF CONNECTION:\n"); + printf ("\tconn %p\n", c); + printf ("\tfd %d\n", c->fd); + printf ("\tregion %p\n", c->regions); + printf ("\ttx %p\n", c->tx_queues); + printf ("\trx %p\n", c->rx_queues); +} + +int memif_init (memif_control_fd_update_t *on_control_fd_update) +{ + libmemif_main_t *lm = &libmemif_main; + lm->control_fd_update = on_control_fd_update; + memset (&lm->ms, 0, sizeof (memif_socket_t)); + lm->conn = NULL; + + lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); + if (lm->timerfd < 0) + { + DBG ("timerfd: %s", strerror (errno)); + return -1; + } + + lm->arm.it_value.tv_sec = 2; + lm->arm.it_value.tv_nsec = 0; + lm->arm.it_interval.tv_sec = 2; + lm->arm.it_interval.tv_nsec = 0; + memset (&lm->disarm, 0, sizeof (lm->disarm)); + + /* TODO: check return */ + lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ); + + return 0; +} + +static inline memif_ring_t * +memif_get_ring (memif_connection_t *conn, memif_ring_type_t type, uint16_t rn) +{ + if (conn->regions == NULL) + return NULL; + /* TODO: support multiple regions */ + void *p = conn->regions->shm; + int ring_size = + sizeof (memif_ring_t) + + sizeof (memif_desc_t) * (1 << conn->args.log2_ring_size); + p += (rn + type * conn->args.num_s2m_rings) * ring_size; + + return (memif_ring_t *) p; +} + int -memif_connect (memif_conn_handle_t *conn, memif_conn_args_t *args) +memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, + memif_connection_update_t *on_connect, + memif_connection_update_t *on_disconnect, + void *private_ctx) { + memif_connection_t *conn = (memif_connection_t *) *c; + if (conn != NULL) + { + DBG ("This handle already points to existing memif."); + return -1; + } + conn = (memif_connection_t *) malloc (sizeof (memif_connection_t)); + if (conn == NULL) + { + DBG_UNIX ("out of memory!"); + return -1; + } + libmemif_main_t *lm = &libmemif_main; + + int err; + int sockfd = -1; + conn->args.interface_id = args->interface_id; + /* lib or app? */ + if (args->log2_ring_size == 0) + args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE; + if (!args->buffer_size == 0) + args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE; + + conn->args.num_s2m_rings = args->num_s2m_rings; + conn->args.num_m2s_rings = args->num_m2s_rings; + conn->args.buffer_size = args->buffer_size; + conn->args.log2_ring_size = args->log2_ring_size; + conn->args.is_master = args->is_master; + conn->msg_queue = NULL; + conn->regions = NULL; + conn->tx_queues = NULL; + conn->rx_queues = NULL; + conn->fd = -1; + conn->on_connect = on_connect; + conn->on_disconnect = on_disconnect; + conn->private_ctx = private_ctx; + + uint8_t l = strlen ((char *) args->interface_name); + strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, l); + + l = strlen ((char *) args->instance_name); + strncpy ((char *) conn->args.instance_name, (char *) args->instance_name, l); + + if (args->socket_filename) + { + conn->args.socket_filename = (uint8_t *) malloc ( + strlen ((char *) args->socket_filename)); + if (conn->args.socket_filename == NULL) + { + DBG_UNIX ("out of memory!"); + err = errno; + goto error; + } + strncpy ((char *) conn->args.socket_filename, (char *) args->socket_filename, + strlen ((char *) args->socket_filename)); + } + else + { + uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR); + uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME); + conn->args.socket_filename = (uint8_t *) malloc (sdl + sfl + 1); + if (conn->args.socket_filename == NULL) + { + DBG_UNIX ("out of memory!"); + err = errno; + goto error; + } + strncpy ((char *) conn->args.socket_filename, + MEMIF_DEFAULT_SOCKET_DIR, sdl); + conn->args.socket_filename[sdl] = '/'; + strncpy ((char *) (conn->args.socket_filename + 1 +sdl), + MEMIF_DEFAULT_SOCKET_FILENAME, sfl); + } + + if (args->secret) + { + l = strlen ((char *) args->secret); + strncpy ((char *) conn->args.secret, (char *) args->secret, l); + } + + if (conn->args.is_master) + { + /* master */ + struct sockaddr_un un; + struct stat file_stat; + int on = 1; + + memif_socket_t ms = lm->ms; + + DBG ("memif master mode draft..."); + + if (lm->ms.fd == 0) + { + if (stat ((char *) conn->args.socket_filename, &file_stat) == 0) + { + if (S_ISSOCK (file_stat.st_mode)) + { + unlink ((char *) conn->args.socket_filename); + } + else + { + err = 0; + DBG ("file with specified socket filename exists but is not socket"); + goto error; + } + } + + sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); + if (sockfd < 0) + { + err = errno; + goto error; + } + un.sun_family = AF_UNIX; + strncpy ((char *) un.sun_path, (char *) conn->args.socket_filename, + sizeof (un.sun_path) - 1); + + if (setsockopt (sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) + { + err = errno; + goto error; + } + if (bind (sockfd, (struct sockaddr *) &un, sizeof (un)) == -1) + { + err = errno; + goto error; + } + if (listen (sockfd, 1) == -1) + { + err = errno; + goto error; + } + if (stat ((char *) conn->args.socket_filename, &file_stat) == -1) + { + err = errno; + goto error; + } + lm->ms.fd = sockfd; + lm->ms.use_count++; + lm->ms.filename = malloc (strlen ((char *) conn->args.socket_filename)); + strncpy ((char *) lm->ms.filename, (char *) conn->args.socket_filename, + strlen ((char *) conn->args.socket_filename)); + } + + DBG ("fd %d, uc %u, filename %s", lm->ms.fd, lm->ms.use_count, lm->ms.filename); + conn->fd = lm->ms.fd; + conn->read_fn = memif_conn_fd_accept_ready; + } + else + { + if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + { + DBG ("timerfd: %s", strerror (errno)); + goto error; + } + } + + *c = lm->conn = conn; + return 0; + +error: + if (sockfd > 0) + close (sockfd); + sockfd = -1; + if (conn->args.socket_filename) + free (conn->args.socket_filename); + free (conn); + *c = conn = NULL; + error_return_unix ("%s", strerror (err)); } +/* TODO: support multiple interfaces */ int -memif_get_control_fd (memif_conn_handle_t conn) +memif_control_fd_handler (int fd, uint8_t events) { + int i; + memif_connection_t *conn; + libmemif_main_t *lm = &libmemif_main; + if (fd == lm->timerfd) + { + uint64_t b; + ssize_t size; + size = read (fd, &b, sizeof (b)); + conn = lm->conn; + if (conn->fd < 0) + { + DBG ("try connect"); + struct sockaddr_un sun; + int sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); + if (sockfd < 0) + { + DBG ("%s", strerror (errno)); + } + + sun.sun_family = AF_UNIX; + strncpy (sun.sun_path, conn->args.socket_filename, + sizeof (sun.sun_path) -1); + + if (connect (sockfd, (struct sockaddr *) &sun, + sizeof (struct sockaddr_un)) == 0) + { + conn->fd = sockfd; + conn->read_fn = memif_conn_fd_read_ready; + conn->write_fn = memif_conn_fd_write_ready; + conn->error_fn = memif_conn_fd_error; + + lm->control_fd_update ( + sockfd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE); + + /* TODO: with multiple connections support, + only disarm if there is no disconnected slave */ + if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + { + DBG ("timerfd: %s", strerror (errno)); + } + } + else + { + DBG ("%s", strerror (errno)); + if (sockfd > 0) + close (sockfd); + sockfd = -1; + } + } + } + else + { + conn = lm->conn; + if (conn->fd == fd) + { + if (events & MEMIF_FD_EVENT_READ) + conn->read_fn (conn); + if (events & MEMIF_FD_EVENT_WRITE) + conn->write_fn (conn); + if (events & MEMIF_FD_EVENT_ERROR) + conn->error_fn (conn); + } + } return 0; } +/* TODO: support multiple regions */ +static void +memif_region_munmap_free (memif_region_t **mr) +{ + if (*mr == NULL) + return; + if (munmap ((*mr)->shm, (*mr)->region_size) < 0) + DBG ("munmap: %s", strerror (errno)); + if ((*mr)->fd > 0) + close ((*mr)->fd); + (*mr)->fd = -1; + free (*mr); + *mr = NULL; + return; +} + +static void +memif_msg_queue_free (memif_msg_queue_elt_t **e) +{ + if (*e == NULL) + return; + memif_msg_queue_free (&(*e)->next); + free (*e); + *e = NULL; + return; +} + +/* send disconnect msg and close interface */ +int +memif_disconnect_internal (memif_connection_t *c) +{ + if (c == NULL) + { + DBG ("no connection"); + return -1; + } + + libmemif_main_t *lm = &libmemif_main; + + memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); + + if (c->fd > 0) + close (c->fd); + c->fd = -1; + + /* TODO: support multiple rings */ + if (c->tx_queues != NULL) + { + free (c->tx_queues); + c->tx_queues = NULL; + } + if (c->rx_queues != NULL) + { + free (c->rx_queues); + c->rx_queues = NULL; + } + + memif_region_munmap_free (&c->regions); + + memif_msg_queue_free (&c->msg_queue); + + /* TODO: use timerfd_gettime to check if timer is armed + only arm if timer is disarmed */ + if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + { + DBG ("timerfd: %s", strerror (errno)); + } + + c->on_disconnect ((void *) c, c->private_ctx); + + return 0; +} + +int +memif_delete (memif_conn_handle_t *conn) +{ + memif_connection_t *c = (memif_connection_t *) *conn; + libmemif_main_t *lm = &libmemif_main; + + /* only fail if there is no connection to remove */ + if (memif_disconnect_internal (c) < 0) + return -1; + + /* TODO: only disarm if this is the only disconnected slave */ + if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + { + DBG ("timerfd: %s", strerror (errno)); + } + + if (c->args.socket_filename) + free (c->args.socket_filename); + c->args.socket_filename = NULL; + + free (c); + c = NULL; + + *conn = c; + return 0; +} + + int -memif_control_fd_handler (memif_conn_handle_t conn, int fd) +memif_connect1 (memif_connection_t *c) { + memif_region_t *mr = c->regions; + /* TODO: support multiple regions */ + if (mr != NULL) + { + if (!mr->shm) + { + if (mr->fd < 0) + error_return ("no memory region fd"); + + if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, + MAP_SHARED, mr->fd, 0)) == MAP_FAILED) + error_return_unix ("mmap"); + } + } + + /* TODO: support multiple queues */ + int i = 0; + memif_queue_t *mq = c->tx_queues; + if (mq != NULL) + { + mq->ring = c->regions->shm + mq->offset; + if (mq->ring->cookie != MEMIF_COOKIE) + error_return ("wrong cookie on tx ring %u", i); + i++; + } + i = 0; + mq = c->rx_queues; + if (mq != NULL) + { + mq->ring = c->regions->shm + mq->offset; + if (mq->ring->cookie != MEMIF_COOKIE) + error_return ("wrong cookie on tx ring %u", i); + i++; + } return 0; } int -memif_disconnect (memif_conn_handle_t conn) +memif_init_regions_and_queues (memif_connection_t *conn) { + memif_ring_t *ring = NULL; + uint64_t buffer_offset; + memif_region_t *r; + int i,j; + + conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t)); + r = conn->regions; + + buffer_offset = (conn->args.num_s2m_rings + conn->args.num_m2s_rings) * + (sizeof (memif_ring_t) + + sizeof (memif_desc_t) * (1 << conn->args.log2_ring_size)); + + r->region_size = buffer_offset + + conn->args.buffer_size * (1 << conn->args.log2_ring_size) * + (conn->args.num_s2m_rings + conn->args.num_m2s_rings); + + if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) + error_return_unix ("memfd_create: %s", strerror (errno)); + + if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) + error_return_unix ("fcntl: %s", strerror (errno)); + + if ((ftruncate (r->fd, r->region_size)) == -1) + error_return_unix ("ftruncate: %s", strerror (errno)); + + if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE, + MAP_SHARED, r->fd, 0)) == MAP_FAILED) + error_return_unix ("mmap: %s", strerror (errno)); + + for (i = 0; i < conn->args.num_s2m_rings; i++) + { + ring = memif_get_ring (conn, MEMIF_RING_S2M, i); + ring->head = ring->tail = 0; + ring->cookie = MEMIF_COOKIE; + for (j = 0; j < (1 << conn->args.log2_ring_size); j++) + { + uint16_t slot = i * (1 << conn->args.log2_ring_size) + j; + ring->desc[j].region = 0; + ring->desc[j].offset = buffer_offset + + (uint32_t) (slot * conn->args.buffer_size); + ring->desc[j].buffer_length = conn->args.buffer_size; + } + } + for (i = 0; i < conn->args.num_m2s_rings; i++) + { + ring = memif_get_ring (conn, MEMIF_RING_M2S, i); + ring->head = ring->tail = 0; + ring->cookie = MEMIF_COOKIE; + for (j = 0; j < (1 << conn->args.log2_ring_size); j++) + { + uint16_t slot = (i + conn->args.num_s2m_rings) * (1 << conn->args.log2_ring_size) + j; + ring->desc[j].region = 0; + ring->desc[j].offset = buffer_offset + + (uint32_t) (slot * conn->args.buffer_size); + ring->desc[j].buffer_length = conn->args.buffer_size; + } + } + memif_queue_t *mq; + int x; + for (x = 0; x < conn->args.num_s2m_rings; x++) + { + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) + error_return_unix ("eventfd: %s", strerror (errno)); + mq->ring = memif_get_ring (conn, MEMIF_RING_S2M, x); + mq->log2_ring_size = conn->args.log2_ring_size; + mq->region = 0; + mq->offset = (void *) mq->ring - (void *) conn->regions->shm; + conn->tx_queues = mq; + } + + for (x = 0; x < conn->args.num_m2s_rings; x++) + { + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) + error_return_unix ("eventfd: %s", strerror (errno)); + mq->ring = memif_get_ring (conn, MEMIF_RING_M2S, x); + mq->log2_ring_size = conn->args.log2_ring_size; + mq->region = 0; + mq->offset = (void *) mq->ring - (void *) conn->regions->shm; + mq->last_head = 0; + conn->rx_queues = mq; + } + return 0; } diff --git a/src/memif_private.h b/src/memif_private.h index 4b00109..bc4c6d2 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -47,16 +47,16 @@ #if MEMIF_DEBUG == 1 #define DBG(...) do { \ - printf("MEMIF_DEBUG: %s:%d: ", __func__, __LINE__); \ + printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \ printf(__VA_ARGS__); \ printf("\n"); \ } while (0) #define DBG_UNIX(...) do { \ - printf("MEMIF_DEBUG_UNIX: %s:%d: ", __func__, __LINE__); \ - printf(__VA_ARGS__); \ - printf("\n"); \ - } while (0) + printf("MEMIF_DEBUG_UNIX:%s:%s:%d: ", __FILE__, __func__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) #else #define DBG(...) @@ -116,16 +116,23 @@ typedef struct memif_connection memif_fn *write_fn, *read_fn, *error_fn; + memif_connection_update_t *on_connect, *on_disconnect; + void *private_ctx; + /* connection message queue */ memif_msg_queue_elt_t *msg_queue; uint8_t remote_if_name[32]; uint8_t remote_name[32]; + uint8_t remote_disconnect_string[96]; memif_region_t *regions; memif_queue_t *rx_queues; memif_queue_t *tx_queues; + + uint16_t flags; +#define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) } memif_connection_t; /* main.c */ @@ -136,6 +143,8 @@ int memif_connect1 (memif_connection_t *c); /* memory map region, initalize rings and queues */ int memif_init_regions_and_queues (memif_connection_t *c); +int memif_disconnect_internal (memif_connection_t *c); + #ifndef __NR_memfd_create #if defined __x86_64__ #define __NR_memfd_create 319 From d8e9b3432525b500d846344ebafd4f598f5f6a07 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 14:21:15 +0200 Subject: [PATCH 09/63] ODPM-417 (connection) --- examples/icmp_responder/main.c | 281 ++++++++++++++++++++++++++++++++- 1 file changed, 279 insertions(+), 2 deletions(-) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 133be88..e2fcf23 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -15,12 +15,289 @@ *------------------------------------------------------------------ */ +#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 -int main () +#define APP_NAME "ICMP_Responder" +#define IF_NAME "memif_connection" + +#define DBG(...) do { \ + printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ + printf (__VA_ARGS__); \ + printf ("\n"); \ + } while (0) + +int epfd; +memif_conn_handle_t conn; + +/* TODO: memif connection status report (memif_details...) */ + +int +add_epoll_fd (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 +del_epoll_fd (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; +} + +/* 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) { - printf ("foo is nowhere to be found..."); + DBG ("connected!"); + 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) +{ + DBG ("disconnected!"); + return 0; +} + +/* user needs to watch new fd or stop watching fd that is about to be closed */ +int +control_fd_update (int fd, uint8_t events) +{ + if (events & MEMIF_FD_EVENT_DEL) + return del_epoll_fd (fd); + + uint32_t evt = 0; + if (events & MEMIF_FD_EVENT_READ) + evt |= EPOLLIN; + if (events & MEMIF_FD_EVENT_WRITE) + evt |= EPOLLOUT; + + return add_epoll_fd (fd, evt); +} + +int +icmpr_memif_create (int is_master) +{ + /* setting memif connection arguments */ + memif_conn_args_t args; + int fd = -1; + memset (&args, 0, sizeof (args)); + args.is_master = is_master; + args.log2_ring_size = 10; + args.buffer_size = 2048; + args.num_s2m_rings = 1; + args.num_m2s_rings = 1; + 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 */ + + args.interface_id = 0; + /* last argument for memif_create (void * private_ctx) is used by user + to identify connection. this context is returned with callbacks */ + int rv = memif_create (&conn, &args, on_connect, on_disconnect, NULL); + if (rv < 0) + { + DBG ("memif create error!"); + } + else + { + DBG ("memif created!"); + } + return rv; +} + +int +icmpr_memif_delete () +{ + int rv = 0; + /* disconenct then delete memif connection */ + rv = memif_delete (&conn); + if (rv < 0) + { + DBG ("memif delete error!"); + } + else + { + DBG ("memif deleted!"); + } + return 0; +} + +void +print_help () +{ + printf ("LIBMEMIF EXAMPLE APP: %s\n", APP_NAME); + printf ("==============================\n"); + printf ("libmemif version: %s\n", LIBMEMIF_VERSION); + 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"); +} + +int +user_input_handler () +{ + char *ui = (char *) malloc (256); + char *r = fgets (ui, 256, stdin); + if (ui[0] == '\n') + return 0; + ui = strtok (ui, " "); + if (strncmp (ui, "exit", 4) == 0) + { + free (ui); + icmpr_memif_delete (); + exit (EXIT_SUCCESS); + } + else if (strncmp (ui, "help", 4) == 0) + { + print_help (); + return 0; + } + else if (strncmp (ui, "conn", 4) == 0) + { + icmpr_memif_create (0); + return 0; + } + else if (strncmp (ui, "del", 3) == 0) + { + icmpr_memif_delete (); + return 0; + } + else + DBG ("unknown command: %s", ui); + return 0; +} + +int +poll_event (int timeout) +{ + struct epoll_event evt, *e; + int app_err = 0, memif_err = 0, en = 0; + int tmp, nfd; + memset (&evt, 0, sizeof (evt)); + evt.events = EPOLLIN | EPOLLOUT; + en = epoll_pwait (epfd, &evt, 1, timeout, 0); + 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) + { + uint32_t events = 0; + 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); + } + 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 () +{ + epfd = epoll_create (1); + add_epoll_fd (0, EPOLLIN); + + /* initialize global memif connection handle */ + conn = NULL; + + /* initialize memory interface */ + memif_init (control_fd_update); + + /* main loop */ + while (1) + { + if (poll_event (0) < 0) + { + DBG ("poll_event error!"); + } + } +} From fdb6eec9b531e6eb5538a0c86299ad2184df45b8 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 10 Jul 2017 14:55:13 +0200 Subject: [PATCH 10/63] ODPM-412 (memif_delete_internal patch) --- src/main.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main.c b/src/main.c index 833ba87..ee3ed3e 100644 --- a/src/main.c +++ b/src/main.c @@ -63,6 +63,8 @@ typedef struct /* * WIP */ +/* probably function like memif_cleanup () will need to be called + close timerfd, free struct libmemif_main and its nested structures */ typedef struct { memif_control_fd_update_t *control_fd_update; @@ -376,22 +378,6 @@ memif_control_fd_handler (int fd, uint8_t events) return 0; } -/* TODO: support multiple regions */ -static void -memif_region_munmap_free (memif_region_t **mr) -{ - if (*mr == NULL) - return; - if (munmap ((*mr)->shm, (*mr)->region_size) < 0) - DBG ("munmap: %s", strerror (errno)); - if ((*mr)->fd > 0) - close ((*mr)->fd); - (*mr)->fd = -1; - free (*mr); - *mr = NULL; - return; -} - static void memif_msg_queue_free (memif_msg_queue_elt_t **e) { @@ -418,7 +404,10 @@ memif_disconnect_internal (memif_connection_t *c) memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); if (c->fd > 0) + { + lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL); close (c->fd); + } c->fd = -1; /* TODO: support multiple rings */ @@ -433,7 +422,17 @@ memif_disconnect_internal (memif_connection_t *c) c->rx_queues = NULL; } - memif_region_munmap_free (&c->regions); + /* TODO: support multiple regions */ + if (c->regions != NULL) + { + if (munmap (c->regions->shm, c->regions->region_size) < 0) + DBG ("munmap: %s", strerror (errno)); + if (c->regions->fd > 0) + close (c->regions->fd); + c->regions->fd = -1; + free (c->regions); + c->regions = NULL; + } memif_msg_queue_free (&c->msg_queue); From 72d3c58b0eac330a4250693ee8197dc6f197cd03 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:40:54 +0200 Subject: [PATCH 11/63] add ring msg bug-fix --- src/socket.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/socket.c b/src/socket.c index bd54d60..132314d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -206,6 +206,7 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) e->fd = mq->int_fd; ar->index = index; ar->offset = mq->offset; + ar->region = index; ar->log2_ring_size = mq->log2_ring_size; ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0; @@ -488,7 +489,7 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) if (err < 0) return err; - strncpy ((char *) c->remote_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); @@ -506,7 +507,7 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) if (err < 0) return err; - strncpy ((char *) c->remote_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); From b059fbcf6f381405522bd9a733314867da9db56b Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:42:56 +0200 Subject: [PATCH 12/63] ODPM-413 --- src/libmemif.h | 40 +++++++++ src/main.c | 192 ++++++++++++++++++++++++++++++++++++++++++++ src/memif_private.h | 20 +++-- 3 files changed, 246 insertions(+), 6 deletions(-) diff --git a/src/libmemif.h b/src/libmemif.h index 3787b5e..3d6b110 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -32,6 +32,8 @@ #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) typedef void* memif_conn_handle_t; @@ -72,6 +74,37 @@ typedef struct memif_interface_mode_t mode:8; } memif_conn_args_t; +typedef struct +{ + uint16_t desc_index; + uint32_t buffer_len; + uint32_t data_len; + void *data; +} memif_buffer_t; + +typedef struct +{ + uint8_t if_name[32]; + uint8_t inst_name[32]; + uint8_t remote_if_name[32]; + uint8_t remote_inst_name[32]; + + uint32_t id; + uint8_t secret[24]; /* optional */ + uint8_t role; /* 0 = master, 1 = slave */ + uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */ + uint8_t socket_filename[128]; + uint32_t ring_size; + uint16_t buffer_size; + uint8_t rx_queues; + uint8_t tx_queues; + + uint8_t link_up_down; /* 1 = up, 0 = down */ +} memif_details_t; + + +memif_details_t memif_get_details (memif_conn_handle_t conn); + /** \brief Memif initialization @param on_control_fd_update - if control fd updates inform user to watch new fd @@ -135,4 +168,11 @@ int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid); */ int memif_delete (memif_conn_handle_t *conn); +int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); + +int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); + + #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index ee3ed3e..a710e51 100644 --- a/src/main.c +++ b/src/main.c @@ -172,6 +172,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->on_connect = on_connect; conn->on_disconnect = on_disconnect; conn->private_ctx = private_ctx; + conn->alloc_buf_num = 0; uint8_t l = strlen ((char *) args->interface_name); strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, l); @@ -413,11 +414,23 @@ memif_disconnect_internal (memif_connection_t *c) /* TODO: support multiple rings */ if (c->tx_queues != NULL) { + if (c->tx_queues->int_fd > 0) + { + lm->control_fd_update (c->tx_queues->int_fd, MEMIF_FD_EVENT_DEL); + close (c->tx_queues->int_fd); + } + c->tx_queues->int_fd = -1; free (c->tx_queues); c->tx_queues = NULL; } if (c->rx_queues != NULL) { + if (c->rx_queues->int_fd > 0) + { + lm->control_fd_update (c->rx_queues->int_fd, MEMIF_FD_EVENT_DEL); + close (c->rx_queues->int_fd); + } + c->rx_queues->int_fd = -1; free (c->rx_queues); c->rx_queues = NULL; } @@ -479,6 +492,8 @@ memif_delete (memif_conn_handle_t *conn) int memif_connect1 (memif_connection_t *c) { + libmemif_main_t *lm = &libmemif_main; + memif_region_t *mr = c->regions; /* TODO: support multiple regions */ if (mr != NULL) @@ -513,6 +528,9 @@ memif_connect1 (memif_connection_t *c) error_return ("wrong cookie on tx ring %u", i); i++; } + + lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); + return 0; } @@ -605,3 +623,177 @@ memif_init_regions_and_queues (memif_connection_t *conn) return 0; } + +int +memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->tx_queues; + memif_ring_t *ring = mq->ring; + memif_buffer_t *b0, *b1; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint16_t i = 0, s0, s1, ns; + + if (ring->tail != ring->head) + { + if (ring->head > ring->tail) + ns = (1 << mq->log2_ring_size) - ring->head + ring->tail; + else + ns = ring->tail - ring->head; + } + else + ns = (1 << mq->log2_ring_size); + + /* if head == tail receive function will asume that no packets are available */ + ns -= 1; + + while (count && ns) + { + while ((count > 2) && (ns > 2)) + { + s0 = (ring->head + c->alloc_buf_num + i) & mask; + s1 = (ring->head + c->alloc_buf_num + i + 1) & mask; + + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + b0->desc_index = s0; + b1->desc_index = s1; + b0->buffer_len = ring->desc[s0].buffer_length; + b1->buffer_len = ring->desc[s1].buffer_length; + /* 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; + + (*(bufs + i)) = b0; + (*(bufs + i + 1)) = b1; + DBG ("allocated ring slots %u, %u", s0, s1); + count -= 2; + ns -= 2; + i += 2; + } + s0 = (ring->head + c->alloc_buf_num + i) & mask; + + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + b0->desc_index = s0; + b0->buffer_len = ring->desc[s0].buffer_length; + b0->data = c->regions->shm + ring->desc[s0].offset; + + (*(bufs + i)) = b0; + DBG ("allocated ring slot %u", s0); + count--; + ns--; + i++; + } + + c->alloc_buf_num += i; + + DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, c->alloc_buf_num); + + if (count) + DBG ("ring buffer full! qid: %u", qid); + + return i; +} + +int +memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->rx_queues; + memif_ring_t *ring = mq->ring; + uint16_t tail = ring->tail; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + int i = 0; + memif_buffer_t *b0, *b1; + + while (count) + { + while (count > 2) + { + b0 = (*(bufs + i)); + b1 = (*(bufs + i + 1)); + tail = (b0->desc_index + 1) & mask; + tail = (b1->desc_index + 1) & mask; + b0->data = NULL; + b1->data = NULL; + free (b0); + free (b1); + (*(bufs + i)) = b0 = NULL; + (*(bufs + i + 1)) = b1 = NULL; + + + count -= 2; + i += 2; + } + b0 = (*(bufs + i)); + tail = (b0->desc_index + 1) & mask; + b0->data = NULL; + free (b0); + (*(bufs + i)) = b0 = NULL; + + count--; + i++; + } + + ring->tail = tail; + + return i; +} + +int +memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid) +{ + memif_connection_t *c = (memif_connection_t *) conn; + return c->rx_queues->int_fd; +} + +memif_details_t +memif_get_details (memif_conn_handle_t conn) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_details_t md; + memset (&md, 0, sizeof (md)); + /* interface name */ + strncpy ((char *) md.if_name, (char *) c->args.interface_name, + strlen ((char *) c->args.interface_name)); + /* instance name */ + strncpy ((char *) md.inst_name, (char *) c->args.instance_name, + strlen ((char *) c->args.instance_name)); + /* remote interface name */ + strncpy ((char *) md.remote_if_name, (char *) c->remote_if_name, + strlen ((char *) c->remote_if_name)); + /* remote instance name */ + strncpy ((char *) md.remote_inst_name, (char *) c->remote_name, + strlen ((char *) c->remote_name)); + /* interface id */ + md.id = c->args.interface_id; + /* secret */ + if (c->args.secret) + strncpy ((char *) md.secret, (char *) c->args.secret, + strlen ((char *) c->args.secret)); + /* role */ + md.role = (c->args.is_master) ? 0 : 1; + /* mode */ + md.mode = c->args.mode; + /* socket filename */ + uint32_t l = strlen ((char *) c->args.socket_filename); + if (l > 128) + l = 128; + strncpy ((char *) md.socket_filename, (char *) c->args.socket_filename, l); + /* ring size */ + md.ring_size = (1 << c->args.log2_ring_size); + /* buffer size */ + md.buffer_size = c->args.buffer_size; + /* rx queues */ + md.rx_queues = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + /* tx queues */ + md.tx_queues = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + /* link up down */ + md.link_up_down = (c->fd > 0) ? 1 : 0; + + return md; +} diff --git a/src/memif_private.h b/src/memif_private.h index bc4c6d2..01c077d 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -43,9 +43,8 @@ #define MEMIF_MAX_FDS 512 -#define MEMIF_DEBUG 1 -#if MEMIF_DEBUG == 1 +#ifdef MEMIF_DBG #define DBG(...) do { \ printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \ printf(__VA_ARGS__); \ @@ -58,20 +57,27 @@ printf("\n"); \ } while (0) +#define error_return_unix(...) do { \ + DBG_UNIX(__VA_ARGS__); \ + return -1; \ + } while (0) +#define error_return(...) do { \ + DBG(__VA_ARGS__); \ + return -1; \ + } while (0) #else #define DBG(...) #define DBG_UNIX(...) -#endif /* MEMIF_DEBUG */ - #define error_return_unix(...) do { \ - DBG_UNIX(__VA_ARGS__); \ return -1; \ } while (0) #define error_return(...) do { \ - DBG(__VA_ARGS__); \ return -1; \ } while (0) +#endif /* MEMIF_DBG */ + + typedef struct { void *shm; @@ -131,6 +137,8 @@ typedef struct memif_connection memif_queue_t *rx_queues; memif_queue_t *tx_queues; + uint32_t alloc_buf_num; + uint16_t flags; #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) } memif_connection_t; From b5730c2cfdf80650348f5792bfa83f661548ef5f Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:44:53 +0200 Subject: [PATCH 13/63] ODPM-414 --- src/libmemif.h | 5 ++ src/main.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/src/libmemif.h b/src/libmemif.h index 3d6b110..4ce0470 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -174,5 +174,10 @@ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t **bufs, uint16_t count); +int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); + +int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index a710e51..c8b0d5d 100644 --- a/src/main.c +++ b/src/main.c @@ -78,6 +78,26 @@ typedef struct libmemif_main_t libmemif_main; +#define DBG_TX_BUF (0) +#define DBG_RX_BUF (1) + +static void +print_bytes (void *data, uint16_t len, uint8_t q) +{ + if (q == DBG_TX_BUF) + printf ("\nTX:\n\t"); + else + printf ("\nRX:\n\t"); + int i; + for (i = 0; i < len; i++) + { + if (i % 8 == 0) + printf ("\n%d:\t", i); + printf ("%02X ", ((uint8_t *) (data)) [i]); + } + printf ("\n\n"); +} + static void print_conn (memif_connection_t *c) { @@ -744,6 +764,152 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, return i; } +int +memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->tx_queues; + memif_ring_t *ring = mq->ring; + uint16_t head = ring->head; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint16_t tx = 0; + memif_buffer_t *b0, *b1; + + 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; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_TX_BUF); + print_bytes (b1->data , b1->data_len, DBG_TX_BUF); +#endif + + head = (b0->desc_index + 1) & mask; + head = (b1->desc_index + 1) & mask; + + b0->data = NULL; + b0->data_len = 0; + b1->data = NULL; + b1->data_len = 0; + + count -= 2; + tx += 2; + } + + b0 = (*(bufs + tx)); + ring->desc[b0->desc_index].length = b0->data_len; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_TX_BUF); +#endif + + head = (b0->desc_index + 1) & mask; + + b0->data = NULL; + b0->data_len = 0; + + count--; + tx++; + } + ring->head = head; + + c->alloc_buf_num -= tx; + + if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) + { + uint64_t a = 1; + int r = write (mq->int_fd, &a, sizeof (a)); + if (r < 0) + error_return ("write: %s fd %d", strerror (errno), mq->int_fd); + } + + return tx; +} + +int +memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->rx_queues; + memif_ring_t *ring = mq->ring; + uint16_t head = ring->head; + uint16_t ns, rx = 0; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + memif_buffer_t *b0, *b1; + + if (head == mq->last_head) + return 0; + + if (head > mq->last_head) + ns = head - mq->last_head; + else + ns = (1 << mq->log2_ring_size) - mq->last_head + head; + + while (ns && count) + { + while ((ns > 2) && (count > 2)) + { + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + 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; + + (*(bufs + rx)) = b0; + (*(bufs + rx + 1)) = b1; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_RX_BUF); + print_bytes (b1->data , b1->data_len, DBG_RX_BUF); +#endif + + mq->last_head = (mq->last_head + 2) & mask; + + ns -= 2; + count -= 2; + rx += 2; + } + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + 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; + + (*(bufs + rx)) = b0; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_RX_BUF); +#endif + + mq->last_head = (mq->last_head + 1) & mask; + + ns--; + count--; + rx++; + } + + if (ns) + { + DBG ("ring buffer full"); + } + + return rx; +} + int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid) { From d4948fb491113e894c1b64d54ae235360ca00137 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:46:25 +0200 Subject: [PATCH 14/63] ODPM-417 --- Makefile.am | 12 +- examples/icmp_responder/icmp_proto.c | 225 +++++++++++++++++++++++++ examples/icmp_responder/icmp_proto.h | 8 + examples/icmp_responder/main.c | 239 ++++++++++++++++++++++++--- 4 files changed, 462 insertions(+), 22 deletions(-) create mode 100644 examples/icmp_responder/icmp_proto.c create mode 100644 examples/icmp_responder/icmp_proto.h diff --git a/Makefile.am b/Makefile.am index 08d21be..b663fcf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,6 +15,12 @@ AUTOMAKE_OPTIONS = foreign subdir-objects ACLOCAL_AMFLAGS = -I m4 +AM_CPPFLAGS = -g -DMEMIF_DBG -DICMP_DBG + +.PHONY: release +release: + $(MAKE) AM_CPPFLAGS="-O3" + # # unit_test # @@ -31,14 +37,14 @@ unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST # main lib # libmemif_la_SOURCES = src/main.c src/socket.c -libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -ggdb +libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc # # ICMP responder example # -icmp_responder_SOURCES = examples/icmp_responder/main.c +icmp_responder_SOURCES = examples/icmp_responder/main.c examples/icmp_responder/icmp_proto.c icmp_responder_LDADD = libmemif.la -icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -ggdb +icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder noinst_PROGRAMS = icmp_responder diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c new file mode 100644 index 0000000..689dc5e --- /dev/null +++ b/examples/icmp_responder/icmp_proto.c @@ -0,0 +1,225 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static uint16_t +cksum (void *addr, ssize_t len) +{ + char* data = (char *)addr; + + uint32_t acc=0xffff; + + ssize_t i; + for (i = 0; (i + 1) < len; i += 2) + { + uint16_t word; + memcpy (&word, data + i, 2); + acc += ntohs (word); + if (acc > 0xffff) + acc -= 0xffff; + } + + if (len & 1) + { + uint16_t word = 0; + memcpy (&word, data + len - 1, 1); + acc += ntohs (word); + if (acc > 0xffff) + acc -= 0xffff; + } + return htons (~acc); +} + +int +print_packet (void * pck) +{ + if (pck == NULL) + { + printf ("ICMP_PROTO: no data\n"); + return -1; + } + struct iphdr *ip; + struct icmphdr *icmp; + ip = (struct iphdr *) pck; + icmp = (struct icmphdr *) (pck + sizeof (struct iphdr)); + printf ("received packet:\n"); + printf ("\tiphdr:\n"); + printf ("\t\tihl: %u\n\t\tversion: %u\n\t\tlen: %u\n\t\tid: %u\n", + ip->ihl, ip->version, __bswap_16(ip->tot_len), ip->id); + printf ("\t\tprotocol: %u\n", ip->protocol); + + printf ("\t\tsaddr: "); + int i; + for (i = 0; i < 4; i++) + { + printf ("%u.", ((uint8_t *) &ip->saddr)[i]); + } + printf ("\n"); + + printf ("\t\tdaddr: "); + for (i = 0; i < 4; i++) + { + printf ("%u.", ((uint8_t *) &ip->daddr)[i]); + } + printf ("\n"); + printf ("\ticmphdr:\n"); + printf ("\t\ttype: %s\n", (icmp->type == ICMP_ECHO) ? "ICMP_ECHO" : "ICMP_ECHOREPLY"); + + return 0; +} + +static ssize_t +resolve_arp (void *arp) +{ + struct arphdr *resp = (struct arphdr *) arp; + + resp->ar_hrd = __bswap_16 (ARPHRD_ETHER); + + resp->ar_pro = __bswap_16 (0x0800); + + resp->ar_hln = 6; + resp->ar_pln = 4; + + resp->ar_op = __bswap_16 (ARPOP_REPLY); + + return sizeof (struct arphdr); +} + +static ssize_t +resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp) +{ + struct ether_arp *resp = (struct ether_arp *) eth_arp_resp; + + resolve_arp (&resp->ea_hdr); + + memcpy (resp->arp_tha, eth_arp->arp_sha, 6); + memcpy (resp->arp_tpa, eth_arp->arp_spa, 4); + + memcpy (resp->arp_sha, (((struct ether_header *) ( + eth_arp_resp - sizeof (struct ether_header)))->ether_shost), 6); + + uint8_t ip_addr[4]; + ip_addr[0] = 192; + ip_addr[1] = 168; + ip_addr[2] = 1; + ip_addr[3] = 2; + memcpy (resp->arp_spa, ip_addr, 4); + + return sizeof (struct ether_arp); +} + +static ssize_t +resolve_eth (struct ether_header *eth, void *eth_resp) +{ + struct ether_header *resp = (struct ether_header *) eth_resp; + memcpy (resp->ether_dhost, eth->ether_shost, 6); + + uint8_t hw_addr[6]; + int i; + for (i = 0; i < 6; i++) + { + hw_addr[i] = 'a'; + } + memcpy (resp->ether_shost, hw_addr, 6); + + resp->ether_type = eth->ether_type; + + return sizeof (struct ether_header); +} + +static ssize_t +resolve_ip (struct iphdr *ip, void *ip_resp) +{ + struct iphdr *resp = (struct iphdr *) ip_resp; + resp->ihl = 5; + resp->version = 4; + resp->tos = 0; + /*len updated later */ + resp->tot_len = 0x5400; + resp->id = 0; + resp->frag_off = 0; + resp->ttl = 0x40; + resp->protocol = 1; + resp->saddr = 0xC0A80102; + resp->saddr = __bswap_32 (resp->saddr); + resp->daddr = 0xC0A80101; + resp->daddr = __bswap_32 (resp->daddr); + + resp->check = cksum (resp, sizeof (struct iphdr)); + + return sizeof (struct iphdr); +} + +static ssize_t +resolve_icmp (struct icmphdr *icmp, void *icmp_resp) +{ + struct icmphdr *resp = (struct icmphdr *) icmp_resp; + resp->type = ICMP_ECHOREPLY; + resp->code = 0; + resp->un.echo.id = icmp->un.echo.id; + resp->un.echo.sequence = icmp->un.echo.sequence; + + /*resp->checksum = cksum (resp, sizeof (struct icmphdr));*/ + + return sizeof (struct icmphdr); +} + +int +resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size) +{ + struct ether_header *eh; + struct ether_arp *eah; + struct iphdr *ip; + struct icmphdr *icmp; + *out_size = 0; + + eh = (struct ether_header *) in_pck; + *out_size = resolve_eth (eh, out_pck); + + if (eh->ether_type == 0x0608) + { + eah = (struct ether_arp *) (in_pck + *out_size); + *out_size += resolve_eth_arp (eah, out_pck + *out_size); + + } + else if (eh->ether_type == 0x0008) + { +#ifdef ICMP_DBG + print_packet (in_pck + *out_size); +#endif + ip = (struct iphdr *) (in_pck + *out_size); + *out_size += resolve_ip (ip, out_pck + *out_size); + if (ip->protocol == 1) + { + icmp = (struct icmphdr *) (in_pck + *out_size); + *out_size += resolve_icmp (icmp, out_pck + *out_size); + ((struct icmphdr *)(out_pck + *out_size - sizeof (struct icmphdr)))->checksum = + cksum (out_pck + *out_size - sizeof (struct icmphdr), sizeof (struct icmphdr)); + /* payload */ + memcpy (out_pck + *out_size, in_pck + *out_size, in_size - *out_size); + *out_size = in_size; + } + } + return 0; +} diff --git a/examples/icmp_responder/icmp_proto.h b/examples/icmp_responder/icmp_proto.h new file mode 100644 index 0000000..ec5ae70 --- /dev/null +++ b/examples/icmp_responder/icmp_proto.h @@ -0,0 +1,8 @@ +#ifndef _ICMP_PROTO_H_ +#define _ICMP_PROTO_H_ + +int resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size); + +int print_packet (void *pck); + +#endif /* _ICMP_PROTO_H_ */ diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index e2fcf23..f78d7b7 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -42,22 +42,113 @@ #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) int epfd; -memif_conn_handle_t conn; -/* TODO: memif connection status report (memif_details...) */ +/* + * WIP + */ +/* interrupt fd specific for queue */ +typedef struct +{ + uint16_t qid; + int fd; +} int_fd_t; + +typedef struct +{ + uint16_t index; + /* memif conenction handle */ + memif_conn_handle_t conn; + /* interrupt file descriptor (specific for each queue) */ + int_fd_t *int_fd; + /* tx buffers */ + memif_buffer_t **tx_bufs; + /* number of allocated tx buffers */ + uint16_t tx_buf_num; + /* rx buffers */ + memif_buffer_t **rx_bufs; + /* number of allocated rx buffers */ + uint16_t rx_buf_num; +} memif_connection_t; + +memif_connection_t memif_connection; + +static void +print_memif_details () +{ + memif_connection_t *c = &memif_connection; + printf ("MEMIF DETAILS\n"); + printf ("==============================\n"); + + /* TODO: loop for all connections */ + if (c->conn == NULL) + { + printf ("no connection!\n"); + return; + } + memif_details_t md = memif_get_details (c->conn); + 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 ("\tring_size: %u\n", md.ring_size); + printf ("\tbuffer_size: %u\n", md.buffer_size); + printf ("\trx queues: %u\n", md.rx_queues); + printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\tlink: "); + if (md.link_up_down) + printf ("up\n"); + else + printf ("down\n"); +} int add_epoll_fd (int fd, uint32_t events) @@ -80,6 +171,27 @@ add_epoll_fd (int fd, uint32_t events) return 0; } +int +mod_epoll_fd (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 fd) { @@ -104,8 +216,12 @@ del_epoll_fd (int fd) int on_connect (memif_conn_handle_t conn, void *private_ctx) { - DBG ("connected!"); - return 0; + INFO ("memif connected!"); + int_fd_t *ifd = (int_fd_t *) malloc (sizeof (int_fd_t)); + ifd->fd = memif_get_queue_efd ((&memif_connection)->conn, 0); + ifd->qid = 0; + (&memif_connection)->int_fd = ifd; + return add_epoll_fd (ifd->fd, EPOLLIN); } /* informs user about disconnected status. private_ctx is used by user to identify connection @@ -113,7 +229,7 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) int on_disconnect (memif_conn_handle_t conn, void *private_ctx) { - DBG ("disconnected!"); + INFO ("memif disconnected!"); return 0; } @@ -130,6 +246,9 @@ control_fd_update (int fd, uint8_t events) if (events & MEMIF_FD_EVENT_WRITE) evt |= EPOLLOUT; + if (events & MEMIF_FD_EVENT_MOD) + return mod_epoll_fd (fd, evt); + return add_epoll_fd (fd, evt); } @@ -154,7 +273,7 @@ icmpr_memif_create (int is_master) args.interface_id = 0; /* last argument for memif_create (void * private_ctx) is used by user to identify connection. this context is returned with callbacks */ - int rv = memif_create (&conn, &args, on_connect, on_disconnect, NULL); + int rv = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, NULL); if (rv < 0) { DBG ("memif create error!"); @@ -171,7 +290,7 @@ icmpr_memif_delete () { int rv = 0; /* disconenct then delete memif connection */ - rv = memif_delete (&conn); + rv = memif_delete (&(&memif_connection)->conn); if (rv < 0) { DBG ("memif delete error!"); @@ -186,15 +305,46 @@ icmpr_memif_delete () void print_help () { - printf ("LIBMEMIF EXAMPLE APP: %s\n", APP_NAME); + printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME); +#ifdef ICMP_DBG + printf (" (debug)"); +#endif + printf ("\n"); printf ("==============================\n"); - printf ("libmemif version: %s\n", LIBMEMIF_VERSION); + 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 - print memif details\n"); +} + +int +icmpr_buffer_alloc (long n) +{ + memif_connection_t *c = &memif_connection; + c->tx_bufs = (memif_buffer_t **) malloc (sizeof (memif_buffer_t *) * n); + DBG ("call memif_buffer_alloc"); + int r = memif_buffer_alloc (c->conn, 0, c->tx_bufs, n); + DBG ("allocated %d/%ld buffers", r, n); + c->tx_buf_num += r; + return 0; +} + +int +icmpr_tx_burst () +{ + memif_connection_t *c = &memif_connection; + int r = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num); + DBG ("tx: %d/%u", r, c->tx_buf_num); + c->tx_buf_num -= r; + return 0; } int @@ -226,19 +376,58 @@ user_input_handler () icmpr_memif_delete (); return 0; } + else if (strncmp (ui, "show", 4) == 0) + { + print_memif_details (); + } else DBG ("unknown command: %s", ui); return 0; } +int +icmpr_interrupt (int fd) +{ + memif_connection_t *c = &memif_connection; + DBG ("interrupted!"); + uint64_t b; + ssize_t r = read (fd, &b, sizeof (b)); + + int rx = memif_rx_burst (c->conn, 0, c->rx_bufs, c->rx_buf_num); + c->rx_buf_num -= rx; + + DBG ("received %d buffers. %u free buffers", rx, c->rx_buf_num); + + icmpr_buffer_alloc (rx); + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (*(c->rx_bufs + i))->data, + (*(c->rx_bufs + i))->data_len, (void *) (*(c->tx_bufs + i))->data, + &(*(c->tx_bufs + i))->data_len); + } + + int fb = memif_buffer_free (c->conn, 0, c->rx_bufs, rx); + c->rx_buf_num += fb; + + DBG ("freed %d buffers. %u free buffers", fb, c->rx_buf_num); + + icmpr_tx_burst (); + + return 0; +} + int poll_event (int timeout) { + memif_connection_t *c = &memif_connection; 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; en = epoll_pwait (epfd, &evt, 1, timeout, 0); if (en < 0) { @@ -250,14 +439,20 @@ poll_event (int timeout) /* this app does not use any other file descriptors than stds and memif control fds */ if ( evt.data.fd > 2) { - uint32_t events = 0; - 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 (evt.data.fd == c->int_fd->fd) + { + icmpr_interrupt (evt.data.fd); + } + else + { + 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); + } } else if (evt.data.fd == 0) { @@ -286,8 +481,14 @@ int main () epfd = epoll_create (1); add_epoll_fd (0, EPOLLIN); + memif_connection_t *c = &memif_connection; + /* initialize global memif connection handle */ - conn = NULL; + c->conn = NULL; + c->int_fd = malloc (sizeof (int_fd_t)); + c->int_fd->fd = -1; + c->rx_buf_num = 256; + c->rx_bufs = malloc (sizeof (memif_buffer_t *) * c->rx_buf_num); /* initialize memory interface */ memif_init (control_fd_update); @@ -295,7 +496,7 @@ int main () /* main loop */ while (1) { - if (poll_event (0) < 0) + if (poll_event (-1) < 0) { DBG ("poll_event error!"); } From 78fcdfb32d68f4388793fed9ccc26c9c31212318 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 12:55:31 +0200 Subject: [PATCH 15/63] shm buffer alloc fix --- src/main.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index c8b0d5d..66f6264 100644 --- a/src/main.c +++ b/src/main.c @@ -192,7 +192,6 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->on_connect = on_connect; conn->on_disconnect = on_disconnect; conn->private_ctx = private_ctx; - conn->alloc_buf_num = 0; uint8_t l = strlen ((char *) args->interface_name); strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, l); @@ -625,6 +624,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) mq->log2_ring_size = conn->args.log2_ring_size; mq->region = 0; mq->offset = (void *) mq->ring - (void *) conn->regions->shm; + mq->alloc_bufs = 0; conn->tx_queues = mq; } @@ -638,6 +638,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) mq->region = 0; mq->offset = (void *) mq->ring - (void *) conn->regions->shm; mq->last_head = 0; + mq->alloc_bufs = 0; conn->rx_queues = mq; } @@ -672,8 +673,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, { while ((count > 2) && (ns > 2)) { - s0 = (ring->head + c->alloc_buf_num + i) & mask; - s1 = (ring->head + c->alloc_buf_num + i + 1) & mask; + s0 = (ring->head + mq->alloc_bufs + i) & mask; + s1 = (ring->head + mq->alloc_bufs + i + 1) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); @@ -693,7 +694,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, ns -= 2; i += 2; } - s0 = (ring->head + c->alloc_buf_num + i) & mask; + s0 = (ring->head + mq->alloc_bufs + i) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); @@ -708,9 +709,9 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, i++; } - c->alloc_buf_num += i; + mq->alloc_bufs += i; - DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, c->alloc_buf_num); + DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, mq->alloc_bufs); if (count) DBG ("ring buffer full! qid: %u", qid); @@ -819,8 +820,8 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, } ring->head = head; - c->alloc_buf_num -= tx; - + mq->alloc_bufs -= tx; + if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) { uint64_t a = 1; From 4d7bed51721ab020c0179800fd5c79644d261dfd Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 17 Jul 2017 15:53:21 +0200 Subject: [PATCH 16/63] error code->msg --- src/libmemif.h | 98 +++++++++- src/main.c | 465 ++++++++++++++++++++++++++++++-------------- src/memif_private.h | 64 +++++- src/socket.c | 230 +++++++++++++--------- src/socket.h | 10 +- 5 files changed, 621 insertions(+), 246 deletions(-) diff --git a/src/libmemif.h b/src/libmemif.h index 4ce0470..363a4b3 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -24,6 +24,48 @@ #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_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_INT_WRITE, /* send interrupt error */ + MEMIF_ERR_MFMSG, /* malformed msg received */ +/* 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; + /* 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) @@ -102,12 +144,20 @@ typedef struct uint8_t link_up_down; /* 1 = up, 0 = down */ } memif_details_t; +char *memif_strerror (int err_code); memif_details_t memif_get_details (memif_conn_handle_t conn); /** \brief Memif initialization @param on_control_fd_update - if control fd updates inform user to watch new fd + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_FILE_LIMIT - system open file limit reached + MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached + MEMIF_ERR_BAD_FD - invalid fd + 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 @@ -123,6 +173,20 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update); creates memory interface. + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_ACCES - permission denied + MEMIF_ERR_FILE_LIMIT - system open file limit reached + MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached + MEMIF_ERR_AGAIN - fd is not socket, or operation would block + MEMIF_ERR_BAD_FD - invalid fd + MEMIF_ERR_NOMEM - out of memory + TODO: MEMIF_ERR_INVAL_ARG - invalid argument + MEMIF_ERR_CONN - connection already exists on this handle + MEMIF_ERR_FILE_NOT_SOCK - file specified by socket filename exists, + but it's not a socket + 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. @@ -142,6 +206,18 @@ int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args, memif_co if event occures on any control fd, call memif_control_fd_handler internal - lib will "identify" fd (timerfd, lsitener, control) and handle event accordingly + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_ACCES - permission denied + MEMIF_ERR_FILE_LIMIT - system open file limit reached + MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached + MEMIF_ERR_AGAIN - fd is not socket, or operation would block + MEMIF_ERR_BAD_FD - invalid fd + MEMIF_ERR_NOMEM - out of memory + MEMIF_ERR_NO_SHMFD - missing shared memory fd + MEMIF_ERR_COOKIE - invalid cookie on ring + fd type timerfd every disconnected memif in slave mode will request connection @@ -155,29 +231,39 @@ int memif_control_fd_handler (int fd, uint8_t events); /** \brief Memif get queue event file descriptor @param conn - memif connection handle @param qid - number identifying queue + @param efd - return interrupt fd for memif queue specified by qid - return interrupt fd for memif queue specified by qid + rerurn + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL */ -int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid); +int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd); /** \brief Memif delete @param conn - memif connection handle + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_BAD_FD - invalid fd + disconnect session (free queues and regions, close file descriptors, unmap shared memory) set connection handle to NULL, to avoid possible double free */ int memif_delete (memif_conn_handle_t *conn); + int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *tx); int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *rx); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index 66f6264..ce4d20c 100644 --- a/src/main.c +++ b/src/main.c @@ -50,37 +50,103 @@ /* private structs and functions */ #include -/* - * WIP - */ -typedef struct -{ - int fd; - uint16_t use_count; - uint8_t *filename; -} memif_socket_t; - -/* - * WIP - */ -/* probably function like memif_cleanup () will need to be called - close timerfd, free struct libmemif_main and its nested structures */ -typedef struct -{ - memif_control_fd_update_t *control_fd_update; - int timerfd; - struct itimerspec arm, disarm; - - /* TODO: update to arrays support multiple connections */ - memif_socket_t ms; - memif_connection_t *conn; -} libmemif_main_t; +#define ERRLIST_LEN 33 +#define MAX_ERRBUF_LEN 256 libmemif_main_t libmemif_main; +static char memif_buf[MAX_ERRBUF_LEN]; + +const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ + "Success.", + /* MEMIF_ERR_SYSCALL */ + "Unspecified syscall error (build with -DMEMIF_DBG or make debug).", + /* MEMIF_ERR_ACCES */ + "Permission to resoure denied.", + /* MEMIF_ERR_FILE_LIMIT */ + "System limit on total numer of open files reached.", + /* MEMIF_ERR_PROC_FILE_LIMIT */ + "Per-process limit on total number of open files reached.", + /* MEMIF_ERR_ALREADY */ + "Connection already requested.", + /* MEMIF_ERR_AGAIN */ + "File descriptor refers to file other than socket, or operation would block.", + /* MEMIF_ERR_BAD_FD */ + "Bad file descriptor.", + /* MEMIF_ERR_NOMEM */ + "Out of memory.", + /* MEMIF_ERR_INVAL_ARG */ + "Invalid argument.", + /* MEMIF_ERR_NOCONN */ + "Memif connection handle does not point to existing conenction", + /* MEMIF_ERR_CONN */ + "Memif connection handle points to existing connection", + /* MEMIF_ERR_CB_FDUPDATE */ + "User defined callback memif_control_fd_update_t returned error", + /* MEMIF_ERR_FILE_NOT_SOCK */ + "File specified by socket filename exists and is not socket.", + /* MEMIF_ERR_NO_SHMFD */ + "Missing shared memory file descriptor. (internal error)", + /* MEMIF_ERR_COOKIE */ + "Invalid cookie on ring. (internal error)", + /* MEMIF_ERR_NOBUF_RING */ + "Ring buffer full.", + /* MEMIF_ERR_NOBUF */ + "Not enough memif buffers. There are unreceived data in shared memory.", + /* MEMIF_ERR_INT_WRITE */ + "Send interrupt error.", + /* MEMIF_ERR_MFMSG */ + "Malformed message received on control channel.", + /* MEMIF_ERR_PROTO */ + "Incompatible memory interface protocol version.", + /* MEMIF_ERR_ID */ + "Unmatched interface id.", + /* MEMIF_ERR_ACCSLAVE */ + "Slave cannot accept connection reqest.", + /* MEMIF_ERR_ALRCONN */ + "Interface is already connected.", + /* MEMIF_ERR_MODE */ + "Mode mismatch.", + /* MEMIF_ERR_SECRET */ + "Secret mismatch.", + /* MEMIF_ERR_NOSECRET */ + "Secret required.", + /* MEMIF_ERR_MAXREG */ + "Limit on total number of regions reached.", + /* MEMIF_ERR_MAXRING */ + "Limit on total number of ring reached.", + /* MEMIF_ERR_NO_INTFD */ + "Missing interrupt file descriptor. (internal error)", + /* MEMIF_ERR_DISCONNECT */ + "Interface received disconnect request.", + /* MEMIF_ERR_DISCONNECTED */ + "Interface is disconnected.", + /* MEMIF_ERR_UNKNOWN_MSG */ + "Unknown message type received on control channel. (internal error)" + }; + +#define MEMIF_ERR_UNDEFINED "undefined error" + +char * +memif_strerror (int err_code) +{ + if (err_code > ERRLIST_LEN) + { + strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED)); + memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0'; + } + else + { + strncpy (memif_buf, memif_errlist[err_code], strlen (memif_errlist[err_code])); + memif_buf[strlen (memif_errlist[err_code])] = '\0'; + } + return memif_buf; +} + #define DBG_TX_BUF (0) #define DBG_RX_BUF (1) +#if MEMIF_DBG_SHM static void print_bytes (void *data, uint16_t len, uint8_t q) { @@ -97,20 +163,41 @@ print_bytes (void *data, uint16_t len, uint8_t q) } printf ("\n\n"); } +#endif /* MEMIF_DBG */ -static void -print_conn (memif_connection_t *c) +int +memif_syscall_error_handler (int err_code) { - printf ("MEMIF CONNECTION:\n"); - printf ("\tconn %p\n", c); - printf ("\tfd %d\n", c->fd); - printf ("\tregion %p\n", c->regions); - printf ("\ttx %p\n", c->tx_queues); - printf ("\trx %p\n", c->rx_queues); + DBG_UNIX ("%s", strerror (err_code)); + + if (err_code == 0) + return MEMIF_ERR_SUCCESS; + if (err_code == EACCES) + return MEMIF_ERR_ACCES; + if (err_code == ENFILE) + return MEMIF_ERR_FILE_LIMIT; + if (err_code == EMFILE) + return MEMIF_ERR_PROC_FILE_LIMIT; + if (err_code == ENOMEM) + return MEMIF_ERR_NOMEM; +/* connection refused if master dows not exist + this error would spam the user until master was created */ + if (err_code == ECONNREFUSED) + return MEMIF_ERR_SUCCESS; + if (err_code == EALREADY) + return MEMIF_ERR_ALREADY; + if (err_code == EAGAIN) + return MEMIF_ERR_AGAIN; + if (err_code == EBADF) + return MEMIF_ERR_BAD_FD; + + /* other syscall errors */ + return MEMIF_ERR_SYSCALL; } int memif_init (memif_control_fd_update_t *on_control_fd_update) { + int err = MEMIF_ERR_SUCCESS; /* 0 */ libmemif_main_t *lm = &libmemif_main; lm->control_fd_update = on_control_fd_update; memset (&lm->ms, 0, sizeof (memif_socket_t)); @@ -119,8 +206,9 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); if (lm->timerfd < 0) { - DBG ("timerfd: %s", strerror (errno)); - return -1; + err = errno; + DBG ("timerfd: %s", strerror (err)); + return memif_syscall_error_handler (err); } lm->arm.it_value.tv_sec = 2; @@ -129,8 +217,12 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) lm->arm.it_interval.tv_nsec = 0; memset (&lm->disarm, 0, sizeof (lm->disarm)); - /* TODO: check return */ - lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ); + /* check return or not? */ + if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0) + { + DBG ("user defined callback type memif_control_fd_update_t error!"); + return MEMIF_ERR_CB_FDUPDATE; + } return 0; } @@ -156,22 +248,23 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, memif_connection_update_t *on_disconnect, void *private_ctx) { + int err; + int sockfd = -1; memif_connection_t *conn = (memif_connection_t *) *c; if (conn != NULL) { DBG ("This handle already points to existing memif."); - return -1; + return MEMIF_ERR_CONN; } conn = (memif_connection_t *) malloc (sizeof (memif_connection_t)); if (conn == NULL) { - DBG_UNIX ("out of memory!"); - return -1; + err = memif_syscall_error_handler (errno); + goto error; } + libmemif_main_t *lm = &libmemif_main; - int err; - int sockfd = -1; conn->args.interface_id = args->interface_id; /* lib or app? */ if (args->log2_ring_size == 0) @@ -205,8 +298,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, strlen ((char *) args->socket_filename)); if (conn->args.socket_filename == NULL) { - DBG_UNIX ("out of memory!"); - err = errno; + err = memif_syscall_error_handler (errno); goto error; } strncpy ((char *) conn->args.socket_filename, (char *) args->socket_filename, @@ -219,8 +311,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->args.socket_filename = (uint8_t *) malloc (sdl + sfl + 1); if (conn->args.socket_filename == NULL) { - DBG_UNIX ("out of memory!"); - err = errno; + err = memif_syscall_error_handler (errno); goto error; } strncpy ((char *) conn->args.socket_filename, @@ -257,7 +348,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, } else { - err = 0; + err = MEMIF_ERR_FILE_NOT_SOCK; DBG ("file with specified socket filename exists but is not socket"); goto error; } @@ -266,7 +357,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); if (sockfd < 0) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } un.sun_family = AF_UNIX; @@ -275,27 +366,32 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, if (setsockopt (sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } if (bind (sockfd, (struct sockaddr *) &un, sizeof (un)) == -1) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } if (listen (sockfd, 1) == -1) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } if (stat ((char *) conn->args.socket_filename, &file_stat) == -1) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } lm->ms.fd = sockfd; lm->ms.use_count++; lm->ms.filename = malloc (strlen ((char *) conn->args.socket_filename)); + if (lm->ms.filename == NULL) + { + err = memif_syscall_error_handler (errno); + goto error; + } strncpy ((char *) lm->ms.filename, (char *) conn->args.socket_filename, strlen ((char *) conn->args.socket_filename)); } @@ -308,7 +404,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, { if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); + err = memif_syscall_error_handler (errno); goto error; } } @@ -323,16 +419,17 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, sockfd = -1; if (conn->args.socket_filename) free (conn->args.socket_filename); - free (conn); + if (conn != NULL) + free (conn); *c = conn = NULL; - error_return_unix ("%s", strerror (err)); + return err; } /* TODO: support multiple interfaces */ int memif_control_fd_handler (int fd, uint8_t events) { - int i; + int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */ memif_connection_t *conn; libmemif_main_t *lm = &libmemif_main; if (fd == lm->timerfd) @@ -345,10 +442,11 @@ memif_control_fd_handler (int fd, uint8_t events) { DBG ("try connect"); struct sockaddr_un sun; - int sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); + sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); if (sockfd < 0) { - DBG ("%s", strerror (errno)); + err = memif_syscall_error_handler (errno); + goto error; } sun.sun_family = AF_UNIX; @@ -370,32 +468,50 @@ memif_control_fd_handler (int fd, uint8_t events) only disarm if there is no disconnected slave */ if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); - } + err = memif_syscall_error_handler (errno); + goto error; + } } else { - DBG ("%s", strerror (errno)); - if (sockfd > 0) - close (sockfd); - sockfd = -1; + err = memif_syscall_error_handler (errno); + goto error; } } } else { - conn = lm->conn; - if (conn->fd == fd) + conn = lm->conn; + if (conn->fd == fd) + { + if (events & MEMIF_FD_EVENT_READ) + { + err = conn->read_fn (conn); + if (err != MEMIF_ERR_SUCCESS) + return err; + } + if (events & MEMIF_FD_EVENT_WRITE) + { + err = conn->write_fn (conn); + if (err != MEMIF_ERR_SUCCESS) + return err; + } + if (events & MEMIF_FD_EVENT_ERROR) { - if (events & MEMIF_FD_EVENT_READ) - conn->read_fn (conn); - if (events & MEMIF_FD_EVENT_WRITE) - conn->write_fn (conn); - if (events & MEMIF_FD_EVENT_ERROR) - conn->error_fn (conn); + err = conn->error_fn (conn); + if (err != MEMIF_ERR_SUCCESS) + return err; } + } } - return 0; + + return MEMIF_ERR_SUCCESS; /* 0 */ + +error: + if (sockfd > 0) + close (sockfd); + sockfd = -1; + return err; } static void @@ -416,15 +532,16 @@ memif_disconnect_internal (memif_connection_t *c) if (c == NULL) { DBG ("no connection"); - return -1; + return MEMIF_ERR_NOCONN; } - libmemif_main_t *lm = &libmemif_main; + int err = MEMIF_ERR_SUCCESS; /* 0 */ - memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); + libmemif_main_t *lm = &libmemif_main; if (c->fd > 0) { + memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL); close (c->fd); } @@ -458,7 +575,7 @@ memif_disconnect_internal (memif_connection_t *c) if (c->regions != NULL) { if (munmap (c->regions->shm, c->regions->region_size) < 0) - DBG ("munmap: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if (c->regions->fd > 0) close (c->regions->fd); c->regions->fd = -1; @@ -472,12 +589,13 @@ memif_disconnect_internal (memif_connection_t *c) only arm if timer is disarmed */ if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); + err = memif_syscall_error_handler (errno); + DBG_UNIX ("timerfd_settime: arm"); } c->on_disconnect ((void *) c, c->private_ctx); - return 0; + return err; } int @@ -485,15 +603,18 @@ memif_delete (memif_conn_handle_t *conn) { memif_connection_t *c = (memif_connection_t *) *conn; libmemif_main_t *lm = &libmemif_main; + + int err; - /* only fail if there is no connection to remove */ - if (memif_disconnect_internal (c) < 0) - return -1; + err = memif_disconnect_internal (c); + if (err == MEMIF_ERR_NOCONN) + return err; /* TODO: only disarm if this is the only disconnected slave */ if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); + err = memif_syscall_error_handler (errno); + DBG ("timerfd_settime: disarm"); } if (c->args.socket_filename) @@ -504,10 +625,9 @@ memif_delete (memif_conn_handle_t *conn) c = NULL; *conn = c; - return 0; + return err; } - int memif_connect1 (memif_connection_t *c) { @@ -520,11 +640,13 @@ memif_connect1 (memif_connection_t *c) if (!mr->shm) { if (mr->fd < 0) - error_return ("no memory region fd"); + return MEMIF_ERR_NO_SHMFD; if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, MAP_SHARED, mr->fd, 0)) == MAP_FAILED) - error_return_unix ("mmap"); + { + return memif_syscall_error_handler (errno); + } } } @@ -535,8 +657,10 @@ memif_connect1 (memif_connection_t *c) { mq->ring = c->regions->shm + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) - error_return ("wrong cookie on tx ring %u", i); - i++; + { + DBG ("wrong cookie on tx ring %u", i); + return MEMIF_ERR_COOKIE; + } } i = 0; mq = c->rx_queues; @@ -544,10 +668,13 @@ memif_connect1 (memif_connection_t *c) { mq->ring = c->regions->shm + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) - error_return ("wrong cookie on tx ring %u", i); - i++; + { + DBG ("wrong cookie on rx ring %u", i); + return MEMIF_ERR_COOKIE; + } } + /* callback */ lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); return 0; @@ -562,6 +689,8 @@ memif_init_regions_and_queues (memif_connection_t *conn) int i,j; conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t)); + if (conn->regions == NULL) + return memif_syscall_error_handler (errno); r = conn->regions; buffer_offset = (conn->args.num_s2m_rings + conn->args.num_m2s_rings) * @@ -573,17 +702,17 @@ memif_init_regions_and_queues (memif_connection_t *conn) (conn->args.num_s2m_rings + conn->args.num_m2s_rings); if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) - error_return_unix ("memfd_create: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) - error_return_unix ("fcntl: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if ((ftruncate (r->fd, r->region_size)) == -1) - error_return_unix ("ftruncate: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE, MAP_SHARED, r->fd, 0)) == MAP_FAILED) - error_return_unix ("mmap: %s", strerror (errno)); + return memif_syscall_error_handler (errno); for (i = 0; i < conn->args.num_s2m_rings; i++) { @@ -619,7 +748,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) { mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) - error_return_unix ("eventfd: %s", strerror (errno)); + return memif_syscall_error_handler (errno); mq->ring = memif_get_ring (conn, MEMIF_RING_S2M, x); mq->log2_ring_size = conn->args.log2_ring_size; mq->region = 0; @@ -632,7 +761,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) { mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) - error_return_unix ("eventfd: %s", strerror (errno)); + return memif_syscall_error_handler (errno); mq->ring = memif_get_ring (conn, MEMIF_RING_M2S, x); mq->log2_ring_size = conn->args.log2_ring_size; mq->region = 0; @@ -647,14 +776,20 @@ 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) + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; memif_queue_t *mq = c->tx_queues; memif_ring_t *ring = mq->ring; memif_buffer_t *b0, *b1; uint16_t mask = (1 << mq->log2_ring_size) - 1; - uint16_t i = 0, s0, s1, ns; + uint16_t s0, s1, ns; + *count_out = 0; + int err = MEMIF_ERR_SUCCESS; /* 0 */ if (ring->tail != ring->head) { @@ -666,18 +801,22 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, else ns = (1 << mq->log2_ring_size); - /* if head == tail receive function will asume that no packets are available */ + /* (head == tail) ? receive function will asume that no packets are available */ ns -= 1; while (count && ns) { while ((count > 2) && (ns > 2)) { - s0 = (ring->head + mq->alloc_bufs + i) & mask; - s1 = (ring->head + mq->alloc_bufs + i + 1) & mask; + s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; + s1 = (ring->head + mq->alloc_bufs + *count_out + 1) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b1 == NULL) + return memif_syscall_error_handler (errno); b0->desc_index = s0; b1->desc_index = s1; @@ -687,102 +826,118 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, b0->data = c->regions->shm + ring->desc[s0].offset; b1->data = c->regions->shm + ring->desc[s1].offset; - (*(bufs + i)) = b0; - (*(bufs + i + 1)) = b1; + (*(bufs + *count_out)) = b0; + (*(bufs + *count_out + 1)) = b1; DBG ("allocated ring slots %u, %u", s0, s1); count -= 2; ns -= 2; - i += 2; + *count_out += 2; } - s0 = (ring->head + mq->alloc_bufs + i) & mask; + s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); b0->desc_index = s0; b0->buffer_len = ring->desc[s0].buffer_length; b0->data = c->regions->shm + ring->desc[s0].offset; - (*(bufs + i)) = b0; + (*(bufs + *count_out)) = b0; DBG ("allocated ring slot %u", s0); count--; ns--; - i++; + *count_out += 1; } - mq->alloc_bufs += i; + mq->alloc_bufs += *count_out; - DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, mq->alloc_bufs); + DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, mq->alloc_bufs); if (count) + { DBG ("ring buffer full! qid: %u", qid); + err = MEMIF_ERR_NOBUF_RING; + } - return i; + return err; } int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count) + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; + libmemif_main_t *lm = &libmemif_main; memif_queue_t *mq = c->rx_queues; memif_ring_t *ring = mq->ring; uint16_t tail = ring->tail; uint16_t mask = (1 << mq->log2_ring_size) - 1; - int i = 0; memif_buffer_t *b0, *b1; + *count_out = 0; + + if (mq->alloc_bufs < count) + count = mq->alloc_bufs; while (count) { while (count > 2) { - b0 = (*(bufs + i)); - b1 = (*(bufs + i + 1)); + b0 = (*(bufs + *count_out)); + b1 = (*(bufs + *count_out + 1)); tail = (b0->desc_index + 1) & mask; tail = (b1->desc_index + 1) & mask; b0->data = NULL; b1->data = NULL; free (b0); free (b1); - (*(bufs + i)) = b0 = NULL; - (*(bufs + i + 1)) = b1 = NULL; - + (*(bufs + *count_out)) = b0 = NULL; + (*(bufs + *count_out + 1)) = b1 = NULL; count -= 2; - i += 2; + *count_out += 2; } - b0 = (*(bufs + i)); + b0 = (*(bufs + *count_out)); tail = (b0->desc_index + 1) & mask; b0->data = NULL; free (b0); - (*(bufs + i)) = b0 = NULL; + (*(bufs + *count_out)) = b0 = NULL; count--; - i++; + *count_out += 1; } ring->tail = tail; - return i; + return MEMIF_ERR_SUCCESS; /* 0 */ } int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count) + memif_buffer_t **bufs, uint16_t count, uint16_t *tx) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; memif_queue_t *mq = c->tx_queues; memif_ring_t *ring = mq->ring; uint16_t head = ring->head; uint16_t mask = (1 << mq->log2_ring_size) - 1; - uint16_t tx = 0; + *tx = 0; memif_buffer_t *b0, *b1; while (count) { while (count > 2) { - b0 = (*(bufs + tx)); - b1 = (*(bufs + tx + 1)); + 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; @@ -800,10 +955,10 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b1->data_len = 0; count -= 2; - tx += 2; + *tx += 2; } - b0 = (*(bufs + tx)); + b0 = (*(bufs + *tx)); ring->desc[b0->desc_index].length = b0->data_len; #ifdef MEMIF_DBG_SHM @@ -816,34 +971,39 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b0->data_len = 0; count--; - tx++; + *tx += 1; } ring->head = head; - mq->alloc_bufs -= tx; + mq->alloc_bufs -= *tx; if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) { uint64_t a = 1; int r = write (mq->int_fd, &a, sizeof (a)); if (r < 0) - error_return ("write: %s fd %d", strerror (errno), mq->int_fd); + return MEMIF_ERR_INT_WRITE; } - return tx; + return MEMIF_ERR_SUCCESS; /* 0 */ } int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count) + memif_buffer_t **bufs, uint16_t count, uint16_t *rx) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; memif_queue_t *mq = c->rx_queues; memif_ring_t *ring = mq->ring; uint16_t head = ring->head; - uint16_t ns, rx = 0; + uint16_t ns; uint16_t mask = (1 << mq->log2_ring_size) - 1; memif_buffer_t *b0, *b1; + *rx = 0; if (head == mq->last_head) return 0; @@ -858,7 +1018,11 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, while ((ns > 2) && (count > 2)) { b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b1 == NULL) + return memif_syscall_error_handler (errno); b0->desc_index = mq->last_head; b1->desc_index = mq->last_head + 1; @@ -869,8 +1033,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, b0->buffer_len = ring->desc[mq->last_head].buffer_length; b1->buffer_len = ring->desc[mq->last_head + 1].buffer_length; - (*(bufs + rx)) = b0; - (*(bufs + rx + 1)) = b1; + (*(bufs + *rx)) = b0; + (*(bufs + *rx + 1)) = b1; #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); @@ -881,16 +1045,18 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, ns -= 2; count -= 2; - rx += 2; + *rx += 2; } b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); 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; - (*(bufs + rx)) = b0; + (*(bufs + *rx)) = b0; #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); @@ -900,22 +1066,33 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, ns--; count--; - rx++; + *rx += 1; } + mq->alloc_bufs += *rx; + if (ns) { - DBG ("ring buffer full"); + DBG ("not enough buffers!"); + return MEMIF_ERR_NOBUF; } - return rx; + return MEMIF_ERR_SUCCESS; /* 0 */ } int -memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid) +memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) { memif_connection_t *c = (memif_connection_t *) conn; - return c->rx_queues->int_fd; + if (c == NULL) + { + *efd = -1; + return MEMIF_ERR_NOCONN; + } + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; + *efd = c->rx_queues->int_fd; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_details_t diff --git a/src/memif_private.h b/src/memif_private.h index 01c077d..aed36ca 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -77,6 +78,35 @@ #endif /* MEMIF_DBG */ +#if 0 + +typedef enum +{ + MEMIF_ERR_SUCCESS = 0, /* success */ +/* SYSCALL ERRORS */ + MEMIF_ERR_SYSCALL, /* other syscall error */ + MEMIF_ERR_ACCES, /* permission denied */ + 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_INT_WRITE, /* send interrupt error */ +} memif_err_t; + +#endif /* 0 */ typedef struct { @@ -98,6 +128,7 @@ typedef struct int int_fd; uint64_t int_count; + uint32_t alloc_bufs; } memif_queue_t; typedef struct memif_msg_queue_elt @@ -137,12 +168,38 @@ typedef struct memif_connection memif_queue_t *rx_queues; memif_queue_t *tx_queues; - uint32_t alloc_buf_num; - uint16_t flags; #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) } memif_connection_t; +/* + * WIP + */ +typedef struct +{ + int fd; + uint16_t use_count; + uint8_t *filename; +} memif_socket_t; + +/* + * WIP + */ +/* probably function like memif_cleanup () will need to be called + close timerfd, free struct libmemif_main and its nested structures */ +typedef struct +{ + memif_control_fd_update_t *control_fd_update; + int timerfd; + struct itimerspec arm, disarm; + + /* TODO: update to arrays support multiple connections */ + memif_socket_t ms; + memif_connection_t *conn; +} libmemif_main_t; + +extern libmemif_main_t libmemif_main; + /* main.c */ /* if region doesn't contain shared memory, mmap region, check ring cookie */ @@ -153,6 +210,9 @@ int memif_init_regions_and_queues (memif_connection_t *c); int memif_disconnect_internal (memif_connection_t *c); +/* map errno to memif error code */ +int memif_syscall_error_handler (int err_code); + #ifndef __NR_memfd_create #if defined __x86_64__ #define __NR_memfd_create 319 diff --git a/src/socket.c b/src/socket.c index 132314d..0447569 100644 --- a/src/socket.c +++ b/src/socket.c @@ -42,7 +42,7 @@ memif_msg_send (int fd, memif_msg_t *msg, int afd) struct msghdr mh = { 0 }; struct iovec iov[1]; char ctl[CMSG_SPACE (sizeof (int))]; - int rv; + int rv, err = MEMIF_ERR_SUCCESS; /* 0 */ iov[0].iov_base = (void *) msg; iov[0].iov_len = sizeof (memif_msg_t); @@ -63,17 +63,19 @@ memif_msg_send (int fd, memif_msg_t *msg, int afd) } rv = sendmsg (fd, &mh, 0); if (rv < 0) - error_return ("sendmsg: %s fd %d", strerror (errno), fd); + err = memif_syscall_error_handler (errno); DBG ("Message type %u sent", msg->type); - return rv; + return err; } /* response from memif master - master is ready to handle next message */ -static_fn void +static_fn int memif_msg_enq_ack (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); e->msg.type = MEMIF_MSG_TYPE_ACK; e->fd = -1; @@ -82,7 +84,7 @@ memif_msg_enq_ack (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -91,6 +93,8 @@ memif_msg_enq_ack (memif_connection_t *c) cur = cur->next; } cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ } static_fn int @@ -115,11 +119,13 @@ memif_msg_send_hello (memif_connection_t *c) } /* send id and secret (optional) for interface identification */ -static_fn void +static_fn int memif_msg_enq_init (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); memif_msg_init_t *i = &e->msg.init; @@ -138,7 +144,7 @@ memif_msg_enq_init (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -147,20 +153,21 @@ memif_msg_enq_init (memif_connection_t *c) cur = cur->next; } cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* send information about region specified by region_index */ static_fn int memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) { - if (c->regions == NULL) - error_return ("no regions initialized"); - /* TODO: support multiple regions */ memif_region_t *mr = c->regions; memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); memif_msg_add_region_t *ar = &e->msg.add_region; @@ -173,7 +180,7 @@ memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) if (c->msg_queue == NULL) { c->msg_queue = e; - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -182,7 +189,8 @@ memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) cur = cur->next; } cur->next = e; - return 0; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* send information about ring specified by direction (S2M | M2S) and index */ @@ -191,6 +199,8 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); memif_msg_add_ring_t *ar = &e->msg.add_ring; @@ -214,7 +224,7 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) if (c->msg_queue == NULL) { c->msg_queue = e; - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -223,15 +233,19 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) cur = cur->next; } cur->next = e; - return 0; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* used as connection request from slave */ -static_fn void +static_fn int memif_msg_enq_connect (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); + memif_msg_connect_t *cm = &e->msg.connect; e->msg.type = MEMIF_MSG_TYPE_CONNECT; @@ -243,7 +257,7 @@ memif_msg_enq_connect (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -252,15 +266,19 @@ memif_msg_enq_connect (memif_connection_t *c) cur = cur->next; } cur->next = e; - return; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* used as confirmation of connection by master */ -static_fn void +static_fn int memif_msg_enq_connected (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); + memif_msg_connected_t *cm = &e->msg.connected; e->msg.type = MEMIF_MSG_TYPE_CONNECTED; @@ -272,7 +290,7 @@ memif_msg_enq_connected (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -281,7 +299,8 @@ memif_msg_enq_connected (memif_connection_t *c) cur = cur->next; } cur->next = e; - return; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* immediately send disconnect msg */ @@ -315,7 +334,7 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) msg->hello.max_version < MEMIF_VERSION) { DBG ("incompatible protocol version"); - return -1; + return MEMIF_ERR_PROTO; } /* use nested struct c->run containing following variables? (this would be used to adjust shared memory information while keeping @@ -328,7 +347,7 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) c->args.log2_ring_size); strncpy ((char *) c->remote_name, (char *) h->name, strlen ((char *) h->name)); - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } /* handle interface identification (id, secret (optional)) */ @@ -337,16 +356,20 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) { memif_msg_init_t *i = &msg->init; uint8_t err_string[96]; + int err = MEMIF_ERR_SUCCESS; /* 0 */ + int err_disc; if (i->version != MEMIF_VERSION) { DBG ("MEMIF_VER_ERR"); strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR)); + err = MEMIF_ERR_PROTO; goto error; } if (c->args.interface_id != i->id) { DBG ("MEMIF_ID_ERR"); strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR)); + err = MEMIF_ERR_ID; goto error; } @@ -355,18 +378,21 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) { DBG ("MEMIF_SLAVE_ERR"); strncpy ((char *) err_string, MEMIF_SLAVE_ERR, strlen (MEMIF_SLAVE_ERR)); + err = MEMIF_ERR_ACCSLAVE; goto error; } if (c->fd != -1) { DBG ("MEMIF_CONN_ERR"); strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR)); + err = MEMIF_ERR_ALRCONN; goto error; } if (i->mode != c->args.mode) { DBG ("MEMIF_MODE_ERR"); strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR)); + err = MEMIF_ERR_MODE; goto error; } @@ -382,7 +408,8 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) DBG ("MEMIF_SECRET_ERR"); strncpy ((char *) err_string, MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); - return -1; + err = MEMIF_ERR_SECRET; + goto error; } r = strncmp ((char *) i->secret, (char *) c->args.secret, strlen ((char *) c->args.secret)); @@ -391,7 +418,8 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) DBG ("MEMIF_SECRET_ERR"); strncpy ((char *) err_string, MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); - return -1; + err = MEMIF_ERR_SECRET; + goto error; } } else @@ -399,14 +427,21 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) DBG ("MEMIF_NOSECRET_ERR"); strncpy ((char *) err_string, MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR)); - return -1; + err = MEMIF_ERR_NOSECRET; + goto error; } } - return 0; + + return err; error: - memif_msg_send_disconnect (c, err_string, 1); - return -1; + if (c->fd != -1) + { + err_disc = memif_msg_send_disconnect (c, err_string, 1); + if (err_disc != 0) + return err_disc; + } + return err; } /* receive region information and add new region to connection (if possible) */ @@ -416,12 +451,14 @@ memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) memif_msg_add_region_t *ar = &msg->add_region; memif_region_t *mr; if (fd < 0) - error_return ("missing memory region fd"); + return MEMIF_ERR_NO_SHMFD; if (ar->index > MEMIF_MAX_REGION) - error_return ("maximum region limit reached"); + return MEMIF_ERR_MAXREG; mr = (memif_region_t *) malloc (sizeof (memif_region_t )); + if (mr == NULL) + return memif_syscall_error_handler (errno); mr->fd = fd; mr->region_size = ar->size; mr->shm = NULL; @@ -429,7 +466,7 @@ memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) /* TODO: support multiple regions */ c->regions = mr; - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } /* receive ring information and add new ring to connection queue @@ -442,12 +479,12 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) memif_queue_t *mq; if (fd < 0) - error_return ("missing ring interrupt fd"); + return MEMIF_ERR_NO_INTFD; if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) { if (ar->index > MEMIF_MAX_S2M_RING) - error_return ("maximum ring limit reached"); + return MEMIF_ERR_MAXRING; mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); mq->int_fd = fd; @@ -462,9 +499,11 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) else { if (ar->index > MEMIF_MAX_M2S_RING) - error_return ("maximum ring limit reached"); + return MEMIF_ERR_MAXRING; mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + if (mq == NULL) + return memif_syscall_error_handler (errno); mq->int_fd = fd; mq->log2_ring_size = ar->log2_ring_size; mq->region = ar->region; @@ -475,7 +514,7 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) c->args.num_m2s_rings++; } - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } /* slave -> master */ @@ -484,16 +523,16 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) { memif_msg_connect_t *cm = &msg->connect; - int err = 0; + int err; err = memif_connect1 (c); - if (err < 0) + if (err != MEMIF_ERR_SUCCESS) /* 0 */ return err; strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); - return 0; + return err; } /* master -> slave */ @@ -502,16 +541,16 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) { memif_msg_connect_t *cm = &msg->connect; - int err = 0; + int err; err = memif_connect1 (c); - if (err < 0) + if (err != MEMIF_ERR_SUCCESS) /* 0 */ return err; strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); - return 0; + return err; } static_fn int @@ -523,8 +562,9 @@ memif_msg_receive_disconnect (memif_connection_t *c, memif_msg_t *msg) strlen ((char *) d->string)); /* on returning error, handle function will call memif_disconnect () */ - error_return ("disconnect received: %s, mode: %d", + DBG ("disconnect received: %s, mode: %d", c->remote_disconnect_string, c->args.mode); + return MEMIF_ERR_DISCONNECT; } static_fn int @@ -536,7 +576,7 @@ memif_msg_receive (memif_connection_t *c) struct iovec iov[1]; memif_msg_t msg = { 0 }; ssize_t size; - int err = 0; + int err = MEMIF_ERR_SUCCESS; /* 0 */ int fd = -1; int i = 0; @@ -551,9 +591,9 @@ memif_msg_receive (memif_connection_t *c) if (size != sizeof (memif_msg_t)) { if (size == 0) - error_return ("disconnected"); + return MEMIF_ERR_DISCONNECTED; else - error_return ("malformed message received on fd %d", c->fd); + return MEMIF_ERR_MFMSG; } struct ucred *cr = 0; @@ -585,63 +625,73 @@ memif_msg_receive (memif_connection_t *c) break; case MEMIF_MSG_TYPE_HELLO: - if ((err = memif_msg_receive_hello (c, &msg)) < 0) + if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) return err; - if ((err = memif_init_regions_and_queues (c)) < 0) + if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_init (c); - memif_msg_enq_add_region (c, 0); /* TODO: support multiple rings */ - memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M); - memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S); - memif_msg_enq_connect (c); + if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS) + return err; break; case MEMIF_MSG_TYPE_INIT: - if ((err = memif_msg_receive_init (c, &msg)) < 0) + if ((err = memif_msg_receive_init (c, &msg)) != MEMIF_ERR_SUCCESS) return err; /* c->remote_pid = cr->pid */ /* c->remote_uid = cr->uid */ /* c->remote_gid = cr->gid */ - memif_msg_enq_ack (c); + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) + return err; break; case MEMIF_MSG_TYPE_ADD_REGION: - if ((err = memif_msg_receive_add_region (c, &msg, fd)) < 0) + if ((err = memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_ack (c); break; case MEMIF_MSG_TYPE_ADD_RING: - if ((err = memif_msg_receive_add_ring (c, &msg, fd)) < 0) + if ((err = memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_ack (c); break; case MEMIF_MSG_TYPE_CONNECT: - if ((err = memif_msg_receive_connect (c, &msg)) < 0) + if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_connected (c); break; case MEMIF_MSG_TYPE_CONNECTED: - if ((err = memif_msg_receive_connected (c, &msg)) < 0) + if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS) return err; break; case MEMIF_MSG_TYPE_DISCONNECT: - if ((err = memif_msg_receive_disconnect (c, &msg)) < 0) + if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS) return err; break; default: - error_return ("unknown message type (0x%x)", msg.type); + return MEMIF_ERR_UNKNOWN_MSG;; break; } - /* can send msg */ c->flags |= MEMIF_CONNECTION_FLAG_WRITE; - return 0; +/* libmemif_main_t *lm = &libmemif_main; + lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */ + return MEMIF_ERR_SUCCESS; /* 0 */ } int @@ -650,42 +700,52 @@ memif_conn_fd_error (memif_connection_t *c) DBG ("connection fd error"); strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19); - memif_disconnect_internal (c); - return 0; + int err = memif_disconnect_internal (c); + return err; } /* calls memif_msg_receive to handle pending messages on socket */ int memif_conn_fd_read_ready (memif_connection_t *c) { - int rv = 0; - rv = memif_msg_receive (c); - if (rv < 0) + int err; + err = memif_msg_receive (c); + if (err != 0) { - memif_disconnect_internal (c); + err = memif_disconnect_internal (c); } - return 0; + return err; } /* get msg from msg queue buffer and send it to socket */ int memif_conn_fd_write_ready (memif_connection_t *c) { - int rv = 0; + int err = MEMIF_ERR_SUCCESS; /* 0 */ + + if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0) - return rv; + goto done; memif_msg_queue_elt_t *e = c->msg_queue; if (e == NULL) - return rv; + goto done; c->msg_queue = c->msg_queue->next; - + c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE; - - rv = memif_msg_send (c->fd, &e->msg, e->fd); +/* + libmemif_main_t *lm = &libmemif_main; + + lm->control_fd_update (c->fd, + MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD); +*/ + err = memif_msg_send (c->fd, &e->msg, e->fd); free(e); - return rv; + goto done; + +done: + return err; } int @@ -702,8 +762,7 @@ memif_conn_fd_accept_ready (memif_connection_t *c) if (conn_fd < 0) { - DBG ("accept fd %d", c->fd); - return -1; + return memif_syscall_error_handler (errno); } DBG ("accept fd %d", c->fd); DBG ("conn fd %d", conn_fd); @@ -713,12 +772,5 @@ memif_conn_fd_accept_ready (memif_connection_t *c) c->error_fn = memif_conn_fd_error; c->fd = conn_fd; - int e = memif_msg_send_hello (c); - if (e < 0) - { - DBG ("memif msg send hello error!"); - return -1; - } - - return 0; + return memif_msg_send_hello (c); } diff --git a/src/socket.h b/src/socket.h index 1280505..d96c5dd 100644 --- a/src/socket.h +++ b/src/socket.h @@ -20,7 +20,7 @@ #include -/* interface identification errors */ +/* interface identification errors (disconnect messages)*/ #define MEMIF_VER_ERR "incompatible version" #define MEMIF_ID_ERR "unmatched interface id" #define MEMIF_SLAVE_ERR "cannot connect to salve" @@ -48,11 +48,11 @@ int memif_msg_send_disconnect (memif_connection_t *c, uint8_t *err_string, uint3 int memif_msg_send (int fd, memif_msg_t *msg, int afd); -void memif_msg_enq_ack (memif_connection_t *c); +int memif_msg_enq_ack (memif_connection_t *c); int memif_msg_send_hello (memif_connection_t *c); -void memif_msg_enq_init (memif_connection_t *c); +int memif_msg_enq_init (memif_connection_t *c); int memif_msg_enq_add_region (memif_connection_t *c, uint8_t region); @@ -66,9 +66,9 @@ int memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int f int memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd); -void memif_msg_enq_connect (memif_connection_t *c); +int memif_msg_enq_connect (memif_connection_t *c); -void memif_msg_enq_connected (memif_connection_t *c); +int memif_msg_enq_connected (memif_connection_t *c); int memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg); From c076f05608cd556d56e91e4624b324c99edc9030 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Tue, 18 Jul 2017 13:43:28 +0200 Subject: [PATCH 17/63] optimization, fix memleaks, doxygen comments (libmemif.h) --- Makefile.am | 2 +- examples/icmp_responder/icmp_proto.c | 17 +++ examples/icmp_responder/icmp_proto.h | 17 +++ examples/icmp_responder/main.c | 132 ++++++++++------- src/libmemif.h | 135 +++++++++++++++-- src/main.c | 207 +++++++++++++++------------ 6 files changed, 356 insertions(+), 154 deletions(-) diff --git a/Makefile.am b/Makefile.am index b663fcf..7700548 100644 --- a/Makefile.am +++ b/Makefile.am @@ -31,7 +31,7 @@ unit_test_SOURCES = test/socket_test.c \ src/main.c # macro MEMIF_UNIT_TEST -> compile functions without static keyword # and declare them in header files, so they can be called from unit tests -unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST +unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST -g # # main lib diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c index 689dc5e..f4700f2 100644 --- a/examples/icmp_responder/icmp_proto.c +++ b/examples/icmp_responder/icmp_proto.c @@ -1,3 +1,20 @@ +/* + *------------------------------------------------------------------ + * 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 diff --git a/examples/icmp_responder/icmp_proto.h b/examples/icmp_responder/icmp_proto.h index ec5ae70..48e3779 100644 --- a/examples/icmp_responder/icmp_proto.h +++ b/examples/icmp_responder/icmp_proto.h @@ -1,3 +1,20 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + #ifndef _ICMP_PROTO_H_ #define _ICMP_PROTO_H_ diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index f78d7b7..d1df27d 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -66,6 +66,8 @@ printf ("\n"); \ } while (0) +#define MAX_MEMIF_BUFS 256 + int epfd; /* @@ -86,12 +88,12 @@ typedef struct /* interrupt file descriptor (specific for each queue) */ int_fd_t *int_fd; /* tx buffers */ - memif_buffer_t **tx_bufs; - /* number of allocated tx buffers */ + memif_buffer_t *tx_bufs; + /* number of tx buffers pointing to shared memory */ uint16_t tx_buf_num; /* rx buffers */ - memif_buffer_t **rx_bufs; - /* number of allocated rx buffers */ + memif_buffer_t *rx_bufs; + /* number of rx buffers pointing to shared memory */ uint16_t rx_buf_num; } memif_connection_t; @@ -104,13 +106,21 @@ print_memif_details () printf ("MEMIF DETAILS\n"); printf ("==============================\n"); - /* TODO: loop for all connections */ - if (c->conn == NULL) + + memif_details_t md; + memset (&md, 0, sizeof (md)); + ssize_t buflen = 2048; + char *buf = malloc (buflen); + int err; + + err = memif_get_details (c->conn, &md, buf, buflen); + if (err != MEMIF_ERR_SUCCESS) { - printf ("no connection!\n"); - return; + INFO ("%s", memif_strerror (err)); + if (err == MEMIF_ERR_NOCONN) + return; } - memif_details_t md = memif_get_details (c->conn); + 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); @@ -218,7 +228,9 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) { INFO ("memif connected!"); int_fd_t *ifd = (int_fd_t *) malloc (sizeof (int_fd_t)); - ifd->fd = memif_get_queue_efd ((&memif_connection)->conn, 0); + int err; + err = memif_get_queue_efd ((&memif_connection)->conn, 0, &ifd->fd); + INFO ("memif_get_queue_efd: %s", memif_strerror (err)); ifd->qid = 0; (&memif_connection)->int_fd = ifd; return add_epoll_fd (ifd->fd, EPOLLIN); @@ -273,32 +285,18 @@ icmpr_memif_create (int is_master) args.interface_id = 0; /* last argument for memif_create (void * private_ctx) is used by user to identify connection. this context is returned with callbacks */ - int rv = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, NULL); - if (rv < 0) - { - DBG ("memif create error!"); - } - else - { - DBG ("memif created!"); - } - return rv; + int err = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, NULL); + INFO ("memif_create: %s", memif_strerror (err)); + return 0; } int icmpr_memif_delete () { - int rv = 0; + int err; /* disconenct then delete memif connection */ - rv = memif_delete (&(&memif_connection)->conn); - if (rv < 0) - { - DBG ("memif delete error!"); - } - else - { - DBG ("memif deleted!"); - } + err = memif_delete (&(&memif_connection)->conn); + INFO ("memif_delete: %s", memif_strerror (err)); return 0; } @@ -322,18 +320,19 @@ print_help () printf ("\texit - exit app\n"); printf ("\tconn - create memif (slave-mode)\n"); printf ("\tdel - delete memif\n"); - printf ("\tshow - print memif details\n"); + printf ("\tshow - show connection details\n"); } int icmpr_buffer_alloc (long n) { memif_connection_t *c = &memif_connection; - c->tx_bufs = (memif_buffer_t **) malloc (sizeof (memif_buffer_t *) * n); - DBG ("call memif_buffer_alloc"); - int r = memif_buffer_alloc (c->conn, 0, c->tx_bufs, n); - DBG ("allocated %d/%ld buffers", r, n); + int err; + uint16_t r, qid = 0; + err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); c->tx_buf_num += r; + DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); return 0; } @@ -341,12 +340,32 @@ int icmpr_tx_burst () { memif_connection_t *c = &memif_connection; - int r = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num); + int err; + uint16_t r, qid = 0; + err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); + INFO ("memif_tx_burst: %s", memif_strerror (err)); DBG ("tx: %d/%u", r, c->tx_buf_num); c->tx_buf_num -= r; return 0; } +int +icmpr_free () +{ + memif_connection_t *c = &memif_connection; + if (c->int_fd != NULL) + free (c->int_fd); + c->int_fd = NULL; + if (c->tx_bufs != NULL) + free (c->tx_bufs); + c->tx_bufs = NULL; + if (c->rx_bufs) + free (c->rx_bufs); + c->rx_bufs = NULL; + + return 0; +} + int user_input_handler () { @@ -359,6 +378,7 @@ user_input_handler () { free (ui); icmpr_memif_delete (); + icmpr_free (); exit (EXIT_SUCCESS); } else if (strncmp (ui, "help", 4) == 0) @@ -389,28 +409,36 @@ int icmpr_interrupt (int fd) { memif_connection_t *c = &memif_connection; - DBG ("interrupted!"); +/* DBG ("interrupted!"); uint64_t b; ssize_t r = read (fd, &b, sizeof (b)); +*/ - int rx = memif_rx_burst (c->conn, 0, c->rx_bufs, c->rx_buf_num); - c->rx_buf_num -= rx; + int err; + uint16_t rx; + err = memif_rx_burst (c->conn, 0, c->rx_bufs, MAX_MEMIF_BUFS, &rx); + INFO ("memif_rx_burst: %s", memif_strerror (err)); + c->rx_buf_num += rx; - DBG ("received %d buffers. %u free buffers", rx, c->rx_buf_num); + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); icmpr_buffer_alloc (rx); int i; for (i = 0; i < rx; i++) { - resolve_packet ((void *) (*(c->rx_bufs + i))->data, - (*(c->rx_bufs + i))->data_len, (void *) (*(c->tx_bufs + i))->data, - &(*(c->tx_bufs + i))->data_len); + resolve_packet ((void *) (c->rx_bufs + i)->data, + (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, + &(c->tx_bufs + i)->data_len); } - int fb = memif_buffer_free (c->conn, 0, c->rx_bufs, rx); - c->rx_buf_num += fb; + uint16_t fb; + err = memif_buffer_free (c->conn, 0, c->rx_bufs, rx, &fb); + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; - DBG ("freed %d buffers. %u free buffers", fb, c->rx_buf_num); + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); icmpr_tx_burst (); @@ -452,6 +480,7 @@ poll_event (int timeout) if (evt.events & EPOLLERR) events |= MEMIF_FD_EVENT_ERROR; memif_err = memif_control_fd_handler (evt.data.fd, events); + INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); } } else if (evt.data.fd == 0) @@ -487,11 +516,16 @@ int main () c->conn = NULL; c->int_fd = malloc (sizeof (int_fd_t)); c->int_fd->fd = -1; - c->rx_buf_num = 256; - c->rx_bufs = malloc (sizeof (memif_buffer_t *) * c->rx_buf_num); + /* alloc memif buffers */ + c->rx_buf_num = 0; + c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->tx_buf_num = 0; + c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); /* initialize memory interface */ - memif_init (control_fd_update); + int err; + err = memif_init (control_fd_update); + INFO ("memif_init: %s", memif_strerror (err)); /* main loop */ while (1) diff --git a/src/libmemif.h b/src/libmemif.h index 363a4b3..8da178f 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -48,6 +48,7 @@ typedef enum 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 PROTO ERRORS */ @@ -77,6 +78,9 @@ typedef enum /* 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; /** \brief Memif control file descriptor update (callback function) @@ -99,10 +103,23 @@ typedef int (memif_control_fd_update_t) (int fd, uint8_t events); */ typedef int (memif_connection_update_t) (memif_conn_handle_t conn, void *private_ctx); +/** \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; - uint8_t secret[24]; + uint8_t secret[24]; /* optional */ uint8_t num_s2m_rings; uint8_t num_m2s_rings; @@ -116,6 +133,12 @@ typedef struct memif_interface_mode_t mode:8; } memif_conn_args_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; @@ -124,18 +147,34 @@ typedef struct void *data; } memif_buffer_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 ring_size - ring size + @param buffer_size - size of buffer in shared memory + @param rx_queues - number of receive queues + @param tx_queues - number of transmit queues + @param link_up_down - 1 = up (connected), 2 = down (disconnected) +*/ typedef struct { - uint8_t if_name[32]; - uint8_t inst_name[32]; - uint8_t remote_if_name[32]; - uint8_t remote_inst_name[32]; + uint8_t *if_name; + uint8_t *inst_name; + uint8_t *remote_if_name; + uint8_t *remote_inst_name; uint32_t id; - uint8_t secret[24]; /* optional */ + 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[128]; + uint8_t *socket_filename; uint32_t ring_size; uint16_t buffer_size; uint8_t rx_queues; @@ -144,9 +183,30 @@ typedef struct uint8_t link_up_down; /* 1 = up, 0 = down */ } memif_details_t; +/** \brief Memif strerror + @param - err_code - error code + + converts error code to error message + + return + error string +*/ char *memif_strerror (int err_code); -memif_details_t memif_get_details (memif_conn_handle_t conn); +/** \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_SUCCESS - no error + MEMIF_ERR_NOCONN - connection handle points to NULL + MEMIF_ERR_NOBUF_DET - privided buffer is not long enough + +*/ +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 @@ -240,7 +300,7 @@ int memif_control_fd_handler (int fd, uint8_t events); int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd); /** \brief Memif delete - @param conn - memif connection handle + @param conn - pointer to memif connection handle return MEMIF_ERR_SUCCESS - no error @@ -253,17 +313,66 @@ int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd); */ 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_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted + MEMIF_ERR_NOBUF_RING - ring buffer full +*/ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); + 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_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted +*/ int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); + 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 count_out - returns number of transmitted buffers + + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted +*/ int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *tx); + 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 count_out - returns number of received buffers + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted + MEMIF_ERR_NOBUF - not enough buffers provided +*/ int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *rx); + memif_buffer_t *bufs, uint16_t count, uint16_t *rx); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index ce4d20c..3c9e33c 100644 --- a/src/main.c +++ b/src/main.c @@ -50,9 +50,15 @@ /* private structs and functions */ #include -#define ERRLIST_LEN 33 +#define ERRLIST_LEN 34 #define MAX_ERRBUF_LEN 256 +#if __x86_x64__ +#define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence () +#else +#define MEMIF_MEORY_BARRIER() __sync_synchronize () +#endif /* __x86_x64__ */ + libmemif_main_t libmemif_main; static char memif_buf[MAX_ERRBUF_LEN]; @@ -93,6 +99,8 @@ const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ "Ring buffer full.", /* MEMIF_ERR_NOBUF */ "Not enough memif buffers. There are unreceived data in shared memory.", + /* MEMIF_ERR_NOBUF_DET */ + "Not enough space for memif details in suplied buffer. String data might be malformed.", /* MEMIF_ERR_INT_WRITE */ "Send interrupt error.", /* MEMIF_ERR_MFMSG */ @@ -551,10 +559,7 @@ memif_disconnect_internal (memif_connection_t *c) if (c->tx_queues != NULL) { if (c->tx_queues->int_fd > 0) - { - lm->control_fd_update (c->tx_queues->int_fd, MEMIF_FD_EVENT_DEL); close (c->tx_queues->int_fd); - } c->tx_queues->int_fd = -1; free (c->tx_queues); c->tx_queues = NULL; @@ -776,7 +781,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) + memif_buffer_t *bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -811,12 +816,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; s1 = (ring->head + mq->alloc_bufs + *count_out + 1) & mask; - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); - b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b1 == NULL) - return memif_syscall_error_handler (errno); + b0 = (bufs + *count_out); + b1 = (bufs + *count_out + 1); b0->desc_index = s0; b1->desc_index = s1; @@ -826,24 +827,19 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, b0->data = c->regions->shm + ring->desc[s0].offset; b1->data = c->regions->shm + ring->desc[s1].offset; - (*(bufs + *count_out)) = b0; - (*(bufs + *count_out + 1)) = b1; DBG ("allocated ring slots %u, %u", s0, s1); count -= 2; ns -= 2; *count_out += 2; } s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; - - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); + + b0 = (bufs + *count_out); b0->desc_index = s0; b0->buffer_len = ring->desc[s0].buffer_length; b0->data = c->regions->shm + ring->desc[s0].offset; - (*(bufs + *count_out)) = b0; DBG ("allocated ring slot %u", s0); count--; ns--; @@ -865,7 +861,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *count_out) + memif_buffer_t *bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -887,30 +883,24 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, { while (count > 2) { - b0 = (*(bufs + *count_out)); - b1 = (*(bufs + *count_out + 1)); + b0 = (bufs + *count_out); + b1 = (bufs + *count_out + 1); tail = (b0->desc_index + 1) & mask; tail = (b1->desc_index + 1) & mask; b0->data = NULL; b1->data = NULL; - free (b0); - free (b1); - (*(bufs + *count_out)) = b0 = NULL; - (*(bufs + *count_out + 1)) = b1 = NULL; count -= 2; *count_out += 2; } - b0 = (*(bufs + *count_out)); + b0 = (bufs + *count_out); tail = (b0->desc_index + 1) & mask; b0->data = NULL; - free (b0); - (*(bufs + *count_out)) = b0 = NULL; count--; *count_out += 1; } - + MEMIF_MEORY_BARRIER (); ring->tail = tail; return MEMIF_ERR_SUCCESS; /* 0 */ @@ -918,7 +908,7 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *tx) + memif_buffer_t *bufs, uint16_t count, uint16_t *tx) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -936,8 +926,8 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, { while (count > 2) { - b0 = (*(bufs + *tx)); - b1 = (*(bufs + *tx + 1)); + 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; @@ -953,12 +943,12 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b0->data_len = 0; b1->data = NULL; b1->data_len = 0; - + count -= 2; *tx += 2; } - b0 = (*(bufs + *tx)); + b0 = (bufs + *tx); ring->desc[b0->desc_index].length = b0->data_len; #ifdef MEMIF_DBG_SHM @@ -973,6 +963,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, count--; *tx += 1; } + MEMIF_MEORY_BARRIER (); ring->head = head; mq->alloc_bufs -= *tx; @@ -990,7 +981,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *rx) + memif_buffer_t *bufs, uint16_t count, uint16_t *rx) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -1005,6 +996,12 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *b0, *b1; *rx = 0; + uint64_t b; + ssize_t r = read (mq->int_fd, &b, sizeof (b)); + if ((r == -1) && (errno != EAGAIN)) + return memif_syscall_error_handler (errno); + + if (head == mq->last_head) return 0; @@ -1017,12 +1014,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, { while ((ns > 2) && (count > 2)) { - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); - b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b1 == NULL) - return memif_syscall_error_handler (errno); + b0 = (bufs + *rx); + b1 = (bufs + *rx + 1); b0->desc_index = mq->last_head; b1->desc_index = mq->last_head + 1; @@ -1033,9 +1026,6 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, b0->buffer_len = ring->desc[mq->last_head].buffer_length; b1->buffer_len = ring->desc[mq->last_head + 1].buffer_length; - (*(bufs + *rx)) = b0; - (*(bufs + *rx + 1)) = b1; - #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); print_bytes (b1->data , b1->data_len, DBG_RX_BUF); @@ -1047,17 +1037,13 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, count -= 2; *rx += 2; } - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); + b0 = (bufs + *rx); 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; - (*(bufs + *rx)) = b0; - #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); #endif @@ -1095,49 +1081,88 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) return MEMIF_ERR_SUCCESS; /* 0 */ } -memif_details_t -memif_get_details (memif_conn_handle_t conn) +int +memif_get_details (memif_conn_handle_t conn, memif_details_t *md, + char *buf, ssize_t buflen) { memif_connection_t *c = (memif_connection_t *) conn; - memif_details_t md; - memset (&md, 0, sizeof (md)); - /* interface name */ - strncpy ((char *) md.if_name, (char *) c->args.interface_name, - strlen ((char *) c->args.interface_name)); - /* instance name */ - strncpy ((char *) md.inst_name, (char *) c->args.instance_name, - strlen ((char *) c->args.instance_name)); - /* remote interface name */ - strncpy ((char *) md.remote_if_name, (char *) c->remote_if_name, - strlen ((char *) c->remote_if_name)); - /* remote instance name */ - strncpy ((char *) md.remote_inst_name, (char *) c->remote_name, - strlen ((char *) c->remote_name)); - /* interface id */ - md.id = c->args.interface_id; - /* secret */ + if (c == NULL) + return MEMIF_ERR_NOCONN; + + int err = MEMIF_ERR_SUCCESS; + ssize_t l0, l1, total_l; + l0 = 0; + + l1 = strlen ((char *) c->args.interface_name); + if (l0 + l1 <= buflen) + { + md->if_name = strncpy (buf + l0, (char *) c->args.interface_name, l1); + md->if_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + l1 = strlen ((char *) c->args.instance_name); + if (l0 + l1 <= buflen) + { + md->inst_name = strncpy (buf + l0, (char *) c->args.instance_name, l1); + md->inst_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + l1 = strlen ((char *) c->remote_if_name); + if (l0 + l1 <= buflen) + { + md->remote_if_name = strncpy (buf + l0, (char *) c->remote_if_name, l1); + md->remote_if_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + l1 = strlen ((char *) c->remote_name); + if (l0 + l1 <= buflen) + { + md->remote_inst_name = strncpy (buf + l0, (char *) c->remote_name, l1); + md->remote_inst_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + md->id = c->args.interface_id; + if (c->args.secret) - strncpy ((char *) md.secret, (char *) c->args.secret, - strlen ((char *) c->args.secret)); - /* role */ - md.role = (c->args.is_master) ? 0 : 1; - /* mode */ - md.mode = c->args.mode; - /* socket filename */ - uint32_t l = strlen ((char *) c->args.socket_filename); - if (l > 128) - l = 128; - strncpy ((char *) md.socket_filename, (char *) c->args.socket_filename, l); - /* ring size */ - md.ring_size = (1 << c->args.log2_ring_size); - /* buffer size */ - md.buffer_size = c->args.buffer_size; - /* rx queues */ - md.rx_queues = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; - /* tx queues */ - md.tx_queues = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; - /* link up down */ - md.link_up_down = (c->fd > 0) ? 1 : 0; - - return md; + { + l1 = strlen ((char *) c->args.secret); + md->secret = strncpy (buf + l0, (char *) c->args.secret, l1); + md->secret[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + md->role = (c->args.is_master) ? 0 : 1; + md->mode = c->args.mode; + + l1 = strlen ((char *) c->args.socket_filename); + if (l0 + l1 <= buflen) + { + md->socket_filename = strncpy (buf + l0, (char *) c->args.socket_filename, l1); + md->socket_filename[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + md->ring_size = (1 << c->args.log2_ring_size); + md->buffer_size = c->args.buffer_size; + md->rx_queues = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + md->tx_queues = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + md->link_up_down = (c->fd > 0) ? 1 : 0; + + return err; /* 0 */ } From 9ecda929a096d649ad97b33992fed1d284513abd Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Tue, 18 Jul 2017 15:22:39 +0200 Subject: [PATCH 18/63] memleaks fix --- examples/icmp_responder/main.c | 53 ++++++++++++++++++++-------------- src/main.c | 14 ++++++--- src/socket.c | 9 +++++- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index d1df27d..24ac0ef 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -111,6 +111,7 @@ print_memif_details () memset (&md, 0, sizeof (md)); ssize_t buflen = 2048; char *buf = malloc (buflen); + memset (buf, 0, buflen); int err; err = memif_get_details (c->conn, &md, buf, buflen); @@ -118,7 +119,10 @@ print_memif_details () { INFO ("%s", memif_strerror (err)); if (err == MEMIF_ERR_NOCONN) + { + free (buf); return; + } } printf ("\tinterface name: %s\n",(char *) md.if_name); @@ -158,6 +162,8 @@ print_memif_details () printf ("up\n"); else printf ("down\n"); + + free (buf); } int @@ -226,14 +232,13 @@ del_epoll_fd (int fd) int on_connect (memif_conn_handle_t conn, void *private_ctx) { + memif_connection_t *c = &memif_connection; INFO ("memif connected!"); - int_fd_t *ifd = (int_fd_t *) malloc (sizeof (int_fd_t)); int err; - err = memif_get_queue_efd ((&memif_connection)->conn, 0, &ifd->fd); + err = memif_get_queue_efd (c->conn, 0, &c->int_fd->fd); INFO ("memif_get_queue_efd: %s", memif_strerror (err)); - ifd->qid = 0; - (&memif_connection)->int_fd = ifd; - return add_epoll_fd (ifd->fd, EPOLLIN); + c->int_fd->qid = 0; + return add_epoll_fd (c->int_fd->fd, EPOLLIN); } /* informs user about disconnected status. private_ctx is used by user to identify connection @@ -353,14 +358,11 @@ int icmpr_free () { memif_connection_t *c = &memif_connection; - if (c->int_fd != NULL) - free (c->int_fd); + free (c->int_fd); c->int_fd = NULL; - if (c->tx_bufs != NULL) - free (c->tx_bufs); + free (c->tx_bufs); c->tx_bufs = NULL; - if (c->rx_bufs) - free (c->rx_bufs); + free (c->rx_bufs); c->rx_bufs = NULL; return 0; @@ -369,14 +371,14 @@ icmpr_free () int user_input_handler () { - char *ui = (char *) malloc (256); - char *r = fgets (ui, 256, stdin); - if (ui[0] == '\n') - return 0; - ui = strtok (ui, " "); + char *in = (char *) malloc (256); + char *ui = fgets (in, 256, stdin); + if (in[0] == '\n') + goto done; + ui = strtok (in, " "); if (strncmp (ui, "exit", 4) == 0) { - free (ui); + free (in); icmpr_memif_delete (); icmpr_free (); exit (EXIT_SUCCESS); @@ -384,24 +386,32 @@ user_input_handler () else if (strncmp (ui, "help", 4) == 0) { print_help (); - return 0; + goto done; } else if (strncmp (ui, "conn", 4) == 0) { icmpr_memif_create (0); - return 0; + goto done; } else if (strncmp (ui, "del", 3) == 0) { icmpr_memif_delete (); - return 0; + goto done; } else if (strncmp (ui, "show", 4) == 0) { print_memif_details (); + goto done; } else + { DBG ("unknown command: %s", ui); + goto done; + } + + return 0; +done: + free (in); return 0; } @@ -456,7 +466,8 @@ poll_event (int timeout) memset (&evt, 0, sizeof (evt)); evt.events = EPOLLIN | EPOLLOUT; sigset_t sigset; - en = epoll_pwait (epfd, &evt, 1, timeout, 0); + sigemptyset (&sigset); + en = epoll_pwait (epfd, &evt, 1, timeout, &sigset); if (en < 0) { DBG ("epoll_pwait: %s", strerror (errno)); diff --git a/src/main.c b/src/main.c index 3c9e33c..f00650a 100644 --- a/src/main.c +++ b/src/main.c @@ -270,6 +270,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, err = memif_syscall_error_handler (errno); goto error; } + memset (conn, 0, sizeof (memif_connection_t)); libmemif_main_t *lm = &libmemif_main; @@ -300,10 +301,13 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, l = strlen ((char *) args->instance_name); strncpy ((char *) conn->args.instance_name, (char *) args->instance_name, l); + /* allocate and initialize socket_filename so it can be copyed to sun_path + without memory leaks */ + conn->args.socket_filename = malloc (sizeof (char *) * 108); + memset (conn->args.socket_filename, 0, 108 * sizeof (char *)); + if (args->socket_filename) { - conn->args.socket_filename = (uint8_t *) malloc ( - strlen ((char *) args->socket_filename)); if (conn->args.socket_filename == NULL) { err = memif_syscall_error_handler (errno); @@ -316,7 +320,6 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, { uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR); uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME); - conn->args.socket_filename = (uint8_t *) malloc (sdl + sfl + 1); if (conn->args.socket_filename == NULL) { err = memif_syscall_error_handler (errno); @@ -458,8 +461,9 @@ memif_control_fd_handler (int fd, uint8_t events) } sun.sun_family = AF_UNIX; + strncpy (sun.sun_path, conn->args.socket_filename, - sizeof (sun.sun_path) -1); + sizeof (sun.sun_path) - 1); if (connect (sockfd, (struct sockaddr *) &sun, sizeof (struct sockaddr_un)) == 0) @@ -709,8 +713,10 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); +/* if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) return memif_syscall_error_handler (errno); +*/ if ((ftruncate (r->fd, r->region_size)) == -1) return memif_syscall_error_handler (errno); diff --git a/src/socket.c b/src/socket.c index 0447569..5c4eb15 100644 --- a/src/socket.c +++ b/src/socket.c @@ -77,6 +77,7 @@ memif_msg_enq_ack (memif_connection_t *c) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); e->msg.type = MEMIF_MSG_TYPE_ACK; e->fd = -1; @@ -126,7 +127,9 @@ memif_msg_enq_init (memif_connection_t *c) (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); if (e == NULL) return memif_syscall_error_handler (errno); - + memset (e, 0, sizeof (memif_msg_queue_elt_t)); + + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_init_t *i = &e->msg.init; e->msg.type = MEMIF_MSG_TYPE_INIT; @@ -169,6 +172,7 @@ memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_add_region_t *ar = &e->msg.add_region; e->msg.type = MEMIF_MSG_TYPE_ADD_REGION; @@ -202,6 +206,7 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_add_ring_t *ar = &e->msg.add_ring; e->msg.type = MEMIF_MSG_TYPE_ADD_RING; @@ -246,6 +251,7 @@ memif_msg_enq_connect (memif_connection_t *c) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_connect_t *cm = &e->msg.connect; e->msg.type = MEMIF_MSG_TYPE_CONNECT; @@ -279,6 +285,7 @@ memif_msg_enq_connected (memif_connection_t *c) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_connected_t *cm = &e->msg.connected; e->msg.type = MEMIF_MSG_TYPE_CONNECTED; From e7cb9b2b7c3f16feae5ed11dbf1658dcfa203f46 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 19 Jul 2017 12:45:41 +0200 Subject: [PATCH 19/63] README.md + additional example app comments --- README.md | 127 +++++++++++++++++++++++++++++++++ examples/icmp_responder/main.c | 31 ++++++-- src/libmemif.h | 12 ++-- 3 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6692fd5 --- /dev/null +++ b/README.md @@ -0,0 +1,127 @@ +Shared Memory Packet Interface (memif) Library +============================================== +## Introduction + +Shared memory packet interface (memif) provides high performance packet transmit and receive between user application and Vector Packet Processing (VPP). + +## Work in progress + +- [x] Slave mode + - [x] Connect to VPP over memif + - [x] ICMP responder example app +- [x] Transmit/receive packets +- [x] Interrupt mode support +- [ ] File descriptor event polling in libmemif (optional) + - [ ] Simplify file descriptor event polling (one handler for control and interrupt channel) +- [ ] Multiple connections +- [ ] Multiple regions +- [ ] Multipe queues + - [ ] Multithread support +- [ ] Master mode +- [ ] Performance testing +- [ ] Documentation + + +## Getting started + +#### Instalation + +Clone repository to your local machine. From root directory execute: + +For debug build: +``` +# ./bootstrap +# ./configure +# make +# make install +``` + +For release build: +``` +# ./bootstrap +# ./configure +# make release +# make install +``` +Verify installation: +``` +# ./.libs/icmp_responder +``` +> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies succesfull build. Default install path is /usr/lib. +Use _help_ command to display build information and commands: +``` +LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) +============================== +libmemif version: 1.0 (debug) +memif version: 256 +commands: + help - prints this help + exit - exit app + conn - create memif (slave-mode) + del - delete memif + show - show connection details +``` + +#### Connecting to VPP + +For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) + +1. Initialize memif + - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t + - Call memif initialization function. memif_init + +> If event occures on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. + +> Mmeif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. + +2. Creating interface + - Declare memif conenction handle. memif\_conn\_handle\_t + - Specify connection arguments. memif\_conn\_args\_t + - Declare callback functions called on connected/disconencted status changed. memif\_connection\_update\_t + - Call memif interface create function. memif\_create +> Arms timer file descriptor. + +3. Connection establishment + - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback.. + - On event call memif\_control\_fd\_handler. + - Everything else regarding connection establishment will be done internally. + - Once connection has been established, a callback will inform the user about connection status change. + +4. Interrupt packet receive + - Interrupt mode is still Work In Progress and will be simplified in furure patch. + - For interrupt mode, user application is required to register interrupt file descriptor for event polling. Api call memif\_get\_queue\_efd will return this interrupt file descriptor. + - If event occures on this file descriptor, there are packets in shared memory to be received. + +6. Memif buffers + - Packet data are stored in memif\_buffer\_t. Pointer _data_ points to shared memory buffer, and unsigned integer *data\_len* contains packet data length. + +5. Packet receive + - Api call memif\_rx\_burst will set all required fields in memif buffers provided by user application. + - User application can then process packets. + - Api call memif\_buffer\_free will make supplied memif buffers ready for next receive and mark shared memory buffers as free. + +6. Packet transmit + - Api call memif\_buffer\_alloc will set all required fields in memif buffers provided by user application. + - User application can populate shared memory buffers with packets. + - Api call memif\_tx\_burst will inform peer interface (master memif on VPP) that there are packets ready to receive and mark memif buffers as free. + +7. Helper functions + - Memif details + - Api call memif\_get\_details will return details about connection. + - Memif error messages + - Every api call returns error code (integer value) maped to error string. + - Call memif\_strerror will return error message assigned to specific error code. + - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. + + +#### Example app: + +- [ICMP Responder](examples/icmp_responder/main.c) + +VPP side config: +``` +# create memif id 0 master +# set int state memif0 up +# set int ip address memif0 192.168.1.1/24 +# ping 192.168.1.2 +``` diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 24ac0ef..40f6bad 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -66,6 +66,7 @@ printf ("\n"); \ } while (0) +/* maximum tx/rx memif buffers */ #define MAX_MEMIF_BUFS 256 int epfd; @@ -89,10 +90,12 @@ typedef struct int_fd_t *int_fd; /* tx buffers */ memif_buffer_t *tx_bufs; + /* allocated tx buffers counter */ /* number of tx buffers pointing to shared memory */ uint16_t tx_buf_num; /* rx buffers */ memif_buffer_t *rx_bufs; + /* allcoated rx buffers counter */ /* number of rx buffers pointing to shared memory */ uint16_t rx_buf_num; } memif_connection_t; @@ -235,9 +238,12 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) memif_connection_t *c = &memif_connection; INFO ("memif connected!"); int err; - err = memif_get_queue_efd (c->conn, 0, &c->int_fd->fd); + uint16_t qid = 0; + /* get interrupt file descriptor for queue specified by qid */ + err = memif_get_queue_efd (c->conn, qid, &c->int_fd->fd); INFO ("memif_get_queue_efd: %s", memif_strerror (err)); c->int_fd->qid = 0; + /* add interrupt fd to epoll */ return add_epoll_fd (c->int_fd->fd, EPOLLIN); } @@ -250,10 +256,12 @@ on_disconnect (memif_conn_handle_t conn, void *private_ctx) return 0; } -/* user needs to watch new fd or stop watching fd that is about to be closed */ +/* 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 (fd); @@ -286,6 +294,7 @@ icmpr_memif_create (int is_master) 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 = 0; /* last argument for memif_create (void * private_ctx) is used by user @@ -334,6 +343,7 @@ icmpr_buffer_alloc (long n) memif_connection_t *c = &memif_connection; int err; uint16_t r, qid = 0; + /* 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); INFO ("memif_buffer_alloc: %s", memif_strerror (err)); c->tx_buf_num += r; @@ -347,6 +357,8 @@ icmpr_tx_burst () memif_connection_t *c = &memif_connection; int err; uint16_t r, qid = 0; + /* inform peer memif interface about data in shared memory buffers */ + /* mark memif buffers as free */ err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); INFO ("memif_tx_burst: %s", memif_strerror (err)); DBG ("tx: %d/%u", r, c->tx_buf_num); @@ -357,6 +369,7 @@ icmpr_tx_burst () int icmpr_free () { + /* application cleanup */ memif_connection_t *c = &memif_connection; free (c->int_fd); c->int_fd = NULL; @@ -419,13 +432,9 @@ int icmpr_interrupt (int fd) { memif_connection_t *c = &memif_connection; -/* DBG ("interrupted!"); - uint64_t b; - ssize_t r = read (fd, &b, sizeof (b)); -*/ - int err; uint16_t rx; + /* receive data from shared memory buffers */ err = memif_rx_burst (c->conn, 0, c->rx_bufs, MAX_MEMIF_BUFS, &rx); INFO ("memif_rx_burst: %s", memif_strerror (err)); c->rx_buf_num += rx; @@ -443,6 +452,7 @@ icmpr_interrupt (int fd) } uint16_t fb; + /* mark memif buffers and shared memory buffers as free */ err = memif_buffer_free (c->conn, 0, c->rx_bufs, rx, &fb); INFO ("memif_buffer_free: %s", memif_strerror (err)); c->rx_buf_num -= fb; @@ -478,12 +488,17 @@ poll_event (int timeout) /* this app does not use any other file descriptors than stds and memif control fds */ if ( evt.data.fd > 2) { + /* event on memif interrupt fd */ if (evt.data.fd == c->int_fd->fd) { + /* WIP */ + /* in future patch there will be one handler and callback on interrupt */ icmpr_interrupt (evt.data.fd); } else { + /* event of memif control fd */ + /* convert epolle events to memif events */ if (evt.events & EPOLLIN) events |= MEMIF_FD_EVENT_READ; if (evt.events & EPOLLOUT) @@ -538,6 +553,8 @@ int main () err = memif_init (control_fd_update); INFO ("memif_init: %s", memif_strerror (err)); + print_help (); + /* main loop */ while (1) { diff --git a/src/libmemif.h b/src/libmemif.h index 8da178f..a788a0c 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -118,13 +118,13 @@ typedef int (memif_connection_update_t) (memif_conn_handle_t conn, void *private */ typedef struct { - uint8_t *socket_filename; - uint8_t secret[24]; /* optional */ + uint8_t *socket_filename; /* default = /run/vpp/memif.sock */ + uint8_t secret[24]; /* optional (interface authentication) */ - uint8_t num_s2m_rings; - uint8_t num_m2s_rings; - uint16_t buffer_size; - memif_log2_ring_size_t log2_ring_size; + 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; From d74d8bfa1d4800778aaa9e72009001f3050aebcf Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:40:54 +0200 Subject: [PATCH 20/63] add ring msg bug-fix --- src/socket.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/socket.c b/src/socket.c index bd54d60..132314d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -206,6 +206,7 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) e->fd = mq->int_fd; ar->index = index; ar->offset = mq->offset; + ar->region = index; ar->log2_ring_size = mq->log2_ring_size; ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0; @@ -488,7 +489,7 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) if (err < 0) return err; - strncpy ((char *) c->remote_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); @@ -506,7 +507,7 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) if (err < 0) return err; - strncpy ((char *) c->remote_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); From 8fd2280ef70d0e54e5a1d26d60aee8763d2807b2 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:42:56 +0200 Subject: [PATCH 21/63] ODPM-413 --- src/libmemif.h | 40 +++++++++ src/main.c | 192 ++++++++++++++++++++++++++++++++++++++++++++ src/memif_private.h | 20 +++-- 3 files changed, 246 insertions(+), 6 deletions(-) diff --git a/src/libmemif.h b/src/libmemif.h index 3787b5e..3d6b110 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -32,6 +32,8 @@ #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) typedef void* memif_conn_handle_t; @@ -72,6 +74,37 @@ typedef struct memif_interface_mode_t mode:8; } memif_conn_args_t; +typedef struct +{ + uint16_t desc_index; + uint32_t buffer_len; + uint32_t data_len; + void *data; +} memif_buffer_t; + +typedef struct +{ + uint8_t if_name[32]; + uint8_t inst_name[32]; + uint8_t remote_if_name[32]; + uint8_t remote_inst_name[32]; + + uint32_t id; + uint8_t secret[24]; /* optional */ + uint8_t role; /* 0 = master, 1 = slave */ + uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */ + uint8_t socket_filename[128]; + uint32_t ring_size; + uint16_t buffer_size; + uint8_t rx_queues; + uint8_t tx_queues; + + uint8_t link_up_down; /* 1 = up, 0 = down */ +} memif_details_t; + + +memif_details_t memif_get_details (memif_conn_handle_t conn); + /** \brief Memif initialization @param on_control_fd_update - if control fd updates inform user to watch new fd @@ -135,4 +168,11 @@ int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid); */ int memif_delete (memif_conn_handle_t *conn); +int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); + +int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); + + #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index ee3ed3e..a710e51 100644 --- a/src/main.c +++ b/src/main.c @@ -172,6 +172,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->on_connect = on_connect; conn->on_disconnect = on_disconnect; conn->private_ctx = private_ctx; + conn->alloc_buf_num = 0; uint8_t l = strlen ((char *) args->interface_name); strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, l); @@ -413,11 +414,23 @@ memif_disconnect_internal (memif_connection_t *c) /* TODO: support multiple rings */ if (c->tx_queues != NULL) { + if (c->tx_queues->int_fd > 0) + { + lm->control_fd_update (c->tx_queues->int_fd, MEMIF_FD_EVENT_DEL); + close (c->tx_queues->int_fd); + } + c->tx_queues->int_fd = -1; free (c->tx_queues); c->tx_queues = NULL; } if (c->rx_queues != NULL) { + if (c->rx_queues->int_fd > 0) + { + lm->control_fd_update (c->rx_queues->int_fd, MEMIF_FD_EVENT_DEL); + close (c->rx_queues->int_fd); + } + c->rx_queues->int_fd = -1; free (c->rx_queues); c->rx_queues = NULL; } @@ -479,6 +492,8 @@ memif_delete (memif_conn_handle_t *conn) int memif_connect1 (memif_connection_t *c) { + libmemif_main_t *lm = &libmemif_main; + memif_region_t *mr = c->regions; /* TODO: support multiple regions */ if (mr != NULL) @@ -513,6 +528,9 @@ memif_connect1 (memif_connection_t *c) error_return ("wrong cookie on tx ring %u", i); i++; } + + lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); + return 0; } @@ -605,3 +623,177 @@ memif_init_regions_and_queues (memif_connection_t *conn) return 0; } + +int +memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->tx_queues; + memif_ring_t *ring = mq->ring; + memif_buffer_t *b0, *b1; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint16_t i = 0, s0, s1, ns; + + if (ring->tail != ring->head) + { + if (ring->head > ring->tail) + ns = (1 << mq->log2_ring_size) - ring->head + ring->tail; + else + ns = ring->tail - ring->head; + } + else + ns = (1 << mq->log2_ring_size); + + /* if head == tail receive function will asume that no packets are available */ + ns -= 1; + + while (count && ns) + { + while ((count > 2) && (ns > 2)) + { + s0 = (ring->head + c->alloc_buf_num + i) & mask; + s1 = (ring->head + c->alloc_buf_num + i + 1) & mask; + + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + b0->desc_index = s0; + b1->desc_index = s1; + b0->buffer_len = ring->desc[s0].buffer_length; + b1->buffer_len = ring->desc[s1].buffer_length; + /* 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; + + (*(bufs + i)) = b0; + (*(bufs + i + 1)) = b1; + DBG ("allocated ring slots %u, %u", s0, s1); + count -= 2; + ns -= 2; + i += 2; + } + s0 = (ring->head + c->alloc_buf_num + i) & mask; + + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + b0->desc_index = s0; + b0->buffer_len = ring->desc[s0].buffer_length; + b0->data = c->regions->shm + ring->desc[s0].offset; + + (*(bufs + i)) = b0; + DBG ("allocated ring slot %u", s0); + count--; + ns--; + i++; + } + + c->alloc_buf_num += i; + + DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, c->alloc_buf_num); + + if (count) + DBG ("ring buffer full! qid: %u", qid); + + return i; +} + +int +memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->rx_queues; + memif_ring_t *ring = mq->ring; + uint16_t tail = ring->tail; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + int i = 0; + memif_buffer_t *b0, *b1; + + while (count) + { + while (count > 2) + { + b0 = (*(bufs + i)); + b1 = (*(bufs + i + 1)); + tail = (b0->desc_index + 1) & mask; + tail = (b1->desc_index + 1) & mask; + b0->data = NULL; + b1->data = NULL; + free (b0); + free (b1); + (*(bufs + i)) = b0 = NULL; + (*(bufs + i + 1)) = b1 = NULL; + + + count -= 2; + i += 2; + } + b0 = (*(bufs + i)); + tail = (b0->desc_index + 1) & mask; + b0->data = NULL; + free (b0); + (*(bufs + i)) = b0 = NULL; + + count--; + i++; + } + + ring->tail = tail; + + return i; +} + +int +memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid) +{ + memif_connection_t *c = (memif_connection_t *) conn; + return c->rx_queues->int_fd; +} + +memif_details_t +memif_get_details (memif_conn_handle_t conn) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_details_t md; + memset (&md, 0, sizeof (md)); + /* interface name */ + strncpy ((char *) md.if_name, (char *) c->args.interface_name, + strlen ((char *) c->args.interface_name)); + /* instance name */ + strncpy ((char *) md.inst_name, (char *) c->args.instance_name, + strlen ((char *) c->args.instance_name)); + /* remote interface name */ + strncpy ((char *) md.remote_if_name, (char *) c->remote_if_name, + strlen ((char *) c->remote_if_name)); + /* remote instance name */ + strncpy ((char *) md.remote_inst_name, (char *) c->remote_name, + strlen ((char *) c->remote_name)); + /* interface id */ + md.id = c->args.interface_id; + /* secret */ + if (c->args.secret) + strncpy ((char *) md.secret, (char *) c->args.secret, + strlen ((char *) c->args.secret)); + /* role */ + md.role = (c->args.is_master) ? 0 : 1; + /* mode */ + md.mode = c->args.mode; + /* socket filename */ + uint32_t l = strlen ((char *) c->args.socket_filename); + if (l > 128) + l = 128; + strncpy ((char *) md.socket_filename, (char *) c->args.socket_filename, l); + /* ring size */ + md.ring_size = (1 << c->args.log2_ring_size); + /* buffer size */ + md.buffer_size = c->args.buffer_size; + /* rx queues */ + md.rx_queues = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + /* tx queues */ + md.tx_queues = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + /* link up down */ + md.link_up_down = (c->fd > 0) ? 1 : 0; + + return md; +} diff --git a/src/memif_private.h b/src/memif_private.h index bc4c6d2..01c077d 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -43,9 +43,8 @@ #define MEMIF_MAX_FDS 512 -#define MEMIF_DEBUG 1 -#if MEMIF_DEBUG == 1 +#ifdef MEMIF_DBG #define DBG(...) do { \ printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \ printf(__VA_ARGS__); \ @@ -58,20 +57,27 @@ printf("\n"); \ } while (0) +#define error_return_unix(...) do { \ + DBG_UNIX(__VA_ARGS__); \ + return -1; \ + } while (0) +#define error_return(...) do { \ + DBG(__VA_ARGS__); \ + return -1; \ + } while (0) #else #define DBG(...) #define DBG_UNIX(...) -#endif /* MEMIF_DEBUG */ - #define error_return_unix(...) do { \ - DBG_UNIX(__VA_ARGS__); \ return -1; \ } while (0) #define error_return(...) do { \ - DBG(__VA_ARGS__); \ return -1; \ } while (0) +#endif /* MEMIF_DBG */ + + typedef struct { void *shm; @@ -131,6 +137,8 @@ typedef struct memif_connection memif_queue_t *rx_queues; memif_queue_t *tx_queues; + uint32_t alloc_buf_num; + uint16_t flags; #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) } memif_connection_t; From dbe578b1d781ab4cf24e1e40b3ef220251561238 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:44:53 +0200 Subject: [PATCH 22/63] ODPM-414 --- src/libmemif.h | 5 ++ src/main.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/src/libmemif.h b/src/libmemif.h index 3d6b110..4ce0470 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -174,5 +174,10 @@ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t **bufs, uint16_t count); +int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); + +int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index a710e51..c8b0d5d 100644 --- a/src/main.c +++ b/src/main.c @@ -78,6 +78,26 @@ typedef struct libmemif_main_t libmemif_main; +#define DBG_TX_BUF (0) +#define DBG_RX_BUF (1) + +static void +print_bytes (void *data, uint16_t len, uint8_t q) +{ + if (q == DBG_TX_BUF) + printf ("\nTX:\n\t"); + else + printf ("\nRX:\n\t"); + int i; + for (i = 0; i < len; i++) + { + if (i % 8 == 0) + printf ("\n%d:\t", i); + printf ("%02X ", ((uint8_t *) (data)) [i]); + } + printf ("\n\n"); +} + static void print_conn (memif_connection_t *c) { @@ -744,6 +764,152 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, return i; } +int +memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->tx_queues; + memif_ring_t *ring = mq->ring; + uint16_t head = ring->head; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint16_t tx = 0; + memif_buffer_t *b0, *b1; + + 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; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_TX_BUF); + print_bytes (b1->data , b1->data_len, DBG_TX_BUF); +#endif + + head = (b0->desc_index + 1) & mask; + head = (b1->desc_index + 1) & mask; + + b0->data = NULL; + b0->data_len = 0; + b1->data = NULL; + b1->data_len = 0; + + count -= 2; + tx += 2; + } + + b0 = (*(bufs + tx)); + ring->desc[b0->desc_index].length = b0->data_len; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_TX_BUF); +#endif + + head = (b0->desc_index + 1) & mask; + + b0->data = NULL; + b0->data_len = 0; + + count--; + tx++; + } + ring->head = head; + + c->alloc_buf_num -= tx; + + if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) + { + uint64_t a = 1; + int r = write (mq->int_fd, &a, sizeof (a)); + if (r < 0) + error_return ("write: %s fd %d", strerror (errno), mq->int_fd); + } + + return tx; +} + +int +memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t **bufs, uint16_t count) +{ + memif_connection_t *c = (memif_connection_t *) conn; + memif_queue_t *mq = c->rx_queues; + memif_ring_t *ring = mq->ring; + uint16_t head = ring->head; + uint16_t ns, rx = 0; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + memif_buffer_t *b0, *b1; + + if (head == mq->last_head) + return 0; + + if (head > mq->last_head) + ns = head - mq->last_head; + else + ns = (1 << mq->log2_ring_size) - mq->last_head + head; + + while (ns && count) + { + while ((ns > 2) && (count > 2)) + { + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + 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; + + (*(bufs + rx)) = b0; + (*(bufs + rx + 1)) = b1; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_RX_BUF); + print_bytes (b1->data , b1->data_len, DBG_RX_BUF); +#endif + + mq->last_head = (mq->last_head + 2) & mask; + + ns -= 2; + count -= 2; + rx += 2; + } + b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + + 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; + + (*(bufs + rx)) = b0; + +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data , b0->data_len, DBG_RX_BUF); +#endif + + mq->last_head = (mq->last_head + 1) & mask; + + ns--; + count--; + rx++; + } + + if (ns) + { + DBG ("ring buffer full"); + } + + return rx; +} + int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid) { From 2d9824c2c393380fed2cbf58982acd3df3886eba Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 10:46:25 +0200 Subject: [PATCH 23/63] ODPM-417 --- Makefile.am | 12 +- examples/icmp_responder/icmp_proto.c | 225 +++++++++++++++++++++++++ examples/icmp_responder/icmp_proto.h | 8 + examples/icmp_responder/main.c | 239 ++++++++++++++++++++++++--- 4 files changed, 462 insertions(+), 22 deletions(-) create mode 100644 examples/icmp_responder/icmp_proto.c create mode 100644 examples/icmp_responder/icmp_proto.h diff --git a/Makefile.am b/Makefile.am index 08d21be..b663fcf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,6 +15,12 @@ AUTOMAKE_OPTIONS = foreign subdir-objects ACLOCAL_AMFLAGS = -I m4 +AM_CPPFLAGS = -g -DMEMIF_DBG -DICMP_DBG + +.PHONY: release +release: + $(MAKE) AM_CPPFLAGS="-O3" + # # unit_test # @@ -31,14 +37,14 @@ unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST # main lib # libmemif_la_SOURCES = src/main.c src/socket.c -libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -ggdb +libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc # # ICMP responder example # -icmp_responder_SOURCES = examples/icmp_responder/main.c +icmp_responder_SOURCES = examples/icmp_responder/main.c examples/icmp_responder/icmp_proto.c icmp_responder_LDADD = libmemif.la -icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -ggdb +icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder noinst_PROGRAMS = icmp_responder diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c new file mode 100644 index 0000000..689dc5e --- /dev/null +++ b/examples/icmp_responder/icmp_proto.c @@ -0,0 +1,225 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static uint16_t +cksum (void *addr, ssize_t len) +{ + char* data = (char *)addr; + + uint32_t acc=0xffff; + + ssize_t i; + for (i = 0; (i + 1) < len; i += 2) + { + uint16_t word; + memcpy (&word, data + i, 2); + acc += ntohs (word); + if (acc > 0xffff) + acc -= 0xffff; + } + + if (len & 1) + { + uint16_t word = 0; + memcpy (&word, data + len - 1, 1); + acc += ntohs (word); + if (acc > 0xffff) + acc -= 0xffff; + } + return htons (~acc); +} + +int +print_packet (void * pck) +{ + if (pck == NULL) + { + printf ("ICMP_PROTO: no data\n"); + return -1; + } + struct iphdr *ip; + struct icmphdr *icmp; + ip = (struct iphdr *) pck; + icmp = (struct icmphdr *) (pck + sizeof (struct iphdr)); + printf ("received packet:\n"); + printf ("\tiphdr:\n"); + printf ("\t\tihl: %u\n\t\tversion: %u\n\t\tlen: %u\n\t\tid: %u\n", + ip->ihl, ip->version, __bswap_16(ip->tot_len), ip->id); + printf ("\t\tprotocol: %u\n", ip->protocol); + + printf ("\t\tsaddr: "); + int i; + for (i = 0; i < 4; i++) + { + printf ("%u.", ((uint8_t *) &ip->saddr)[i]); + } + printf ("\n"); + + printf ("\t\tdaddr: "); + for (i = 0; i < 4; i++) + { + printf ("%u.", ((uint8_t *) &ip->daddr)[i]); + } + printf ("\n"); + printf ("\ticmphdr:\n"); + printf ("\t\ttype: %s\n", (icmp->type == ICMP_ECHO) ? "ICMP_ECHO" : "ICMP_ECHOREPLY"); + + return 0; +} + +static ssize_t +resolve_arp (void *arp) +{ + struct arphdr *resp = (struct arphdr *) arp; + + resp->ar_hrd = __bswap_16 (ARPHRD_ETHER); + + resp->ar_pro = __bswap_16 (0x0800); + + resp->ar_hln = 6; + resp->ar_pln = 4; + + resp->ar_op = __bswap_16 (ARPOP_REPLY); + + return sizeof (struct arphdr); +} + +static ssize_t +resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp) +{ + struct ether_arp *resp = (struct ether_arp *) eth_arp_resp; + + resolve_arp (&resp->ea_hdr); + + memcpy (resp->arp_tha, eth_arp->arp_sha, 6); + memcpy (resp->arp_tpa, eth_arp->arp_spa, 4); + + memcpy (resp->arp_sha, (((struct ether_header *) ( + eth_arp_resp - sizeof (struct ether_header)))->ether_shost), 6); + + uint8_t ip_addr[4]; + ip_addr[0] = 192; + ip_addr[1] = 168; + ip_addr[2] = 1; + ip_addr[3] = 2; + memcpy (resp->arp_spa, ip_addr, 4); + + return sizeof (struct ether_arp); +} + +static ssize_t +resolve_eth (struct ether_header *eth, void *eth_resp) +{ + struct ether_header *resp = (struct ether_header *) eth_resp; + memcpy (resp->ether_dhost, eth->ether_shost, 6); + + uint8_t hw_addr[6]; + int i; + for (i = 0; i < 6; i++) + { + hw_addr[i] = 'a'; + } + memcpy (resp->ether_shost, hw_addr, 6); + + resp->ether_type = eth->ether_type; + + return sizeof (struct ether_header); +} + +static ssize_t +resolve_ip (struct iphdr *ip, void *ip_resp) +{ + struct iphdr *resp = (struct iphdr *) ip_resp; + resp->ihl = 5; + resp->version = 4; + resp->tos = 0; + /*len updated later */ + resp->tot_len = 0x5400; + resp->id = 0; + resp->frag_off = 0; + resp->ttl = 0x40; + resp->protocol = 1; + resp->saddr = 0xC0A80102; + resp->saddr = __bswap_32 (resp->saddr); + resp->daddr = 0xC0A80101; + resp->daddr = __bswap_32 (resp->daddr); + + resp->check = cksum (resp, sizeof (struct iphdr)); + + return sizeof (struct iphdr); +} + +static ssize_t +resolve_icmp (struct icmphdr *icmp, void *icmp_resp) +{ + struct icmphdr *resp = (struct icmphdr *) icmp_resp; + resp->type = ICMP_ECHOREPLY; + resp->code = 0; + resp->un.echo.id = icmp->un.echo.id; + resp->un.echo.sequence = icmp->un.echo.sequence; + + /*resp->checksum = cksum (resp, sizeof (struct icmphdr));*/ + + return sizeof (struct icmphdr); +} + +int +resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size) +{ + struct ether_header *eh; + struct ether_arp *eah; + struct iphdr *ip; + struct icmphdr *icmp; + *out_size = 0; + + eh = (struct ether_header *) in_pck; + *out_size = resolve_eth (eh, out_pck); + + if (eh->ether_type == 0x0608) + { + eah = (struct ether_arp *) (in_pck + *out_size); + *out_size += resolve_eth_arp (eah, out_pck + *out_size); + + } + else if (eh->ether_type == 0x0008) + { +#ifdef ICMP_DBG + print_packet (in_pck + *out_size); +#endif + ip = (struct iphdr *) (in_pck + *out_size); + *out_size += resolve_ip (ip, out_pck + *out_size); + if (ip->protocol == 1) + { + icmp = (struct icmphdr *) (in_pck + *out_size); + *out_size += resolve_icmp (icmp, out_pck + *out_size); + ((struct icmphdr *)(out_pck + *out_size - sizeof (struct icmphdr)))->checksum = + cksum (out_pck + *out_size - sizeof (struct icmphdr), sizeof (struct icmphdr)); + /* payload */ + memcpy (out_pck + *out_size, in_pck + *out_size, in_size - *out_size); + *out_size = in_size; + } + } + return 0; +} diff --git a/examples/icmp_responder/icmp_proto.h b/examples/icmp_responder/icmp_proto.h new file mode 100644 index 0000000..ec5ae70 --- /dev/null +++ b/examples/icmp_responder/icmp_proto.h @@ -0,0 +1,8 @@ +#ifndef _ICMP_PROTO_H_ +#define _ICMP_PROTO_H_ + +int resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size); + +int print_packet (void *pck); + +#endif /* _ICMP_PROTO_H_ */ diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index e2fcf23..f78d7b7 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -42,22 +42,113 @@ #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) int epfd; -memif_conn_handle_t conn; -/* TODO: memif connection status report (memif_details...) */ +/* + * WIP + */ +/* interrupt fd specific for queue */ +typedef struct +{ + uint16_t qid; + int fd; +} int_fd_t; + +typedef struct +{ + uint16_t index; + /* memif conenction handle */ + memif_conn_handle_t conn; + /* interrupt file descriptor (specific for each queue) */ + int_fd_t *int_fd; + /* tx buffers */ + memif_buffer_t **tx_bufs; + /* number of allocated tx buffers */ + uint16_t tx_buf_num; + /* rx buffers */ + memif_buffer_t **rx_bufs; + /* number of allocated rx buffers */ + uint16_t rx_buf_num; +} memif_connection_t; + +memif_connection_t memif_connection; + +static void +print_memif_details () +{ + memif_connection_t *c = &memif_connection; + printf ("MEMIF DETAILS\n"); + printf ("==============================\n"); + + /* TODO: loop for all connections */ + if (c->conn == NULL) + { + printf ("no connection!\n"); + return; + } + memif_details_t md = memif_get_details (c->conn); + 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 ("\tring_size: %u\n", md.ring_size); + printf ("\tbuffer_size: %u\n", md.buffer_size); + printf ("\trx queues: %u\n", md.rx_queues); + printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\tlink: "); + if (md.link_up_down) + printf ("up\n"); + else + printf ("down\n"); +} int add_epoll_fd (int fd, uint32_t events) @@ -80,6 +171,27 @@ add_epoll_fd (int fd, uint32_t events) return 0; } +int +mod_epoll_fd (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 fd) { @@ -104,8 +216,12 @@ del_epoll_fd (int fd) int on_connect (memif_conn_handle_t conn, void *private_ctx) { - DBG ("connected!"); - return 0; + INFO ("memif connected!"); + int_fd_t *ifd = (int_fd_t *) malloc (sizeof (int_fd_t)); + ifd->fd = memif_get_queue_efd ((&memif_connection)->conn, 0); + ifd->qid = 0; + (&memif_connection)->int_fd = ifd; + return add_epoll_fd (ifd->fd, EPOLLIN); } /* informs user about disconnected status. private_ctx is used by user to identify connection @@ -113,7 +229,7 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) int on_disconnect (memif_conn_handle_t conn, void *private_ctx) { - DBG ("disconnected!"); + INFO ("memif disconnected!"); return 0; } @@ -130,6 +246,9 @@ control_fd_update (int fd, uint8_t events) if (events & MEMIF_FD_EVENT_WRITE) evt |= EPOLLOUT; + if (events & MEMIF_FD_EVENT_MOD) + return mod_epoll_fd (fd, evt); + return add_epoll_fd (fd, evt); } @@ -154,7 +273,7 @@ icmpr_memif_create (int is_master) args.interface_id = 0; /* last argument for memif_create (void * private_ctx) is used by user to identify connection. this context is returned with callbacks */ - int rv = memif_create (&conn, &args, on_connect, on_disconnect, NULL); + int rv = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, NULL); if (rv < 0) { DBG ("memif create error!"); @@ -171,7 +290,7 @@ icmpr_memif_delete () { int rv = 0; /* disconenct then delete memif connection */ - rv = memif_delete (&conn); + rv = memif_delete (&(&memif_connection)->conn); if (rv < 0) { DBG ("memif delete error!"); @@ -186,15 +305,46 @@ icmpr_memif_delete () void print_help () { - printf ("LIBMEMIF EXAMPLE APP: %s\n", APP_NAME); + printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME); +#ifdef ICMP_DBG + printf (" (debug)"); +#endif + printf ("\n"); printf ("==============================\n"); - printf ("libmemif version: %s\n", LIBMEMIF_VERSION); + 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 - print memif details\n"); +} + +int +icmpr_buffer_alloc (long n) +{ + memif_connection_t *c = &memif_connection; + c->tx_bufs = (memif_buffer_t **) malloc (sizeof (memif_buffer_t *) * n); + DBG ("call memif_buffer_alloc"); + int r = memif_buffer_alloc (c->conn, 0, c->tx_bufs, n); + DBG ("allocated %d/%ld buffers", r, n); + c->tx_buf_num += r; + return 0; +} + +int +icmpr_tx_burst () +{ + memif_connection_t *c = &memif_connection; + int r = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num); + DBG ("tx: %d/%u", r, c->tx_buf_num); + c->tx_buf_num -= r; + return 0; } int @@ -226,19 +376,58 @@ user_input_handler () icmpr_memif_delete (); return 0; } + else if (strncmp (ui, "show", 4) == 0) + { + print_memif_details (); + } else DBG ("unknown command: %s", ui); return 0; } +int +icmpr_interrupt (int fd) +{ + memif_connection_t *c = &memif_connection; + DBG ("interrupted!"); + uint64_t b; + ssize_t r = read (fd, &b, sizeof (b)); + + int rx = memif_rx_burst (c->conn, 0, c->rx_bufs, c->rx_buf_num); + c->rx_buf_num -= rx; + + DBG ("received %d buffers. %u free buffers", rx, c->rx_buf_num); + + icmpr_buffer_alloc (rx); + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (*(c->rx_bufs + i))->data, + (*(c->rx_bufs + i))->data_len, (void *) (*(c->tx_bufs + i))->data, + &(*(c->tx_bufs + i))->data_len); + } + + int fb = memif_buffer_free (c->conn, 0, c->rx_bufs, rx); + c->rx_buf_num += fb; + + DBG ("freed %d buffers. %u free buffers", fb, c->rx_buf_num); + + icmpr_tx_burst (); + + return 0; +} + int poll_event (int timeout) { + memif_connection_t *c = &memif_connection; 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; en = epoll_pwait (epfd, &evt, 1, timeout, 0); if (en < 0) { @@ -250,14 +439,20 @@ poll_event (int timeout) /* this app does not use any other file descriptors than stds and memif control fds */ if ( evt.data.fd > 2) { - uint32_t events = 0; - 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 (evt.data.fd == c->int_fd->fd) + { + icmpr_interrupt (evt.data.fd); + } + else + { + 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); + } } else if (evt.data.fd == 0) { @@ -286,8 +481,14 @@ int main () epfd = epoll_create (1); add_epoll_fd (0, EPOLLIN); + memif_connection_t *c = &memif_connection; + /* initialize global memif connection handle */ - conn = NULL; + c->conn = NULL; + c->int_fd = malloc (sizeof (int_fd_t)); + c->int_fd->fd = -1; + c->rx_buf_num = 256; + c->rx_bufs = malloc (sizeof (memif_buffer_t *) * c->rx_buf_num); /* initialize memory interface */ memif_init (control_fd_update); @@ -295,7 +496,7 @@ int main () /* main loop */ while (1) { - if (poll_event (0) < 0) + if (poll_event (-1) < 0) { DBG ("poll_event error!"); } From c8296d0d67e8242761987bb3eadd0382801f0fa8 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 13 Jul 2017 12:55:31 +0200 Subject: [PATCH 24/63] shm buffer alloc fix --- src/main.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index c8b0d5d..66f6264 100644 --- a/src/main.c +++ b/src/main.c @@ -192,7 +192,6 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->on_connect = on_connect; conn->on_disconnect = on_disconnect; conn->private_ctx = private_ctx; - conn->alloc_buf_num = 0; uint8_t l = strlen ((char *) args->interface_name); strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, l); @@ -625,6 +624,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) mq->log2_ring_size = conn->args.log2_ring_size; mq->region = 0; mq->offset = (void *) mq->ring - (void *) conn->regions->shm; + mq->alloc_bufs = 0; conn->tx_queues = mq; } @@ -638,6 +638,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) mq->region = 0; mq->offset = (void *) mq->ring - (void *) conn->regions->shm; mq->last_head = 0; + mq->alloc_bufs = 0; conn->rx_queues = mq; } @@ -672,8 +673,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, { while ((count > 2) && (ns > 2)) { - s0 = (ring->head + c->alloc_buf_num + i) & mask; - s1 = (ring->head + c->alloc_buf_num + i + 1) & mask; + s0 = (ring->head + mq->alloc_bufs + i) & mask; + s1 = (ring->head + mq->alloc_bufs + i + 1) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); @@ -693,7 +694,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, ns -= 2; i += 2; } - s0 = (ring->head + c->alloc_buf_num + i) & mask; + s0 = (ring->head + mq->alloc_bufs + i) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); @@ -708,9 +709,9 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, i++; } - c->alloc_buf_num += i; + mq->alloc_bufs += i; - DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, c->alloc_buf_num); + DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, mq->alloc_bufs); if (count) DBG ("ring buffer full! qid: %u", qid); @@ -819,8 +820,8 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, } ring->head = head; - c->alloc_buf_num -= tx; - + mq->alloc_bufs -= tx; + if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) { uint64_t a = 1; From a27a8432da4ed7c952265ff22d22524341cd609e Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 17 Jul 2017 15:53:21 +0200 Subject: [PATCH 25/63] error code->msg --- src/libmemif.h | 98 +++++++++- src/main.c | 465 ++++++++++++++++++++++++++++++-------------- src/memif_private.h | 64 +++++- src/socket.c | 230 +++++++++++++--------- src/socket.h | 10 +- 5 files changed, 621 insertions(+), 246 deletions(-) diff --git a/src/libmemif.h b/src/libmemif.h index 4ce0470..363a4b3 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -24,6 +24,48 @@ #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_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_INT_WRITE, /* send interrupt error */ + MEMIF_ERR_MFMSG, /* malformed msg received */ +/* 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; + /* 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) @@ -102,12 +144,20 @@ typedef struct uint8_t link_up_down; /* 1 = up, 0 = down */ } memif_details_t; +char *memif_strerror (int err_code); memif_details_t memif_get_details (memif_conn_handle_t conn); /** \brief Memif initialization @param on_control_fd_update - if control fd updates inform user to watch new fd + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_FILE_LIMIT - system open file limit reached + MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached + MEMIF_ERR_BAD_FD - invalid fd + 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 @@ -123,6 +173,20 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update); creates memory interface. + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_ACCES - permission denied + MEMIF_ERR_FILE_LIMIT - system open file limit reached + MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached + MEMIF_ERR_AGAIN - fd is not socket, or operation would block + MEMIF_ERR_BAD_FD - invalid fd + MEMIF_ERR_NOMEM - out of memory + TODO: MEMIF_ERR_INVAL_ARG - invalid argument + MEMIF_ERR_CONN - connection already exists on this handle + MEMIF_ERR_FILE_NOT_SOCK - file specified by socket filename exists, + but it's not a socket + 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. @@ -142,6 +206,18 @@ int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args, memif_co if event occures on any control fd, call memif_control_fd_handler internal - lib will "identify" fd (timerfd, lsitener, control) and handle event accordingly + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_ACCES - permission denied + MEMIF_ERR_FILE_LIMIT - system open file limit reached + MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached + MEMIF_ERR_AGAIN - fd is not socket, or operation would block + MEMIF_ERR_BAD_FD - invalid fd + MEMIF_ERR_NOMEM - out of memory + MEMIF_ERR_NO_SHMFD - missing shared memory fd + MEMIF_ERR_COOKIE - invalid cookie on ring + fd type timerfd every disconnected memif in slave mode will request connection @@ -155,29 +231,39 @@ int memif_control_fd_handler (int fd, uint8_t events); /** \brief Memif get queue event file descriptor @param conn - memif connection handle @param qid - number identifying queue + @param efd - return interrupt fd for memif queue specified by qid - return interrupt fd for memif queue specified by qid + rerurn + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL */ -int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid); +int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd); /** \brief Memif delete @param conn - memif connection handle + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) + MEMIF_ERR_BAD_FD - invalid fd + disconnect session (free queues and regions, close file descriptors, unmap shared memory) set connection handle to NULL, to avoid possible double free */ int memif_delete (memif_conn_handle_t *conn); + int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *tx); int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count); + memif_buffer_t **bufs, uint16_t count, uint16_t *rx); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index 66f6264..ce4d20c 100644 --- a/src/main.c +++ b/src/main.c @@ -50,37 +50,103 @@ /* private structs and functions */ #include -/* - * WIP - */ -typedef struct -{ - int fd; - uint16_t use_count; - uint8_t *filename; -} memif_socket_t; - -/* - * WIP - */ -/* probably function like memif_cleanup () will need to be called - close timerfd, free struct libmemif_main and its nested structures */ -typedef struct -{ - memif_control_fd_update_t *control_fd_update; - int timerfd; - struct itimerspec arm, disarm; - - /* TODO: update to arrays support multiple connections */ - memif_socket_t ms; - memif_connection_t *conn; -} libmemif_main_t; +#define ERRLIST_LEN 33 +#define MAX_ERRBUF_LEN 256 libmemif_main_t libmemif_main; +static char memif_buf[MAX_ERRBUF_LEN]; + +const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ + "Success.", + /* MEMIF_ERR_SYSCALL */ + "Unspecified syscall error (build with -DMEMIF_DBG or make debug).", + /* MEMIF_ERR_ACCES */ + "Permission to resoure denied.", + /* MEMIF_ERR_FILE_LIMIT */ + "System limit on total numer of open files reached.", + /* MEMIF_ERR_PROC_FILE_LIMIT */ + "Per-process limit on total number of open files reached.", + /* MEMIF_ERR_ALREADY */ + "Connection already requested.", + /* MEMIF_ERR_AGAIN */ + "File descriptor refers to file other than socket, or operation would block.", + /* MEMIF_ERR_BAD_FD */ + "Bad file descriptor.", + /* MEMIF_ERR_NOMEM */ + "Out of memory.", + /* MEMIF_ERR_INVAL_ARG */ + "Invalid argument.", + /* MEMIF_ERR_NOCONN */ + "Memif connection handle does not point to existing conenction", + /* MEMIF_ERR_CONN */ + "Memif connection handle points to existing connection", + /* MEMIF_ERR_CB_FDUPDATE */ + "User defined callback memif_control_fd_update_t returned error", + /* MEMIF_ERR_FILE_NOT_SOCK */ + "File specified by socket filename exists and is not socket.", + /* MEMIF_ERR_NO_SHMFD */ + "Missing shared memory file descriptor. (internal error)", + /* MEMIF_ERR_COOKIE */ + "Invalid cookie on ring. (internal error)", + /* MEMIF_ERR_NOBUF_RING */ + "Ring buffer full.", + /* MEMIF_ERR_NOBUF */ + "Not enough memif buffers. There are unreceived data in shared memory.", + /* MEMIF_ERR_INT_WRITE */ + "Send interrupt error.", + /* MEMIF_ERR_MFMSG */ + "Malformed message received on control channel.", + /* MEMIF_ERR_PROTO */ + "Incompatible memory interface protocol version.", + /* MEMIF_ERR_ID */ + "Unmatched interface id.", + /* MEMIF_ERR_ACCSLAVE */ + "Slave cannot accept connection reqest.", + /* MEMIF_ERR_ALRCONN */ + "Interface is already connected.", + /* MEMIF_ERR_MODE */ + "Mode mismatch.", + /* MEMIF_ERR_SECRET */ + "Secret mismatch.", + /* MEMIF_ERR_NOSECRET */ + "Secret required.", + /* MEMIF_ERR_MAXREG */ + "Limit on total number of regions reached.", + /* MEMIF_ERR_MAXRING */ + "Limit on total number of ring reached.", + /* MEMIF_ERR_NO_INTFD */ + "Missing interrupt file descriptor. (internal error)", + /* MEMIF_ERR_DISCONNECT */ + "Interface received disconnect request.", + /* MEMIF_ERR_DISCONNECTED */ + "Interface is disconnected.", + /* MEMIF_ERR_UNKNOWN_MSG */ + "Unknown message type received on control channel. (internal error)" + }; + +#define MEMIF_ERR_UNDEFINED "undefined error" + +char * +memif_strerror (int err_code) +{ + if (err_code > ERRLIST_LEN) + { + strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED)); + memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0'; + } + else + { + strncpy (memif_buf, memif_errlist[err_code], strlen (memif_errlist[err_code])); + memif_buf[strlen (memif_errlist[err_code])] = '\0'; + } + return memif_buf; +} + #define DBG_TX_BUF (0) #define DBG_RX_BUF (1) +#if MEMIF_DBG_SHM static void print_bytes (void *data, uint16_t len, uint8_t q) { @@ -97,20 +163,41 @@ print_bytes (void *data, uint16_t len, uint8_t q) } printf ("\n\n"); } +#endif /* MEMIF_DBG */ -static void -print_conn (memif_connection_t *c) +int +memif_syscall_error_handler (int err_code) { - printf ("MEMIF CONNECTION:\n"); - printf ("\tconn %p\n", c); - printf ("\tfd %d\n", c->fd); - printf ("\tregion %p\n", c->regions); - printf ("\ttx %p\n", c->tx_queues); - printf ("\trx %p\n", c->rx_queues); + DBG_UNIX ("%s", strerror (err_code)); + + if (err_code == 0) + return MEMIF_ERR_SUCCESS; + if (err_code == EACCES) + return MEMIF_ERR_ACCES; + if (err_code == ENFILE) + return MEMIF_ERR_FILE_LIMIT; + if (err_code == EMFILE) + return MEMIF_ERR_PROC_FILE_LIMIT; + if (err_code == ENOMEM) + return MEMIF_ERR_NOMEM; +/* connection refused if master dows not exist + this error would spam the user until master was created */ + if (err_code == ECONNREFUSED) + return MEMIF_ERR_SUCCESS; + if (err_code == EALREADY) + return MEMIF_ERR_ALREADY; + if (err_code == EAGAIN) + return MEMIF_ERR_AGAIN; + if (err_code == EBADF) + return MEMIF_ERR_BAD_FD; + + /* other syscall errors */ + return MEMIF_ERR_SYSCALL; } int memif_init (memif_control_fd_update_t *on_control_fd_update) { + int err = MEMIF_ERR_SUCCESS; /* 0 */ libmemif_main_t *lm = &libmemif_main; lm->control_fd_update = on_control_fd_update; memset (&lm->ms, 0, sizeof (memif_socket_t)); @@ -119,8 +206,9 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); if (lm->timerfd < 0) { - DBG ("timerfd: %s", strerror (errno)); - return -1; + err = errno; + DBG ("timerfd: %s", strerror (err)); + return memif_syscall_error_handler (err); } lm->arm.it_value.tv_sec = 2; @@ -129,8 +217,12 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) lm->arm.it_interval.tv_nsec = 0; memset (&lm->disarm, 0, sizeof (lm->disarm)); - /* TODO: check return */ - lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ); + /* check return or not? */ + if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0) + { + DBG ("user defined callback type memif_control_fd_update_t error!"); + return MEMIF_ERR_CB_FDUPDATE; + } return 0; } @@ -156,22 +248,23 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, memif_connection_update_t *on_disconnect, void *private_ctx) { + int err; + int sockfd = -1; memif_connection_t *conn = (memif_connection_t *) *c; if (conn != NULL) { DBG ("This handle already points to existing memif."); - return -1; + return MEMIF_ERR_CONN; } conn = (memif_connection_t *) malloc (sizeof (memif_connection_t)); if (conn == NULL) { - DBG_UNIX ("out of memory!"); - return -1; + err = memif_syscall_error_handler (errno); + goto error; } + libmemif_main_t *lm = &libmemif_main; - int err; - int sockfd = -1; conn->args.interface_id = args->interface_id; /* lib or app? */ if (args->log2_ring_size == 0) @@ -205,8 +298,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, strlen ((char *) args->socket_filename)); if (conn->args.socket_filename == NULL) { - DBG_UNIX ("out of memory!"); - err = errno; + err = memif_syscall_error_handler (errno); goto error; } strncpy ((char *) conn->args.socket_filename, (char *) args->socket_filename, @@ -219,8 +311,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->args.socket_filename = (uint8_t *) malloc (sdl + sfl + 1); if (conn->args.socket_filename == NULL) { - DBG_UNIX ("out of memory!"); - err = errno; + err = memif_syscall_error_handler (errno); goto error; } strncpy ((char *) conn->args.socket_filename, @@ -257,7 +348,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, } else { - err = 0; + err = MEMIF_ERR_FILE_NOT_SOCK; DBG ("file with specified socket filename exists but is not socket"); goto error; } @@ -266,7 +357,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); if (sockfd < 0) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } un.sun_family = AF_UNIX; @@ -275,27 +366,32 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, if (setsockopt (sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } if (bind (sockfd, (struct sockaddr *) &un, sizeof (un)) == -1) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } if (listen (sockfd, 1) == -1) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } if (stat ((char *) conn->args.socket_filename, &file_stat) == -1) { - err = errno; + err = memif_syscall_error_handler (errno); goto error; } lm->ms.fd = sockfd; lm->ms.use_count++; lm->ms.filename = malloc (strlen ((char *) conn->args.socket_filename)); + if (lm->ms.filename == NULL) + { + err = memif_syscall_error_handler (errno); + goto error; + } strncpy ((char *) lm->ms.filename, (char *) conn->args.socket_filename, strlen ((char *) conn->args.socket_filename)); } @@ -308,7 +404,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, { if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); + err = memif_syscall_error_handler (errno); goto error; } } @@ -323,16 +419,17 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, sockfd = -1; if (conn->args.socket_filename) free (conn->args.socket_filename); - free (conn); + if (conn != NULL) + free (conn); *c = conn = NULL; - error_return_unix ("%s", strerror (err)); + return err; } /* TODO: support multiple interfaces */ int memif_control_fd_handler (int fd, uint8_t events) { - int i; + int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */ memif_connection_t *conn; libmemif_main_t *lm = &libmemif_main; if (fd == lm->timerfd) @@ -345,10 +442,11 @@ memif_control_fd_handler (int fd, uint8_t events) { DBG ("try connect"); struct sockaddr_un sun; - int sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); + sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); if (sockfd < 0) { - DBG ("%s", strerror (errno)); + err = memif_syscall_error_handler (errno); + goto error; } sun.sun_family = AF_UNIX; @@ -370,32 +468,50 @@ memif_control_fd_handler (int fd, uint8_t events) only disarm if there is no disconnected slave */ if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); - } + err = memif_syscall_error_handler (errno); + goto error; + } } else { - DBG ("%s", strerror (errno)); - if (sockfd > 0) - close (sockfd); - sockfd = -1; + err = memif_syscall_error_handler (errno); + goto error; } } } else { - conn = lm->conn; - if (conn->fd == fd) + conn = lm->conn; + if (conn->fd == fd) + { + if (events & MEMIF_FD_EVENT_READ) + { + err = conn->read_fn (conn); + if (err != MEMIF_ERR_SUCCESS) + return err; + } + if (events & MEMIF_FD_EVENT_WRITE) + { + err = conn->write_fn (conn); + if (err != MEMIF_ERR_SUCCESS) + return err; + } + if (events & MEMIF_FD_EVENT_ERROR) { - if (events & MEMIF_FD_EVENT_READ) - conn->read_fn (conn); - if (events & MEMIF_FD_EVENT_WRITE) - conn->write_fn (conn); - if (events & MEMIF_FD_EVENT_ERROR) - conn->error_fn (conn); + err = conn->error_fn (conn); + if (err != MEMIF_ERR_SUCCESS) + return err; } + } } - return 0; + + return MEMIF_ERR_SUCCESS; /* 0 */ + +error: + if (sockfd > 0) + close (sockfd); + sockfd = -1; + return err; } static void @@ -416,15 +532,16 @@ memif_disconnect_internal (memif_connection_t *c) if (c == NULL) { DBG ("no connection"); - return -1; + return MEMIF_ERR_NOCONN; } - libmemif_main_t *lm = &libmemif_main; + int err = MEMIF_ERR_SUCCESS; /* 0 */ - memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); + libmemif_main_t *lm = &libmemif_main; if (c->fd > 0) { + memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL); close (c->fd); } @@ -458,7 +575,7 @@ memif_disconnect_internal (memif_connection_t *c) if (c->regions != NULL) { if (munmap (c->regions->shm, c->regions->region_size) < 0) - DBG ("munmap: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if (c->regions->fd > 0) close (c->regions->fd); c->regions->fd = -1; @@ -472,12 +589,13 @@ memif_disconnect_internal (memif_connection_t *c) only arm if timer is disarmed */ if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); + err = memif_syscall_error_handler (errno); + DBG_UNIX ("timerfd_settime: arm"); } c->on_disconnect ((void *) c, c->private_ctx); - return 0; + return err; } int @@ -485,15 +603,18 @@ memif_delete (memif_conn_handle_t *conn) { memif_connection_t *c = (memif_connection_t *) *conn; libmemif_main_t *lm = &libmemif_main; + + int err; - /* only fail if there is no connection to remove */ - if (memif_disconnect_internal (c) < 0) - return -1; + err = memif_disconnect_internal (c); + if (err == MEMIF_ERR_NOCONN) + return err; /* TODO: only disarm if this is the only disconnected slave */ if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) { - DBG ("timerfd: %s", strerror (errno)); + err = memif_syscall_error_handler (errno); + DBG ("timerfd_settime: disarm"); } if (c->args.socket_filename) @@ -504,10 +625,9 @@ memif_delete (memif_conn_handle_t *conn) c = NULL; *conn = c; - return 0; + return err; } - int memif_connect1 (memif_connection_t *c) { @@ -520,11 +640,13 @@ memif_connect1 (memif_connection_t *c) if (!mr->shm) { if (mr->fd < 0) - error_return ("no memory region fd"); + return MEMIF_ERR_NO_SHMFD; if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, MAP_SHARED, mr->fd, 0)) == MAP_FAILED) - error_return_unix ("mmap"); + { + return memif_syscall_error_handler (errno); + } } } @@ -535,8 +657,10 @@ memif_connect1 (memif_connection_t *c) { mq->ring = c->regions->shm + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) - error_return ("wrong cookie on tx ring %u", i); - i++; + { + DBG ("wrong cookie on tx ring %u", i); + return MEMIF_ERR_COOKIE; + } } i = 0; mq = c->rx_queues; @@ -544,10 +668,13 @@ memif_connect1 (memif_connection_t *c) { mq->ring = c->regions->shm + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) - error_return ("wrong cookie on tx ring %u", i); - i++; + { + DBG ("wrong cookie on rx ring %u", i); + return MEMIF_ERR_COOKIE; + } } + /* callback */ lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); return 0; @@ -562,6 +689,8 @@ memif_init_regions_and_queues (memif_connection_t *conn) int i,j; conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t)); + if (conn->regions == NULL) + return memif_syscall_error_handler (errno); r = conn->regions; buffer_offset = (conn->args.num_s2m_rings + conn->args.num_m2s_rings) * @@ -573,17 +702,17 @@ memif_init_regions_and_queues (memif_connection_t *conn) (conn->args.num_s2m_rings + conn->args.num_m2s_rings); if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) - error_return_unix ("memfd_create: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) - error_return_unix ("fcntl: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if ((ftruncate (r->fd, r->region_size)) == -1) - error_return_unix ("ftruncate: %s", strerror (errno)); + return memif_syscall_error_handler (errno); if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE, MAP_SHARED, r->fd, 0)) == MAP_FAILED) - error_return_unix ("mmap: %s", strerror (errno)); + return memif_syscall_error_handler (errno); for (i = 0; i < conn->args.num_s2m_rings; i++) { @@ -619,7 +748,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) { mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) - error_return_unix ("eventfd: %s", strerror (errno)); + return memif_syscall_error_handler (errno); mq->ring = memif_get_ring (conn, MEMIF_RING_S2M, x); mq->log2_ring_size = conn->args.log2_ring_size; mq->region = 0; @@ -632,7 +761,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) { mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) - error_return_unix ("eventfd: %s", strerror (errno)); + return memif_syscall_error_handler (errno); mq->ring = memif_get_ring (conn, MEMIF_RING_M2S, x); mq->log2_ring_size = conn->args.log2_ring_size; mq->region = 0; @@ -647,14 +776,20 @@ 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) + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; memif_queue_t *mq = c->tx_queues; memif_ring_t *ring = mq->ring; memif_buffer_t *b0, *b1; uint16_t mask = (1 << mq->log2_ring_size) - 1; - uint16_t i = 0, s0, s1, ns; + uint16_t s0, s1, ns; + *count_out = 0; + int err = MEMIF_ERR_SUCCESS; /* 0 */ if (ring->tail != ring->head) { @@ -666,18 +801,22 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, else ns = (1 << mq->log2_ring_size); - /* if head == tail receive function will asume that no packets are available */ + /* (head == tail) ? receive function will asume that no packets are available */ ns -= 1; while (count && ns) { while ((count > 2) && (ns > 2)) { - s0 = (ring->head + mq->alloc_bufs + i) & mask; - s1 = (ring->head + mq->alloc_bufs + i + 1) & mask; + s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; + s1 = (ring->head + mq->alloc_bufs + *count_out + 1) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b1 == NULL) + return memif_syscall_error_handler (errno); b0->desc_index = s0; b1->desc_index = s1; @@ -687,102 +826,118 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, b0->data = c->regions->shm + ring->desc[s0].offset; b1->data = c->regions->shm + ring->desc[s1].offset; - (*(bufs + i)) = b0; - (*(bufs + i + 1)) = b1; + (*(bufs + *count_out)) = b0; + (*(bufs + *count_out + 1)) = b1; DBG ("allocated ring slots %u, %u", s0, s1); count -= 2; ns -= 2; - i += 2; + *count_out += 2; } - s0 = (ring->head + mq->alloc_bufs + i) & mask; + s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); b0->desc_index = s0; b0->buffer_len = ring->desc[s0].buffer_length; b0->data = c->regions->shm + ring->desc[s0].offset; - (*(bufs + i)) = b0; + (*(bufs + *count_out)) = b0; DBG ("allocated ring slot %u", s0); count--; ns--; - i++; + *count_out += 1; } - mq->alloc_bufs += i; + mq->alloc_bufs += *count_out; - DBG ("allocated: %u/%u bufs. Total %u allocated bufs", i, count, mq->alloc_bufs); + DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, mq->alloc_bufs); if (count) + { DBG ("ring buffer full! qid: %u", qid); + err = MEMIF_ERR_NOBUF_RING; + } - return i; + return err; } int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count) + memif_buffer_t **bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; + libmemif_main_t *lm = &libmemif_main; memif_queue_t *mq = c->rx_queues; memif_ring_t *ring = mq->ring; uint16_t tail = ring->tail; uint16_t mask = (1 << mq->log2_ring_size) - 1; - int i = 0; memif_buffer_t *b0, *b1; + *count_out = 0; + + if (mq->alloc_bufs < count) + count = mq->alloc_bufs; while (count) { while (count > 2) { - b0 = (*(bufs + i)); - b1 = (*(bufs + i + 1)); + b0 = (*(bufs + *count_out)); + b1 = (*(bufs + *count_out + 1)); tail = (b0->desc_index + 1) & mask; tail = (b1->desc_index + 1) & mask; b0->data = NULL; b1->data = NULL; free (b0); free (b1); - (*(bufs + i)) = b0 = NULL; - (*(bufs + i + 1)) = b1 = NULL; - + (*(bufs + *count_out)) = b0 = NULL; + (*(bufs + *count_out + 1)) = b1 = NULL; count -= 2; - i += 2; + *count_out += 2; } - b0 = (*(bufs + i)); + b0 = (*(bufs + *count_out)); tail = (b0->desc_index + 1) & mask; b0->data = NULL; free (b0); - (*(bufs + i)) = b0 = NULL; + (*(bufs + *count_out)) = b0 = NULL; count--; - i++; + *count_out += 1; } ring->tail = tail; - return i; + return MEMIF_ERR_SUCCESS; /* 0 */ } int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count) + memif_buffer_t **bufs, uint16_t count, uint16_t *tx) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; memif_queue_t *mq = c->tx_queues; memif_ring_t *ring = mq->ring; uint16_t head = ring->head; uint16_t mask = (1 << mq->log2_ring_size) - 1; - uint16_t tx = 0; + *tx = 0; memif_buffer_t *b0, *b1; while (count) { while (count > 2) { - b0 = (*(bufs + tx)); - b1 = (*(bufs + tx + 1)); + 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; @@ -800,10 +955,10 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b1->data_len = 0; count -= 2; - tx += 2; + *tx += 2; } - b0 = (*(bufs + tx)); + b0 = (*(bufs + *tx)); ring->desc[b0->desc_index].length = b0->data_len; #ifdef MEMIF_DBG_SHM @@ -816,34 +971,39 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b0->data_len = 0; count--; - tx++; + *tx += 1; } ring->head = head; - mq->alloc_bufs -= tx; + mq->alloc_bufs -= *tx; if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) { uint64_t a = 1; int r = write (mq->int_fd, &a, sizeof (a)); if (r < 0) - error_return ("write: %s fd %d", strerror (errno), mq->int_fd); + return MEMIF_ERR_INT_WRITE; } - return tx; + return MEMIF_ERR_SUCCESS; /* 0 */ } int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count) + memif_buffer_t **bufs, uint16_t count, uint16_t *rx) { memif_connection_t *c = (memif_connection_t *) conn; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; memif_queue_t *mq = c->rx_queues; memif_ring_t *ring = mq->ring; uint16_t head = ring->head; - uint16_t ns, rx = 0; + uint16_t ns; uint16_t mask = (1 << mq->log2_ring_size) - 1; memif_buffer_t *b0, *b1; + *rx = 0; if (head == mq->last_head) return 0; @@ -858,7 +1018,11 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, while ((ns > 2) && (count > 2)) { b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b1 == NULL) + return memif_syscall_error_handler (errno); b0->desc_index = mq->last_head; b1->desc_index = mq->last_head + 1; @@ -869,8 +1033,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, b0->buffer_len = ring->desc[mq->last_head].buffer_length; b1->buffer_len = ring->desc[mq->last_head + 1].buffer_length; - (*(bufs + rx)) = b0; - (*(bufs + rx + 1)) = b1; + (*(bufs + *rx)) = b0; + (*(bufs + *rx + 1)) = b1; #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); @@ -881,16 +1045,18 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, ns -= 2; count -= 2; - rx += 2; + *rx += 2; } b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); + if (b0 == NULL) + return memif_syscall_error_handler (errno); 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; - (*(bufs + rx)) = b0; + (*(bufs + *rx)) = b0; #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); @@ -900,22 +1066,33 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, ns--; count--; - rx++; + *rx += 1; } + mq->alloc_bufs += *rx; + if (ns) { - DBG ("ring buffer full"); + DBG ("not enough buffers!"); + return MEMIF_ERR_NOBUF; } - return rx; + return MEMIF_ERR_SUCCESS; /* 0 */ } int -memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid) +memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) { memif_connection_t *c = (memif_connection_t *) conn; - return c->rx_queues->int_fd; + if (c == NULL) + { + *efd = -1; + return MEMIF_ERR_NOCONN; + } + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; + *efd = c->rx_queues->int_fd; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_details_t diff --git a/src/memif_private.h b/src/memif_private.h index 01c077d..aed36ca 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -77,6 +78,35 @@ #endif /* MEMIF_DBG */ +#if 0 + +typedef enum +{ + MEMIF_ERR_SUCCESS = 0, /* success */ +/* SYSCALL ERRORS */ + MEMIF_ERR_SYSCALL, /* other syscall error */ + MEMIF_ERR_ACCES, /* permission denied */ + 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_INT_WRITE, /* send interrupt error */ +} memif_err_t; + +#endif /* 0 */ typedef struct { @@ -98,6 +128,7 @@ typedef struct int int_fd; uint64_t int_count; + uint32_t alloc_bufs; } memif_queue_t; typedef struct memif_msg_queue_elt @@ -137,12 +168,38 @@ typedef struct memif_connection memif_queue_t *rx_queues; memif_queue_t *tx_queues; - uint32_t alloc_buf_num; - uint16_t flags; #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) } memif_connection_t; +/* + * WIP + */ +typedef struct +{ + int fd; + uint16_t use_count; + uint8_t *filename; +} memif_socket_t; + +/* + * WIP + */ +/* probably function like memif_cleanup () will need to be called + close timerfd, free struct libmemif_main and its nested structures */ +typedef struct +{ + memif_control_fd_update_t *control_fd_update; + int timerfd; + struct itimerspec arm, disarm; + + /* TODO: update to arrays support multiple connections */ + memif_socket_t ms; + memif_connection_t *conn; +} libmemif_main_t; + +extern libmemif_main_t libmemif_main; + /* main.c */ /* if region doesn't contain shared memory, mmap region, check ring cookie */ @@ -153,6 +210,9 @@ int memif_init_regions_and_queues (memif_connection_t *c); int memif_disconnect_internal (memif_connection_t *c); +/* map errno to memif error code */ +int memif_syscall_error_handler (int err_code); + #ifndef __NR_memfd_create #if defined __x86_64__ #define __NR_memfd_create 319 diff --git a/src/socket.c b/src/socket.c index 132314d..0447569 100644 --- a/src/socket.c +++ b/src/socket.c @@ -42,7 +42,7 @@ memif_msg_send (int fd, memif_msg_t *msg, int afd) struct msghdr mh = { 0 }; struct iovec iov[1]; char ctl[CMSG_SPACE (sizeof (int))]; - int rv; + int rv, err = MEMIF_ERR_SUCCESS; /* 0 */ iov[0].iov_base = (void *) msg; iov[0].iov_len = sizeof (memif_msg_t); @@ -63,17 +63,19 @@ memif_msg_send (int fd, memif_msg_t *msg, int afd) } rv = sendmsg (fd, &mh, 0); if (rv < 0) - error_return ("sendmsg: %s fd %d", strerror (errno), fd); + err = memif_syscall_error_handler (errno); DBG ("Message type %u sent", msg->type); - return rv; + return err; } /* response from memif master - master is ready to handle next message */ -static_fn void +static_fn int memif_msg_enq_ack (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); e->msg.type = MEMIF_MSG_TYPE_ACK; e->fd = -1; @@ -82,7 +84,7 @@ memif_msg_enq_ack (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -91,6 +93,8 @@ memif_msg_enq_ack (memif_connection_t *c) cur = cur->next; } cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ } static_fn int @@ -115,11 +119,13 @@ memif_msg_send_hello (memif_connection_t *c) } /* send id and secret (optional) for interface identification */ -static_fn void +static_fn int memif_msg_enq_init (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); memif_msg_init_t *i = &e->msg.init; @@ -138,7 +144,7 @@ memif_msg_enq_init (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -147,20 +153,21 @@ memif_msg_enq_init (memif_connection_t *c) cur = cur->next; } cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* send information about region specified by region_index */ static_fn int memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) { - if (c->regions == NULL) - error_return ("no regions initialized"); - /* TODO: support multiple regions */ memif_region_t *mr = c->regions; memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); memif_msg_add_region_t *ar = &e->msg.add_region; @@ -173,7 +180,7 @@ memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) if (c->msg_queue == NULL) { c->msg_queue = e; - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -182,7 +189,8 @@ memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) cur = cur->next; } cur->next = e; - return 0; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* send information about ring specified by direction (S2M | M2S) and index */ @@ -191,6 +199,8 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); memif_msg_add_ring_t *ar = &e->msg.add_ring; @@ -214,7 +224,7 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) if (c->msg_queue == NULL) { c->msg_queue = e; - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -223,15 +233,19 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) cur = cur->next; } cur->next = e; - return 0; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* used as connection request from slave */ -static_fn void +static_fn int memif_msg_enq_connect (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); + memif_msg_connect_t *cm = &e->msg.connect; e->msg.type = MEMIF_MSG_TYPE_CONNECT; @@ -243,7 +257,7 @@ memif_msg_enq_connect (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -252,15 +266,19 @@ memif_msg_enq_connect (memif_connection_t *c) cur = cur->next; } cur->next = e; - return; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* used as confirmation of connection by master */ -static_fn void +static_fn int memif_msg_enq_connected (memif_connection_t *c) { memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); + if (e == NULL) + return memif_syscall_error_handler (errno); + memif_msg_connected_t *cm = &e->msg.connected; e->msg.type = MEMIF_MSG_TYPE_CONNECTED; @@ -272,7 +290,7 @@ memif_msg_enq_connected (memif_connection_t *c) if (c->msg_queue == NULL) { c->msg_queue = e; - return; + return MEMIF_ERR_SUCCESS; /* 0 */ } memif_msg_queue_elt_t *cur = c->msg_queue; @@ -281,7 +299,8 @@ memif_msg_enq_connected (memif_connection_t *c) cur = cur->next; } cur->next = e; - return; + + return MEMIF_ERR_SUCCESS; /* 0 */ } /* immediately send disconnect msg */ @@ -315,7 +334,7 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) msg->hello.max_version < MEMIF_VERSION) { DBG ("incompatible protocol version"); - return -1; + return MEMIF_ERR_PROTO; } /* use nested struct c->run containing following variables? (this would be used to adjust shared memory information while keeping @@ -328,7 +347,7 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) c->args.log2_ring_size); strncpy ((char *) c->remote_name, (char *) h->name, strlen ((char *) h->name)); - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } /* handle interface identification (id, secret (optional)) */ @@ -337,16 +356,20 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) { memif_msg_init_t *i = &msg->init; uint8_t err_string[96]; + int err = MEMIF_ERR_SUCCESS; /* 0 */ + int err_disc; if (i->version != MEMIF_VERSION) { DBG ("MEMIF_VER_ERR"); strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR)); + err = MEMIF_ERR_PROTO; goto error; } if (c->args.interface_id != i->id) { DBG ("MEMIF_ID_ERR"); strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR)); + err = MEMIF_ERR_ID; goto error; } @@ -355,18 +378,21 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) { DBG ("MEMIF_SLAVE_ERR"); strncpy ((char *) err_string, MEMIF_SLAVE_ERR, strlen (MEMIF_SLAVE_ERR)); + err = MEMIF_ERR_ACCSLAVE; goto error; } if (c->fd != -1) { DBG ("MEMIF_CONN_ERR"); strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR)); + err = MEMIF_ERR_ALRCONN; goto error; } if (i->mode != c->args.mode) { DBG ("MEMIF_MODE_ERR"); strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR)); + err = MEMIF_ERR_MODE; goto error; } @@ -382,7 +408,8 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) DBG ("MEMIF_SECRET_ERR"); strncpy ((char *) err_string, MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); - return -1; + err = MEMIF_ERR_SECRET; + goto error; } r = strncmp ((char *) i->secret, (char *) c->args.secret, strlen ((char *) c->args.secret)); @@ -391,7 +418,8 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) DBG ("MEMIF_SECRET_ERR"); strncpy ((char *) err_string, MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); - return -1; + err = MEMIF_ERR_SECRET; + goto error; } } else @@ -399,14 +427,21 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) DBG ("MEMIF_NOSECRET_ERR"); strncpy ((char *) err_string, MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR)); - return -1; + err = MEMIF_ERR_NOSECRET; + goto error; } } - return 0; + + return err; error: - memif_msg_send_disconnect (c, err_string, 1); - return -1; + if (c->fd != -1) + { + err_disc = memif_msg_send_disconnect (c, err_string, 1); + if (err_disc != 0) + return err_disc; + } + return err; } /* receive region information and add new region to connection (if possible) */ @@ -416,12 +451,14 @@ memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) memif_msg_add_region_t *ar = &msg->add_region; memif_region_t *mr; if (fd < 0) - error_return ("missing memory region fd"); + return MEMIF_ERR_NO_SHMFD; if (ar->index > MEMIF_MAX_REGION) - error_return ("maximum region limit reached"); + return MEMIF_ERR_MAXREG; mr = (memif_region_t *) malloc (sizeof (memif_region_t )); + if (mr == NULL) + return memif_syscall_error_handler (errno); mr->fd = fd; mr->region_size = ar->size; mr->shm = NULL; @@ -429,7 +466,7 @@ memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) /* TODO: support multiple regions */ c->regions = mr; - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } /* receive ring information and add new ring to connection queue @@ -442,12 +479,12 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) memif_queue_t *mq; if (fd < 0) - error_return ("missing ring interrupt fd"); + return MEMIF_ERR_NO_INTFD; if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) { if (ar->index > MEMIF_MAX_S2M_RING) - error_return ("maximum ring limit reached"); + return MEMIF_ERR_MAXRING; mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); mq->int_fd = fd; @@ -462,9 +499,11 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) else { if (ar->index > MEMIF_MAX_M2S_RING) - error_return ("maximum ring limit reached"); + return MEMIF_ERR_MAXRING; mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + if (mq == NULL) + return memif_syscall_error_handler (errno); mq->int_fd = fd; mq->log2_ring_size = ar->log2_ring_size; mq->region = ar->region; @@ -475,7 +514,7 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) c->args.num_m2s_rings++; } - return 0; + return MEMIF_ERR_SUCCESS; /* 0 */ } /* slave -> master */ @@ -484,16 +523,16 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) { memif_msg_connect_t *cm = &msg->connect; - int err = 0; + int err; err = memif_connect1 (c); - if (err < 0) + if (err != MEMIF_ERR_SUCCESS) /* 0 */ return err; strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); - return 0; + return err; } /* master -> slave */ @@ -502,16 +541,16 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) { memif_msg_connect_t *cm = &msg->connect; - int err = 0; + int err; err = memif_connect1 (c); - if (err < 0) + if (err != MEMIF_ERR_SUCCESS) /* 0 */ return err; strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); c->on_connect ((void *) c, c->private_ctx); - return 0; + return err; } static_fn int @@ -523,8 +562,9 @@ memif_msg_receive_disconnect (memif_connection_t *c, memif_msg_t *msg) strlen ((char *) d->string)); /* on returning error, handle function will call memif_disconnect () */ - error_return ("disconnect received: %s, mode: %d", + DBG ("disconnect received: %s, mode: %d", c->remote_disconnect_string, c->args.mode); + return MEMIF_ERR_DISCONNECT; } static_fn int @@ -536,7 +576,7 @@ memif_msg_receive (memif_connection_t *c) struct iovec iov[1]; memif_msg_t msg = { 0 }; ssize_t size; - int err = 0; + int err = MEMIF_ERR_SUCCESS; /* 0 */ int fd = -1; int i = 0; @@ -551,9 +591,9 @@ memif_msg_receive (memif_connection_t *c) if (size != sizeof (memif_msg_t)) { if (size == 0) - error_return ("disconnected"); + return MEMIF_ERR_DISCONNECTED; else - error_return ("malformed message received on fd %d", c->fd); + return MEMIF_ERR_MFMSG; } struct ucred *cr = 0; @@ -585,63 +625,73 @@ memif_msg_receive (memif_connection_t *c) break; case MEMIF_MSG_TYPE_HELLO: - if ((err = memif_msg_receive_hello (c, &msg)) < 0) + if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) return err; - if ((err = memif_init_regions_and_queues (c)) < 0) + if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_init (c); - memif_msg_enq_add_region (c, 0); /* TODO: support multiple rings */ - memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M); - memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S); - memif_msg_enq_connect (c); + if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS) + return err; break; case MEMIF_MSG_TYPE_INIT: - if ((err = memif_msg_receive_init (c, &msg)) < 0) + if ((err = memif_msg_receive_init (c, &msg)) != MEMIF_ERR_SUCCESS) return err; /* c->remote_pid = cr->pid */ /* c->remote_uid = cr->uid */ /* c->remote_gid = cr->gid */ - memif_msg_enq_ack (c); + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) + return err; break; case MEMIF_MSG_TYPE_ADD_REGION: - if ((err = memif_msg_receive_add_region (c, &msg, fd)) < 0) + if ((err = memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_ack (c); break; case MEMIF_MSG_TYPE_ADD_RING: - if ((err = memif_msg_receive_add_ring (c, &msg, fd)) < 0) + if ((err = memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_ack (c); break; case MEMIF_MSG_TYPE_CONNECT: - if ((err = memif_msg_receive_connect (c, &msg)) < 0) + if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) return err; - memif_msg_enq_connected (c); break; case MEMIF_MSG_TYPE_CONNECTED: - if ((err = memif_msg_receive_connected (c, &msg)) < 0) + if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS) return err; break; case MEMIF_MSG_TYPE_DISCONNECT: - if ((err = memif_msg_receive_disconnect (c, &msg)) < 0) + if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS) return err; break; default: - error_return ("unknown message type (0x%x)", msg.type); + return MEMIF_ERR_UNKNOWN_MSG;; break; } - /* can send msg */ c->flags |= MEMIF_CONNECTION_FLAG_WRITE; - return 0; +/* libmemif_main_t *lm = &libmemif_main; + lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */ + return MEMIF_ERR_SUCCESS; /* 0 */ } int @@ -650,42 +700,52 @@ memif_conn_fd_error (memif_connection_t *c) DBG ("connection fd error"); strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19); - memif_disconnect_internal (c); - return 0; + int err = memif_disconnect_internal (c); + return err; } /* calls memif_msg_receive to handle pending messages on socket */ int memif_conn_fd_read_ready (memif_connection_t *c) { - int rv = 0; - rv = memif_msg_receive (c); - if (rv < 0) + int err; + err = memif_msg_receive (c); + if (err != 0) { - memif_disconnect_internal (c); + err = memif_disconnect_internal (c); } - return 0; + return err; } /* get msg from msg queue buffer and send it to socket */ int memif_conn_fd_write_ready (memif_connection_t *c) { - int rv = 0; + int err = MEMIF_ERR_SUCCESS; /* 0 */ + + if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0) - return rv; + goto done; memif_msg_queue_elt_t *e = c->msg_queue; if (e == NULL) - return rv; + goto done; c->msg_queue = c->msg_queue->next; - + c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE; - - rv = memif_msg_send (c->fd, &e->msg, e->fd); +/* + libmemif_main_t *lm = &libmemif_main; + + lm->control_fd_update (c->fd, + MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD); +*/ + err = memif_msg_send (c->fd, &e->msg, e->fd); free(e); - return rv; + goto done; + +done: + return err; } int @@ -702,8 +762,7 @@ memif_conn_fd_accept_ready (memif_connection_t *c) if (conn_fd < 0) { - DBG ("accept fd %d", c->fd); - return -1; + return memif_syscall_error_handler (errno); } DBG ("accept fd %d", c->fd); DBG ("conn fd %d", conn_fd); @@ -713,12 +772,5 @@ memif_conn_fd_accept_ready (memif_connection_t *c) c->error_fn = memif_conn_fd_error; c->fd = conn_fd; - int e = memif_msg_send_hello (c); - if (e < 0) - { - DBG ("memif msg send hello error!"); - return -1; - } - - return 0; + return memif_msg_send_hello (c); } diff --git a/src/socket.h b/src/socket.h index 1280505..d96c5dd 100644 --- a/src/socket.h +++ b/src/socket.h @@ -20,7 +20,7 @@ #include -/* interface identification errors */ +/* interface identification errors (disconnect messages)*/ #define MEMIF_VER_ERR "incompatible version" #define MEMIF_ID_ERR "unmatched interface id" #define MEMIF_SLAVE_ERR "cannot connect to salve" @@ -48,11 +48,11 @@ int memif_msg_send_disconnect (memif_connection_t *c, uint8_t *err_string, uint3 int memif_msg_send (int fd, memif_msg_t *msg, int afd); -void memif_msg_enq_ack (memif_connection_t *c); +int memif_msg_enq_ack (memif_connection_t *c); int memif_msg_send_hello (memif_connection_t *c); -void memif_msg_enq_init (memif_connection_t *c); +int memif_msg_enq_init (memif_connection_t *c); int memif_msg_enq_add_region (memif_connection_t *c, uint8_t region); @@ -66,9 +66,9 @@ int memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int f int memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd); -void memif_msg_enq_connect (memif_connection_t *c); +int memif_msg_enq_connect (memif_connection_t *c); -void memif_msg_enq_connected (memif_connection_t *c); +int memif_msg_enq_connected (memif_connection_t *c); int memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg); From e0bb9690e2c10d71e9d821f2592240ea7821bad3 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Tue, 18 Jul 2017 13:43:28 +0200 Subject: [PATCH 26/63] optimization, fix memleaks, doxygen comments (libmemif.h) --- Makefile.am | 2 +- examples/icmp_responder/icmp_proto.c | 17 +++ examples/icmp_responder/icmp_proto.h | 17 +++ examples/icmp_responder/main.c | 132 ++++++++++------- src/libmemif.h | 135 +++++++++++++++-- src/main.c | 207 +++++++++++++++------------ 6 files changed, 356 insertions(+), 154 deletions(-) diff --git a/Makefile.am b/Makefile.am index b663fcf..7700548 100644 --- a/Makefile.am +++ b/Makefile.am @@ -31,7 +31,7 @@ unit_test_SOURCES = test/socket_test.c \ src/main.c # macro MEMIF_UNIT_TEST -> compile functions without static keyword # and declare them in header files, so they can be called from unit tests -unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST +unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST -g # # main lib diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c index 689dc5e..f4700f2 100644 --- a/examples/icmp_responder/icmp_proto.c +++ b/examples/icmp_responder/icmp_proto.c @@ -1,3 +1,20 @@ +/* + *------------------------------------------------------------------ + * 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 diff --git a/examples/icmp_responder/icmp_proto.h b/examples/icmp_responder/icmp_proto.h index ec5ae70..48e3779 100644 --- a/examples/icmp_responder/icmp_proto.h +++ b/examples/icmp_responder/icmp_proto.h @@ -1,3 +1,20 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + #ifndef _ICMP_PROTO_H_ #define _ICMP_PROTO_H_ diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index f78d7b7..d1df27d 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -66,6 +66,8 @@ printf ("\n"); \ } while (0) +#define MAX_MEMIF_BUFS 256 + int epfd; /* @@ -86,12 +88,12 @@ typedef struct /* interrupt file descriptor (specific for each queue) */ int_fd_t *int_fd; /* tx buffers */ - memif_buffer_t **tx_bufs; - /* number of allocated tx buffers */ + memif_buffer_t *tx_bufs; + /* number of tx buffers pointing to shared memory */ uint16_t tx_buf_num; /* rx buffers */ - memif_buffer_t **rx_bufs; - /* number of allocated rx buffers */ + memif_buffer_t *rx_bufs; + /* number of rx buffers pointing to shared memory */ uint16_t rx_buf_num; } memif_connection_t; @@ -104,13 +106,21 @@ print_memif_details () printf ("MEMIF DETAILS\n"); printf ("==============================\n"); - /* TODO: loop for all connections */ - if (c->conn == NULL) + + memif_details_t md; + memset (&md, 0, sizeof (md)); + ssize_t buflen = 2048; + char *buf = malloc (buflen); + int err; + + err = memif_get_details (c->conn, &md, buf, buflen); + if (err != MEMIF_ERR_SUCCESS) { - printf ("no connection!\n"); - return; + INFO ("%s", memif_strerror (err)); + if (err == MEMIF_ERR_NOCONN) + return; } - memif_details_t md = memif_get_details (c->conn); + 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); @@ -218,7 +228,9 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) { INFO ("memif connected!"); int_fd_t *ifd = (int_fd_t *) malloc (sizeof (int_fd_t)); - ifd->fd = memif_get_queue_efd ((&memif_connection)->conn, 0); + int err; + err = memif_get_queue_efd ((&memif_connection)->conn, 0, &ifd->fd); + INFO ("memif_get_queue_efd: %s", memif_strerror (err)); ifd->qid = 0; (&memif_connection)->int_fd = ifd; return add_epoll_fd (ifd->fd, EPOLLIN); @@ -273,32 +285,18 @@ icmpr_memif_create (int is_master) args.interface_id = 0; /* last argument for memif_create (void * private_ctx) is used by user to identify connection. this context is returned with callbacks */ - int rv = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, NULL); - if (rv < 0) - { - DBG ("memif create error!"); - } - else - { - DBG ("memif created!"); - } - return rv; + int err = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, NULL); + INFO ("memif_create: %s", memif_strerror (err)); + return 0; } int icmpr_memif_delete () { - int rv = 0; + int err; /* disconenct then delete memif connection */ - rv = memif_delete (&(&memif_connection)->conn); - if (rv < 0) - { - DBG ("memif delete error!"); - } - else - { - DBG ("memif deleted!"); - } + err = memif_delete (&(&memif_connection)->conn); + INFO ("memif_delete: %s", memif_strerror (err)); return 0; } @@ -322,18 +320,19 @@ print_help () printf ("\texit - exit app\n"); printf ("\tconn - create memif (slave-mode)\n"); printf ("\tdel - delete memif\n"); - printf ("\tshow - print memif details\n"); + printf ("\tshow - show connection details\n"); } int icmpr_buffer_alloc (long n) { memif_connection_t *c = &memif_connection; - c->tx_bufs = (memif_buffer_t **) malloc (sizeof (memif_buffer_t *) * n); - DBG ("call memif_buffer_alloc"); - int r = memif_buffer_alloc (c->conn, 0, c->tx_bufs, n); - DBG ("allocated %d/%ld buffers", r, n); + int err; + uint16_t r, qid = 0; + err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); c->tx_buf_num += r; + DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); return 0; } @@ -341,12 +340,32 @@ int icmpr_tx_burst () { memif_connection_t *c = &memif_connection; - int r = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num); + int err; + uint16_t r, qid = 0; + err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); + INFO ("memif_tx_burst: %s", memif_strerror (err)); DBG ("tx: %d/%u", r, c->tx_buf_num); c->tx_buf_num -= r; return 0; } +int +icmpr_free () +{ + memif_connection_t *c = &memif_connection; + if (c->int_fd != NULL) + free (c->int_fd); + c->int_fd = NULL; + if (c->tx_bufs != NULL) + free (c->tx_bufs); + c->tx_bufs = NULL; + if (c->rx_bufs) + free (c->rx_bufs); + c->rx_bufs = NULL; + + return 0; +} + int user_input_handler () { @@ -359,6 +378,7 @@ user_input_handler () { free (ui); icmpr_memif_delete (); + icmpr_free (); exit (EXIT_SUCCESS); } else if (strncmp (ui, "help", 4) == 0) @@ -389,28 +409,36 @@ int icmpr_interrupt (int fd) { memif_connection_t *c = &memif_connection; - DBG ("interrupted!"); +/* DBG ("interrupted!"); uint64_t b; ssize_t r = read (fd, &b, sizeof (b)); +*/ - int rx = memif_rx_burst (c->conn, 0, c->rx_bufs, c->rx_buf_num); - c->rx_buf_num -= rx; + int err; + uint16_t rx; + err = memif_rx_burst (c->conn, 0, c->rx_bufs, MAX_MEMIF_BUFS, &rx); + INFO ("memif_rx_burst: %s", memif_strerror (err)); + c->rx_buf_num += rx; - DBG ("received %d buffers. %u free buffers", rx, c->rx_buf_num); + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); icmpr_buffer_alloc (rx); int i; for (i = 0; i < rx; i++) { - resolve_packet ((void *) (*(c->rx_bufs + i))->data, - (*(c->rx_bufs + i))->data_len, (void *) (*(c->tx_bufs + i))->data, - &(*(c->tx_bufs + i))->data_len); + resolve_packet ((void *) (c->rx_bufs + i)->data, + (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, + &(c->tx_bufs + i)->data_len); } - int fb = memif_buffer_free (c->conn, 0, c->rx_bufs, rx); - c->rx_buf_num += fb; + uint16_t fb; + err = memif_buffer_free (c->conn, 0, c->rx_bufs, rx, &fb); + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; - DBG ("freed %d buffers. %u free buffers", fb, c->rx_buf_num); + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); icmpr_tx_burst (); @@ -452,6 +480,7 @@ poll_event (int timeout) if (evt.events & EPOLLERR) events |= MEMIF_FD_EVENT_ERROR; memif_err = memif_control_fd_handler (evt.data.fd, events); + INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); } } else if (evt.data.fd == 0) @@ -487,11 +516,16 @@ int main () c->conn = NULL; c->int_fd = malloc (sizeof (int_fd_t)); c->int_fd->fd = -1; - c->rx_buf_num = 256; - c->rx_bufs = malloc (sizeof (memif_buffer_t *) * c->rx_buf_num); + /* alloc memif buffers */ + c->rx_buf_num = 0; + c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->tx_buf_num = 0; + c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); /* initialize memory interface */ - memif_init (control_fd_update); + int err; + err = memif_init (control_fd_update); + INFO ("memif_init: %s", memif_strerror (err)); /* main loop */ while (1) diff --git a/src/libmemif.h b/src/libmemif.h index 363a4b3..8da178f 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -48,6 +48,7 @@ typedef enum 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 PROTO ERRORS */ @@ -77,6 +78,9 @@ typedef enum /* 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; /** \brief Memif control file descriptor update (callback function) @@ -99,10 +103,23 @@ typedef int (memif_control_fd_update_t) (int fd, uint8_t events); */ typedef int (memif_connection_update_t) (memif_conn_handle_t conn, void *private_ctx); +/** \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; - uint8_t secret[24]; + uint8_t secret[24]; /* optional */ uint8_t num_s2m_rings; uint8_t num_m2s_rings; @@ -116,6 +133,12 @@ typedef struct memif_interface_mode_t mode:8; } memif_conn_args_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; @@ -124,18 +147,34 @@ typedef struct void *data; } memif_buffer_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 ring_size - ring size + @param buffer_size - size of buffer in shared memory + @param rx_queues - number of receive queues + @param tx_queues - number of transmit queues + @param link_up_down - 1 = up (connected), 2 = down (disconnected) +*/ typedef struct { - uint8_t if_name[32]; - uint8_t inst_name[32]; - uint8_t remote_if_name[32]; - uint8_t remote_inst_name[32]; + uint8_t *if_name; + uint8_t *inst_name; + uint8_t *remote_if_name; + uint8_t *remote_inst_name; uint32_t id; - uint8_t secret[24]; /* optional */ + 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[128]; + uint8_t *socket_filename; uint32_t ring_size; uint16_t buffer_size; uint8_t rx_queues; @@ -144,9 +183,30 @@ typedef struct uint8_t link_up_down; /* 1 = up, 0 = down */ } memif_details_t; +/** \brief Memif strerror + @param - err_code - error code + + converts error code to error message + + return + error string +*/ char *memif_strerror (int err_code); -memif_details_t memif_get_details (memif_conn_handle_t conn); +/** \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_SUCCESS - no error + MEMIF_ERR_NOCONN - connection handle points to NULL + MEMIF_ERR_NOBUF_DET - privided buffer is not long enough + +*/ +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 @@ -240,7 +300,7 @@ int memif_control_fd_handler (int fd, uint8_t events); int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd); /** \brief Memif delete - @param conn - memif connection handle + @param conn - pointer to memif connection handle return MEMIF_ERR_SUCCESS - no error @@ -253,17 +313,66 @@ int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd); */ 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_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted + MEMIF_ERR_NOBUF_RING - ring buffer full +*/ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); + 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_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted +*/ int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *count_out); + 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 count_out - returns number of transmitted buffers + + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted +*/ int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *tx); + 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 count_out - returns number of received buffers + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - handle points to NULL + MEMIF_ERR_DISCONNECTED - not conencted + MEMIF_ERR_NOBUF - not enough buffers provided +*/ int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *rx); + memif_buffer_t *bufs, uint16_t count, uint16_t *rx); #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index ce4d20c..3c9e33c 100644 --- a/src/main.c +++ b/src/main.c @@ -50,9 +50,15 @@ /* private structs and functions */ #include -#define ERRLIST_LEN 33 +#define ERRLIST_LEN 34 #define MAX_ERRBUF_LEN 256 +#if __x86_x64__ +#define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence () +#else +#define MEMIF_MEORY_BARRIER() __sync_synchronize () +#endif /* __x86_x64__ */ + libmemif_main_t libmemif_main; static char memif_buf[MAX_ERRBUF_LEN]; @@ -93,6 +99,8 @@ const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ "Ring buffer full.", /* MEMIF_ERR_NOBUF */ "Not enough memif buffers. There are unreceived data in shared memory.", + /* MEMIF_ERR_NOBUF_DET */ + "Not enough space for memif details in suplied buffer. String data might be malformed.", /* MEMIF_ERR_INT_WRITE */ "Send interrupt error.", /* MEMIF_ERR_MFMSG */ @@ -551,10 +559,7 @@ memif_disconnect_internal (memif_connection_t *c) if (c->tx_queues != NULL) { if (c->tx_queues->int_fd > 0) - { - lm->control_fd_update (c->tx_queues->int_fd, MEMIF_FD_EVENT_DEL); close (c->tx_queues->int_fd); - } c->tx_queues->int_fd = -1; free (c->tx_queues); c->tx_queues = NULL; @@ -776,7 +781,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) + memif_buffer_t *bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -811,12 +816,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; s1 = (ring->head + mq->alloc_bufs + *count_out + 1) & mask; - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); - b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b1 == NULL) - return memif_syscall_error_handler (errno); + b0 = (bufs + *count_out); + b1 = (bufs + *count_out + 1); b0->desc_index = s0; b1->desc_index = s1; @@ -826,24 +827,19 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, b0->data = c->regions->shm + ring->desc[s0].offset; b1->data = c->regions->shm + ring->desc[s1].offset; - (*(bufs + *count_out)) = b0; - (*(bufs + *count_out + 1)) = b1; DBG ("allocated ring slots %u, %u", s0, s1); count -= 2; ns -= 2; *count_out += 2; } s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; - - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); + + b0 = (bufs + *count_out); b0->desc_index = s0; b0->buffer_len = ring->desc[s0].buffer_length; b0->data = c->regions->shm + ring->desc[s0].offset; - (*(bufs + *count_out)) = b0; DBG ("allocated ring slot %u", s0); count--; ns--; @@ -865,7 +861,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *count_out) + memif_buffer_t *bufs, uint16_t count, uint16_t *count_out) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -887,30 +883,24 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, { while (count > 2) { - b0 = (*(bufs + *count_out)); - b1 = (*(bufs + *count_out + 1)); + b0 = (bufs + *count_out); + b1 = (bufs + *count_out + 1); tail = (b0->desc_index + 1) & mask; tail = (b1->desc_index + 1) & mask; b0->data = NULL; b1->data = NULL; - free (b0); - free (b1); - (*(bufs + *count_out)) = b0 = NULL; - (*(bufs + *count_out + 1)) = b1 = NULL; count -= 2; *count_out += 2; } - b0 = (*(bufs + *count_out)); + b0 = (bufs + *count_out); tail = (b0->desc_index + 1) & mask; b0->data = NULL; - free (b0); - (*(bufs + *count_out)) = b0 = NULL; count--; *count_out += 1; } - + MEMIF_MEORY_BARRIER (); ring->tail = tail; return MEMIF_ERR_SUCCESS; /* 0 */ @@ -918,7 +908,7 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *tx) + memif_buffer_t *bufs, uint16_t count, uint16_t *tx) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -936,8 +926,8 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, { while (count > 2) { - b0 = (*(bufs + *tx)); - b1 = (*(bufs + *tx + 1)); + 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; @@ -953,12 +943,12 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b0->data_len = 0; b1->data = NULL; b1->data_len = 0; - + count -= 2; *tx += 2; } - b0 = (*(bufs + *tx)); + b0 = (bufs + *tx); ring->desc[b0->desc_index].length = b0->data_len; #ifdef MEMIF_DBG_SHM @@ -973,6 +963,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, count--; *tx += 1; } + MEMIF_MEORY_BARRIER (); ring->head = head; mq->alloc_bufs -= *tx; @@ -990,7 +981,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t **bufs, uint16_t count, uint16_t *rx) + memif_buffer_t *bufs, uint16_t count, uint16_t *rx) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -1005,6 +996,12 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *b0, *b1; *rx = 0; + uint64_t b; + ssize_t r = read (mq->int_fd, &b, sizeof (b)); + if ((r == -1) && (errno != EAGAIN)) + return memif_syscall_error_handler (errno); + + if (head == mq->last_head) return 0; @@ -1017,12 +1014,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, { while ((ns > 2) && (count > 2)) { - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); - b1 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b1 == NULL) - return memif_syscall_error_handler (errno); + b0 = (bufs + *rx); + b1 = (bufs + *rx + 1); b0->desc_index = mq->last_head; b1->desc_index = mq->last_head + 1; @@ -1033,9 +1026,6 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, b0->buffer_len = ring->desc[mq->last_head].buffer_length; b1->buffer_len = ring->desc[mq->last_head + 1].buffer_length; - (*(bufs + *rx)) = b0; - (*(bufs + *rx + 1)) = b1; - #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); print_bytes (b1->data , b1->data_len, DBG_RX_BUF); @@ -1047,17 +1037,13 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, count -= 2; *rx += 2; } - b0 = (memif_buffer_t *) malloc (sizeof (memif_buffer_t)); - if (b0 == NULL) - return memif_syscall_error_handler (errno); + b0 = (bufs + *rx); 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; - (*(bufs + *rx)) = b0; - #ifdef MEMIF_DBG_SHM print_bytes (b0->data , b0->data_len, DBG_RX_BUF); #endif @@ -1095,49 +1081,88 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) return MEMIF_ERR_SUCCESS; /* 0 */ } -memif_details_t -memif_get_details (memif_conn_handle_t conn) +int +memif_get_details (memif_conn_handle_t conn, memif_details_t *md, + char *buf, ssize_t buflen) { memif_connection_t *c = (memif_connection_t *) conn; - memif_details_t md; - memset (&md, 0, sizeof (md)); - /* interface name */ - strncpy ((char *) md.if_name, (char *) c->args.interface_name, - strlen ((char *) c->args.interface_name)); - /* instance name */ - strncpy ((char *) md.inst_name, (char *) c->args.instance_name, - strlen ((char *) c->args.instance_name)); - /* remote interface name */ - strncpy ((char *) md.remote_if_name, (char *) c->remote_if_name, - strlen ((char *) c->remote_if_name)); - /* remote instance name */ - strncpy ((char *) md.remote_inst_name, (char *) c->remote_name, - strlen ((char *) c->remote_name)); - /* interface id */ - md.id = c->args.interface_id; - /* secret */ + if (c == NULL) + return MEMIF_ERR_NOCONN; + + int err = MEMIF_ERR_SUCCESS; + ssize_t l0, l1, total_l; + l0 = 0; + + l1 = strlen ((char *) c->args.interface_name); + if (l0 + l1 <= buflen) + { + md->if_name = strncpy (buf + l0, (char *) c->args.interface_name, l1); + md->if_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + l1 = strlen ((char *) c->args.instance_name); + if (l0 + l1 <= buflen) + { + md->inst_name = strncpy (buf + l0, (char *) c->args.instance_name, l1); + md->inst_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + l1 = strlen ((char *) c->remote_if_name); + if (l0 + l1 <= buflen) + { + md->remote_if_name = strncpy (buf + l0, (char *) c->remote_if_name, l1); + md->remote_if_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + l1 = strlen ((char *) c->remote_name); + if (l0 + l1 <= buflen) + { + md->remote_inst_name = strncpy (buf + l0, (char *) c->remote_name, l1); + md->remote_inst_name[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + md->id = c->args.interface_id; + if (c->args.secret) - strncpy ((char *) md.secret, (char *) c->args.secret, - strlen ((char *) c->args.secret)); - /* role */ - md.role = (c->args.is_master) ? 0 : 1; - /* mode */ - md.mode = c->args.mode; - /* socket filename */ - uint32_t l = strlen ((char *) c->args.socket_filename); - if (l > 128) - l = 128; - strncpy ((char *) md.socket_filename, (char *) c->args.socket_filename, l); - /* ring size */ - md.ring_size = (1 << c->args.log2_ring_size); - /* buffer size */ - md.buffer_size = c->args.buffer_size; - /* rx queues */ - md.rx_queues = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; - /* tx queues */ - md.tx_queues = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; - /* link up down */ - md.link_up_down = (c->fd > 0) ? 1 : 0; - - return md; + { + l1 = strlen ((char *) c->args.secret); + md->secret = strncpy (buf + l0, (char *) c->args.secret, l1); + md->secret[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + md->role = (c->args.is_master) ? 0 : 1; + md->mode = c->args.mode; + + l1 = strlen ((char *) c->args.socket_filename); + if (l0 + l1 <= buflen) + { + md->socket_filename = strncpy (buf + l0, (char *) c->args.socket_filename, l1); + md->socket_filename[l0 + l1] = '\0'; + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + md->ring_size = (1 << c->args.log2_ring_size); + md->buffer_size = c->args.buffer_size; + md->rx_queues = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + md->tx_queues = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + md->link_up_down = (c->fd > 0) ? 1 : 0; + + return err; /* 0 */ } From ca39abdeed522f9f4de0bd5bf2479bc815fa689e Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Tue, 18 Jul 2017 15:22:39 +0200 Subject: [PATCH 27/63] memleaks fix --- examples/icmp_responder/main.c | 53 ++++++++++++++++++++-------------- src/main.c | 14 ++++++--- src/socket.c | 9 +++++- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index d1df27d..24ac0ef 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -111,6 +111,7 @@ print_memif_details () memset (&md, 0, sizeof (md)); ssize_t buflen = 2048; char *buf = malloc (buflen); + memset (buf, 0, buflen); int err; err = memif_get_details (c->conn, &md, buf, buflen); @@ -118,7 +119,10 @@ print_memif_details () { INFO ("%s", memif_strerror (err)); if (err == MEMIF_ERR_NOCONN) + { + free (buf); return; + } } printf ("\tinterface name: %s\n",(char *) md.if_name); @@ -158,6 +162,8 @@ print_memif_details () printf ("up\n"); else printf ("down\n"); + + free (buf); } int @@ -226,14 +232,13 @@ del_epoll_fd (int fd) int on_connect (memif_conn_handle_t conn, void *private_ctx) { + memif_connection_t *c = &memif_connection; INFO ("memif connected!"); - int_fd_t *ifd = (int_fd_t *) malloc (sizeof (int_fd_t)); int err; - err = memif_get_queue_efd ((&memif_connection)->conn, 0, &ifd->fd); + err = memif_get_queue_efd (c->conn, 0, &c->int_fd->fd); INFO ("memif_get_queue_efd: %s", memif_strerror (err)); - ifd->qid = 0; - (&memif_connection)->int_fd = ifd; - return add_epoll_fd (ifd->fd, EPOLLIN); + c->int_fd->qid = 0; + return add_epoll_fd (c->int_fd->fd, EPOLLIN); } /* informs user about disconnected status. private_ctx is used by user to identify connection @@ -353,14 +358,11 @@ int icmpr_free () { memif_connection_t *c = &memif_connection; - if (c->int_fd != NULL) - free (c->int_fd); + free (c->int_fd); c->int_fd = NULL; - if (c->tx_bufs != NULL) - free (c->tx_bufs); + free (c->tx_bufs); c->tx_bufs = NULL; - if (c->rx_bufs) - free (c->rx_bufs); + free (c->rx_bufs); c->rx_bufs = NULL; return 0; @@ -369,14 +371,14 @@ icmpr_free () int user_input_handler () { - char *ui = (char *) malloc (256); - char *r = fgets (ui, 256, stdin); - if (ui[0] == '\n') - return 0; - ui = strtok (ui, " "); + char *in = (char *) malloc (256); + char *ui = fgets (in, 256, stdin); + if (in[0] == '\n') + goto done; + ui = strtok (in, " "); if (strncmp (ui, "exit", 4) == 0) { - free (ui); + free (in); icmpr_memif_delete (); icmpr_free (); exit (EXIT_SUCCESS); @@ -384,24 +386,32 @@ user_input_handler () else if (strncmp (ui, "help", 4) == 0) { print_help (); - return 0; + goto done; } else if (strncmp (ui, "conn", 4) == 0) { icmpr_memif_create (0); - return 0; + goto done; } else if (strncmp (ui, "del", 3) == 0) { icmpr_memif_delete (); - return 0; + goto done; } else if (strncmp (ui, "show", 4) == 0) { print_memif_details (); + goto done; } else + { DBG ("unknown command: %s", ui); + goto done; + } + + return 0; +done: + free (in); return 0; } @@ -456,7 +466,8 @@ poll_event (int timeout) memset (&evt, 0, sizeof (evt)); evt.events = EPOLLIN | EPOLLOUT; sigset_t sigset; - en = epoll_pwait (epfd, &evt, 1, timeout, 0); + sigemptyset (&sigset); + en = epoll_pwait (epfd, &evt, 1, timeout, &sigset); if (en < 0) { DBG ("epoll_pwait: %s", strerror (errno)); diff --git a/src/main.c b/src/main.c index 3c9e33c..f00650a 100644 --- a/src/main.c +++ b/src/main.c @@ -270,6 +270,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, err = memif_syscall_error_handler (errno); goto error; } + memset (conn, 0, sizeof (memif_connection_t)); libmemif_main_t *lm = &libmemif_main; @@ -300,10 +301,13 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, l = strlen ((char *) args->instance_name); strncpy ((char *) conn->args.instance_name, (char *) args->instance_name, l); + /* allocate and initialize socket_filename so it can be copyed to sun_path + without memory leaks */ + conn->args.socket_filename = malloc (sizeof (char *) * 108); + memset (conn->args.socket_filename, 0, 108 * sizeof (char *)); + if (args->socket_filename) { - conn->args.socket_filename = (uint8_t *) malloc ( - strlen ((char *) args->socket_filename)); if (conn->args.socket_filename == NULL) { err = memif_syscall_error_handler (errno); @@ -316,7 +320,6 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, { uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR); uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME); - conn->args.socket_filename = (uint8_t *) malloc (sdl + sfl + 1); if (conn->args.socket_filename == NULL) { err = memif_syscall_error_handler (errno); @@ -458,8 +461,9 @@ memif_control_fd_handler (int fd, uint8_t events) } sun.sun_family = AF_UNIX; + strncpy (sun.sun_path, conn->args.socket_filename, - sizeof (sun.sun_path) -1); + sizeof (sun.sun_path) - 1); if (connect (sockfd, (struct sockaddr *) &sun, sizeof (struct sockaddr_un)) == 0) @@ -709,8 +713,10 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); +/* if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) return memif_syscall_error_handler (errno); +*/ if ((ftruncate (r->fd, r->region_size)) == -1) return memif_syscall_error_handler (errno); diff --git a/src/socket.c b/src/socket.c index 0447569..5c4eb15 100644 --- a/src/socket.c +++ b/src/socket.c @@ -77,6 +77,7 @@ memif_msg_enq_ack (memif_connection_t *c) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); e->msg.type = MEMIF_MSG_TYPE_ACK; e->fd = -1; @@ -126,7 +127,9 @@ memif_msg_enq_init (memif_connection_t *c) (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); if (e == NULL) return memif_syscall_error_handler (errno); - + memset (e, 0, sizeof (memif_msg_queue_elt_t)); + + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_init_t *i = &e->msg.init; e->msg.type = MEMIF_MSG_TYPE_INIT; @@ -169,6 +172,7 @@ memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_add_region_t *ar = &e->msg.add_region; e->msg.type = MEMIF_MSG_TYPE_ADD_REGION; @@ -202,6 +206,7 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_add_ring_t *ar = &e->msg.add_ring; e->msg.type = MEMIF_MSG_TYPE_ADD_RING; @@ -246,6 +251,7 @@ memif_msg_enq_connect (memif_connection_t *c) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_connect_t *cm = &e->msg.connect; e->msg.type = MEMIF_MSG_TYPE_CONNECT; @@ -279,6 +285,7 @@ memif_msg_enq_connected (memif_connection_t *c) if (e == NULL) return memif_syscall_error_handler (errno); + memset (&e->msg, 0, sizeof (e->msg)); memif_msg_connected_t *cm = &e->msg.connected; e->msg.type = MEMIF_MSG_TYPE_CONNECTED; From ca73c01d590f24215b04fcd24e76c62f656f63a3 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 20 Jul 2017 12:09:44 +0200 Subject: [PATCH 28/63] ODPM-406 --- Makefile.am | 12 +- README.md | 17 +- examples/icmp_responder/icmp_proto.c | 2 +- examples/icmp_responder/icmp_proto.h | 2 +- examples/icmp_responder/main.c | 199 +++++++-------- examples/icmp_responder2/main.c | 363 +++++++++++++++++++++++++++ src/libmemif.h | 44 +++- src/main.c | 164 +++++++++++- src/memif_private.h | 34 +-- src/socket.c | 6 +- src/socket.h | 2 +- test/socket_test.c | 2 +- test/socket_test.h | 2 +- test/unit_test.c | 2 +- test/unit_test.h | 2 +- 15 files changed, 673 insertions(+), 180 deletions(-) create mode 100644 examples/icmp_responder2/main.c diff --git a/Makefile.am b/Makefile.am index 7700548..c4c3510 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Cisco and/or its affiliates. +# Copyright (c) 2017 Pantheon technologies. # 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: @@ -46,7 +46,15 @@ icmp_responder_SOURCES = examples/icmp_responder/main.c examples/icmp_responder/ icmp_responder_LDADD = libmemif.la icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder -noinst_PROGRAMS = icmp_responder +# +# ICMP responder libmemif event polling example +# +icmpr_lep_SOURCES = examples/icmp_responder2/main.c \ + examples/icmp_responder/icmp_proto.c +icmpr_lep_LDADD = libmemif.la +icmpr_lep_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder + +noinst_PROGRAMS = icmp_responder icmpr_lep check_PROGRAMS = unit_test diff --git a/README.md b/README.md index 6692fd5..543ae2e 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ Shared memory packet interface (memif) provides high performance packet transmit - [x] ICMP responder example app - [x] Transmit/receive packets - [x] Interrupt mode support -- [ ] File descriptor event polling in libmemif (optional) - - [ ] Simplify file descriptor event polling (one handler for control and interrupt channel) +- [x] File descriptor event polling in libmemif (optional) + - [x] Simplify file descriptor event polling (one handler for control and interrupt channel) - [ ] Multiple connections - [ ] Multiple regions - [ ] Multipe queues @@ -70,7 +70,9 @@ For detailed information on api calls and structures please refer to [libmemif.h - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t - Call memif initialization function. memif_init -> If event occures on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. +> If event occures on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. +> If callback function parameter for memif\_init function is set to NULL, libmemif will handle file descriptor event polling. + Api call memif\_poll\_event will call epoll\_pwait wit user defined timeout to poll event on file descriptors opend by libmemif. > Mmeif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. @@ -88,9 +90,7 @@ For detailed information on api calls and structures please refer to [libmemif.h - Once connection has been established, a callback will inform the user about connection status change. 4. Interrupt packet receive - - Interrupt mode is still Work In Progress and will be simplified in furure patch. - - For interrupt mode, user application is required to register interrupt file descriptor for event polling. Api call memif\_get\_queue\_efd will return this interrupt file descriptor. - - If event occures on this file descriptor, there are packets in shared memory to be received. + - If event is polled on interrupt file descriptor, libmemif will call memif\_interrupt\_t callback specified for every connection instance. 6. Memif buffers - Packet data are stored in memif\_buffer\_t. Pointer _data_ points to shared memory buffer, and unsigned integer *data\_len* contains packet data length. @@ -118,6 +118,10 @@ For detailed information on api calls and structures please refer to [libmemif.h - [ICMP Responder](examples/icmp_responder/main.c) +#### Example app (libmemif fd event polling): +- [ICMP Responder](examples/icmp_responder2/main.c) +> Application will create memif interface in slave mode and try to connect to VPP. Exit using Ctrl+C. Application will handle SIGINT signal, free allocated memory and exit with EXIT_SUCCESS. + VPP side config: ``` # create memif id 0 master @@ -125,3 +129,4 @@ VPP side config: # set int ip address memif0 192.168.1.1/24 # ping 192.168.1.2 ``` +> Example applications use VPP default socket file for memif: /run/vpp/memif.sock diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c index f4700f2..9a6b3bc 100644 --- a/examples/icmp_responder/icmp_proto.c +++ b/examples/icmp_responder/icmp_proto.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: diff --git a/examples/icmp_responder/icmp_proto.h b/examples/icmp_responder/icmp_proto.h index 48e3779..ef4c7da 100644 --- a/examples/icmp_responder/icmp_proto.h +++ b/examples/icmp_responder/icmp_proto.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 40f6bad..b71480a 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: @@ -71,23 +71,11 @@ int epfd; -/* - * WIP - */ -/* interrupt fd specific for queue */ -typedef struct -{ - uint16_t qid; - int fd; -} int_fd_t; - typedef struct { uint16_t index; /* memif conenction handle */ memif_conn_handle_t conn; - /* interrupt file descriptor (specific for each queue) */ - int_fd_t *int_fd; /* tx buffers */ memif_buffer_t *tx_bufs; /* allocated tx buffers counter */ @@ -235,16 +223,8 @@ del_epoll_fd (int fd) int on_connect (memif_conn_handle_t conn, void *private_ctx) { - memif_connection_t *c = &memif_connection; INFO ("memif connected!"); - int err; - uint16_t qid = 0; - /* get interrupt file descriptor for queue specified by qid */ - err = memif_get_queue_efd (c->conn, qid, &c->int_fd->fd); - INFO ("memif_get_queue_efd: %s", memif_strerror (err)); - c->int_fd->qid = 0; - /* add interrupt fd to epoll */ - return add_epoll_fd (c->int_fd->fd, EPOLLIN); + return 0; } /* informs user about disconnected status. private_ctx is used by user to identify connection @@ -277,6 +257,74 @@ control_fd_update (int fd, uint8_t events) return add_epoll_fd (fd, evt); } +int +icmpr_buffer_alloc (long n, uint16_t qid) +{ + memif_connection_t *c = &memif_connection; + 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); + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + c->tx_buf_num += r; + DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); + return 0; +} + +int +icmpr_tx_burst (uint16_t qid) +{ + memif_connection_t *c = &memif_connection; + int err; + uint16_t r; + /* inform peer memif interface about data in shared memory buffers */ + /* mark memif buffers as free */ + err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); + INFO ("memif_tx_burst: %s", memif_strerror (err)); + DBG ("tx: %d/%u", r, c->tx_buf_num); + c->tx_buf_num -= r; + return 0; +} + +/* called when event is polled on interrupt file descriptor. + there are packets in shared memory ready to be received */ +int +on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) +{ + memif_connection_t *c = &memif_connection; + int err; + uint16_t rx; + /* receive data from shared memory buffers */ + err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); + INFO ("memif_rx_burst: %s", memif_strerror (err)); + c->rx_buf_num += rx; + + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + icmpr_buffer_alloc (rx, qid); + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (c->rx_bufs + i)->data, + (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, + &(c->tx_bufs + i)->data_len); + } + + uint16_t fb; + /* mark memif buffers and shared memory buffers as free */ + err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + icmpr_tx_burst (qid); + + return 0; +} + int icmpr_memif_create (int is_master) { @@ -299,7 +347,8 @@ icmpr_memif_create (int is_master) args.interface_id = 0; /* 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 (&(&memif_connection)->conn, &args, on_connect, on_disconnect, NULL); + int err = memif_create (&(&memif_connection)->conn, + &args, on_connect, on_disconnect, on_interrupt, NULL); INFO ("memif_create: %s", memif_strerror (err)); return 0; } @@ -336,43 +385,11 @@ print_help () printf ("\tdel - delete memif\n"); printf ("\tshow - show connection details\n"); } - -int -icmpr_buffer_alloc (long n) -{ - memif_connection_t *c = &memif_connection; - int err; - uint16_t r, qid = 0; - /* 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); - INFO ("memif_buffer_alloc: %s", memif_strerror (err)); - c->tx_buf_num += r; - DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); - return 0; -} - -int -icmpr_tx_burst () -{ - memif_connection_t *c = &memif_connection; - int err; - uint16_t r, qid = 0; - /* inform peer memif interface about data in shared memory buffers */ - /* mark memif buffers as free */ - err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); - INFO ("memif_tx_burst: %s", memif_strerror (err)); - DBG ("tx: %d/%u", r, c->tx_buf_num); - c->tx_buf_num -= r; - return 0; -} - int icmpr_free () { /* application cleanup */ memif_connection_t *c = &memif_connection; - free (c->int_fd); - c->int_fd = NULL; free (c->tx_bufs); c->tx_bufs = NULL; free (c->rx_bufs); @@ -428,43 +445,6 @@ user_input_handler () return 0; } -int -icmpr_interrupt (int fd) -{ - memif_connection_t *c = &memif_connection; - int err; - uint16_t rx; - /* receive data from shared memory buffers */ - err = memif_rx_burst (c->conn, 0, c->rx_bufs, MAX_MEMIF_BUFS, &rx); - INFO ("memif_rx_burst: %s", memif_strerror (err)); - c->rx_buf_num += rx; - - DBG ("received %d buffers. %u/%u alloc/free buffers", - rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - - icmpr_buffer_alloc (rx); - int i; - for (i = 0; i < rx; i++) - { - resolve_packet ((void *) (c->rx_bufs + i)->data, - (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, - &(c->tx_bufs + i)->data_len); - } - - uint16_t fb; - /* mark memif buffers and shared memory buffers as free */ - err = memif_buffer_free (c->conn, 0, c->rx_bufs, rx, &fb); - INFO ("memif_buffer_free: %s", memif_strerror (err)); - c->rx_buf_num -= fb; - - DBG ("freed %d buffers. %u/%u alloc/free buffers", - fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - - icmpr_tx_burst (); - - return 0; -} - int poll_event (int timeout) { @@ -488,26 +468,16 @@ poll_event (int timeout) /* this app does not use any other file descriptors than stds and memif control fds */ if ( evt.data.fd > 2) { - /* event on memif interrupt fd */ - if (evt.data.fd == c->int_fd->fd) - { - /* WIP */ - /* in future patch there will be one handler and callback on interrupt */ - icmpr_interrupt (evt.data.fd); - } - else - { - /* 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); - INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); - } + /* 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); + INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); } else if (evt.data.fd == 0) { @@ -540,8 +510,6 @@ int main () /* initialize global memif connection handle */ c->conn = NULL; - c->int_fd = malloc (sizeof (int_fd_t)); - c->int_fd->fd = -1; /* alloc memif buffers */ c->rx_buf_num = 0; c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); @@ -550,6 +518,9 @@ int main () /* initialize memory interface */ int err; + /* 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); INFO ("memif_init: %s", memif_strerror (err)); diff --git a/examples/icmp_responder2/main.c b/examples/icmp_responder2/main.c new file mode 100644 index 0000000..ca18a70 --- /dev/null +++ b/examples/icmp_responder2/main.c @@ -0,0 +1,363 @@ +/* + *------------------------------------------------------------------ + * Copyright (c) 2017 Pantheon technologies. + * 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 + +#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 + +typedef struct +{ + uint16_t index; + /* memif conenction handle */ + memif_conn_handle_t conn; + /* tx buffers */ + memif_buffer_t *tx_bufs; + /* allocated tx buffers counter */ + /* number of tx buffers pointing to shared memory */ + uint16_t tx_buf_num; + /* rx buffers */ + memif_buffer_t *rx_bufs; + /* allcoated rx buffers counter */ + /* number of rx buffers pointing to shared memory */ + uint16_t rx_buf_num; +} memif_connection_t; + +memif_connection_t memif_connection; +int epfd; + +static void +print_memif_details () +{ + memif_connection_t *c = &memif_connection; + printf ("MEMIF DETAILS\n"); + printf ("==============================\n"); + + + memif_details_t md; + memset (&md, 0, sizeof (md)); + ssize_t buflen = 2048; + char *buf = malloc (buflen); + memset (buf, 0, buflen); + int err; + + err = memif_get_details (c->conn, &md, buf, buflen); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("%s", memif_strerror (err)); + if (err == MEMIF_ERR_NOCONN) + { + free (buf); + return; + } + } + + 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 ("\tring_size: %u\n", md.ring_size); + printf ("\tbuffer_size: %u\n", md.buffer_size); + printf ("\trx queues: %u\n", md.rx_queues); + printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\tlink: "); + if (md.link_up_down) + printf ("up\n"); + else + printf ("down\n"); + + free (buf); +} +/* 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) +{ + INFO ("memif connected!"); + 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) +{ + INFO ("memif disconnected!"); + return 0; +} + +int +icmpr_memif_delete () +{ + int err; + /* disconenct then delete memif connection */ + err = memif_delete (&(&memif_connection)->conn); + 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"); +} + +int +icmpr_buffer_alloc (long n, uint16_t qid) +{ + memif_connection_t *c = &memif_connection; + 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); + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + c->tx_buf_num += r; + DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); + return 0; +} + +int +icmpr_tx_burst (uint16_t qid) +{ + memif_connection_t *c = &memif_connection; + int err; + uint16_t r; + /* inform peer memif interface about data in shared memory buffers */ + /* mark memif buffers as free */ + err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); + INFO ("memif_tx_burst: %s", memif_strerror (err)); + DBG ("tx: %d/%u", r, c->tx_buf_num); + c->tx_buf_num -= r; + return 0; +} + +int +icmpr_free () +{ + /* application cleanup */ + memif_connection_t *c = &memif_connection; + free (c->tx_bufs); + c->tx_bufs = NULL; + free (c->rx_bufs); + c->rx_bufs = NULL; + + return 0; +} + +void +icmpr_exit (int sig) +{ + printf ("\n"); + icmpr_memif_delete (); + icmpr_free (); + exit (EXIT_SUCCESS); +} + +/* called when event is polled on interrupt file descriptor. + there are packets in shared memory ready to be received */ +int +on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) +{ + DBG ("interrupted"); + memif_connection_t *c = &memif_connection; + int err; + uint16_t rx; + /* receive data from shared memory buffers */ + err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); + INFO ("memif_rx_burst: %s", memif_strerror (err)); + c->rx_buf_num += rx; + + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + icmpr_buffer_alloc (rx, qid); + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (c->rx_bufs + i)->data, + (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, + &(c->tx_bufs + i)->data_len); + } + + uint16_t fb; + /* mark memif buffers and shared memory buffers as free */ + err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + icmpr_tx_burst (qid); + + return 0; +} + +int +icmpr_memif_create (int is_master) +{ + /* setting memif connection arguments */ + memif_conn_args_t args; + int fd = -1; + memset (&args, 0, sizeof (args)); + args.is_master = is_master; + args.log2_ring_size = 10; + args.buffer_size = 2048; + args.num_s2m_rings = 1; + args.num_m2s_rings = 1; + 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 = 0; + /* 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 (&(&memif_connection)->conn, + &args, on_connect, on_disconnect, on_interrupt, NULL); + INFO ("memif_create: %s", memif_strerror (err)); + return 0; +} + +int main () +{ + memif_connection_t *c = &memif_connection; + + signal (SIGINT, icmpr_exit); + + /* initialize global memif connection handle */ + c->conn = NULL; + /* alloc memif buffers */ + c->rx_buf_num = 0; + c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->tx_buf_num = 0; + c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + + /* initialize memory interface */ + int err; + /* 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 (NULL); + INFO ("memif_init: %s", memif_strerror (err)); + + print_help (); + + icmpr_memif_create (0); + print_memif_details (); + + /* main loop */ + while (1) + { + if (memif_poll_event (-1) < 0) + { + DBG ("poll_event error!"); + } + } +} diff --git a/src/libmemif.h b/src/libmemif.h index a788a0c..a44867e 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: @@ -103,6 +103,13 @@ typedef int (memif_control_fd_update_t) (int fd, uint8_t events); */ 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 +*/ +typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx, uint16_t qid); + /** \brief Memif connection arguments @param socket_filename - socket filename @param secret - otional parameter used as interface autenthication @@ -211,6 +218,12 @@ int memif_get_details (memif_conn_handle_t conn, memif_details_t *md, /** \brief Memif initialization @param on_control_fd_update - if control fd updates inform user to watch new fd + 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 + return MEMIF_ERR_SUCCESS - no error MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) @@ -257,7 +270,11 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update); 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 */ -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, void * private_ctx); +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 @@ -288,17 +305,6 @@ int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args, memif_co */ int memif_control_fd_handler (int fd, uint8_t events); -/** \brief Memif get queue event file descriptor - @param conn - memif connection handle - @param qid - number identifying queue - @param efd - return interrupt fd for memif queue specified by qid - - rerurn - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - handle points to NULL -*/ -int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd); - /** \brief Memif delete @param conn - pointer to memif connection handle @@ -375,4 +381,16 @@ int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, 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_SUCCESS - no error +*/ +int memif_poll_event (int timeout); + #endif /* _LIBMEMIF_H_ */ diff --git a/src/main.c b/src/main.c index f00650a..30bab40 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,6 @@ /* + *------------------------------------------------------------------ + * Copyright (c) 2017 Pantheon technologies. * 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: @@ -40,6 +42,8 @@ #include #include #include +#include +#include /* memif protocol msg, ring and descriptor definitions */ #include @@ -60,6 +64,7 @@ #endif /* __x86_x64__ */ libmemif_main_t libmemif_main; +int memif_epfd; static char memif_buf[MAX_ERRBUF_LEN]; @@ -88,7 +93,7 @@ const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ /* MEMIF_ERR_CONN */ "Memif connection handle points to existing connection", /* MEMIF_ERR_CB_FDUPDATE */ - "User defined callback memif_control_fd_update_t returned error", + "Callback memif_control_fd_update_t returned error", /* MEMIF_ERR_FILE_NOT_SOCK */ "File specified by socket filename exists and is not socket.", /* MEMIF_ERR_NO_SHMFD */ @@ -203,11 +208,106 @@ memif_syscall_error_handler (int err_code) return MEMIF_ERR_SYSCALL; } +static int +memif_add_epoll_fd (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 (memif_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; +} + +static int +memif_mod_epoll_fd (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 (memif_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; +} + +static int +memif_del_epoll_fd (int fd) +{ + if (fd < 0) + { + DBG ("invalid fd %d", fd); + return -1; + } + struct epoll_event evt; + memset (&evt, 0, sizeof (evt)); + if (epoll_ctl (memif_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; +} + +int +memif_control_fd_update (int fd, uint8_t events) +{ + if (events & MEMIF_FD_EVENT_DEL) + return memif_del_epoll_fd (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 memif_mod_epoll_fd (fd, evt); + + return memif_add_epoll_fd (fd, evt); +} + +static void +memif_control_fd_update_register (memif_control_fd_update_t *cb) +{ + libmemif_main_t *lm = &libmemif_main; + lm->control_fd_update = cb; +} + int memif_init (memif_control_fd_update_t *on_control_fd_update) { int err = MEMIF_ERR_SUCCESS; /* 0 */ libmemif_main_t *lm = &libmemif_main; - lm->control_fd_update = on_control_fd_update; + + /* register control fd update callback */ + if (on_control_fd_update != NULL) + memif_control_fd_update_register (on_control_fd_update); + else + { + memif_epfd = epoll_create (1); + memif_control_fd_update_register (memif_control_fd_update); + } + memset (&lm->ms, 0, sizeof (memif_socket_t)); lm->conn = NULL; @@ -225,10 +325,9 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) lm->arm.it_interval.tv_nsec = 0; memset (&lm->disarm, 0, sizeof (lm->disarm)); - /* check return or not? */ if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0) { - DBG ("user defined callback type memif_control_fd_update_t error!"); + DBG ("callback type memif_control_fd_update_t error!"); return MEMIF_ERR_CB_FDUPDATE; } @@ -254,6 +353,7 @@ 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) { int err; @@ -293,6 +393,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->fd = -1; conn->on_connect = on_connect; conn->on_disconnect = on_disconnect; + conn->on_interrupt = on_interrupt; conn->private_ctx = private_ctx; uint8_t l = strlen ((char *) args->interface_name); @@ -473,8 +574,8 @@ memif_control_fd_handler (int fd, uint8_t events) conn->write_fn = memif_conn_fd_write_ready; conn->error_fn = memif_conn_fd_error; - lm->control_fd_update ( - sockfd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE); + lm->control_fd_update ( + sockfd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE); /* TODO: with multiple connections support, only disarm if there is no disconnected slave */ @@ -494,6 +595,14 @@ memif_control_fd_handler (int fd, uint8_t events) else { conn = lm->conn; + if (conn->rx_queues != NULL) + { + if (fd == conn->rx_queues->int_fd) + { + conn->on_interrupt ((void *) conn, conn->private_ctx, 0); + return MEMIF_ERR_SUCCESS; /* 0 */ + } + } if (conn->fd == fd) { if (events & MEMIF_FD_EVENT_READ) @@ -526,6 +635,49 @@ memif_control_fd_handler (int fd, uint8_t events) return err; } +int +memif_poll_event (int timeout) +{ + libmemif_main_t *lm = &libmemif_main; + struct epoll_event evt, *e; + int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */ + uint32_t events = 0; + memset (&evt, 0, sizeof (evt)); + evt.events = EPOLLIN | EPOLLOUT; + sigset_t sigset; + sigemptyset (&sigset); + en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset); + if (en < 0) + { + DBG ("epoll_pwait: %s", strerror (errno)); + return -1; + } + if (en > 0) + { + /* event on memif interrupt fd */ + if (lm->conn->rx_queues != NULL) + { + if (evt.data.fd == lm->conn->rx_queues->int_fd) + { + lm->conn->on_interrupt ((void *) lm->conn, + lm->conn->private_ctx, 0); + return 0; + } + } + /* 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; + err = memif_control_fd_handler (evt.data.fd, events); + DBG ("memif_control_fd_handler: %s", memif_strerror (err)); + } + return 0; +} + static void memif_msg_queue_free (memif_msg_queue_elt_t **e) { diff --git a/src/memif_private.h b/src/memif_private.h index aed36ca..4021e6d 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: @@ -78,36 +78,6 @@ #endif /* MEMIF_DBG */ -#if 0 - -typedef enum -{ - MEMIF_ERR_SUCCESS = 0, /* success */ -/* SYSCALL ERRORS */ - MEMIF_ERR_SYSCALL, /* other syscall error */ - MEMIF_ERR_ACCES, /* permission denied */ - 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_INT_WRITE, /* send interrupt error */ -} memif_err_t; - -#endif /* 0 */ - typedef struct { void *shm; @@ -154,6 +124,7 @@ typedef struct memif_connection memif_fn *write_fn, *read_fn, *error_fn; memif_connection_update_t *on_connect, *on_disconnect; + memif_interrupt_t *on_interrupt; void *private_ctx; /* connection message queue */ @@ -199,6 +170,7 @@ typedef struct } libmemif_main_t; extern libmemif_main_t libmemif_main; +extern int memif_epfd; /* main.c */ diff --git a/src/socket.c b/src/socket.c index 5c4eb15..c62fb95 100644 --- a/src/socket.c +++ b/src/socket.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: @@ -529,6 +529,7 @@ static_fn int memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) { memif_msg_connect_t *cm = &msg->connect; + libmemif_main_t *lm = &libmemif_main; int err; err = memif_connect1 (c); @@ -537,6 +538,7 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + lm->control_fd_update (c->rx_queues->int_fd, MEMIF_FD_EVENT_READ); c->on_connect ((void *) c, c->private_ctx); return err; @@ -547,6 +549,7 @@ static_fn int memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) { memif_msg_connect_t *cm = &msg->connect; + libmemif_main_t *lm = &libmemif_main; int err; err = memif_connect1 (c); @@ -555,6 +558,7 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); + lm->control_fd_update (c->rx_queues->int_fd, MEMIF_FD_EVENT_READ); c->on_connect ((void *) c, c->private_ctx); return err; diff --git a/src/socket.h b/src/socket.h index d96c5dd..3c750b5 100644 --- a/src/socket.h +++ b/src/socket.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: diff --git a/test/socket_test.c b/test/socket_test.c index 17d13cf..b086a93 100644 --- a/test/socket_test.c +++ b/test/socket_test.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: diff --git a/test/socket_test.h b/test/socket_test.h index b55ac47..69a82a1 100644 --- a/test/socket_test.h +++ b/test/socket_test.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: diff --git a/test/unit_test.c b/test/unit_test.c index f53cb2f..eec9b2f 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: diff --git a/test/unit_test.h b/test/unit_test.h index 36ab77d..43954dd 100644 --- a/test/unit_test.h +++ b/test/unit_test.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017 Pantheon technologies. * 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: From d334c0ffd85e33e20e5715f391163dbffd076fa4 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 26 Jul 2017 13:16:59 +0200 Subject: [PATCH 29/63] ODPM-461 --- examples/icmp_responder/main.c | 70 ++++++-- examples/icmp_responder2/main.c | 61 ++++--- src/libmemif.h | 2 + src/main.c | 279 ++++++++++++++------------------ src/memif_private.h | 4 +- src/socket.c | 43 ++--- 6 files changed, 246 insertions(+), 213 deletions(-) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index b71480a..1be4d5b 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -76,6 +76,8 @@ typedef struct uint16_t index; /* memif conenction handle */ memif_conn_handle_t conn; + /* transmit queue id */ + uint16_t tx_qid; /* tx buffers */ memif_buffer_t *tx_bufs; /* allocated tx buffers counter */ @@ -265,7 +267,12 @@ icmpr_buffer_alloc (long n, uint16_t qid) 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); - INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + c->tx_buf_num += r; + return -1; + } c->tx_buf_num += r; DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); return 0; @@ -280,7 +287,8 @@ icmpr_tx_burst (uint16_t qid) /* inform peer memif interface about data in shared memory buffers */ /* mark memif buffers as free */ err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); - INFO ("memif_tx_burst: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_tx_burst: %s", memif_strerror (err)); DBG ("tx: %d/%u", r, c->tx_buf_num); c->tx_buf_num -= r; return 0; @@ -294,15 +302,25 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) memif_connection_t *c = &memif_connection; int err; uint16_t rx; + uint16_t fb; /* receive data from shared memory buffers */ err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); - INFO ("memif_rx_burst: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_rx_burst: %s", memif_strerror (err)); + c->rx_buf_num += rx; + goto error; + } c->rx_buf_num += rx; DBG ("received %d buffers. %u/%u alloc/free buffers", rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_buffer_alloc (rx, qid); + if (icmpr_buffer_alloc (rx, c->tx_qid) < 0) + { + INFO ("buffer_alloc error"); + goto error; + } int i; for (i = 0; i < rx; i++) { @@ -311,17 +329,26 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) &(c->tx_bufs + i)->data_len); } - uint16_t fb; /* mark memif buffers and shared memory buffers as free */ err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); - INFO ("memif_buffer_free: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); c->rx_buf_num -= fb; DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_tx_burst (qid); + icmpr_tx_burst (c->tx_qid); + + return 0; +error: + err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); return 0; } @@ -335,8 +362,8 @@ icmpr_memif_create (int is_master) args.is_master = is_master; args.log2_ring_size = 10; args.buffer_size = 2048; - args.num_s2m_rings = 1; - args.num_m2s_rings = 1; + 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; @@ -349,7 +376,8 @@ icmpr_memif_create (int is_master) to identify connection. this context is returned with callbacks */ int err = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, on_interrupt, NULL); - INFO ("memif_create: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_create: %s", memif_strerror (err)); return 0; } @@ -359,7 +387,8 @@ icmpr_memif_delete () int err; /* disconenct then delete memif connection */ err = memif_delete (&(&memif_connection)->conn); - INFO ("memif_delete: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); return 0; } @@ -384,6 +413,7 @@ print_help () printf ("\tconn - create memif (slave-mode)\n"); printf ("\tdel - delete memif\n"); printf ("\tshow - show connection details\n"); + printf ("\ttx-qid - set transmit queue id\n"); } int icmpr_free () @@ -401,8 +431,10 @@ icmpr_free () int user_input_handler () { + memif_connection_t *c = &memif_connection; char *in = (char *) malloc (256); char *ui = fgets (in, 256, stdin); + char *end; if (in[0] == '\n') goto done; ui = strtok (in, " "); @@ -433,6 +465,14 @@ user_input_handler () print_memif_details (); goto done; } + else if (strncmp (ui, "tx-qid", 6) == 0) + { + ui = strtok (NULL, " "); + if (ui != NULL) + c->tx_qid = strtol (ui, &end, 10); + INFO ("tx-qid %u", c->tx_qid); + goto done; + } else { DBG ("unknown command: %s", ui); @@ -477,7 +517,8 @@ poll_event (int timeout) if (evt.events & EPOLLERR) events |= MEMIF_FD_EVENT_ERROR; memif_err = memif_control_fd_handler (evt.data.fd, events); - INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); + if (memif_err != MEMIF_ERR_SUCCESS) + INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); } else if (evt.data.fd == 0) { @@ -510,6 +551,8 @@ int main () /* initialize global memif connection handle */ c->conn = NULL; + + c->tx_qid = 0; /* alloc memif buffers */ c->rx_buf_num = 0; c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); @@ -522,7 +565,8 @@ int main () 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); - INFO ("memif_init: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_init: %s", memif_strerror (err)); print_help (); diff --git a/examples/icmp_responder2/main.c b/examples/icmp_responder2/main.c index ca18a70..106b8ec 100644 --- a/examples/icmp_responder2/main.c +++ b/examples/icmp_responder2/main.c @@ -74,6 +74,8 @@ typedef struct uint16_t index; /* memif conenction handle */ memif_conn_handle_t conn; + /* transmit queue id */ + uint16_t tx_qid; /* tx buffers */ memif_buffer_t *tx_bufs; /* allocated tx buffers counter */ @@ -179,7 +181,8 @@ icmpr_memif_delete () int err; /* disconenct then delete memif connection */ err = memif_delete (&(&memif_connection)->conn); - INFO ("memif_delete: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); return 0; } @@ -198,12 +201,7 @@ print_help () #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 ("\tuse CTRL+C to exit\n"); } int @@ -214,7 +212,12 @@ icmpr_buffer_alloc (long n, uint16_t qid) 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); - INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + c->tx_buf_num += r; + return -1; + } c->tx_buf_num += r; DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); return 0; @@ -229,7 +232,8 @@ icmpr_tx_burst (uint16_t qid) /* inform peer memif interface about data in shared memory buffers */ /* mark memif buffers as free */ err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); - INFO ("memif_tx_burst: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_tx_burst: %s", memif_strerror (err)); DBG ("tx: %d/%u", r, c->tx_buf_num); c->tx_buf_num -= r; return 0; @@ -268,13 +272,16 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) uint16_t rx; /* receive data from shared memory buffers */ err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); - INFO ("memif_rx_burst: %s", memif_strerror (err)); c->rx_buf_num += rx; DBG ("received %d buffers. %u/%u alloc/free buffers", rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_buffer_alloc (rx, qid); + if (icmpr_buffer_alloc (rx, c->tx_qid) < 0) + { + INFO ("buffer_alloc error"); + goto error; + } int i; for (i = 0; i < rx; i++) { @@ -286,15 +293,23 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) uint16_t fb; /* mark memif buffers and shared memory buffers as free */ err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); - INFO ("memif_buffer_free: %s", memif_strerror (err)); c->rx_buf_num -= fb; DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_tx_burst (qid); + icmpr_tx_burst (c->tx_qid); return 0; + +error: + err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + return 0; } int @@ -307,8 +322,8 @@ icmpr_memif_create (int is_master) args.is_master = is_master; args.log2_ring_size = 10; args.buffer_size = 2048; - args.num_s2m_rings = 1; - args.num_m2s_rings = 1; + 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; @@ -321,11 +336,12 @@ icmpr_memif_create (int is_master) to identify connection. this context is returned with callbacks */ int err = memif_create (&(&memif_connection)->conn, &args, on_connect, on_disconnect, on_interrupt, NULL); - INFO ("memif_create: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_create: %s", memif_strerror (err)); return 0; } -int main () +int main (int argc, char *argv[]) { memif_connection_t *c = &memif_connection; @@ -333,6 +349,14 @@ int main () /* initialize global memif connection handle */ c->conn = NULL; + if (argc == 1) + c->tx_qid = 0; + else + { + char *end; + c->tx_qid = strtol (argv[1], &end, 10); + } + INFO ("tx qid: %u", c->tx_qid); /* alloc memif buffers */ c->rx_buf_num = 0; c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); @@ -345,7 +369,8 @@ int main () 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 (NULL); - INFO ("memif_init: %s", memif_strerror (err)); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_init: %s", memif_strerror (err)); print_help (); diff --git a/src/libmemif.h b/src/libmemif.h index a44867e..f26a734 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -31,6 +31,7 @@ typedef enum /* 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 */ @@ -51,6 +52,7 @@ typedef enum 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 */ diff --git a/src/main.c b/src/main.c index 30bab40..57041ef 100644 --- a/src/main.c +++ b/src/main.c @@ -54,7 +54,7 @@ /* private structs and functions */ #include -#define ERRLIST_LEN 34 +#define ERRLIST_LEN 36 #define MAX_ERRBUF_LEN 256 #if __x86_x64__ @@ -74,6 +74,8 @@ const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ "Unspecified syscall error (build with -DMEMIF_DBG or make debug).", /* MEMIF_ERR_ACCES */ "Permission to resoure denied.", + /* MEMIF_ERR_NO_FILE */ + "Socket file does not exist", /* MEMIF_ERR_FILE_LIMIT */ "System limit on total numer of open files reached.", /* MEMIF_ERR_PROC_FILE_LIMIT */ @@ -110,6 +112,8 @@ const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ "Send interrupt error.", /* MEMIF_ERR_MFMSG */ "Malformed message received on control channel.", + /* MEMIF_ERR_QID */ + "Invalid queue id", /* MEMIF_ERR_PROTO */ "Incompatible memory interface protocol version.", /* MEMIF_ERR_ID */ @@ -203,6 +207,8 @@ memif_syscall_error_handler (int err_code) return MEMIF_ERR_AGAIN; if (err_code == EBADF) return MEMIF_ERR_BAD_FD; + if (err_code == ENOENT) + return MEMIF_ERR_NO_FILE; /* other syscall errors */ return MEMIF_ERR_SYSCALL; @@ -335,16 +341,15 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) } static inline memif_ring_t * -memif_get_ring (memif_connection_t *conn, memif_ring_type_t type, uint16_t rn) +memif_get_ring (memif_connection_t *conn, memif_ring_type_t type, uint16_t ring_num) { - if (conn->regions == NULL) + if (&conn->regions[0] == NULL) return NULL; - /* TODO: support multiple regions */ - void *p = conn->regions->shm; + void *p = conn->regions[0].shm; int ring_size = sizeof (memif_ring_t) + sizeof (memif_desc_t) * (1 << conn->args.log2_ring_size); - p += (rn + type * conn->args.num_s2m_rings) * ring_size; + p += (ring_num + type * conn->args.num_s2m_rings) * ring_size; return (memif_ring_t *) p; } @@ -375,17 +380,22 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, libmemif_main_t *lm = &libmemif_main; conn->args.interface_id = args->interface_id; - /* lib or app? */ + if (args->log2_ring_size == 0) args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE; - if (!args->buffer_size == 0) + if (args->buffer_size == 0) args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE; + if (args->num_s2m_rings == 0) + args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES; + if (args->num_m2s_rings == 0) + args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES; conn->args.num_s2m_rings = args->num_s2m_rings; conn->args.num_m2s_rings = args->num_m2s_rings; conn->args.buffer_size = args->buffer_size; conn->args.log2_ring_size = args->log2_ring_size; conn->args.is_master = args->is_master; + conn->args.mode = args->mode; conn->msg_queue = NULL; conn->regions = NULL; conn->tx_queues = NULL; @@ -439,86 +449,10 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, strncpy ((char *) conn->args.secret, (char *) args->secret, l); } - if (conn->args.is_master) - { - /* master */ - struct sockaddr_un un; - struct stat file_stat; - int on = 1; - - memif_socket_t ms = lm->ms; - - DBG ("memif master mode draft..."); - - if (lm->ms.fd == 0) - { - if (stat ((char *) conn->args.socket_filename, &file_stat) == 0) - { - if (S_ISSOCK (file_stat.st_mode)) - { - unlink ((char *) conn->args.socket_filename); - } - else - { - err = MEMIF_ERR_FILE_NOT_SOCK; - DBG ("file with specified socket filename exists but is not socket"); - goto error; - } - } - - sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); - if (sockfd < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - un.sun_family = AF_UNIX; - strncpy ((char *) un.sun_path, (char *) conn->args.socket_filename, - sizeof (un.sun_path) - 1); - - if (setsockopt (sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - if (bind (sockfd, (struct sockaddr *) &un, sizeof (un)) == -1) - { - err = memif_syscall_error_handler (errno); - goto error; - } - if (listen (sockfd, 1) == -1) - { - err = memif_syscall_error_handler (errno); - goto error; - } - if (stat ((char *) conn->args.socket_filename, &file_stat) == -1) - { - err = memif_syscall_error_handler (errno); - goto error; - } - lm->ms.fd = sockfd; - lm->ms.use_count++; - lm->ms.filename = malloc (strlen ((char *) conn->args.socket_filename)); - if (lm->ms.filename == NULL) - { - err = memif_syscall_error_handler (errno); - goto error; - } - strncpy ((char *) lm->ms.filename, (char *) conn->args.socket_filename, - strlen ((char *) conn->args.socket_filename)); - } - - DBG ("fd %d, uc %u, filename %s", lm->ms.fd, lm->ms.use_count, lm->ms.filename); - conn->fd = lm->ms.fd; - conn->read_fn = memif_conn_fd_accept_ready; - } - else + if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) { - if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } + err = memif_syscall_error_handler (errno); + goto error; } *c = lm->conn = conn; @@ -552,7 +486,6 @@ memif_control_fd_handler (int fd, uint8_t events) conn = lm->conn; if (conn->fd < 0) { - DBG ("try connect"); struct sockaddr_un sun; sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); if (sockfd < 0) @@ -673,7 +606,6 @@ memif_poll_event (int timeout) if (evt.events & EPOLLERR) events |= MEMIF_FD_EVENT_ERROR; err = memif_control_fd_handler (evt.data.fd, events); - DBG ("memif_control_fd_handler: %s", memif_strerror (err)); } return 0; } @@ -698,9 +630,9 @@ memif_disconnect_internal (memif_connection_t *c) DBG ("no connection"); return MEMIF_ERR_NOCONN; } - - int err = MEMIF_ERR_SUCCESS; /* 0 */ - + uint16_t num; + int err = MEMIF_ERR_SUCCESS, i; /* 0 */ + memif_queue_t *mq; libmemif_main_t *lm = &libmemif_main; if (c->fd > 0) @@ -711,35 +643,50 @@ memif_disconnect_internal (memif_connection_t *c) } c->fd = -1; - /* TODO: support multiple rings */ if (c->tx_queues != NULL) { - if (c->tx_queues->int_fd > 0) - close (c->tx_queues->int_fd); - c->tx_queues->int_fd = -1; + num = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + for (i = 0; i < num; i++) + { + mq = &c->tx_queues[i]; + if (mq != NULL) + { + if (mq->int_fd > 0) + close (mq->int_fd); + mq->int_fd = -1; + } + } free (c->tx_queues); c->tx_queues = NULL; } + if (c->rx_queues != NULL) { - if (c->rx_queues->int_fd > 0) + num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + for (i = 0; i < num; i++) { - lm->control_fd_update (c->rx_queues->int_fd, MEMIF_FD_EVENT_DEL); - close (c->rx_queues->int_fd); + mq = &c->rx_queues[i]; + if (mq != NULL) + { + if (mq->int_fd > 0) + { + lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL); + close (mq->int_fd); + } + mq->int_fd = -1; + } } - c->rx_queues->int_fd = -1; free (c->rx_queues); c->rx_queues = NULL; } - /* TODO: support multiple regions */ if (c->regions != NULL) { - if (munmap (c->regions->shm, c->regions->region_size) < 0) + if (munmap (c->regions[0].shm, c->regions[0].region_size) < 0) return memif_syscall_error_handler (errno); - if (c->regions->fd > 0) - close (c->regions->fd); - c->regions->fd = -1; + if (c->regions[0].fd > 0) + close (c->regions[0].fd); + c->regions[0].fd = -1; free (c->regions); c->regions = NULL; } @@ -793,9 +740,11 @@ int memif_connect1 (memif_connection_t *c) { libmemif_main_t *lm = &libmemif_main; - memif_region_t *mr = c->regions; - /* TODO: support multiple regions */ + memif_queue_t *mq; + int i; + uint16_t num; + if (mr != NULL) { if (!mr->shm) @@ -811,31 +760,35 @@ memif_connect1 (memif_connection_t *c) } } - /* TODO: support multiple queues */ - int i = 0; - memif_queue_t *mq = c->tx_queues; - if (mq != NULL) + num = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + for (i = 0; i < num; i++) { - mq->ring = c->regions->shm + mq->offset; - if (mq->ring->cookie != MEMIF_COOKIE) + mq = &c->tx_queues[i]; + if (mq != NULL) { - DBG ("wrong cookie on tx ring %u", i); - return MEMIF_ERR_COOKIE; + mq->ring = c->regions[mq->region].shm + mq->offset; + if (mq->ring->cookie != MEMIF_COOKIE) + { + DBG ("wrong cookie on tx ring %u", i); + return MEMIF_ERR_COOKIE; + } } } - i = 0; - mq = c->rx_queues; - if (mq != NULL) + num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + for (i = 0; i < num; i++) { - mq->ring = c->regions->shm + mq->offset; - if (mq->ring->cookie != MEMIF_COOKIE) + mq = &c->rx_queues[i]; + if (mq != NULL) { - DBG ("wrong cookie on rx ring %u", i); - return MEMIF_ERR_COOKIE; + mq->ring = c->regions[mq->region].shm + mq->offset; + if (mq->ring->cookie != MEMIF_COOKIE) + { + DBG ("wrong cookie on rx ring %u", i); + return MEMIF_ERR_COOKIE; + } } } - /* callback */ lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); return 0; @@ -864,12 +817,10 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); - /* if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) return memif_syscall_error_handler (errno); */ - if ((ftruncate (r->fd, r->region_size)) == -1) return memif_syscall_error_handler (errno); @@ -880,6 +831,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) for (i = 0; i < conn->args.num_s2m_rings; i++) { ring = memif_get_ring (conn, MEMIF_RING_S2M, i); + DBG ("RING: %p I: %d", ring, i); ring->head = ring->tail = 0; ring->cookie = MEMIF_COOKIE; for (j = 0; j < (1 << conn->args.log2_ring_size); j++) @@ -894,6 +846,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) for (i = 0; i < conn->args.num_m2s_rings; i++) { ring = memif_get_ring (conn, MEMIF_RING_M2S, i); + DBG ("RING: %p I: %d", ring, i); ring->head = ring->tail = 0; ring->cookie = MEMIF_COOKIE; for (j = 0; j < (1 << conn->args.log2_ring_size); j++) @@ -906,33 +859,40 @@ memif_init_regions_and_queues (memif_connection_t *conn) } } memif_queue_t *mq; + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t) * conn->args.num_s2m_rings); + if (mq == NULL) + return memif_syscall_error_handler (errno); int x; for (x = 0; x < conn->args.num_s2m_rings; x++) { - mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); - if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) + if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); - mq->ring = memif_get_ring (conn, MEMIF_RING_S2M, x); - mq->log2_ring_size = conn->args.log2_ring_size; - mq->region = 0; - mq->offset = (void *) mq->ring - (void *) conn->regions->shm; - mq->alloc_bufs = 0; - conn->tx_queues = mq; + mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x); + DBG ("RING: %p I: %d", mq[x].ring, x); + mq[x].log2_ring_size = conn->args.log2_ring_size; + mq[x].region = 0; + mq[x].offset = (void *) mq[x].ring - (void *) conn->regions[mq->region].shm; + mq[x].last_head = 0; + mq[x].alloc_bufs = 0; } + conn->tx_queues = mq; + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t) * conn->args.num_m2s_rings); + if (mq == NULL) + return memif_syscall_error_handler (errno); for (x = 0; x < conn->args.num_m2s_rings; x++) { - mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); - if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0) + if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); - mq->ring = memif_get_ring (conn, MEMIF_RING_M2S, x); - mq->log2_ring_size = conn->args.log2_ring_size; - mq->region = 0; - mq->offset = (void *) mq->ring - (void *) conn->regions->shm; - mq->last_head = 0; - mq->alloc_bufs = 0; - conn->rx_queues = mq; + mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x); + DBG ("RING: %p I: %d", mq[x].ring, x); + mq[x].log2_ring_size = conn->args.log2_ring_size; + mq[x].region = 0; + mq[x].offset = (void *) mq[x].ring - (void *) conn->regions[mq->region].shm; + mq[x].last_head = 0; + mq[x].alloc_bufs = 0; } + conn->rx_queues = mq; return 0; } @@ -946,7 +906,10 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - memif_queue_t *mq = c->tx_queues; + uint8_t num = (c->args.is_master) ? c->args.num_m2s_rings : c->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; uint16_t mask = (1 << mq->log2_ring_size) - 1; @@ -1026,8 +989,11 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; + uint8_t num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + if (qid >= num) + return MEMIF_ERR_QID; libmemif_main_t *lm = &libmemif_main; - memif_queue_t *mq = c->rx_queues; + memif_queue_t *mq = &c->rx_queues[qid]; memif_ring_t *ring = mq->ring; uint16_t tail = ring->tail; uint16_t mask = (1 << mq->log2_ring_size) - 1; @@ -1073,7 +1039,10 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - memif_queue_t *mq = c->tx_queues; + uint8_t num = (c->args.is_master) ? c->args.num_m2s_rings : c->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; @@ -1146,7 +1115,10 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - memif_queue_t *mq = c->rx_queues; + uint8_t num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + if (qid >= num) + return MEMIF_ERR_QID; + memif_queue_t *mq = &c->rx_queues[qid]; memif_ring_t *ring = mq->ring; uint16_t head = ring->head; uint16_t ns; @@ -1224,21 +1196,6 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_SUCCESS; /* 0 */ } -int -memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) -{ - memif_connection_t *c = (memif_connection_t *) conn; - if (c == NULL) - { - *efd = -1; - return MEMIF_ERR_NOCONN; - } - if (c->fd < 0) - return MEMIF_ERR_DISCONNECTED; - *efd = c->rx_queues->int_fd; - return MEMIF_ERR_SUCCESS; /* 0 */ -} - int memif_get_details (memif_conn_handle_t conn, memif_details_t *md, char *buf, ssize_t buflen) diff --git a/src/memif_private.h b/src/memif_private.h index 4021e6d..619cc50 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -37,8 +37,8 @@ #define MEMIF_DEFAULT_TX_QUEUES 1 #define MEMIF_DEFAULT_BUFFER_SIZE 2048 -#define MEMIF_MAX_M2S_RING 1 -#define MEMIF_MAX_S2M_RING 1 +#define MEMIF_MAX_M2S_RING 255 +#define MEMIF_MAX_S2M_RING 255 #define MEMIF_MAX_REGION 255 #define MEMIF_MAX_LOG2_RING_SIZE 14 diff --git a/src/socket.c b/src/socket.c index c62fb95..e3f5905 100644 --- a/src/socket.c +++ b/src/socket.c @@ -164,8 +164,8 @@ memif_msg_enq_init (memif_connection_t *c) static_fn int memif_msg_enq_add_region (memif_connection_t *c, uint8_t region_index) { - /* TODO: support multiple regions */ - memif_region_t *mr = c->regions; + /* maybe check if region is valid? */ + memif_region_t *mr = &c->regions[region_index]; memif_msg_queue_elt_t *e = (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t)); @@ -214,14 +214,14 @@ memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir) /* TODO: support multiple rings */ memif_queue_t *mq; if (dir == MEMIF_RING_M2S) - mq = c->rx_queues; + mq = &c->rx_queues[index]; else - mq = c->tx_queues; + mq = &c->tx_queues[index]; e->fd = mq->int_fd; ar->index = index; ar->offset = mq->offset; - ar->region = index; + ar->region = mq->region; ar->log2_ring_size = mq->log2_ring_size; ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0; @@ -346,6 +346,7 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) /* use nested struct c->run containing following variables? (this would be used to adjust shared memory information while keeping configured values intact) */ + DBG ("%u", h->max_s2m_ring); c->args.num_s2m_rings = memif_min (h->max_s2m_ring + 1, c->args.num_s2m_rings); c->args.num_m2s_rings = memif_min (h->max_m2s_ring + 1, @@ -463,15 +464,13 @@ memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) if (ar->index > MEMIF_MAX_REGION) return MEMIF_ERR_MAXREG; - mr = (memif_region_t *) malloc (sizeof (memif_region_t )); + mr = (memif_region_t *) realloc (c->regions, sizeof (memif_region_t)); if (mr == NULL) return memif_syscall_error_handler (errno); - mr->fd = fd; - mr->region_size = ar->size; - mr->shm = NULL; - - /* TODO: support multiple regions */ c->regions = mr; + c->regions[ar->index].fd = fd; + c->regions[ar->index].region_size = ar->size; + c->regions[ar->index].shm = NULL; return MEMIF_ERR_SUCCESS; /* 0 */ } @@ -533,7 +532,7 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) int err; err = memif_connect1 (c); - if (err != MEMIF_ERR_SUCCESS) /* 0 */ + if (err != MEMIF_ERR_SUCCESS) return err; strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); @@ -553,7 +552,7 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) int err; err = memif_connect1 (c); - if (err != MEMIF_ERR_SUCCESS) /* 0 */ + if (err != MEMIF_ERR_SUCCESS) return err; strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); @@ -569,6 +568,7 @@ memif_msg_receive_disconnect (memif_connection_t *c, memif_msg_t *msg) { memif_msg_disconnect_t *d = &msg->disconnect; + memset (c->remote_disconnect_string, 0, sizeof (c->remote_disconnect_string)); strncpy ((char *) c->remote_disconnect_string, (char *) d->string, strlen ((char *) d->string)); @@ -589,7 +589,7 @@ memif_msg_receive (memif_connection_t *c) ssize_t size; int err = MEMIF_ERR_SUCCESS; /* 0 */ int fd = -1; - int i = 0; + int i; iov[0].iov_base = (void *) &msg; iov[0].iov_len = sizeof (memif_msg_t); @@ -644,11 +644,16 @@ memif_msg_receive (memif_connection_t *c) return err; if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS) return err; - /* TODO: support multiple rings */ - if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS) - return err; - if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS) - return err; + for (i = 0; i < c->args.num_s2m_rings; i++) + { + if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS) + return err; + } + for (i = 0; i < c->args.num_m2s_rings; i++) + { + if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS) + return err; + } if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS) return err; break; From 63c75c2aebf80ff938f8d401a246b2d5ea7faf83 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 26 Jul 2017 13:42:35 +0200 Subject: [PATCH 30/63] unit tests (check framework) --- Makefile.am | 15 +- README.md | 42 +- configure.ac | 2 + examples/icmp_responder/main.c | 2 +- src/libmemif.h | 2 +- src/main.c | 2 +- test/main_test.c | 805 +++++++++++++++++++++++++++ test/main_test.h | 25 + test/socket_test.c | 970 ++++++++++++--------------------- test/socket_test.h | 4 +- test/unit_test.c | 43 +- test/unit_test.h | 48 +- 12 files changed, 1290 insertions(+), 670 deletions(-) create mode 100644 test/main_test.c create mode 100644 test/main_test.h diff --git a/Makefile.am b/Makefile.am index c4c3510..2dc7ee6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Pantheon technologies. +# 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: @@ -24,14 +24,15 @@ release: # # unit_test # -unit_test_SOURCES = test/socket_test.c \ - test/main_test.c \ - test/unit_test.c \ - src/socket.c \ - src/main.c +unit_test_SOURCES = test/unit_test.c \ + test/main_test.c \ + test/socket_test.c \ + src/main.c \ + src/socket.c # macro MEMIF_UNIT_TEST -> compile functions without static keyword # and declare them in header files, so they can be called from unit tests -unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST -g +unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST -g $(CHECK_CFLAGS) +unit_test_LDADD = $(CHECK_LIBS) # # main lib diff --git a/README.md b/README.md index 543ae2e..f6df994 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ Shared memory packet interface (memif) provides high performance packet transmit - [x] File descriptor event polling in libmemif (optional) - [x] Simplify file descriptor event polling (one handler for control and interrupt channel) - [ ] Multiple connections -- [ ] Multiple regions -- [ ] Multipe queues +- [x] Multipe queues - [ ] Multithread support - [ ] Master mode + - [ ] Multiple regions - [ ] Performance testing - [ ] Documentation @@ -62,13 +62,22 @@ commands: show - show connection details ``` +#### Unit tests + +Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be instaled in order to build *unit\_test* binary. +Ubuntu/Debian: +``` +sudo apt-get install check +``` +[More platforms](https://libcheck.github.io/check/web/install.html) + #### Connecting to VPP For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) 1. Initialize memif - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t - - Call memif initialization function. memif_init + - Call memif initialization function. memif\_init > If event occures on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. > If callback function parameter for memif\_init function is set to NULL, libmemif will handle file descriptor event polling. @@ -120,13 +129,38 @@ For detailed information on api calls and structures please refer to [libmemif.h #### Example app (libmemif fd event polling): - [ICMP Responder](examples/icmp_responder2/main.c) +> Optional argument: transmit queue id. +``` +icmpr_lep 1 +``` +> Set transmit queue id to 1. Default is 0. > Application will create memif interface in slave mode and try to connect to VPP. Exit using Ctrl+C. Application will handle SIGINT signal, free allocated memory and exit with EXIT_SUCCESS. -VPP side config: +VPP config: ``` # create memif id 0 master # set int state memif0 up # set int ip address memif0 192.168.1.1/24 # ping 192.168.1.2 ``` +For multipe rings (queues) support run VPP with worker threads: +example startup.conf: +``` +unix { + interactive + nodaemon + full-coredump +} + +cpu { + workers 2 +} +``` +VPP config: +``` +# create memif id 0 master rx-queues 2 tx-queues 2 +# set int state memif0 up +# set int ip address memif0 192.168.1.1/24 +# ping 192.168.1.2 +``` > Example applications use VPP default socket file for memif: /run/vpp/memif.sock diff --git a/configure.ac b/configure.ac index ef46f31..30fd3dd 100644 --- a/configure.ac +++ b/configure.ac @@ -4,6 +4,8 @@ AM_INIT_AUTOMAKE AM_SILENT_RULES([yes]) AC_PREFIX_DEFAULT([/usr]) +PKG_CHECK_MODULES([CHECK], [check]) + AC_PROG_CC AC_OUTPUT([Makefile]) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 1be4d5b..e99cee5 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/src/libmemif.h b/src/libmemif.h index f26a734..ae90347 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/src/main.c b/src/main.c index 57041ef..14485e1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/test/main_test.c b/test/main_test.c new file mode 100644 index 0000000..302fda0 --- /dev/null +++ b/test/main_test.c @@ -0,0 +1,805 @@ +/* + *------------------------------------------------------------------ + * Copyright (c) 2017 Pantheon technologies. + * 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 + +#define SOCKET_FILENAME "/run/vpp/memif.sock" + +uint8_t ready_called; +#define read_call (1 << 0) +#define write_call (1 << 1) +#define error_call (1 << 2) + +int +read_fn (memif_connection_t *c) +{ + ready_called |= read_call; + return 0; +} + +int +write_fn (memif_connection_t *c) +{ + ready_called |= write_call; + return 0; +} + +int +error_fn (memif_connection_t *c) +{ + ready_called |= error_call; + return 0; +} + +static void +register_fd_ready_fn (memif_connection_t *c, + memif_fn *read_fn, memif_fn *write_fn, memif_fn *error_fn) +{ + c->read_fn = read_fn; + c->write_fn = write_fn; + c->error_fn = error_fn; +} + +START_TEST (test_init) +{ + int err; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + libmemif_main_t *lm = &libmemif_main; + + ck_assert_ptr_ne (lm, NULL); + ck_assert_ptr_ne (lm->control_fd_update, NULL); + ck_assert_int_gt (lm->timerfd, 2); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; +} +END_TEST + +START_TEST (test_init_epoll) +{ + int err; + + if ((err = memif_init (NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + libmemif_main_t *lm = &libmemif_main; + + ck_assert_ptr_ne (lm, NULL); + ck_assert_ptr_ne (lm->control_fd_update, NULL); + ck_assert_int_gt (lm->timerfd, 2); + ck_assert_int_gt (memif_epfd, -1); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; +} +END_TEST + +START_TEST (test_create) +{ + int err; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + ck_assert_ptr_ne (c, NULL); + + ck_assert_uint_eq (c->args.interface_id, args.interface_id); + ck_assert_uint_eq (c->args.is_master, args.is_master); + ck_assert_uint_eq (c->args.mode, args.mode); + + ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES); + ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES); + ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE); + ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE); + + ck_assert_ptr_eq (c->msg_queue, NULL); + ck_assert_ptr_eq (c->regions, NULL); + ck_assert_ptr_eq (c->tx_queues, NULL); + ck_assert_ptr_eq (c->rx_queues, NULL); + + ck_assert_int_eq (c->fd, -1); + + ck_assert_ptr_ne (c->on_connect, NULL); + ck_assert_ptr_ne (c->on_disconnect, NULL); + ck_assert_ptr_ne (c->on_interrupt, NULL); + + ck_assert_str_eq (c->args.interface_name, args.interface_name); + ck_assert_str_eq (c->args.instance_name, args.instance_name); + ck_assert_str_eq (c->args.socket_filename, SOCKET_FILENAME); + + struct itimerspec timer; + timerfd_gettime (lm->timerfd, &timer); + + ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec, + "timerfd not armed!"); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_control_fd_handler) +{ + int err; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_control_fd_handler ( + lm->timerfd, MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_NO_FILE, + "err code: %u, err msg: %s", err, memif_strerror (err)); + + register_fd_ready_fn (c, read_fn, write_fn, error_fn); + c->fd = 69; + + if ((err = memif_control_fd_handler ( + c->fd, MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert (ready_called & read_call); + + if ((err = memif_control_fd_handler ( + c->fd, MEMIF_FD_EVENT_WRITE)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert (ready_called & write_call); + + if ((err = memif_control_fd_handler ( + c->fd, MEMIF_FD_EVENT_ERROR)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert (ready_called & error_call); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_buffer_alloc) +{ + int err, i; + uint8_t qid; + uint16_t buf; + memif_buffer_t *bufs; + uint16_t max_buf = 10; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + c->fd = 69; + + /* test buffer allocation qid 0 (positive) */ + + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + qid = 0; + if ((err = memif_buffer_alloc (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (buf, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE); + + /* test buffer allocation qid 1 (positive) */ + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + qid = 1; + if ((err = memif_buffer_alloc (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (buf, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE); + + /* test buffer allocation qid 2 (negative) */ + + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + qid = 2; + if ((err = memif_buffer_alloc (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_QID, + "err code: %u, err msg: %s", err, memif_strerror (err)); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + free (bufs); + bufs = NULL; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_tx_burst) +{ + int err, i; + uint16_t max_buf = 10, buf, tx; + uint8_t qid; + memif_buffer_t *bufs; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + c->fd = 69; + + /* test transmit qid 0 (positive) */ + + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + qid = 0; + if ((err = memif_buffer_alloc (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (buf, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE); + + if ((err = memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (tx, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_eq (bufs[i].data, NULL); + + /* test transmit qid 1 (positive) */ + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + qid = 1; + if ((err = memif_buffer_alloc (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (buf, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_uint_eq (bufs[i].buffer_len, MEMIF_DEFAULT_BUFFER_SIZE); + + if ((err = memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (tx, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_eq (bufs[i].data, NULL); + + /* test transmit qid 2 (negative) */ + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + qid = 2; + if ((err = memif_tx_burst (conn, qid, bufs, max_buf, &tx)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_QID, + "err code: %u, err msg: %s", err, memif_strerror (err)); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + free (bufs); + bufs = NULL; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_rx_burst) +{ + int err, i; + uint16_t max_buf = 10, buf, rx; + uint8_t qid; + memif_buffer_t *bufs; + memif_queue_t *mq; + memif_ring_t *ring; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + c->fd = 69; + + /* test receive qid 0 (positive) */ + qid = 0; + mq = &c->rx_queues[qid]; + ring = mq->ring; + ring->head += max_buf; + + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + if ((err = memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (rx, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_ne (bufs[i].data, NULL); + + /* test receive qid 1 (positive) */ + qid = 1; + mq = &c->rx_queues[qid]; + ring = mq->ring; + ring->head += max_buf; + + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + if ((err = memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (rx, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_ne (bufs[i].data, NULL); + + /* test receive qid 2 (negative) */ + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + if ((err = memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_QID, + "err code: %u, err msg: %s", err, memif_strerror (err)); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + free (bufs); + bufs = NULL; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_buffer_free) +{ + int err, i; + uint16_t max_buf = 10, buf, rx; + uint8_t qid; + memif_buffer_t *bufs; + memif_queue_t *mq; + memif_ring_t *ring; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + c->fd = 69; + + /* test buffer free qid 0 (positive) */ + qid = 0; + mq = &c->rx_queues[qid]; + ring = mq->ring; + ring->head += 10; + + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + if ((err = memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (rx, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_ne (bufs[i].data, NULL); + + if ((err = memif_buffer_free (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (buf, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_eq (bufs[i].data, NULL); + ck_assert_uint_eq (ring->head, ring->tail); + + /* test buffer free qid 1 (positive) */ + qid = 1; + mq = &c->rx_queues[qid]; + ring = mq->ring; + ring->head += 10; + + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + if ((err = memif_rx_burst (conn, qid, bufs, max_buf, &rx)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (rx, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_ne (bufs[i].data, NULL); + + if ((err = memif_buffer_free (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_uint_eq (buf, max_buf); + for (i = 0; i < max_buf; i++) + ck_assert_ptr_eq (bufs[i].data, NULL); + ck_assert_uint_eq (ring->head, ring->tail); + + + /* test buffer free qid 2 (negative) */ + qid = 2; + free (bufs); + bufs = malloc (sizeof (memif_buffer_t) * max_buf); + + if ((err = memif_buffer_free (conn, qid, bufs, max_buf, &buf)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_QID, + "err code: %u, err msg: %s", err, memif_strerror (err)); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + free (bufs); + bufs = NULL; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_get_details) +{ + int err; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + memif_details_t md; + memset (&md, 0, sizeof (md)); + ssize_t buflen = 2048; + char *buf = malloc (buflen); + memset (buf, 0, buflen); + + if ((err = memif_get_details (conn, &md, buf, buflen)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_str_eq (md.if_name, c->args.interface_name); + ck_assert_str_eq (md.inst_name, c->args.instance_name); + ck_assert_str_eq (md.remote_if_name, c->remote_if_name); + ck_assert_str_eq (md.remote_inst_name, c->remote_name); + ck_assert_str_eq (md.secret, c->args.secret); + ck_assert_str_eq (md.socket_filename, c->args.socket_filename); + + ck_assert_uint_eq (md.id, c->args.interface_id); + ck_assert_uint_ne (md.role, c->args.is_master); + ck_assert_uint_eq (md.mode, c->args.mode); + ck_assert_uint_eq (md.ring_size, (1 << c->args.log2_ring_size)); + ck_assert_uint_eq (md.buffer_size, c->args.buffer_size); + ck_assert_uint_eq (md.tx_queues, c->args.num_s2m_rings); + ck_assert_uint_eq (md.rx_queues, c->args.num_m2s_rings); + ck_assert_uint_eq (md.link_up_down, 0); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_init_regions_and_queues) +{ + int err; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_ptr_ne (c->regions, NULL); + ck_assert_ptr_ne (c->tx_queues, NULL); + ck_assert_ptr_ne (c->rx_queues, NULL); + + ck_assert_ptr_ne (c->regions->shm, NULL); + ck_assert_ptr_ne (c->tx_queues->ring, NULL); + ck_assert_ptr_ne (c->rx_queues->ring, NULL); + + ck_assert_int_ne (c->regions->fd, -1); + ck_assert_uint_eq (c->tx_queues->ring->cookie, MEMIF_COOKIE); + ck_assert_uint_eq (c->rx_queues->ring->cookie, MEMIF_COOKIE); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_connect1) +{ + int err; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + if ((err = memif_connect1 (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +START_TEST (test_disconnect_internal) +{ + int err; + ready_called = 0; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.num_s2m_rings = 2; + args.num_m2s_rings = 2; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + if ((err = memif_disconnect_internal (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_int_eq (c->fd, -1); + + ck_assert_ptr_eq (c->tx_queues, NULL); + ck_assert_ptr_eq (c->rx_queues, NULL); + ck_assert_ptr_eq (c->regions, NULL); + ck_assert_ptr_eq (c->msg_queue, NULL); + + struct itimerspec timer; + timerfd_gettime (lm->timerfd, &timer); + + ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec, + "timerfd not armed!"); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + +Suite * +main_suite () +{ + Suite *s; + + TCase *tc_api; + TCase *tc_internal; + + /* create main test suite */ + s = suite_create ("Libmemif main"); + + /* create api test case */ + tc_api = tcase_create ("Api calls"); + /* add tests to test case */ + tcase_add_test (tc_api, test_init); + tcase_add_test (tc_api, test_init_epoll); + tcase_add_test (tc_api, test_create); + tcase_add_test (tc_api, test_control_fd_handler); + tcase_add_test (tc_api, test_buffer_alloc); + tcase_add_test (tc_api, test_tx_burst); + tcase_add_test (tc_api, test_rx_burst); + tcase_add_test (tc_api, test_buffer_free); + tcase_add_test (tc_api, test_get_details); + + /* create internal test case */ + tc_internal = tcase_create ("Internal"); + /* add tests to test case */ + tcase_add_test (tc_internal, test_init_regions_and_queues); + tcase_add_test (tc_internal, test_connect1); + tcase_add_test (tc_internal, test_disconnect_internal); + + /* add test cases to test suite */ + suite_add_tcase (s, tc_api); + suite_add_tcase (s, tc_internal); + + /* return main test suite to test runner */ + return s; +} diff --git a/test/main_test.h b/test/main_test.h new file mode 100644 index 0000000..4b81af5 --- /dev/null +++ b/test/main_test.h @@ -0,0 +1,25 @@ +/* + *------------------------------------------------------------------ + * Copyright (c) 2017 Pantheon technologies. + * 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. + *------------------------------------------------------------------ + */ + +#ifndef _MAIN_TEST_H_ +#define _MAIN_TEST_H_ + +#include + +Suite * main_suite (); + +#endif /* _MAIN_TEST_H_ */ diff --git a/test/socket_test.c b/test/socket_test.c index b086a93..7700c9e 100644 --- a/test/socket_test.c +++ b/test/socket_test.c @@ -15,25 +15,10 @@ *------------------------------------------------------------------ */ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include -#define TEST_SOCK_DIR "/libmemif/memif.sock" +#include +#include static int get_queue_len (memif_msg_queue_elt_t *q) @@ -48,10 +33,19 @@ get_queue_len (memif_msg_queue_elt_t *q) return r; } -static int -test_msg_queue_add_pop () +static void +queue_free (memif_msg_queue_elt_t **e) +{ + if (*e == NULL) + return; + queue_free (&(*e)->next); + free (*e); + *e = NULL; + return; +} + +START_TEST (test_msg_queue) { - int rv = 0; memif_connection_t conn; conn.msg_queue = NULL; conn.fd = -1; @@ -66,11 +60,7 @@ test_msg_queue_add_pop () memif_msg_enq_init (&conn); } - if (len != get_queue_len (conn.msg_queue)) - { - ERROR("incorrect queue len"); - rv = -1; - } + ck_assert_int_eq (len, get_queue_len (conn.msg_queue)); int pop = 6; @@ -78,63 +68,42 @@ test_msg_queue_add_pop () { if (i % 2) { - if (conn.msg_queue->msg.type != MEMIF_MSG_TYPE_ACK) - { - ERROR ("incorrect msg type"); - rv = -1; - } + ck_assert_uint_eq (conn.msg_queue->msg.type, MEMIF_MSG_TYPE_ACK); } else { - if (conn.msg_queue->msg.type != MEMIF_MSG_TYPE_INIT) - { - ERROR ("incorrect msg type"); - rv = -1; - } + ck_assert_uint_eq (conn.msg_queue->msg.type, MEMIF_MSG_TYPE_INIT); } conn.flags |= MEMIF_CONNECTION_FLAG_WRITE; /* function will return -1 because no socket is created */ memif_conn_fd_write_ready (&conn); } - if ((len - pop) != get_queue_len (conn.msg_queue)) - { - ERROR("incorrect queue_len"); - rv = -1; - } - - return rv; + ck_assert_int_eq ((len - pop), get_queue_len (conn.msg_queue)); + queue_free (&conn.msg_queue); } +END_TEST -static int -test_msg_enq_ack () +START_TEST (test_enq_ack) { - int rv = 0; + int err; memif_connection_t conn; conn.msg_queue = NULL; - memif_msg_enq_ack (&conn); + if ((err = memif_msg_enq_ack (&conn)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); memif_msg_queue_elt_t *e = conn.msg_queue; - if (e->msg.type != MEMIF_MSG_TYPE_ACK) - { - ERROR ("incorrect msg type"); - rv = -1; - } - if (e->fd != -1) - { - ERROR ("incorrect file descriptor"); - rv = -1; - } - return rv; + ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_ACK); + ck_assert_int_eq (e->fd, -1); + queue_free (&conn.msg_queue); } +END_TEST - -static int -test_msg_enq_init () +START_TEST (test_enq_init) { - int rv = 0; + int err; memif_connection_t conn; conn.msg_queue = NULL; @@ -144,170 +113,177 @@ test_msg_enq_init () strncpy ((char *)conn.args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); strncpy ((char *) conn.args.secret, TEST_SECRET, strlen (TEST_SECRET)); - memif_msg_enq_init (&conn); + if ((err = memif_msg_enq_init (&conn)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + memif_msg_queue_elt_t *e = conn.msg_queue; + + ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_INIT); + ck_assert_int_eq (e->fd, -1); - if (e->msg.type != MEMIF_MSG_TYPE_INIT) - { - ERROR ("incorrect msg type"); - rv = -1; - } - if (e->fd != -1) - { - ERROR ("incorrect file descriptor"); - rv = -1; - } memif_msg_init_t *i = &e->msg.init; - if (i->version != MEMIF_VERSION) - { - ERROR ("incorrect memif version"); - rv = -1; - } - if (i->id != conn.args.interface_id) - { - ERROR ("incorrect interface id"); - rv = -1; - } - if (i->mode != conn.args.mode) - { - ERROR ("incorrect mode"); - rv = -1; - } - if (strncmp ((char *) i->name, (char *) conn.args.instance_name, - strlen ((char *) i->name)) != 0) - { - ERROR ("incorrect interface name"); - rv = -1; - } - if (strncmp ((char *) i->secret, (char *) conn.args.secret, - strlen ((char *) i->secret)) != 0) - { - ERROR ("incorrect secret"); - rv = -1; - } - return rv; + ck_assert_uint_eq (i->version, MEMIF_VERSION); + ck_assert_uint_eq (i->id, conn.args.interface_id); + ck_assert_uint_eq (i->mode, conn.args.mode); + ck_assert_str_eq (i->name, conn.args.instance_name); + ck_assert_str_eq (i->secret, conn.args.secret); + queue_free (&conn.msg_queue); } +END_TEST -static int -test_msg_enq_add_region () +START_TEST (test_enq_add_region) { - int rv = 0, frv = 0; + int err; memif_connection_t conn; conn.msg_queue = NULL; conn.regions = (memif_region_t *) malloc (sizeof(memif_region_t)); memif_region_t *mr = conn.regions; mr->fd = 5; mr->region_size = 2048; - mr->next = NULL; uint8_t region_index = 0; - frv = memif_msg_enq_add_region (&conn, region_index); - if (frv < 0) - { - ERROR ("function error"); - rv = -1; - } + if ((err = memif_msg_enq_add_region (&conn, region_index)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + memif_msg_queue_elt_t *e = conn.msg_queue; - if (e->msg.type != MEMIF_MSG_TYPE_ADD_REGION) - { - ERROR ("incorrect msg type"); - rv = -1; - } - if (e->fd != mr->fd) - { - ERROR ("file descriptor mismatch"); - rv = -1; - } + + ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_ADD_REGION); + ck_assert_int_eq (e->fd, mr->fd); + memif_msg_add_region_t *ar = &e->msg.add_region; - if (ar->index != region_index) - { - ERROR ("region index mismatch"); - rv = -1; - } - if (ar->size != mr->region_size) - { - ERROR ("region size mismatch"); - rv = -1; - } - region_index = 9; - frv = memif_msg_enq_add_region (&conn, region_index); - if (frv == 0) - { - ERROR ("invalid region_index (success fn)"); - rv = -1; - } - region_index = 0; + ck_assert_uint_eq (ar->index, region_index); + ck_assert_uint_eq (ar->size, mr->region_size); free (conn.regions); conn.regions = NULL; mr = NULL; - frv = memif_msg_enq_add_region (&conn, region_index); - if (frv == 0) - { - ERROR ("invalid region (success fn)"); - rv = -1; - } + queue_free (&conn.msg_queue); +} +END_TEST + +START_TEST (test_enq_add_ring) +{ + int err; + memif_connection_t conn; + conn.msg_queue = NULL; + conn.rx_queues = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + conn.tx_queues = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + + memif_queue_t *mq = conn.tx_queues; + uint8_t dir = MEMIF_RING_S2M; + mq->int_fd = 5; + mq->offset = 0; + mq->log2_ring_size = 10; + + if ((err = memif_msg_enq_add_ring (&conn, 0, dir)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_msg_queue_elt_t *e = conn.msg_queue; + + ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_ADD_RING); + ck_assert_int_eq (e->fd, mq->int_fd); + + memif_msg_add_ring_t *ar = &e->msg.add_ring; + + ck_assert_uint_eq (ar->index, 0); + ck_assert_uint_eq (ar->offset, mq->offset); + ck_assert_uint_eq (ar->log2_ring_size, mq->log2_ring_size); + ck_assert (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M); - return rv; + dir = MEMIF_RING_M2S; + if ((err = memif_msg_enq_add_ring (&conn, 0, dir)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + queue_free (&conn.msg_queue); } +END_TEST -static int -test_msg_receive_add_region () +START_TEST (test_enq_connect) { - int rv = 0, frv = 0; + int err; memif_connection_t conn; - conn.regions = NULL; - memif_msg_t msg; - msg.type = MEMIF_MSG_TYPE_ADD_REGION; - msg.add_region.size = 2048; - msg.add_region.index = 0; + conn.msg_queue = NULL; + memset (conn.args.interface_name, 0, sizeof (conn.args.interface_name)); + strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); - int fd = 5; + if ((err = memif_msg_enq_connect (&conn)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); - frv = memif_msg_receive_add_region (&conn, &msg, fd); - if (frv < 0) - { - ERROR ("fuinction error"); - rv = -1; - } + memif_msg_queue_elt_t *e = conn.msg_queue; - msg.add_region.index = 9; - frv = memif_msg_receive_add_region (&conn, &msg, fd); - if (frv == 0) - { - ERROR ("invalid region index (success fn)"); - rv = -1; - } - - memif_region_t *mr = conn.regions; - if (mr->fd != fd) - { - ERROR ("incorrect file descriptor"); - rv = -1; - } - if (mr->region_size != 2048) - { - ERROR ("incorrect region size"); - rv = -1; - } - if (mr->shm != NULL) - { - ERROR ("invalid shm"); - rv = -1; - } - - return rv; + ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_CONNECT); + ck_assert_int_eq (e->fd, -1); + ck_assert_str_eq (e->msg.connect.if_name, TEST_IF_NAME); + queue_free (&conn.msg_queue); } +END_TEST -static int -test_msg_receive_hello () +START_TEST (test_enq_connected) { - int rv = 0; + int err; memif_connection_t conn; conn.msg_queue = NULL; + memset (conn.args.interface_name, 0, sizeof (conn.args.interface_name)); + strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + if ((err = memif_msg_enq_connected (&conn)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_msg_queue_elt_t *e = conn.msg_queue; + + ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_CONNECTED); + ck_assert_int_eq (e->fd, -1); + ck_assert_str_eq (e->msg.connect.if_name, TEST_IF_NAME); + queue_free (&conn.msg_queue); +} +END_TEST + +START_TEST (test_send) +{ + int err; + int fd = -1, afd = 5; + memif_msg_t msg; + memset (&msg, 0, sizeof (msg)); + + if ((err = memif_msg_send (fd, &msg, afd)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_BAD_FD, + "err code: %u, err msg: %s", err, memif_strerror (err)); +} +END_TEST + +START_TEST (test_send_hello) +{ + int err; + memif_connection_t conn; + conn.fd = -1; + memset (conn.args.instance_name, 0, sizeof (conn.args.instance_name)); + strncpy ((char *) conn.args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_msg_send_hello (&conn)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_BAD_FD, + "err code: %u, err msg: %s", err, memif_strerror (err)); +} +END_TEST + +START_TEST (test_send_disconnect) +{ + int err; + memif_connection_t conn; + conn.fd = -1; + + /* only possible fail if memif_msg_send fails... */ + /* obsolete without socket */ + if ((err = memif_msg_send_disconnect (&conn, "unit_test_dc", 0)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_BAD_FD, + "err code: %u, err msg: %s", err, memif_strerror (err)); +} +END_TEST + +START_TEST (test_recv_hello) +{ + int err; + memif_connection_t conn; memif_msg_t msg; memif_msg_hello_t *h = &msg.hello; @@ -325,50 +301,25 @@ test_msg_receive_hello () conn.args.num_m2s_rings = 6; conn.args.log2_ring_size = 10; - rv = memif_msg_receive_hello (&conn, &msg); - if (rv < 0) - { - ERROR ("memif protocol mismatch"); - return rv; - } + if ((err = memif_msg_receive_hello (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); - if (conn.args.num_s2m_rings != 2) - { - ERROR ("incorrect number of slave to master rings"); - rv = -1; - } - if (conn.args.num_m2s_rings != 2) - { - ERROR ("incorrect number of master to slave rings"); - rv = -1; - } - if (conn.args.log2_ring_size != 10) - { - ERROR ("incorrect ring size"); - rv = -1; - } - if (strncmp ((char *) conn.remote_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0) - { - ERROR ("incorrect remote name"); - rv = -1; - } + ck_assert_uint_eq (conn.args.num_s2m_rings, 2); + ck_assert_uint_eq (conn.args.num_m2s_rings, 2); + ck_assert_uint_eq (conn.args.log2_ring_size, 10); + ck_assert_str_eq (conn.remote_name, TEST_IF_NAME); h->max_version = 9; - if (memif_msg_receive_hello (&conn, &msg) == 0) - { - ERROR ("no error on protocol mismatch"); - rv = -1; - } - - return rv; + if ((err = memif_msg_receive_hello (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_PROTO, + "err code: %u, err msg: %s", err, memif_strerror (err)); } +END_TEST -static int -test_msg_receive_init () +START_TEST (test_recv_init) { - int rv = 0, frv = 0; + int err; memif_connection_t conn; - conn.msg_queue = NULL; conn.args.interface_id = 69; conn.args.is_master = 1; @@ -377,7 +328,6 @@ test_msg_receive_init () memset (conn.args.secret, '\0', 24); strncpy ((char *) conn.args.secret, TEST_SECRET, strlen (TEST_SECRET)); - memif_msg_t msg; memif_msg_init_t *i = &msg.init; @@ -392,434 +342,240 @@ test_msg_receive_init () strncpy ((char *) i->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); - frv = memif_msg_receive_init (&conn, &msg); - if (frv < 0) - { - ERROR ("function error"); - rv = -1; - } + if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + i->version = 9; - frv = memif_msg_receive_init (&conn, &msg); - if (frv == 0) - { - ERROR ("version mismatch (fn succes)"); - rv = -1; - } + if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_PROTO, + "err code: %u, err msg: %s", err, memif_strerror (err)); i->version = MEMIF_VERSION; + i->id = 78; - frv = memif_msg_receive_init (&conn, &msg); - if (frv == 0) - { - ERROR ("id mismatch (fn success)"); - rv = -1; - } + if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_ID, + "err code: %u, err msg: %s", err, memif_strerror (err)); i->id = 69; + i->mode = 1; - frv = memif_msg_receive_init (&conn, &msg); - if (frv == 0) - { - ERROR ("mode mismatch (fn success)"); - rv = -1; - } + if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_MODE, + "err code: %u, err msg: %s", err, memif_strerror (err)); i->mode = 0; + i->secret[0] = '\0'; - frv = memif_msg_receive_init (&conn, &msg); - if (frv == 0) - { - ERROR ("secret mismatch (fn success)"); - rv = -1; - } + if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_SECRET, + "err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); + conn.args.is_master = 0; - frv = memif_msg_receive_init (&conn, &msg); - if (frv == 0) - { - ERROR ("slave cannot accept connection (fn success)"); - rv = -1; - } + if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_ACCSLAVE, + "err code: %u, err msg: %s", err, memif_strerror (err)); conn.args.is_master = 1; + conn.fd = 5; - frv = memif_msg_receive_init (&conn, &msg); - if (frv == 0) - { - ERROR ("already connected (fn success)"); - rv = -1; - } - return rv; + if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg ((err == MEMIF_ERR_ALRCONN) || (err == MEMIF_ERR_BAD_FD), + "err code: %u, err msg: %s", err, memif_strerror (err)); } +END_TEST -static int -test_msg_enq_add_ring () +START_TEST (test_recv_add_region) { - int rv = 0, frv = 0; + int err; memif_connection_t conn; - conn.msg_queue = NULL; - conn.rx_queues = NULL; - conn.tx_queues = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + conn.regions = NULL; + memif_msg_t msg; + msg.type = MEMIF_MSG_TYPE_ADD_REGION; + msg.add_region.size = 2048; + msg.add_region.index = 0; - memif_queue_t *mq = conn.tx_queues; - uint8_t dir = MEMIF_RING_S2M; - mq->int_fd = 5; - mq->offset = 0; - mq->log2_ring_size = 10; + int fd = 5; - frv = memif_msg_enq_add_ring (&conn, 0, dir); - if (frv < 0) - { - ERROR ("function error"); - rv = -1; - } - memif_msg_queue_elt_t *e = conn.msg_queue; - if (e->msg.type != MEMIF_MSG_TYPE_ADD_RING) - { - ERROR ("incorrect msg type"); - rv = -1; - } - if (e->fd != mq->int_fd) - { - ERROR ("incorrect int fd"); - rv = -1; - } - memif_msg_add_ring_t *ar = &e->msg.add_ring; - if (ar->index != 0) - { - ERROR ("incorrect queue index"); - rv = -1; - } - if (ar->offset != mq->offset) - { - ERROR ("ring offset mismatch"); - rv = -1; - } - if (ar->log2_ring_size != mq->log2_ring_size) - { - ERROR ("ring size mismatch"); - rv = -1; - } - if ((ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) == 0) - { - ERROR ("incorrect ring flag"); - rv = -1; - } - - dir = MEMIF_RING_M2S; - frv = memif_msg_enq_add_ring (&conn, 0, dir); - if (frv == 0) - { - ERROR ("uninitialized queue (success fn)"); - rv = -1; - } - dir = MEMIF_RING_S2M; - frv = memif_msg_enq_add_ring (&conn, 9, dir); - if (frv == 0) - { - ERROR ("invalid queue index (success fn)"); - rv = -1; - } + if ((err = memif_msg_receive_add_region (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); - return rv; + memif_region_t *mr = conn.regions; + + ck_assert_uint_eq (mr->fd, fd); + ck_assert_uint_eq (mr->region_size, 2048); + ck_assert_ptr_eq (mr->shm, NULL); } +END_TEST -static int -test_msg_enq_connect () +START_TEST (test_recv_add_ring) { - int rv = 0; + int err; memif_connection_t conn; - conn.msg_queue = NULL; - strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + int fd = 5; + memif_msg_t msg; - memif_msg_enq_connect (&conn); - memif_msg_queue_elt_t *e = conn.msg_queue; - if (e->msg.type != MEMIF_MSG_TYPE_CONNECT) - { - ERROR ("incorrect msg type"); - rv = -1; - } - if (e->fd != -1) - { - ERROR ("invalid file descriptor"); - rv = -1; - } - if (strncmp ((char *) e->msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0) - { - ERROR ("incorrect interface name"); - rv = -1; - } - return rv; -} + msg.type = MEMIF_MSG_TYPE_ADD_RING; + memif_msg_add_ring_t *ar = &msg.add_ring; -static int -test_msg_enq_connected () -{ - int rv = 0; - memif_connection_t conn; - conn.msg_queue = NULL; - strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + ar->log2_ring_size = 10; + ar->region = 0; + ar->offset = 0; + ar->flags = 0; + ar->flags |= MEMIF_MSG_ADD_RING_FLAG_S2M; - memif_msg_enq_connected (&conn); - memif_msg_queue_elt_t *e = conn.msg_queue; - if (e->msg.type != MEMIF_MSG_TYPE_CONNECTED) - { - ERROR ("incorrect msg type"); - rv = -1; - } - if (e->fd != -1) - { - ERROR ("invalid file descriptor"); - rv = -1; - } - if (strncmp ((char *) e->msg.connected.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0) - { - ERROR ("incorrect interface name"); - rv = -1; - } - return rv; -} + if ((err = memif_msg_receive_add_ring (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); -static int -test_msg_send_disconnect () -{ - int rv = 0, frv = 0; - memif_connection_t conn; - conn.fd = -1; - /* only possible fail if memif_msg_send fails... */ - /* obsolete without socket */ - frv = memif_msg_send_disconnect (&conn); - if (frv < 0) - { - /*ERROR ("function error");*/ - /*rv = -1;*/ - rv = 0; - } - return rv; + ar->offset = 2048; + ar->flags &= ~MEMIF_MSG_ADD_RING_FLAG_S2M; + + if ((err = memif_msg_receive_add_ring (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + } +END_TEST -static int -test_msg_receive_connect () +START_TEST (test_recv_connect) { - int rv = 0, frv = 0; - memif_connection_t conn; - conn.regions = NULL; - conn.tx_queues = NULL; - conn.rx_queues = NULL; - memif_msg_t msg; + int err; + memif_conn_handle_t c = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + + args.interface_id = 0; + args.is_master = 0; + args.mode = 0; + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + if ((err = memif_create (&c, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *conn = (memif_connection_t *) c; + + if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_msg_t msg; + memset (&msg, 0, sizeof (msg)); msg.type = MEMIF_MSG_TYPE_CONNECT; + + memset (msg.connect.if_name, 0, sizeof (msg.connect.if_name)); strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); - frv = memif_msg_receive_connect (&conn, &msg); - if (frv < 0) - { - /* fail only if memif_connect1 fails - (obsolete test) */ - ERROR ("function error"); - rv = -1; - } - if (strncmp ((char *) conn.remote_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0 ) - { - ERROR ("incorrect remote interface name"); - rv = -1; - } - return rv; + + if ((err = memif_msg_receive_connect (conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_str_eq (conn->remote_if_name, TEST_IF_NAME); } +END_TEST -static int -test_msg_receive_connected () +START_TEST (test_recv_connected) { - int rv = 0, frv = 0; - memif_connection_t conn; - conn.regions = NULL; - conn.tx_queues = NULL; - conn.rx_queues = NULL; + int err; + memif_conn_handle_t c = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + + args.interface_id = 0; + args.is_master = 0; + args.mode = 0; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + if ((err = memif_create (&c, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *conn = (memif_connection_t *) c; + + if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + memif_msg_t msg; + memset (&msg, 0, sizeof (msg)); + msg.type = MEMIF_MSG_TYPE_CONNECT; - msg.type = MEMIF_MSG_TYPE_CONNECTED; + memset (msg.connect.if_name, 0, sizeof (msg.connect.if_name)); strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); - - frv = memif_msg_receive_connected (&conn, &msg); - if (frv < 0) - { - /* fail only if memif_connect1 fails - (obsolete test) */ - ERROR ("function error"); - rv = -1; - } - if (strncmp ((char *) conn.remote_name, TEST_IF_NAME, strlen (TEST_IF_NAME)) != 0 ) - { - ERROR ("incorrect remote interface name"); - rv = -1; - } - return rv; + + if ((err = memif_msg_receive_connected (conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_str_eq (conn->remote_if_name, TEST_IF_NAME); } +END_TEST -static int -test_msg_receive_disconnect () +START_TEST (test_recv_disconnect) { - int rv = 0, frv = 0; + int err; memif_connection_t conn; memif_msg_t msg; msg.type = MEMIF_MSG_TYPE_DISCONNECT; - strncpy ((char *) msg.disconnect.string, "DC", 2); + memset (msg.disconnect.string, 0, sizeof (msg.disconnect.string)); + strncpy ((char *) msg.disconnect.string, "unit_test_dc", 12); - frv = memif_msg_receive_disconnect (&conn, &msg); - if (frv == 0) - { - /* function can only return -1 */ - ERROR ("not possible"); - rv = -1; - } - if (strncmp ((char *) conn.remote_disconnect_string, "DC", 2) != 0) - { - ERROR ("incorrect disconnect string"); - rv = -1; - } - return rv; + if ((err = memif_msg_receive_disconnect (&conn, &msg)) != MEMIF_ERR_SUCCESS) + ck_assert_msg (err == MEMIF_ERR_DISCONNECT, + "err code: %u, err msg: %s", err, memif_strerror (err)); + + ck_assert_str_eq (conn.remote_disconnect_string, "unit_test_dc"); } +END_TEST -int -test_socket (uint16_t *s, uint16_t *f) +Suite * +socket_suite () { - TEST_SET ("SOCKET.C"); + Suite *s; + TCase *tc_msg_queue; + TCase *tc_msg_enq; + TCase *tc_msg_send; + TCase *tc_msg_recv; + + /* create socket test suite */ + s = suite_create ("Socket messaging"); - if (test_msg_queue_add_pop () < 0) - { - (*f)++; - TEST_FAIL ("msg queue add/pop"); - } - else - { - (*s)++; - TEST_OK ("msg queue add/pop"); - } - if (test_msg_enq_ack () < 0) - { - (*f)++; - TEST_FAIL ("msg enq ack"); - } - else - { - (*s)++; - TEST_OK ("msg enq ack"); - } - if (test_msg_enq_init () < 0) - { - (*f)++; - TEST_FAIL ("msg enq init"); - } - else - { - (*s)++; - TEST_OK ("msg enq init"); - } - if (test_msg_enq_add_region () < 0) - { - (*f)++; - TEST_FAIL ("msg enq add region"); - } - else - { - (*s)++; - TEST_OK ("msg enq add region"); - } - if (test_msg_enq_add_ring () < 0) - { - (*f)++; - TEST_FAIL ("msg enq add ring"); - } - else - { - (*s)++; - TEST_OK ("msg enq add ring"); - } - if (test_msg_receive_hello () < 0) - { - (*f)++; - TEST_FAIL ("msg recv hello"); - } - else - { - (*s)++; - TEST_OK ("msg recv hello"); - } - if (test_msg_receive_init () < 0) - { - (*f)++; - TEST_FAIL ("msg recv init"); - } - else - { - (*s)++; - TEST_OK ("msg recv init"); - } - if (test_msg_receive_add_region () < 0) - { - (*f)++; - TEST_FAIL ("msg recv add region"); - } - else - { - (*s)++; - TEST_OK ("msg recv add region"); - } - if (test_msg_enq_connect () < 0) - { - (*f)++; - TEST_FAIL ("msg enq connect"); - } - else - { - (*s)++; - TEST_OK ("msg enq connect"); - } - if (test_msg_enq_connected () < 0) - { - (*f)++; - TEST_FAIL ("msg enq connected"); - } - else - { - (*s)++; - TEST_OK ("msg enq connected"); - } - if (test_msg_send_disconnect () < 0) - { - (*f)++; - TEST_FAIL ("msg send disconnect"); - } - else - { - (*s)++; - TEST_OK ("msg send disconnect"); - } - if (test_msg_receive_connect () < 0) - { - (*f)++; - TEST_FAIL ("msg recv connect"); - } - else - { - (*s)++; - TEST_OK ("msg recv connect"); - } - if (test_msg_receive_connected () < 0) - { - (*f)++; - TEST_FAIL ("msg recv connected"); - } - else - { - (*s)++; - TEST_OK ("msg recv connected"); - } - if (test_msg_receive_disconnect () < 0) - { - (*f)++; - TEST_FAIL ("msg recv disconnect"); - } - else - { - (*s)++; - TEST_OK ("msg recv disconnect"); - } - return 0; + /* create msg queue test case */ + tc_msg_queue = tcase_create ("Message queue"); + /* add tests to test case */ + tcase_add_test (tc_msg_queue, test_msg_queue); + + /* create msg enq test case */ + tc_msg_enq = tcase_create ("Message enqueue"); + /* add tests to test case */ + tcase_add_test (tc_msg_enq, test_enq_ack); + tcase_add_test (tc_msg_enq, test_enq_init); + tcase_add_test (tc_msg_enq, test_enq_add_region); + tcase_add_test (tc_msg_enq, test_enq_add_ring); + tcase_add_test (tc_msg_enq, test_enq_connect); + tcase_add_test (tc_msg_enq, test_enq_connected); + + /* create msg send test case */ + tc_msg_send = tcase_create ("Message send"); + /* add tests to test case */ + tcase_add_test (tc_msg_send, test_send); + tcase_add_test (tc_msg_send, test_send_hello); + tcase_add_test (tc_msg_send, test_send_disconnect); + + /* create msg recv test case */ + tc_msg_recv = tcase_create ("Message receive"); + /* add tests to test case */ + tcase_add_test (tc_msg_recv, test_recv_hello); + tcase_add_test (tc_msg_recv, test_recv_init); + tcase_add_test (tc_msg_recv, test_recv_add_region); + tcase_add_test (tc_msg_recv, test_recv_add_ring); + tcase_add_test (tc_msg_recv, test_recv_connect); + tcase_add_test (tc_msg_recv, test_recv_connected); + tcase_add_test (tc_msg_recv, test_recv_disconnect); + + /* add test cases to test suite */ + suite_add_tcase (s, tc_msg_queue); + suite_add_tcase (s, tc_msg_enq); + suite_add_tcase (s, tc_msg_send); + suite_add_tcase (s, tc_msg_recv); + + /* return socket test suite to test runner */ + return s; } diff --git a/test/socket_test.h b/test/socket_test.h index 69a82a1..2c25375 100644 --- a/test/socket_test.h +++ b/test/socket_test.h @@ -19,9 +19,7 @@ #define _SOCKET_TEST_H_ #include -#include -#include -int test_socket (uint16_t *success, uint16_t *fail); +Suite * socket_suite (); #endif /* _SOCKET_TEST_H_ */ diff --git a/test/unit_test.c b/test/unit_test.c index eec9b2f..e0ee3b4 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -15,21 +15,48 @@ *------------------------------------------------------------------ */ +#include #include +int +on_connect (memif_conn_handle_t conn, void *ctx) +{ + return 0; +} int -main (void) +on_disconnect (memif_conn_handle_t conn, void *ctx) { - uint16_t s = 0, f = 0; + return 0; +} -/* test_main (&s, &f);*/ +int +on_interrupt (memif_conn_handle_t conn, void *ctx, uint16_t qid) +{ + return 0; +} - test_socket (&s, &f); +int +control_fd_update (int fd, uint8_t events) +{ + return 0; +} - INFO ("Success: %u Fail: %u", s, f); +int main (void) +{ + int num_fail; + Suite *main, *socket; + SRunner *sr; - if (f != 0) - return -1; - return 0; + main = main_suite (); + socket = socket_suite (); + + sr = srunner_create (main); + + srunner_add_suite (sr, socket); + + srunner_run_all (sr, CK_VERBOSE); + num_fail = srunner_ntests_failed (sr); + srunner_free (sr); + return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/test/unit_test.h b/test/unit_test.h index 43954dd..3e00682 100644 --- a/test/unit_test.h +++ b/test/unit_test.h @@ -18,49 +18,21 @@ #ifndef _UNIT_TEST_H_ #define _UNIT_TEST_H_ -#include -#include +#include +#include -#define RED "\x1b[31m" -#define GREEN "\x1b[32m" -#define YELLOW "\x1b[33m" -#define DEFAULT "\x1b[0m" +#include -#define INFO(...) do { \ - printf (DEFAULT"info: "__VA_ARGS__); \ - printf ("\n"); \ - } while (0) - -#define TEST_SET(...) do { \ - printf (YELLOW"TEST SET: "__VA_ARGS__); \ - printf (DEFAULT"\n"); \ - printf ("=============================================\n"); \ - } while (0) - -#define TEST_OK(...) do { \ - printf ("%-40s", DEFAULT"test: "__VA_ARGS__); \ - printf ("\t"GREEN"OK!"DEFAULT"\n"); \ - } while (0) +#define TEST_APP_NAME "unit_test_app" +#define TEST_IF_NAME "unit_test_if" +#define TEST_SECRET "psst" -#define TEST_FAIL(...) do { \ - printf ("%-40s", DEFAULT"test: "__VA_ARGS__); \ - printf ("\t"RED"FAIL!"DEFAULT"\n"); \ - } while (0) \ +int on_connect (memif_conn_handle_t conn, void *ctx); -#define ERROR(...) do { \ - printf (RED"ERROR:"DEFAULT"%s:%d: ",__func__, __LINE__);\ - printf (__VA_ARGS__); \ - printf ("\n"); \ - } while (0) +int on_disconnect (memif_conn_handle_t conn, void *ctx); -#define UNIX_ERROR(...) do { \ - printf (RED"UNIX ERROR:"DEFAULT"%s:%d: ",__func__, __LINE__);\ - printf (__VA_ARGS__); \ - printf ("\n"); \ - } while (0) +int on_interrupt (memif_conn_handle_t conn, void *ctx, uint16_t qid); -#define TEST_APP_NAME "unit_test_app" -#define TEST_IF_NAME "unit_test_if" -#define TEST_SECRET "psst" +int control_fd_update (int fd, uint8_t events); #endif /* _UNIT_TEST_H_ */ From 5a56505a4bb272a60a139b9bc92f251ed7bdfc5c Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Fri, 28 Jul 2017 11:10:03 +0200 Subject: [PATCH 31/63] ODPM-461 --- README.md | 2 +- examples/icmp_responder/icmp_proto.c | 25 +- examples/icmp_responder/icmp_proto.h | 2 +- examples/icmp_responder/main.c | 329 +++++++++++++++++++-------- examples/icmp_responder2/main.c | 9 +- src/main.c | 271 +++++++++++++++++----- src/memif_private.h | 32 ++- src/socket.c | 8 +- 8 files changed, 497 insertions(+), 181 deletions(-) diff --git a/README.md b/README.md index f6df994..02a6f60 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Shared memory packet interface (memif) provides high performance packet transmit - [x] Interrupt mode support - [x] File descriptor event polling in libmemif (optional) - [x] Simplify file descriptor event polling (one handler for control and interrupt channel) -- [ ] Multiple connections +- [x] Multiple connections - [x] Multipe queues - [ ] Multithread support - [ ] Master mode diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c index 9a6b3bc..99ec422 100644 --- a/examples/icmp_responder/icmp_proto.c +++ b/examples/icmp_responder/icmp_proto.c @@ -124,7 +124,7 @@ resolve_arp (void *arp) } static ssize_t -resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp) +resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp, uint8_t ip_addr[4]) { struct ether_arp *resp = (struct ether_arp *) eth_arp_resp; @@ -136,11 +136,6 @@ resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp) memcpy (resp->arp_sha, (((struct ether_header *) ( eth_arp_resp - sizeof (struct ether_header)))->ether_shost), 6); - uint8_t ip_addr[4]; - ip_addr[0] = 192; - ip_addr[1] = 168; - ip_addr[2] = 1; - ip_addr[3] = 2; memcpy (resp->arp_spa, ip_addr, 4); return sizeof (struct ether_arp); @@ -166,7 +161,7 @@ resolve_eth (struct ether_header *eth, void *eth_resp) } static ssize_t -resolve_ip (struct iphdr *ip, void *ip_resp) +resolve_ip (struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4]) { struct iphdr *resp = (struct iphdr *) ip_resp; resp->ihl = 5; @@ -178,10 +173,11 @@ resolve_ip (struct iphdr *ip, void *ip_resp) resp->frag_off = 0; resp->ttl = 0x40; resp->protocol = 1; - resp->saddr = 0xC0A80102; - resp->saddr = __bswap_32 (resp->saddr); - resp->daddr = 0xC0A80101; - resp->daddr = __bswap_32 (resp->daddr); + ((uint8_t *) &resp->saddr)[0] = ip_addr[0]; + ((uint8_t *) &resp->saddr)[1] = ip_addr[1]; + ((uint8_t *) &resp->saddr)[2] = ip_addr[2]; + ((uint8_t *) &resp->saddr)[3] = ip_addr[3]; + resp->daddr = ip->saddr; resp->check = cksum (resp, sizeof (struct iphdr)); @@ -203,7 +199,8 @@ resolve_icmp (struct icmphdr *icmp, void *icmp_resp) } int -resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size) +resolve_packet (void *in_pck, ssize_t in_size, + void *out_pck, uint32_t *out_size, uint8_t ip_addr[4]) { struct ether_header *eh; struct ether_arp *eah; @@ -217,7 +214,7 @@ resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size if (eh->ether_type == 0x0608) { eah = (struct ether_arp *) (in_pck + *out_size); - *out_size += resolve_eth_arp (eah, out_pck + *out_size); + *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr); } else if (eh->ether_type == 0x0008) @@ -226,7 +223,7 @@ resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size print_packet (in_pck + *out_size); #endif ip = (struct iphdr *) (in_pck + *out_size); - *out_size += resolve_ip (ip, out_pck + *out_size); + *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr); if (ip->protocol == 1) { icmp = (struct icmphdr *) (in_pck + *out_size); diff --git a/examples/icmp_responder/icmp_proto.h b/examples/icmp_responder/icmp_proto.h index ef4c7da..732d801 100644 --- a/examples/icmp_responder/icmp_proto.h +++ b/examples/icmp_responder/icmp_proto.h @@ -18,7 +18,7 @@ #ifndef _ICMP_PROTO_H_ #define _ICMP_PROTO_H_ -int resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size); +int resolve_packet (void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size, uint8_t ip_addr[4]); int print_packet (void *pck); diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index e99cee5..74d0c86 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -67,7 +67,8 @@ } while (0) /* maximum tx/rx memif buffers */ -#define MAX_MEMIF_BUFS 256 +#define MAX_MEMIF_BUFS 256 +#define MAX_CONNS 2 int epfd; @@ -88,75 +89,90 @@ typedef struct /* allcoated rx buffers counter */ /* number of rx buffers pointing to shared memory */ uint16_t rx_buf_num; + /* interface ip address */ + uint8_t ip_addr[4]; } memif_connection_t; -memif_connection_t memif_connection; +memif_connection_t memif_connection[MAX_CONNS]; static void print_memif_details () { - memif_connection_t *c = &memif_connection; + memif_details_t md; + ssize_t buflen; + char *buf; + int err, i; printf ("MEMIF DETAILS\n"); printf ("==============================\n"); + for (i = 0; i < MAX_CONNS; i++) + { + memif_connection_t *c = &memif_connection[i]; + printf ("interface index: %d\n", i); + memset (&md, 0, sizeof (md)); + buflen = 2048; + buf = malloc (buflen); + memset (buf, 0, buflen); - memif_details_t md; - memset (&md, 0, sizeof (md)); - ssize_t buflen = 2048; - char *buf = malloc (buflen); - memset (buf, 0, buflen); - int err; - - err = memif_get_details (c->conn, &md, buf, buflen); - if (err != MEMIF_ERR_SUCCESS) - { - INFO ("%s", memif_strerror (err)); - if (err == MEMIF_ERR_NOCONN) + err = memif_get_details (c->conn, &md, buf, buflen); + if (err != MEMIF_ERR_SUCCESS) { - free (buf); - return; + if (err == MEMIF_ERR_NOCONN) + { + printf ("\tno connection\n"); + free (buf); + continue; + } + else + { + INFO ("%s", memif_strerror (err)); + free (buf); + continue; + } } - } - 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 ("\tring_size: %u\n", md.ring_size); - printf ("\tbuffer_size: %u\n", md.buffer_size); - printf ("\trx queues: %u\n", md.rx_queues); - printf ("\ttx queues: %u\n", md.tx_queues); - printf ("\tlink: "); - if (md.link_up_down) - printf ("up\n"); - else - printf ("down\n"); + 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 ("\tring_size: %u\n", md.ring_size); + printf ("\tbuffer_size: %u\n", md.buffer_size); + printf ("\trx queues: %u\n", md.rx_queues); + printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\tlink: "); + if (md.link_up_down) + printf ("up\n"); + else + printf ("down\n"); - free (buf); + free (buf); + } } int @@ -260,9 +276,9 @@ control_fd_update (int fd, uint8_t events) } int -icmpr_buffer_alloc (long n, uint16_t qid) +icmpr_buffer_alloc (long index, long n, uint16_t qid) { - memif_connection_t *c = &memif_connection; + memif_connection_t *c = &memif_connection[index]; int err; uint16_t r; /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */ @@ -279,9 +295,9 @@ icmpr_buffer_alloc (long n, uint16_t qid) } int -icmpr_tx_burst (uint16_t qid) +icmpr_tx_burst (long index, uint16_t qid) { - memif_connection_t *c = &memif_connection; + memif_connection_t *c = &memif_connection[index]; int err; uint16_t r; /* inform peer memif interface about data in shared memory buffers */ @@ -299,7 +315,13 @@ icmpr_tx_burst (uint16_t qid) int on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) { - memif_connection_t *c = &memif_connection; + long index = *((long *) private_ctx); + memif_connection_t *c = &memif_connection[index]; + if (c->index != index) + { + INFO ("invalid context: %ld/%u", index, c->index); + return 0; + } int err; uint16_t rx; uint16_t fb; @@ -316,7 +338,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) DBG ("received %d buffers. %u/%u alloc/free buffers", rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - if (icmpr_buffer_alloc (rx, c->tx_qid) < 0) + if (icmpr_buffer_alloc (index, rx, c->tx_qid) < 0) { INFO ("buffer_alloc error"); goto error; @@ -326,7 +348,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) { resolve_packet ((void *) (c->rx_bufs + i)->data, (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, - &(c->tx_bufs + i)->data_len); + &(c->tx_bufs + i)->data_len, c->ip_addr); } /* mark memif buffers and shared memory buffers as free */ @@ -338,7 +360,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_tx_burst (c->tx_qid); + icmpr_tx_burst (index, c->tx_qid); return 0; @@ -353,13 +375,25 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) } int -icmpr_memif_create (int is_master) +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 = is_master; + args.is_master = 0; args.log2_ring_size = 10; args.buffer_size = 2048; args.num_s2m_rings = 2; @@ -371,22 +405,53 @@ icmpr_memif_create (int is_master) connect to VPP over memif. so default socket filename will be used */ /* default socketfile = /run/vpp/memif.sock */ - args.interface_id = 0; + 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 (&(&memif_connection)->conn, - &args, on_connect, on_disconnect, on_interrupt, NULL); + long *ctx = malloc (sizeof (long)); + *ctx = index; + int err = memif_create (&c->conn, + &args, on_connect, on_disconnect, on_interrupt, ctx); if (err != MEMIF_ERR_SUCCESS) + { INFO ("memif_create: %s", memif_strerror (err)); + return 0; + } + + c->index = index; + /* tx queue id */ + c->tx_qid = 0; + /* alloc memif buffers */ + c->rx_buf_num = 0; + c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->tx_buf_num = 0; + c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + + 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 () +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 (&(&memif_connection)->conn); + err = memif_delete (&c->conn); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_delete: %s", memif_strerror (err)); return 0; @@ -410,28 +475,92 @@ print_help () 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 ("\tconn - create memif (slave-mode)\n"); + printf ("\tdel - delete memif\n"); printf ("\tshow - show connection details\n"); - printf ("\ttx-qid - set transmit queue id\n"); + printf ("\ttx-qid - set transmit queue id (TODO)\n"); + printf ("\tip-set - set interface ip address\n"); } int icmpr_free () -{ +{ /* application cleanup */ - memif_connection_t *c = &memif_connection; - free (c->tx_bufs); - c->tx_bufs = NULL; - free (c->rx_bufs); - c->rx_bufs = NULL; + long i; + for (i = 0; i < MAX_CONNS; i++) + { + memif_connection_t *c = &memif_connection[i]; + icmpr_memif_delete (i); + free (c->tx_bufs); + c->tx_bufs = NULL; + free (c->rx_bufs); + c->rx_bufs = NULL; + } 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 () { - memif_connection_t *c = &memif_connection; + int i; char *in = (char *) malloc (256); char *ui = fgets (in, 256, stdin); char *end; @@ -441,7 +570,6 @@ user_input_handler () if (strncmp (ui, "exit", 4) == 0) { free (in); - icmpr_memif_delete (); icmpr_free (); exit (EXIT_SUCCESS); } @@ -452,12 +580,20 @@ user_input_handler () } else if (strncmp (ui, "conn", 4) == 0) { - icmpr_memif_create (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) { - icmpr_memif_delete (); + 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) @@ -469,8 +605,16 @@ user_input_handler () { ui = strtok (NULL, " "); if (ui != NULL) - c->tx_qid = strtol (ui, &end, 10); - INFO ("tx-qid %u", c->tx_qid); + /* TODO: set transmit qid */ + 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 @@ -488,7 +632,6 @@ user_input_handler () int poll_event (int timeout) { - memif_connection_t *c = &memif_connection; struct epoll_event evt, *e; int app_err = 0, memif_err = 0, en = 0; int tmp, nfd; @@ -547,18 +690,6 @@ int main () epfd = epoll_create (1); add_epoll_fd (0, EPOLLIN); - memif_connection_t *c = &memif_connection; - - /* initialize global memif connection handle */ - c->conn = NULL; - - c->tx_qid = 0; - /* alloc memif buffers */ - c->rx_buf_num = 0; - c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - c->tx_buf_num = 0; - c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - /* initialize memory interface */ int err; /* if valid callback is passed as argument, fd event polling will be done by user diff --git a/examples/icmp_responder2/main.c b/examples/icmp_responder2/main.c index 106b8ec..e09a35b 100644 --- a/examples/icmp_responder2/main.c +++ b/examples/icmp_responder2/main.c @@ -86,6 +86,8 @@ typedef struct /* allcoated rx buffers counter */ /* number of rx buffers pointing to shared memory */ uint16_t rx_buf_num; + /* interface ip address */ + uint8_t ip_addr[4]; } memif_connection_t; memif_connection_t memif_connection; @@ -287,7 +289,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) { resolve_packet ((void *) (c->rx_bufs + i)->data, (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, - &(c->tx_bufs + i)->data_len); + &(c->tx_bufs + i)->data_len, c->ip_addr); } uint16_t fb; @@ -362,7 +364,10 @@ int main (int argc, char *argv[]) c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); c->tx_buf_num = 0; c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - + c->ip_addr[0] = 192; + c->ip_addr[1] = 168; + c->ip_addr[2] = 1; + c->ip_addr[3] = 2; /* initialize memory interface */ int err; /* if valid callback is passed as argument, fd event polling will be done by user diff --git a/src/main.c b/src/main.c index 14485e1..c78932a 100644 --- a/src/main.c +++ b/src/main.c @@ -163,7 +163,7 @@ memif_strerror (int err_code) #define DBG_TX_BUF (0) #define DBG_RX_BUF (1) -#if MEMIF_DBG_SHM +#ifdef MEMIF_DBG_SHM static void print_bytes (void *data, uint16_t len, uint8_t q) { @@ -293,6 +293,84 @@ memif_control_fd_update (int fd, uint8_t events) return memif_add_epoll_fd (fd, evt); } +static int +add_fd_list_elt (memif_fd_list_elt_t *e, memif_fd_list_type_t type) +{ + libmemif_main_t *lm = &libmemif_main; + memif_fd_list_elt_t *list = + (type == MEMIF_FD_LIST_CONTROL) ? lm->control_list : lm->interrupt_list; + uint16_t len = + (type == MEMIF_FD_LIST_CONTROL) ? lm->control_list_len : lm->interrupt_list_len; + + int i; + for (i = 0; i < len; i++) + { + if (list[i].conn == NULL) + { + list[i].fd = e->fd; + list[i].conn = e->conn; + list[i].qid = e->qid; + return i; + } + } + memif_fd_list_elt_t *tmp; + tmp = realloc (list, sizeof (memif_fd_list_elt_t) * len * 2); + if (tmp == NULL) + return -1; + + tmp[len].fd = e->fd; + tmp[len].conn = e->conn; + tmp[len].qid = e->qid; + + if (type == MEMIF_FD_LIST_CONTROL) + { + lm->control_list = tmp; + lm->control_list_len *= 2; + } + else + { + lm->interrupt_list = tmp; + lm->interrupt_list_len *= 2; + } + + DBG ("con: %u, int: %u", lm->control_list_len, lm->interrupt_list_len); + + return len; +} + +static int +get_fd_list_elt (memif_fd_list_elt_t **e, memif_fd_list_elt_t *list, uint16_t len, int fd) +{ + int i; + for (i = 0; i < len; i++) + { + if (list[i].fd == fd) + { + *e = &list[i]; + return 0; + } + } + *e = NULL; + return -1; +} + +static int +free_fd_list_elt (memif_fd_list_elt_t *list, uint16_t len, int fd) +{ + int i; + for (i = 0; i < len; i++) + { + if (list[i].fd == fd) + { + list[i].fd = -1; + list[i].conn = NULL; + return 0; + } + } + + return -1; +} + static void memif_control_fd_update_register (memif_control_fd_update_t *cb) { @@ -315,7 +393,26 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) } memset (&lm->ms, 0, sizeof (memif_socket_t)); - lm->conn = NULL; + + lm->control_list_len = 1; + lm->interrupt_list_len = 1; + + lm->control_list = malloc (sizeof (memif_fd_list_elt_t) * lm->control_list_len); + lm->interrupt_list = malloc (sizeof (memif_fd_list_elt_t) * lm->interrupt_list_len); + + int i; + for (i = 0; i < lm->control_list_len; i++) + { + lm->control_list[i].fd = -1; + lm->control_list[i].conn = NULL; + } + for (i = 0; i < lm->interrupt_list_len; i++) + { + lm->interrupt_list[i].fd = -1; + lm->interrupt_list[i].conn = NULL; + } + + lm->disconn_slaves = 0; lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); if (lm->timerfd < 0) @@ -363,6 +460,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, { int err; int sockfd = -1; + memif_fd_list_elt_t list_elt; memif_connection_t *conn = (memif_connection_t *) *c; if (conn != NULL) { @@ -449,13 +547,27 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, strncpy ((char *) conn->args.secret, (char *) args->secret, l); } - if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + if (lm->disconn_slaves == 0) { - err = memif_syscall_error_handler (errno); + if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + } + + lm->disconn_slaves++; + + list_elt.fd = -1; + *c = list_elt.conn = conn; + int index; + if ((index = add_fd_list_elt (&list_elt, MEMIF_FD_LIST_CONTROL)) < 0) + { + err = MEMIF_ERR_NOMEM; goto error; } - *c = lm->conn = conn; + conn->index = index; return 0; @@ -471,11 +583,11 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, return err; } -/* TODO: support multiple interfaces */ int memif_control_fd_handler (int fd, uint8_t events) { int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */ + memif_fd_list_elt_t *e = NULL; memif_connection_t *conn; libmemif_main_t *lm = &libmemif_main; if (fd == lm->timerfd) @@ -483,9 +595,14 @@ memif_control_fd_handler (int fd, uint8_t events) uint64_t b; ssize_t size; size = read (fd, &b, sizeof (b)); - conn = lm->conn; - if (conn->fd < 0) + for (i = 0; i < 4; i++) + { + if ((lm->control_list[i].fd < 0) && (lm->control_list[i].conn != NULL)) { + conn = lm->control_list[i].conn; + if (conn->args.is_master) + continue; + struct sockaddr_un sun; sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); if (sockfd < 0) @@ -507,15 +624,19 @@ memif_control_fd_handler (int fd, uint8_t events) conn->write_fn = memif_conn_fd_write_ready; conn->error_fn = memif_conn_fd_error; + lm->control_list[conn->index].fd = conn->fd; + lm->control_fd_update ( sockfd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE); - /* TODO: with multiple connections support, - only disarm if there is no disconnected slave */ - if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + lm->disconn_slaves--; + if (lm->disconn_slaves == 0) { - err = memif_syscall_error_handler (errno); - goto error; + if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } } } else @@ -524,35 +645,35 @@ memif_control_fd_handler (int fd, uint8_t events) goto error; } } + } } else { - conn = lm->conn; - if (conn->rx_queues != NULL) + get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); + if (e != NULL) { - if (fd == conn->rx_queues->int_fd) - { - conn->on_interrupt ((void *) conn, conn->private_ctx, 0); - return MEMIF_ERR_SUCCESS; /* 0 */ - } + e->conn->on_interrupt ((void *) e->conn, e->conn->private_ctx, e->qid); + return MEMIF_ERR_SUCCESS; } - if (conn->fd == fd) + + get_fd_list_elt (&e, lm->control_list, lm->control_list_len, fd); + if (e != NULL) { if (events & MEMIF_FD_EVENT_READ) { - err = conn->read_fn (conn); + err = e->conn->read_fn (e->conn); if (err != MEMIF_ERR_SUCCESS) return err; } if (events & MEMIF_FD_EVENT_WRITE) { - err = conn->write_fn (conn); + err = e->conn->write_fn (e->conn); if (err != MEMIF_ERR_SUCCESS) return err; } if (events & MEMIF_FD_EVENT_ERROR) { - err = conn->error_fn (conn); + err = e->conn->error_fn (e->conn); if (err != MEMIF_ERR_SUCCESS) return err; } @@ -572,6 +693,7 @@ int memif_poll_event (int timeout) { libmemif_main_t *lm = &libmemif_main; + memif_fd_list_elt_t *elt; struct epoll_event evt, *e; int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */ uint32_t events = 0; @@ -587,25 +709,24 @@ memif_poll_event (int timeout) } if (en > 0) { - /* event on memif interrupt fd */ - if (lm->conn->rx_queues != NULL) + get_fd_list_elt (&elt, lm->interrupt_list, lm->interrupt_list_len, evt.data.fd); + if (elt != NULL) { - if (evt.data.fd == lm->conn->rx_queues->int_fd) - { - lm->conn->on_interrupt ((void *) lm->conn, - lm->conn->private_ctx, 0); - return 0; - } + elt->conn->on_interrupt ((void *) elt->conn, elt->conn->private_ctx, elt->qid); + return 0; + } + get_fd_list_elt (&elt, lm->control_list, lm->control_list_len, evt.data.fd); + if (elt != NULL) + { + 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; + err = memif_control_fd_handler (evt.data.fd, events); + return err; } - /* 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; - err = memif_control_fd_handler (evt.data.fd, events); } return 0; } @@ -623,7 +744,7 @@ memif_msg_queue_free (memif_msg_queue_elt_t **e) /* send disconnect msg and close interface */ int -memif_disconnect_internal (memif_connection_t *c) +memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) { if (c == NULL) { @@ -634,6 +755,7 @@ memif_disconnect_internal (memif_connection_t *c) int err = MEMIF_ERR_SUCCESS, i; /* 0 */ memif_queue_t *mq; libmemif_main_t *lm = &libmemif_main; + memif_fd_list_elt_t *e; if (c->fd > 0) { @@ -641,7 +763,13 @@ memif_disconnect_internal (memif_connection_t *c) lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL); close (c->fd); } - c->fd = -1; + get_fd_list_elt (&e, lm->control_list, lm->control_list_len, c->fd); + if (e != NULL) + { + e->fd = c->fd = -1; + if (is_del) + e->conn = NULL; + } if (c->tx_queues != NULL) { @@ -653,7 +781,12 @@ memif_disconnect_internal (memif_connection_t *c) { if (mq->int_fd > 0) close (mq->int_fd); - mq->int_fd = -1; + get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, mq->int_fd); + if (e != NULL) + { + e->fd = mq->int_fd = -1; + e->conn = NULL; + } } } free (c->tx_queues); @@ -673,7 +806,12 @@ memif_disconnect_internal (memif_connection_t *c) lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL); close (mq->int_fd); } - mq->int_fd = -1; + get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, mq->int_fd); + if (e != NULL) + { + e->fd = mq->int_fd = -1; + e->conn = NULL; + } } } free (c->rx_queues); @@ -693,13 +831,15 @@ memif_disconnect_internal (memif_connection_t *c) memif_msg_queue_free (&c->msg_queue); - /* TODO: use timerfd_gettime to check if timer is armed - only arm if timer is disarmed */ - if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + if (lm->disconn_slaves == 0) { - err = memif_syscall_error_handler (errno); - DBG_UNIX ("timerfd_settime: arm"); + if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + { + err = memif_syscall_error_handler (errno); + DBG_UNIX ("timerfd_settime: arm"); + } } + lm->disconn_slaves++; c->on_disconnect ((void *) c, c->private_ctx); @@ -714,15 +854,18 @@ memif_delete (memif_conn_handle_t *conn) int err; - err = memif_disconnect_internal (c); + err = memif_disconnect_internal (c, 1); if (err == MEMIF_ERR_NOCONN) return err; - /* TODO: only disarm if this is the only disconnected slave */ - if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + lm->disconn_slaves--; + if (lm->disconn_slaves == 0) { - err = memif_syscall_error_handler (errno); - DBG ("timerfd_settime: disarm"); + if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + { + err = memif_syscall_error_handler (errno); + DBG ("timerfd_settime: disarm"); + } } if (c->args.socket_filename) @@ -801,6 +944,8 @@ memif_init_regions_and_queues (memif_connection_t *conn) uint64_t buffer_offset; memif_region_t *r; int i,j; + libmemif_main_t *lm = &libmemif_main; + memif_fd_list_elt_t e; conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t)); if (conn->regions == NULL) @@ -817,10 +962,10 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); -/* + if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) return memif_syscall_error_handler (errno); -*/ + if ((ftruncate (r->fd, r->region_size)) == -1) return memif_syscall_error_handler (errno); @@ -867,6 +1012,12 @@ memif_init_regions_and_queues (memif_connection_t *conn) { if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); + /* add int fd to interrupt fd list */ + e.fd = mq[x].int_fd; + e.conn = conn; + e.qid = x; + add_fd_list_elt (&e, MEMIF_FD_LIST_INTERRUPT); + mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x); DBG ("RING: %p I: %d", mq[x].ring, x); mq[x].log2_ring_size = conn->args.log2_ring_size; @@ -884,6 +1035,12 @@ memif_init_regions_and_queues (memif_connection_t *conn) { if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); + /* add int fd to interrupt fd list */ + e.fd = mq[x].int_fd; + e.conn = conn; + e.qid = x; + add_fd_list_elt (&e, MEMIF_FD_LIST_INTERRUPT); + mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x); DBG ("RING: %p I: %d", mq[x].ring, x); mq[x].log2_ring_size = conn->args.log2_ring_size; diff --git a/src/memif_private.h b/src/memif_private.h index 619cc50..1887ef9 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -78,6 +78,12 @@ #endif /* MEMIF_DBG */ +typedef enum +{ + MEMIF_FD_LIST_CONTROL, + MEMIF_FD_LIST_INTERRUPT +} memif_fd_list_type_t; + typedef struct { void *shm; @@ -117,6 +123,7 @@ typedef int (memif_fn) (memif_connection_t *conn); typedef struct memif_connection { + uint16_t index; memif_conn_args_t args; int fd; @@ -153,20 +160,35 @@ typedef struct uint8_t *filename; } memif_socket_t; +/* + * WIP + */ +typedef struct +{ + int fd; + memif_connection_t *conn; + /* used only in interrupt list */ + uint8_t qid; +} memif_fd_list_elt_t; + /* * WIP */ -/* probably function like memif_cleanup () will need to be called - close timerfd, free struct libmemif_main and its nested structures */ +/* probably function like memif_cleanup () will need to be called to close timerfd */ typedef struct { memif_control_fd_update_t *control_fd_update; int timerfd; struct itimerspec arm, disarm; + uint16_t disconn_slaves; - /* TODO: update to arrays support multiple connections */ + /* master iomplementation... */ memif_socket_t ms; - memif_connection_t *conn; + + uint16_t control_list_len; + uint16_t interrupt_list_len; + memif_fd_list_elt_t *control_list; + memif_fd_list_elt_t *interrupt_list; } libmemif_main_t; extern libmemif_main_t libmemif_main; @@ -180,7 +202,7 @@ int memif_connect1 (memif_connection_t *c); /* memory map region, initalize rings and queues */ int memif_init_regions_and_queues (memif_connection_t *c); -int memif_disconnect_internal (memif_connection_t *c); +int memif_disconnect_internal (memif_connection_t *c, uint8_t is_del); /* map errno to memif error code */ int memif_syscall_error_handler (int err_code); diff --git a/src/socket.c b/src/socket.c index e3f5905..c39402d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -598,7 +598,9 @@ memif_msg_receive (memif_connection_t *c) mh.msg_control = ctl; mh.msg_controllen = sizeof (ctl); + DBG ("recvmsg fd %d", c->fd); size = recvmsg (c->fd, &mh, 0); + DBG ("done"); if (size != sizeof (memif_msg_t)) { if (size == 0) @@ -716,7 +718,7 @@ memif_conn_fd_error (memif_connection_t *c) DBG ("connection fd error"); strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19); - int err = memif_disconnect_internal (c); + int err = memif_disconnect_internal (c, 0); return err; } @@ -725,10 +727,12 @@ int memif_conn_fd_read_ready (memif_connection_t *c) { int err; + DBG ("call recv"); err = memif_msg_receive (c); + DBG ("recv finished"); if (err != 0) { - err = memif_disconnect_internal (c); + err = memif_disconnect_internal (c, 0); } return err; } From d446a954f606638ba1266d3d255f96d58ad19e6c Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Fri, 28 Jul 2017 11:31:20 +0200 Subject: [PATCH 32/63] ODPM-461 tests --- examples/icmp_responder/icmp_proto.c | 2 +- examples/icmp_responder/icmp_proto.h | 2 +- examples/icmp_responder2/main.c | 2 +- src/memif_private.h | 2 +- src/socket.c | 2 +- src/socket.h | 2 +- test/main_test.c | 96 +++++++++++++++++++++++++++- test/main_test.h | 2 +- test/socket_test.c | 4 +- test/socket_test.h | 2 +- test/unit_test.c | 2 +- test/unit_test.h | 2 +- 12 files changed, 107 insertions(+), 13 deletions(-) diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c index 99ec422..f8e69c4 100644 --- a/examples/icmp_responder/icmp_proto.c +++ b/examples/icmp_responder/icmp_proto.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/examples/icmp_responder/icmp_proto.h b/examples/icmp_responder/icmp_proto.h index 732d801..fb683a1 100644 --- a/examples/icmp_responder/icmp_proto.h +++ b/examples/icmp_responder/icmp_proto.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/examples/icmp_responder2/main.c b/examples/icmp_responder2/main.c index e09a35b..e0922fb 100644 --- a/examples/icmp_responder2/main.c +++ b/examples/icmp_responder2/main.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/src/memif_private.h b/src/memif_private.h index 1887ef9..dbffc89 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/src/socket.c b/src/socket.c index c39402d..ddc7ca5 100644 --- a/src/socket.c +++ b/src/socket.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/src/socket.h b/src/socket.h index 3c750b5..d96c5dd 100644 --- a/src/socket.h +++ b/src/socket.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/test/main_test.c b/test/main_test.c index 302fda0..eec8421 100644 --- a/test/main_test.c +++ b/test/main_test.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: @@ -157,6 +157,95 @@ START_TEST (test_create) } END_TEST +START_TEST (test_create_mult) +{ + int err; + memif_conn_handle_t conn = NULL; + memif_conn_handle_t conn1 = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + args.interface_id = 1; + + if ((err = memif_create (&conn1, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + memif_connection_t *c1 = (memif_connection_t *) conn1; + + ck_assert_ptr_ne (c, NULL); + ck_assert_ptr_ne (c1, NULL); + + ck_assert_uint_eq (c->args.interface_id, 0); + ck_assert_uint_eq (c->args.is_master, args.is_master); + ck_assert_uint_eq (c->args.mode, args.mode); + ck_assert_uint_eq (c1->args.interface_id, 1); + ck_assert_uint_eq (c1->args.is_master, args.is_master); + ck_assert_uint_eq (c1->args.mode, args.mode); + + ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES); + ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES); + ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE); + ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE); + ck_assert_uint_eq (c1->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES); + ck_assert_uint_eq (c1->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES); + ck_assert_uint_eq (c1->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE); + ck_assert_uint_eq (c1->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE); + + ck_assert_ptr_eq (c->msg_queue, NULL); + ck_assert_ptr_eq (c->regions, NULL); + ck_assert_ptr_eq (c->tx_queues, NULL); + ck_assert_ptr_eq (c->rx_queues, NULL); + ck_assert_ptr_eq (c1->msg_queue, NULL); + ck_assert_ptr_eq (c1->regions, NULL); + ck_assert_ptr_eq (c1->tx_queues, NULL); + ck_assert_ptr_eq (c1->rx_queues, NULL); + + ck_assert_int_eq (c->fd, -1); + ck_assert_int_eq (c1->fd, -1); + + ck_assert_ptr_ne (c->on_connect, NULL); + ck_assert_ptr_ne (c->on_disconnect, NULL); + ck_assert_ptr_ne (c->on_interrupt, NULL); + ck_assert_ptr_ne (c1->on_connect, NULL); + ck_assert_ptr_ne (c1->on_disconnect, NULL); + ck_assert_ptr_ne (c1->on_interrupt, NULL); + + ck_assert_str_eq (c->args.interface_name, args.interface_name); + ck_assert_str_eq (c->args.instance_name, args.instance_name); + ck_assert_str_eq (c->args.socket_filename, SOCKET_FILENAME); + ck_assert_str_eq (c1->args.interface_name, args.interface_name); + ck_assert_str_eq (c1->args.instance_name, args.instance_name); + ck_assert_str_eq (c1->args.socket_filename, SOCKET_FILENAME); + + struct itimerspec timer; + timerfd_gettime (lm->timerfd, &timer); + + ck_assert_msg (timer.it_interval.tv_sec == lm->arm.it_interval.tv_sec, + "timerfd not armed!"); + + if (lm->timerfd > 0) + close (lm->timerfd); + lm->timerfd = -1; + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + START_TEST (test_control_fd_handler) { int err; @@ -188,6 +277,8 @@ START_TEST (test_control_fd_handler) register_fd_ready_fn (c, read_fn, write_fn, error_fn); c->fd = 69; + lm->control_list[0].fd = c->fd; + lm->control_list[0].conn = c; if ((err = memif_control_fd_handler ( c->fd, MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS) @@ -740,7 +831,7 @@ START_TEST (test_disconnect_internal) if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); - if ((err = memif_disconnect_internal (c)) != MEMIF_ERR_SUCCESS) + if ((err = memif_disconnect_internal (c, 0)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); ck_assert_int_eq (c->fd, -1); @@ -782,6 +873,7 @@ main_suite () tcase_add_test (tc_api, test_init); tcase_add_test (tc_api, test_init_epoll); tcase_add_test (tc_api, test_create); + tcase_add_test (tc_api, test_create_mult); tcase_add_test (tc_api, test_control_fd_handler); tcase_add_test (tc_api, test_buffer_alloc); tcase_add_test (tc_api, test_tx_burst); diff --git a/test/main_test.h b/test/main_test.h index 4b81af5..a3bc2f0 100644 --- a/test/main_test.h +++ b/test/main_test.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/test/socket_test.c b/test/socket_test.c index 7700c9e..819a1d7 100644 --- a/test/socket_test.c +++ b/test/socket_test.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: @@ -296,6 +296,7 @@ START_TEST (test_recv_hello) h->max_m2s_ring = 1; h->max_log2_ring_size = 14; strncpy ((char *) h->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + memset (conn.remote_name, 0, sizeof (conn.remote_name)); conn.args.num_s2m_rings = 4; conn.args.num_m2s_rings = 6; @@ -420,6 +421,7 @@ START_TEST (test_recv_add_ring) ar->offset = 0; ar->flags = 0; ar->flags |= MEMIF_MSG_ADD_RING_FLAG_S2M; + ar->index = 1; if ((err = memif_msg_receive_add_ring (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); diff --git a/test/socket_test.h b/test/socket_test.h index 2c25375..686d30f 100644 --- a/test/socket_test.h +++ b/test/socket_test.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/test/unit_test.c b/test/unit_test.c index e0ee3b4..0a6a5f8 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: diff --git a/test/unit_test.h b/test/unit_test.h index 3e00682..fae3cba 100644 --- a/test/unit_test.h +++ b/test/unit_test.h @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Pantheon technologies. + * 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: From 7af94a72de263212638bed3de1ad60d50dfee668 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 19 Jul 2017 12:45:41 +0200 Subject: [PATCH 33/63] README.md + additional example app comments --- README.md | 127 +++++++++++++++++++++++++++++++++ examples/icmp_responder/main.c | 31 ++++++-- src/libmemif.h | 12 ++-- src/main.c | 2 + test/unit_test.c | 1 - 5 files changed, 159 insertions(+), 14 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6692fd5 --- /dev/null +++ b/README.md @@ -0,0 +1,127 @@ +Shared Memory Packet Interface (memif) Library +============================================== +## Introduction + +Shared memory packet interface (memif) provides high performance packet transmit and receive between user application and Vector Packet Processing (VPP). + +## Work in progress + +- [x] Slave mode + - [x] Connect to VPP over memif + - [x] ICMP responder example app +- [x] Transmit/receive packets +- [x] Interrupt mode support +- [ ] File descriptor event polling in libmemif (optional) + - [ ] Simplify file descriptor event polling (one handler for control and interrupt channel) +- [ ] Multiple connections +- [ ] Multiple regions +- [ ] Multipe queues + - [ ] Multithread support +- [ ] Master mode +- [ ] Performance testing +- [ ] Documentation + + +## Getting started + +#### Instalation + +Clone repository to your local machine. From root directory execute: + +For debug build: +``` +# ./bootstrap +# ./configure +# make +# make install +``` + +For release build: +``` +# ./bootstrap +# ./configure +# make release +# make install +``` +Verify installation: +``` +# ./.libs/icmp_responder +``` +> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies succesfull build. Default install path is /usr/lib. +Use _help_ command to display build information and commands: +``` +LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) +============================== +libmemif version: 1.0 (debug) +memif version: 256 +commands: + help - prints this help + exit - exit app + conn - create memif (slave-mode) + del - delete memif + show - show connection details +``` + +#### Connecting to VPP + +For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) + +1. Initialize memif + - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t + - Call memif initialization function. memif_init + +> If event occures on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. + +> Mmeif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. + +2. Creating interface + - Declare memif conenction handle. memif\_conn\_handle\_t + - Specify connection arguments. memif\_conn\_args\_t + - Declare callback functions called on connected/disconencted status changed. memif\_connection\_update\_t + - Call memif interface create function. memif\_create +> Arms timer file descriptor. + +3. Connection establishment + - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback.. + - On event call memif\_control\_fd\_handler. + - Everything else regarding connection establishment will be done internally. + - Once connection has been established, a callback will inform the user about connection status change. + +4. Interrupt packet receive + - Interrupt mode is still Work In Progress and will be simplified in furure patch. + - For interrupt mode, user application is required to register interrupt file descriptor for event polling. Api call memif\_get\_queue\_efd will return this interrupt file descriptor. + - If event occures on this file descriptor, there are packets in shared memory to be received. + +6. Memif buffers + - Packet data are stored in memif\_buffer\_t. Pointer _data_ points to shared memory buffer, and unsigned integer *data\_len* contains packet data length. + +5. Packet receive + - Api call memif\_rx\_burst will set all required fields in memif buffers provided by user application. + - User application can then process packets. + - Api call memif\_buffer\_free will make supplied memif buffers ready for next receive and mark shared memory buffers as free. + +6. Packet transmit + - Api call memif\_buffer\_alloc will set all required fields in memif buffers provided by user application. + - User application can populate shared memory buffers with packets. + - Api call memif\_tx\_burst will inform peer interface (master memif on VPP) that there are packets ready to receive and mark memif buffers as free. + +7. Helper functions + - Memif details + - Api call memif\_get\_details will return details about connection. + - Memif error messages + - Every api call returns error code (integer value) maped to error string. + - Call memif\_strerror will return error message assigned to specific error code. + - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. + + +#### Example app: + +- [ICMP Responder](examples/icmp_responder/main.c) + +VPP side config: +``` +# create memif id 0 master +# set int state memif0 up +# set int ip address memif0 192.168.1.1/24 +# ping 192.168.1.2 +``` diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 24ac0ef..40f6bad 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -66,6 +66,7 @@ printf ("\n"); \ } while (0) +/* maximum tx/rx memif buffers */ #define MAX_MEMIF_BUFS 256 int epfd; @@ -89,10 +90,12 @@ typedef struct int_fd_t *int_fd; /* tx buffers */ memif_buffer_t *tx_bufs; + /* allocated tx buffers counter */ /* number of tx buffers pointing to shared memory */ uint16_t tx_buf_num; /* rx buffers */ memif_buffer_t *rx_bufs; + /* allcoated rx buffers counter */ /* number of rx buffers pointing to shared memory */ uint16_t rx_buf_num; } memif_connection_t; @@ -235,9 +238,12 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) memif_connection_t *c = &memif_connection; INFO ("memif connected!"); int err; - err = memif_get_queue_efd (c->conn, 0, &c->int_fd->fd); + uint16_t qid = 0; + /* get interrupt file descriptor for queue specified by qid */ + err = memif_get_queue_efd (c->conn, qid, &c->int_fd->fd); INFO ("memif_get_queue_efd: %s", memif_strerror (err)); c->int_fd->qid = 0; + /* add interrupt fd to epoll */ return add_epoll_fd (c->int_fd->fd, EPOLLIN); } @@ -250,10 +256,12 @@ on_disconnect (memif_conn_handle_t conn, void *private_ctx) return 0; } -/* user needs to watch new fd or stop watching fd that is about to be closed */ +/* 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 (fd); @@ -286,6 +294,7 @@ icmpr_memif_create (int is_master) 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 = 0; /* last argument for memif_create (void * private_ctx) is used by user @@ -334,6 +343,7 @@ icmpr_buffer_alloc (long n) memif_connection_t *c = &memif_connection; int err; uint16_t r, qid = 0; + /* 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); INFO ("memif_buffer_alloc: %s", memif_strerror (err)); c->tx_buf_num += r; @@ -347,6 +357,8 @@ icmpr_tx_burst () memif_connection_t *c = &memif_connection; int err; uint16_t r, qid = 0; + /* inform peer memif interface about data in shared memory buffers */ + /* mark memif buffers as free */ err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); INFO ("memif_tx_burst: %s", memif_strerror (err)); DBG ("tx: %d/%u", r, c->tx_buf_num); @@ -357,6 +369,7 @@ icmpr_tx_burst () int icmpr_free () { + /* application cleanup */ memif_connection_t *c = &memif_connection; free (c->int_fd); c->int_fd = NULL; @@ -419,13 +432,9 @@ int icmpr_interrupt (int fd) { memif_connection_t *c = &memif_connection; -/* DBG ("interrupted!"); - uint64_t b; - ssize_t r = read (fd, &b, sizeof (b)); -*/ - int err; uint16_t rx; + /* receive data from shared memory buffers */ err = memif_rx_burst (c->conn, 0, c->rx_bufs, MAX_MEMIF_BUFS, &rx); INFO ("memif_rx_burst: %s", memif_strerror (err)); c->rx_buf_num += rx; @@ -443,6 +452,7 @@ icmpr_interrupt (int fd) } uint16_t fb; + /* mark memif buffers and shared memory buffers as free */ err = memif_buffer_free (c->conn, 0, c->rx_bufs, rx, &fb); INFO ("memif_buffer_free: %s", memif_strerror (err)); c->rx_buf_num -= fb; @@ -478,12 +488,17 @@ poll_event (int timeout) /* this app does not use any other file descriptors than stds and memif control fds */ if ( evt.data.fd > 2) { + /* event on memif interrupt fd */ if (evt.data.fd == c->int_fd->fd) { + /* WIP */ + /* in future patch there will be one handler and callback on interrupt */ icmpr_interrupt (evt.data.fd); } else { + /* event of memif control fd */ + /* convert epolle events to memif events */ if (evt.events & EPOLLIN) events |= MEMIF_FD_EVENT_READ; if (evt.events & EPOLLOUT) @@ -538,6 +553,8 @@ int main () err = memif_init (control_fd_update); INFO ("memif_init: %s", memif_strerror (err)); + print_help (); + /* main loop */ while (1) { diff --git a/src/libmemif.h b/src/libmemif.h index 8da178f..a788a0c 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -118,13 +118,13 @@ typedef int (memif_connection_update_t) (memif_conn_handle_t conn, void *private */ typedef struct { - uint8_t *socket_filename; - uint8_t secret[24]; /* optional */ + uint8_t *socket_filename; /* default = /run/vpp/memif.sock */ + uint8_t secret[24]; /* optional (interface authentication) */ - uint8_t num_s2m_rings; - uint8_t num_m2s_rings; - uint16_t buffer_size; - memif_log2_ring_size_t log2_ring_size; + 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; diff --git a/src/main.c b/src/main.c index f00650a..8daebcb 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,6 @@ /* + *------------------------------------------------------------------ + * 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: diff --git a/test/unit_test.c b/test/unit_test.c index f53cb2f..664c3c9 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -14,7 +14,6 @@ * limitations under the License. *------------------------------------------------------------------ */ - #include From f8c7ba3e02cef9d7db663501ddb180b45182b9b3 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 2 Aug 2017 12:53:53 +0200 Subject: [PATCH 34/63] set rx mode api + multiqueue fix --- examples/icmp_responder/main.c | 61 +++++++++++++++++++++++++++++----- src/libmemif.h | 12 +++++++ src/main.c | 17 +++++++++- src/socket.c | 10 ++++-- 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 74d0c86..bb609c7 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -338,7 +338,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) DBG ("received %d buffers. %u/%u alloc/free buffers", rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - if (icmpr_buffer_alloc (index, rx, c->tx_qid) < 0) + if (icmpr_buffer_alloc (index, rx, qid) < 0) { INFO ("buffer_alloc error"); goto error; @@ -360,7 +360,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_tx_burst (index, c->tx_qid); + icmpr_tx_burst (index, qid); return 0; @@ -478,7 +478,6 @@ print_help () printf ("\tconn - create memif (slave-mode)\n"); printf ("\tdel - delete memif\n"); printf ("\tshow - show connection details\n"); - printf ("\ttx-qid - set transmit queue id (TODO)\n"); printf ("\tip-set - set interface ip address\n"); } int @@ -557,6 +556,41 @@ icmpr_set_ip (long index, char* ip) return 0; } +int +icmpr_set_rx_mode (long index, long qid, char* mode) +{ + 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; + } + + if (strncmp (mode, "interrupt", 9) == 0) + { + memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid); + } + + else if (strncmp (mode, "polling", 7) == 0) + { + memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid); + } + else + INFO ("expected rx mode "); + return 0; +} + int user_input_handler () { @@ -601,21 +635,32 @@ user_input_handler () print_memif_details (); goto done; } - else if (strncmp (ui, "tx-qid", 6) == 0) + else if (strncmp (ui, "ip-set", 6) == 0) { ui = strtok (NULL, " "); if (ui != NULL) - /* TODO: set transmit qid */ + icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " ")); + else + INFO ("expected id"); goto done; } - else if (strncmp (ui, "ip-set", 6) == 0) + else if (strncmp (ui, "rx-mode", 7) == 0) { ui = strtok (NULL, " "); + long a; if (ui != NULL) - icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " ")); + a = strtol (ui, &end, 10); else + { INFO ("expected id"); - goto done; + goto done; + } + ui = strtok (NULL, " "); + if (ui != NULL) + icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " ")); + else + INFO ("expected qid"); + goto done; } else { diff --git a/src/libmemif.h b/src/libmemif.h index ae90347..1d831c6 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -192,6 +192,18 @@ typedef struct uint8_t link_up_down; /* 1 = up, 0 = down */ } memif_details_t; +/* memif receive mode */ +typedef enum +{ + MEMIF_RX_MODE_INTERRUPT = 0, + MEMIF_RX_MODE_POLLING +} memif_rx_mode_t; + +/** \brief Memif set rx mode + @param rx_mode - receive mode. 0 = interrupt, 1 = polling +*/ +int memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, uint16_t qid); + /** \brief Memif strerror @param - err_code - error code diff --git a/src/main.c b/src/main.c index c78932a..e0f4af3 100644 --- a/src/main.c +++ b/src/main.c @@ -451,6 +451,18 @@ memif_get_ring (memif_connection_t *conn, memif_ring_type_t type, uint16_t ring_ return (memif_ring_t *) p; } +int +memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, uint16_t qid) +{ + memif_connection_t *conn = (memif_connection_t *) c; + if (conn == NULL) + return MEMIF_ERR_NOCONN; + + conn->rx_queues[qid].ring->flags = rx_mode; + DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags); + return MEMIF_ERR_SUCCESS; +} + int memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, memif_connection_update_t *on_connect, @@ -757,6 +769,8 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) libmemif_main_t *lm = &libmemif_main; memif_fd_list_elt_t *e; + c->on_disconnect ((void *) c, c->private_ctx); + if (c->fd > 0) { memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); @@ -841,7 +855,6 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) } lm->disconn_slaves++; - c->on_disconnect ((void *) c, c->private_ctx); return err; } @@ -979,6 +992,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) DBG ("RING: %p I: %d", ring, i); ring->head = ring->tail = 0; ring->cookie = MEMIF_COOKIE; + ring->flags = 0; for (j = 0; j < (1 << conn->args.log2_ring_size); j++) { uint16_t slot = i * (1 << conn->args.log2_ring_size) + j; @@ -994,6 +1008,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) DBG ("RING: %p I: %d", ring, i); ring->head = ring->tail = 0; ring->cookie = MEMIF_COOKIE; + ring->flags = 0; for (j = 0; j < (1 << conn->args.log2_ring_size); j++) { uint16_t slot = (i + conn->args.num_s2m_rings) * (1 << conn->args.log2_ring_size) + j; diff --git a/src/socket.c b/src/socket.c index ddc7ca5..ab3138b 100644 --- a/src/socket.c +++ b/src/socket.c @@ -537,7 +537,10 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); - lm->control_fd_update (c->rx_queues->int_fd, MEMIF_FD_EVENT_READ); + int i; + for (i = 0; i < c->args.num_m2s_rings; i++) + lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); + c->on_connect ((void *) c, c->private_ctx); return err; @@ -557,7 +560,10 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); - lm->control_fd_update (c->rx_queues->int_fd, MEMIF_FD_EVENT_READ); + int i; + for (i = 0; i < c->args.num_s2m_rings; i++) + lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); + c->on_connect ((void *) c, c->private_ctx); return err; From a25dc90b0393d7bcb87c5f0179f721fe9f617e3d Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 2 Aug 2017 12:57:34 +0200 Subject: [PATCH 35/63] ODPM-267 WIP --- examples/icmp_responder3/main.c | 773 ++++++++++++++++++++++++++++++++ 1 file changed, 773 insertions(+) create mode 100644 examples/icmp_responder3/main.c diff --git a/examples/icmp_responder3/main.c b/examples/icmp_responder3/main.c new file mode 100644 index 0000000..5576190 --- /dev/null +++ b/examples/icmp_responder3/main.c @@ -0,0 +1,773 @@ +/* + *------------------------------------------------------------------ + * 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 2 +#define MAX_THREADS 4 + +int epfd; + +typedef struct +{ + uint8_t id; + uint16_t index; + uint8_t rx_qid; +} memif_thread_data_t; + +typedef struct +{ + uint16_t index; + /* memif conenction handle */ + memif_conn_handle_t conn; + /* transmit queue id */ + uint16_t tx_qid; + /* tx buffers */ + memif_buffer_t *tx_bufs; + /* allocated tx buffers counter */ + /* number of tx buffers pointing to shared memory */ + uint16_t tx_buf_num; + /* rx buffers */ + memif_buffer_t *rx_bufs; + /* allcoated rx buffers counter */ + /* number of rx buffers pointing to shared memory */ + uint16_t rx_buf_num; + /* interface ip address */ + uint8_t ip_addr[4]; + /* inform pthread about connection termination */ + uint8_t pending_del; +} memif_connection_t; + +memif_thread_data_t thread_data[MAX_THREADS]; +memif_connection_t memif_connection[MAX_CONNS]; +pthread_t thread[MAX_THREADS]; +pthread_mutex_t memif_mutex; + +static void +print_memif_details () +{ + memif_details_t md; + ssize_t buflen; + char *buf; + int err, i; + printf ("MEMIF DETAILS\n"); + printf ("==============================\n"); + for (i = 0; i < MAX_CONNS; i++) + { + memif_connection_t *c = &memif_connection[i]; + printf ("interface index: %d\n", i); + + memset (&md, 0, sizeof (md)); + buflen = 2048; + buf = malloc (buflen); + memset (buf, 0, buflen); + + err = memif_get_details (c->conn, &md, buf, buflen); + if (err != MEMIF_ERR_SUCCESS) + { + if (err == MEMIF_ERR_NOCONN) + { + printf ("\tno connection\n"); + free (buf); + continue; + } + else + { + INFO ("%s", memif_strerror (err)); + free (buf); + continue; + } + } + + 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 ("\tring_size: %u\n", md.ring_size); + printf ("\tbuffer_size: %u\n", md.buffer_size); + printf ("\trx queues: %u\n", md.rx_queues); + printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\tlink: "); + if (md.link_up_down) + printf ("up\n"); + else + printf ("down\n"); + + free (buf); + } +} + +int +add_epoll_fd (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 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 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; +} + +int +icmpr_buffer_alloc (long index, long n, uint16_t qid) +{ + memif_connection_t *c = &memif_connection[index]; + 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); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + c->tx_buf_num += r; + return -1; + } + c->tx_buf_num += r; + DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); + return 0; +} + +int +icmpr_tx_burst (long index, uint16_t qid) +{ + memif_connection_t *c = &memif_connection[index]; + int err; + uint16_t r; + /* inform peer memif interface about data in shared memory buffers */ + /* mark memif buffers as free */ + err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_tx_burst: %s", memif_strerror (err)); + DBG ("tx: %d/%u", r, c->tx_buf_num); + c->tx_buf_num -= r; + 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; + uint16_t fb; + + DBG ("pthread id %u start", data->id); + + while (1) + { + if (c->pending_del) + { + DBG ("pthread id %u exit", data->id); + pthread_exit (NULL); + } + + pthread_mutex_lock (&memif_mutex); + + /* receive data from shared memory buffers */ + err = memif_rx_burst (c->conn, data->rx_qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_rx_burst: %s", memif_strerror (err)); + c->rx_buf_num += rx; + goto error; + } + c->rx_buf_num += rx; + if (rx == 0) + { + pthread_mutex_unlock (&memif_mutex); + continue; + } + + INFO ("thread id: %u", data->id); + + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + if (icmpr_buffer_alloc (c->index, rx, c->tx_qid) < 0) + { + INFO ("buffer_alloc error"); + goto error; + } + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (c->rx_bufs + i)->data, + (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, + &(c->tx_bufs + i)->data_len, c->ip_addr); + } + + /* mark memif buffers and shared memory buffers as free */ + err = memif_buffer_free (c->conn, data->rx_qid, c->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + icmpr_tx_burst (c->index, c->tx_qid); + pthread_mutex_unlock (&memif_mutex); + } + +error: + INFO ("thread %u error!", data->id); + err = memif_buffer_free (c->conn, data->rx_qid, c->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + DBG ("pthread id %u exit", data->id); + pthread_mutex_unlock (&memif_mutex); + 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); + INFO ("memif connected! index %ld", index); + memif_connection_t *c = &memif_connection[index]; + memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 0); + memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 1); + c->pending_del = 0; + thread_data[index].index = index; + thread_data[index].rx_qid = 0; + thread_data[index].id = 0; + pthread_create (&thread[0], NULL, memif_rx_poll, (void *) &thread_data[index]); + + thread_data[index + 1].index = index; + thread_data[index + 1].rx_qid = 1; + thread_data[index + 1].id = 1; + pthread_create (&thread[1], NULL, memif_rx_poll, (void *) &thread_data[index + 1]); + 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) +{ + INFO ("memif disconnected!"); + memif_connection_t *c = &memif_connection[(*(long *) private_ctx)]; + c->pending_del = 1; + void *ptr; + pthread_join (thread[0], &ptr); + pthread_join (thread[1], &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 (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 (fd, evt); + + return add_epoll_fd (fd, evt); +} + +/* called when event is polled on interrupt file descriptor. + there are packets in shared memory ready to be received */ +int +on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) +{ + return 0; +} + +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 */ + long *ctx = malloc (sizeof (long)); + *ctx = index; + int err = memif_create (&c->conn, + &args, on_connect, on_disconnect, on_interrupt, ctx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_create: %s", memif_strerror (err)); + return 0; + } + + c->index = index; + /* tx queue id */ + c->tx_qid = 0; + /* alloc memif buffers */ + c->rx_buf_num = 0; + c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->tx_buf_num = 0; + c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + + 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 ("\ttx-qid - set transmit queue id (TODO)\n"); + printf ("\tip-set - set interface ip address\n"); +} + +int +icmpr_free () +{ + /* application cleanup */ + long i; + for (i = 0; i < MAX_CONNS; i++) + { + memif_connection_t *c = &memif_connection[i]; + icmpr_memif_delete (i); + free (c->tx_bufs); + c->tx_bufs = NULL; + free (c->rx_bufs); + c->rx_bufs = NULL; + } + + pthread_exit (NULL); + + 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, "tx-qid", 6) == 0) + { + ui = strtok (NULL, " "); + if (ui != NULL) + /* TODO: set transmit qid */ + 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 (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 () +{ + epfd = epoll_create (1); + add_epoll_fd (0, EPOLLIN); + + /* initialize memory interface */ + int err; + /* 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); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_init: %s", memif_strerror (err)); + + print_help (); + + /* main loop */ + while (1) + { + if (poll_event (-1) < 0) + { + DBG ("poll_event error!"); + } + } +} From 3439ac5a3b99beed4bd0f62acb48bfc3ba2868a0 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 3 Aug 2017 10:20:52 +0200 Subject: [PATCH 36/63] ODPM-267 --- README.md | 7 +++++ examples/icmp_responder3/main.c | 47 +++++++++++++++++---------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 02a6f60..229f77b 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ For detailed information on api calls and structures please refer to [libmemif.h - [ICMP Responder](examples/icmp_responder/main.c) #### Example app (libmemif fd event polling): + - [ICMP Responder](examples/icmp_responder2/main.c) > Optional argument: transmit queue id. ``` @@ -136,6 +137,12 @@ icmpr_lep 1 > Set transmit queue id to 1. Default is 0. > Application will create memif interface in slave mode and try to connect to VPP. Exit using Ctrl+C. Application will handle SIGINT signal, free allocated memory and exit with EXIT_SUCCESS. +#### Example app (multi-thread queue polling) + +- [ICMP Responder](examples/icmp_responder3/main.c) + +> Connection establishment is handled by main thread, while polling for available packets is handled by threads. Each thread is handling single queue. + VPP config: ``` # create memif id 0 master diff --git a/examples/icmp_responder3/main.c b/examples/icmp_responder3/main.c index 5576190..3f7ce0c 100644 --- a/examples/icmp_responder3/main.c +++ b/examples/icmp_responder3/main.c @@ -76,8 +76,11 @@ int epfd; typedef struct { + /* thread id */ uint8_t id; + /* memif connection index */ uint16_t index; + /* id of queue to be handled by thread */ uint8_t rx_qid; } memif_thread_data_t; @@ -86,8 +89,6 @@ typedef struct uint16_t index; /* memif conenction handle */ memif_conn_handle_t conn; - /* transmit queue id */ - uint16_t tx_qid; /* tx buffers */ memif_buffer_t *tx_bufs; /* allocated tx buffers counter */ @@ -104,10 +105,13 @@ typedef struct uint8_t pending_del; } memif_connection_t; -memif_thread_data_t thread_data[MAX_THREADS]; memif_connection_t memif_connection[MAX_CONNS]; + +/* thread data specific for each thread */ +memif_thread_data_t thread_data[MAX_THREADS]; pthread_t thread[MAX_THREADS]; -pthread_mutex_t memif_mutex; +/* mutex specific for each connection */ +pthread_mutex_t memif_mutex[MAX_CONNS]; static void print_memif_details () @@ -304,7 +308,9 @@ memif_rx_poll (void *ptr) pthread_exit (NULL); } - pthread_mutex_lock (&memif_mutex); + /* threads are operating different rings in shared memory, so there is no need to regulate access to it */ + /* in this example, there are memif buffers per connection, so access needs to be regulated, as all queues are using same buffers */ + pthread_mutex_lock (&memif_mutex[data->index]); /* receive data from shared memory buffers */ err = memif_rx_burst (c->conn, data->rx_qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); @@ -317,16 +323,16 @@ memif_rx_poll (void *ptr) c->rx_buf_num += rx; if (rx == 0) { - pthread_mutex_unlock (&memif_mutex); + pthread_mutex_unlock (&memif_mutex[data->index]); continue; } - INFO ("thread id: %u", data->id); + DBG ("thread id: %u", data->id); DBG ("received %d buffers. %u/%u alloc/free buffers", rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - if (icmpr_buffer_alloc (c->index, rx, c->tx_qid) < 0) + if (icmpr_buffer_alloc (c->index, rx, data->rx_qid) < 0) { INFO ("buffer_alloc error"); goto error; @@ -348,8 +354,8 @@ memif_rx_poll (void *ptr) DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_tx_burst (c->index, c->tx_qid); - pthread_mutex_unlock (&memif_mutex); + icmpr_tx_burst (c->index, data->rx_qid); + pthread_mutex_unlock (&memif_mutex[data->index]); } error: @@ -361,7 +367,7 @@ memif_rx_poll (void *ptr) DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); DBG ("pthread id %u exit", data->id); - pthread_mutex_unlock (&memif_mutex); + pthread_mutex_unlock (&memif_mutex[data->index]); pthread_exit (NULL); } @@ -371,10 +377,15 @@ int on_connect (memif_conn_handle_t conn, void *private_ctx) { long index = (*(long *) private_ctx); + int err; INFO ("memif connected! index %ld", index); memif_connection_t *c = &memif_connection[index]; - memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 0); - memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 1); + err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 0); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), 0); + err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 1); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), 1); c->pending_del = 0; thread_data[index].index = index; thread_data[index].rx_qid = 0; @@ -476,8 +487,6 @@ icmpr_memif_create (long index) } c->index = index; - /* tx queue id */ - c->tx_qid = 0; /* alloc memif buffers */ c->rx_buf_num = 0; c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); @@ -535,7 +544,6 @@ print_help () printf ("\tconn - create memif (slave-mode)\n"); printf ("\tdel - delete memif\n"); printf ("\tshow - show connection details\n"); - printf ("\ttx-qid - set transmit queue id (TODO)\n"); printf ("\tip-set - set interface ip address\n"); } @@ -662,13 +670,6 @@ user_input_handler () print_memif_details (); goto done; } - else if (strncmp (ui, "tx-qid", 6) == 0) - { - ui = strtok (NULL, " "); - if (ui != NULL) - /* TODO: set transmit qid */ - goto done; - } else if (strncmp (ui, "ip-set", 6) == 0) { ui = strtok (NULL, " "); From f4ee4f95af4393f678cd4a33813f25c0b1c41bc4 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Fri, 4 Aug 2017 13:53:07 +0200 Subject: [PATCH 37/63] ODMP-267 patch --- README.md | 26 +-- examples/icmp_responder3/main.c | 317 ++++++++++++++++++++------------ src/libmemif.h | 28 ++- src/main.c | 44 ++++- src/memif_private.h | 4 +- src/socket.c | 14 +- 6 files changed, 292 insertions(+), 141 deletions(-) diff --git a/README.md b/README.md index 229f77b..77a0b18 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ Shared memory packet interface (memif) provides high performance packet transmit - [x] File descriptor event polling in libmemif (optional) - [x] Simplify file descriptor event polling (one handler for control and interrupt channel) - [x] Multiple connections -- [x] Multipe queues - - [ ] Multithread support +- [x] Multiple queues + - [ ] Multi-thread support - [ ] Master mode - [ ] Multiple regions - [ ] Performance testing @@ -24,7 +24,7 @@ Shared memory packet interface (memif) provides high performance packet transmit ## Getting started -#### Instalation +#### Installation Clone repository to your local machine. From root directory execute: @@ -47,7 +47,7 @@ Verify installation: ``` # ./.libs/icmp_responder ``` -> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies succesfull build. Default install path is /usr/lib. +> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies successful build. Default install path is /usr/lib. Use _help_ command to display build information and commands: ``` LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) @@ -64,7 +64,7 @@ commands: #### Unit tests -Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be instaled in order to build *unit\_test* binary. +Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be installed in order to build *unit\_test* binary. Ubuntu/Debian: ``` sudo apt-get install check @@ -79,16 +79,16 @@ For detailed information on api calls and structures please refer to [libmemif.h - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t - Call memif initialization function. memif\_init -> If event occures on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. +> If event occurres on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. > If callback function parameter for memif\_init function is set to NULL, libmemif will handle file descriptor event polling. - Api call memif\_poll\_event will call epoll\_pwait wit user defined timeout to poll event on file descriptors opend by libmemif. + Api call memif\_poll\_event will call epoll\_pwait wit user defined timeout to poll event on file descriptors opened by libmemif. -> Mmeif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. +> Memif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. 2. Creating interface - - Declare memif conenction handle. memif\_conn\_handle\_t + - Declare memif connction handle. memif\_conn\_handle\_t - Specify connection arguments. memif\_conn\_args\_t - - Declare callback functions called on connected/disconencted status changed. memif\_connection\_update\_t + - Declare callback functions called on connected/disconnected status changed. memif\_connection\_update\_t - Call memif interface create function. memif\_create > Arms timer file descriptor. @@ -118,7 +118,7 @@ For detailed information on api calls and structures please refer to [libmemif.h - Memif details - Api call memif\_get\_details will return details about connection. - Memif error messages - - Every api call returns error code (integer value) maped to error string. + - Every api call returns error code (integer value) mapped to error string. - Call memif\_strerror will return error message assigned to specific error code. - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. @@ -141,7 +141,7 @@ icmpr_lep 1 - [ICMP Responder](examples/icmp_responder3/main.c) -> Connection establishment is handled by main thread, while polling for available packets is handled by threads. Each thread is handling single queue. +> Simple example of libmemif multi-thread usage. Connection establishment is handled by main thread. There are two rx queues in this example. One in polling mode and second in interrupt mode. VPP config: ``` @@ -150,7 +150,7 @@ VPP config: # set int ip address memif0 192.168.1.1/24 # ping 192.168.1.2 ``` -For multipe rings (queues) support run VPP with worker threads: +For multiple rings (queues) support run VPP with worker threads: example startup.conf: ``` unix { diff --git a/examples/icmp_responder3/main.c b/examples/icmp_responder3/main.c index 3f7ce0c..17c0bf9 100644 --- a/examples/icmp_responder3/main.c +++ b/examples/icmp_responder3/main.c @@ -69,10 +69,10 @@ /* maximum tx/rx memif buffers */ #define MAX_MEMIF_BUFS 256 -#define MAX_CONNS 2 +#define MAX_CONNS 1 #define MAX_THREADS 4 -int epfd; +int main_epfd; typedef struct { @@ -81,7 +81,12 @@ typedef struct /* memif connection index */ uint16_t index; /* id of queue to be handled by thread */ - uint8_t rx_qid; + uint8_t qid; + + 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 @@ -89,16 +94,6 @@ typedef struct uint16_t index; /* memif conenction handle */ memif_conn_handle_t conn; - /* tx buffers */ - memif_buffer_t *tx_bufs; - /* allocated tx buffers counter */ - /* number of tx buffers pointing to shared memory */ - uint16_t tx_buf_num; - /* rx buffers */ - memif_buffer_t *rx_bufs; - /* allcoated rx buffers counter */ - /* number of rx buffers pointing to shared memory */ - uint16_t rx_buf_num; /* interface ip address */ uint8_t ip_addr[4]; /* inform pthread about connection termination */ @@ -110,8 +105,12 @@ memif_connection_t memif_connection[MAX_CONNS]; /* thread data specific for each thread */ memif_thread_data_t thread_data[MAX_THREADS]; pthread_t thread[MAX_THREADS]; -/* mutex specific for each connection */ -pthread_mutex_t memif_mutex[MAX_CONNS]; + +void +user_signal_handler (int sig) +{ + sig = sig; +} static void print_memif_details () @@ -194,7 +193,7 @@ print_memif_details () } int -add_epoll_fd (int fd, uint32_t events) +add_epoll_fd (int epfd, int fd, uint32_t events) { if (fd < 0) { @@ -215,7 +214,7 @@ add_epoll_fd (int fd, uint32_t events) } int -mod_epoll_fd (int fd, uint32_t events) +mod_epoll_fd (int epfd, int fd, uint32_t events) { if (fd < 0) { @@ -236,7 +235,7 @@ mod_epoll_fd (int fd, uint32_t events) } int -del_epoll_fd (int fd) +del_epoll_fd (int epfd, int fd) { if (fd < 0) { @@ -254,123 +253,225 @@ del_epoll_fd (int fd) return 0; } -int -icmpr_buffer_alloc (long index, long n, uint16_t qid) -{ - memif_connection_t *c = &memif_connection[index]; - 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); - if (err != MEMIF_ERR_SUCCESS) - { - INFO ("memif_buffer_alloc: %s", memif_strerror (err)); - c->tx_buf_num += r; - return -1; - } - c->tx_buf_num += r; - DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); - return 0; -} - -int -icmpr_tx_burst (long index, uint16_t qid) -{ - memif_connection_t *c = &memif_connection[index]; - int err; - uint16_t r; - /* inform peer memif interface about data in shared memory buffers */ - /* mark memif buffers as free */ - err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_tx_burst: %s", memif_strerror (err)); - DBG ("tx: %d/%u", r, c->tx_buf_num); - c->tx_buf_num -= r; - 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; - uint16_t fb; + uint16_t rx, tx, fb; - DBG ("pthread id %u start", data->id); + 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; + + INFO ("pthread id %u starts in polling mode", data->id); while (1) { if (c->pending_del) - { - DBG ("pthread id %u exit", data->id); - pthread_exit (NULL); - } - - /* threads are operating different rings in shared memory, so there is no need to regulate access to it */ - /* in this example, there are memif buffers per connection, so access needs to be regulated, as all queues are using same buffers */ - pthread_mutex_lock (&memif_mutex[data->index]); + goto close; /* receive data from shared memory buffers */ - err = memif_rx_burst (c->conn, data->rx_qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); + 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)); - c->rx_buf_num += rx; + data->rx_buf_num += rx; goto error; } - c->rx_buf_num += rx; + data->rx_buf_num += rx; if (rx == 0) { - pthread_mutex_unlock (&memif_mutex[data->index]); continue; } DBG ("thread id: %u", data->id); DBG ("received %d buffers. %u/%u alloc/free buffers", - rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); - if (icmpr_buffer_alloc (c->index, rx, data->rx_qid) < 0) + err = memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, data->rx_buf_num, &tx); + if (err != MEMIF_ERR_SUCCESS) { - INFO ("buffer_alloc error"); + 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 *) (c->rx_bufs + i)->data, - (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, - &(c->tx_bufs + i)->data_len, c->ip_addr); + 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->rx_qid, c->rx_bufs, rx, &fb); + 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)); - c->rx_buf_num -= fb; + data->rx_buf_num -= fb; DBG ("freed %d buffers. %u/%u alloc/free buffers", - fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); - icmpr_tx_burst (c->index, data->rx_qid); - pthread_mutex_unlock (&memif_mutex[data->index]); + 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); - err = memif_buffer_free (c->conn, data->rx_qid, c->rx_bufs, rx, &fb); + 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)); - c->rx_buf_num -= fb; + data->rx_buf_num -= fb; DBG ("freed %d buffers. %u/%u alloc/free buffers", - fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - DBG ("pthread id %u exit", data->id); - pthread_mutex_unlock (&memif_mutex[data->index]); + fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); + free (data->rx_bufs); + free (data->tx_bufs); + 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, tx, fb; + 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; + + 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); + 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 @@ -383,19 +484,20 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 0); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), 0); - err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 1); + err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, 1); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), 1); c->pending_del = 0; thread_data[index].index = index; - thread_data[index].rx_qid = 0; + thread_data[index].qid = 0; thread_data[index].id = 0; pthread_create (&thread[0], NULL, memif_rx_poll, (void *) &thread_data[index]); thread_data[index + 1].index = index; - thread_data[index + 1].rx_qid = 1; + thread_data[index + 1].qid = 1; thread_data[index + 1].id = 1; - pthread_create (&thread[1], NULL, memif_rx_poll, (void *) &thread_data[index + 1]); + pthread_create (&thread[1], NULL, memif_rx_interrupt, (void *) &thread_data[index + 1]); + return 0; } @@ -404,11 +506,15 @@ on_connect (memif_conn_handle_t conn, void *private_ctx) int on_disconnect (memif_conn_handle_t conn, void *private_ctx) { - INFO ("memif disconnected!"); + void *ptr; memif_connection_t *c = &memif_connection[(*(long *) private_ctx)]; + + INFO ("memif disconnected!"); + /* inform thread in polling mode about memif disconenction */ c->pending_del = 1; - void *ptr; pthread_join (thread[0], &ptr); + /* send custom sugnal to interrupt thread */ + pthread_kill (thread[1], SIGUSR1); pthread_join (thread[1], &ptr); return 0; } @@ -420,7 +526,7 @@ 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 (fd); + return del_epoll_fd (main_epfd, fd); uint32_t evt = 0; if (events & MEMIF_FD_EVENT_READ) @@ -429,17 +535,9 @@ control_fd_update (int fd, uint8_t events) evt |= EPOLLOUT; if (events & MEMIF_FD_EVENT_MOD) - return mod_epoll_fd (fd, evt); - - return add_epoll_fd (fd, evt); -} + return mod_epoll_fd (main_epfd, fd, evt); -/* called when event is polled on interrupt file descriptor. - there are packets in shared memory ready to be received */ -int -on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) -{ - return 0; + return add_epoll_fd (main_epfd, fd, evt); } int @@ -479,7 +577,7 @@ icmpr_memif_create (long index) long *ctx = malloc (sizeof (long)); *ctx = index; int err = memif_create (&c->conn, - &args, on_connect, on_disconnect, on_interrupt, ctx); + &args, on_connect, on_disconnect, NULL, ctx); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_create: %s", memif_strerror (err)); @@ -487,11 +585,6 @@ icmpr_memif_create (long index) } c->index = index; - /* alloc memif buffers */ - c->rx_buf_num = 0; - c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - c->tx_buf_num = 0; - c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); c->ip_addr[0] = 192; c->ip_addr[1] = 168; @@ -556,10 +649,6 @@ icmpr_free () { memif_connection_t *c = &memif_connection[i]; icmpr_memif_delete (i); - free (c->tx_bufs); - c->tx_bufs = NULL; - free (c->rx_bufs); - c->rx_bufs = NULL; } pthread_exit (NULL); @@ -702,7 +791,7 @@ poll_event (int timeout) evt.events = EPOLLIN | EPOLLOUT; sigset_t sigset; sigemptyset (&sigset); - en = epoll_pwait (epfd, &evt, 1, timeout, &sigset); + en = epoll_pwait (main_epfd, &evt, 1, timeout, &sigset); if (en < 0) { DBG ("epoll_pwait: %s", strerror (errno)); @@ -749,8 +838,8 @@ poll_event (int timeout) int main () { - epfd = epoll_create (1); - add_epoll_fd (0, EPOLLIN); + main_epfd = epoll_create (1); + add_epoll_fd (main_epfd, 0, EPOLLIN); /* initialize memory interface */ int err; diff --git a/src/libmemif.h b/src/libmemif.h index 1d831c6..20ca6ba 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -199,10 +199,30 @@ typedef enum MEMIF_RX_MODE_POLLING } memif_rx_mode_t; +/** \biref Memif get queue event file descriptor + @param conn - memif connection handle + @param qid - queue id + @param fd - returns event file descriptor + + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - connection handle points to NULL + MEMIF_ERR_QID - invalid queue id +*/ + +int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *fd); + /** \brief Memif set rx mode - @param rx_mode - receive mode. 0 = interrupt, 1 = polling + @param conn - memif connection handle + @param rx_mode - receive mode + @param qid - queue id + + return + MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_NOCONN - connection handle points to NULL + MEMIF_ERR_QID - invalid queue id */ -int memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, uint16_t qid); +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 @@ -360,6 +380,7 @@ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, MEMIF_ERR_SUCCESS - no error MEMIF_ERR_NOCONN - handle points to NULL MEMIF_ERR_DISCONNECTED - not conencted + MEMIF_ERR_QID - invalid queue id */ int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *count_out); @@ -375,6 +396,7 @@ int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, MEMIF_ERR_SUCCESS - no error MEMIF_ERR_NOCONN - handle points to NULL MEMIF_ERR_DISCONNECTED - not conencted + MEMIF_ERR_QID - invalid queue id */ int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *tx); @@ -391,6 +413,7 @@ int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, MEMIF_ERR_NOCONN - handle points to NULL MEMIF_ERR_DISCONNECTED - not conencted MEMIF_ERR_NOBUF - not enough buffers provided + MEMIF_ERR_QID - invalid queue id */ int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *rx); @@ -404,6 +427,7 @@ int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_SUCCESS - no error + MEMIF_ERR_QID - invalid queue id */ int memif_poll_event (int timeout); diff --git a/src/main.c b/src/main.c index e0f4af3..e47d246 100644 --- a/src/main.c +++ b/src/main.c @@ -457,6 +457,9 @@ memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, uint16_t qid) memif_connection_t *conn = (memif_connection_t *) c; if (conn == NULL) return MEMIF_ERR_NOCONN; + uint8_t num = (conn->args.is_master) ? conn->args.num_s2m_rings : conn->args.num_m2s_rings; + if (qid >= num) + return MEMIF_ERR_QID; conn->rx_queues[qid].ring->flags = rx_mode; DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags); @@ -559,6 +562,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, strncpy ((char *) conn->args.secret, (char *) args->secret, l); } +/* SLAVE */ if (lm->disconn_slaves == 0) { if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) @@ -579,6 +583,10 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, goto error; } + + + + conn->index = index; return 0; @@ -638,7 +646,7 @@ memif_control_fd_handler (int fd, uint8_t events) lm->control_list[conn->index].fd = conn->fd; - lm->control_fd_update ( + lm->control_fd_update ( sockfd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE); lm->disconn_slaves--; @@ -664,7 +672,8 @@ memif_control_fd_handler (int fd, uint8_t events) get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); if (e != NULL) { - e->conn->on_interrupt ((void *) e->conn, e->conn->private_ctx, e->qid); + if (e->conn->on_interrupt != NULL) + e->conn->on_interrupt ((void *) e->conn, e->conn->private_ctx, e->qid); return MEMIF_ERR_SUCCESS; } @@ -724,7 +733,8 @@ memif_poll_event (int timeout) get_fd_list_elt (&elt, lm->interrupt_list, lm->interrupt_list_len, evt.data.fd); if (elt != NULL) { - elt->conn->on_interrupt ((void *) elt->conn, elt->conn->private_ctx, elt->qid); + if (elt->conn->on_interrupt != NULL) + elt->conn->on_interrupt ((void *) elt->conn, elt->conn->private_ctx, elt->qid); return 0; } get_fd_list_elt (&elt, lm->control_list, lm->control_list_len, evt.data.fd); @@ -817,7 +827,8 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) { if (mq->int_fd > 0) { - lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL); + if (c->on_interrupt != NULL) + lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL); close (mq->int_fd); } get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, mq->int_fd); @@ -853,8 +864,8 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) DBG_UNIX ("timerfd_settime: arm"); } } - lm->disconn_slaves++; + lm->disconn_slaves++; return err; } @@ -1188,6 +1199,7 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, count -= 2; *count_out += 2; + mq->alloc_bufs -= 2; } b0 = (bufs + *count_out); tail = (b0->desc_index + 1) & mask; @@ -1195,6 +1207,7 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, count--; *count_out += 1; + mq->alloc_bufs--; } MEMIF_MEORY_BARRIER (); ring->tail = tail; @@ -1297,12 +1310,11 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, uint16_t mask = (1 << mq->log2_ring_size) - 1; memif_buffer_t *b0, *b1; *rx = 0; - + uint64_t b; ssize_t r = read (mq->int_fd, &b, sizeof (b)); if ((r == -1) && (errno != EAGAIN)) return memif_syscall_error_handler (errno); - if (head == mq->last_head) return 0; @@ -1453,3 +1465,21 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t *md, return err; /* 0 */ } + +int +memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) +{ + memif_connection_t *c = (memif_connection_t *) conn; + *efd = -1; + if (c == NULL) + return MEMIF_ERR_NOCONN; + if (c->fd < 0) + return MEMIF_ERR_DISCONNECTED; + uint8_t num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + if (qid >= num) + return MEMIF_ERR_QID; + + *efd = c->rx_queues[qid].int_fd; + + return MEMIF_ERR_SUCCESS; +} diff --git a/src/memif_private.h b/src/memif_private.h index dbffc89..47e6e00 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -169,6 +169,7 @@ typedef struct memif_connection_t *conn; /* used only in interrupt list */ uint8_t qid; + uint8_t *filename; } memif_fd_list_elt_t; /* @@ -182,11 +183,12 @@ typedef struct struct itimerspec arm, disarm; uint16_t disconn_slaves; - /* master iomplementation... */ + /* master implementation... */ memif_socket_t ms; uint16_t control_list_len; uint16_t interrupt_list_len; + uint16_t listener_list_len; memif_fd_list_elt_t *control_list; memif_fd_list_elt_t *interrupt_list; } libmemif_main_t; diff --git a/src/socket.c b/src/socket.c index ab3138b..95df9bc 100644 --- a/src/socket.c +++ b/src/socket.c @@ -538,8 +538,11 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); int i; - for (i = 0; i < c->args.num_m2s_rings; i++) - lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); + if (c->on_interrupt != NULL) + { + for (i = 0; i < c->args.num_m2s_rings; i++) + lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); + } c->on_connect ((void *) c, c->private_ctx); @@ -561,8 +564,11 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) strncpy ((char *) c->remote_if_name, (char *) cm->if_name, strlen ((char *) cm->if_name)); int i; - for (i = 0; i < c->args.num_s2m_rings; i++) - lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); + if (c->on_interrupt != NULL) + { + for (i = 0; i < c->args.num_s2m_rings; i++) + lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); + } c->on_connect ((void *) c, c->private_ctx); From 9da52c190422c63d494f25d3cd3223848f4261f1 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 9 Aug 2017 14:29:50 +0200 Subject: [PATCH 38/63] ODPM-462 --- README.md | 4 +- examples/icmp_responder/main.c | 7 +- src/main.c | 376 +++++++++++++++++++++++---------- src/memif_private.h | 35 +-- src/socket.c | 153 +++++++++----- src/socket.h | 10 +- test/main_test.c | 68 +++++- test/socket_test.c | 33 ++- 8 files changed, 484 insertions(+), 202 deletions(-) diff --git a/README.md b/README.md index 77a0b18..35af8ae 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ Shared memory packet interface (memif) provides high performance packet transmit - [x] Simplify file descriptor event polling (one handler for control and interrupt channel) - [x] Multiple connections - [x] Multiple queues - - [ ] Multi-thread support -- [ ] Master mode + - [x] Multi-thread support +- [x] Master mode - [ ] Multiple regions - [ ] Performance testing - [ ] Documentation diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index bb609c7..f070599 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -77,8 +77,6 @@ typedef struct uint16_t index; /* memif conenction handle */ memif_conn_handle_t conn; - /* transmit queue id */ - uint16_t tx_qid; /* tx buffers */ memif_buffer_t *tx_bufs; /* allocated tx buffers counter */ @@ -393,7 +391,7 @@ icmpr_memif_create (long index) memif_conn_args_t args; int fd = -1; memset (&args, 0, sizeof (args)); - args.is_master = 0; + args.is_master = 1; args.log2_ring_size = 10; args.buffer_size = 2048; args.num_s2m_rings = 2; @@ -419,8 +417,6 @@ icmpr_memif_create (long index) } c->index = index; - /* tx queue id */ - c->tx_qid = 0; /* alloc memif buffers */ c->rx_buf_num = 0; c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); @@ -479,6 +475,7 @@ print_help () printf ("\tdel - delete memif\n"); printf ("\tshow - show connection details\n"); printf ("\tip-set - set interface ip address\n"); + printf ("\trx-mode - set queue rx mode\n"); } int icmpr_free () diff --git a/src/main.c b/src/main.c index e47d246..a00c8bd 100644 --- a/src/main.c +++ b/src/main.c @@ -293,58 +293,42 @@ memif_control_fd_update (int fd, uint8_t events) return memif_add_epoll_fd (fd, evt); } -static int -add_fd_list_elt (memif_fd_list_elt_t *e, memif_fd_list_type_t type) +int +add_list_elt (memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len) { libmemif_main_t *lm = &libmemif_main; - memif_fd_list_elt_t *list = - (type == MEMIF_FD_LIST_CONTROL) ? lm->control_list : lm->interrupt_list; - uint16_t len = - (type == MEMIF_FD_LIST_CONTROL) ? lm->control_list_len : lm->interrupt_list_len; int i; - for (i = 0; i < len; i++) + for (i = 0; i < *len; i++) { - if (list[i].conn == NULL) + if ((*list)[i].data_struct == NULL) { - list[i].fd = e->fd; - list[i].conn = e->conn; - list[i].qid = e->qid; + (*list)[i].key = e->key; + (*list)[i].data_struct = e->data_struct; return i; } } - memif_fd_list_elt_t *tmp; - tmp = realloc (list, sizeof (memif_fd_list_elt_t) * len * 2); + memif_list_elt_t *tmp; + tmp = realloc (*list, sizeof (memif_list_elt_t) * *len * 2); if (tmp == NULL) return -1; - tmp[len].fd = e->fd; - tmp[len].conn = e->conn; - tmp[len].qid = e->qid; - - if (type == MEMIF_FD_LIST_CONTROL) - { - lm->control_list = tmp; - lm->control_list_len *= 2; - } - else - { - lm->interrupt_list = tmp; - lm->interrupt_list_len *= 2; - } - - DBG ("con: %u, int: %u", lm->control_list_len, lm->interrupt_list_len); + tmp[*len].key = e->key; + tmp[*len].data_struct = e->data_struct; + i = *len; + *len = *len * 2; + *list = tmp; - return len; + return i; } -static int -get_fd_list_elt (memif_fd_list_elt_t **e, memif_fd_list_elt_t *list, uint16_t len, int fd) +int +get_list_elt (memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key) { int i; for (i = 0; i < len; i++) { - if (list[i].fd == fd) + if (list[i].key == key) { *e = &list[i]; return 0; @@ -354,16 +338,17 @@ get_fd_list_elt (memif_fd_list_elt_t **e, memif_fd_list_elt_t *list, uint16_t le return -1; } -static int -free_fd_list_elt (memif_fd_list_elt_t *list, uint16_t len, int fd) +/* does not free memory, only marks element as free */ +int +free_list_elt (memif_list_elt_t *list, uint16_t len, int key) { int i; for (i = 0; i < len; i++) { - if (list[i].fd == fd) + if (list[i].key == key) { - list[i].fd = -1; - list[i].conn = NULL; + list[i].key = -1; + list[i].data_struct = NULL; return 0; } } @@ -394,22 +379,36 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) memset (&lm->ms, 0, sizeof (memif_socket_t)); - lm->control_list_len = 1; - lm->interrupt_list_len = 1; + lm->control_list_len = 2; + lm->interrupt_list_len = 2; + lm->listener_list_len = 1; + lm->pending_list_len = 1; - lm->control_list = malloc (sizeof (memif_fd_list_elt_t) * lm->control_list_len); - lm->interrupt_list = malloc (sizeof (memif_fd_list_elt_t) * lm->interrupt_list_len); + lm->control_list = malloc (sizeof (memif_list_elt_t) * lm->control_list_len); + lm->interrupt_list = malloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len); + lm->listener_list = malloc (sizeof (memif_list_elt_t) * lm->listener_list_len); + lm->pending_list = malloc (sizeof (memif_list_elt_t) * lm->pending_list_len); int i; for (i = 0; i < lm->control_list_len; i++) { - lm->control_list[i].fd = -1; - lm->control_list[i].conn = NULL; + lm->control_list[i].key = -1; + lm->control_list[i].data_struct = NULL; } for (i = 0; i < lm->interrupt_list_len; i++) { - lm->interrupt_list[i].fd = -1; - lm->interrupt_list[i].conn = NULL; + lm->interrupt_list[i].key = -1; + lm->interrupt_list[i].data_struct = NULL; + } + for (i = 0; i < lm->listener_list_len; i++) + { + lm->listener_list[i].key = -1; + lm->listener_list[i].data_struct = NULL; + } + for (i = 0; i < lm->pending_list_len; i++) + { + lm->pending_list[i].key = -1; + lm->pending_list[i].data_struct = NULL; } lm->disconn_slaves = 0; @@ -473,9 +472,8 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, memif_interrupt_t *on_interrupt, void *private_ctx) { - int err; - int sockfd = -1; - memif_fd_list_elt_t list_elt; + int err, i, index, sockfd = -1; + memif_list_elt_t list_elt; memif_connection_t *conn = (memif_connection_t *) *c; if (conn != NULL) { @@ -562,30 +560,120 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, strncpy ((char *) conn->args.secret, (char *) args->secret, l); } -/* SLAVE */ - if (lm->disconn_slaves == 0) + if (conn->args.is_master) { - if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + memif_socket_t *ms; + memif_list_elt_t elt; + for (i = 0; i < lm->listener_list_len; i++) { - err = memif_syscall_error_handler (errno); - goto error; + if ((ms = (memif_socket_t *) lm->listener_list[i].data_struct) != NULL) + { + if (strncmp ((char *) ms->filename, (char *) conn->args.socket_filename, + strlen ((char *) ms->filename)) == 0) + { + /* add interface to listener socket */ + elt.key = conn->args.interface_id; + *c = elt.data_struct = conn; + add_list_elt (&elt, &ms->interface_list, &ms->interface_list_len); + ms->use_count++; + conn->listener_fd = ms->fd; + break; + } + } + else + { + struct stat file_stat; + if (stat ((char *) conn->args.socket_filename, &file_stat) == 0) + { + if (S_ISSOCK (file_stat.st_mode)) + unlink ((char *) conn->args.socket_filename); + else + return memif_syscall_error_handler (errno); + } + DBG ("creating socket file"); + ms = malloc (sizeof (memif_socket_t)); + ms->filename = malloc (strlen ((char *) conn->args.socket_filename)); + strncpy ((char *) ms->filename, (char *) conn->args.socket_filename, + strlen ((char *) conn->args.socket_filename)); + ms->interface_list_len = 1; + ms->interface_list = malloc ( + sizeof (memif_list_elt_t) * ms->interface_list_len); + DBG ("filename: %s", (char *) ms->filename); + struct sockaddr_un un = { 0 }; + int on = 1; + + ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0); + if (ms->fd < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + DBG ("socket %d created", ms->fd); + un.sun_family = AF_UNIX; + strncpy ((char *) un.sun_path, (char *) ms->filename, + sizeof (un.sun_path) - 1); + DBG ("sockopt"); + if (setsockopt (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + DBG ("bind"); + if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + DBG ("listen"); + if (listen (ms->fd, 1) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + DBG ("stat"); + if (stat ((char *) ms->filename, &file_stat) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + + /* add interface to listener socket */ + elt.key = conn->args.interface_id; + *c = elt.data_struct = conn; + add_list_elt (&elt, &ms->interface_list, &ms->interface_list_len); + ms->use_count = 1; + conn->listener_fd = ms->fd; + + /* add listener socket to libmemif main */ + elt.key = ms->fd; + elt.data_struct = ms; + add_list_elt (&elt, &lm->listener_list, &lm->listener_list_len); + lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ); + break; + } } } - - lm->disconn_slaves++; - - list_elt.fd = -1; - *c = list_elt.conn = conn; - int index; - if ((index = add_fd_list_elt (&list_elt, MEMIF_FD_LIST_CONTROL)) < 0) + else { - err = MEMIF_ERR_NOMEM; - goto error; - } - - + if (lm->disconn_slaves == 0) + { + if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + } + lm->disconn_slaves++; + list_elt.key = -1; + *c = list_elt.data_struct = conn; + if ((index = add_list_elt (&list_elt, &lm->control_list, &lm->control_list_len)) < 0) + { + err = MEMIF_ERR_NOMEM; + goto error; + } + } conn->index = index; @@ -607,7 +695,8 @@ int memif_control_fd_handler (int fd, uint8_t events) { int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */ - memif_fd_list_elt_t *e = NULL; + uint16_t num; + memif_list_elt_t *e = NULL; memif_connection_t *conn; libmemif_main_t *lm = &libmemif_main; if (fd == lm->timerfd) @@ -617,9 +706,9 @@ memif_control_fd_handler (int fd, uint8_t events) size = read (fd, &b, sizeof (b)); for (i = 0; i < 4; i++) { - if ((lm->control_list[i].fd < 0) && (lm->control_list[i].conn != NULL)) + if ((lm->control_list[i].key < 0) && (lm->control_list[i].data_struct != NULL)) { - conn = lm->control_list[i].conn; + conn = lm->control_list[i].data_struct; if (conn->args.is_master) continue; @@ -644,7 +733,7 @@ memif_control_fd_handler (int fd, uint8_t events) conn->write_fn = memif_conn_fd_write_ready; conn->error_fn = memif_conn_fd_error; - lm->control_list[conn->index].fd = conn->fd; + lm->control_list[conn->index].key = conn->fd; lm->control_fd_update ( sockfd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE); @@ -669,32 +758,61 @@ memif_control_fd_handler (int fd, uint8_t events) } else { - get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); + DBG ("fd %d", fd); + get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); + if (e != NULL) + { + 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)->args.num_s2m_rings : + ((memif_connection_t *)e->data_struct)->args.num_m2s_rings; + for (i = 0; i < num; i++) + { + 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); + return MEMIF_ERR_SUCCESS; + } + } + } + return MEMIF_ERR_SUCCESS; + } + get_list_elt (&e, lm->listener_list, lm->listener_list_len, fd); if (e != NULL) { - if (e->conn->on_interrupt != NULL) - e->conn->on_interrupt ((void *) e->conn, e->conn->private_ctx, e->qid); + DBG ("listeneeeeeeeeeeer"); + memif_conn_fd_accept_ready ((memif_socket_t *)e->data_struct); return MEMIF_ERR_SUCCESS; } - get_fd_list_elt (&e, lm->control_list, lm->control_list_len, fd); + get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd); + if (e != NULL) + { + memif_read_ready (fd); + return MEMIF_ERR_SUCCESS; + } + + get_list_elt (&e, lm->control_list, lm->control_list_len, fd); if (e != NULL) { if (events & MEMIF_FD_EVENT_READ) { - err = e->conn->read_fn (e->conn); + err = ((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 = e->conn->write_fn (e->conn); + err = ((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 = e->conn->error_fn (e->conn); + err = ((memif_connection_t *)e->data_struct)->error_fn (e->data_struct); if (err != MEMIF_ERR_SUCCESS) return err; } @@ -714,9 +832,10 @@ int memif_poll_event (int timeout) { libmemif_main_t *lm = &libmemif_main; - memif_fd_list_elt_t *elt; + memif_list_elt_t *elt; struct epoll_event evt, *e; - int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */ + int en = 0, err = MEMIF_ERR_SUCCESS, i = 0; /* 0 */ + uint16_t num; uint32_t events = 0; memset (&evt, 0, sizeof (evt)); evt.events = EPOLLIN | EPOLLOUT; @@ -730,14 +849,21 @@ memif_poll_event (int timeout) } if (en > 0) { - get_fd_list_elt (&elt, lm->interrupt_list, lm->interrupt_list_len, evt.data.fd); + get_list_elt (&elt, lm->interrupt_list, lm->interrupt_list_len, evt.data.fd); if (elt != NULL) { - if (elt->conn->on_interrupt != NULL) - elt->conn->on_interrupt ((void *) elt->conn, elt->conn->private_ctx, elt->qid); - return 0; + num = (((memif_connection_t *)elt->data_struct)->args.is_master) ? + ((memif_connection_t *)elt->data_struct)->args.num_s2m_rings : + ((memif_connection_t *)elt->data_struct)->args.num_m2s_rings; + for (i = 0; i < num; i++) + { + if (((memif_connection_t *)elt->data_struct)->on_interrupt != NULL) + ((memif_connection_t *)elt->data_struct)->on_interrupt ( + elt->data_struct, ((memif_connection_t *)elt->data_struct)->private_ctx, i); + return 0; + } } - get_fd_list_elt (&elt, lm->control_list, lm->control_list_len, evt.data.fd); + get_list_elt (&elt, lm->control_list, lm->control_list_len, evt.data.fd); if (elt != NULL) { if (evt.events & EPOLLIN) @@ -777,22 +903,22 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) int err = MEMIF_ERR_SUCCESS, i; /* 0 */ memif_queue_t *mq; libmemif_main_t *lm = &libmemif_main; - memif_fd_list_elt_t *e; + memif_list_elt_t *e; c->on_disconnect ((void *) c, c->private_ctx); if (c->fd > 0) { - memif_msg_send_disconnect (c, c->remote_disconnect_string, 1); + memif_msg_send_disconnect (c->fd, "interface deleted", 0); lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL); close (c->fd); } - get_fd_list_elt (&e, lm->control_list, lm->control_list_len, c->fd); + get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd); if (e != NULL) { - e->fd = c->fd = -1; - if (is_del) - e->conn = NULL; + if (is_del || c->args.is_master) + free_list_elt (lm->control_list, lm->control_list_len, c->fd); + e->key = c->fd = -1; } if (c->tx_queues != NULL) @@ -805,12 +931,8 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) { if (mq->int_fd > 0) close (mq->int_fd); - get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, mq->int_fd); - if (e != NULL) - { - e->fd = mq->int_fd = -1; - e->conn = NULL; - } + free_list_elt (lm->interrupt_list, lm->interrupt_list_len, mq->int_fd); + mq->int_fd = -1; } } free (c->tx_queues); @@ -831,12 +953,8 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL); close (mq->int_fd); } - get_fd_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, mq->int_fd); - if (e != NULL) - { - e->fd = mq->int_fd = -1; - e->conn = NULL; - } + free_list_elt (lm->interrupt_list, lm->interrupt_list_len, mq->int_fd); + mq->int_fd = -1; } } free (c->rx_queues); @@ -875,6 +993,8 @@ memif_delete (memif_conn_handle_t *conn) { memif_connection_t *c = (memif_connection_t *) *conn; libmemif_main_t *lm = &libmemif_main; + memif_list_elt_t *e = NULL; + memif_socket_t *ms = NULL; int err; @@ -882,13 +1002,39 @@ memif_delete (memif_conn_handle_t *conn) if (err == MEMIF_ERR_NOCONN) return err; - lm->disconn_slaves--; - if (lm->disconn_slaves == 0) + if (c->args.is_master) { - if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + get_list_elt (&e, lm->listener_list, lm->listener_list_len, c->listener_fd); + if (e != NULL) { - err = memif_syscall_error_handler (errno); - DBG ("timerfd_settime: disarm"); + ms = (memif_socket_t *) e->data_struct; + ms->use_count--; + free_list_elt (ms->interface_list, ms->interface_list_len, c->args.interface_id); + if (ms->use_count <= 0) + { + lm->control_fd_update (c->listener_fd, MEMIF_FD_EVENT_DEL); + free_list_elt (lm->listener_list, lm->listener_list_len, c->listener_fd); + close (c->listener_fd); + c->listener_fd = ms->fd = -1; + free (ms->interface_list); + ms->interface_list = NULL; + free (ms->filename); + ms->filename = NULL; + free (ms); + ms = NULL; + } + } + } + else + { + lm->disconn_slaves--; + if (lm->disconn_slaves <= 0) + { + if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) + { + err = memif_syscall_error_handler (errno); + DBG ("timerfd_settime: disarm"); + } } } @@ -969,7 +1115,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) memif_region_t *r; int i,j; libmemif_main_t *lm = &libmemif_main; - memif_fd_list_elt_t e; + memif_list_elt_t e; conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t)); if (conn->regions == NULL) @@ -1039,10 +1185,9 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); /* add int fd to interrupt fd list */ - e.fd = mq[x].int_fd; - e.conn = conn; - e.qid = x; - add_fd_list_elt (&e, MEMIF_FD_LIST_INTERRUPT); + e.key = mq[x].int_fd; + e.data_struct = conn; + add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len); mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x); DBG ("RING: %p I: %d", mq[x].ring, x); @@ -1062,10 +1207,9 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); /* add int fd to interrupt fd list */ - e.fd = mq[x].int_fd; - e.conn = conn; - e.qid = x; - add_fd_list_elt (&e, MEMIF_FD_LIST_INTERRUPT); + e.key = mq[x].int_fd; + e.data_struct = conn; + add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len); mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x); DBG ("RING: %p I: %d", mq[x].ring, x); diff --git a/src/memif_private.h b/src/memif_private.h index 47e6e00..d1ff60f 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -78,12 +78,6 @@ #endif /* MEMIF_DBG */ -typedef enum -{ - MEMIF_FD_LIST_CONTROL, - MEMIF_FD_LIST_INTERRUPT -} memif_fd_list_type_t; - typedef struct { void *shm; @@ -127,6 +121,7 @@ typedef struct memif_connection memif_conn_args_t args; int fd; + int listener_fd; memif_fn *write_fn, *read_fn, *error_fn; @@ -155,10 +150,9 @@ typedef struct memif_connection */ typedef struct { - int fd; - uint16_t use_count; - uint8_t *filename; -} memif_socket_t; + int key; /* fd or id */ + void *data_struct; +} memif_list_elt_t; /* * WIP @@ -166,11 +160,11 @@ typedef struct typedef struct { int fd; - memif_connection_t *conn; - /* used only in interrupt list */ - uint8_t qid; + uint16_t use_count; uint8_t *filename; -} memif_fd_list_elt_t; + uint16_t interface_list_len; + memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */ +} memif_socket_t; /* * WIP @@ -189,8 +183,11 @@ typedef struct uint16_t control_list_len; uint16_t interrupt_list_len; uint16_t listener_list_len; - memif_fd_list_elt_t *control_list; - memif_fd_list_elt_t *interrupt_list; + uint16_t pending_list_len; + memif_list_elt_t *control_list; + memif_list_elt_t *interrupt_list; + memif_list_elt_t *listener_list; + memif_list_elt_t *pending_list; } libmemif_main_t; extern libmemif_main_t libmemif_main; @@ -209,6 +206,12 @@ int memif_disconnect_internal (memif_connection_t *c, uint8_t is_del); /* map errno to memif error code */ int memif_syscall_error_handler (int err_code); +int add_list_elt (memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len); + +int get_list_elt (memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key); + +int free_list_elt (memif_list_elt_t *list, uint16_t len, int key); + #ifndef __NR_memfd_create #if defined __x86_64__ #define __NR_memfd_create 319 diff --git a/src/socket.c b/src/socket.c index 95df9bc..4063f87 100644 --- a/src/socket.c +++ b/src/socket.c @@ -35,6 +35,8 @@ #define memif_min(a,b) ((a < b) ? (a) : (b)) +#define APP_NAME "ICMP_Responder" + /* sends msg to socket */ static_fn int memif_msg_send (int fd, memif_msg_t *msg, int afd) @@ -99,7 +101,7 @@ memif_msg_enq_ack (memif_connection_t *c) } static_fn int -memif_msg_send_hello (memif_connection_t *c) +memif_msg_send_hello (int fd) { memif_msg_t msg = { 0 }; memif_msg_hello_t *h = &msg.hello; @@ -111,12 +113,12 @@ memif_msg_send_hello (memif_connection_t *c) h->max_region = MEMIF_MAX_REGION; h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE; - strncpy ((char *) h->name, (char *) c->args.instance_name, - strlen ((char *) c->args.instance_name)); + strncpy ((char *) h->name, APP_NAME, + strlen (APP_NAME)); /* msg hello is not enqueued but sent directly, because it is the first msg to be sent */ - return memif_msg_send (c->fd, &msg, -1); + return memif_msg_send (fd, &msg, -1); } /* send id and secret (optional) for interface identification */ @@ -314,7 +316,7 @@ memif_msg_enq_connected (memif_connection_t *c) /* specifie protocol for disconnect msg err_code so that it will be compatible with VPP? (header/doc) */ int -memif_msg_send_disconnect (memif_connection_t *c, uint8_t *err_string, uint32_t err_code) +memif_msg_send_disconnect (int fd, uint8_t *err_string, uint32_t err_code) { memif_msg_t msg = { 0 }; memif_msg_disconnect_t *d = &msg.disconnect; @@ -329,7 +331,7 @@ memif_msg_send_disconnect (memif_connection_t *c, uint8_t *err_string, uint32_t } strncpy ((char *) d->string, (char *) err_string, l); - return memif_msg_send (c->fd, &msg, -1); + return memif_msg_send (fd, &msg, -1); } static_fn int @@ -360,9 +362,13 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) /* handle interface identification (id, secret (optional)) */ static_fn int -memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) +memif_msg_receive_init (memif_socket_t *ms, int fd, memif_msg_t *msg) { memif_msg_init_t *i = &msg->init; + memif_list_elt_t *elt = NULL; + memif_list_elt_t elt2; + memif_connection_t *c = NULL; + libmemif_main_t *lm = &libmemif_main; uint8_t err_string[96]; int err = MEMIF_ERR_SUCCESS; /* 0 */ int err_disc; @@ -373,7 +379,9 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) err = MEMIF_ERR_PROTO; goto error; } - if (c->args.interface_id != i->id) + + get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id); + if (elt == NULL) { DBG ("MEMIF_ID_ERR"); strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR)); @@ -381,6 +389,7 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) goto error; } + c = (memif_connection_t *) elt->data_struct; if (!(c->args.is_master)) { @@ -396,6 +405,9 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) err = MEMIF_ERR_ALRCONN; goto error; } + + c->fd = fd; + if (i->mode != c->args.mode) { DBG ("MEMIF_MODE_ERR"); @@ -440,15 +452,24 @@ memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg) } } + c->read_fn = memif_conn_fd_read_ready; + c->write_fn = memif_conn_fd_write_ready; + c->error_fn = memif_conn_fd_error; + + elt2.key = c->fd; + elt2.data_struct = c; + + add_list_elt (&elt2, &lm->control_list, &lm->control_list_len); + free_list_elt (lm->pending_list, lm->pending_list_len, fd); + return err; error: - if (c->fd != -1) - { - err_disc = memif_msg_send_disconnect (c, err_string, 1); - if (err_disc != 0) - return err_disc; - } + memif_msg_send_disconnect (fd, err_string, 0); + lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL); + free_list_elt (lm->pending_list, lm->pending_list_len, fd); + close (fd); + fd = -1; return err; } @@ -464,7 +485,7 @@ memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd) if (ar->index > MEMIF_MAX_REGION) return MEMIF_ERR_MAXREG; - mr = (memif_region_t *) realloc (c->regions, sizeof (memif_region_t)); + mr = (memif_region_t *) realloc (c->regions, sizeof (memif_region_t) * (ar->index + 1)); if (mr == NULL) return memif_syscall_error_handler (errno); c->regions = mr; @@ -491,33 +512,33 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) { if (ar->index > MEMIF_MAX_S2M_RING) return MEMIF_ERR_MAXRING; + if (ar->index >= c->args.num_s2m_rings) + return MEMIF_ERR_MAXRING; - mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); - mq->int_fd = fd; - mq->log2_ring_size = ar->log2_ring_size; - mq->region = ar->region; - mq->offset = ar->offset; - - /* TODO: support multiple rings */ + mq = (memif_queue_t *) realloc (c->rx_queues, sizeof (memif_queue_t) * (ar->index + 1)); + if (mq == NULL) + return memif_syscall_error_handler (errno); c->rx_queues = mq; - c->args.num_s2m_rings++; + c->rx_queues[ar->index].int_fd = fd; + c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size; + c->rx_queues[ar->index].region = ar->region; + c->rx_queues[ar->index].offset = ar->offset; } else { if (ar->index > MEMIF_MAX_M2S_RING) return MEMIF_ERR_MAXRING; + if (ar->index >= c->args.num_m2s_rings) + return MEMIF_ERR_MAXRING; - mq = (memif_queue_t *) malloc (sizeof (memif_queue_t)); + mq = (memif_queue_t *) realloc (c->tx_queues, sizeof (memif_queue_t) * (ar->index + 1)); if (mq == NULL) return memif_syscall_error_handler (errno); - mq->int_fd = fd; - mq->log2_ring_size = ar->log2_ring_size; - mq->region = ar->region; - mq->offset = ar->offset; - - /* TODO: support multiple rings */ c->tx_queues = mq; - c->args.num_m2s_rings++; + c->tx_queues[ar->index].int_fd = fd; + c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size; + c->tx_queues[ar->index].region = ar->region; + c->tx_queues[ar->index].offset = ar->offset; } return MEMIF_ERR_SUCCESS; /* 0 */ @@ -529,6 +550,7 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) { memif_msg_connect_t *cm = &msg->connect; libmemif_main_t *lm = &libmemif_main; + memif_list_elt_t elt; int err; err = memif_connect1 (c); @@ -541,7 +563,14 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) if (c->on_interrupt != NULL) { for (i = 0; i < c->args.num_m2s_rings; i++) + { + elt.key = c->rx_queues[i].int_fd; + elt.data_struct = c; + add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len); + lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); + } + } c->on_connect ((void *) c, c->private_ctx); @@ -591,7 +620,7 @@ memif_msg_receive_disconnect (memif_connection_t *c, memif_msg_t *msg) } static_fn int -memif_msg_receive (memif_connection_t *c) +memif_msg_receive (int ifd) { char ctl[CMSG_SPACE (sizeof (int)) + CMSG_SPACE (sizeof (struct ucred))] = { 0 }; @@ -602,6 +631,10 @@ memif_msg_receive (memif_connection_t *c) int err = MEMIF_ERR_SUCCESS; /* 0 */ int fd = -1; int i; + libmemif_main_t *lm = &libmemif_main; + memif_connection_t *c = NULL; + memif_socket_t *ms = NULL; + memif_list_elt_t *elt = NULL; iov[0].iov_base = (void *) &msg; iov[0].iov_len = sizeof (memif_msg_t); @@ -610,8 +643,8 @@ memif_msg_receive (memif_connection_t *c) mh.msg_control = ctl; mh.msg_controllen = sizeof (ctl); - DBG ("recvmsg fd %d", c->fd); - size = recvmsg (c->fd, &mh, 0); + DBG ("recvmsg fd %d", ifd); + size = recvmsg (ifd, &mh, 0); DBG ("done"); if (size != sizeof (memif_msg_t)) { @@ -644,6 +677,10 @@ memif_msg_receive (memif_connection_t *c) DBG ("Message type %u received", msg.type); + get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd); + if (elt != NULL) + c = (memif_connection_t *) elt->data_struct; + switch (msg.type) { case MEMIF_MSG_TYPE_ACK: @@ -673,11 +710,19 @@ memif_msg_receive (memif_connection_t *c) break; case MEMIF_MSG_TYPE_INIT: - if ((err = memif_msg_receive_init (c, &msg)) != MEMIF_ERR_SUCCESS) + get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd); + if (elt == NULL) + return -1; + ms = (memif_socket_t *) elt->data_struct; + if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS) return err; /* c->remote_pid = cr->pid */ /* c->remote_uid = cr->uid */ /* c->remote_gid = cr->gid */ + get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd); + if (elt == NULL) + return -1; + c = (memif_connection_t *) elt->data_struct; if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) return err; break; @@ -699,7 +744,7 @@ memif_msg_receive (memif_connection_t *c) case MEMIF_MSG_TYPE_CONNECT: if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS) return err; - if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS) return err; break; @@ -718,7 +763,8 @@ memif_msg_receive (memif_connection_t *c) break; } - c->flags |= MEMIF_CONNECTION_FLAG_WRITE; + if (c != NULL) + c->flags |= MEMIF_CONNECTION_FLAG_WRITE; /* libmemif_main_t *lm = &libmemif_main; lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */ return MEMIF_ERR_SUCCESS; /* 0 */ @@ -739,9 +785,7 @@ int memif_conn_fd_read_ready (memif_connection_t *c) { int err; - DBG ("call recv"); - err = memif_msg_receive (c); - DBG ("recv finished"); + err = memif_msg_receive (c->fd); if (err != 0) { err = memif_disconnect_internal (c, 0); @@ -781,28 +825,41 @@ memif_conn_fd_write_ready (memif_connection_t *c) } int -memif_conn_fd_accept_ready (memif_connection_t *c) +memif_conn_fd_accept_ready (memif_socket_t *ms) { int addr_len; struct sockaddr_un client; int conn_fd; + libmemif_main_t *lm = &libmemif_main; DBG ("accept called"); addr_len = sizeof (client); - conn_fd = accept (c->fd, (struct sockaddr *) &client, (socklen_t *) &addr_len); + conn_fd = accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) &addr_len); if (conn_fd < 0) { return memif_syscall_error_handler (errno); } - DBG ("accept fd %d", c->fd); + DBG ("accept fd %d", ms->fd); DBG ("conn fd %d", conn_fd); - c->read_fn = memif_conn_fd_read_ready; - c->write_fn = memif_conn_fd_write_ready; - c->error_fn = memif_conn_fd_error; - c->fd = conn_fd; + memif_list_elt_t elt; + elt.key = conn_fd; + elt.data_struct = ms; + + add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len); + lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE); - return memif_msg_send_hello (c); + return memif_msg_send_hello (conn_fd); +} + +int +memif_read_ready (int fd) +{ + int err; + DBG ("call recv"); + err = memif_msg_receive (fd); + DBG ("recv finished"); + return err; } diff --git a/src/socket.h b/src/socket.h index d96c5dd..d333b3c 100644 --- a/src/socket.h +++ b/src/socket.h @@ -37,9 +37,11 @@ int memif_conn_fd_write_ready (memif_connection_t *c); int memif_conn_fd_error (memif_connection_t *c); -int memif_conn_fd_accept_ready (memif_connection_t *c); +int memif_conn_fd_accept_ready (memif_socket_t *ms); -int memif_msg_send_disconnect (memif_connection_t *c, uint8_t *err_string, uint32_t err_code); +int memif_read_ready (int fd); + +int memif_msg_send_disconnect (int fd, uint8_t *err_string, uint32_t err_code); /* when compiling unit tests, compile functions without static keyword and declare functions in header file */ @@ -50,7 +52,7 @@ int memif_msg_send (int fd, memif_msg_t *msg, int afd); int memif_msg_enq_ack (memif_connection_t *c); -int memif_msg_send_hello (memif_connection_t *c); +int memif_msg_send_hello (int fd); int memif_msg_enq_init (memif_connection_t *c); @@ -60,7 +62,7 @@ int memif_msg_enq_add_ring (memif_connection_t *c, uint8_t index, uint8_t dir); int memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg); -int memif_msg_receive_init (memif_connection_t *c, memif_msg_t *msg); +int memif_msg_receive_init (memif_socket_t *ms, int fd, memif_msg_t *msg); int memif_msg_receive_add_region (memif_connection_t *c, memif_msg_t *msg, int fd); diff --git a/test/main_test.c b/test/main_test.c index eec8421..781a9e7 100644 --- a/test/main_test.c +++ b/test/main_test.c @@ -14,6 +14,9 @@ * limitations under the License. *------------------------------------------------------------------ */ +#include +#include +#include #include @@ -157,6 +160,66 @@ START_TEST (test_create) } END_TEST +START_TEST (test_create_master) +{ + int err, rv; + memif_conn_handle_t conn = NULL; + memif_conn_args_t args; + memset (&args, 0, sizeof (args)); + args.is_master = 1; + + libmemif_main_t *lm = &libmemif_main; + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); + strncpy ((char *) args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + + if ((err = memif_create (&conn, &args, on_connect, + on_disconnect, on_interrupt, NULL)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + memif_connection_t *c = (memif_connection_t *) conn; + + ck_assert_ptr_ne (c, NULL); + + ck_assert_uint_eq (c->args.interface_id, args.interface_id); + ck_assert_uint_eq (c->args.is_master, args.is_master); + ck_assert_uint_eq (c->args.mode, args.mode); + + ck_assert_uint_eq (c->args.num_s2m_rings, MEMIF_DEFAULT_TX_QUEUES); + ck_assert_uint_eq (c->args.num_m2s_rings, MEMIF_DEFAULT_RX_QUEUES); + ck_assert_uint_eq (c->args.buffer_size, MEMIF_DEFAULT_BUFFER_SIZE); + ck_assert_uint_eq (c->args.log2_ring_size, MEMIF_DEFAULT_LOG2_RING_SIZE); + + ck_assert_ptr_eq (c->msg_queue, NULL); + ck_assert_ptr_eq (c->regions, NULL); + ck_assert_ptr_eq (c->tx_queues, NULL); + ck_assert_ptr_eq (c->rx_queues, NULL); + + ck_assert_int_eq (c->fd, -1); + + ck_assert_ptr_ne (c->on_connect, NULL); + ck_assert_ptr_ne (c->on_disconnect, NULL); + ck_assert_ptr_ne (c->on_interrupt, NULL); + + ck_assert_str_eq (c->args.interface_name, args.interface_name); + ck_assert_str_eq (c->args.instance_name, args.instance_name); + ck_assert_str_eq (c->args.socket_filename, SOCKET_FILENAME); + + struct stat file_stat; + + rv = stat (SOCKET_FILENAME, &file_stat); + ck_assert_int_eq (rv, 0); + + ck_assert (S_ISSOCK (file_stat.st_mode)); + + memif_delete (&conn); + ck_assert_ptr_eq (conn, NULL); +} +END_TEST + START_TEST (test_create_mult) { int err; @@ -277,8 +340,8 @@ START_TEST (test_control_fd_handler) register_fd_ready_fn (c, read_fn, write_fn, error_fn); c->fd = 69; - lm->control_list[0].fd = c->fd; - lm->control_list[0].conn = c; + lm->control_list[0].key = c->fd; + lm->control_list[0].data_struct = c; if ((err = memif_control_fd_handler ( c->fd, MEMIF_FD_EVENT_READ)) != MEMIF_ERR_SUCCESS) @@ -873,6 +936,7 @@ main_suite () tcase_add_test (tc_api, test_init); tcase_add_test (tc_api, test_init_epoll); tcase_add_test (tc_api, test_create); + tcase_add_test (tc_api, test_create_master); tcase_add_test (tc_api, test_create_mult); tcase_add_test (tc_api, test_control_fd_handler); tcase_add_test (tc_api, test_buffer_alloc); diff --git a/test/socket_test.c b/test/socket_test.c index 819a1d7..631970e 100644 --- a/test/socket_test.c +++ b/test/socket_test.c @@ -260,7 +260,7 @@ START_TEST (test_send_hello) memset (conn.args.instance_name, 0, sizeof (conn.args.instance_name)); strncpy ((char *) conn.args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); - if ((err = memif_msg_send_hello (&conn)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_send_hello (conn.fd)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_BAD_FD, "err code: %u, err msg: %s", err, memif_strerror (err)); } @@ -274,7 +274,7 @@ START_TEST (test_send_disconnect) /* only possible fail if memif_msg_send fails... */ /* obsolete without socket */ - if ((err = memif_msg_send_disconnect (&conn, "unit_test_dc", 0)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_send_disconnect (conn.fd, "unit_test_dc", 0)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_BAD_FD, "err code: %u, err msg: %s", err, memif_strerror (err)); } @@ -343,41 +343,52 @@ START_TEST (test_recv_init) strncpy ((char *) i->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); - if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + memif_socket_t ms; + ms.interface_list_len = 1; + ms.interface_list = malloc (sizeof (memif_list_elt_t)); + memif_list_elt_t elt; + elt.key = 69; + elt.data_struct = &conn; + add_list_elt (&elt, &ms.interface_list, &ms.interface_list_len); + + if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + + if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); i->version = 9; - if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_PROTO, "err code: %u, err msg: %s", err, memif_strerror (err)); i->version = MEMIF_VERSION; i->id = 78; - if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_ID, "err code: %u, err msg: %s", err, memif_strerror (err)); i->id = 69; i->mode = 1; - if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_MODE, "err code: %u, err msg: %s", err, memif_strerror (err)); i->mode = 0; i->secret[0] = '\0'; - if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_SECRET, "err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); conn.args.is_master = 0; - if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_ACCSLAVE, "err code: %u, err msg: %s", err, memif_strerror (err)); conn.args.is_master = 1; conn.fd = 5; - if ((err = memif_msg_receive_init (&conn, &msg)) != MEMIF_ERR_SUCCESS) + if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) ck_assert_msg ((err == MEMIF_ERR_ALRCONN) || (err == MEMIF_ERR_BAD_FD), "err code: %u, err msg: %s", err, memif_strerror (err)); } @@ -412,6 +423,10 @@ START_TEST (test_recv_add_ring) memif_connection_t conn; int fd = 5; memif_msg_t msg; + conn.args.num_s2m_rings = 2; + conn.args.num_m2s_rings = 2; + conn.rx_queues = NULL; + conn.tx_queues = NULL; msg.type = MEMIF_MSG_TYPE_ADD_RING; memif_msg_add_ring_t *ar = &msg.add_ring; From 177a31904515dcf704543b9e8737c7343909046a Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 10 Aug 2017 13:07:48 +0200 Subject: [PATCH 39/63] ODPM-462 patch --- README.md | 7 +- examples/icmp_responder/main.c | 39 +++++-- examples/icmp_responder2/main.c | 23 ++-- examples/icmp_responder3/main.c | 30 ++++-- src/libmemif.h | 35 ++++-- src/main.c | 181 +++++++++++++++++++------------- src/memif_private.h | 10 ++ src/socket.c | 23 ++-- 8 files changed, 226 insertions(+), 122 deletions(-) diff --git a/README.md b/README.md index 35af8ae..95da2db 100644 --- a/README.md +++ b/README.md @@ -88,9 +88,10 @@ For detailed information on api calls and structures please refer to [libmemif.h 2. Creating interface - Declare memif connction handle. memif\_conn\_handle\_t - Specify connection arguments. memif\_conn\_args\_t - - Declare callback functions called on connected/disconnected status changed. memif\_connection\_update\_t + - Declare callback functions called on connected/disconnected/interrupted status changed. memif\_connection\_update\_t - Call memif interface create function. memif\_create -> Arms timer file descriptor. +> If connection is in slave mode, arms timer file descriptor. +> If on interrupt callback is set to NULL, user will not be notified about interrupt. Use memif\_get\_queue\_efd call to get interrupt file descrip[tor for specific queue. 3. Connection establishment - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback.. @@ -121,7 +122,6 @@ For detailed information on api calls and structures please refer to [libmemif.h - Every api call returns error code (integer value) mapped to error string. - Call memif\_strerror will return error message assigned to specific error code. - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. - #### Example app: @@ -171,3 +171,4 @@ VPP config: # ping 192.168.1.2 ``` > Example applications use VPP default socket file for memif: /run/vpp/memif.sock +> For master mode, socket directory must exist prior to memif\_create call. diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index f070599..cddfe23 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -93,13 +93,14 @@ typedef struct memif_connection_t memif_connection[MAX_CONNS]; +/* print details for all memif connections */ static void print_memif_details () { memif_details_t md; ssize_t buflen; char *buf; - int err, i; + int err, i, e; printf ("MEMIF DETAILS\n"); printf ("==============================\n"); for (i = 0; i < MAX_CONNS; i++) @@ -159,10 +160,20 @@ print_memif_details () break; } printf ("\tsocket filename: %s\n",(char *) md.socket_filename); - printf ("\tring_size: %u\n", md.ring_size); - printf ("\tbuffer_size: %u\n", md.buffer_size); - printf ("\trx queues: %u\n", md.rx_queues); - printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\trx queues:\n"); + for (e = 0; e < md.rx_queues_num; e++) + { + printf ("\t\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 ("\ttx queues:\n"); + for (e = 0; e < md.tx_queues_num; e++) + { + printf ("\t\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"); @@ -373,7 +384,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) } int -icmpr_memif_create (long index) +icmpr_memif_create (long index, long mode) { if (index >= MAX_CONNS) { @@ -391,7 +402,7 @@ icmpr_memif_create (long index) memif_conn_args_t args; int fd = -1; memset (&args, 0, sizeof (args)); - args.is_master = 1; + args.is_master = mode; args.log2_ring_size = 10; args.buffer_size = 2048; args.num_s2m_rings = 2; @@ -595,6 +606,7 @@ user_input_handler () char *in = (char *) malloc (256); char *ui = fgets (in, 256, stdin); char *end; + long a; if (in[0] == '\n') goto done; ui = strtok (in, " "); @@ -613,9 +625,17 @@ user_input_handler () { ui = strtok (NULL, " "); if (ui != NULL) - icmpr_memif_create (strtol (ui, &end, 10)); + a = strtol (ui, &end, 10); else + { INFO ("expected id"); + goto done; + } + ui = strtok (NULL, " "); + if (ui != NULL) + icmpr_memif_create (a, strtol (ui, &end, 10)); + else + INFO ("expected mode <0|1>"); goto done; } else if (strncmp (ui, "del", 3) == 0) @@ -644,7 +664,6 @@ user_input_handler () else if (strncmp (ui, "rx-mode", 7) == 0) { ui = strtok (NULL, " "); - long a; if (ui != NULL) a = strtol (ui, &end, 10); else @@ -737,7 +756,7 @@ int main () /* 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); + err = memif_init (control_fd_update, APP_NAME); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_init: %s", memif_strerror (err)); diff --git a/examples/icmp_responder2/main.c b/examples/icmp_responder2/main.c index e0922fb..7ef0ae8 100644 --- a/examples/icmp_responder2/main.c +++ b/examples/icmp_responder2/main.c @@ -106,7 +106,7 @@ print_memif_details () ssize_t buflen = 2048; char *buf = malloc (buflen); memset (buf, 0, buflen); - int err; + int err, e; err = memif_get_details (c->conn, &md, buf, buflen); if (err != MEMIF_ERR_SUCCESS) @@ -147,10 +147,21 @@ print_memif_details () break; } printf ("\tsocket filename: %s\n",(char *) md.socket_filename); - printf ("\tring_size: %u\n", md.ring_size); - printf ("\tbuffer_size: %u\n", md.buffer_size); - printf ("\trx queues: %u\n", md.rx_queues); - printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\tsocket filename: %s\n",(char *) md.socket_filename); + printf ("\trx queues:\n"); + for (e = 0; e < md.rx_queues_num; e++) + { + printf ("\t\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 ("\ttx queues:\n"); + for (e = 0; e < md.tx_queues_num; e++) + { + printf ("\t\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"); @@ -373,7 +384,7 @@ int main (int argc, char *argv[]) /* 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 (NULL); + err = memif_init (NULL, APP_NAME); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_init: %s", memif_strerror (err)); diff --git a/examples/icmp_responder3/main.c b/examples/icmp_responder3/main.c index 17c0bf9..52d0fce 100644 --- a/examples/icmp_responder3/main.c +++ b/examples/icmp_responder3/main.c @@ -118,7 +118,7 @@ print_memif_details () memif_details_t md; ssize_t buflen; char *buf; - int err, i; + int err, i, e; printf ("MEMIF DETAILS\n"); printf ("==============================\n"); for (i = 0; i < MAX_CONNS; i++) @@ -133,15 +133,15 @@ print_memif_details () err = memif_get_details (c->conn, &md, buf, buflen); if (err != MEMIF_ERR_SUCCESS) - { + { if (err == MEMIF_ERR_NOCONN) - { + { printf ("\tno connection\n"); free (buf); continue; } else - { + { INFO ("%s", memif_strerror (err)); free (buf); continue; @@ -163,7 +163,7 @@ print_memif_details () printf ("master\n"); printf ("\tmode: "); switch (md.mode) - { + { case 0: printf ("ethernet\n"); break; @@ -178,10 +178,20 @@ print_memif_details () break; } printf ("\tsocket filename: %s\n",(char *) md.socket_filename); - printf ("\tring_size: %u\n", md.ring_size); - printf ("\tbuffer_size: %u\n", md.buffer_size); - printf ("\trx queues: %u\n", md.rx_queues); - printf ("\ttx queues: %u\n", md.tx_queues); + printf ("\trx queues:\n"); + for (e = 0; e < md.rx_queues_num; 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 ("\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"); @@ -846,7 +856,7 @@ int main () /* 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); + err = memif_init (control_fd_update, APP_NAME); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_init: %s", memif_strerror (err)); diff --git a/src/libmemif.h b/src/libmemif.h index 20ca6ba..ef85758 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -20,6 +20,8 @@ #define LIBMEMIF_VERSION "1.0" +#define MEMIF_DEFAULT_APP_NAME "libmemif-app" + #include #include @@ -156,6 +158,19 @@ typedef struct void *data; } memif_buffer_t; +/** \briefMemif 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 @@ -166,10 +181,10 @@ typedef struct @param role - 0 = master, 1 = slave @param mode - 0 = ethernet, 1 = ip , 2 = punt/inject @param socket_filename = socket filename - @param ring_size - ring size - @param buffer_size - size of buffer in shared memory - @param rx_queues - number of receive queues - @param tx_queues - number of transmit queues + @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 @@ -184,10 +199,10 @@ typedef struct uint8_t role; /* 0 = master, 1 = slave */ uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */ uint8_t *socket_filename; - uint32_t ring_size; - uint16_t buffer_size; - uint8_t rx_queues; - uint8_t tx_queues; + 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; @@ -251,6 +266,7 @@ int memif_get_details (memif_conn_handle_t conn, memif_details_t *md, /** \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 @@ -269,13 +285,14 @@ int memif_get_details (memif_conn_handle_t conn, memif_details_t *md, 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 */ -int memif_init (memif_control_fd_update_t *on_control_fd_update); +int memif_init (memif_control_fd_update_t *on_control_fd_update, char *app_name); /** \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. diff --git a/src/main.c b/src/main.c index a00c8bd..2a2e5a7 100644 --- a/src/main.c +++ b/src/main.c @@ -363,11 +363,25 @@ memif_control_fd_update_register (memif_control_fd_update_t *cb) lm->control_fd_update = cb; } -int memif_init (memif_control_fd_update_t *on_control_fd_update) +int memif_init (memif_control_fd_update_t *on_control_fd_update, char *app_name) { int err = MEMIF_ERR_SUCCESS; /* 0 */ libmemif_main_t *lm = &libmemif_main; + if (app_name) + { + lm->app_name = malloc (strlen (app_name)); + strncpy ((char *) lm->app_name, app_name, strlen (app_name)); + } + else + { + lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME)); + strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME, + strlen (MEMIF_DEFAULT_APP_NAME)); + } + + DBG ("app name: %s", lm->app_name); + /* register control fd update callback */ if (on_control_fd_update != NULL) memif_control_fd_update_register (on_control_fd_update); @@ -375,6 +389,7 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update) { memif_epfd = epoll_create (1); memif_control_fd_update_register (memif_control_fd_update); + DBG ("libmemif event polling initialized"); } memset (&lm->ms, 0, sizeof (memif_socket_t)); @@ -444,8 +459,8 @@ memif_get_ring (memif_connection_t *conn, memif_ring_type_t type, uint16_t ring_ void *p = conn->regions[0].shm; int ring_size = sizeof (memif_ring_t) + - sizeof (memif_desc_t) * (1 << conn->args.log2_ring_size); - p += (ring_num + type * conn->args.num_s2m_rings) * ring_size; + sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size); + p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size; return (memif_ring_t *) p; } @@ -516,6 +531,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, conn->on_disconnect = on_disconnect; conn->on_interrupt = on_interrupt; conn->private_ctx = private_ctx; + memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t)); uint8_t l = strlen ((char *) args->interface_name); strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, l); @@ -562,6 +578,7 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, if (conn->args.is_master) { + conn->run_args.buffer_size = conn->args.buffer_size; memif_socket_t *ms; memif_list_elt_t elt; for (i = 0; i < lm->listener_list_len; i++) @@ -704,7 +721,7 @@ memif_control_fd_handler (int fd, uint8_t events) uint64_t b; ssize_t size; size = read (fd, &b, sizeof (b)); - for (i = 0; i < 4; i++) + for (i = 0; i < lm->control_list_len; i++) { if ((lm->control_list[i].key < 0) && (lm->control_list[i].data_struct != NULL)) { @@ -758,15 +775,14 @@ memif_control_fd_handler (int fd, uint8_t events) } else { - DBG ("fd %d", fd); get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); if (e != NULL) { 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)->args.num_s2m_rings : - ((memif_connection_t *)e->data_struct)->args.num_m2s_rings; + ((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) @@ -783,7 +799,6 @@ memif_control_fd_handler (int fd, uint8_t events) get_list_elt (&e, lm->listener_list, lm->listener_list_len, fd); if (e != NULL) { - DBG ("listeneeeeeeeeeeer"); memif_conn_fd_accept_ready ((memif_socket_t *)e->data_struct); return MEMIF_ERR_SUCCESS; } @@ -849,32 +864,14 @@ memif_poll_event (int timeout) } if (en > 0) { - get_list_elt (&elt, lm->interrupt_list, lm->interrupt_list_len, evt.data.fd); - if (elt != NULL) - { - num = (((memif_connection_t *)elt->data_struct)->args.is_master) ? - ((memif_connection_t *)elt->data_struct)->args.num_s2m_rings : - ((memif_connection_t *)elt->data_struct)->args.num_m2s_rings; - for (i = 0; i < num; i++) - { - if (((memif_connection_t *)elt->data_struct)->on_interrupt != NULL) - ((memif_connection_t *)elt->data_struct)->on_interrupt ( - elt->data_struct, ((memif_connection_t *)elt->data_struct)->private_ctx, i); - return 0; - } - } - get_list_elt (&elt, lm->control_list, lm->control_list_len, evt.data.fd); - if (elt != NULL) - { - 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; - err = memif_control_fd_handler (evt.data.fd, events); - return err; - } + 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; + err = memif_control_fd_handler (evt.data.fd, events); + return err; } return 0; } @@ -923,7 +920,7 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) if (c->tx_queues != NULL) { - num = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + num = (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]; @@ -941,7 +938,7 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) if (c->rx_queues != NULL) { - num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + num = (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]; @@ -972,19 +969,23 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) c->regions = NULL; } + memset (&c->run_args, 0, sizeof (memif_conn_run_args_t)); + memif_msg_queue_free (&c->msg_queue); - if (lm->disconn_slaves == 0) + if (!(c->args.is_master)) { - if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + if (lm->disconn_slaves == 0) { - err = memif_syscall_error_handler (errno); - DBG_UNIX ("timerfd_settime: arm"); + if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + { + err = memif_syscall_error_handler (errno); + DBG_UNIX ("timerfd_settime: arm"); + } } + lm->disconn_slaves++; } - lm->disconn_slaves++; - return err; } @@ -1073,7 +1074,7 @@ memif_connect1 (memif_connection_t *c) } } - num = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + num = (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]; @@ -1087,7 +1088,7 @@ memif_connect1 (memif_connection_t *c) } } } - num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + num = (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]; @@ -1122,13 +1123,13 @@ memif_init_regions_and_queues (memif_connection_t *conn) return memif_syscall_error_handler (errno); r = conn->regions; - buffer_offset = (conn->args.num_s2m_rings + conn->args.num_m2s_rings) * + buffer_offset = (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) + - sizeof (memif_desc_t) * (1 << conn->args.log2_ring_size)); + sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size)); r->region_size = buffer_offset + - conn->args.buffer_size * (1 << conn->args.log2_ring_size) * - (conn->args.num_s2m_rings + conn->args.num_m2s_rings); + conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) * + (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings); if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); @@ -1143,44 +1144,44 @@ memif_init_regions_and_queues (memif_connection_t *conn) MAP_SHARED, r->fd, 0)) == MAP_FAILED) return memif_syscall_error_handler (errno); - for (i = 0; i < conn->args.num_s2m_rings; i++) + for (i = 0; i < conn->run_args.num_s2m_rings; i++) { ring = memif_get_ring (conn, MEMIF_RING_S2M, i); DBG ("RING: %p I: %d", ring, i); ring->head = ring->tail = 0; ring->cookie = MEMIF_COOKIE; ring->flags = 0; - for (j = 0; j < (1 << conn->args.log2_ring_size); j++) + for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) { - uint16_t slot = i * (1 << conn->args.log2_ring_size) + j; + uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j; ring->desc[j].region = 0; ring->desc[j].offset = buffer_offset + - (uint32_t) (slot * conn->args.buffer_size); - ring->desc[j].buffer_length = conn->args.buffer_size; + (uint32_t) (slot * conn->run_args.buffer_size); + ring->desc[j].buffer_length = conn->run_args.buffer_size; } } - for (i = 0; i < conn->args.num_m2s_rings; i++) + for (i = 0; i < conn->run_args.num_m2s_rings; i++) { ring = memif_get_ring (conn, MEMIF_RING_M2S, i); DBG ("RING: %p I: %d", ring, i); ring->head = ring->tail = 0; ring->cookie = MEMIF_COOKIE; ring->flags = 0; - for (j = 0; j < (1 << conn->args.log2_ring_size); j++) + for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) { - uint16_t slot = (i + conn->args.num_s2m_rings) * (1 << conn->args.log2_ring_size) + j; + uint16_t slot = (i + conn->run_args.num_s2m_rings) * (1 << conn->run_args.log2_ring_size) + j; ring->desc[j].region = 0; ring->desc[j].offset = buffer_offset + - (uint32_t) (slot * conn->args.buffer_size); - ring->desc[j].buffer_length = conn->args.buffer_size; + (uint32_t) (slot * conn->run_args.buffer_size); + ring->desc[j].buffer_length = conn->run_args.buffer_size; } } memif_queue_t *mq; - mq = (memif_queue_t *) malloc (sizeof (memif_queue_t) * conn->args.num_s2m_rings); + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t) * conn->run_args.num_s2m_rings); if (mq == NULL) return memif_syscall_error_handler (errno); int x; - for (x = 0; x < conn->args.num_s2m_rings; x++) + for (x = 0; x < conn->run_args.num_s2m_rings; x++) { if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); @@ -1191,7 +1192,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x); DBG ("RING: %p I: %d", mq[x].ring, x); - mq[x].log2_ring_size = conn->args.log2_ring_size; + mq[x].log2_ring_size = conn->run_args.log2_ring_size; mq[x].region = 0; mq[x].offset = (void *) mq[x].ring - (void *) conn->regions[mq->region].shm; mq[x].last_head = 0; @@ -1199,10 +1200,10 @@ memif_init_regions_and_queues (memif_connection_t *conn) } conn->tx_queues = mq; - mq = (memif_queue_t *) malloc (sizeof (memif_queue_t) * conn->args.num_m2s_rings); + mq = (memif_queue_t *) malloc (sizeof (memif_queue_t) * conn->run_args.num_m2s_rings); if (mq == NULL) return memif_syscall_error_handler (errno); - for (x = 0; x < conn->args.num_m2s_rings; x++) + for (x = 0; x < conn->run_args.num_m2s_rings; x++) { if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); @@ -1213,7 +1214,7 @@ memif_init_regions_and_queues (memif_connection_t *conn) mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x); DBG ("RING: %p I: %d", mq[x].ring, x); - mq[x].log2_ring_size = conn->args.log2_ring_size; + mq[x].log2_ring_size = conn->run_args.log2_ring_size; mq[x].region = 0; mq[x].offset = (void *) mq[x].ring - (void *) conn->regions[mq->region].shm; mq[x].last_head = 0; @@ -1233,7 +1234,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - uint8_t num = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + uint8_t num = (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]; @@ -1316,7 +1317,7 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - uint8_t num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + uint8_t num = (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; @@ -1368,7 +1369,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - uint8_t num = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + uint8_t num = (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]; @@ -1444,7 +1445,7 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - uint8_t num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + uint8_t num = (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]; @@ -1532,7 +1533,7 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t *md, if (c == NULL) return MEMIF_ERR_NOCONN; - int err = MEMIF_ERR_SUCCESS; + int err = MEMIF_ERR_SUCCESS, i; ssize_t l0, l1, total_l; l0 = 0; @@ -1601,10 +1602,42 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t *md, else err = MEMIF_ERR_NOBUF_DET; - md->ring_size = (1 << c->args.log2_ring_size); - md->buffer_size = c->args.buffer_size; - md->rx_queues = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; - md->tx_queues = (c->args.is_master) ? c->args.num_m2s_rings : c->args.num_s2m_rings; + md->rx_queues_num = (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) + { + md->rx_queues = (memif_queue_details_t *) buf + l0; + l0 = l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + for (i = 0; i < md->rx_queues_num; i++) + { + md->rx_queues[i].qid = i; + md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size); + md->rx_queues[i].buffer_size = c->run_args.buffer_size; + } + + md->tx_queues_num = (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) + { + md->tx_queues = (memif_queue_details_t *) buf + l0; + l0 = l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; + + for (i = 0; i < md->tx_queues_num; i++) + { + md->tx_queues[i].qid = i; + md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size); + md->tx_queues[i].buffer_size = c->run_args.buffer_size; + } + md->link_up_down = (c->fd > 0) ? 1 : 0; return err; /* 0 */ @@ -1619,7 +1652,7 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) return MEMIF_ERR_NOCONN; if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; - uint8_t num = (c->args.is_master) ? c->args.num_s2m_rings : c->args.num_m2s_rings; + uint8_t num = (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/src/memif_private.h b/src/memif_private.h index d1ff60f..919904a 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -115,10 +115,19 @@ typedef struct memif_connection memif_connection_t; /* functions called by memif_control_fd_handler */ typedef int (memif_fn) (memif_connection_t *conn); +typedef struct +{ + uint8_t num_s2m_rings; + uint8_t num_m2s_rings; + uint16_t buffer_size; + memif_log2_ring_size_t log2_ring_size; +} memif_conn_run_args_t; + typedef struct memif_connection { uint16_t index; memif_conn_args_t args; + memif_conn_run_args_t run_args; int fd; int listener_fd; @@ -176,6 +185,7 @@ typedef struct int timerfd; struct itimerspec arm, disarm; uint16_t disconn_slaves; + uint8_t *app_name; /* master implementation... */ memif_socket_t ms; diff --git a/src/socket.c b/src/socket.c index 4063f87..1da670d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -103,6 +103,7 @@ memif_msg_enq_ack (memif_connection_t *c) static_fn int memif_msg_send_hello (int fd) { + libmemif_main_t *lm = &libmemif_main; memif_msg_t msg = { 0 }; memif_msg_hello_t *h = &msg.hello; msg.type = MEMIF_MSG_TYPE_HELLO; @@ -113,8 +114,8 @@ memif_msg_send_hello (int fd) h->max_region = MEMIF_MAX_REGION; h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE; - strncpy ((char *) h->name, APP_NAME, - strlen (APP_NAME)); + strncpy ((char *) h->name, lm->app_name, + strlen (lm->app_name)); /* msg hello is not enqueued but sent directly, because it is the first msg to be sent */ @@ -348,13 +349,13 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) /* use nested struct c->run containing following variables? (this would be used to adjust shared memory information while keeping configured values intact) */ - DBG ("%u", h->max_s2m_ring); - c->args.num_s2m_rings = memif_min (h->max_s2m_ring + 1, + c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1, c->args.num_s2m_rings); - c->args.num_m2s_rings = memif_min (h->max_m2s_ring + 1, + c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1, c->args.num_m2s_rings); - c->args.log2_ring_size = memif_min (h->max_log2_ring_size, + c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size, c->args.log2_ring_size); + c->run_args.buffer_size = c->args.buffer_size; strncpy ((char *) c->remote_name, (char *) h->name, strlen ((char *) h->name)); return MEMIF_ERR_SUCCESS; /* 0 */ @@ -523,6 +524,7 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size; c->rx_queues[ar->index].region = ar->region; c->rx_queues[ar->index].offset = ar->offset; + c->run_args.num_s2m_rings++; } else { @@ -539,6 +541,7 @@ memif_msg_receive_add_ring (memif_connection_t *c, memif_msg_t *msg, int fd) c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size; c->tx_queues[ar->index].region = ar->region; c->tx_queues[ar->index].offset = ar->offset; + c->run_args.num_m2s_rings++; } return MEMIF_ERR_SUCCESS; /* 0 */ @@ -562,7 +565,7 @@ memif_msg_receive_connect (memif_connection_t *c, memif_msg_t *msg) int i; if (c->on_interrupt != NULL) { - for (i = 0; i < c->args.num_m2s_rings; i++) + for (i = 0; i < c->run_args.num_m2s_rings; i++) { elt.key = c->rx_queues[i].int_fd; elt.data_struct = c; @@ -595,7 +598,7 @@ memif_msg_receive_connected (memif_connection_t *c, memif_msg_t *msg) int i; if (c->on_interrupt != NULL) { - for (i = 0; i < c->args.num_s2m_rings; i++) + for (i = 0; i < c->run_args.num_s2m_rings; i++) lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ); } @@ -695,12 +698,12 @@ memif_msg_receive (int ifd) return err; if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS) return err; - for (i = 0; i < c->args.num_s2m_rings; i++) + for (i = 0; i < c->run_args.num_s2m_rings; i++) { if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS) return err; } - for (i = 0; i < c->args.num_m2s_rings; i++) + for (i = 0; i < c->run_args.num_m2s_rings; i++) { if ((err = memif_msg_enq_add_ring (c, i, MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS) return err; From 70af874959ca10124867a53425c71a68a7c9de7e Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 10 Aug 2017 13:42:57 +0200 Subject: [PATCH 40/63] unit-test update --- test/main_test.c | 89 ++++++++++++++++++++++++++++++++++++---------- test/socket_test.c | 25 +++++++++---- 2 files changed, 89 insertions(+), 25 deletions(-) diff --git a/test/main_test.c b/test/main_test.c index 781a9e7..b7b8351 100644 --- a/test/main_test.c +++ b/test/main_test.c @@ -63,7 +63,7 @@ START_TEST (test_init) { int err; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); libmemif_main_t *lm = &libmemif_main; @@ -82,7 +82,7 @@ START_TEST (test_init_epoll) { int err; - if ((err = memif_init (NULL)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (NULL, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); libmemif_main_t *lm = &libmemif_main; @@ -107,7 +107,7 @@ START_TEST (test_create) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -170,7 +170,7 @@ START_TEST (test_create_master) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -230,7 +230,7 @@ START_TEST (test_create_mult) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -321,7 +321,7 @@ START_TEST (test_control_fd_handler) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -386,7 +386,7 @@ START_TEST (test_buffer_alloc) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -398,6 +398,11 @@ START_TEST (test_buffer_alloc) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); @@ -464,7 +469,7 @@ START_TEST (test_tx_burst) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -476,6 +481,11 @@ START_TEST (test_tx_burst) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); @@ -553,7 +563,7 @@ START_TEST (test_rx_burst) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -565,6 +575,11 @@ START_TEST (test_rx_burst) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); @@ -637,7 +652,7 @@ START_TEST (test_buffer_free) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -649,6 +664,11 @@ START_TEST (test_buffer_free) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); @@ -724,7 +744,7 @@ END_TEST START_TEST (test_get_details) { - int err; + int err, i; ready_called = 0; memif_conn_handle_t conn = NULL; memif_conn_args_t args; @@ -734,7 +754,7 @@ START_TEST (test_get_details) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -746,6 +766,14 @@ START_TEST (test_get_details) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + memif_details_t md; memset (&md, 0, sizeof (md)); ssize_t buflen = 2048; @@ -765,10 +793,18 @@ START_TEST (test_get_details) ck_assert_uint_eq (md.id, c->args.interface_id); ck_assert_uint_ne (md.role, c->args.is_master); ck_assert_uint_eq (md.mode, c->args.mode); - ck_assert_uint_eq (md.ring_size, (1 << c->args.log2_ring_size)); - ck_assert_uint_eq (md.buffer_size, c->args.buffer_size); - ck_assert_uint_eq (md.tx_queues, c->args.num_s2m_rings); - ck_assert_uint_eq (md.rx_queues, c->args.num_m2s_rings); + for (i = 0; i < md.rx_queues_num; i++) + { + ck_assert_uint_eq (md.rx_queues[i].qid, i); + ck_assert_uint_eq (md.rx_queues[i].ring_size, (1 << c->args.log2_ring_size)); + ck_assert_uint_eq (md.rx_queues[i].buffer_size, c->args.buffer_size); + } + for (i = 0; i < md.tx_queues_num; i++) + { + ck_assert_uint_eq (md.tx_queues[i].qid, i); + ck_assert_uint_eq (md.tx_queues[i].ring_size, (1 << c->args.log2_ring_size)); + ck_assert_uint_eq (md.tx_queues[i].buffer_size, c->args.buffer_size); + } ck_assert_uint_eq (md.link_up_down, 0); if (lm->timerfd > 0) @@ -792,7 +828,7 @@ START_TEST (test_init_regions_and_queues) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -804,6 +840,11 @@ START_TEST (test_init_regions_and_queues) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); @@ -840,7 +881,7 @@ START_TEST (test_connect1) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -852,6 +893,11 @@ START_TEST (test_connect1) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); @@ -879,7 +925,7 @@ START_TEST (test_disconnect_internal) libmemif_main_t *lm = &libmemif_main; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); strncpy ((char *) args.interface_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); @@ -891,6 +937,11 @@ START_TEST (test_disconnect_internal) memif_connection_t *c = (memif_connection_t *) conn; + c->run_args.num_s2m_rings = 2; + c->run_args.num_m2s_rings = 2; + c->run_args.log2_ring_size = 10; + c->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); diff --git a/test/socket_test.c b/test/socket_test.c index 631970e..d7f19ef 100644 --- a/test/socket_test.c +++ b/test/socket_test.c @@ -260,6 +260,9 @@ START_TEST (test_send_hello) memset (conn.args.instance_name, 0, sizeof (conn.args.instance_name)); strncpy ((char *) conn.args.instance_name, TEST_APP_NAME, strlen (TEST_APP_NAME)); + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) + ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); + if ((err = memif_msg_send_hello (conn.fd)) != MEMIF_ERR_SUCCESS) ck_assert_msg (err == MEMIF_ERR_BAD_FD, "err code: %u, err msg: %s", err, memif_strerror (err)); @@ -305,9 +308,9 @@ START_TEST (test_recv_hello) if ((err = memif_msg_receive_hello (&conn, &msg)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); - ck_assert_uint_eq (conn.args.num_s2m_rings, 2); - ck_assert_uint_eq (conn.args.num_m2s_rings, 2); - ck_assert_uint_eq (conn.args.log2_ring_size, 10); + ck_assert_uint_eq (conn.run_args.num_s2m_rings, 2); + ck_assert_uint_eq (conn.run_args.num_m2s_rings, 2); + ck_assert_uint_eq (conn.run_args.log2_ring_size, 10); ck_assert_str_eq (conn.remote_name, TEST_IF_NAME); h->max_version = 9; @@ -351,7 +354,7 @@ START_TEST (test_recv_init) elt.data_struct = &conn; add_list_elt (&elt, &ms.interface_list, &ms.interface_list_len); - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) @@ -462,7 +465,7 @@ START_TEST (test_recv_connect) args.is_master = 0; args.mode = 0; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); if ((err = memif_create (&c, &args, on_connect, @@ -471,6 +474,11 @@ START_TEST (test_recv_connect) memif_connection_t *conn = (memif_connection_t *) c; + conn->run_args.num_s2m_rings = 1; + conn->run_args.num_m2s_rings = 1; + conn->run_args.log2_ring_size = 10; + conn->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); @@ -499,7 +507,7 @@ START_TEST (test_recv_connected) args.is_master = 0; args.mode = 0; - if ((err = memif_init (control_fd_update)) != MEMIF_ERR_SUCCESS) + if ((err = memif_init (control_fd_update, TEST_APP_NAME)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); if ((err = memif_create (&c, &args, on_connect, @@ -508,6 +516,11 @@ START_TEST (test_recv_connected) memif_connection_t *conn = (memif_connection_t *) c; + conn->run_args.num_s2m_rings = 1; + conn->run_args.num_m2s_rings = 1; + conn->run_args.log2_ring_size = 10; + conn->run_args.buffer_size = 2048; + if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); From a7040a06e1dd294c891e25639c834c7e6aaa62bc Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Fri, 11 Aug 2017 12:17:10 +0200 Subject: [PATCH 41/63] ICMP-responder help update --- examples/icmp_responder/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index cddfe23..f679dcb 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -482,7 +482,7 @@ print_help () printf ("commands:\n"); printf ("\thelp - prints this help\n"); printf ("\texit - exit app\n"); - printf ("\tconn - create memif (slave-mode)\n"); + printf ("\tconn - create memif. index is also used as interface id, mode 0 = slave 1 = master\n"); printf ("\tdel - delete memif\n"); printf ("\tshow - show connection details\n"); printf ("\tip-set - set interface ip address\n"); From fddcc22ec98c779b522f4cb4539f2a53874e77b0 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 14 Aug 2017 10:17:39 +0200 Subject: [PATCH 42/63] dockerfile + doc update --- README.md | 310 ++++++++++++++++++++++++++++++++------------------ UserManual.md | 169 +++++++++++++++++++++++++++ dockerfile | 32 ++++++ 3 files changed, 399 insertions(+), 112 deletions(-) create mode 100644 UserManual.md create mode 100644 dockerfile diff --git a/README.md b/README.md index 95da2db..8c8fd33 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Shared Memory Packet Interface (memif) Library Shared memory packet interface (memif) provides high performance packet transmit and receive between user application and Vector Packet Processing (VPP). -## Work in progress +## Features - [x] Slave mode - [x] Connect to VPP over memif @@ -17,141 +17,150 @@ Shared memory packet interface (memif) provides high performance packet transmit - [x] Multiple queues - [x] Multi-thread support - [x] Master mode - - [ ] Multiple regions -- [ ] Performance testing -- [ ] Documentation + - [ ] Multiple regions (TODO) +- [ ] Performance testing (TODO) +- [ ] Documentation (TODO) +## Quickstart -## Getting started +#### Run in container -#### Installation +Install [docker](https://docs.docker.com/engine/installation) engine. +Useful link: [Docker documentation](https://docs.docker.com/get-started). -Clone repository to your local machine. From root directory execute: -For debug build: +Use [example dockerfile](dockerfile). This dockerfile builds and installs libmemif in debug mode and runs ICMP responder example app. Run following command in directory with example dockerfile: ``` -# ./bootstrap -# ./configure -# make -# make install +# docker build . ``` - -For release build: +When build is completed: ``` -# ./bootstrap -# ./configure -# make release -# make install +# docker images +REPOSITORY TAG IMAGE ID CREATED SIZE + c83438f19940 3 seconds ago 468MB +ubuntu xenial 0ef2e08ed3fa 5 months ago 130MB ``` -Verify installation: +Tag your new image: ``` -# ./.libs/icmp_responder +# docker tag c83438f19940 icmp-responder:latest +# docker images +REPOSITORY TAG IMAGE ID CREATED SIZE +icmp-responder latest c83438f19940 3 seconds ago 468MB +ubuntu xenial 0ef2e08ed3fa 5 months ago 130MB ``` -> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies successful build. Default install path is /usr/lib. -Use _help_ command to display build information and commands: +Run container: ``` +docker run -it --rm --name icmp-responder --hostname icmp-responder --privileged -v "/run/vpp/:/run/vpp/" icmp-responder +``` +Example application will start in debug mode. Output should look like this: +``` +ICMP_Responder:add_epoll_fd:204: fd 0 added to epoll +MEMIF_DEBUG:src/main.c:memif_init:383: app name: ICMP_Responder +ICMP_Responder:add_epoll_fd:204: fd 4 added to epoll LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) ============================== libmemif version: 1.0 (debug) memif version: 256 commands: - help - prints this help - exit - exit app - conn - create memif (slave-mode) - del - delete memif - show - show connection details -``` - -#### Unit tests - -Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be installed in order to build *unit\_test* binary. -Ubuntu/Debian: -``` -sudo apt-get install check + help - prints this help + exit - exit app + conn - create memif (slave-mode) + del - delete memif + show - show connection details + ip-set - set interface ip address + rx-mode - set queue rx mode ``` -[More platforms](https://libcheck.github.io/check/web/install.html) - -#### Connecting to VPP - -For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) - -1. Initialize memif - - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t - - Call memif initialization function. memif\_init - -> If event occurres on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. -> If callback function parameter for memif\_init function is set to NULL, libmemif will handle file descriptor event polling. - Api call memif\_poll\_event will call epoll\_pwait wit user defined timeout to poll event on file descriptors opened by libmemif. - -> Memif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. - -2. Creating interface - - Declare memif connction handle. memif\_conn\_handle\_t - - Specify connection arguments. memif\_conn\_args\_t - - Declare callback functions called on connected/disconnected/interrupted status changed. memif\_connection\_update\_t - - Call memif interface create function. memif\_create -> If connection is in slave mode, arms timer file descriptor. -> If on interrupt callback is set to NULL, user will not be notified about interrupt. Use memif\_get\_queue\_efd call to get interrupt file descrip[tor for specific queue. - -3. Connection establishment - - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback.. - - On event call memif\_control\_fd\_handler. - - Everything else regarding connection establishment will be done internally. - - Once connection has been established, a callback will inform the user about connection status change. - -4. Interrupt packet receive - - If event is polled on interrupt file descriptor, libmemif will call memif\_interrupt\_t callback specified for every connection instance. -6. Memif buffers - - Packet data are stored in memif\_buffer\_t. Pointer _data_ points to shared memory buffer, and unsigned integer *data\_len* contains packet data length. +#### Run without container -5. Packet receive - - Api call memif\_rx\_burst will set all required fields in memif buffers provided by user application. - - User application can then process packets. - - Api call memif\_buffer\_free will make supplied memif buffers ready for next receive and mark shared memory buffers as free. +Build process is explained in [User Manual](UserManual.md). -6. Packet transmit - - Api call memif\_buffer\_alloc will set all required fields in memif buffers provided by user application. - - User application can populate shared memory buffers with packets. - - Api call memif\_tx\_burst will inform peer interface (master memif on VPP) that there are packets ready to receive and mark memif buffers as free. +#### Connection to VPP-memif -7. Helper functions - - Memif details - - Api call memif\_get\_details will return details about connection. - - Memif error messages - - Every api call returns error code (integer value) mapped to error string. - - Call memif\_strerror will return error message assigned to specific error code. - - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. +> Libmemif example app(s) use memif default socket file: /run/vpp/memif.sock. -#### Example app: +###### Example setup (VPP-memif master icmp_responder slave) -- [ICMP Responder](examples/icmp_responder/main.c) - -#### Example app (libmemif fd event polling): - -- [ICMP Responder](examples/icmp_responder2/main.c) -> Optional argument: transmit queue id. +Run VPP and icmp_responder example. +VPP-side config: ``` -icmpr_lep 1 +# create memif id 0 master +# set int state memif0/0 up +# set int ip address memif0/0 192.168.1.1/24 ``` -> Set transmit queue id to 1. Default is 0. -> Application will create memif interface in slave mode and try to connect to VPP. Exit using Ctrl+C. Application will handle SIGINT signal, free allocated memory and exit with EXIT_SUCCESS. - -#### Example app (multi-thread queue polling) +icmp_responder: +``` +# conn 0 0 +``` +Memif in slave mode will try to connect every 2 seconds. If connection establishment is successfull, a message will show. +``` +INFO: memif connected! +``` +> Error messages like "unmatched interface id" are printed only in debug mode. -- [ICMP Responder](examples/icmp_responder3/main.c) +Check connected status. +Use show command in icmp_responder +``` +show +MEMIF DETAILS +============================== +interface index: 0 + interface ip: 192.168.1.2 + interface name: memif_connection + app name: ICMP_Responder + remote interface name: memif0/0 + remote app name: VPP 17.10-rc0~132-g62f9cdd + id: 0 + secret: + role: slave + mode: ethernet + socket filename: /run/vpp/memif.sock + rx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + tx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + link: up +interface index: 1 + no connection -> Simple example of libmemif multi-thread usage. Connection establishment is handled by main thread. There are two rx queues in this example. One in polling mode and second in interrupt mode. +``` +Use sh memif command in VPP: +``` +DBGvpp# sh memif +interface memif0/0 + remote-name "ICMP_Responder" + remote-interface "memif_connection" + id 0 mode ethernet file /run/vpp/memif.sock + flags admin-up connected + listener-fd 12 conn-fd 13 + num-s2m-rings 1 num-m2s-rings 1 buffer-size 0 + master-to-slave ring 0: + region 0 offset 32896 ring-size 1024 int-fd 16 + head 0 tail 0 flags 0x0000 interrupts 0 + master-to-slave ring 0: + region 0 offset 0 ring-size 1024 int-fd 15 + head 0 tail 0 flags 0x0001 interrupts 0 +``` -VPP config: +Send ping from VPP to icmp_responder: ``` -# create memif id 0 master -# set int state memif0 up -# set int ip address memif0 192.168.1.1/24 -# ping 192.168.1.2 +DBGvpp# ping 192.168.1.2 +64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=.1888 ms +64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=.1985 ms +64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=.1813 ms +64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=.1929 ms + +Statistics: 5 sent, 4 received, 20% packet loss ``` -For multiple rings (queues) support run VPP with worker threads: -example startup.conf: +###### Example setup multiple queues (VPP-memif slave icmp_responder master) + +Run icmp_responder as in previous example setup. +Run VPP with startup conf, enabling 2 worker threads. +Example startup.conf: ``` unix { interactive @@ -163,12 +172,89 @@ cpu { workers 2 } ``` -VPP config: +VPP-side config: +``` +# create memif id 0 slave rx-queues 2 tx-queues 2 +# set int state memif0/0 up +# set int ip address memif0/0 192.168.1.1/24 +``` +icmp_responder: +``` +# conn 0 1 +``` +When connection is established a message will print: +``` +INFO: memif connected! ``` -# create memif id 0 master rx-queues 2 tx-queues 2 -# set int state memif0 up -# set int ip address memif0 192.168.1.1/24 -# ping 192.168.1.2 +> Error messages like "unmatched interface id" are printed only in debug mode. + +Check connected status. +Use show command in icmp_responder +``` +show +MEMIF DETAILS +============================== +interface index: 0 + interface ip: 192.168.1.2 + interface name: memif_connection + app name: ICMP_Responder + remote interface name: memif0/0 + remote app name: VPP 17.10-rc0~132-g62f9cdd + id: 0 + secret: + role: master + mode: ethernet + socket filename: /run/vpp/memif.sock + rx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + queue id: 1 + ring size: 1024 + buffer size: 2048 + tx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + queue id: 1 + ring size: 1024 + buffer size: 2048 + link: up +interface index: 1 + no connection + +``` +Use sh memif command in VPP: +``` +DBGvpp# sh memif +interface memif0/0 + remote-name "ICMP_Responder" + remote-interface "memif_connection" + id 0 mode ethernet file /run/vpp/memif.sock + flags admin-up slave connected + listener-fd -1 conn-fd 12 + num-s2m-rings 2 num-m2s-rings 2 buffer-size 2048 + slave-to-master ring 0: + region 0 offset 0 ring-size 1024 int-fd 14 + head 0 tail 0 flags 0x0000 interrupts 0 + slave-to-master ring 1: + region 0 offset 32896 ring-size 1024 int-fd 15 + head 0 tail 0 flags 0x0000 interrupts 0 + slave-to-master ring 0: + region 0 offset 65792 ring-size 1024 int-fd 16 + head 0 tail 0 flags 0x0001 interrupts 0 + slave-to-master ring 1: + region 0 offset 98688 ring-size 1024 int-fd 17 + head 0 tail 0 flags 0x0001 interrupts 0 + +``` +Send ping from VPP to icmp_responder: +``` +DBGvpp# ping 192.168.1.2 +64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=.1439 ms +64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=.2184 ms +64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=.1458 ms +64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=.1687 ms + +Statistics: 5 sent, 4 received, 20% packet loss ``` -> Example applications use VPP default socket file for memif: /run/vpp/memif.sock -> For master mode, socket directory must exist prior to memif\_create call. diff --git a/UserManual.md b/UserManual.md new file mode 100644 index 0000000..3735f30 --- /dev/null +++ b/UserManual.md @@ -0,0 +1,169 @@ +## Getting started + +#### Build + +Install dependencies +``` +# sudo apt-get install -y git autoconf pkg_config libtool check +``` + +Clone repository to your local machine. +``` +# git clone https://github.com/JakubGrajciar/libmemif.git +``` + +From root directory execute: +For debug build: +``` +# ./bootstrap +# ./configure +# make +# make install +``` + +For release build: +``` +# ./bootstrap +# ./configure +# make release +# make install +``` +Verify installation: +``` +# ./.libs/icmp_responder +``` +> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies successful build. Default install path is /usr/lib. +Use _help_ command to display build information and commands: +``` +ICMP_Responder:add_epoll_fd:204: fd 0 added to epoll +MEMIF_DEBUG:src/main.c:memif_init:383: app name: ICMP_Responder +ICMP_Responder:add_epoll_fd:204: fd 4 added to epoll +LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) +============================== +libmemif version: 1.0 (debug) +memif version: 256 +commands: + help - prints this help + exit - exit app + conn - create memif (slave-mode) + del - delete memif + show - show connection details + ip-set - set interface ip address + rx-mode - set queue rx mode +``` + +#### Unit tests + +Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be installed in order to build *unit\_test* binary. +Ubuntu/Debian: +``` +sudo apt-get install check +``` +[More platforms](https://libcheck.github.io/check/web/install.html) + +#### Concept (Connecting to VPP) + +For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) + +1. Initialize memif + - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t + - Call memif initialization function. memif\_init + +> If event occurres on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. +> If callback function parameter for memif\_init function is set to NULL, libmemif will handle file descriptor event polling. + Api call memif\_poll\_event will call epoll\_pwait wit user defined timeout to poll event on file descriptors opened by libmemif. + +> Memif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. + +2. Creating interface + - Declare memif connction handle. memif\_conn\_handle\_t + - Specify connection arguments. memif\_conn\_args\_t + - Declare callback functions called on connected/disconnected/interrupted status changed. memif\_connection\_update\_t + - Call memif interface create function. memif\_create +> If connection is in slave mode, arms timer file descriptor. +> If on interrupt callback is set to NULL, user will not be notified about interrupt. Use memif\_get\_queue\_efd call to get interrupt file descrip[tor for specific queue. + +3. Connection establishment + - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback.. + - On event call memif\_control\_fd\_handler. + - Everything else regarding connection establishment will be done internally. + - Once connection has been established, a callback will inform the user about connection status change. + +4. Interrupt packet receive + - If event is polled on interrupt file descriptor, libmemif will call memif\_interrupt\_t callback specified for every connection instance. + +6. Memif buffers + - Packet data are stored in memif\_buffer\_t. Pointer _data_ points to shared memory buffer, and unsigned integer *data\_len* contains packet data length. + +5. Packet receive + - Api call memif\_rx\_burst will set all required fields in memif buffers provided by user application. + - User application can then process packets. + - Api call memif\_buffer\_free will make supplied memif buffers ready for next receive and mark shared memory buffers as free. + +6. Packet transmit + - Api call memif\_buffer\_alloc will set all required fields in memif buffers provided by user application. + - User application can populate shared memory buffers with packets. + - Api call memif\_tx\_burst will inform peer interface (master memif on VPP) that there are packets ready to receive and mark memif buffers as free. + +7. Helper functions + - Memif details + - Api call memif\_get\_details will return details about connection. + - Memif error messages + - Every api call returns error code (integer value) mapped to error string. + - Call memif\_strerror will return error message assigned to specific error code. + - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. + +#### Example app: + +- [ICMP Responder](examples/icmp_responder/main.c) + +#### Example app (libmemif fd event polling): + +- [ICMP Responder](examples/icmp_responder2/main.c) +> Optional argument: transmit queue id. +``` +icmpr_lep 1 +``` +> Set transmit queue id to 1. Default is 0. +> Application will create memif interface in slave mode and try to connect to VPP. Exit using Ctrl+C. Application will handle SIGINT signal, free allocated memory and exit with EXIT_SUCCESS. + +#### Example app (multi-thread queue polling) + +- [ICMP Responder](examples/icmp_responder3/main.c) + +> Simple example of libmemif multi-thread usage. Connection establishment is handled by main thread. There are two rx queues in this example. One in polling mode and second in interrupt mode. + +VPP config: +``` +# create memif id 0 master +# set int state memif0 up +# set int ip address memif0 192.168.1.1/24 +# ping 192.168.1.2 +``` +For multiple rings (queues) support run VPP with worker threads: +example startup.conf: +``` +unix { + interactive + nodaemon + full-coredump +} + +cpu { + workers 2 +} +``` +VPP config: +``` +# create memif id 0 master +# set int state memif0 up +# set int ip address memif0 192.168.1.1/24 +# ping 192.168.1.2 +``` +> Master mode queue number is limited by worker threads. Slave mode interface needs to specify number of queues. +``` +# create memif id 0 slave rx-queues 2 tx-queues 2 +``` +> Example applications use VPP default socket file for memif: /run/vpp/memif.sock +> For master mode, socket directory must exist prior to memif\_create call. + diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..51151bd --- /dev/null +++ b/dockerfile @@ -0,0 +1,32 @@ +# 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. + +FROM ubuntu:xenial + +RUN apt-get update && \ + apt-get install -y git build-essential autoconf pkg-config libtool sudo check +RUN rm -rf /var/lib/apt/lists/* + +RUN git clone https://github.com/JakubGrajciar/libmemif.git /libmemif +WORKDIR /libmemif +RUN git checkout dev +RUN ./bootstrap +RUN ./configure +RUN make +RUN make install + +RUN mkdir /var/vpp + +RUN ulimit -c unlimited + +CMD ./.libs/icmp_responder From 7c0c148231fbe7577fa2f1ed4416adebe91ba852 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 14 Aug 2017 13:30:19 +0200 Subject: [PATCH 43/63] memleaks fix + memif_cleanup () --- examples/icmp_responder/main.c | 34 ++++++++++---- examples/icmp_responder2/main.c | 5 +++ examples/icmp_responder3/main.c | 21 ++++++--- src/libmemif.h | 6 +++ src/main.c | 80 ++++++++++++++++++++++++++++----- src/socket.c | 5 +-- 6 files changed, 122 insertions(+), 29 deletions(-) diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index f679dcb..21daa70 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -68,7 +68,7 @@ /* maximum tx/rx memif buffers */ #define MAX_MEMIF_BUFS 256 -#define MAX_CONNS 2 +#define MAX_CONNS 50 int epfd; @@ -92,6 +92,7 @@ typedef struct } memif_connection_t; memif_connection_t memif_connection[MAX_CONNS]; +long ctx[MAX_CONNS]; /* print details for all memif connections */ static void @@ -417,10 +418,8 @@ icmpr_memif_create (long index, long mode) 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 */ - long *ctx = malloc (sizeof (long)); - *ctx = index; int err = memif_create (&c->conn, - &args, on_connect, on_disconnect, on_interrupt, ctx); + &args, on_connect, on_disconnect, on_interrupt, &ctx[index]); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_create: %s", memif_strerror (err)); @@ -456,11 +455,22 @@ icmpr_memif_delete (long index) } memif_connection_t *c = &memif_connection[index]; + if (c->rx_bufs) + free (c->rx_bufs); + c->rx_bufs = NULL; + c->rx_buf_num = 0; + if (c->tx_bufs) + free (c->tx_bufs); + c->tx_bufs = NULL; + c->tx_buf_num = 0; + int err; /* disconenct then delete memif connection */ err = memif_delete (&c->conn); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_delete: %s", memif_strerror (err)); + if (c->conn != NULL) + INFO ("memif delete fail"); return 0; } @@ -492,17 +502,18 @@ int icmpr_free () { /* application cleanup */ + int err; long i; for (i = 0; i < MAX_CONNS; i++) { memif_connection_t *c = &memif_connection[i]; icmpr_memif_delete (i); - free (c->tx_bufs); - c->tx_bufs = NULL; - free (c->rx_bufs); - c->rx_bufs = NULL; } + err = memif_cleanup (); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); + return 0; } @@ -752,7 +763,7 @@ int main () add_epoll_fd (0, EPOLLIN); /* initialize memory interface */ - int err; + 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 */ @@ -760,6 +771,11 @@ int main () if (err != MEMIF_ERR_SUCCESS) INFO ("memif_init: %s", memif_strerror (err)); + for (i = 0; i < MAX_CONNS; i++) + { + ctx[i] = i; + } + print_help (); /* main loop */ diff --git a/examples/icmp_responder2/main.c b/examples/icmp_responder2/main.c index 7ef0ae8..f62af55 100644 --- a/examples/icmp_responder2/main.c +++ b/examples/icmp_responder2/main.c @@ -256,12 +256,17 @@ int icmpr_free () { /* application cleanup */ + int err; memif_connection_t *c = &memif_connection; free (c->tx_bufs); c->tx_bufs = NULL; free (c->rx_bufs); c->rx_bufs = NULL; + err = memif_cleanup (); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); + return 0; } diff --git a/examples/icmp_responder3/main.c b/examples/icmp_responder3/main.c index 52d0fce..8f56f3c 100644 --- a/examples/icmp_responder3/main.c +++ b/examples/icmp_responder3/main.c @@ -101,6 +101,7 @@ typedef struct } 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]; @@ -269,7 +270,7 @@ 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, tx, fb; + 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); @@ -364,7 +365,7 @@ 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, tx, fb; + uint16_t rx = 0, tx = 0, fb = 0; struct epoll_event evt, *e; int en = 0; uint32_t events = 0; @@ -584,10 +585,8 @@ icmpr_memif_create (long index) 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 */ - long *ctx = malloc (sizeof (long)); - *ctx = index; int err = memif_create (&c->conn, - &args, on_connect, on_disconnect, NULL, ctx); + &args, on_connect, on_disconnect, NULL, &ctx[index]); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_create: %s", memif_strerror (err)); @@ -654,6 +653,7 @@ int icmpr_free () { /* application cleanup */ + int err; long i; for (i = 0; i < MAX_CONNS; i++) { @@ -661,7 +661,9 @@ icmpr_free () icmpr_memif_delete (i); } - pthread_exit (NULL); + err = memif_cleanup (); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); return 0; } @@ -852,7 +854,7 @@ int main () add_epoll_fd (main_epfd, 0, EPOLLIN); /* initialize memory interface */ - int err; + 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 */ @@ -860,6 +862,11 @@ int main () if (err != MEMIF_ERR_SUCCESS) INFO ("memif_init: %s", memif_strerror (err)); + for (i = 0; i < MAX_CONNS; i++) + { + ctx[i] = i; + } + print_help (); /* main loop */ diff --git a/src/libmemif.h b/src/libmemif.h index ef85758..3242c24 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -287,6 +287,12 @@ int memif_get_details (memif_conn_handle_t conn, memif_details_t *md, */ int memif_init (memif_control_fd_update_t *on_control_fd_update, char *app_name); +/** \brief Memif cleanup + + free libmemif internal allocations +*/ +int memif_cleanup (); + /** \brief Memory interface create function @param conn - connection handle for user app @param args - memory interface connection arguments diff --git a/src/main.c b/src/main.c index 2a2e5a7..53d8831 100644 --- a/src/main.c +++ b/src/main.c @@ -313,6 +313,12 @@ add_list_elt (memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len) if (tmp == NULL) return -1; + for (i = *len; i < *len * 2; i++) + { + tmp[i].key = -1; + tmp[i].data_struct = NULL; + } + tmp[*len].key = e->key; tmp[*len].data_struct = e->data_struct; i = *len; @@ -325,6 +331,11 @@ add_list_elt (memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len) int get_list_elt (memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key) { + if (key == -1) + { + *e = NULL; + return -1; + } int i; for (i = 0; i < len; i++) { @@ -356,6 +367,25 @@ free_list_elt (memif_list_elt_t *list, uint16_t len, int key) return -1; } +int +free_list_elt_ctx (memif_list_elt_t *list, uint16_t len, memif_connection_t *ctx) +{ + int i; + for (i = 0; i < len; i++) + { + if (list[i].key == -1) + { + if (list[i].data_struct == ctx) + { + list[i].data_struct = NULL; + return 0; + } + } + } + + return -1; +} + static void memif_control_fd_update_register (memif_control_fd_update_t *cb) { @@ -370,18 +400,18 @@ int memif_init (memif_control_fd_update_t *on_control_fd_update, char *app_name) if (app_name) { - lm->app_name = malloc (strlen (app_name)); + lm->app_name = malloc (strlen (app_name) + sizeof (char)); + memset (lm->app_name, 0, strlen (app_name) + sizeof (char)); strncpy ((char *) lm->app_name, app_name, strlen (app_name)); } else { - lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME)); + lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME) + sizeof (char)); + memset (lm->app_name, 0, strlen (app_name) + sizeof (char)); strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME, strlen (MEMIF_DEFAULT_APP_NAME)); } - DBG ("app name: %s", lm->app_name); - /* register control fd update callback */ if (on_control_fd_update != NULL) memif_control_fd_update_register (on_control_fd_update); @@ -609,13 +639,17 @@ 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)); + ms->filename = malloc (strlen ( + (char *) conn->args.socket_filename) + sizeof (char)); + memset (ms->filename, 0, strlen ( + (char *) conn->args.socket_filename) + sizeof (char)); strncpy ((char *) ms->filename, (char *) conn->args.socket_filename, strlen ((char *) conn->args.socket_filename)); ms->interface_list_len = 1; ms->interface_list = malloc ( sizeof (memif_list_elt_t) * ms->interface_list_len); - DBG ("filename: %s", (char *) ms->filename); + ms->interface_list[0].key = -1; + ms->interface_list[0].data_struct = NULL; struct sockaddr_un un = { 0 }; int on = 1; @@ -917,6 +951,8 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) free_list_elt (lm->control_list, lm->control_list_len, c->fd); e->key = c->fd = -1; } + else if (is_del) + free_list_elt_ctx (lm->control_list, lm->control_list_len, c); if (c->tx_queues != NULL) { @@ -1086,6 +1122,7 @@ memif_connect1 (memif_connection_t *c) DBG ("wrong cookie on tx ring %u", i); return MEMIF_ERR_COOKIE; } + mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs = 0; } } num = (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.num_m2s_rings; @@ -1100,6 +1137,7 @@ memif_connect1 (memif_connection_t *c) DBG ("wrong cookie on rx ring %u", i); return MEMIF_ERR_COOKIE; } + mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs = 0; } } @@ -1133,10 +1171,10 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); - +/* if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) return memif_syscall_error_handler (errno); - +*/ if ((ftruncate (r->fd, r->region_size)) == -1) return memif_syscall_error_handler (errno); @@ -1296,8 +1334,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, } mq->alloc_bufs += *count_out; - - DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, mq->alloc_bufs); + DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, mq->alloc_bufs); if (count) { @@ -1660,3 +1697,26 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) return MEMIF_ERR_SUCCESS; } + +int +memif_cleanup () +{ + libmemif_main_t *lm = &libmemif_main; + if (lm->app_name) + free (lm->app_name); + lm->app_name = NULL; + if (lm->control_list) + free (lm->control_list); + lm->control_list = NULL; + if (lm->interrupt_list) + free (lm->interrupt_list); + lm->interrupt_list = NULL; + if (lm->listener_list) + free (lm->listener_list); + lm->listener_list = NULL; + if (lm->pending_list) + free (lm->pending_list); + lm->pending_list = NULL; + + return MEMIF_ERR_SUCCESS; /* 0 */ +} diff --git a/src/socket.c b/src/socket.c index 1da670d..22562e0 100644 --- a/src/socket.c +++ b/src/socket.c @@ -346,9 +346,7 @@ memif_msg_receive_hello (memif_connection_t *c, memif_msg_t *msg) DBG ("incompatible protocol version"); return MEMIF_ERR_PROTO; } - /* use nested struct c->run containing following variables? - (this would be used to adjust shared memory information while keeping - configured values intact) */ + c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1, c->args.num_s2m_rings); c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1, @@ -371,6 +369,7 @@ memif_msg_receive_init (memif_socket_t *ms, int fd, memif_msg_t *msg) memif_connection_t *c = NULL; libmemif_main_t *lm = &libmemif_main; uint8_t err_string[96]; + memset (err_string, 0, sizeof (char) * 96); int err = MEMIF_ERR_SUCCESS; /* 0 */ int err_disc; if (i->version != MEMIF_VERSION) From cc972a4f67501df34ce3f9337bc9833ee5f8cc01 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 14 Aug 2017 14:09:19 +0200 Subject: [PATCH 44/63] doc update --- README.md | 8 ++-- UserManual.md | 118 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 113 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8c8fd33..87c2cb4 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ Shared memory packet interface (memif) provides high performance packet transmit ## Quickstart +For information on how to use libmemif API, please refer to [User manual](UserManual.md). + #### Run in container Install [docker](https://docs.docker.com/engine/installation) engine. @@ -75,11 +77,11 @@ commands: Build process is explained in [User Manual](UserManual.md). -#### Connection to VPP-memif +### Connection to VPP-memif > Libmemif example app(s) use memif default socket file: /run/vpp/memif.sock. -###### Example setup (VPP-memif master icmp_responder slave) +#### Example setup (VPP-memif master icmp_responder slave) Run VPP and icmp_responder example. VPP-side config: @@ -156,7 +158,7 @@ DBGvpp# ping 192.168.1.2 Statistics: 5 sent, 4 received, 20% packet loss ``` -###### Example setup multiple queues (VPP-memif slave icmp_responder master) +#### Example setup multiple queues (VPP-memif slave icmp_responder master) Run icmp_responder as in previous example setup. Run VPP with startup conf, enabling 2 worker threads. diff --git a/UserManual.md b/UserManual.md index 3735f30..a7750b4 100644 --- a/UserManual.md +++ b/UserManual.md @@ -66,51 +66,149 @@ sudo apt-get install check For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) 1. Initialize memif - - Declare callback function handling file descriptor event polling. memif\_control\_fd\_update\_t + - Declare callback function handling file descriptor event polling. +```C +int +control_fd_update (int fd, uint8_t events) +{ +... +} +``` - Call memif initialization function. memif\_init +```C +err = memif_init (control_fd_update, APP_NAME); +``` -> If event occurres on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. +> If event occurres on any file descriptor returned by this callback, call memif\_control\_fd\_handler function. +```C +memif_err = memif_control_fd_handler (evt.data.fd, events); +``` > If callback function parameter for memif\_init function is set to NULL, libmemif will handle file descriptor event polling. - Api call memif\_poll\_event will call epoll\_pwait wit user defined timeout to poll event on file descriptors opened by libmemif. + Api call memif\_poll\_event will call epoll\_pwait with user defined timeout to poll event on file descriptors opened by libmemif. +```C +/* main loop */ + while (1) + { + if (memif_poll_event (-1) < 0) + { + DBG ("poll_event error!"); + } + } +``` > Memif initialization function will initialize internal structures and create timer file descriptor, which will be used for sending periodic connection requests. Timer is disarmed if no memif interface is created. 2. Creating interface - - Declare memif connction handle. memif\_conn\_handle\_t - - Specify connection arguments. memif\_conn\_args\_t - - Declare callback functions called on connected/disconnected/interrupted status changed. memif\_connection\_update\_t + - Declare memif connction handle. +```C +memif_conn_handle_t c; +``` +> example app uses struct that contains connection handle, rx/tx buffers and other connection specific information. + + - Specify connection arguments. +```C +memif_conn_args_t args; +memset (&args, 0, sizeof (args)); +args.is_master = is_master; +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; +args.interface_id = 0; +``` + - Declare callback functions called on connected/disconnected/interrupted status changed. +```C +int +on_connect (memif_conn_handle_t conn, void *private_ctx) +{ +... +} + +int +on_disconnect (memif_conn_handle_t conn, void *private_ctx) +{ + INFO ("memif connected!"); + return 0; +} +``` - Call memif interface create function. memif\_create +```C +err = memif_create (&c->conn, + &args, on_connect, on_disconnect, on_interrupt, &ctx[index]); +``` > If connection is in slave mode, arms timer file descriptor. -> If on interrupt callback is set to NULL, user will not be notified about interrupt. Use memif\_get\_queue\_efd call to get interrupt file descrip[tor for specific queue. +> If on interrupt callback is set to NULL, user will not be notified about interrupt. Use memif\_get\_queue\_efd call to get interrupt file descriptor for specific queue. +```C +int fd = -1; +err = memif_get_queue_efd (c->conn, data->qid, &fd); +``` 3. Connection establishment - - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback.. + - User application will poll events on all file descriptors returned in memif\_control\_fd\_update\_t callback. - On event call memif\_control\_fd\_handler. - Everything else regarding connection establishment will be done internally. - Once connection has been established, a callback will inform the user about connection status change. 4. Interrupt packet receive - If event is polled on interrupt file descriptor, libmemif will call memif\_interrupt\_t callback specified for every connection instance. +```C +int +on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) +{ +... +} +``` 6. Memif buffers - Packet data are stored in memif\_buffer\_t. Pointer _data_ points to shared memory buffer, and unsigned integer *data\_len* contains packet data length. +```C +typedef struct +{ + uint16_t desc_index; + uint32_t buffer_len; + uint32_t data_len; + void *data; +} memif_buffer_t; +``` 5. Packet receive - Api call memif\_rx\_burst will set all required fields in memif buffers provided by user application. +```C +err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); +``` - User application can then process packets. - Api call memif\_buffer\_free will make supplied memif buffers ready for next receive and mark shared memory buffers as free. +```C +err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); +``` 6. Packet transmit - - Api call memif\_buffer\_alloc will set all required fields in memif buffers provided by user application. + - Api call memif\_buffer\_alloc will set all required fields in memif buffers provided by user application. +```C +err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); +``` - User application can populate shared memory buffers with packets. - Api call memif\_tx\_burst will inform peer interface (master memif on VPP) that there are packets ready to receive and mark memif buffers as free. +```C +err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); +``` 7. Helper functions - Memif details - Api call memif\_get\_details will return details about connection. +```C +err = memif_get_details (c->conn, &md, buf, buflen); +``` - Memif error messages - Every api call returns error code (integer value) mapped to error string. - Call memif\_strerror will return error message assigned to specific error code. +```C +if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_get_details: %s", memif_strerror (err)); +``` - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. #### Example app: @@ -145,7 +243,7 @@ example startup.conf: ``` unix { interactive - nodaemon + nodaemon full-coredump } From fbe548949cb0341045b6c7dbb5840818aa47e864 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Tue, 15 Aug 2017 11:16:26 +0200 Subject: [PATCH 45/63] bugfixing, example apps patch --- UserManual.md | 4 ++ examples/icmp_responder/main.c | 27 +++----- examples/icmp_responder3/main.c | 109 ++++++++++++++++++++------------ src/main.c | 24 ++++--- src/memif_private.h | 2 +- src/socket.c | 4 +- test/main_test.c | 2 +- 7 files changed, 99 insertions(+), 73 deletions(-) diff --git a/UserManual.md b/UserManual.md index a7750b4..11c2e6f 100644 --- a/UserManual.md +++ b/UserManual.md @@ -179,8 +179,10 @@ typedef struct ```C err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); ``` + - User application can then process packets. - Api call memif\_buffer\_free will make supplied memif buffers ready for next receive and mark shared memory buffers as free. + ```C err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); ``` @@ -190,8 +192,10 @@ err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); ```C err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); ``` + - User application can populate shared memory buffers with packets. - Api call memif\_tx\_burst will inform peer interface (master memif on VPP) that there are packets ready to receive and mark memif buffers as free. + ```C err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); ``` diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 21daa70..625f985 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -102,35 +102,27 @@ print_memif_details () ssize_t buflen; char *buf; int err, i, e; + 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]; - printf ("interface index: %d\n", i); memset (&md, 0, sizeof (md)); - buflen = 2048; - buf = malloc (buflen); memset (buf, 0, buflen); err = memif_get_details (c->conn, &md, buf, buflen); if (err != MEMIF_ERR_SUCCESS) { - if (err == MEMIF_ERR_NOCONN) - { - printf ("\tno connection\n"); - free (buf); - continue; - } - else - { + if (err != MEMIF_ERR_NOCONN) INFO ("%s", memif_strerror (err)); - free (buf); - continue; - } + 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); @@ -180,9 +172,8 @@ print_memif_details () printf ("up\n"); else printf ("down\n"); - - free (buf); } + free (buf); } int @@ -507,7 +498,8 @@ icmpr_free () for (i = 0; i < MAX_CONNS; i++) { memif_connection_t *c = &memif_connection[i]; - icmpr_memif_delete (i); + if (c->conn) + icmpr_memif_delete (i); } err = memif_cleanup (); @@ -773,6 +765,7 @@ int main () for (i = 0; i < MAX_CONNS; i++) { + memif_connection[i].conn = NULL; ctx[i] = i; } diff --git a/examples/icmp_responder3/main.c b/examples/icmp_responder3/main.c index 8f56f3c..4e5e326 100644 --- a/examples/icmp_responder3/main.c +++ b/examples/icmp_responder3/main.c @@ -69,8 +69,9 @@ /* maximum tx/rx memif buffers */ #define MAX_MEMIF_BUFS 256 -#define MAX_CONNS 1 -#define MAX_THREADS 4 +#define MAX_CONNS 50 +#define MAX_QUEUES 2 +#define MAX_THREADS ((MAX_CONNS) * (MAX_QUEUES)) int main_epfd; @@ -82,6 +83,7 @@ typedef struct 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; @@ -119,36 +121,28 @@ print_memif_details () memif_details_t md; ssize_t buflen; char *buf; - int err, i, e; + 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]; - printf ("interface index: %d\n", i); memset (&md, 0, sizeof (md)); - buflen = 2048; - buf = malloc (buflen); memset (buf, 0, buflen); err = memif_get_details (c->conn, &md, buf, buflen); if (err != MEMIF_ERR_SUCCESS) { - if (err == MEMIF_ERR_NOCONN) - { - printf ("\tno connection\n"); - free (buf); - continue; - } - else - { + if (err != MEMIF_ERR_NOCONN) INFO ("%s", memif_strerror (err)); - free (buf); - continue; - } + 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); @@ -182,9 +176,17 @@ print_memif_details () 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++) @@ -198,9 +200,8 @@ print_memif_details () printf ("up\n"); else printf ("down\n"); - - free (buf); } + free (buf); } int @@ -277,6 +278,7 @@ memif_rx_poll (void *ptr) 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) @@ -355,6 +357,7 @@ memif_rx_poll (void *ptr) 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); } @@ -378,6 +381,7 @@ memif_rx_interrupt (void *ptr) 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); @@ -478,6 +482,7 @@ memif_rx_interrupt (void *ptr) 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); @@ -489,26 +494,36 @@ int on_connect (memif_conn_handle_t conn, void *private_ctx) { long index = (*(long *) private_ctx); - int err; + int err, i, ti; INFO ("memif connected! index %ld", index); memif_connection_t *c = &memif_connection[index]; - err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, 0); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), 0); - err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, 1); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), 1); c->pending_del = 0; - thread_data[index].index = index; - thread_data[index].qid = 0; - thread_data[index].id = 0; - pthread_create (&thread[0], NULL, memif_rx_poll, (void *) &thread_data[index]); - - thread_data[index + 1].index = index; - thread_data[index + 1].qid = 1; - thread_data[index + 1].id = 1; - pthread_create (&thread[1], NULL, memif_rx_interrupt, (void *) &thread_data[index + 1]); + 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; } @@ -518,15 +533,21 @@ int on_disconnect (memif_conn_handle_t conn, void *private_ctx) { void *ptr; - memif_connection_t *c = &memif_connection[(*(long *) private_ctx)]; - + 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; - pthread_join (thread[0], &ptr); - /* send custom sugnal to interrupt thread */ - pthread_kill (thread[1], SIGUSR1); - pthread_join (thread[1], &ptr); + 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; } @@ -658,7 +679,8 @@ icmpr_free () for (i = 0; i < MAX_CONNS; i++) { memif_connection_t *c = &memif_connection[i]; - icmpr_memif_delete (i); + if (c->conn) + icmpr_memif_delete (i); } err = memif_cleanup (); @@ -864,9 +886,12 @@ int main () 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 */ diff --git a/src/main.c b/src/main.c index 53d8831..b4761a0 100644 --- a/src/main.c +++ b/src/main.c @@ -147,7 +147,7 @@ const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ char * memif_strerror (int err_code) { - if (err_code > ERRLIST_LEN) + if (err_code >= ERRLIST_LEN) { strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED)); memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0'; @@ -501,7 +501,7 @@ memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, uint16_t qid) memif_connection_t *conn = (memif_connection_t *) c; if (conn == NULL) return MEMIF_ERR_NOCONN; - uint8_t num = (conn->args.is_master) ? conn->args.num_s2m_rings : conn->args.num_m2s_rings; + uint8_t num = (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; @@ -923,7 +923,7 @@ memif_msg_queue_free (memif_msg_queue_elt_t **e) /* send disconnect msg and close interface */ int -memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) +memif_disconnect_internal (memif_connection_t *c) { if (c == NULL) { @@ -947,12 +947,10 @@ memif_disconnect_internal (memif_connection_t *c, uint8_t is_del) get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd); if (e != NULL) { - if (is_del || c->args.is_master) + if (c->args.is_master) free_list_elt (lm->control_list, lm->control_list_len, c->fd); e->key = c->fd = -1; } - else if (is_del) - free_list_elt_ctx (lm->control_list, lm->control_list_len, c); if (c->tx_queues != NULL) { @@ -1033,11 +1031,17 @@ memif_delete (memif_conn_handle_t *conn) memif_list_elt_t *e = NULL; memif_socket_t *ms = NULL; - int err; + int err = MEMIF_ERR_SUCCESS; - err = memif_disconnect_internal (c, 1); - if (err == MEMIF_ERR_NOCONN) - return err; + if (c->fd > 0) + { + DBG ("DISCONNECTING"); + err = memif_disconnect_internal (c); + if (err == MEMIF_ERR_NOCONN) + return err; + } + + free_list_elt_ctx (lm->control_list, lm->control_list_len, c); if (c->args.is_master) { diff --git a/src/memif_private.h b/src/memif_private.h index 919904a..04e1bf3 100644 --- a/src/memif_private.h +++ b/src/memif_private.h @@ -211,7 +211,7 @@ int memif_connect1 (memif_connection_t *c); /* memory map region, initalize rings and queues */ int memif_init_regions_and_queues (memif_connection_t *c); -int memif_disconnect_internal (memif_connection_t *c, uint8_t is_del); +int memif_disconnect_internal (memif_connection_t *c); /* map errno to memif error code */ int memif_syscall_error_handler (int err_code); diff --git a/src/socket.c b/src/socket.c index 22562e0..fbb5023 100644 --- a/src/socket.c +++ b/src/socket.c @@ -778,7 +778,7 @@ memif_conn_fd_error (memif_connection_t *c) DBG ("connection fd error"); strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19); - int err = memif_disconnect_internal (c, 0); + int err = memif_disconnect_internal (c); return err; } @@ -790,7 +790,7 @@ memif_conn_fd_read_ready (memif_connection_t *c) err = memif_msg_receive (c->fd); if (err != 0) { - err = memif_disconnect_internal (c, 0); + err = memif_disconnect_internal (c); } return err; } diff --git a/test/main_test.c b/test/main_test.c index b7b8351..f09468e 100644 --- a/test/main_test.c +++ b/test/main_test.c @@ -945,7 +945,7 @@ START_TEST (test_disconnect_internal) if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); - if ((err = memif_disconnect_internal (c, 0)) != MEMIF_ERR_SUCCESS) + if ((err = memif_disconnect_internal (c)) != MEMIF_ERR_SUCCESS) ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); ck_assert_int_eq (c->fd, -1); From a850ba6578d412a322f863eb3f05c04d153f6bb4 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Tue, 15 Aug 2017 14:35:44 +0200 Subject: [PATCH 46/63] doc update --- BuildInstructions.md | 51 ++ ExampleSetup.md | 185 +++++ UserManual.md => GettingStarted.md | 88 +- Makefile.am | 22 +- README.md | 224 +---- dockerfile | 15 +- examples/icmp_responder-epoll/main.c | 782 ++++++++++++++++++ .../main.c | 0 examples/icmp_responder/main.c | 689 ++++----------- examples/icmp_responder2/main.c | 409 --------- 10 files changed, 1231 insertions(+), 1234 deletions(-) create mode 100644 BuildInstructions.md create mode 100644 ExampleSetup.md rename UserManual.md => GettingStarted.md (83%) create mode 100644 examples/icmp_responder-epoll/main.c rename examples/{icmp_responder3 => icmp_responder-mt}/main.c (100%) delete mode 100644 examples/icmp_responder2/main.c diff --git a/BuildInstructions.md b/BuildInstructions.md new file mode 100644 index 0000000..3f0ea82 --- /dev/null +++ b/BuildInstructions.md @@ -0,0 +1,51 @@ +## Build Instructions + +Install dependencies +``` +# sudo apt-get install -y git autoconf pkg_config libtool check +``` + +Clone repository to your local machine. +``` +# git clone https://github.com/JakubGrajciar/libmemif.git +``` + +From root directory execute: +For debug build: +``` +# ./bootstrap +# ./configure +# make +# make install +``` + +For release build: +``` +# ./bootstrap +# ./configure +# make release +# make install +``` +Verify installation: +``` +# ./.libs/icmpr-epoll +``` +> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies successful build. Default install path is /usr/lib. +Use _help_ command to display build information and commands: +``` +ICMP_Responder:add_epoll_fd:204: fd 0 added to epoll +MEMIF_DEBUG:src/main.c:memif_init:383: app name: ICMP_Responder +ICMP_Responder:add_epoll_fd:204: fd 4 added to epoll +LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) +============================== +libmemif version: 1.0 (debug) +memif version: 256 +commands: + help - prints this help + exit - exit app + conn - create memif (slave-mode) + del - delete memif + show - show connection details + ip-set - set interface ip address + rx-mode - set queue rx mode +``` diff --git a/ExampleSetup.md b/ExampleSetup.md new file mode 100644 index 0000000..16337f4 --- /dev/null +++ b/ExampleSetup.md @@ -0,0 +1,185 @@ +## Example setup + +#### VPP-memif master icmp_responder slave + +> Libmemif example app(s) use memif default socket file: /run/vpp/memif.sock. + +Run VPP and icmpr-epoll example (default example when running in container). +> Other examples work similar to icmpr-epoll. Brief explanation can be found in [Readme](README.md). + +VPP-side config: +``` +# create memif id 0 master +# set int state memif0/0 up +# set int ip address memif0/0 192.168.1.1/24 +``` +icmpr-epoll: +``` +# conn 0 0 +``` +Memif in slave mode will try to connect every 2 seconds. If connection establishment is successfull, a message will show. +``` +INFO: memif connected! +``` +> Error messages like "unmatched interface id" are printed only in debug mode. + +Check connected status. +Use show command in icmpr-epoll: +``` +show +MEMIF DETAILS +============================== +interface index: 0 + interface ip: 192.168.1.2 + interface name: memif_connection + app name: ICMP_Responder + remote interface name: memif0/0 + remote app name: VPP 17.10-rc0~132-g62f9cdd + id: 0 + secret: + role: slave + mode: ethernet + socket filename: /run/vpp/memif.sock + rx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + tx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + link: up +interface index: 1 + no connection + +``` +Use sh memif command in VPP: +``` +DBGvpp# sh memif +interface memif0/0 + remote-name "ICMP_Responder" + remote-interface "memif_connection" + id 0 mode ethernet file /run/vpp/memif.sock + flags admin-up connected + listener-fd 12 conn-fd 13 + num-s2m-rings 1 num-m2s-rings 1 buffer-size 0 + master-to-slave ring 0: + region 0 offset 32896 ring-size 1024 int-fd 16 + head 0 tail 0 flags 0x0000 interrupts 0 + master-to-slave ring 0: + region 0 offset 0 ring-size 1024 int-fd 15 + head 0 tail 0 flags 0x0001 interrupts 0 +``` + +Send ping from VPP to icmpr-epoll: +``` +DBGvpp# ping 192.168.1.2 +64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=.1888 ms +64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=.1985 ms +64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=.1813 ms +64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=.1929 ms + +Statistics: 5 sent, 4 received, 20% packet loss +``` +#### multiple queues VPP-memif slave icmp_responder master + +Run icmpr-epoll as in previous example setup. +Run VPP with startup conf, enabling 2 worker threads. +Example startup.conf: +``` +unix { + interactive + nodaemon + full-coredump +} + +cpu { + workers 2 +} +``` +VPP-side config: +``` +# create memif id 0 slave rx-queues 2 tx-queues 2 +# set int state memif0/0 up +# set int ip address memif0/0 192.168.1.1/24 +``` +icmpr-epoll: +``` +# conn 0 1 +``` +When connection is established a message will print: +``` +INFO: memif connected! +``` +> Error messages like "unmatched interface id" are printed only in debug mode. + +Check connected status. +Use show command in icmpr-epoll: +``` +show +MEMIF DETAILS +============================== +interface index: 0 + interface ip: 192.168.1.2 + interface name: memif_connection + app name: ICMP_Responder + remote interface name: memif0/0 + remote app name: VPP 17.10-rc0~132-g62f9cdd + id: 0 + secret: + role: master + mode: ethernet + socket filename: /run/vpp/memif.sock + rx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + queue id: 1 + ring size: 1024 + buffer size: 2048 + tx queues: + queue id: 0 + ring size: 1024 + buffer size: 2048 + queue id: 1 + ring size: 1024 + buffer size: 2048 + link: up +interface index: 1 + no connection + +``` +Use sh memif command in VPP: +``` +DBGvpp# sh memif +interface memif0/0 + remote-name "ICMP_Responder" + remote-interface "memif_connection" + id 0 mode ethernet file /run/vpp/memif.sock + flags admin-up slave connected + listener-fd -1 conn-fd 12 + num-s2m-rings 2 num-m2s-rings 2 buffer-size 2048 + slave-to-master ring 0: + region 0 offset 0 ring-size 1024 int-fd 14 + head 0 tail 0 flags 0x0000 interrupts 0 + slave-to-master ring 1: + region 0 offset 32896 ring-size 1024 int-fd 15 + head 0 tail 0 flags 0x0000 interrupts 0 + slave-to-master ring 0: + region 0 offset 65792 ring-size 1024 int-fd 16 + head 0 tail 0 flags 0x0001 interrupts 0 + slave-to-master ring 1: + region 0 offset 98688 ring-size 1024 int-fd 17 + head 0 tail 0 flags 0x0001 interrupts 0 + +``` +Send ping from VPP to icmpr-epoll: +``` +DBGvpp# ping 192.168.1.2 +64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=.1439 ms +64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=.2184 ms +64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=.1458 ms +64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=.1687 ms + +Statistics: 5 sent, 4 received, 20% packet loss +``` diff --git a/UserManual.md b/GettingStarted.md similarity index 83% rename from UserManual.md rename to GettingStarted.md index 11c2e6f..10dac20 100644 --- a/UserManual.md +++ b/GettingStarted.md @@ -1,66 +1,5 @@ ## Getting started -#### Build - -Install dependencies -``` -# sudo apt-get install -y git autoconf pkg_config libtool check -``` - -Clone repository to your local machine. -``` -# git clone https://github.com/JakubGrajciar/libmemif.git -``` - -From root directory execute: -For debug build: -``` -# ./bootstrap -# ./configure -# make -# make install -``` - -For release build: -``` -# ./bootstrap -# ./configure -# make release -# make install -``` -Verify installation: -``` -# ./.libs/icmp_responder -``` -> Make sure to run the binary file from ./.libs. File ./icmp\_responder in libmemif root directory is script that links the library, so it only verifies successful build. Default install path is /usr/lib. -Use _help_ command to display build information and commands: -``` -ICMP_Responder:add_epoll_fd:204: fd 0 added to epoll -MEMIF_DEBUG:src/main.c:memif_init:383: app name: ICMP_Responder -ICMP_Responder:add_epoll_fd:204: fd 4 added to epoll -LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) -============================== -libmemif version: 1.0 (debug) -memif version: 256 -commands: - help - prints this help - exit - exit app - conn - create memif (slave-mode) - del - delete memif - show - show connection details - ip-set - set interface ip address - rx-mode - set queue rx mode -``` - -#### Unit tests - -Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be installed in order to build *unit\_test* binary. -Ubuntu/Debian: -``` -sudo apt-get install check -``` -[More platforms](https://libcheck.github.io/check/web/install.html) - #### Concept (Connecting to VPP) For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) @@ -179,10 +118,8 @@ typedef struct ```C err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); ``` - - User application can then process packets. - Api call memif\_buffer\_free will make supplied memif buffers ready for next receive and mark shared memory buffers as free. - ```C err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); ``` @@ -192,10 +129,8 @@ err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); ```C err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); ``` - - User application can populate shared memory buffers with packets. - Api call memif\_tx\_burst will inform peer interface (master memif on VPP) that there are packets ready to receive and mark memif buffers as free. - ```C err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); ``` @@ -215,23 +150,23 @@ if (err != MEMIF_ERR_SUCCESS) ``` - Not all syscall errors are translated to memif error codes. If error code 1 (MEMIF\_ERR\_SYSCALL) is returned then libmemif needs to be compiled with -DMEMIF_DBG flag to print error message. Use _make -B_ to rebuild libmemif in debug mode. -#### Example app: - -- [ICMP Responder](examples/icmp_responder/main.c) - #### Example app (libmemif fd event polling): -- [ICMP Responder](examples/icmp_responder2/main.c) +- [ICMP Responder](examples/icmp_responder/main.c) > Optional argument: transmit queue id. ``` -icmpr_lep 1 +icmpr 1 ``` > Set transmit queue id to 1. Default is 0. > Application will create memif interface in slave mode and try to connect to VPP. Exit using Ctrl+C. Application will handle SIGINT signal, free allocated memory and exit with EXIT_SUCCESS. +#### Example app: + +- [ICMP Responder custom fd event polling](examples/icmp_responder-epoll/main.c) + #### Example app (multi-thread queue polling) -- [ICMP Responder](examples/icmp_responder3/main.c) +- [ICMP Responder multi-thread](examples/icmp_responder-mt/main.c) > Simple example of libmemif multi-thread usage. Connection establishment is handled by main thread. There are two rx queues in this example. One in polling mode and second in interrupt mode. @@ -269,3 +204,12 @@ VPP config: > Example applications use VPP default socket file for memif: /run/vpp/memif.sock > For master mode, socket directory must exist prior to memif\_create call. +#### Unit tests + +Unit tests use [Check](https://libcheck.github.io/check/index.html) framework. This framework must be installed in order to build *unit\_test* binary. +Ubuntu/Debian: +``` +sudo apt-get install check +``` +[More platforms](https://libcheck.github.io/check/web/install.html) + diff --git a/Makefile.am b/Makefile.am index 2dc7ee6..5a118db 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,19 +43,27 @@ libmemif_la_CPPFLAGS = $(AM_CPPFLAGS) -Isrc # # ICMP responder example # -icmp_responder_SOURCES = examples/icmp_responder/main.c examples/icmp_responder/icmp_proto.c -icmp_responder_LDADD = libmemif.la -icmp_responder_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder +icmpr_SOURCES = examples/icmp_responder/main.c examples/icmp_responder/icmp_proto.c +icmpr_LDADD = libmemif.la +icmpr_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder # # ICMP responder libmemif event polling example # -icmpr_lep_SOURCES = examples/icmp_responder2/main.c \ +icmpr_epoll_SOURCES = examples/icmp_responder-epoll/main.c \ examples/icmp_responder/icmp_proto.c -icmpr_lep_LDADD = libmemif.la -icmpr_lep_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder +icmpr_epoll_LDADD = libmemif.la +icmpr_epoll_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder -noinst_PROGRAMS = icmp_responder icmpr_lep +# +# ICMP responder multi-thread example +# +icmpr_mt_SOURCES = examples/icmp_responder-mt/main.c \ + examples/icmp_responder/icmp_proto.c +icmpr_mt_LDADD = libmemif.la -lpthread +icmpr_mt_CPPFLAGS = $(AM_CPPFLAGS) -Isrc -Iexamples/icmp_responder + +noinst_PROGRAMS = icmpr icmpr-epoll icmpr-mt check_PROGRAMS = unit_test diff --git a/README.md b/README.md index 87c2cb4..6da0c12 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Shared Memory Packet Interface (memif) Library ============================================== ## Introduction -Shared memory packet interface (memif) provides high performance packet transmit and receive between user application and Vector Packet Processing (VPP). +Shared memory packet interface (memif) provides high performance packet transmit and receive between user application and Vector Packet Processing (VPP). ## Features @@ -19,40 +19,32 @@ Shared memory packet interface (memif) provides high performance packet transmit - [x] Master mode - [ ] Multiple regions (TODO) - [ ] Performance testing (TODO) -- [ ] Documentation (TODO) ## Quickstart -For information on how to use libmemif API, please refer to [User manual](UserManual.md). +For information on how to use libmemif API, please refer to [Getting started](GettingStarted.md). #### Run in container Install [docker](https://docs.docker.com/engine/installation) engine. Useful link: [Docker documentation](https://docs.docker.com/get-started). - -Use [example dockerfile](dockerfile). This dockerfile builds and installs libmemif in debug mode and runs ICMP responder example app. Run following command in directory with example dockerfile: -``` -# docker build . -``` -When build is completed: +Pull image: ``` -# docker images -REPOSITORY TAG IMAGE ID CREATED SIZE - c83438f19940 3 seconds ago 468MB -ubuntu xenial 0ef2e08ed3fa 5 months ago 130MB +# docker pull ligato/libmemif-sample-service ``` -Tag your new image: + +Now you should be able to see ligato/libmemif-sample-service image on your local machine (IMAGE ID in this README may be outdated): ``` -# docker tag c83438f19940 icmp-responder:latest # docker images -REPOSITORY TAG IMAGE ID CREATED SIZE -icmp-responder latest c83438f19940 3 seconds ago 468MB -ubuntu xenial 0ef2e08ed3fa 5 months ago 130MB +REPOSITORY TAG IMAGE ID CREATED SIZE +ligato/libmemif-sample-service latest 32ecc2f9d013 About a minute ago 468MB +... ``` + Run container: ``` -docker run -it --rm --name icmp-responder --hostname icmp-responder --privileged -v "/run/vpp/:/run/vpp/" icmp-responder +# docker run -it --rm --name icmp-responder --hostname icmp-responder --privileged -v "/run/vpp/:/run/vpp/" ligato/libmemif-sample-service ``` Example application will start in debug mode. Output should look like this: ``` @@ -73,190 +65,20 @@ commands: rx-mode - set queue rx mode ``` -#### Run without container - -Build process is explained in [User Manual](UserManual.md). - -### Connection to VPP-memif - -> Libmemif example app(s) use memif default socket file: /run/vpp/memif.sock. - -#### Example setup (VPP-memif master icmp_responder slave) - -Run VPP and icmp_responder example. -VPP-side config: -``` -# create memif id 0 master -# set int state memif0/0 up -# set int ip address memif0/0 192.168.1.1/24 -``` -icmp_responder: -``` -# conn 0 0 -``` -Memif in slave mode will try to connect every 2 seconds. If connection establishment is successfull, a message will show. -``` -INFO: memif connected! -``` -> Error messages like "unmatched interface id" are printed only in debug mode. - -Check connected status. -Use show command in icmp_responder -``` -show -MEMIF DETAILS -============================== -interface index: 0 - interface ip: 192.168.1.2 - interface name: memif_connection - app name: ICMP_Responder - remote interface name: memif0/0 - remote app name: VPP 17.10-rc0~132-g62f9cdd - id: 0 - secret: - role: slave - mode: ethernet - socket filename: /run/vpp/memif.sock - rx queues: - queue id: 0 - ring size: 1024 - buffer size: 2048 - tx queues: - queue id: 0 - ring size: 1024 - buffer size: 2048 - link: up -interface index: 1 - no connection - -``` -Use sh memif command in VPP: -``` -DBGvpp# sh memif -interface memif0/0 - remote-name "ICMP_Responder" - remote-interface "memif_connection" - id 0 mode ethernet file /run/vpp/memif.sock - flags admin-up connected - listener-fd 12 conn-fd 13 - num-s2m-rings 1 num-m2s-rings 1 buffer-size 0 - master-to-slave ring 0: - region 0 offset 32896 ring-size 1024 int-fd 16 - head 0 tail 0 flags 0x0000 interrupts 0 - master-to-slave ring 0: - region 0 offset 0 ring-size 1024 int-fd 15 - head 0 tail 0 flags 0x0001 interrupts 0 -``` - -Send ping from VPP to icmp_responder: -``` -DBGvpp# ping 192.168.1.2 -64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=.1888 ms -64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=.1985 ms -64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=.1813 ms -64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=.1929 ms - -Statistics: 5 sent, 4 received, 20% packet loss -``` -#### Example setup multiple queues (VPP-memif slave icmp_responder master) - -Run icmp_responder as in previous example setup. -Run VPP with startup conf, enabling 2 worker threads. -Example startup.conf: -``` -unix { - interactive - nodaemon - full-coredump -} - -cpu { - workers 2 -} -``` -VPP-side config: -``` -# create memif id 0 slave rx-queues 2 tx-queues 2 -# set int state memif0/0 up -# set int ip address memif0/0 192.168.1.1/24 -``` -icmp_responder: -``` -# conn 0 1 -``` -When connection is established a message will print: -``` -INFO: memif connected! -``` -> Error messages like "unmatched interface id" are printed only in debug mode. - -Check connected status. -Use show command in icmp_responder -``` -show -MEMIF DETAILS -============================== -interface index: 0 - interface ip: 192.168.1.2 - interface name: memif_connection - app name: ICMP_Responder - remote interface name: memif0/0 - remote app name: VPP 17.10-rc0~132-g62f9cdd - id: 0 - secret: - role: master - mode: ethernet - socket filename: /run/vpp/memif.sock - rx queues: - queue id: 0 - ring size: 1024 - buffer size: 2048 - queue id: 1 - ring size: 1024 - buffer size: 2048 - tx queues: - queue id: 0 - ring size: 1024 - buffer size: 2048 - queue id: 1 - ring size: 1024 - buffer size: 2048 - link: up -interface index: 1 - no connection +[Example setup](ExampleSetup.md) contains instructions on how to set up conenction between icmpr-epoll example app and VPP-memif. +This image contains more example apps. To run different examples, override docker CMD to start container in bash: ``` -Use sh memif command in VPP: +# docker run -it --entrypoint=/bin/bash -i --rm --name icmp-responder --hostname icmp-responder --privileged -v "/run/vpp/:/run/vpp/" ligato/libmemif-sample-service ``` -DBGvpp# sh memif -interface memif0/0 - remote-name "ICMP_Responder" - remote-interface "memif_connection" - id 0 mode ethernet file /run/vpp/memif.sock - flags admin-up slave connected - listener-fd -1 conn-fd 12 - num-s2m-rings 2 num-m2s-rings 2 buffer-size 2048 - slave-to-master ring 0: - region 0 offset 0 ring-size 1024 int-fd 14 - head 0 tail 0 flags 0x0000 interrupts 0 - slave-to-master ring 1: - region 0 offset 32896 ring-size 1024 int-fd 15 - head 0 tail 0 flags 0x0000 interrupts 0 - slave-to-master ring 0: - region 0 offset 65792 ring-size 1024 int-fd 16 - head 0 tail 0 flags 0x0001 interrupts 0 - slave-to-master ring 1: - region 0 offset 98688 ring-size 1024 int-fd 17 - head 0 tail 0 flags 0x0001 interrupts 0 +Current WORKDIR is root repository directory. Example apps can be run from this directory (a script linking binary with library), or browse to ./.libs folder and execute binary directly. Example apps: +1. icmpr +> Simplest implementaion. Event polling is handled by libmemif. Single memif conenction in slave mode is created (id 0). Use Ctrl + C to exit app. +2. icmpr-epoll (run in container by default) +> Supports multiple connections and master mode. User can create/delete connections, set ip addresses, print connection information. +3. icmpr-mt +> Multi-thread example, very similar to icmpr-epoll. Packets are handled in threads assigned to specific queues. Slave mode only. -``` -Send ping from VPP to icmp_responder: -``` -DBGvpp# ping 192.168.1.2 -64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=.1439 ms -64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=.2184 ms -64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=.1458 ms -64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=.1687 ms +#### Build from source -Statistics: 5 sent, 4 received, 20% packet loss -``` +Build process is explained in [Build Instructions](BuildInstructions.md). diff --git a/dockerfile b/dockerfile index 51151bd..92b2d2a 100644 --- a/dockerfile +++ b/dockerfile @@ -1,16 +1,3 @@ -# 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. - FROM ubuntu:xenial RUN apt-get update && \ @@ -29,4 +16,4 @@ RUN mkdir /var/vpp RUN ulimit -c unlimited -CMD ./.libs/icmp_responder +CMD ./.libs/icmpr-epoll diff --git a/examples/icmp_responder-epoll/main.c b/examples/icmp_responder-epoll/main.c new file mode 100644 index 0000000..625f985 --- /dev/null +++ b/examples/icmp_responder-epoll/main.c @@ -0,0 +1,782 @@ +/* + *------------------------------------------------------------------ + * 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 + +#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 + +int epfd; + +typedef struct +{ + uint16_t index; + /* memif conenction handle */ + memif_conn_handle_t conn; + /* tx buffers */ + memif_buffer_t *tx_bufs; + /* allocated tx buffers counter */ + /* number of tx buffers pointing to shared memory */ + uint16_t tx_buf_num; + /* rx buffers */ + memif_buffer_t *rx_bufs; + /* allcoated rx buffers counter */ + /* number of rx buffers pointing to shared memory */ + uint16_t rx_buf_num; + /* interface ip address */ + uint8_t ip_addr[4]; +} memif_connection_t; + +memif_connection_t memif_connection[MAX_CONNS]; +long ctx[MAX_CONNS]; + +/* print details for all memif connections */ +static void +print_memif_details () +{ + memif_details_t md; + ssize_t buflen; + char *buf; + int err, i, e; + 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++) + { + printf ("\t\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 ("\ttx queues:\n"); + for (e = 0; e < md.tx_queues_num; e++) + { + printf ("\t\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 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 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 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; +} + +/* 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) +{ + INFO ("memif connected!"); + 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) +{ + INFO ("memif disconnected!"); + 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 (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 (fd, evt); + + return add_epoll_fd (fd, evt); +} + +int +icmpr_buffer_alloc (long index, long n, uint16_t qid) +{ + memif_connection_t *c = &memif_connection[index]; + 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); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_buffer_alloc: %s", memif_strerror (err)); + c->tx_buf_num += r; + return -1; + } + c->tx_buf_num += r; + DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); + return 0; +} + +int +icmpr_tx_burst (long index, uint16_t qid) +{ + memif_connection_t *c = &memif_connection[index]; + int err; + uint16_t r; + /* inform peer memif interface about data in shared memory buffers */ + /* mark memif buffers as free */ + err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_tx_burst: %s", memif_strerror (err)); + DBG ("tx: %d/%u", r, c->tx_buf_num); + c->tx_buf_num -= r; + return 0; +} + +/* called when event is polled on interrupt file descriptor. + there are packets in shared memory ready to be received */ +int +on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) +{ + long index = *((long *) private_ctx); + memif_connection_t *c = &memif_connection[index]; + if (c->index != index) + { + INFO ("invalid context: %ld/%u", index, c->index); + return 0; + } + int err; + uint16_t rx; + uint16_t fb; + /* receive data from shared memory buffers */ + err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_rx_burst: %s", memif_strerror (err)); + c->rx_buf_num += rx; + goto error; + } + c->rx_buf_num += rx; + + DBG ("received %d buffers. %u/%u alloc/free buffers", + rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + if (icmpr_buffer_alloc (index, rx, qid) < 0) + { + INFO ("buffer_alloc error"); + goto error; + } + int i; + for (i = 0; i < rx; i++) + { + resolve_packet ((void *) (c->rx_bufs + i)->data, + (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, + &(c->tx_bufs + i)->data_len, c->ip_addr); + } + + /* mark memif buffers and shared memory buffers as free */ + err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + + icmpr_tx_burst (index, qid); + + return 0; + +error: + err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_buffer_free: %s", memif_strerror (err)); + c->rx_buf_num -= fb; + DBG ("freed %d buffers. %u/%u alloc/free buffers", + fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); + return 0; +} + +int +icmpr_memif_create (long index, long mode) +{ + 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 = mode; + 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, on_interrupt, &ctx[index]); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("memif_create: %s", memif_strerror (err)); + return 0; + } + + c->index = index; + /* alloc memif buffers */ + c->rx_buf_num = 0; + c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->tx_buf_num = 0; + c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + + 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]; + + if (c->rx_bufs) + free (c->rx_bufs); + c->rx_bufs = NULL; + c->rx_buf_num = 0; + if (c->tx_bufs) + free (c->tx_bufs); + c->tx_bufs = NULL; + c->tx_buf_num = 0; + + int err; + /* disconenct then delete memif connection */ + err = memif_delete (&c->conn); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); + if (c->conn != NULL) + INFO ("memif delete fail"); + 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. index is also used as interface id, mode 0 = slave 1 = master\n"); + printf ("\tdel - delete memif\n"); + printf ("\tshow - show connection details\n"); + printf ("\tip-set - set interface ip address\n"); + printf ("\trx-mode - set queue rx mode\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 +icmpr_set_rx_mode (long index, long qid, char* mode) +{ + 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; + } + + if (strncmp (mode, "interrupt", 9) == 0) + { + memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid); + } + + else if (strncmp (mode, "polling", 7) == 0) + { + memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid); + } + else + INFO ("expected rx mode "); + return 0; +} + +int +user_input_handler () +{ + int i; + char *in = (char *) malloc (256); + char *ui = fgets (in, 256, stdin); + char *end; + long a; + 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) + a = strtol (ui, &end, 10); + else + { + INFO ("expected id"); + goto done; + } + ui = strtok (NULL, " "); + if (ui != NULL) + icmpr_memif_create (a, strtol (ui, &end, 10)); + else + INFO ("expected mode <0|1>"); + 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 if (strncmp (ui, "rx-mode", 7) == 0) + { + ui = strtok (NULL, " "); + if (ui != NULL) + a = strtol (ui, &end, 10); + else + { + INFO ("expected id"); + goto done; + } + ui = strtok (NULL, " "); + if (ui != NULL) + icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " ")); + else + INFO ("expected qid"); + 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 (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 () +{ + epfd = epoll_create (1); + add_epoll_fd (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; + } + + print_help (); + + /* main loop */ + while (1) + { + if (poll_event (-1) < 0) + { + DBG ("poll_event error!"); + } + } +} diff --git a/examples/icmp_responder3/main.c b/examples/icmp_responder-mt/main.c similarity index 100% rename from examples/icmp_responder3/main.c rename to examples/icmp_responder-mt/main.c diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index 625f985..f62af55 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -67,16 +67,15 @@ } while (0) /* maximum tx/rx memif buffers */ -#define MAX_MEMIF_BUFS 256 -#define MAX_CONNS 50 - -int epfd; +#define MAX_MEMIF_BUFS 256 typedef struct { uint16_t index; /* memif conenction handle */ memif_conn_handle_t conn; + /* transmit queue id */ + uint16_t tx_qid; /* tx buffers */ memif_buffer_t *tx_bufs; /* allocated tx buffers counter */ @@ -91,152 +90,86 @@ typedef struct uint8_t ip_addr[4]; } memif_connection_t; -memif_connection_t memif_connection[MAX_CONNS]; -long ctx[MAX_CONNS]; +memif_connection_t memif_connection; +int epfd; -/* print details for all memif connections */ static void print_memif_details () { - memif_details_t md; - ssize_t buflen; - char *buf; - int err, i, e; - buflen = 2048; - buf = malloc (buflen); + memif_connection_t *c = &memif_connection; 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; - } + memif_details_t md; + memset (&md, 0, sizeof (md)); + ssize_t buflen = 2048; + char *buf = malloc (buflen); + memset (buf, 0, buflen); + int err, e; - 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++) - { - printf ("\t\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 ("\ttx queues:\n"); - for (e = 0; e < md.tx_queues_num; e++) + err = memif_get_details (c->conn, &md, buf, buflen); + if (err != MEMIF_ERR_SUCCESS) + { + INFO ("%s", memif_strerror (err)); + if (err == MEMIF_ERR_NOCONN) { - printf ("\t\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); + free (buf); + return; } - printf ("\tlink: "); - if (md.link_up_down) - printf ("up\n"); - else - printf ("down\n"); - } - free (buf); -} - -int -add_epoll_fd (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 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; -} + 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 ("\tsocket filename: %s\n",(char *) md.socket_filename); + printf ("\trx queues:\n"); + for (e = 0; e < md.rx_queues_num; e++) + { + printf ("\t\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 ("\ttx queues:\n"); + for (e = 0; e < md.tx_queues_num; e++) + { + printf ("\t\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"); -int -del_epoll_fd (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; + free (buf); } - /* informs user about connected status. private_ctx is used by user to identify connection (multiple connections WIP) */ int @@ -255,31 +188,39 @@ on_disconnect (memif_conn_handle_t conn, void *private_ctx) 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) +icmpr_memif_delete () { - /* convert memif event definitions to epoll events */ - if (events & MEMIF_FD_EVENT_DEL) - return del_epoll_fd (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 (fd, evt); + int err; + /* disconenct then delete memif connection */ + err = memif_delete (&(&memif_connection)->conn); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); + return 0; +} - return add_epoll_fd (fd, evt); +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 ("\tuse CTRL+C to exit\n"); } int -icmpr_buffer_alloc (long index, long n, uint16_t qid) +icmpr_buffer_alloc (long n, uint16_t qid) { - memif_connection_t *c = &memif_connection[index]; + memif_connection_t *c = &memif_connection; int err; uint16_t r; /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */ @@ -296,9 +237,9 @@ icmpr_buffer_alloc (long index, long n, uint16_t qid) } int -icmpr_tx_burst (long index, uint16_t qid) +icmpr_tx_burst (uint16_t qid) { - memif_connection_t *c = &memif_connection[index]; + memif_connection_t *c = &memif_connection; int err; uint16_t r; /* inform peer memif interface about data in shared memory buffers */ @@ -311,35 +252,50 @@ icmpr_tx_burst (long index, uint16_t qid) return 0; } +int +icmpr_free () +{ + /* application cleanup */ + int err; + memif_connection_t *c = &memif_connection; + free (c->tx_bufs); + c->tx_bufs = NULL; + free (c->rx_bufs); + c->rx_bufs = NULL; + + err = memif_cleanup (); + if (err != MEMIF_ERR_SUCCESS) + INFO ("memif_delete: %s", memif_strerror (err)); + + return 0; +} + +void +icmpr_exit (int sig) +{ + printf ("\n"); + icmpr_memif_delete (); + icmpr_free (); + exit (EXIT_SUCCESS); +} + /* called when event is polled on interrupt file descriptor. there are packets in shared memory ready to be received */ int on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) { - long index = *((long *) private_ctx); - memif_connection_t *c = &memif_connection[index]; - if (c->index != index) - { - INFO ("invalid context: %ld/%u", index, c->index); - return 0; - } + DBG ("interrupted"); + memif_connection_t *c = &memif_connection; int err; uint16_t rx; - uint16_t fb; /* receive data from shared memory buffers */ err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); - if (err != MEMIF_ERR_SUCCESS) - { - INFO ("memif_rx_burst: %s", memif_strerror (err)); - c->rx_buf_num += rx; - goto error; - } c->rx_buf_num += rx; DBG ("received %d buffers. %u/%u alloc/free buffers", rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - if (icmpr_buffer_alloc (index, rx, qid) < 0) + if (icmpr_buffer_alloc (rx, c->tx_qid) < 0) { INFO ("buffer_alloc error"); goto error; @@ -352,16 +308,15 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) &(c->tx_bufs + i)->data_len, c->ip_addr); } + uint16_t fb; /* mark memif buffers and shared memory buffers as free */ err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_buffer_free: %s", memif_strerror (err)); c->rx_buf_num -= fb; DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - icmpr_tx_burst (index, qid); + icmpr_tx_burst (c->tx_qid); return 0; @@ -376,25 +331,13 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) } int -icmpr_memif_create (long index, long mode) +icmpr_memif_create (int is_master) { - 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 = mode; + args.is_master = is_master; args.log2_ring_size = 10; args.buffer_size = 2048; args.num_s2m_rings = 2; @@ -406,375 +349,59 @@ icmpr_memif_create (long index, long mode) connect to VPP over memif. so default socket filename will be used */ /* default socketfile = /run/vpp/memif.sock */ - args.interface_id = index; + args.interface_id = 0; /* 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, on_interrupt, &ctx[index]); + int err = memif_create (&(&memif_connection)->conn, + &args, on_connect, on_disconnect, on_interrupt, NULL); if (err != MEMIF_ERR_SUCCESS) - { INFO ("memif_create: %s", memif_strerror (err)); - return 0; - } - - c->index = index; - /* alloc memif buffers */ - c->rx_buf_num = 0; - c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - c->tx_buf_num = 0; - c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - - 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) +int main (int argc, char *argv[]) { - 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]; + memif_connection_t *c = &memif_connection; - if (c->rx_bufs) - free (c->rx_bufs); - c->rx_bufs = NULL; - c->rx_buf_num = 0; - if (c->tx_bufs) - free (c->tx_bufs); - c->tx_bufs = NULL; - c->tx_buf_num = 0; - - int err; - /* disconenct then delete memif connection */ - err = memif_delete (&c->conn); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_delete: %s", memif_strerror (err)); - if (c->conn != NULL) - INFO ("memif delete fail"); - return 0; -} + signal (SIGINT, icmpr_exit); -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. index is also used as interface id, mode 0 = slave 1 = master\n"); - printf ("\tdel - delete memif\n"); - printf ("\tshow - show connection details\n"); - printf ("\tip-set - set interface ip address\n"); - printf ("\trx-mode - set queue rx mode\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 -icmpr_set_rx_mode (long index, long qid, char* mode) -{ - 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; - } - - if (strncmp (mode, "interrupt", 9) == 0) - { - memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid); - } - - else if (strncmp (mode, "polling", 7) == 0) - { - memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid); - } - else - INFO ("expected rx mode "); - return 0; -} - -int -user_input_handler () -{ - int i; - char *in = (char *) malloc (256); - char *ui = fgets (in, 256, stdin); - char *end; - long a; - 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) - a = strtol (ui, &end, 10); - else - { - INFO ("expected id"); - goto done; - } - ui = strtok (NULL, " "); - if (ui != NULL) - icmpr_memif_create (a, strtol (ui, &end, 10)); - else - INFO ("expected mode <0|1>"); - 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 if (strncmp (ui, "rx-mode", 7) == 0) - { - ui = strtok (NULL, " "); - if (ui != NULL) - a = strtol (ui, &end, 10); - else - { - INFO ("expected id"); - goto done; - } - ui = strtok (NULL, " "); - if (ui != NULL) - icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " ")); - else - INFO ("expected qid"); - goto done; - } + /* initialize global memif connection handle */ + c->conn = NULL; + if (argc == 1) + c->tx_qid = 0; else { - DBG ("unknown command: %s", ui); - goto done; + char *end; + c->tx_qid = strtol (argv[1], &end, 10); } - - 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 (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 () -{ - epfd = epoll_create (1); - add_epoll_fd (0, EPOLLIN); - + INFO ("tx qid: %u", c->tx_qid); + /* alloc memif buffers */ + c->rx_buf_num = 0; + c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->tx_buf_num = 0; + c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); + c->ip_addr[0] = 192; + c->ip_addr[1] = 168; + c->ip_addr[2] = 1; + c->ip_addr[3] = 2; /* initialize memory interface */ - int err, i; + int err; /* 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); + err = memif_init (NULL, 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; - } - print_help (); + icmpr_memif_create (0); + print_memif_details (); + /* main loop */ while (1) { - if (poll_event (-1) < 0) + if (memif_poll_event (-1) < 0) { DBG ("poll_event error!"); } diff --git a/examples/icmp_responder2/main.c b/examples/icmp_responder2/main.c deleted file mode 100644 index f62af55..0000000 --- a/examples/icmp_responder2/main.c +++ /dev/null @@ -1,409 +0,0 @@ -/* - *------------------------------------------------------------------ - * 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 - -#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 - -typedef struct -{ - uint16_t index; - /* memif conenction handle */ - memif_conn_handle_t conn; - /* transmit queue id */ - uint16_t tx_qid; - /* tx buffers */ - memif_buffer_t *tx_bufs; - /* allocated tx buffers counter */ - /* number of tx buffers pointing to shared memory */ - uint16_t tx_buf_num; - /* rx buffers */ - memif_buffer_t *rx_bufs; - /* allcoated rx buffers counter */ - /* number of rx buffers pointing to shared memory */ - uint16_t rx_buf_num; - /* interface ip address */ - uint8_t ip_addr[4]; -} memif_connection_t; - -memif_connection_t memif_connection; -int epfd; - -static void -print_memif_details () -{ - memif_connection_t *c = &memif_connection; - printf ("MEMIF DETAILS\n"); - printf ("==============================\n"); - - - memif_details_t md; - memset (&md, 0, sizeof (md)); - ssize_t buflen = 2048; - char *buf = malloc (buflen); - memset (buf, 0, buflen); - int err, e; - - err = memif_get_details (c->conn, &md, buf, buflen); - if (err != MEMIF_ERR_SUCCESS) - { - INFO ("%s", memif_strerror (err)); - if (err == MEMIF_ERR_NOCONN) - { - free (buf); - return; - } - } - - 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 ("\tsocket filename: %s\n",(char *) md.socket_filename); - printf ("\trx queues:\n"); - for (e = 0; e < md.rx_queues_num; e++) - { - printf ("\t\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 ("\ttx queues:\n"); - for (e = 0; e < md.tx_queues_num; e++) - { - printf ("\t\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); -} -/* 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) -{ - INFO ("memif connected!"); - 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) -{ - INFO ("memif disconnected!"); - return 0; -} - -int -icmpr_memif_delete () -{ - int err; - /* disconenct then delete memif connection */ - err = memif_delete (&(&memif_connection)->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 ("\tuse CTRL+C to exit\n"); -} - -int -icmpr_buffer_alloc (long n, uint16_t qid) -{ - memif_connection_t *c = &memif_connection; - 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); - if (err != MEMIF_ERR_SUCCESS) - { - INFO ("memif_buffer_alloc: %s", memif_strerror (err)); - c->tx_buf_num += r; - return -1; - } - c->tx_buf_num += r; - DBG ("allocated %d/%ld buffers, %u free buffers", r, n, MAX_MEMIF_BUFS - c->tx_buf_num); - return 0; -} - -int -icmpr_tx_burst (uint16_t qid) -{ - memif_connection_t *c = &memif_connection; - int err; - uint16_t r; - /* inform peer memif interface about data in shared memory buffers */ - /* mark memif buffers as free */ - err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_tx_burst: %s", memif_strerror (err)); - DBG ("tx: %d/%u", r, c->tx_buf_num); - c->tx_buf_num -= r; - return 0; -} - -int -icmpr_free () -{ - /* application cleanup */ - int err; - memif_connection_t *c = &memif_connection; - free (c->tx_bufs); - c->tx_bufs = NULL; - free (c->rx_bufs); - c->rx_bufs = NULL; - - err = memif_cleanup (); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_delete: %s", memif_strerror (err)); - - return 0; -} - -void -icmpr_exit (int sig) -{ - printf ("\n"); - icmpr_memif_delete (); - icmpr_free (); - exit (EXIT_SUCCESS); -} - -/* called when event is polled on interrupt file descriptor. - there are packets in shared memory ready to be received */ -int -on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) -{ - DBG ("interrupted"); - memif_connection_t *c = &memif_connection; - int err; - uint16_t rx; - /* receive data from shared memory buffers */ - err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); - c->rx_buf_num += rx; - - DBG ("received %d buffers. %u/%u alloc/free buffers", - rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - - if (icmpr_buffer_alloc (rx, c->tx_qid) < 0) - { - INFO ("buffer_alloc error"); - goto error; - } - int i; - for (i = 0; i < rx; i++) - { - resolve_packet ((void *) (c->rx_bufs + i)->data, - (c->rx_bufs + i)->data_len, (void *) (c->tx_bufs + i)->data, - &(c->tx_bufs + i)->data_len, c->ip_addr); - } - - uint16_t fb; - /* mark memif buffers and shared memory buffers as free */ - err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); - c->rx_buf_num -= fb; - - DBG ("freed %d buffers. %u/%u alloc/free buffers", - fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - - icmpr_tx_burst (c->tx_qid); - - return 0; - -error: - err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_buffer_free: %s", memif_strerror (err)); - c->rx_buf_num -= fb; - DBG ("freed %d buffers. %u/%u alloc/free buffers", - fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); - return 0; -} - -int -icmpr_memif_create (int is_master) -{ - /* setting memif connection arguments */ - memif_conn_args_t args; - int fd = -1; - memset (&args, 0, sizeof (args)); - args.is_master = is_master; - 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 = 0; - /* 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 (&(&memif_connection)->conn, - &args, on_connect, on_disconnect, on_interrupt, NULL); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_create: %s", memif_strerror (err)); - return 0; -} - -int main (int argc, char *argv[]) -{ - memif_connection_t *c = &memif_connection; - - signal (SIGINT, icmpr_exit); - - /* initialize global memif connection handle */ - c->conn = NULL; - if (argc == 1) - c->tx_qid = 0; - else - { - char *end; - c->tx_qid = strtol (argv[1], &end, 10); - } - INFO ("tx qid: %u", c->tx_qid); - /* alloc memif buffers */ - c->rx_buf_num = 0; - c->rx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - c->tx_buf_num = 0; - c->tx_bufs = (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); - c->ip_addr[0] = 192; - c->ip_addr[1] = 168; - c->ip_addr[2] = 1; - c->ip_addr[3] = 2; - /* initialize memory interface */ - int err; - /* 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 (NULL, APP_NAME); - if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_init: %s", memif_strerror (err)); - - print_help (); - - icmpr_memif_create (0); - print_memif_details (); - - /* main loop */ - while (1) - { - if (memif_poll_event (-1) < 0) - { - DBG ("poll_event error!"); - } - } -} From 64d6dc6001241453bedf0f7399cb9ca920943c81 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 16 Aug 2017 14:01:11 +0200 Subject: [PATCH 47/63] documentatin update --- README.md | 24 +- GettingStarted.md => docs/GettingStarted.md | 8 +- BuildInstructions.md => docs/NextSteps.md | 5 +- docs/architecture.png | Bin 0 -> 18805 bytes ExampleSetup.md => examples/ExampleSetup.md | 23 +- examples/README.md | 19 ++ src/libmemif.h | 316 +++++++++----------- 7 files changed, 204 insertions(+), 191 deletions(-) rename GettingStarted.md => docs/GettingStarted.md (96%) rename BuildInstructions.md => docs/NextSteps.md (85%) create mode 100644 docs/architecture.png rename ExampleSetup.md => examples/ExampleSetup.md (85%) create mode 100644 examples/README.md diff --git a/README.md b/README.md index 6da0c12..aa9191a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ Shared Memory Packet Interface (memif) Library ============================================== ## Introduction -Shared memory packet interface (memif) provides high performance packet transmit and receive between user application and Vector Packet Processing (VPP). +Shared memory packet interface (memif) provides high performance packet transmit and receive between user application and Vector Packet Processing (VPP) or multiple user applications. Using libmemif, user application can create shared memory interface in master or slave mode and connect to VPP or another application using libmemif. Once the connection is established, user application can receive or transmit packets using libmemif API. + +![Architecture](docs/architecture.png) ## Features @@ -22,7 +24,7 @@ Shared memory packet interface (memif) provides high performance packet transmit ## Quickstart -For information on how to use libmemif API, please refer to [Getting started](GettingStarted.md). +For information on how to use libmemif API, please refer to [Getting started](docs/GettingStarted.md). #### Run in container @@ -65,20 +67,8 @@ commands: rx-mode - set queue rx mode ``` -[Example setup](ExampleSetup.md) contains instructions on how to set up conenction between icmpr-epoll example app and VPP-memif. - -This image contains more example apps. To run different examples, override docker CMD to start container in bash: -``` -# docker run -it --entrypoint=/bin/bash -i --rm --name icmp-responder --hostname icmp-responder --privileged -v "/run/vpp/:/run/vpp/" ligato/libmemif-sample-service -``` -Current WORKDIR is root repository directory. Example apps can be run from this directory (a script linking binary with library), or browse to ./.libs folder and execute binary directly. Example apps: -1. icmpr -> Simplest implementaion. Event polling is handled by libmemif. Single memif conenction in slave mode is created (id 0). Use Ctrl + C to exit app. -2. icmpr-epoll (run in container by default) -> Supports multiple connections and master mode. User can create/delete connections, set ip addresses, print connection information. -3. icmpr-mt -> Multi-thread example, very similar to icmpr-epoll. Packets are handled in threads assigned to specific queues. Slave mode only. +[Example setup](examples/ExampleSetup.md) contains instructions on how to set up conenction between icmpr-epoll example app and VPP-memif. Brief description and instructions on how to run other example apps can be found in [Examples readme](examples/README.md) -#### Build from source +#### Next steps -Build process is explained in [Build Instructions](BuildInstructions.md). +Continue to [Next steps](docs/NextSteps.md). diff --git a/GettingStarted.md b/docs/GettingStarted.md similarity index 96% rename from GettingStarted.md rename to docs/GettingStarted.md index 10dac20..3cc0ceb 100644 --- a/GettingStarted.md +++ b/docs/GettingStarted.md @@ -2,7 +2,7 @@ #### Concept (Connecting to VPP) -For detailed information on api calls and structures please refer to [libmemif.h](src/libmemif.h) +For detailed information on api calls and structures please refer to [libmemif.h](../src/libmemif.h) 1. Initialize memif - Declare callback function handling file descriptor event polling. @@ -152,7 +152,7 @@ if (err != MEMIF_ERR_SUCCESS) #### Example app (libmemif fd event polling): -- [ICMP Responder](examples/icmp_responder/main.c) +- [ICMP Responder](../examples/icmp_responder/main.c) > Optional argument: transmit queue id. ``` icmpr 1 @@ -162,11 +162,11 @@ icmpr 1 #### Example app: -- [ICMP Responder custom fd event polling](examples/icmp_responder-epoll/main.c) +- [ICMP Responder custom fd event polling](../examples/icmp_responder-epoll/main.c) #### Example app (multi-thread queue polling) -- [ICMP Responder multi-thread](examples/icmp_responder-mt/main.c) +- [ICMP Responder multi-thread](../examples/icmp_responder-mt/main.c) > Simple example of libmemif multi-thread usage. Connection establishment is handled by main thread. There are two rx queues in this example. One in polling mode and second in interrupt mode. diff --git a/BuildInstructions.md b/docs/NextSteps.md similarity index 85% rename from BuildInstructions.md rename to docs/NextSteps.md index 3f0ea82..4c4ffb6 100644 --- a/BuildInstructions.md +++ b/docs/NextSteps.md @@ -1,4 +1,4 @@ -## Build Instructions +## Next Steps Install dependencies ``` @@ -49,3 +49,6 @@ commands: ip-set - set interface ip address rx-mode - set queue rx mode ``` +#### Examples + +Once the library is build/installed, refer to [Examples](../examples/README.md) and [Getting started](GettingStarted.md) for additional information on basic use cases and API usage. diff --git a/docs/architecture.png b/docs/architecture.png new file mode 100644 index 0000000000000000000000000000000000000000..b196c25c5aa6d965ce194f7e5c6b8a20fe88cf2a GIT binary patch literal 18805 zcmbun1z6Ng+c&%-pbH4P2uP!#2na}bEFdMYNFycPp`diQgaRTZBHi5$QVIgn-7Nyr z-Sy3aUiW=p&-Fa-_dc(O9xm+sXU;k2oZlJqo4>EToWwOO5-bP=a!pE7OaTJ9Yz~23 zV!Db3eiJuN&I10sWUC+{0?F+lTL3?x8^dMc5J*8F_OUJo_!(*=scs8_!0b@}E=?Fw zIzS*&SW;qeB?s-5aqMU^%BZ!LHFJN%nuG^#mR-%`ol$k8$ zB?-Q;@jxejVo}99OlCfuajcXZ#C+3V+)vC49ehVC8FZte(VQlj!Z%F`Xd?fk0c>}N-j-x%%g-g;`BU7c0Nz}Y7hEtx{Qg+X&ZALRLbw8zIq z17JUMtE;sHL{CJ0f9})d!olKuu+e;fTL?>bytX-&n=5$!$of?Gh!|DTflccaPAkU~h}J<$!} zH2=q`{5AgQ$R%CxX-sCDXfeMP8)~@tO&E>Cn~XaO567l3-iJySkXr%eiXhR~9x6jT z&%fA-s_K<4L0}&+fSul_kzB`B#DGZ36QWie=$uc6073;<_u$H>d7Y{{lf;E> zAtV_zg^`K{35tIJfhB(o=!qF}d3FRxqAZfq0r~Usbq9+oBl`RE#Aj^1+KRz}D_5W} z2u*n@Is`Ua=z*R=%&Fbm`zVRvai3ob3`6kaz3MchgeUxStCV=U`!eBq|jU*-o)_0wBweOC;p&|2xC?P zDrl5;>$?wM9T{C>K2S%^Shci-`k`4%@o@GFi)tPzi@3vsTcc*SO>shJ86$#ka05<$ zX3eSCqNPXC(rb7djSn7lU)pZOHLbZWmX5>t2CS)aGos&(3QH|u1{gpJQ!)>#8UkzJK>D!pZPP*Oz1j>Q8l&`GCQmGn8frD^{hy= zK}%^82F%jDtn3WyR_h@Owl$;m3ASfScZHOXUf+M1rD2oi*Rr;qt(=+9;XYJ6(u!H4 zT(jGQR9ZEk_x;=oQ(i@N|B8RhTUf-o)Y1b+ukl&bgSCK zx%EdiuV=8o_Br}%zKx-Oxe|uC!24S5sxxIFnyr|w>9g}eiNY}YL1ai_taGaH<4PD9?7-*5)6L%4{q?oM&8!9zPp5>LG8EWG zM30Q2TAExi$q@Sz>F<5*MZ(t7IFrC-apg_L5=xKyw!@dTBR@?=t*o}hR>+sI1agXV z1^TMQ8%fVb5#bVeOuPICXdbD+CiM^iGST_rBh^Zlnc5F-PUgQg50VVJE>J>*nY;&6 zrgu)+xMO9)k9fORn2Ihs&JQ=6_Z%TgBcl)~A5xsfhKz_9%D)vr)X3l1MjxI^e719U zIg;TmYNB*wv*e+re}sZuB}VP!M43)>(<6~rch8#L*Y5lj8CALF56tS_xmiK3vq}a* zsJhwVBUM3`XE4s^ZQdMf$=m-j4B_OrH*vTeUeC}dSiI5jL|5MvdF2_NFXgFevK3Oi z7kep*{vcxnPwY~vpX65?Pa%h%nhX5|23l(SIUD@lo=# z@~_xrw846OV^3+mM-mmrnQp^4;Ycy07vYx+bnc>t`?=q>6LUSKG zUJ7|r`@2#5sm7IHs$i-FjNj01Cl%CBRH$C$2#unG-+_x@Kg$F`Qwy3&C{+qk;fg*b zj0Q$i47q@D4?!qQ=EAff5MlJ&aI=eL1RB%SK!f;NstY6g{zE}=v)Bk9qrF+S%fzDSw8}WI!-&YPxXR{94AE$ zV+y)VtPkszZqOx=TLGh%Cqyk&Fcru70wS(Qq#V^4^R*SWw^k^BGatZuR3i>R-&QoK zdZE)X^H~!tQab=oJ2{^9{;Ve9Tf6TGrrrBfj-Kc4O)>~qO(5+>dsg*D7X_7?xgbIhmN>rq@O7_Vyu@}2wF7;%6z_4>EKQ!I@? zimi_H&J*IBqDUNK%CJjN1vZPSASzPWW3I~f{K+G4y7+1KZXhz!fzR&zY1MmC`iy*G zaV-R!-g@1i7)WGp-tJm<3}~6cgt8j^n-t%(XTpAT`teTBMlo*X3^Zarcg1&prtxj` zM*aJ{HIUa$e+y*DbzKQSyDW1Vm~Yh%k3AHxm1`~&~eP(Z%UKErI}*`W^Ip+zwB zh9%moJd_ogaf;#aecV4+Q|xJXY)!Y?ncAAXiw%32q~X0Zlf;K}z9nTbYc7^j5Nwz)tpDA|M~fmVMm+!SkKDc-PMV~$#Fhb?hxseqUA9OB>LSy zV`D=V+)=8&%{^4`4XhuCxPQHkn0Nbo7%h((250U7D}#!Z{w1XPydXy;?W>iQunt!m zNqMxt8fCMHBfWS;9qVo@Tui4T_6P)V-nQyg={Qc$b8#etbf@?mC4;)Kz^5NhtJENf zkqo*$qe%?b_lx5@04xOH)3+UzxXeLRqKy@-RB)fyTeqm;A`IXYw8K>%gu%bpdjFQb zM&|BJ$Fa0eOVfJhGdD47D#3)o>4^Jh8N*x}WT|naP*$0_)iHaYmiTYeSz!FzGfALF zv&N=dqY5Q)exF0s={^LQXG|(A)}m)IO$6-CbB1#q$tVeJ-MbC<@m;i?B&HV)c&Lm? z^F3(e2@sQGM*p2<#zJ8S1lvQg;vn3}ct5R0O# zMdg`FT~hF(JO1;OiU(^pAAuUh4l35#_z4+k-xPeolUhoBemU&5b=LJ)ZAz4U4^gZ7 zA8OPOW_XaNqjHVYT4?}Qpy-;xX{}P3&llxPrzvKRSE)23{)tK}a zk8$^=y}awYMc$4@x`~v{v1DU~@qn+I8bvWzTYumeSq<`QRFaq8I^VppOBH$hy)G6A zD)Xrr+$g?G$Xuib*u?4+6&mtHN4N)yJHuqEUe0q-= z`|*IRt$UTk%gQRsqazZopICI`p=+_Hfdk!~i&ojJYYpxfKvu1n;|2Hwi!>ihJ4J3M#Bi|NERsi({$4-)6@}CDYZ{F z4_-OO&E+POD-mXqKTttTgZ`yX6m!P4dy_slYc^Y9o$yf@=P5}EIr|r!0~6TEe)u#( zYUa3)m*T?$cR}~fRZ%OKtlbH1P`#HJON5G{b+i7GEE7m}r@^zE_j^iG_4M%T7I|A{ z&x-0oi#eFV1{)~g2_7+3iRZ7HFwzmQLeqg&BXj=>geXOzlgi_j|!TM6B;w+l>r z@4013U7%X$J&*{Pq&+fx{qMTyltnHtFKd=s5bHo(v(<|lCoNtBumDo5q5HPPClQ8W zATO*7#5f)L{8yl?;jP^?1$GMxH8_Xy8F~{{j-sNXF2kU5@o>X+l@Bbwl7Gv&Ko_2h zQs1I=&yCY!DF4N*bQ?ZzVg_=$H5LV9YbY4=Om%o7uSIy3-c$R+RifbYbhikI9(nuK z^3ZEX5earFi{V1=)9Qn{r~`UyWH41Isdf2TcF7)dt*&#MD@(pb9IXxySLftp#Trf+ zT!)b!8$B$6_^Api06_I8xXZ@Q-ZwBHUXq%Yc4f=!{gBev#^*}|IW9R@UdsfHa~0Hm z-d!-ZD8>A^U3{%`Og~#!R&(->UB@MkeZiaGIl%GV*qMSN`ZH zT6RNO0)u*=Yd{qJGGkxz$1Vxp(Z*<6bG}Y4v1(;>&8o#}Md#g&Uvx9v9sfrPGczHL zGAm1S^Q48>jC!$MAGnqCYoo`@pHpi@UjruMO&DT?Z1lf8Iv5eAN?)^l_qKj}Y1;6B z*_%TSv^Rqxk(0*lz3(lZ+7|0a$TY2O1oo=kBk|}(uNGejyi2_O2UDybA!Sk&*cskZ z-V)#XR^_;5g*o2E{ZtWSVR26P z`lMI(=jQenSP^}l3N2vMQlOGv?(=alnPtO4M1wM}F+cPQx06Hj+zOv@Ba^wQk}G!l zT1HJ6dL{`zG($ zHG;e2oUWr-;lEuhHKm=k`HAWYu}BgD%JBQv65M2Gnc#W9oqh{GO<0Iw>&Nz*55mv1 z+zw_*IW4BJYfy1QaZsA0NP6w@o|ygQL;fGkGda*@%hsZrbxd9h4TI?gob zQK7IrapbVE>lF}nv^&1*zbEOJo1D|2o1o=<+I`o7FZr^tJf+Jz@N1U7k7(^0gVs5P-`OFcK%UG+2m zU@ADrJd4UFstlVkx1*IRF0b*Aj2a)Av_^heT9VwA)DPGe-;sW_tJBNC7SOClSFJPA zDVRmh{urpGnl~7Zm2zu;e~^%8%1Y zsa!4<^kdMT3ZHTc#_PC64^Z1T!gWBv3->$#7i=8*7=B_gQ8heGB!4yFF?02{(U!&n zN%ws?Nx#be7j5$pDz`c4WgXL!;gzD(+du^Lu-jNh>HthrB(QpoE?;0N3c7~96pmKwQgv>YtxdJQ@dR~oj<7+#FRT(uzDS!jnB&bDxWu^8#w2w3u6C)~S0C~4M^ZmwESZqm*CBTSvrY5KpC@x(Ter%X z;HhoQ6?)#7TVAQz8A$Q`oE+KyvYx*jsVvwWm>2kmS2jtAUH`LbLNE z6-ER6=F{Ka3gtexsrR}Xzaj4)@0e$i+t=+kti`!aKbkPbpYzZ(PjpFnNW0!fpOb7*$%C@xY8*{IVU>rmsE&QqtmyDOcRq_^u7uUyeD$Q zzPhZq%-=pnV9rllDJZ~TeYPE4_ZbYk6yG0eW*S>iwPo>LeN@VHNv&iySa1WB&m z>{H^2pnthaXK^aKnh+=BHZ!nI5GNC}*8voSZ6LBeb!iRgXpSv{L*y^91&Q}g%Wu=%ye-p>*p>n7*z zK&s`tRI$bfhbdvMTzHQQd&rHJnRDCDTL3cdKc~HAc`D_5@EC^u@!p7>+ynlKCpN{A z&#!HUv>F{=**A2E=_)Yl@n2)*Astc7#9YleS!Fu~A$XC8UVZ|c>pkv0IP@Y{6gd?9{=#uFj`EiC%<~O> z#9PXuPkK7X0ZmU%TV19fXDFK|2VOzMJdfSD!hVy1NL7`@vN=N>S^Q(p*V-2>R>|Ry z`W+s@-)ZCDr(NtWr6_lgO{biR5U3sL0Ps*`dI%w{0qi>dA zDEBP;OiXZNTwk=oF-rh3S*SIS>v7`aP;0(W@9+B?w0Ao{KXYoeJ-&pC4?@Vt{cezp zjcc=7KPG%$2f03iF4YntlTn>iT(q_*J)aWT_GaP=1t`T)eg+hn|Edxc9k5eE zut8)Bc8XwoQ;_@qWB`{~;b1CJY&7?(*YVl#w=!9T;#k{BYapBZ2^YR~S4^l+3FTg}ShP(yRC%1^iLP*HER$v%%@Dxs}|f-_F$lh>^Wne`yl38vCr z{R^cfgW}Q8RVCVoZ@?2H7wf2foQ{!Ut3u>f=AHLasbiK^r`&4!ZIa@A^_Gq6jnZ!y zm6g9`G_WcgCgA>26`tFhP}a}{be`SGIYa9MT%FlW&Dg4G^G0JSCPBj=igAo3l&5F% z=Zeii8F}tJP=^CZUs%Dj($GS+NMiCR;#<+gWIrq&lKaL(KTm0G2=NHI&K@cZx+Qs* zD4U&Jo^?R}pkATdJ2{$>Af7~;XjW;Kq?YT`I(yYO{B!9?j0#TZ-^*+fbG$hDzIM;^ z!>-wgH&Kd84eiEU8EM=l$DUDz`D(V3L%j+-YofadA+9}UAwj$EZ{-#D#z=)W9d-|b zH}r|pI(UU#51NX~-1D}b`20WFw*566`n=$M)HDR?MIB&2+ormHs#?oLAMbuU4*k*e zAhFok<)K3f<9kYbh9@{sH#9w&t zi}5%lEjH|AmwZ;K9)V_d<6=WmVS#eYzl|jcGsvL*c={`>k*K+tkwZ`K?)rhr2P)dy z?J&^_vXxh0H~QXfeTPR8{+ZFKXoG8fYlL;sB_mogfBqrYKO>i}WDuM0&u==Kc&2sykJh~SwzFR-~t*CW^(8XCcD^-v+>O?bnM`-|hBlWr$n zZSM|xi=!{54)MbM7!eV%u%LG@ic~7u`A~vycW({Q50a?Gn{GXQsF7q#88>%16KvTb z3kK2aLhZ&zN{I%&fh5wXv$Hs5VkmG*WfoDN%$!xwCS_#y7S7kVQ7k|SruUV=kjwjxFxD6o*$HX zsBL{S;1q%(_O=6Fy~0}_vt8$~jg8mGVGLpvzuY&`U&f50I>@9H0U#ix_l#{%WkqL! z%LyH!dFL0LxfmROKiHrS+#8z`tth$4wN-r5(NPB-`EM9YPT zX1rjfD-%nMZYHBAJSe)YB-qqwBrf6j#@F}9`;(&Lis(sZbryhN@;?}+#lgfS^W_Bx6O&1wDYq$NP?&L`%iiq>|qSaJ?iwl1+NQM zCvNbjccG2$Hu&sPlBG%|THj}hlr%1^KvUM&^hAFNVtR|xuxYAAU@Lh6x%k}8v#^r zK`#&?ZM+Z<4i1wN5*A+j(6L1%N!67@)LG?@89+4z-Jaq+QOp`v!JJ9))xc9kJ|JIi zx7d%HSv#8AKN8|UJ#CG$prml0G>qufQ615D>66RI6WU72>x%q2+F^G{8u=)V2BFRA zcybnbxT-b0Hm2TNYPo@kr<7d3z z6Z6{qDpRKCJ`f4UP3|flUE5y;hCw>B@UB>UPdt>DBl5OMpGio3Z>&#`Gpon_Er)2Y za@MLE^F8Ib!|g5Rs{1*3@T@$XQfAqK_?JuNK9k{TT~RdW+2E?24ex%wWx{BXWC9yN!lQKHg;9k+P5t=C5>kvKwlI zlJ^BhlFex1}w5ANao&P)Gtg%P@)+JIZ(K^)56pg)zWp6xpE$5p9&{RgA667 zYJszFq29Ey^v8+K;`RrDwElU^+)588t<>lV>{kvr@r$}K&$GI<<<#|7 zox4DsvFW`0Ky`M<8QGpmyTHIep{=%}m6cWN!tFl;OUe>Z^d^|MpkFPM48vGk=;AT- zk)eVaIO^T8Kf|Lv8yuvq-IYbGtXZML$C`lRNA#=S*OUKuJTk?TnYb-wm=rAr?0!4VP1?86x-i`~$Azul)DPe-XQP z-fcWh8CM~(l&(G7jF`UR_Qub0v~=9AmuszhPkUkTgNxwVm~H3ei`E?X4WhWyZ?uBX zcluS`*PJJv4`##te-P0-T~IBAx%Zh4r=8CflXDp_aj&0f=oQtz$BeU7zbb}ovpyc& zk0YZL($hY6CmR*xd*PO7nekyIo%m}vPIxn0CqiepJEL+fhB&uIquc+OT8QnmtFn8t zt4p-X^t8{urqvmn!trN!S?x(Np8Ni$uP?M_u`5CB&hw?5vYI8WHSHRg26babp|z^5 zPVSkuqE7AS8x8JGgIe|oIk`=@i3`xWjs}kA_HIa!60GovCb8RhuX7h*|u|7 zJJgekZ255lVjHTKJV{R{XqC~xHJYF!=17Zw_dBS~t_&4;S`VlL1`|(2Kexz9_XYup-##WDNHyEEY~5AE0RHUs%NPwEC)gwAjPh#s0O# zeTPZcGk!tA4waCCf&#wP@=%eVkBGF6H(QyvN+^B{tHQ3#O(W^X@D8w5NOWtNHoc#2 z^BHj0CFXS9{Vw`uGb;As1eU3mPjoF!$uUx)iK1qh-8P@ud2}+cc0QzUGc2QMe1wPS z9ztV!{jg+1&GK>D>4J96-cto+bVS6#W_X^^%sVzNi*i8?dk#B0yPu0$XJ!q4wyRZJ zay2c#xiWQtZ3&@~47$?*EwrOjaX7T^tsQ4QsbuU$oVgty2z{Vsbs@DJpR~+2)-WF5 z6gsM%A*vPXHgYNC^0x<}W2eh=QT2U>k9B!*ayd^!xlhHmMsFb4C+;i^yhGd|qSm)e zib?L!(lT3@^-(~Qk&|nct+o_(3LZ_Ku1u<3Y4*eubU0ivcG)db4d-`}!2L~Cg0oX& zz-T-?O(+1N_)Zw8+u)Oe{i$!w&vcrKv5`dhEif8U!RXvtHf;Z+ZF?_J=Y={bmu%(~ zS#J^EBfOf&5v6)%E0Di27KiTmM|g8ttd)yr=@RX!gM~kW#Vyg=&0k2EoDSZoSHO^~ zR_U~}m=^2g_cG{s*;q)Uba^Fi{~Pa``-hwh;uM@c4GVBVuXOXv1yMbs$2Zt$$Pv%y zbJ9H?YRC?#-Wb|Wq11zhl-~RJfbDedb74tFPfL8P-htUq)19)I&T29pfurH9iTX0megNN&BRmQQ*{1%8 zgrAWIQF)t9(y2X^)jj)?HW^WCuYPC*SeKac@tl3{91=>7ekqz2>u<4C=Nk;U4}scM-EQw6h| zAhI&9k}7!eRx_UlWCb_fHXiR%Ra|j1uzR)d`RQKN24S;4k#X(TIEeWDv?@p9TsAWV zoHKQ_Y3@pH{tQLrYM*o{XE9Z+)f_3*(fTX6Bu_a^IL_UD>MGU)#N~vVyY4vg(z-+T+$8Pft&a*`&ahxGOvy96LLC?)qS}FoC$o=NhTo zVecCN9NS5hmQ{0+wSoY9(#Ky8YVGq&UIm*NMFEB-gQJ1?)Ef8Nja}(!@{b%VpB@22 zHK=ur>MqgjaNA>=X~mi6__dr7^X&A=u9Ucuz(x)}p;*-lelZza+s$3agbK4s>&f^9fyOU>L^RwzKpXq9gL8g1w zs`qaas$>uh*eXgU17X9{xvUIZCK)YEm7$wWaP zrb!}N@kTGAIAejc{?u)(U*?2Vfc(jsg6|J?+dbGtLULUN#dm}wZ<>ZsC5T**H}m~0 zkniS7?n0RFL{V9`qWz)LUAmf8As3|IIbpxmz9+!fNQ4JS+aBV$fF~w?e)4{ihSs$h zBiUQ4Q+z*9ZgB6oN1rKcQ^)eQ!(cSR)N`gQ3~)1T6s<`3_h6e5RNm576WhFs4xhUo z(yCIU5IR9Ia^3qW6I5aA{ABcZT3pbNEkExKu#vvvc6qKDdWR_|*^e-*(A zU90Ic)fgK3(T1Z>qzp%y57KY8Px}}>+Tu=N3j(5aKo#~{p7D*H6Tn}NUuM?coATFj zdA3x7ce)XIrPOuxwW{FOlzH6gbS~l~|Aan?*tLos(CP0)DthOko2tCP&ibMEvdn7m z!*L6`-m}nm`E_4ZRA_Mun#N=ZARz#I=XmC+;buC-7H>#@Q|@$`9@%EDhf#k+~6gwtt=23c`_c8T_Li{&DTw4Wun3!h(F`96->1Q(wX{za|R;fcsGpJZLfvd zt?ZJ@lleaU)a^*I^A_|) z>%gv%#bnXS&dI2~q6@!pLVg6Ha}M>uf7C^QrF~$R4&n$KtMjXUvKvR>#~a1|uEa$p zy^W-}rQCGfe_cyuwQg6w=(0zJ4?5j*aHeoRmhOz9rt7gl33;u{T#1g**&&}vI?SR=-1bVLB z*pbae#m*|}jptjod~v7sx$~tjPd7?V@>bukjEx>0PE2TNxlU*}t5P;J{g;x2yf+F7 z(S?y8sNf=91sjL`wtVg<{3U~Nr=mw}%r7lmOP2a~V|Q~;1+HN3QQTTyB;K6(U0c{6 z;`(q)HkoZ$Q};7qI#Y?DIzgj?O9S5DuOWvMH}j*9LJCU`tFv-`fqWy*yn8!}Sj*&O zOQi4i@a{&M)JvIpCeir?X80~E<;t_8{lszS_eckE7a-XtUp=3jwu50OW$BvZMnBV5 zgE_U}?smYnfcJrSX<->MFI;+@AEy-EBx|BAjA$lnkeESBK7esD!9~73;)IKDjlevJ zf@OnB4KH|~+9MV2qf18tLNUYS=4-6Ef(kBV2xHo`%@1g{2MeiBYo4*UyniGeIEOuS z{1tKs=XZc}PCI24+M`{7d~)DTZ}C(^S z==3PfVcFh!$jqd+zpR^EYy|W0@HJ9@Hh*6yJ!OvBv^W#B@!&|WiZ|$VPlBj?Uq1O- zbz4U=YGRG=MhkgZ{Dnbj{ch2O& zM4f=Zk_}!#dp^Cs#d+;hmJJlf?g?7FL>e4%{!$=zKoyN|!|ODGm8LrqkO~`vK@9Ws zmM%jTT7A%=U1q=C2l30GJ56nEZQFJ%q3r=QJpw05;6|DKD~hr18%fDX!ZuW61z=d+ zRi)@tj|}jWE!ESdfKtuCgj}DA* zuczR&TVVV$;M0=aE@az%N_*l@ST%4X>C#Abp7q&RUFo3aZG#%{NIcHPm7z!caE0Y~ zbRM3tz%h?025HEH6`VcL8#4BuU(N5@?|pcpmfj z^I_&evo8viB3lR)79KPN4}DRyUz1ery#DmrXw6X9sb~F7$|E0GlCRypD26WPOW+1j z{MFx&R^fu7*MpgFxyyv!iX-z4#CxB7_y+%^zWFo({poemYwb26KLi_^`ouo=xOZO- zVzUswbkol$8Sv_DuD8#Cc`N03-ULS?Ke2m5hh2EtI0S=bQ)m?zEMB<47(D`fe2~=F z6pdIVHoD6prZrvxkAncXr(sF^X)G3(lxm}Gi}#jllD60P7wRPnP}1; z$+6A6_Qd~?S*IrYCH}bU-Z)6pEZ&TM#x&LRiH@Kn^1thM=RkxdJWd?jA*mb@P)Hp> z(jd_iXu*D|59>q;h3-t1g&nX0#Y02jo$?=#4|9Mk-$-*fBDwuHG9&4~z&#f$w4 z43(gC{m@~~JM0L{Hc=vcQ8BhsHrc@!F_5Q&@V;g^1B~NTw#xq9{|yyxyk8N7UT{UmSI9sQD_UY_NwG1JHST)Jh0hTU7Fgc zH?!AgO60q|c&uEeAWv^&fi9jR2;vq6b#|Y%eq;oHN2?s!bTHq(d;0<7u-yhw&v5f3 z`;saFC&q33^UH~~#Egfvlw^<(Z@(+S(|;(Ar!B6{B7X#MO_Uq8MfZjWB7^PkjSD+ zB_e_kM*5}$oB}%J>>=Wp_up<3_IL%iB1mm9SZVH~c%K)Y9oO?G5OHb)R1W3SLfAM+8fx= zgh?9%ZyQ2<8=N=qqKHLLQ92N2xoDq6U7D0MU(%uGtZJ=lk?zhNERC20R~1nQC)%2T z;z9$t@8c-KY*BT7>vql;T;Q#A@;Q71Yu=CoryJgX_4ozvjU=F(G7s?UYMXMGXJz3* zVaLw}!PUXvLxKhwXj@*|n)6T)+kQXbM$vA~&vhw8mWl=Bc^*Rxu8mU9&!A4QbI(`e z*ya;GP;|q~*m(5jN;Gbf|5ziRT1T{a3cw!QN8JNBHwpIRc6`Xrzm^HuWx}*|EF}F6 zIO9GAL@gRfIpBv}bKgQ27C^CXFHAxR48OsJfUskOp)Z>4zX7w+o`cStKZanF>`G$@D;+o zJ+UXQZ<7C#JhKTD7N*O246aN7*qNq=H*yMybI6%-S*LNrPp_NDx%uQh`)tkX z?k7#IiVKC={a^-^X?9=*km6Ztc%g#Eq>+w+F$coJd8B*|+{wgUCn$Z0fRt~B1BPkj z8!#o^pZticDWwVq<7UMSZTLl~bu$5{p}b&iuZzdd`z~gw4lt{A+zk?WBvpGnXa_g@zyDv+kvRf~^bY?rLd{*|OW1iIuT~#YG zRoU{|3Vc=@7Re#&7wR=c(upr8q;*1l7zOMWWeph%sPq(UuDmTT<*oygq;#m^X9Xk! zN5KpGtg8xd4jt=uw8Iq41%lKgy>WMk+(%izYR^T;aORtRC5+GE zWMPq_`~4FC>vM9+r!pzYbKAW?$B*daI%tT*(nfcb&F<5zbTZx<`@UpkNS`_O*yZXg z$D|R7S^nEuCpUi5Bg}Y}Qm;)9j|{pl)Jo07<@VGbQhxLK)2W)7JG$1z7N2vCP0!Qe zbR`++qF>PN&(x|=bY@Si5N24HQT2K7C6V2pyImF;uvdlwvb#oYKz*fs9En` zSLv+=ylRxS2G=>pnjC8|Gn*-F2_obXuToxqXDb>P1jLMqM5GDgu3ZGBj^ z3Je(B-KB`62Z1tYbRa%ZpRzQ3#nBQ=!H~Nv{db3o-kpw#xzYoDUDC3}jE*9m($jN+ zbwrlNL~_&o)5j>eIBgSV_@_^MIk*tu&U9PI`7SX%2bM4ZEF4>294wNLN}qNGaAe!c zmgFoqWykL9-DzwwTgYZuEj@0-4{$@TF-eb+OqP0Y%`q_4iJvQ^mExJ^h?b{UZ^1)J zn~uY);-d;Yq%h-~jJkRLCmA6MSt|X~1cXuT{I``mr3*er%JZ62ls%E!VMml7UsVX> z>J!{reDg_CHuq?NLeq_xV+;K*c+tU^CyxP%upT6)xPw%d=gKcPfg@@9dCl$EXfX>i zr475C0>m>*Rk$)VpUT#xQ?&^PXzy)sBCfxo{OTG@^+O5X8G43c2yWR^bH_tXiA-fdN={%B04@P^ZZ`fdM+Aj3j=-(3F@|( z?(YAHl;;q53VJX~0QmPWzt_;pKkWiS1ndg{bcw{f@Dk?$c223fz$ic6a~Qg?VHAL( zWYhZ#wO|)c5A5#vTYo_AoCbB_c=o5S)BS~(f#((i+#L#n*6VdMzx)AL{ulqZPUb#* zGZ*syf1LS0`3O|lupdPWSwvmGs67W^CU6t19)*y2fNgvMkL!5A6`-6@y9faNi)N=N zJ^TxIP@aAtkmLT}yv9E|qV@XQ3K!T9lumJOx~S1m&hbsX63RdSFP)4$@~74%vj1`B z=T?QX7AF*&HSc_}v1z_{tpmT>_iw>f`8`SYY7!VtTWmPGZ>p*&65XSMnt+O(5&W(= z^87X|0MTc1Pjmr|CnA=~sIVef>~orXvA+8m>TL+TdP;#^$YHVz*GvISbBIJ46J> z;SXCNe`nv|^J;C$dTlXfSWi`z@+kyW2$F@xj9>pYi6u^~x$&?}`JMb2j@oH-DdJ~h^mPdM`M|ilsG~It{Kd)1r{_siy&Yi;k zGzs1PSMt8iMDNPlXd7uIe$04MXPi!po!Jz+-+xW`OWZSw- zz#R!9?e_q6Po8?V)RLO42uPvBoOKqLpVuDA}LHh zv4-+Q(!6WMnR`4m%L<~E9~-DRE{h`*M-|*afkN7CW9WxSV~vbjwnym+qFoX`wIY%* zs=AZq0*tPba75CWe0-L)z(Oqi&5;MhhA3*xSJc&MkO1K;VwkfvK2|j$DF&1?EC-n>KD`tS0EAt-FoCa3i{(>|GW3c{PorrTSgR+NoUgi z@83o9S6^%%Ts<55|L2$2{PjT5fAg*ze_ZmzGd_sc%nnY&EO;jiMCy^8Sgy!3 Libmemif example app(s) use memif default socket file: /run/vpp/memif.sock. Run VPP and icmpr-epoll example (default example when running in container). -> Other examples work similar to icmpr-epoll. Brief explanation can be found in [Readme](README.md). +> Other examples work similar to icmpr-epoll. Brief explanation can be found in [Examples readme](README.md) file. VPP-side config: ``` @@ -183,3 +183,24 @@ DBGvpp# ping 192.168.1.2 Statistics: 5 sent, 4 received, 20% packet loss ``` + +#### icmp_responder master icmp_responder slave + +> Example apps can only repond to ping. This setup creates connection between two applications using libmemif. Traffic functionality is the same as when connection to VPP. App can receive ARP/ICMP request and transmit response, but can not send ARP/ICMP request. + +Run two instances of icmpr-epoll example. +> If not running in container, make sure folder /run/vpp/ exists before creating memif master. +Instance 1 will be in master mode, instance 2 in slave mode. +instance 1: +``` +conn 0 1 +``` +instance 2: +``` +conn 0 0 +``` +In 2 seconds, both instances should print connected! message: +``` +INFO: memif connected! +``` +Check peer interface names using show command. diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..75e729d --- /dev/null +++ b/examples/README.md @@ -0,0 +1,19 @@ +## Examples + +After build, root folder will contain scripts linking binary examples with library (same name as example apps). These scripts can be executed to run example apps without installing the library. Example apps binaries can be found in _libs_ filder. To run binaries directly, make sure that libmemif library is installed. + +#### Run in container +ligato/libmemif-sample-service image contains built and installed libmemf. To run different examples, override docker CMD to start container in bash: +``` +# docker run -it --entrypoint=/bin/bash -i --rm --name icmp-responder --hostname icmp-responder --privileged -v "/run/vpp/:/run/vpp/" ligato/libmemif-sample-service +``` +Current WORKDIR is set to root repository directory. Example apps can be run from this directory (a script linking binary with library), or browse to ./.libs folder and execute binary directly. + +#### Example apps + +1. [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. +2. [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. +3. [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. diff --git a/src/libmemif.h b/src/libmemif.h index 3242c24..94ab4c9 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -15,95 +15,107 @@ *------------------------------------------------------------------ */ +/** @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 */ +/*! Error codes */ typedef enum { - MEMIF_ERR_SUCCESS = 0, /* success */ + 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 */ + 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 + 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_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_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; -/* 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 */ +/** + * @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 */ +/** 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) */ +/** 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 */ +/** 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) + 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 + 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); @@ -111,8 +123,17 @@ typedef int (memif_connection_update_t) (memif_conn_handle_t conn, void *private @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 @@ -129,13 +150,13 @@ typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx, ui */ typedef struct { - uint8_t *socket_filename; /* default = /run/vpp/memif.sock */ - uint8_t secret[24]; /* optional (interface authentication) */ + 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 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; @@ -144,6 +165,13 @@ typedef struct 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 @@ -157,8 +185,15 @@ typedef struct uint32_t data_len; void *data; } memif_buffer_t; +/** @} */ + +/** + * @defgroup MEMIF_DETAILS Memif details structs + * + * @{ + */ -/** \briefMemif queue details +/** \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 @@ -206,23 +241,20 @@ typedef struct uint8_t link_up_down; /* 1 = up, 0 = down */ } memif_details_t; +/** @} */ -/* memif receive mode */ -typedef enum -{ - MEMIF_RX_MODE_INTERRUPT = 0, - MEMIF_RX_MODE_POLLING -} memif_rx_mode_t; +/** + * @defgroup API_CALLS Api calls + * + * @{ + */ /** \biref Memif get queue event file descriptor @param conn - memif connection handle @param qid - queue id - @param fd - returns event file descriptor + @param[out] fd - returns event file descriptor - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - connection handle points to NULL - MEMIF_ERR_QID - invalid queue id + \return memif_err_t */ int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *fd); @@ -232,20 +264,16 @@ int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *fd); @param rx_mode - receive mode @param qid - queue id - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - connection handle points to NULL - MEMIF_ERR_QID - invalid 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 + @param err_code - error code - converts error code to error message + Converts error code to error message. - return - error string + \return Error string */ char *memif_strerror (int err_code); @@ -255,11 +283,7 @@ char *memif_strerror (int err_code); @param buf - buffer containing details strings @param buflen - length of buffer - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - connection handle points to NULL - MEMIF_ERR_NOBUF_DET - privided buffer is not long enough - + \return memif_err_t */ int memif_get_details (memif_conn_handle_t conn, memif_details_t *md, char *buf, ssize_t buflen); @@ -274,22 +298,19 @@ int memif_get_details (memif_conn_handle_t conn, memif_details_t *md, user application, all file descriptors and event types will be passed in this callback to user application - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) - MEMIF_ERR_FILE_LIMIT - system open file limit reached - MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached - MEMIF_ERR_BAD_FD - invalid fd + 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. - 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 + Free libmemif internal allocations. + + \return 0 */ int memif_cleanup (); @@ -301,31 +322,20 @@ int memif_cleanup (); @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. - - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) - MEMIF_ERR_ACCES - permission denied - MEMIF_ERR_FILE_LIMIT - system open file limit reached - MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached - MEMIF_ERR_AGAIN - fd is not socket, or operation would block - MEMIF_ERR_BAD_FD - invalid fd - MEMIF_ERR_NOMEM - out of memory - TODO: MEMIF_ERR_INVAL_ARG - invalid argument - MEMIF_ERR_CONN - connection already exists on this handle - MEMIF_ERR_FILE_NOT_SOCK - file specified by socket filename exists, - but it's not a socket - - slave-mode - start timer that will send events to timerfd. if this fd is passed to memif_control_fd_handler + 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. + 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. - 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, @@ -337,42 +347,30 @@ int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args, @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 - - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) - MEMIF_ERR_ACCES - permission denied - MEMIF_ERR_FILE_LIMIT - system open file limit reached - MEMIF_ERR_PROC_FILE_LIMIT - process open file limit reached - MEMIF_ERR_AGAIN - fd is not socket, or operation would block - MEMIF_ERR_BAD_FD - invalid fd - MEMIF_ERR_NOMEM - out of memory - MEMIF_ERR_NO_SHMFD - missing shared memory fd - MEMIF_ERR_COOKIE - invalid cookie on ring - - fd type - timerfd - every disconnected memif in slave mode will request connection - listener - call accept on this fd and pass new fd to user - control - handle socket messaging (internal connection establishment) + 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 - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - handle points to NULL - MEMIF_ERR_SYSCALL - unspecified syscall error (compile with -DMEMIF_DBG) - MEMIF_ERR_BAD_FD - invalid fd 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); @@ -383,11 +381,7 @@ int memif_delete (memif_conn_handle_t *conn); @param count - number of memif buffers to allocate @param count_out - returns number of allocated buffers - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - handle points to NULL - MEMIF_ERR_DISCONNECTED - not conencted - MEMIF_ERR_NOBUF_RING - ring buffer full + \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); @@ -399,11 +393,7 @@ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, @param count - number of memif buffers to free @param count_out - returns number of freed buffers - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - handle points to NULL - MEMIF_ERR_DISCONNECTED - not conencted - MEMIF_ERR_QID - invalid queue id + \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); @@ -413,13 +403,9 @@ int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, @param qid - number indentifying queue @param bufs - memif buffers @param count - number of memif buffers to transmit - @param count_out - returns number of transmitted buffers + @param tx - returns number of transmitted buffers - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - handle points to NULL - MEMIF_ERR_DISCONNECTED - not conencted - MEMIF_ERR_QID - invalid queue id + \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); @@ -429,14 +415,9 @@ int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, @param qid - number indentifying queue @param bufs - memif buffers @param count - number of memif buffers to receive - @param count_out - returns number of received buffers - - return - MEMIF_ERR_SUCCESS - no error - MEMIF_ERR_NOCONN - handle points to NULL - MEMIF_ERR_DISCONNECTED - not conencted - MEMIF_ERR_NOBUF - not enough buffers provided - MEMIF_ERR_QID - invalid queue id + @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); @@ -444,14 +425,13 @@ int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, /** \brief Memif poll event @param timeout - timeout in seconds - passive event polling + 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_SUCCESS - no error - MEMIF_ERR_QID - invalid queue id + \return memif_err_t */ int memif_poll_event (int timeout); +/** @} */ #endif /* _LIBMEMIF_H_ */ From caef29840b67a0cb2256a331558d49b7a64db018 Mon Sep 17 00:00:00 2001 From: JakubGrajciar Date: Wed, 16 Aug 2017 16:34:58 +0200 Subject: [PATCH 48/63] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aa9191a..a38182e 100644 --- a/README.md +++ b/README.md @@ -67,8 +67,10 @@ commands: rx-mode - set queue rx mode ``` -[Example setup](examples/ExampleSetup.md) contains instructions on how to set up conenction between icmpr-epoll example app and VPP-memif. Brief description and instructions on how to run other example apps can be found in [Examples readme](examples/README.md) +[Example setup](examples/ExampleSetup.md) contains instructions on how to set up conenction between icmpr-epoll example app and VPP-memif. #### Next steps -Continue to [Next steps](docs/NextSteps.md). +[Examples](examples/README.md) +[Build instructions](docs/BuildInstructions.md) +[Getting started](docs/GettingStarted.md) From f79eca5e0c3accdfe9495e283355575f2bab38ac Mon Sep 17 00:00:00 2001 From: JakubGrajciar Date: Wed, 16 Aug 2017 16:36:10 +0200 Subject: [PATCH 49/63] Update and rename NextSteps.md to BuildInstructions.md --- docs/{NextSteps.md => BuildInstructions.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename docs/{NextSteps.md => BuildInstructions.md} (98%) diff --git a/docs/NextSteps.md b/docs/BuildInstructions.md similarity index 98% rename from docs/NextSteps.md rename to docs/BuildInstructions.md index 4c4ffb6..9bb59b9 100644 --- a/docs/NextSteps.md +++ b/docs/BuildInstructions.md @@ -1,4 +1,4 @@ -## Next Steps +## Build Instructions Install dependencies ``` From 12f03ab16bba2252d49481e5a96520f337c0a25f Mon Sep 17 00:00:00 2001 From: JakubGrajciar Date: Wed, 16 Aug 2017 16:41:01 +0200 Subject: [PATCH 50/63] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a38182e..a25b3ee 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,6 @@ commands: #### Next steps -[Examples](examples/README.md) -[Build instructions](docs/BuildInstructions.md) -[Getting started](docs/GettingStarted.md) +- [Build instructions](docs/BuildInstructions.md) +- [Examples](examples/README.md) +- [Getting started](docs/GettingStarted.md) From 8fd57740f653fa0191b9af376fadbe215126da5f Mon Sep 17 00:00:00 2001 From: JakubGrajciar Date: Wed, 16 Aug 2017 16:44:05 +0200 Subject: [PATCH 51/63] Update README.md --- examples/README.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/README.md b/examples/README.md index 75e729d..bbd663b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -9,11 +9,8 @@ ligato/libmemif-sample-service image contains built and installed libmemf. To ru ``` Current WORKDIR is set to root repository directory. Example apps can be run from this directory (a script linking binary with library), or browse to ./.libs folder and execute binary directly. -#### Example apps - -1. [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. -2. [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. -3. [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. +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. From ea1348db4bd8005ab1fd4b7fd82fc2be79dc889f Mon Sep 17 00:00:00 2001 From: JakubGrajciar Date: Wed, 16 Aug 2017 17:06:31 +0200 Subject: [PATCH 52/63] Update README.md --- README.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a25b3ee..0cccfb3 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,7 @@ Shared memory packet interface (memif) provides high performance packet transmit ## Quickstart -For information on how to use libmemif API, please refer to [Getting started](docs/GettingStarted.md). - -#### Run in container - -Install [docker](https://docs.docker.com/engine/installation) engine. +This setup will run libmemif ICMP responder example app in container. Install [docker](https://docs.docker.com/engine/installation) engine. Useful link: [Docker documentation](https://docs.docker.com/get-started). Pull image: @@ -67,10 +63,10 @@ commands: rx-mode - set queue rx mode ``` -[Example setup](examples/ExampleSetup.md) contains instructions on how to set up conenction between icmpr-epoll example app and VPP-memif. +Continue with [Example setup](examples/ExampleSetup.md) which contains instructions on how to set up conenction between icmpr-epoll example app and VPP-memif. #### Next steps -- [Build instructions](docs/BuildInstructions.md) -- [Examples](examples/README.md) -- [Getting started](docs/GettingStarted.md) +- [Build instructions](docs/BuildInstructions.md) Instructions on how to build/install libmemif. +- [Examples](examples/README.md) More example apps presenting different features. +- [Getting started](docs/GettingStarted.md) Introduction to libmemif API. Explaining library usage in custom app. From 333ed8f966fbc288e495525057e272b58e103d97 Mon Sep 17 00:00:00 2001 From: Rastislav Szabo Date: Tue, 22 Aug 2017 09:00:40 +0200 Subject: [PATCH 53/63] Revert "Merge branch 'master' into dev" This reverts commit 7b6aca0a261a67a6e17e941912863be8a817b560, reversing changes made to ea1348db4bd8005ab1fd4b7fd82fc2be79dc889f. --- Makefile.am | 1 - README.md | 1 - examples/icmp_responder/icmp_proto.c | 1 + examples/icmp_responder/main.c | 4 +- src/libmemif.h | 4 +- src/main.c | 112 +-------------------------- 6 files changed, 9 insertions(+), 114 deletions(-) diff --git a/Makefile.am b/Makefile.am index c6e6680..5a118db 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,7 +34,6 @@ unit_test_SOURCES = test/unit_test.c \ unit_test_CPPFLAGS = $(AM_CPPFLAGS) -Itest -Isrc -DMEMIF_UNIT_TEST -g $(CHECK_CFLAGS) unit_test_LDADD = $(CHECK_LIBS) - # # main lib # diff --git a/README.md b/README.md index 7d1fc68..0cccfb3 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,6 @@ Example application will start in debug mode. Output should look like this: ICMP_Responder:add_epoll_fd:204: fd 0 added to epoll MEMIF_DEBUG:src/main.c:memif_init:383: app name: ICMP_Responder ICMP_Responder:add_epoll_fd:204: fd 4 added to epoll - LIBMEMIF EXAMPLE APP: ICMP_Responder (debug) ============================== libmemif version: 1.0 (debug) diff --git a/examples/icmp_responder/icmp_proto.c b/examples/icmp_responder/icmp_proto.c index 1901b48..f8e69c4 100644 --- a/examples/icmp_responder/icmp_proto.c +++ b/examples/icmp_responder/icmp_proto.c @@ -215,6 +215,7 @@ resolve_packet (void *in_pck, ssize_t in_size, { eah = (struct ether_arp *) (in_pck + *out_size); *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr); + } else if (eh->ether_type == 0x0008) { diff --git a/examples/icmp_responder/main.c b/examples/icmp_responder/main.c index f484349..f62af55 100644 --- a/examples/icmp_responder/main.c +++ b/examples/icmp_responder/main.c @@ -69,7 +69,6 @@ /* maximum tx/rx memif buffers */ #define MAX_MEMIF_BUFS 256 - typedef struct { uint16_t index; @@ -146,6 +145,7 @@ print_memif_details () default: printf ("unknown\n"); break; + } printf ("\tsocket filename: %s\n",(char *) md.socket_filename); printf ("\tsocket filename: %s\n",(char *) md.socket_filename); printf ("\trx queues:\n"); @@ -266,7 +266,7 @@ icmpr_free () err = memif_cleanup (); if (err != MEMIF_ERR_SUCCESS) INFO ("memif_delete: %s", memif_strerror (err)); - + return 0; } diff --git a/src/libmemif.h b/src/libmemif.h index de17ae2..94ab4c9 100644 --- a/src/libmemif.h +++ b/src/libmemif.h @@ -134,6 +134,7 @@ typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx, ui * * @{ */ + /** \brief Memif connection arguments @param socket_filename - socket filename @param secret - otional parameter used as interface autenthication @@ -359,13 +360,13 @@ int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args, \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 @@ -391,6 +392,7 @@ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, @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, diff --git a/src/main.c b/src/main.c index 8f50a0f..b4761a0 100644 --- a/src/main.c +++ b/src/main.c @@ -386,105 +386,11 @@ free_list_elt_ctx (memif_list_elt_t *list, uint16_t len, memif_connection_t *ctx return -1; } -static char memif_buf[MAX_ERRBUF_LEN]; - -const char* memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ - "Success.", - /* MEMIF_ERR_SYSCALL */ - "Unspecified syscall error (build with -DMEMIF_DBG or make debug).", - /* MEMIF_ERR_ACCES */ - "Permission to resoure denied.", - /* MEMIF_ERR_FILE_LIMIT */ - "System limit on total numer of open files reached.", - /* MEMIF_ERR_PROC_FILE_LIMIT */ - "Per-process limit on total number of open files reached.", - /* MEMIF_ERR_ALREADY */ - "Connection already requested.", - /* MEMIF_ERR_AGAIN */ - "File descriptor refers to file other than socket, or operation would block.", - /* MEMIF_ERR_BAD_FD */ - "Bad file descriptor.", - /* MEMIF_ERR_NOMEM */ - "Out of memory.", - /* MEMIF_ERR_INVAL_ARG */ - "Invalid argument.", - /* MEMIF_ERR_NOCONN */ - "Memif connection handle does not point to existing conenction", - /* MEMIF_ERR_CONN */ - "Memif connection handle points to existing connection", - /* MEMIF_ERR_CB_FDUPDATE */ - "User defined callback memif_control_fd_update_t returned error", - /* MEMIF_ERR_FILE_NOT_SOCK */ - "File specified by socket filename exists and is not socket.", - /* MEMIF_ERR_NO_SHMFD */ - "Missing shared memory file descriptor. (internal error)", - /* MEMIF_ERR_COOKIE */ - "Invalid cookie on ring. (internal error)", - /* MEMIF_ERR_NOBUF_RING */ - "Ring buffer full.", - /* MEMIF_ERR_NOBUF */ - "Not enough memif buffers. There are unreceived data in shared memory.", - /* MEMIF_ERR_NOBUF_DET */ - "Not enough space for memif details in suplied buffer. String data might be malformed.", - /* MEMIF_ERR_INT_WRITE */ - "Send interrupt error.", - /* MEMIF_ERR_MFMSG */ - "Malformed message received on control channel.", - /* MEMIF_ERR_PROTO */ - "Incompatible memory interface protocol version.", - /* MEMIF_ERR_ID */ - "Unmatched interface id.", - /* MEMIF_ERR_ACCSLAVE */ - "Slave cannot accept connection reqest.", - /* MEMIF_ERR_ALRCONN */ - "Interface is already connected.", - /* MEMIF_ERR_MODE */ - "Mode mismatch.", - /* MEMIF_ERR_SECRET */ - "Secret mismatch.", - /* MEMIF_ERR_NOSECRET */ - "Secret required.", - /* MEMIF_ERR_MAXREG */ - "Limit on total number of regions reached.", - /* MEMIF_ERR_MAXRING */ - "Limit on total number of ring reached.", - /* MEMIF_ERR_NO_INTFD */ - "Missing interrupt file descriptor. (internal error)", - /* MEMIF_ERR_DISCONNECT */ - "Interface received disconnect request.", - /* MEMIF_ERR_DISCONNECTED */ - "Interface is disconnected.", - /* MEMIF_ERR_UNKNOWN_MSG */ - "Unknown message type received on control channel. (internal error)" - }; - -#define MEMIF_ERR_UNDEFINED "undefined error" - -char * -memif_strerror (int err_code) -{ - if (err_code > ERRLIST_LEN) - { - strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED)); - memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0'; - } - else - { - strncpy (memif_buf, memif_errlist[err_code], strlen (memif_errlist[err_code])); - memif_buf[strlen (memif_errlist[err_code])] = '\0'; - } - return memif_buf; -} - -#define DBG_TX_BUF (0) -#define DBG_RX_BUF (1) - -#if MEMIF_DBG_SHM static void memif_control_fd_update_register (memif_control_fd_update_t *cb) { libmemif_main_t *lm = &libmemif_main; - lm->control_fd_update = cb;+ + lm->control_fd_update = cb; } int memif_init (memif_control_fd_update_t *on_control_fd_update, char *app_name) @@ -816,7 +722,6 @@ memif_create (memif_conn_handle_t *c, memif_conn_args_t *args, if ((index = add_list_elt (&list_elt, &lm->control_list, &lm->control_list_len)) < 0) { err = MEMIF_ERR_NOMEM; - goto error; } } @@ -843,7 +748,6 @@ memif_control_fd_handler (int fd, uint8_t events) int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */ uint16_t num; memif_list_elt_t *e = NULL; - memif_connection_t *conn; libmemif_main_t *lm = &libmemif_main; if (fd == lm->timerfd) @@ -1003,14 +907,7 @@ memif_poll_event (int timeout) err = memif_control_fd_handler (evt.data.fd, events); return err; } - - return MEMIF_ERR_SUCCESS; /* 0 */ - -error: - if (sockfd > 0) - close (sockfd); - sockfd = -1; - return err; + return 0; } static void @@ -1197,7 +1094,6 @@ int memif_connect1 (memif_connection_t *c) { libmemif_main_t *lm = &libmemif_main; - memif_region_t *mr = c->regions; memif_queue_t *mq; int i; @@ -1279,12 +1175,10 @@ memif_init_regions_and_queues (memif_connection_t *conn) if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); - /* if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) return memif_syscall_error_handler (errno); */ - if ((ftruncate (r->fd, r->region_size)) == -1) return memif_syscall_error_handler (errno); @@ -1602,6 +1496,7 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, uint16_t mask = (1 << mq->log2_ring_size) - 1; memif_buffer_t *b0, *b1; *rx = 0; + uint64_t b; ssize_t r = read (mq->int_fd, &b, sizeof (b)); if ((r == -1) && (errno != EAGAIN)) @@ -1680,7 +1575,6 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t *md, return MEMIF_ERR_NOCONN; int err = MEMIF_ERR_SUCCESS, i; - ssize_t l0, l1, total_l; l0 = 0; From fdc0e88d583cdb473f16461070de7c6f66b42282 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Mon, 28 Aug 2017 10:21:25 +0200 Subject: [PATCH 54/63] readme update + unused def deleted --- examples/ExampleSetup.md | 16 ++++++++-------- src/socket.c | 2 -- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/ExampleSetup.md b/examples/ExampleSetup.md index d248b0d..a02506d 100644 --- a/examples/ExampleSetup.md +++ b/examples/ExampleSetup.md @@ -9,13 +9,13 @@ Run VPP and icmpr-epoll example (default example when running in container). VPP-side config: ``` -# create memif id 0 master -# set int state memif0/0 up -# set int ip address memif0/0 192.168.1.1/24 +DBGvpp# create memif id 0 master +DBGvpp# set int state memif0/0 up +DBGvpp# set int ip address memif0/0 192.168.1.1/24 ``` icmpr-epoll: ``` -# conn 0 0 +conn 0 0 ``` Memif in slave mode will try to connect every 2 seconds. If connection establishment is successfull, a message will show. ``` @@ -99,13 +99,13 @@ cpu { ``` VPP-side config: ``` -# create memif id 0 slave rx-queues 2 tx-queues 2 -# set int state memif0/0 up -# set int ip address memif0/0 192.168.1.1/24 +DBGvpp# create memif id 0 slave rx-queues 2 tx-queues 2 +DBGvpp# set int state memif0/0 up +DBGvpp# set int ip address memif0/0 192.168.1.1/24 ``` icmpr-epoll: ``` -# conn 0 1 +conn 0 1 ``` When connection is established a message will print: ``` diff --git a/src/socket.c b/src/socket.c index fbb5023..1bdb672 100644 --- a/src/socket.c +++ b/src/socket.c @@ -35,8 +35,6 @@ #define memif_min(a,b) ((a < b) ? (a) : (b)) -#define APP_NAME "ICMP_Responder" - /* sends msg to socket */ static_fn int memif_msg_send (int fd, memif_msg_t *msg, int afd) From 94f0395c7ba0b66cab8f4e5ce623a91a7fdc02aa Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 30 Aug 2017 10:01:13 +0200 Subject: [PATCH 55/63] doxygen (make doc) --- Makefile.am | 6 + doxygen.conf | 2427 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 2433 insertions(+) create mode 100644 doxygen.conf diff --git a/Makefile.am b/Makefile.am index 5a118db..48e4bb8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -21,6 +21,12 @@ AM_CPPFLAGS = -g -DMEMIF_DBG -DICMP_DBG release: $(MAKE) AM_CPPFLAGS="-O3" +.PHONY: doc +doc: + @echo Building doxygen documentation... + doxygen doxygen.conf + @echo Doxygen documentation built in docs directory. + # # unit_test # diff --git a/doxygen.conf b/doxygen.conf new file mode 100644 index 0000000..503eef5 --- /dev/null +++ b/doxygen.conf @@ -0,0 +1,2427 @@ +# Doxyfile 1.8.11 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project. +# +# All text after a double hash (##) is considered a comment and is placed in +# front of the TAG it is preceding. +# +# All text after a single hash (#) is considered a comment and will be ignored. +# The format is: +# TAG = value [value, ...] +# For lists, items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (\" \"). + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all text +# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv +# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv +# for the list of possible encodings. +# The default value is: UTF-8. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by +# double-quotes, unless you are using Doxywizard) that should identify the +# project for which the documentation is generated. This name is used in the +# title of most generated pages and in a few other places. +# The default value is: My Project. + +PROJECT_NAME = "Libmemif" + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. This +# could be handy for archiving the generated documentation or if some version +# control system is used. + +PROJECT_NUMBER = 1.0 + +# Using the PROJECT_BRIEF tag one can provide an optional one line description +# for a project that appears at the top of each page and should give viewer a +# quick idea about the purpose of the project. Keep the description short. + +PROJECT_BRIEF = Shared memory packet interface library + +# With the PROJECT_LOGO tag one can specify a logo or an icon that is included +# in the documentation. The maximum height of the logo should not exceed 55 +# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy +# the logo to the output directory. + +PROJECT_LOGO = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path +# into which the generated documentation will be written. If a relative path is +# entered, it will be relative to the location where doxygen was started. If +# left blank the current directory will be used. + +OUTPUT_DIRECTORY = ./docs + +# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub- +# directories (in 2 levels) under the output directory of each output format and +# will distribute the generated files over these directories. Enabling this +# option can be useful when feeding doxygen a huge amount of source files, where +# putting all generated files in the same directory would otherwise causes +# performance problems for the file system. +# The default value is: NO. + +CREATE_SUBDIRS = NO + +# If the ALLOW_UNICODE_NAMES tag is set to YES, doxygen will allow non-ASCII +# characters to appear in the names of generated files. If set to NO, non-ASCII +# characters will be escaped, for example _xE3_x81_x84 will be used for Unicode +# U+3044. +# The default value is: NO. + +ALLOW_UNICODE_NAMES = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# Possible values are: Afrikaans, Arabic, Armenian, Brazilian, Catalan, Chinese, +# Chinese-Traditional, Croatian, Czech, Danish, Dutch, English (United States), +# Esperanto, Farsi (Persian), Finnish, French, German, Greek, Hungarian, +# Indonesian, Italian, Japanese, Japanese-en (Japanese with English messages), +# Korean, Korean-en (Korean with English messages), Latvian, Lithuanian, +# Macedonian, Norwegian, Persian (Farsi), Polish, Portuguese, Romanian, Russian, +# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish, +# Ukrainian and Vietnamese. +# The default value is: English. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES, doxygen will include brief member +# descriptions after the members that are listed in the file and class +# documentation (similar to Javadoc). Set to NO to disable this. +# The default value is: YES. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES, doxygen will prepend the brief +# description of a member or function before the detailed description +# +# Note: If both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. +# The default value is: YES. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator that is +# used to form the text in various listings. Each string in this list, if found +# as the leading text of the brief description, will be stripped from the text +# and the result, after processing the whole list, is used as the annotated +# text. Otherwise, the brief description is used as-is. If left blank, the +# following values are used ($name is automatically replaced with the name of +# the entity):The $name class, The $name widget, The $name file, is, provides, +# specifies, contains, represents, a, an and the. + +ABBREVIATE_BRIEF = + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# doxygen will generate a detailed section even if there is only a brief +# description. +# The default value is: NO. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. +# The default value is: NO. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES, doxygen will prepend the full path +# before files name in the file list and in the header files. If set to NO the +# shortest path that makes the file name unique will be used +# The default value is: YES. + +FULL_PATH_NAMES = YES + +# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path. +# Stripping is only done if one of the specified strings matches the left-hand +# part of the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the path to +# strip. +# +# Note that you can specify absolute paths here, but also relative paths, which +# will be relative from the directory where doxygen is started. +# This tag requires that the tag FULL_PATH_NAMES is set to YES. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of the +# path mentioned in the documentation of a class, which tells the reader which +# header file to include in order to use a class. If left blank only the name of +# the header file containing the class definition is used. Otherwise one should +# specify the list of include paths that are normally passed to the compiler +# using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but +# less readable) file names. This can be useful is your file systems doesn't +# support long names like on DOS, Mac, or CD-ROM. +# The default value is: NO. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then doxygen will interpret the +# first line (until the first dot) of a Javadoc-style comment as the brief +# description. If set to NO, the Javadoc-style will behave just like regular Qt- +# style comments (thus requiring an explicit @brief command for a brief +# description.) +# The default value is: NO. + +JAVADOC_AUTOBRIEF = NO + +# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first +# line (until the first dot) of a Qt-style comment as the brief description. If +# set to NO, the Qt-style will behave just like regular Qt-style comments (thus +# requiring an explicit \brief command for a brief description.) +# The default value is: NO. + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a +# multi-line C++ special comment block (i.e. a block of //! or /// comments) as +# a brief description. This used to be the default behavior. The new default is +# to treat a multi-line C++ comment block as a detailed description. Set this +# tag to YES if you prefer the old behavior instead. +# +# Note that setting this tag to YES also means that rational rose comments are +# not recognized any more. +# The default value is: NO. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the +# documentation from any documented member that it re-implements. +# The default value is: YES. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES then doxygen will produce a new +# page for each member. If set to NO, the documentation of a member will be part +# of the file/class/namespace that contains it. +# The default value is: NO. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. Doxygen +# uses this value to replace tabs by spaces in code fragments. +# Minimum value: 1, maximum value: 16, default value: 4. + +TAB_SIZE = 4 + +# This tag can be used to specify a number of aliases that act as commands in +# the documentation. An alias has the form: +# name=value +# For example adding +# "sideeffect=@par Side Effects:\n" +# will allow you to put the command \sideeffect (or @sideeffect) in the +# documentation, which will result in a user-defined paragraph with heading +# "Side Effects:". You can put \n's in the value part of an alias to insert +# newlines. + +ALIASES = + +# This tag can be used to specify a number of word-keyword mappings (TCL only). +# A mapping has the form "name=value". For example adding "class=itcl::class" +# will allow you to use the command class in the itcl::class meaning. + +TCL_SUBST = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources +# only. Doxygen will then generate output that is more tailored for C. For +# instance, some of the names that are used will be different. The list of all +# members will be omitted, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_FOR_C = YES + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java or +# Python sources only. Doxygen will then generate output that is more tailored +# for that language. For instance, namespaces will be presented as packages, +# qualified scopes will look different, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources. Doxygen will then generate output that is tailored for Fortran. +# The default value is: NO. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for VHDL. +# The default value is: NO. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it +# parses. With this tag you can assign which parser to use for a given +# extension. Doxygen has a built-in mapping, but you can override or extend it +# using this tag. The format is ext=language, where ext is a file extension, and +# language is one of the parsers supported by doxygen: IDL, Java, Javascript, +# C#, C, C++, D, PHP, Objective-C, Python, Fortran (fixed format Fortran: +# FortranFixed, free formatted Fortran: FortranFree, unknown formatted Fortran: +# Fortran. In the later case the parser tries to guess whether the code is fixed +# or free formatted code, this is the default for Fortran type files), VHDL. For +# instance to make doxygen treat .inc files as Fortran files (default is PHP), +# and .f files as C (default is Fortran), use: inc=Fortran f=C. +# +# Note: For files without extension you can use no_extension as a placeholder. +# +# Note that for custom extensions you also need to set FILE_PATTERNS otherwise +# the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments +# according to the Markdown format, which allows for more readable +# documentation. See http://daringfireball.net/projects/markdown/ for details. +# The output of markdown processing is further processed by doxygen, so you can +# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in +# case of backward compatibilities issues. +# The default value is: YES. + +MARKDOWN_SUPPORT = YES + +# When enabled doxygen tries to link words that correspond to documented +# classes, or namespaces to their corresponding documentation. Such a link can +# be prevented in individual cases by putting a % sign in front of the word or +# globally by setting AUTOLINK_SUPPORT to NO. +# The default value is: YES. + +AUTOLINK_SUPPORT = YES + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should set this +# tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); +# versus func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. +# The default value is: NO. + +BUILTIN_STL_SUPPORT = NO + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. +# The default value is: NO. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip (see: +# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen +# will parse them like normal C++ but will assume all classes use public instead +# of private inheritance when no explicit protection keyword is present. +# The default value is: NO. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate +# getter and setter methods for a property. Setting this option to YES will make +# doxygen to replace the get and set methods by a property in the documentation. +# This will only work if the methods are indeed getting or setting a simple +# type. If this is not the case, or you want to show the methods anyway, you +# should set this option to NO. +# The default value is: YES. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. +# The default value is: NO. + +DISTRIBUTE_GROUP_DOC = NO + +# If one adds a struct or class to a group and this option is enabled, then also +# any nested class or struct is added to the same group. By default this option +# is disabled and one has to add nested compounds explicitly via \ingroup. +# The default value is: NO. + +GROUP_NESTED_COMPOUNDS = NO + +# Set the SUBGROUPING tag to YES to allow class member groups of the same type +# (for instance a group of public functions) to be put as a subgroup of that +# type (e.g. under the Public Functions section). Set it to NO to prevent +# subgrouping. Alternatively, this can be done per class using the +# \nosubgrouping command. +# The default value is: YES. + +SUBGROUPING = YES + +# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and unions +# are shown inside the group in which they are included (e.g. using \ingroup) +# instead of on a separate page (for HTML and Man pages) or section (for LaTeX +# and RTF). +# +# Note that this feature does not work in combination with +# SEPARATE_MEMBER_PAGES. +# The default value is: NO. + +INLINE_GROUPED_CLASSES = NO + +# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and unions +# with only public data fields or simple typedef fields will be shown inline in +# the documentation of the scope in which they are defined (i.e. file, +# namespace, or group documentation), provided this scope is documented. If set +# to NO, structs, classes, and unions are shown on a separate page (for HTML and +# Man pages) or section (for LaTeX and RTF). +# The default value is: NO. + +INLINE_SIMPLE_STRUCTS = NO + +# When TYPEDEF_HIDES_STRUCT tag is enabled, a typedef of a struct, union, or +# enum is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically be +# useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. +# The default value is: NO. + +TYPEDEF_HIDES_STRUCT = NO + +# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This +# cache is used to resolve symbols given their name and scope. Since this can be +# an expensive process and often the same symbol appears multiple times in the +# code, doxygen keeps a cache of pre-resolved symbols. If the cache is too small +# doxygen will become slower. If the cache is too large, memory is wasted. The +# cache size is given by this formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range +# is 0..9, the default is 0, corresponding to a cache size of 2^16=65536 +# symbols. At the end of a run doxygen will report the cache usage and suggest +# the optimal cache size from a speed point of view. +# Minimum value: 0, maximum value: 9, default value: 0. + +LOOKUP_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in +# documentation are documented, even if no documentation was available. Private +# class members and static file members will be hidden unless the +# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES. +# Note: This will also disable the warnings about undocumented members that are +# normally produced when WARNINGS is set to YES. +# The default value is: NO. + +EXTRACT_ALL = YES + +# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will +# be included in the documentation. +# The default value is: NO. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_PACKAGE tag is set to YES, all members with package or internal +# scope will be included in the documentation. +# The default value is: NO. + +EXTRACT_PACKAGE = NO + +# If the EXTRACT_STATIC tag is set to YES, all static members of a file will be +# included in the documentation. +# The default value is: NO. + +EXTRACT_STATIC = NO + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES, classes (and structs) defined +# locally in source files will be included in the documentation. If set to NO, +# only classes defined in header files are included. Does not have any effect +# for Java sources. +# The default value is: YES. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. If set to YES, local methods, +# which are defined in the implementation section but not in the interface are +# included in the documentation. If set to NO, only methods in the interface are +# included. +# The default value is: NO. + +EXTRACT_LOCAL_METHODS = NO + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base name of +# the file that contains the anonymous namespace. By default anonymous namespace +# are hidden. +# The default value is: NO. + +EXTRACT_ANON_NSPACES = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all +# undocumented members inside documented classes or files. If set to NO these +# members will be included in the various overviews, but no documentation +# section is generated. This option has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. If set +# to NO, these classes will be included in the various overviews. This option +# has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend +# (class|struct|union) declarations. If set to NO, these declarations will be +# included in the documentation. +# The default value is: NO. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any +# documentation blocks found inside the body of a function. If set to NO, these +# blocks will be appended to the function's detailed documentation block. +# The default value is: NO. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation that is typed after a +# \internal command is included. If the tag is set to NO then the documentation +# will be excluded. Set it to YES to include the internal documentation. +# The default value is: NO. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file +# names in lower-case letters. If set to YES, upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. +# The default value is: system dependent. + +CASE_SENSE_NAMES = YES + +# If the HIDE_SCOPE_NAMES tag is set to NO then doxygen will show members with +# their full class and namespace scopes in the documentation. If set to YES, the +# scope will be hidden. +# The default value is: NO. + +HIDE_SCOPE_NAMES = NO + +# If the HIDE_COMPOUND_REFERENCE tag is set to NO (default) then doxygen will +# append additional text to a page's title, such as Class Reference. If set to +# YES the compound reference will be hidden. +# The default value is: NO. + +HIDE_COMPOUND_REFERENCE= NO + +# If the SHOW_INCLUDE_FILES tag is set to YES then doxygen will put a list of +# the files that are included by a file in the documentation of that file. +# The default value is: YES. + +SHOW_INCLUDE_FILES = YES + +# If the SHOW_GROUPED_MEMB_INC tag is set to YES then Doxygen will add for each +# grouped member an include statement to the documentation, telling the reader +# which file to include in order to use the member. +# The default value is: NO. + +SHOW_GROUPED_MEMB_INC = NO + +# If the FORCE_LOCAL_INCLUDES tag is set to YES then doxygen will list include +# files with double quotes in the documentation rather than with sharp brackets. +# The default value is: NO. + +FORCE_LOCAL_INCLUDES = NO + +# If the INLINE_INFO tag is set to YES then a tag [inline] is inserted in the +# documentation for inline members. +# The default value is: YES. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES then doxygen will sort the +# (detailed) documentation of file and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. +# The default value is: YES. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief +# descriptions of file, namespace and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. Note that +# this will also influence the order of the classes in the class list. +# The default value is: NO. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the +# (brief and detailed) documentation of class members so that constructors and +# destructors are listed first. If set to NO the constructors will appear in the +# respective orders defined by SORT_BRIEF_DOCS and SORT_MEMBER_DOCS. +# Note: If SORT_BRIEF_DOCS is set to NO this option is ignored for sorting brief +# member documentation. +# Note: If SORT_MEMBER_DOCS is set to NO this option is ignored for sorting +# detailed member documentation. +# The default value is: NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the hierarchy +# of group names into alphabetical order. If set to NO the group names will +# appear in their defined order. +# The default value is: NO. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by +# fully-qualified names, including namespaces. If set to NO, the class list will +# be sorted only by class name, not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the alphabetical +# list. +# The default value is: NO. + +SORT_BY_SCOPE_NAME = NO + +# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to do proper +# type resolution of all parameters of a function it will reject a match between +# the prototype and the implementation of a member function even if there is +# only one candidate or it is obvious which candidate to choose by doing a +# simple string match. By disabling STRICT_PROTO_MATCHING doxygen will still +# accept a match between prototype and implementation in such cases. +# The default value is: NO. + +STRICT_PROTO_MATCHING = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or disable (NO) the todo +# list. This list is created by putting \todo commands in the documentation. +# The default value is: YES. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or disable (NO) the test +# list. This list is created by putting \test commands in the documentation. +# The default value is: YES. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or disable (NO) the bug +# list. This list is created by putting \bug commands in the documentation. +# The default value is: YES. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or disable (NO) +# the deprecated list. This list is created by putting \deprecated commands in +# the documentation. +# The default value is: YES. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional documentation +# sections, marked by \if ... \endif and \cond +# ... \endcond blocks. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines that the +# initial value of a variable or macro / define can have for it to appear in the +# documentation. If the initializer consists of more lines than specified here +# it will be hidden. Use a value of 0 to hide initializers completely. The +# appearance of the value of individual variables and macros / defines can be +# controlled using \showinitializer or \hideinitializer command in the +# documentation regardless of this setting. +# Minimum value: 0, maximum value: 10000, default value: 30. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated at +# the bottom of the documentation of classes and structs. If set to YES, the +# list will mention the files that were used to generate the documentation. +# The default value is: YES. + +SHOW_USED_FILES = YES + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. This +# will remove the Files entry from the Quick Index and from the Folder Tree View +# (if specified). +# The default value is: YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the Namespaces +# page. This will remove the Namespaces entry from the Quick Index and from the +# Folder Tree View (if specified). +# The default value is: YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command command input-file, where command is the value of the +# FILE_VERSION_FILTER tag, and input-file is the name of an input file provided +# by doxygen. Whatever the program writes to standard output is used as the file +# version. For an example see the documentation. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed +# by doxygen. The layout file controls the global structure of the generated +# output files in an output format independent way. To create the layout file +# that represents doxygen's defaults, run doxygen with the -l option. You can +# optionally specify a file name after the option, if omitted DoxygenLayout.xml +# will be used as the name of the layout file. +# +# Note that if you run doxygen from a directory containing a file called +# DoxygenLayout.xml, doxygen will parse it automatically even if the LAYOUT_FILE +# tag is left empty. + +LAYOUT_FILE = + +# The CITE_BIB_FILES tag can be used to specify one or more bib files containing +# the reference definitions. This must be a list of .bib files. The .bib +# extension is automatically appended if omitted. This requires the bibtex tool +# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info. +# For LaTeX the style of the bibliography can be controlled using +# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the +# search path. See also \cite for info how to create references. + +CITE_BIB_FILES = + +#--------------------------------------------------------------------------- +# Configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated to +# standard output by doxygen. If QUIET is set to YES this implies that the +# messages are off. +# The default value is: NO. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated to standard error (stderr) by doxygen. If WARNINGS is set to YES +# this implies that the warnings are on. +# +# Tip: Turn warnings on while writing the documentation. +# The default value is: YES. + +WARNINGS = YES + +# If the WARN_IF_UNDOCUMENTED tag is set to YES then doxygen will generate +# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag +# will automatically be disabled. +# The default value is: YES. + +WARN_IF_UNDOCUMENTED = YES + +# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some parameters +# in a documented function, or documenting parameters that don't exist or using +# markup commands wrongly. +# The default value is: YES. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that +# are documented, but have no documentation for their parameters or return +# value. If set to NO, doxygen will only warn about wrong or incomplete +# parameter documentation, but not about the absence of documentation. +# The default value is: NO. + +WARN_NO_PARAMDOC = NO + +# If the WARN_AS_ERROR tag is set to YES then doxygen will immediately stop when +# a warning is encountered. +# The default value is: NO. + +WARN_AS_ERROR = NO + +# The WARN_FORMAT tag determines the format of the warning messages that doxygen +# can produce. The string should contain the $file, $line, and $text tags, which +# will be replaced by the file and line number from which the warning originated +# and the warning text. Optionally the format may contain $version, which will +# be replaced by the version of the file (if it could be obtained via +# FILE_VERSION_FILTER) +# The default value is: $file:$line: $text. + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning and error +# messages should be written. If left blank the output is written to standard +# error (stderr). + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# Configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag is used to specify the files and/or directories that contain +# documented source files. You may enter file names like myfile.cpp or +# directories like /usr/src/myproject. Separate the files or directories with +# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING +# Note: If this tag is empty the current directory is searched. + +INPUT = ./src/libmemif.h + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses +# libiconv (or the iconv built into libc) for the transcoding. See the libiconv +# documentation (see: http://www.gnu.org/software/libiconv) for the list of +# possible encodings. +# The default value is: UTF-8. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and +# *.h) to filter out the source-files in the directories. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# read by doxygen. +# +# If left blank the following patterns are tested:*.c, *.cc, *.cxx, *.cpp, +# *.c++, *.java, *.ii, *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h, +# *.hh, *.hxx, *.hpp, *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc, +# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f, *.for, *.tcl, +# *.vhd, *.vhdl, *.ucf, *.qsf, *.as and *.js. + +FILE_PATTERNS = + +# The RECURSIVE tag can be used to specify whether or not subdirectories should +# be searched for input files as well. +# The default value is: NO. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should be +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. +# +# Note that relative paths are relative to the directory from which doxygen is +# run. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or +# directories that are symbolic links (a Unix file system feature) are excluded +# from the input. +# The default value is: NO. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories use the pattern */test/* + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or directories +# that contain example code fragments that are included (see the \include +# command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and +# *.h) to filter out the source-files in the directories. If left blank all +# files are included. + +EXAMPLE_PATTERNS = + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude commands +# irrespective of the value of the RECURSIVE tag. +# The default value is: NO. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or directories +# that contain images that are to be included in the documentation (see the +# \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command: +# +# +# +# where is the value of the INPUT_FILTER tag, and is the +# name of an input file. Doxygen will then use the output that the filter +# program writes to standard output. If FILTER_PATTERNS is specified, this tag +# will be ignored. +# +# Note that the filter must not add or remove lines; it is applied before the +# code is scanned, but not when the output code is generated. If lines are added +# or removed, the anchors will not be placed correctly. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# properly processed by doxygen. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: pattern=filter +# (like *.cpp=my_cpp_filter). See INPUT_FILTER for further information on how +# filters are used. If the FILTER_PATTERNS tag is empty or if none of the +# patterns match the file name, INPUT_FILTER is applied. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# properly processed by doxygen. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will also be used to filter the input files that are used for +# producing the source files to browse (i.e. when SOURCE_BROWSER is set to YES). +# The default value is: NO. + +FILTER_SOURCE_FILES = NO + +# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file +# pattern. A pattern will override the setting for FILTER_PATTERN (if any) and +# it is also possible to disable source filtering for a specific pattern using +# *.ext= (so without naming a filter). +# This tag requires that the tag FILTER_SOURCE_FILES is set to YES. + +FILTER_SOURCE_PATTERNS = + +# If the USE_MDFILE_AS_MAINPAGE tag refers to the name of a markdown file that +# is part of the input, its contents will be placed on the main page +# (index.html). This can be useful if you have a project on for instance GitHub +# and want to reuse the introduction page also for the doxygen output. + +USE_MDFILE_AS_MAINPAGE = + +#--------------------------------------------------------------------------- +# Configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will be +# generated. Documented entities will be cross-referenced with these sources. +# +# Note: To get rid of all source code in the generated output, make sure that +# also VERBATIM_HEADERS is set to NO. +# The default value is: NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body of functions, +# classes and enums directly into the documentation. +# The default value is: NO. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES will instruct doxygen to hide any +# special comment blocks from generated source code fragments. Normal C, C++ and +# Fortran comments will always remain visible. +# The default value is: YES. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES then for each documented +# function all documented functions referencing it will be listed. +# The default value is: NO. + +REFERENCED_BY_RELATION = NO + +# If the REFERENCES_RELATION tag is set to YES then for each documented function +# all documented entities called/used by that function will be listed. +# The default value is: NO. + +REFERENCES_RELATION = NO + +# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set +# to YES then the hyperlinks from functions in REFERENCES_RELATION and +# REFERENCED_BY_RELATION lists will link to the source code. Otherwise they will +# link to the documentation. +# The default value is: YES. + +REFERENCES_LINK_SOURCE = YES + +# If SOURCE_TOOLTIPS is enabled (the default) then hovering a hyperlink in the +# source code will show a tooltip with additional information such as prototype, +# brief description and links to the definition and documentation. Since this +# will make the HTML file larger and loading of large files a bit slower, you +# can opt to disable this feature. +# The default value is: YES. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +SOURCE_TOOLTIPS = YES + +# If the USE_HTAGS tag is set to YES then the references to source code will +# point to the HTML generated by the htags(1) tool instead of doxygen built-in +# source browser. The htags tool is part of GNU's global source tagging system +# (see http://www.gnu.org/software/global/global.html). You will need version +# 4.8.6 or higher. +# +# To use it do the following: +# - Install the latest version of global +# - Enable SOURCE_BROWSER and USE_HTAGS in the config file +# - Make sure the INPUT points to the root of the source tree +# - Run doxygen as normal +# +# Doxygen will invoke htags (and that will in turn invoke gtags), so these +# tools must be available from the command line (i.e. in the search path). +# +# The result: instead of the source browser generated by doxygen, the links to +# source code will now point to the output of htags. +# The default value is: NO. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set the YES then doxygen will generate a +# verbatim copy of the header file for each class for which an include is +# specified. Set to NO to disable this. +# See also: Section \class. +# The default value is: YES. + +VERBATIM_HEADERS = YES + +# If the CLANG_ASSISTED_PARSING tag is set to YES then doxygen will use the +# clang parser (see: http://clang.llvm.org/) for more accurate parsing at the +# cost of reduced performance. This can be particularly helpful with template +# rich C++ code for which doxygen's built-in parser lacks the necessary type +# information. +# Note: The availability of this option depends on whether or not doxygen was +# generated with the -Duse-libclang=ON option for CMake. +# The default value is: NO. + +CLANG_ASSISTED_PARSING = NO + +# If clang assisted parsing is enabled you can provide the compiler with command +# line options that you would normally use when invoking the compiler. Note that +# the include paths will already be set by doxygen for the files and directories +# specified with INPUT and INCLUDE_PATH. +# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES. + +CLANG_OPTIONS = + +#--------------------------------------------------------------------------- +# Configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index of all +# compounds will be generated. Enable this if the project contains a lot of +# classes, structs, unions or interfaces. +# The default value is: YES. + +ALPHABETICAL_INDEX = YES + +# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in +# which the alphabetical index list will be split. +# Minimum value: 1, maximum value: 20, default value: 5. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all classes will +# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag +# can be used to specify a prefix (or a list of prefixes) that should be ignored +# while generating the index headers. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output +# The default value is: YES. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. If a +# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of +# it. +# The default directory is: html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each +# generated HTML page (for example: .htm, .php, .asp). +# The default value is: .html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a user-defined HTML header file for +# each generated HTML page. If the tag is left blank doxygen will generate a +# standard header. +# +# To get valid HTML the header file that includes any scripts and style sheets +# that doxygen needs, which is dependent on the configuration options used (e.g. +# the setting GENERATE_TREEVIEW). It is highly recommended to start with a +# default header using +# doxygen -w html new_header.html new_footer.html new_stylesheet.css +# YourConfigFile +# and then modify the file new_header.html. See also section "Doxygen usage" +# for information on how to generate the default header that doxygen normally +# uses. +# Note: The header is subject to change so you typically have to regenerate the +# default header when upgrading to a newer version of doxygen. For a description +# of the possible markers and block names see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a user-defined HTML footer for each +# generated HTML page. If the tag is left blank doxygen will generate a standard +# footer. See HTML_HEADER for more information on how to generate a default +# footer and what special commands can be used inside the footer. See also +# section "Doxygen usage" for information on how to generate the default footer +# that doxygen normally uses. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading style +# sheet that is used by each HTML page. It can be used to fine-tune the look of +# the HTML output. If left blank doxygen will generate a default style sheet. +# See also section "Doxygen usage" for information on how to generate the style +# sheet that doxygen normally uses. +# Note: It is recommended to use HTML_EXTRA_STYLESHEET instead of this tag, as +# it is more robust and this tag (HTML_STYLESHEET) will in the future become +# obsolete. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_STYLESHEET = + +# The HTML_EXTRA_STYLESHEET tag can be used to specify additional user-defined +# cascading style sheets that are included after the standard style sheets +# created by doxygen. Using this option one can overrule certain style aspects. +# This is preferred over using HTML_STYLESHEET since it does not replace the +# standard style sheet and is therefore more robust against future updates. +# Doxygen will copy the style sheet files to the output directory. +# Note: The order of the extra style sheet files is of importance (e.g. the last +# style sheet in the list overrules the setting of the previous ones in the +# list). For an example see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_STYLESHEET = + +# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or +# other source files which should be copied to the HTML output directory. Note +# that these files will be copied to the base HTML output directory. Use the +# $relpath^ marker in the HTML_HEADER and/or HTML_FOOTER files to load these +# files. In the HTML_STYLESHEET file, use the file name only. Also note that the +# files will be copied as-is; there are no commands or markers available. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_FILES = + +# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen +# will adjust the colors in the style sheet and background images according to +# this color. Hue is specified as an angle on a colorwheel, see +# http://en.wikipedia.org/wiki/Hue for more information. For instance the value +# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300 +# purple, and 360 is red again. +# Minimum value: 0, maximum value: 359, default value: 220. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_HUE = 220 + +# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of the colors +# in the HTML output. For a value of 0 the output will use grayscales only. A +# value of 255 will produce the most vivid colors. +# Minimum value: 0, maximum value: 255, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_SAT = 100 + +# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to the +# luminance component of the colors in the HTML output. Values below 100 +# gradually make the output lighter, whereas values above 100 make the output +# darker. The value divided by 100 is the actual gamma applied, so 80 represents +# a gamma of 0.8, The value 220 represents a gamma of 2.2, and 100 does not +# change the gamma. +# Minimum value: 40, maximum value: 240, default value: 80. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_GAMMA = 80 + +# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML +# page will contain the date and time when the page was generated. Setting this +# to YES can help to show when doxygen was last run and thus if the +# documentation is up to date. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_TIMESTAMP = NO + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_DYNAMIC_SECTIONS = NO + +# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries +# shown in the various tree structured indices initially; the user can expand +# and collapse entries dynamically later on. Doxygen will expand the tree to +# such a level that at most the specified number of entries are visible (unless +# a fully collapsed tree already exceeds this amount). So setting the number of +# entries 1 will produce a full collapsed tree by default. 0 is a special value +# representing an infinite number of entries and will result in a full expanded +# tree by default. +# Minimum value: 0, maximum value: 9999, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_INDEX_NUM_ENTRIES = 100 + +# If the GENERATE_DOCSET tag is set to YES, additional index files will be +# generated that can be used as input for Apple's Xcode 3 integrated development +# environment (see: http://developer.apple.com/tools/xcode/), introduced with +# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a +# Makefile in the HTML output directory. Running make will produce the docset in +# that directory and running make install will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at +# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html +# for more information. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_DOCSET = NO + +# This tag determines the name of the docset feed. A documentation feed provides +# an umbrella under which multiple documentation sets from a single provider +# (such as a company or product suite) can be grouped. +# The default value is: Doxygen generated docs. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# This tag specifies a string that should uniquely identify the documentation +# set bundle. This should be a reverse domain-name style string, e.g. +# com.mycompany.MyDocSet. Doxygen will append .docset to the name. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# The DOCSET_PUBLISHER_ID tag specifies a string that should uniquely identify +# the documentation publisher. This should be a reverse domain-name style +# string, e.g. com.mycompany.MyDocSet.documentation. +# The default value is: org.doxygen.Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_ID = org.doxygen.Publisher + +# The DOCSET_PUBLISHER_NAME tag identifies the documentation publisher. +# The default value is: Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_NAME = Publisher + +# If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three +# additional HTML index files: index.hhp, index.hhc, and index.hhk. The +# index.hhp is a project file that can be read by Microsoft's HTML Help Workshop +# (see: http://www.microsoft.com/en-us/download/details.aspx?id=21138) on +# Windows. +# +# The HTML Help Workshop contains a compiler that can convert all HTML output +# generated by doxygen into a single compiled HTML file (.chm). Compiled HTML +# files are now used as the Windows 98 help format, and will replace the old +# Windows help format (.hlp) on all Windows platforms in the future. Compressed +# HTML files also contain an index, a table of contents, and you can search for +# words in the documentation. The HTML workshop also contains a viewer for +# compressed HTML files. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_HTMLHELP = NO + +# The CHM_FILE tag can be used to specify the file name of the resulting .chm +# file. You can add a path in front of the file if the result should not be +# written to the html output directory. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_FILE = + +# The HHC_LOCATION tag can be used to specify the location (absolute path +# including file name) of the HTML help compiler (hhc.exe). If non-empty, +# doxygen will try to run the HTML help compiler on the generated index.hhp. +# The file has to be specified with full path. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +HHC_LOCATION = + +# The GENERATE_CHI flag controls if a separate .chi index file is generated +# (YES) or that it should be included in the master .chm file (NO). +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +GENERATE_CHI = NO + +# The CHM_INDEX_ENCODING is used to encode HtmlHelp index (hhk), content (hhc) +# and project file content. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_INDEX_ENCODING = + +# The BINARY_TOC flag controls whether a binary table of contents is generated +# (YES) or a normal table of contents (NO) in the .chm file. Furthermore it +# enables the Previous and Next buttons. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members to +# the table of contents of the HTML help documentation and to the tree view. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and +# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated that +# can be used as input for Qt's qhelpgenerator to generate a Qt Compressed Help +# (.qch) of the generated HTML documentation. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify +# the file name of the resulting .qch file. The path specified is relative to +# the HTML output folder. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help +# Project output. For more information please see Qt Help Project / Namespace +# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace). +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_NAMESPACE = org.doxygen.Project + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt +# Help Project output. For more information please see Qt Help Project / Virtual +# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual- +# folders). +# The default value is: doc. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_VIRTUAL_FOLDER = doc + +# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom +# filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the +# custom filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this +# project's filter section matches. Qt Help Project / Filter Attributes (see: +# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_SECT_FILTER_ATTRS = + +# The QHG_LOCATION tag can be used to specify the location of Qt's +# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the +# generated .qhp file. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHG_LOCATION = + +# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files will be +# generated, together with the HTML files, they form an Eclipse help plugin. To +# install this plugin and make it available under the help contents menu in +# Eclipse, the contents of the directory containing the HTML and XML files needs +# to be copied into the plugins directory of eclipse. The name of the directory +# within the plugins directory should be the same as the ECLIPSE_DOC_ID value. +# After copying Eclipse needs to be restarted before the help appears. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_ECLIPSEHELP = NO + +# A unique identifier for the Eclipse help plugin. When installing the plugin +# the directory name containing the HTML and XML files should also have this +# name. Each documentation set should have its own identifier. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_ECLIPSEHELP is set to YES. + +ECLIPSE_DOC_ID = org.doxygen.Project + +# If you want full control over the layout of the generated HTML pages it might +# be necessary to disable the index and replace it with your own. The +# DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) at top +# of each HTML page. A value of NO enables the index and the value YES disables +# it. Since the tabs in the index contain the same information as the navigation +# tree, you can set this option to YES if you also set GENERATE_TREEVIEW to YES. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +DISABLE_INDEX = NO + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. If the tag +# value is set to YES, a side panel will be generated containing a tree-like +# index structure (just like the one that is generated for HTML Help). For this +# to work a browser that supports JavaScript, DHTML, CSS and frames is required +# (i.e. any modern browser). Windows users are probably better off using the +# HTML help feature. Via custom style sheets (see HTML_EXTRA_STYLESHEET) one can +# further fine-tune the look of the index. As an example, the default style +# sheet generated by doxygen has an example that shows how to put an image at +# the root of the tree instead of the PROJECT_NAME. Since the tree basically has +# the same information as the tab index, you could consider setting +# DISABLE_INDEX to YES when enabling this option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_TREEVIEW = NO + +# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that +# doxygen will group on one line in the generated HTML documentation. +# +# Note that a value of 0 will completely suppress the enum values from appearing +# in the overview section. +# Minimum value: 0, maximum value: 20, default value: 4. +# This tag requires that the tag GENERATE_HTML is set to YES. + +ENUM_VALUES_PER_LINE = 4 + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be used +# to set the initial width (in pixels) of the frame in which the tree is shown. +# Minimum value: 0, maximum value: 1500, default value: 250. +# This tag requires that the tag GENERATE_HTML is set to YES. + +TREEVIEW_WIDTH = 250 + +# If the EXT_LINKS_IN_WINDOW option is set to YES, doxygen will open links to +# external symbols imported via tag files in a separate window. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +EXT_LINKS_IN_WINDOW = NO + +# Use this tag to change the font size of LaTeX formulas included as images in +# the HTML documentation. When you change the font size after a successful +# doxygen run you need to manually remove any form_*.png images from the HTML +# output directory to force them to be regenerated. +# Minimum value: 8, maximum value: 50, default value: 10. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_FONTSIZE = 10 + +# Use the FORMULA_TRANPARENT tag to determine whether or not the images +# generated for formulas are transparent PNGs. Transparent PNGs are not +# supported properly for IE 6.0, but are supported on all modern browsers. +# +# Note that when changing this option you need to delete any form_*.png files in +# the HTML output directory before the changes have effect. +# The default value is: YES. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_TRANSPARENT = YES + +# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see +# http://www.mathjax.org) which uses client side Javascript for the rendering +# instead of using pre-rendered bitmaps. Use this if you do not have LaTeX +# installed or if you want to formulas look prettier in the HTML output. When +# enabled you may also need to install MathJax separately and configure the path +# to it using the MATHJAX_RELPATH option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +USE_MATHJAX = NO + +# When MathJax is enabled you can set the default output format to be used for +# the MathJax output. See the MathJax site (see: +# http://docs.mathjax.org/en/latest/output.html) for more details. +# Possible values are: HTML-CSS (which is slower, but has the best +# compatibility), NativeMML (i.e. MathML) and SVG. +# The default value is: HTML-CSS. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_FORMAT = HTML-CSS + +# When MathJax is enabled you need to specify the location relative to the HTML +# output directory using the MATHJAX_RELPATH option. The destination directory +# should contain the MathJax.js script. For instance, if the mathjax directory +# is located at the same level as the HTML output directory, then +# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax +# Content Delivery Network so you can quickly see the result without installing +# MathJax. However, it is strongly recommended to install a local copy of +# MathJax from http://www.mathjax.org before deployment. +# The default value is: http://cdn.mathjax.org/mathjax/latest. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest + +# The MATHJAX_EXTENSIONS tag can be used to specify one or more MathJax +# extension names that should be enabled during MathJax rendering. For example +# MATHJAX_EXTENSIONS = TeX/AMSmath TeX/AMSsymbols +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_EXTENSIONS = + +# The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces +# of code that will be used on startup of the MathJax code. See the MathJax site +# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an +# example see the documentation. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_CODEFILE = + +# When the SEARCHENGINE tag is enabled doxygen will generate a search box for +# the HTML output. The underlying search engine uses javascript and DHTML and +# should work on any modern browser. Note that when using HTML help +# (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET) +# there is already a search function so this one should typically be disabled. +# For large projects the javascript based search engine can be slow, then +# enabling SERVER_BASED_SEARCH may provide a better solution. It is possible to +# search using the keyboard; to jump to the search box use + S +# (what the is depends on the OS and browser, but it is typically +# , /