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)
133 memset (&e->
msg, 0, sizeof (e->
msg));
139 i->
id = c->args.interface_id;
140 i->
mode = c->args.mode;
145 strncpy ((
char *) i->
secret, (
char *) c->args.secret, sizeof (i->
secret));
148 if (c->msg_queue ==
NULL)
177 memset (&e->
msg, 0, sizeof (e->
msg));
182 ar->
index = region_index;
186 if (c->msg_queue ==
NULL)
212 memset (&e->
msg, 0, sizeof (e->
msg));
220 mq = &c->rx_queues[index];
222 mq = &c->tx_queues[index];
233 if (c->msg_queue ==
NULL)
259 memset (&e->
msg, 0, sizeof (e->
msg));
264 strncpy ((
char *) cm->
if_name, (
char *) c->args.interface_name,
265 strlen ((
char *) c->args.interface_name));
268 if (c->msg_queue ==
NULL)
294 memset (&e->
msg, 0, sizeof (e->
msg));
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));
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,
501 c->regions[ar->
index].fd = fd;
502 c->regions[ar->
index].region_size = ar->
size;
527 if (ar->
index >= c->args.num_s2m_rings)
537 c->rx_queues[ar->
index].int_fd = fd;
541 c->run_args.num_s2m_rings++;
547 if (ar->
index >= c->args.num_m2s_rings)
557 c->tx_queues[ar->
index].int_fd = fd;
561 c->run_args.num_m2s_rings++;
580 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
581 strlen ((
char *) cm->
if_name));
584 if (c->on_interrupt !=
NULL)
586 for (i = 0; i < c->run_args.num_m2s_rings; i++)
588 elt.
key = c->rx_queues[
i].int_fd;
597 c->on_connect ((
void *) c, c->private_ctx);
614 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
615 strlen ((
char *) cm->
if_name));
618 if (c->on_interrupt !=
NULL)
620 for (i = 0; i < c->run_args.num_s2m_rings; i++)
626 c->on_connect ((
void *) c, c->private_ctx);
636 memset (c->remote_disconnect_string, 0,
637 sizeof (c->remote_disconnect_string));
638 strncpy ((
char *) c->remote_disconnect_string, (
char *) d->
string,
639 strlen ((
char *) d->
string));
642 DBG (
"disconnect received: %s, mode: %d",
643 c->remote_disconnect_string, c->args.mode);
650 char ctl[CMSG_SPACE (
sizeof (
int)) +
651 CMSG_SPACE (
sizeof (
struct ucred))] = { 0 };
652 struct msghdr mh = { 0 };
664 iov[0].iov_base = (
void *) &msg;
668 mh.msg_control = ctl;
669 mh.msg_controllen =
sizeof (ctl);
671 DBG (
"recvmsg fd %d", ifd);
672 size = recvmsg (ifd, &mh, 0);
682 struct ucred *cr = 0;
683 struct cmsghdr *cmsg;
685 cmsg = CMSG_FIRSTHDR (&mh);
688 if (cmsg->cmsg_level == SOL_SOCKET)
690 if (cmsg->cmsg_type == SCM_CREDENTIALS)
692 cr = (
struct ucred *) CMSG_DATA (cmsg);
694 else if (cmsg->cmsg_type == SCM_RIGHTS)
696 int *fdp = (
int *) CMSG_DATA (cmsg);
700 cmsg = CMSG_NXTHDR (&mh, cmsg);
703 DBG (
"Message type %u received", msg.type);
723 for (i = 0; i < c->run_args.num_s2m_rings; i++)
730 for (i = 0; i < c->run_args.num_m2s_rings; i++)
807 DBG (
"connection fd error");
808 strncpy ((
char *) c->remote_disconnect_string,
"connection fd error", 19);
841 c->msg_queue = c->msg_queue->next;
862 struct sockaddr_un client;
866 DBG (
"accept called");
868 addr_len =
sizeof (client);
870 accept (ms->
fd, (
struct sockaddr *) &client, (socklen_t *) & addr_len);
876 DBG (
"accept fd %d", ms->
fd);
877 DBG (
"conn fd %d", conn_fd);
895 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
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_region_index_t region
uint16_t interface_list_len
memif_region_size_t region_size