19 #include <sys/socket.h> 20 #include <sys/types.h> 26 #include <sys/ioctl.h> 29 #include <sys/prctl.h> 40 struct msghdr mh = { 0 };
42 char ctl[CMSG_SPACE (
sizeof (
int))];
45 iov[0].iov_base = (
void *) msg;
53 memset (&ctl, 0,
sizeof (ctl));
55 mh.msg_controllen =
sizeof (ctl);
56 cmsg = CMSG_FIRSTHDR (&mh);
57 cmsg->cmsg_len = CMSG_LEN (
sizeof (
int));
58 cmsg->cmsg_level = SOL_SOCKET;
59 cmsg->cmsg_type = SCM_RIGHTS;
60 memcpy (CMSG_DATA (cmsg), &afd,
sizeof (
int));
62 rv = sendmsg (fd, &mh, 0);
65 DBG (
"Message type %u sent", msg->
type);
78 memset (&e->
msg, 0, sizeof (e->
msg));
83 if (c->msg_queue ==
NULL)
130 memset (&e->
msg, 0, sizeof (e->
msg));
136 i->
id = c->args.interface_id;
137 i->
mode = c->args.mode;
139 strncpy ((
char *) i->
name, (
char *) c->args.instance_name,
140 strlen ((
char *) c->args.instance_name));
142 strncpy ((
char *) i->
secret, (
char *) c->args.secret, sizeof (i->
secret));
145 if (c->msg_queue ==
NULL)
173 memset (&e->
msg, 0, sizeof (e->
msg));
178 ar->
index = region_index;
182 if (c->msg_queue ==
NULL)
207 memset (&e->
msg, 0, sizeof (e->
msg));
215 mq = &c->rx_queues[index];
217 mq = &c->tx_queues[index];
227 if (c->msg_queue ==
NULL)
252 memset (&e->
msg, 0, sizeof (e->
msg));
257 strncpy ((
char *) cm->
if_name, (
char *) c->args.interface_name,
258 strlen ((
char *) c->args.interface_name));
261 if (c->msg_queue ==
NULL)
286 memset (&e->
msg, 0, sizeof (e->
msg));
291 strncpy ((
char *) cm->
if_name, (
char *) c->args.interface_name,
292 strlen ((
char *) c->args.interface_name));
295 if (c->msg_queue ==
NULL)
322 uint16_t l = strlen ((
char *) err_string);
325 DBG (
"Disconnect string too long. Sending first 96 characters.");
328 strncpy ((
char *) d->
string, (
char *) err_string, l);
341 DBG (
"incompatible protocol version");
346 c->args.num_s2m_rings);
348 c->args.num_m2s_rings);
350 c->args.log2_ring_size);
351 c->run_args.buffer_size = c->args.buffer_size;
352 strncpy ((
char *) c->remote_name, (
char *) h->
name,
353 strlen ((
char *) h->
name));
367 uint8_t err_string[96];
368 memset (err_string, 0,
sizeof (
char) * 96);
373 DBG (
"MEMIF_VER_ERR");
382 DBG (
"MEMIF_ID_ERR");
390 if (!(c->args.is_master))
392 DBG (
"MEMIF_SLAVE_ERR");
400 DBG (
"MEMIF_CONN_ERR");
408 if (i->
mode != c->args.mode)
410 DBG (
"MEMIF_MODE_ERR");
416 strncpy ((
char *) c->remote_name, (
char *) i->
name,
417 strlen ((
char *) i->
name));
424 if (strlen ((
char *) c->args.secret) != strlen ((
char *) i->
secret))
426 DBG (
"MEMIF_SECRET_ERR");
427 strncpy ((
char *) err_string,
432 r = strncmp ((
char *) i->
secret, (
char *) c->args.secret,
433 strlen ((
char *) c->args.secret));
436 DBG (
"MEMIF_SECRET_ERR");
437 strncpy ((
char *) err_string,
445 DBG (
"MEMIF_NOSECRET_ERR");
446 strncpy ((
char *) err_string,
493 c->regions[ar->
index].fd = fd;
494 c->regions[ar->
index].region_size = ar->
size;
516 if (ar->
index >= c->args.num_s2m_rings)
525 c->rx_queues[ar->
index].int_fd = fd;
529 c->run_args.num_s2m_rings++;
535 if (ar->
index >= c->args.num_m2s_rings)
544 c->tx_queues[ar->
index].int_fd = fd;
548 c->run_args.num_m2s_rings++;
567 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
568 strlen ((
char *) cm->
if_name));
571 if (c->on_interrupt !=
NULL)
573 for (i = 0; i < c->run_args.num_m2s_rings; i++)
575 elt.
key = c->rx_queues[
i].int_fd;
584 c->on_connect ((
void *) c, c->private_ctx);
601 strncpy ((
char *) c->remote_if_name, (
char *) cm->
if_name,
602 strlen ((
char *) cm->
if_name));
605 if (c->on_interrupt !=
NULL)
607 for (i = 0; i < c->run_args.num_s2m_rings; i++)
611 c->on_connect ((
void *) c, c->private_ctx);
621 memset (c->remote_disconnect_string, 0,
622 sizeof (c->remote_disconnect_string));
623 strncpy ((
char *) c->remote_disconnect_string, (
char *) d->
string,
624 strlen ((
char *) d->
string));
627 DBG (
"disconnect received: %s, mode: %d",
628 c->remote_disconnect_string, c->args.mode);
635 char ctl[CMSG_SPACE (
sizeof (
int)) +
636 CMSG_SPACE (
sizeof (
struct ucred))] = { 0 };
637 struct msghdr mh = { 0 };
649 iov[0].iov_base = (
void *) &msg;
653 mh.msg_control = ctl;
654 mh.msg_controllen =
sizeof (ctl);
656 DBG (
"recvmsg fd %d", ifd);
657 size = recvmsg (ifd, &mh, 0);
667 struct ucred *cr = 0;
668 struct cmsghdr *cmsg;
670 cmsg = CMSG_FIRSTHDR (&mh);
673 if (cmsg->cmsg_level == SOL_SOCKET)
675 if (cmsg->cmsg_type == SCM_CREDENTIALS)
677 cr = (
struct ucred *) CMSG_DATA (cmsg);
679 else if (cmsg->cmsg_type == SCM_RIGHTS)
681 int *fdp = (
int *) CMSG_DATA (cmsg);
685 cmsg = CMSG_NXTHDR (&mh, cmsg);
688 DBG (
"Message type %u received", msg.type);
708 for (i = 0; i < c->run_args.num_s2m_rings; i++)
715 for (i = 0; i < c->run_args.num_m2s_rings; i++)
792 DBG (
"connection fd error");
793 strncpy ((
char *) c->remote_disconnect_string,
"connection fd error", 19);
825 c->msg_queue = c->msg_queue->next;
846 struct sockaddr_un client;
850 DBG (
"accept called");
852 addr_len =
sizeof (client);
854 accept (ms->
fd, (
struct sockaddr *) &client, (socklen_t *) & addr_len);
860 DBG (
"accept fd %d", ms->
fd);
861 DBG (
"conn fd %d", conn_fd);
879 DBG (
"recv finished");
memif_version_t max_version
memif_interface_mode_t mode
sll srl srl sll sra u16x4 i
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
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
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