19 #include <sys/socket.h> 20 #include <sys/types.h> 26 #include <sys/ioctl.h> 29 #include <sys/prctl.h> 41 struct msghdr mh = { 0 };
43 char ctl[CMSG_SPACE (
sizeof (
int))];
46 iov[0].iov_base = (
void *) msg;
54 memset (&ctl, 0,
sizeof (ctl));
56 mh.msg_controllen =
sizeof (ctl);
57 cmsg = CMSG_FIRSTHDR (&mh);
58 cmsg->cmsg_len = CMSG_LEN (
sizeof (
int));
59 cmsg->cmsg_level = SOL_SOCKET;
60 cmsg->cmsg_type = SCM_RIGHTS;
61 memcpy (CMSG_DATA (cmsg), &afd,
sizeof (
int));
63 rv = sendmsg (fd, &mh, 0);
66 DBG (
"Message type %u sent", msg->
type);
80 memset (&e->
msg, 0, sizeof (e->
msg));
85 if (c->msg_queue == NULL)
92 while (cur->
next != NULL)
133 memset (&e->
msg, 0, sizeof (e->
msg));
139 i->
id = c->args.interface_id;
140 i->
mode = c->args.mode;
144 if (strlen ((
char *) c->args.secret) > 0)
145 strncpy ((
char *) i->
secret, (
char *) c->args.secret, sizeof (i->
secret));
148 if (c->msg_queue == NULL)
155 while (cur->
next != NULL)
176 memset (&e->
msg, 0, sizeof (e->
msg));
181 ar->
index = region_index;
185 if (c->msg_queue == NULL)
192 while (cur->
next != NULL)
211 memset (&e->
msg, 0, sizeof (e->
msg));
219 mq = &c->rx_queues[index];
221 mq = &c->tx_queues[index];
232 if (c->msg_queue == NULL)
239 while (cur->
next != NULL)
258 memset (&e->
msg, 0, sizeof (e->
msg));
263 strncpy ((
char *) cm->
if_name, (
char *) c->args.interface_name,
264 strlen ((
char *) c->args.interface_name));
267 if (c->msg_queue == NULL)
274 while (cur->
next != NULL)
293 memset (&e->
msg, 0, sizeof (e->
msg));
298 strncpy ((
char *) cm->
if_name, (
char *) c->args.interface_name,
299 strlen ((
char *) c->args.interface_name));
302 if (c->msg_queue == NULL)
309 while (cur->
next != NULL)
329 uint16_t l = strlen ((
char *) err_string);
332 DBG (
"Disconnect string too long. Sending first 96 characters.");
335 strncpy ((
char *) d->
string, (
char *) err_string, l);
348 DBG (
"incompatible protocol version");
353 c->args.num_s2m_rings);
355 c->args.num_m2s_rings);
357 c->args.log2_ring_size);
358 c->run_args.buffer_size = c->args.buffer_size;
359 strncpy ((
char *) c->remote_name, (
char *) h->
name,
360 strlen ((
char *) h->
name));
374 uint8_t err_string[96];
375 memset (err_string, 0,
sizeof (
char) * 96);
380 DBG (
"MEMIF_VER_ERR");
389 DBG (
"MEMIF_ID_ERR");
397 if (!(c->args.is_master))
399 DBG (
"MEMIF_SLAVE_ERR");
407 DBG (
"MEMIF_CONN_ERR");
415 if (i->
mode != c->args.mode)
417 DBG (
"MEMIF_MODE_ERR");
423 strncpy ((
char *) c->remote_name, (
char *) i->
name,
424 strlen ((
char *) i->
name));
426 if (strlen ((
char *) c->args.secret) > 0)
429 if (strlen ((
char *) i->
secret) > 0)
431 if (strlen ((
char *) c->args.secret) != strlen ((
char *) i->
secret))
433 DBG (
"MEMIF_SECRET_ERR");
434 strncpy ((
char *) err_string,
439 r = strncmp ((
char *) i->
secret, (
char *) c->args.secret,
440 strlen ((
char *) c->args.secret));
443 DBG (
"MEMIF_SECRET_ERR");
444 strncpy ((
char *) err_string,
452 DBG (
"MEMIF_NOSECRET_ERR");
453 strncpy ((
char *) err_string,
504 c->regions[ar->
index].fd = fd;
505 c->regions[ar->
index].region_size = ar->
size;
506 c->regions[ar->
index].addr = NULL;
510 c->regions[ar->
index].is_external = 1;
536 if (ar->
index >= c->args.num_s2m_rings)
542 (++c->rx_queues_num));
547 c->rx_queues[ar->
index].int_fd = fd;
551 c->run_args.num_s2m_rings++;
557 if (ar->
index >= c->args.num_m2s_rings)
563 (++c->tx_queues_num));
568 c->tx_queues[ar->
index].int_fd = fd;
572 c->run_args.num_m2s_rings++;
591 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
592 strlen ((
char *) cm->
if_name));
595 if (c->on_interrupt != NULL)
597 for (i = 0; i < c->run_args.num_m2s_rings; i++)
599 elt.
key = c->rx_queues[
i].int_fd;
610 c->on_connect ((
void *) c, c->private_ctx);
627 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
628 strlen ((
char *) cm->
if_name));
631 if (c->on_interrupt != NULL)
633 for (i = 0; i < c->run_args.num_s2m_rings; i++)
640 c->on_connect ((
void *) c, c->private_ctx);
650 memset (c->remote_disconnect_string, 0,
651 sizeof (c->remote_disconnect_string));
652 strncpy ((
char *) c->remote_disconnect_string, (
char *) d->
string,
653 strlen ((
char *) d->
string));
656 DBG (
"disconnect received: %s, mode: %d",
657 c->remote_disconnect_string, c->args.mode);
664 char ctl[CMSG_SPACE (
sizeof (
int)) +
665 CMSG_SPACE (
sizeof (
struct ucred))] = { 0 };
666 struct msghdr mh = { 0 };
677 iov[0].iov_base = (
void *) &msg;
681 mh.msg_control = ctl;
682 mh.msg_controllen =
sizeof (ctl);
684 DBG (
"recvmsg fd %d", ifd);
685 size = recvmsg (ifd, &mh, 0);
694 struct cmsghdr *cmsg;
696 cmsg = CMSG_FIRSTHDR (&mh);
699 if (cmsg->cmsg_level == SOL_SOCKET)
701 if (cmsg->cmsg_type == SCM_CREDENTIALS)
705 else if (cmsg->cmsg_type == SCM_RIGHTS)
707 int *fdp = (
int *) CMSG_DATA (cmsg);
711 cmsg = CMSG_NXTHDR (&mh, cmsg);
714 DBG (
"Message type %u received", msg.type);
732 for (i = 0; i < c->regions_num; i++)
737 for (i = 0; i < c->run_args.num_s2m_rings; i++)
744 for (i = 0; i < c->run_args.num_m2s_rings; i++)
820 DBG (
"connection fd error");
821 strncpy ((
char *) c->remote_disconnect_string,
"connection fd error", 19);
856 c->msg_queue = c->msg_queue->next;
872 struct sockaddr_un client;
876 DBG (
"accept called");
878 addr_len =
sizeof (client);
880 accept (ms->
fd, (
struct sockaddr *) &client, (socklen_t *) & addr_len);
886 DBG (
"accept fd %d", ms->
fd);
887 DBG (
"conn fd %d", conn_fd);
memif_version_t max_version
memif_interface_mode_t mode
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
memif_msg_add_region_t add_region
#define MEMIF_FD_EVENT_READ
user needs to set events that occured on fd and pass them to memif_control_fd_handler ...
Optimized string handling code, including c11-compliant "safe C library" variants.
memif_msg_add_ring_t add_ring
uint8_t app_name[MEMIF_NAME_LEN]
#define MEMIF_MSG_ADD_RING_FLAG_S2M
int memif_disconnect_internal(memif_connection_t *c)
#define MEMIF_CONNECTION_FLAG_WRITE
memif_region_index_t region
memif_version_t min_version
uint16_t private_hdr_size
memif_list_elt_t * interface_list
libmemif_main_t * get_libmemif_main(memif_socket_t *ms)
memif_ring_index_t max_m2s_ring
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
memif_region_offset_t offset
#define MEMIF_MAX_M2S_RING
memif_region_offset_t offset
#define MEMIF_MAX_LOG2_RING_SIZE
int memif_syscall_error_handler(int err_code)
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
memif_list_elt_t * pending_list
#define MEMIF_FD_EVENT_DEL
if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) ...
memif_msg_disconnect_t disconnect
struct memif_msg_queue_elt * next
memif_list_elt_t * interrupt_list
uint16_t pending_list_len
memif_log2_ring_size_t max_log2_ring_size
int get_list_elt(memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key)
memif_list_elt_t * control_list
memif_region_index_t index
uint16_t interrupt_list_len
uint16_t control_list_len
memif_log2_ring_size_t log2_ring_size
memif_realloc_t * realloc
memif_ring_index_t max_s2m_ring
memif_msg_connect_t connect
memif_get_external_region_addr_t * get_external_region_addr
#define MEMIF_MAX_S2M_RING
memif_region_index_t max_region
memif_log2_ring_size_t log2_ring_size
memif_msg_connected_t connected
memif_control_fd_update_t * control_fd_update
int memif_connect1(memif_connection_t *c)
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
#define MEMIF_FD_EVENT_WRITE
int add_list_elt(libmemif_main_t *lm, memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
memif_region_index_t region
uint16_t interface_list_len
memif_region_size_t region_size