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);
85 if (c->msg_queue ==
NULL)
140 i->
id = c->args.interface_id;
141 i->
mode = c->args.mode;
145 if (strlen ((
char *) c->args.secret) > 0)
146 strncpy ((
char *) i->
secret, (
char *) c->args.secret, sizeof (i->
secret));
149 if (c->msg_queue ==
NULL)
182 ar->
index = region_index;
186 if (c->msg_queue ==
NULL)
220 mq = &c->rx_queues[index];
222 mq = &c->tx_queues[index];
233 if (c->msg_queue ==
NULL)
264 strncpy ((
char *) cm->
if_name, (
char *) c->args.interface_name,
265 strlen ((
char *) c->args.interface_name));
268 if (c->msg_queue ==
NULL)
299 strncpy ((
char *) cm->
if_name, (
char *) c->args.interface_name,
300 strlen ((
char *) c->args.interface_name));
303 if (c->msg_queue ==
NULL)
330 uint16_t l = strlen ((
char *) err_string);
333 DBG (
"Disconnect string too long. Sending first 96 characters.");
336 strncpy ((
char *) d->
string, (
char *) err_string, l);
349 DBG (
"incompatible protocol version");
354 c->args.num_s2m_rings);
356 c->args.num_m2s_rings);
358 c->args.log2_ring_size);
359 c->run_args.buffer_size = c->args.buffer_size;
360 strncpy ((
char *) c->remote_name, (
char *) h->
name,
361 strlen ((
char *) h->
name));
375 uint8_t err_string[96];
376 memset (err_string, 0,
sizeof (
char) * 96);
381 DBG (
"MEMIF_VER_ERR");
390 DBG (
"MEMIF_ID_ERR");
398 if (!(c->args.is_master))
400 DBG (
"MEMIF_SLAVE_ERR");
408 DBG (
"MEMIF_CONN_ERR");
416 if (i->
mode != c->args.mode)
418 DBG (
"MEMIF_MODE_ERR");
424 strncpy ((
char *) c->remote_name, (
char *) i->
name,
425 strlen ((
char *) i->
name));
427 if (strlen ((
char *) c->args.secret) > 0)
430 if (strlen ((
char *) i->
secret) > 0)
432 if (strlen ((
char *) c->args.secret) != strlen ((
char *) i->
secret))
434 DBG (
"MEMIF_SECRET_ERR");
435 strncpy ((
char *) err_string,
440 r = strncmp ((
char *) i->
secret, (
char *) c->args.secret,
441 strlen ((
char *) c->args.secret));
444 DBG (
"MEMIF_SECRET_ERR");
445 strncpy ((
char *) err_string,
453 DBG (
"MEMIF_NOSECRET_ERR");
454 strncpy ((
char *) err_string,
505 c->regions[ar->
index].fd = fd;
506 c->regions[ar->
index].region_size = ar->
size;
511 c->regions[ar->
index].is_external = 1;
537 if (ar->
index >= c->args.num_s2m_rings)
543 (++c->rx_queues_num));
548 c->rx_queues[ar->
index].int_fd = fd;
552 c->run_args.num_s2m_rings++;
558 if (ar->
index >= c->args.num_m2s_rings)
564 (++c->tx_queues_num));
569 c->tx_queues[ar->
index].int_fd = fd;
573 c->run_args.num_m2s_rings++;
592 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
593 strlen ((
char *) cm->
if_name));
596 if (c->on_interrupt !=
NULL)
598 for (i = 0; i < c->run_args.num_m2s_rings; i++)
600 elt.
key = c->rx_queues[
i].int_fd;
609 c->on_connect ((
void *) c, c->private_ctx);
626 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
627 strlen ((
char *) cm->
if_name));
630 if (c->on_interrupt !=
NULL)
632 for (i = 0; i < c->run_args.num_s2m_rings; i++)
638 c->on_connect ((
void *) c, c->private_ctx);
648 memset (c->remote_disconnect_string, 0,
649 sizeof (c->remote_disconnect_string));
650 strncpy ((
char *) c->remote_disconnect_string, (
char *) d->
string,
651 strlen ((
char *) d->
string));
654 DBG (
"disconnect received: %s, mode: %d",
655 c->remote_disconnect_string, c->args.mode);
662 char ctl[CMSG_SPACE (
sizeof (
int)) +
663 CMSG_SPACE (
sizeof (
struct ucred))] = { 0 };
664 struct msghdr mh = { 0 };
676 iov[0].iov_base = (
void *) &msg;
680 mh.msg_control = ctl;
681 mh.msg_controllen =
sizeof (ctl);
683 DBG (
"recvmsg fd %d", ifd);
684 size = recvmsg (ifd, &mh, 0);
694 struct ucred *cr = 0;
695 struct cmsghdr *cmsg;
697 cmsg = CMSG_FIRSTHDR (&mh);
700 if (cmsg->cmsg_level == SOL_SOCKET)
702 if (cmsg->cmsg_type == SCM_CREDENTIALS)
704 cr = (
struct ucred *) CMSG_DATA (cmsg);
706 else if (cmsg->cmsg_type == SCM_RIGHTS)
708 int *fdp = (
int *) CMSG_DATA (cmsg);
712 cmsg = CMSG_NXTHDR (&mh, cmsg);
715 DBG (
"Message type %u received", msg.type);
733 for (i = 0; i < c->regions_num; i++)
738 for (i = 0; i < c->run_args.num_s2m_rings; i++)
745 for (i = 0; i < c->run_args.num_m2s_rings; i++)
822 DBG (
"connection fd error");
823 strncpy ((
char *) c->remote_disconnect_string,
"connection fd error", 19);
856 c->msg_queue = c->msg_queue->next;
877 struct sockaddr_un client;
881 DBG (
"accept called");
883 addr_len =
sizeof (client);
885 accept (ms->
fd, (
struct sockaddr *) &client, (socklen_t *) & addr_len);
891 DBG (
"accept fd %d", ms->
fd);
892 DBG (
"conn fd %d", conn_fd);
910 DBG (
"recv finished");
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 ...
memif_msg_add_ring_t add_ring
#define MEMIF_MSG_ADD_RING_FLAG_S2M
memif_list_elt_t * pending_list
int memif_disconnect_internal(memif_connection_t *c)
#define MEMIF_CONNECTION_FLAG_WRITE
memif_control_fd_update_t * control_fd_update
memif_region_index_t region
memif_version_t min_version
uint16_t private_hdr_size
memif_list_elt_t * interface_list
memset(h->entries, 0, sizeof(h->entries[0])*entries)
memif_realloc_t * realloc
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)
memif_list_elt_t * interrupt_list
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
#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_log2_ring_size_t max_log2_ring_size
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)
memif_region_index_t index
memif_log2_ring_size_t log2_ring_size
memif_ring_index_t max_s2m_ring
memif_msg_connect_t connect
uint16_t interrupt_list_len
#define MEMIF_MAX_S2M_RING
uint8_t app_name[MEMIF_NAME_LEN]
memif_region_index_t max_region
uint16_t pending_list_len
memif_list_elt_t * control_list
memif_log2_ring_size_t log2_ring_size
memif_msg_connected_t connected
uint16_t control_list_len
int memif_connect1(memif_connection_t *c)
libmemif_main_t libmemif_main
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
#define MEMIF_FD_EVENT_WRITE
memif_get_external_region_addr_t * get_external_region_addr
memif_region_index_t region
uint16_t interface_list_len
memif_region_size_t region_size