From 7c5c40db2a8d71a857ae63b6238cfac6e257da6d Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 30 Aug 2017 10:13:25 +0200 Subject: Shared memory packet interface (memif) library Change-Id: I5097462ae85acd705f19e92517c01094dba7565f Signed-off-by: Jakub Grajciar --- extras/libmemif/src/socket.c | 883 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 883 insertions(+) create mode 100644 extras/libmemif/src/socket.c (limited to 'extras/libmemif/src/socket.c') diff --git a/extras/libmemif/src/socket.c b/extras/libmemif/src/socket.c new file mode 100644 index 00000000..9c9b3a8d --- /dev/null +++ b/extras/libmemif/src/socket.c @@ -0,0 +1,883 @@ +/* + *------------------------------------------------------------------ + * 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. + *------------------------------------------------------------------ + */ + +#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)) + +/* sends msg to socket */ +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, err = MEMIF_ERR_SUCCESS; /* 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) + err = memif_syscall_error_handler (errno); + DBG ("Message type %u sent", msg->type); + return err; +} + +/* response from memif master - master is ready to handle next message */ +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); + + memset (&e->msg, 0, sizeof (e->msg)); + e->msg.type = MEMIF_MSG_TYPE_ACK; + e->fd = -1; + + e->next = NULL; + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return MEMIF_ERR_SUCCESS; /* 0 */ + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ +} + +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; + 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, lm->app_name, strlen (lm->app_name)); + + /* msg hello is not enqueued but sent directly, + because it is the first msg to be sent */ + return memif_msg_send (fd, &msg, -1); +} + +/* send id and secret (optional) for interface identification */ +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); + 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; + 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_ERR_SUCCESS; /* 0 */ + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + 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) +{ + /* 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)); + 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; + 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 MEMIF_ERR_SUCCESS; /* 0 */ + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 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)); + 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; + + /* TODO: support multiple rings */ + memif_queue_t *mq; + if (dir == MEMIF_RING_M2S) + mq = &c->rx_queues[index]; + else + mq = &c->tx_queues[index]; + + e->fd = mq->int_fd; + ar->index = index; + ar->offset = mq->offset; + 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; + + e->next = NULL; + if (c->msg_queue == NULL) + { + c->msg_queue = e; + return MEMIF_ERR_SUCCESS; /* 0 */ + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ +} + +/* used as connection request from slave */ +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); + + memset (&e->msg, 0, sizeof (e->msg)); + 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_ERR_SUCCESS; /* 0 */ + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ +} + +/* used as confirmation of connection by master */ +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); + + memset (&e->msg, 0, sizeof (e->msg)); + 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_ERR_SUCCESS; /* 0 */ + } + + memif_msg_queue_elt_t *cur = c->msg_queue; + while (cur->next != NULL) + { + cur = cur->next; + } + cur->next = e; + + return MEMIF_ERR_SUCCESS; /* 0 */ +} + +/* 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 (int fd, 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 (fd, &msg, -1); +} + +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 MEMIF_ERR_PROTO; + } + + 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, + c->args.num_m2s_rings); + 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 */ +} + +/* handle interface identification (id, secret (optional)) */ +static_fn int +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]; + memset (err_string, 0, sizeof (char) * 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; + } + + 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)); + err = MEMIF_ERR_ID; + goto error; + } + + c = (memif_connection_t *) elt->data_struct; + + if (!(c->args.is_master)) + { + 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; + } + + c->fd = fd; + + 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; + } + + 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 ("MEMIF_SECRET_ERR"); + strncpy ((char *) err_string, + MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); + err = MEMIF_ERR_SECRET; + goto error; + } + r = strncmp ((char *) i->secret, (char *) c->args.secret, + strlen ((char *) c->args.secret)); + if (r != 0) + { + DBG ("MEMIF_SECRET_ERR"); + strncpy ((char *) err_string, + MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR)); + err = MEMIF_ERR_SECRET; + goto error; + } + } + else + { + DBG ("MEMIF_NOSECRET_ERR"); + strncpy ((char *) err_string, + MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR)); + err = MEMIF_ERR_NOSECRET; + goto error; + } + } + + 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: + 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; +} + +/* 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) + return MEMIF_ERR_NO_SHMFD; + + if (ar->index > MEMIF_MAX_REGION) + return MEMIF_ERR_MAXREG; + + 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; + 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 */ +} + +/* 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) + return MEMIF_ERR_NO_INTFD; + + if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M) + { + 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 *) 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->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; + c->run_args.num_s2m_rings++; + } + 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 *) realloc (c->tx_queues, + sizeof (memif_queue_t) * (ar->index + 1)); + if (mq == NULL) + return memif_syscall_error_handler (errno); + c->tx_queues = mq; + 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; + c->run_args.num_m2s_rings++; + } + + return MEMIF_ERR_SUCCESS; /* 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; + libmemif_main_t *lm = &libmemif_main; + memif_list_elt_t elt; + + int err; + err = memif_connect1 (c); + if (err != MEMIF_ERR_SUCCESS) + return err; + + strncpy ((char *) c->remote_if_name, (char *) cm->if_name, + strlen ((char *) cm->if_name)); + + int i; + if (c->on_interrupt != NULL) + { + for (i = 0; i < c->run_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); + + return err; +} + +/* master -> slave */ +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); + if (err != MEMIF_ERR_SUCCESS) + return err; + + strncpy ((char *) c->remote_if_name, (char *) cm->if_name, + strlen ((char *) cm->if_name)); + + int i; + if (c->on_interrupt != NULL) + { + 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); + } + + c->on_connect ((void *) c, c->private_ctx); + + return err; +} + +static_fn int +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)); + + /* on returning error, handle function will call memif_disconnect () */ + DBG ("disconnect received: %s, mode: %d", + c->remote_disconnect_string, c->args.mode); + return MEMIF_ERR_DISCONNECT; +} + +static_fn int +memif_msg_receive (int ifd) +{ + 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 = 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); + mh.msg_iov = iov; + mh.msg_iovlen = 1; + mh.msg_control = ctl; + mh.msg_controllen = sizeof (ctl); + + DBG ("recvmsg fd %d", ifd); + size = recvmsg (ifd, &mh, 0); + DBG ("done"); + if (size != sizeof (memif_msg_t)) + { + if (size == 0) + return MEMIF_ERR_DISCONNECTED; + else + return MEMIF_ERR_MFMSG; + } + + 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); + + 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: + break; + + case MEMIF_MSG_TYPE_HELLO: + 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_msg_enq_init (c)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS) + return err; + 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->run_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; + + case MEMIF_MSG_TYPE_INIT: + 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; + + case MEMIF_MSG_TYPE_ADD_REGION: + 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; + break; + + case MEMIF_MSG_TYPE_ADD_RING: + 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; + break; + + case MEMIF_MSG_TYPE_CONNECT: + if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS) + return err; + if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS) + return err; + break; + + case MEMIF_MSG_TYPE_CONNECTED: + 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)) != MEMIF_ERR_SUCCESS) + return err; + break; + + default: + return MEMIF_ERR_UNKNOWN_MSG;; + break; + } + + 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 */ +} + +int +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); + return err; +} + +/* calls memif_msg_receive to handle pending messages on socket */ +int +memif_conn_fd_read_ready (memif_connection_t * c) +{ + int err; + err = memif_msg_receive (c->fd); + if (err != 0) + { + err = memif_disconnect_internal (c); + } + return err; +} + +/* get msg from msg queue buffer and send it to socket */ +int +memif_conn_fd_write_ready (memif_connection_t * c) +{ + int err = MEMIF_ERR_SUCCESS; /* 0 */ + + + if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0) + goto done; + + memif_msg_queue_elt_t *e = c->msg_queue; + if (e == NULL) + goto done; + + c->msg_queue = c->msg_queue->next; + + 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_WRITE | MEMIF_FD_EVENT_MOD); +*/ + err = memif_msg_send (c->fd, &e->msg, e->fd); + free (e); + goto done; + +done: + return err; +} + +int +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 (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len); + + if (conn_fd < 0) + { + return memif_syscall_error_handler (errno); + } + DBG ("accept fd %d", ms->fd); + DBG ("conn fd %d", 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 (conn_fd); +} + +int +memif_read_ready (int fd) +{ + int err; + DBG ("call recv"); + err = memif_msg_receive (fd); + DBG ("recv finished"); + return err; +} -- cgit 1.2.3-korg From b467b2a02be6ea7bab1a4773523afe8a8e3cfd83 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Thu, 14 Sep 2017 14:12:10 +0200 Subject: libmemif: Jumbo frames support Change-Id: I2b316358dcd2de7168a860541bcca35c3dd44649 Signed-off-by: Jakub Grajciar --- extras/libmemif/Makefile.am | 9 + extras/libmemif/examples/README.md | 6 +- .../libmemif/examples/icmp_responder-epoll/main.c | 2 +- extras/libmemif/examples/icmp_responder-mt/main.c | 4 +- extras/libmemif/examples/icmp_responder/main.c | 2 +- extras/libmemif/src/libmemif.h | 3 +- extras/libmemif/src/main.c | 359 +++++++++++++++------ extras/libmemif/src/memif_private.h | 1 + extras/libmemif/src/socket.c | 2 - 9 files changed, 285 insertions(+), 103 deletions(-) (limited to 'extras/libmemif/src/socket.c') diff --git a/extras/libmemif/Makefile.am b/extras/libmemif/Makefile.am index 48e4bb89..1ff7e7e5 100644 --- a/extras/libmemif/Makefile.am +++ b/extras/libmemif/Makefile.am @@ -17,6 +17,9 @@ ACLOCAL_AMFLAGS = -I m4 AM_CPPFLAGS = -g -DMEMIF_DBG -DICMP_DBG +SRCS_C := $(shell find . -name "*.c" ) +SRCS_H := $(shell find . -name "*.h" ) + .PHONY: release release: $(MAKE) AM_CPPFLAGS="-O3" @@ -27,6 +30,12 @@ doc: doxygen doxygen.conf @echo Doxygen documentation built in docs directory. +.PHONY: fixstyle +fixstyle: + @echo Fixing code style... + indent $(SRCS_C) $(SRCS_H) + @echo Code style fixed! + # # unit_test # diff --git a/extras/libmemif/examples/README.md b/extras/libmemif/examples/README.md index bbd663b9..1375d27a 100644 --- a/extras/libmemif/examples/README.md +++ b/extras/libmemif/examples/README.md @@ -11,6 +11,6 @@ Current WORKDIR is set to root repository directory. Example apps can be run fro Example app | Description ------------|------------ -[icmpr](../examples/icmp_responder/main.c) | Simplest implementaion. Event polling is handled by libmemif. Single memif conenction in slave mode is created (id 0). Use Ctrl + C to exit app. -[icmpr-epoll](../examples/icmp_responder-epoll/main.c) (run in container by default) | Supports multiple connections and master mode. User can create/delete connections, set ip addresses, print connection information. [Example setup](ExampleSetup.md) contains instructions on basic connection use cases setups. -[icmpr-mt](../examples/icmp_responder-mt/main.c) | Multi-thread example, very similar to icmpr-epoll. Packets are handled in threads assigned to specific queues. Slave mode only. +[icmpr](../examples/icmp_responder/main.c) | Simplest implementaion. Event polling is handled by libmemif. Single memif conenction in slave mode is created (id 0). Use Ctrl + C to exit app. Memif receive mode: interrupt. +[icmpr-epoll](../examples/icmp_responder-epoll/main.c) (run in container by default) | Supports multiple connections and master mode. User can create/delete connections, set ip addresses, print connection information. [Example setup](ExampleSetup.md) contains instructions on basic connection use cases setups. Memif receive mode: interrupt. App provides functionality to disable interrupts for specified queue/s for testing purposes. Polling mode is not implemented in this example. +[icmpr-mt](../examples/icmp_responder-mt/main.c) | Multi-thread example, very similar to icmpr-epoll. Packets are handled in threads assigned to specific queues. Slave mode only. Memif receive mode: polling (memif_rx_poll function), interrupt (memif_rx_interrupt function). Receive modes differ per queue. diff --git a/extras/libmemif/examples/icmp_responder-epoll/main.c b/extras/libmemif/examples/icmp_responder-epoll/main.c index 4172785f..cff944f9 100644 --- a/extras/libmemif/examples/icmp_responder-epoll/main.c +++ b/extras/libmemif/examples/icmp_responder-epoll/main.c @@ -283,7 +283,7 @@ icmpr_buffer_alloc (long index, long n, uint16_t qid) int err; uint16_t r; /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */ - err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); + err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); diff --git a/extras/libmemif/examples/icmp_responder-mt/main.c b/extras/libmemif/examples/icmp_responder-mt/main.c index 860569bc..c47fc53d 100644 --- a/extras/libmemif/examples/icmp_responder-mt/main.c +++ b/extras/libmemif/examples/icmp_responder-mt/main.c @@ -309,7 +309,7 @@ memif_rx_poll (void *ptr) err = memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, - data->rx_buf_num, &tx); + data->rx_buf_num, &tx, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); @@ -439,7 +439,7 @@ memif_rx_interrupt (void *ptr) err = memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, - data->rx_buf_num, &tx); + data->rx_buf_num, &tx, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); diff --git a/extras/libmemif/examples/icmp_responder/main.c b/extras/libmemif/examples/icmp_responder/main.c index 5351b6b8..9e49771e 100644 --- a/extras/libmemif/examples/icmp_responder/main.c +++ b/extras/libmemif/examples/icmp_responder/main.c @@ -225,7 +225,7 @@ icmpr_buffer_alloc (long n, uint16_t qid) int err; uint16_t r; /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */ - err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r); + err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 0); if (err != MEMIF_ERR_SUCCESS) { INFO ("memif_buffer_alloc: %s", memif_strerror (err)); diff --git a/extras/libmemif/src/libmemif.h b/extras/libmemif/src/libmemif.h index 3732be68..a2d1a5e2 100644 --- a/extras/libmemif/src/libmemif.h +++ b/extras/libmemif/src/libmemif.h @@ -383,12 +383,13 @@ int memif_delete (memif_conn_handle_t * conn); @param bufs - memif buffers @param count - number of memif buffers to allocate @param count_out - returns number of allocated buffers + @param size - minimal buffer size, 0 = standard buffer size \return memif_err_t */ int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t * bufs, uint16_t count, - uint16_t * count_out); + uint16_t * count_out, uint16_t size); /** \brief Memif buffer free @param conn - memif conenction handle diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c index d1b59eea..49bf50cb 100644 --- a/extras/libmemif/src/main.c +++ b/extras/libmemif/src/main.c @@ -512,8 +512,8 @@ memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, if (conn == NULL) return MEMIF_ERR_NOCONN; uint8_t num = - (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args. - num_m2s_rings; + (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; @@ -656,9 +656,9 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args, } DBG ("creating socket file"); ms = malloc (sizeof (memif_socket_t)); - ms->filename = malloc (strlen ((char *) conn->args. - socket_filename) + - sizeof (char)); + ms->filename = + malloc (strlen ((char *) conn->args.socket_filename) + + sizeof (char)); memset (ms->filename, 0, strlen ((char *) conn->args.socket_filename) + sizeof (char)); @@ -842,17 +842,20 @@ memif_control_fd_handler (int fd, uint8_t events) if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL) { num = - (((memif_connection_t *) e->data_struct)->args. - is_master) ? ((memif_connection_t *) e->data_struct)-> - run_args.num_s2m_rings : ((memif_connection_t *) e-> - data_struct)->run_args. - num_m2s_rings; + (((memif_connection_t *) e->data_struct)-> + args.is_master) ? ((memif_connection_t *) e-> + data_struct)->run_args. + num_s2m_rings : ((memif_connection_t *) e->data_struct)-> + run_args.num_m2s_rings; for (i = 0; i < num; i++) { - if (((memif_connection_t *) e->data_struct)->rx_queues[i]. - int_fd == fd) + if (((memif_connection_t *) e->data_struct)-> + rx_queues[i].int_fd == fd) { - ((memif_connection_t *) e->data_struct)->on_interrupt ((void *) e->data_struct, ((memif_connection_t *) e->data_struct)->private_ctx, i); + ((memif_connection_t *) e->data_struct)-> + on_interrupt ((void *) e->data_struct, + ((memif_connection_t *) e-> + data_struct)->private_ctx, i); return MEMIF_ERR_SUCCESS; } } @@ -879,24 +882,24 @@ memif_control_fd_handler (int fd, uint8_t events) if (events & MEMIF_FD_EVENT_READ) { err = - ((memif_connection_t *) e->data_struct)->read_fn (e-> - data_struct); + ((memif_connection_t *) e->data_struct)-> + read_fn (e->data_struct); if (err != MEMIF_ERR_SUCCESS) return err; } if (events & MEMIF_FD_EVENT_WRITE) { err = - ((memif_connection_t *) e->data_struct)->write_fn (e-> - data_struct); + ((memif_connection_t *) e->data_struct)-> + write_fn (e->data_struct); if (err != MEMIF_ERR_SUCCESS) return err; } if (events & MEMIF_FD_EVENT_ERROR) { err = - ((memif_connection_t *) e->data_struct)->error_fn (e-> - data_struct); + ((memif_connection_t *) e->data_struct)-> + error_fn (e->data_struct); if (err != MEMIF_ERR_SUCCESS) return err; } @@ -990,8 +993,8 @@ memif_disconnect_internal (memif_connection_t * c) if (c->tx_queues != NULL) { num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; for (i = 0; i < num; i++) { mq = &c->tx_queues[i]; @@ -1011,8 +1014,8 @@ memif_disconnect_internal (memif_connection_t * c) if (c->rx_queues != NULL) { num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; for (i = 0; i < num; i++) { mq = &c->rx_queues[i]; @@ -1164,8 +1167,8 @@ memif_connect1 (memif_connection_t * c) } num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; for (i = 0; i < num; i++) { mq = &c->tx_queues[i]; @@ -1182,8 +1185,8 @@ memif_connect1 (memif_connection_t * c) } } num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; for (i = 0; i < num; i++) { mq = &c->rx_queues[i]; @@ -1336,7 +1339,7 @@ memif_init_regions_and_queues (memif_connection_t * conn) int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t * bufs, uint16_t count, - uint16_t * count_out) + uint16_t * count_out, uint16_t size) { memif_connection_t *c = (memif_connection_t *) conn; if (c == NULL) @@ -1344,17 +1347,18 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; if (qid >= num) return MEMIF_ERR_QID; memif_queue_t *mq = &c->tx_queues[qid]; memif_ring_t *ring = mq->ring; memif_buffer_t *b0, *b1; + uint8_t chain_buf0, chain_buf1; uint16_t mask = (1 << mq->log2_ring_size) - 1; uint16_t s0, s1, ns; *count_out = 0; - int err = MEMIF_ERR_SUCCESS; /* 0 */ + int i, err = MEMIF_ERR_SUCCESS; /* 0 */ if (ring->tail != ring->head) { @@ -1374,39 +1378,86 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, while ((count > 2) && (ns > 2)) { s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; - s1 = (ring->head + mq->alloc_bufs + *count_out + 1) & mask; + chain_buf0 = size / ring->desc[s0].buffer_length; + if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0)) + chain_buf0++; + + if (chain_buf0 > ns) + break; + + s1 = (ring->head + mq->alloc_bufs + *count_out + chain_buf0) & mask; + chain_buf1 = size / ring->desc[s1].buffer_length; + if (((size % ring->desc[s1].buffer_length) != 0) || (size == 0)) + chain_buf1++; + + if ((chain_buf0 + chain_buf1) > ns) + break; b0 = (bufs + *count_out); b1 = (bufs + *count_out + 1); b0->desc_index = s0; b1->desc_index = s1; - b0->buffer_len = ring->desc[s0].buffer_length; - b1->buffer_len = ring->desc[s1].buffer_length; + b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0; + b1->buffer_len = ring->desc[s1].buffer_length * chain_buf1; /* TODO: support multiple regions -> ring descriptor contains region index */ b0->data = c->regions->shm + ring->desc[s0].offset; b1->data = c->regions->shm + ring->desc[s1].offset; + for (i = 0; i < (memif_min (chain_buf0, chain_buf1) - 1); i++) + { + ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + DBG ("allocating chained buffers"); + } + + if (chain_buf0 > chain_buf1) + { + for (; i < (chain_buf0 - 1); i++) + ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + } + else + { + for (; i < (chain_buf1 - 1); i++) + ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + } + + mq->alloc_bufs += chain_buf0 + chain_buf1; + DBG ("allocated ring slots %u, %u", s0, s1); count -= 2; - ns -= 2; + ns -= chain_buf0 + chain_buf1; *count_out += 2; } s0 = (ring->head + mq->alloc_bufs + *count_out) & mask; b0 = (bufs + *count_out); + chain_buf0 = size / ring->desc[s0].buffer_length; + if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0)) + chain_buf0++; + + if (chain_buf0 > ns) + break; + b0->desc_index = s0; - b0->buffer_len = ring->desc[s0].buffer_length; + b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0; b0->data = c->regions->shm + ring->desc[s0].offset; + for (i = 0; i < (chain_buf0 - 1); i++) + { + ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; + DBG ("allocating chained buffers"); + } + + mq->alloc_bufs += chain_buf0; + DBG ("allocated ring slot %u", s0); count--; - ns--; + ns -= chain_buf0; *count_out += 1; } - mq->alloc_bufs += *count_out; DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, mq->alloc_bufs); @@ -1430,8 +1481,8 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; libmemif_main_t *lm = &libmemif_main; @@ -1439,6 +1490,7 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, memif_ring_t *ring = mq->ring; uint16_t tail = ring->tail; uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint8_t chain_buf0, chain_buf1; memif_buffer_t *b0, *b1; *count_out = 0; @@ -1451,22 +1503,35 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, { b0 = (bufs + *count_out); b1 = (bufs + *count_out + 1); - tail = (b0->desc_index + 1) & mask; - tail = (b1->desc_index + 1) & mask; + chain_buf0 = + b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != + 0) + chain_buf0++; + chain_buf1 = + b1->buffer_len / ring->desc[b1->desc_index].buffer_length; + if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) != + 0) + chain_buf1++; + tail = (b0->desc_index + chain_buf0) & mask; + tail = (b1->desc_index + chain_buf1) & mask; b0->data = NULL; b1->data = NULL; count -= 2; *count_out += 2; - mq->alloc_bufs -= 2; + mq->alloc_bufs -= chain_buf0 + chain_buf1; } b0 = (bufs + *count_out); - tail = (b0->desc_index + 1) & mask; + chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0) + chain_buf0++; + tail = (b0->desc_index + chain_buf0) & mask; b0->data = NULL; count--; *count_out += 1; - mq->alloc_bufs--; + mq->alloc_bufs -= chain_buf0; } MEMIF_MEORY_BARRIER (); ring->tail = tail; @@ -1484,33 +1549,94 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; if (qid >= num) return MEMIF_ERR_QID; memif_queue_t *mq = &c->tx_queues[qid]; memif_ring_t *ring = mq->ring; uint16_t head = ring->head; uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint8_t chain_buf0, chain_buf1; *tx = 0; + uint16_t curr_buf = 0; memif_buffer_t *b0, *b1; + int i; while (count) { while (count > 2) { - b0 = (bufs + *tx); - b1 = (bufs + *tx + 1); - ring->desc[b0->desc_index].length = b0->data_len; - ring->desc[b1->desc_index].length = b1->data_len; + b0 = (bufs + curr_buf); + b1 = (bufs + curr_buf + 1); + chain_buf0 = + b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != + 0) + chain_buf0++; + + chain_buf1 = + b1->buffer_len / ring->desc[b1->desc_index].buffer_length; + if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) != + 0) + chain_buf1++; + + for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++) + { + ring->desc[(b0->desc_index + i) & mask].length = b0->data_len; + ring->desc[(b1->desc_index + i) & mask].length = b1->data_len; +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data + + ring->desc[(b0->desc_index + + i) & mask].buffer_length * + (chain_buf0 - 1), + ring->desc[(b0->desc_index + + i) & mask].buffer_length, DBG_TX_BUF); + print_bytes (b1->data + + ring->desc[(b1->desc_index + + i) & mask].buffer_length * + (chain_buf1 - 1), + ring->desc[(b1->desc_index + + i) & mask].buffer_length, DBG_TX_BUF); +#endif + } + if (chain_buf0 > chain_buf1) + { + for (; i < chain_buf0; i++) + { + ring->desc[(b0->desc_index + i) & mask].length = + b0->data_len; +#ifdef MEMIF_DBG_SHM + print_bytes (b0->data + + ring->desc[(b0->desc_index + + i) & mask].buffer_length * + (chain_buf0 - 1), + ring->desc[(b0->desc_index + + i) & mask].buffer_length, + DBG_TX_BUF); +#endif + } + } + else + { + for (; i < chain_buf1; i++) + { + ring->desc[b1->desc_index + i].length = b1->data_len; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_TX_BUF); - print_bytes (b1->data, b1->data_len, DBG_TX_BUF); + print_bytes (b1->data + + ring->desc[(b1->desc_index + + i) & mask].buffer_length * + (chain_buf1 - 1), + ring->desc[(b1->desc_index + + i) & mask].buffer_length, + DBG_TX_BUF); #endif + } + } - head = (b0->desc_index + 1) & mask; - head = (b1->desc_index + 1) & mask; + head = (b0->desc_index + chain_buf0) & mask; + head = (b1->desc_index + chain_buf1) & mask; b0->data = NULL; b0->data_len = 0; @@ -1518,29 +1644,44 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, b1->data_len = 0; count -= 2; - *tx += 2; + *tx += chain_buf0 + chain_buf1; + curr_buf += 2; } b0 = (bufs + *tx); - ring->desc[b0->desc_index].length = b0->data_len; + chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length; + if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0) + chain_buf0++; + for (i = 0; i < chain_buf0; i++) + { + ring->desc[(b0->desc_index + i) & mask].length = b0->data_len; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_TX_BUF); + print_bytes (b0->data + + ring->desc[(b0->desc_index + i) & mask].buffer_length * + (chain_buf0 - 1), + ring->desc[(b0->desc_index + i) & mask].buffer_length, + DBG_TX_BUF); #endif + } - head = (b0->desc_index + 1) & mask; + head = (b0->desc_index + chain_buf0) & mask; b0->data = NULL; b0->data_len = 0; count--; - *tx += 1; + *tx += chain_buf0; + curr_buf++; } MEMIF_MEORY_BARRIER (); ring->head = head; mq->alloc_bufs -= *tx; + /* TODO: return num of buffers and packets */ + *tx = curr_buf; + if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) { uint64_t a = 1; @@ -1562,8 +1703,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; memif_queue_t *mq = &c->rx_queues[qid]; @@ -1572,7 +1713,9 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, uint16_t ns; uint16_t mask = (1 << mq->log2_ring_size) - 1; memif_buffer_t *b0, *b1; + uint16_t curr_buf = 0; *rx = 0; + int i; uint64_t b; ssize_t r = read (mq->int_fd, &b, sizeof (b)); @@ -1591,49 +1734,79 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, { while ((ns > 2) && (count > 2)) { - b0 = (bufs + *rx); - b1 = (bufs + *rx + 1); + b0 = (bufs + curr_buf); + b1 = (bufs + curr_buf + 1); b0->desc_index = mq->last_head; - b1->desc_index = mq->last_head + 1; - b0->data = memif_get_buffer (conn, ring, mq->last_head); - b1->data = memif_get_buffer (conn, ring, mq->last_head + 1); - b0->data_len = ring->desc[mq->last_head].length; - b1->data_len = ring->desc[mq->last_head + 1].length; - b0->buffer_len = ring->desc[mq->last_head].buffer_length; - b1->buffer_len = ring->desc[mq->last_head + 1].buffer_length; - + i = 0; + do + { + b0->data = memif_get_buffer (conn, ring, mq->last_head); + b0->data_len = ring->desc[mq->last_head].length; + b0->buffer_len = ring->desc[mq->last_head].buffer_length; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_RX_BUF); - print_bytes (b1->data, b1->data_len, DBG_RX_BUF); + print_bytes (b0->data + + ring->desc[b0->desc_index].buffer_length * i++, + ring->desc[b0->desc_index].buffer_length, + DBG_TX_BUF); #endif + mq->last_head = (mq->last_head + 1) & mask; + ns--; + *rx += 1; + } + while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT); - mq->last_head = (mq->last_head + 2) & mask; + b1->desc_index = mq->last_head; + i = 0; + do + { + b1->data = memif_get_buffer (conn, ring, mq->last_head); + b1->data_len = ring->desc[mq->last_head].length; + b1->buffer_len = ring->desc[mq->last_head].buffer_length; +#ifdef MEMIF_DBG_SHM + print_bytes (b1->data + + ring->desc[b1->desc_index].buffer_length * i++, + ring->desc[b1->desc_index].buffer_length, + DBG_TX_BUF); +#endif + mq->last_head = (mq->last_head + 1) & mask; + ns--; + *rx += 1; + } + while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT); - ns -= 2; count -= 2; - *rx += 2; + curr_buf += 2; } - b0 = (bufs + *rx); + b0 = (bufs + curr_buf); b0->desc_index = mq->last_head; - b0->data = memif_get_buffer (conn, ring, mq->last_head); - b0->data_len = ring->desc[mq->last_head].length; - b0->buffer_len = ring->desc[mq->last_head].buffer_length; - + i = 0; + do + { + b0->data = memif_get_buffer (conn, ring, mq->last_head); + b0->data_len = ring->desc[mq->last_head].length; + b0->buffer_len = ring->desc[mq->last_head].buffer_length; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data, b0->data_len, DBG_RX_BUF); + print_bytes (b0->data + + ring->desc[b0->desc_index].buffer_length * i++, + ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF); #endif + mq->last_head = (mq->last_head + 1) & mask; + ns--; + *rx += 1; + } + while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT); - mq->last_head = (mq->last_head + 1) & mask; - - ns--; count--; - *rx += 1; + curr_buf++; } mq->alloc_bufs += *rx; + /* TODO: return num of buffers and packets */ + *rx = curr_buf; + if (ns) { DBG ("not enough buffers!"); @@ -1722,8 +1895,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md, err = MEMIF_ERR_NOBUF_DET; md->rx_queues_num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; l1 = sizeof (memif_queue_details_t) * md->rx_queues_num; if (l0 + l1 <= buflen) @@ -1742,8 +1915,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md, } md->tx_queues_num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; l1 = sizeof (memif_queue_details_t) * md->tx_queues_num; if (l0 + l1 <= buflen) @@ -1776,8 +1949,8 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) if (c->fd < 0) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; diff --git a/extras/libmemif/src/memif_private.h b/extras/libmemif/src/memif_private.h index 51f3be66..83962bcf 100644 --- a/extras/libmemif/src/memif_private.h +++ b/extras/libmemif/src/memif_private.h @@ -44,6 +44,7 @@ #define MEMIF_MAX_FDS 512 +#define memif_min(a,b) (((a) < (b)) ? (a) : (b)) #ifdef MEMIF_DBG #define DBG(...) do { \ diff --git a/extras/libmemif/src/socket.c b/extras/libmemif/src/socket.c index 9c9b3a8d..ca24d929 100644 --- a/extras/libmemif/src/socket.c +++ b/extras/libmemif/src/socket.c @@ -33,8 +33,6 @@ #include #include -#define memif_min(a,b) ((a < b) ? (a) : (b)) - /* sends msg to socket */ static_fn int memif_msg_send (int fd, memif_msg_t * msg, int afd) -- cgit 1.2.3-korg