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)
134 memset (&e->
msg, 0, sizeof (e->
msg));
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)
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);
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;
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;
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++)
639 c->on_connect ((
void *) c, c->private_ctx);
649 memset (c->remote_disconnect_string, 0,
650 sizeof (c->remote_disconnect_string));
651 strncpy ((
char *) c->remote_disconnect_string, (
char *) d->
string,
652 strlen ((
char *) d->
string));
655 DBG (
"disconnect received: %s, mode: %d",
656 c->remote_disconnect_string, c->args.mode);
663 char ctl[CMSG_SPACE (
sizeof (
int)) +
664 CMSG_SPACE (
sizeof (
struct ucred))] = { 0 };
665 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);
854 c->msg_queue = c->msg_queue->next;
870 struct sockaddr_un client;
874 DBG (
"accept called");
876 addr_len =
sizeof (client);
878 accept (ms->
fd, (
struct sockaddr *) &client, (socklen_t *) & addr_len);
884 DBG (
"accept fd %d", ms->
fd);
885 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
#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_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