2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
19 #include <sys/socket.h>
20 #include <sys/types.h>
26 #include <sys/ioctl.h>
29 #include <sys/prctl.h>
35 #include <memif_private.h>
37 /* sends msg to socket */
39 memif_msg_send (int fd, memif_msg_t * msg, int afd)
41 struct msghdr mh = { 0 };
43 char ctl[CMSG_SPACE (sizeof (int))];
44 int rv, err = MEMIF_ERR_SUCCESS; /* 0 */
46 iov[0].iov_base = (void *) msg;
47 iov[0].iov_len = sizeof (memif_msg_t);
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);
65 err = memif_syscall_error_handler (errno);
66 DBG ("Message type %u sent", msg->type);
70 /* response from memif master - master is ready to handle next message */
72 memif_msg_enq_ack (memif_connection_t * c)
74 libmemif_main_t *lm = &libmemif_main;
75 memif_msg_queue_elt_t *e =
76 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
78 return memif_syscall_error_handler (errno);
80 memset (&e->msg, 0, sizeof (e->msg));
81 e->msg.type = MEMIF_MSG_TYPE_ACK;
85 if (c->msg_queue == NULL)
88 return MEMIF_ERR_SUCCESS; /* 0 */
91 memif_msg_queue_elt_t *cur = c->msg_queue;
92 while (cur->next != NULL)
98 return MEMIF_ERR_SUCCESS; /* 0 */
102 memif_msg_send_hello (int fd)
104 libmemif_main_t *lm = &libmemif_main;
105 memif_msg_t msg = { 0 };
106 memif_msg_hello_t *h = &msg.hello;
107 msg.type = MEMIF_MSG_TYPE_HELLO;
108 h->min_version = MEMIF_VERSION;
109 h->max_version = MEMIF_VERSION;
110 h->max_s2m_ring = MEMIF_MAX_M2S_RING;
111 h->max_m2s_ring = MEMIF_MAX_M2S_RING;
112 h->max_region = MEMIF_MAX_REGION;
113 h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
115 strncpy ((char *) h->name, (char *) lm->app_name, strlen ((char *) lm->app_name));
117 /* msg hello is not enqueued but sent directly,
118 because it is the first msg to be sent */
119 return memif_msg_send (fd, &msg, -1);
122 /* send id and secret (optional) for interface identification */
124 memif_msg_enq_init (memif_connection_t * c)
126 libmemif_main_t *lm = &libmemif_main;
127 memif_msg_queue_elt_t *e =
128 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
130 return memif_syscall_error_handler (errno);
131 memset (e, 0, sizeof (memif_msg_queue_elt_t));
133 memset (&e->msg, 0, sizeof (e->msg));
134 memif_msg_init_t *i = &e->msg.init;
136 e->msg.type = MEMIF_MSG_TYPE_INIT;
138 i->version = MEMIF_VERSION;
139 i->id = c->args.interface_id;
140 i->mode = c->args.mode;
142 strncpy ((char *) i->name, (char *) lm->app_name,
143 strlen ((char *) lm->app_name));
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)
151 return MEMIF_ERR_SUCCESS; /* 0 */
154 memif_msg_queue_elt_t *cur = c->msg_queue;
155 while (cur->next != NULL)
161 return MEMIF_ERR_SUCCESS; /* 0 */
164 /* send information about region specified by region_index */
166 memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
168 libmemif_main_t *lm = &libmemif_main;
169 /* maybe check if region is valid? */
170 memif_region_t *mr = &c->regions[region_index];
172 memif_msg_queue_elt_t *e =
173 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
175 return memif_syscall_error_handler (errno);
177 memset (&e->msg, 0, sizeof (e->msg));
178 memif_msg_add_region_t *ar = &e->msg.add_region;
180 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
182 ar->index = region_index;
183 ar->size = mr->region_size;
186 if (c->msg_queue == NULL)
189 return MEMIF_ERR_SUCCESS; /* 0 */
192 memif_msg_queue_elt_t *cur = c->msg_queue;
193 while (cur->next != NULL)
199 return MEMIF_ERR_SUCCESS; /* 0 */
202 /* send information about ring specified by direction (S2M | M2S) and index */
204 memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
206 libmemif_main_t *lm = &libmemif_main;
207 memif_msg_queue_elt_t *e =
208 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
210 return memif_syscall_error_handler (errno);
212 memset (&e->msg, 0, sizeof (e->msg));
213 memif_msg_add_ring_t *ar = &e->msg.add_ring;
215 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
217 /* TODO: support multiple rings */
219 if (dir == MEMIF_RING_M2S)
220 mq = &c->rx_queues[index];
222 mq = &c->tx_queues[index];
226 ar->offset = mq->offset;
227 ar->region = mq->region;
228 ar->log2_ring_size = mq->log2_ring_size;
229 ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
230 ar->private_hdr_size = 0;
233 if (c->msg_queue == NULL)
236 return MEMIF_ERR_SUCCESS; /* 0 */
239 memif_msg_queue_elt_t *cur = c->msg_queue;
240 while (cur->next != NULL)
246 return MEMIF_ERR_SUCCESS; /* 0 */
249 /* used as connection request from slave */
251 memif_msg_enq_connect (memif_connection_t * c)
253 libmemif_main_t *lm = &libmemif_main;
254 memif_msg_queue_elt_t *e =
255 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
257 return memif_syscall_error_handler (errno);
259 memset (&e->msg, 0, sizeof (e->msg));
260 memif_msg_connect_t *cm = &e->msg.connect;
262 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
264 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
265 strlen ((char *) c->args.interface_name));
268 if (c->msg_queue == NULL)
271 return MEMIF_ERR_SUCCESS; /* 0 */
274 memif_msg_queue_elt_t *cur = c->msg_queue;
275 while (cur->next != NULL)
281 return MEMIF_ERR_SUCCESS; /* 0 */
284 /* used as confirmation of connection by master */
286 memif_msg_enq_connected (memif_connection_t * c)
288 libmemif_main_t *lm = &libmemif_main;
289 memif_msg_queue_elt_t *e =
290 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
292 return memif_syscall_error_handler (errno);
294 memset (&e->msg, 0, sizeof (e->msg));
295 memif_msg_connected_t *cm = &e->msg.connected;
297 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
299 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
300 strlen ((char *) c->args.interface_name));
303 if (c->msg_queue == NULL)
306 return MEMIF_ERR_SUCCESS; /* 0 */
309 memif_msg_queue_elt_t *cur = c->msg_queue;
310 while (cur->next != NULL)
316 return MEMIF_ERR_SUCCESS; /* 0 */
319 /* immediately send disconnect msg */
320 /* specifie protocol for disconnect msg err_code
321 so that it will be compatible with VPP? (header/doc) */
323 memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
325 memif_msg_t msg = { 0 };
326 memif_msg_disconnect_t *d = &msg.disconnect;
328 msg.type = MEMIF_MSG_TYPE_DISCONNECT;
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);
338 return memif_msg_send (fd, &msg, -1);
342 memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
344 memif_msg_hello_t *h = &msg->hello;
346 if (msg->hello.min_version > MEMIF_VERSION ||
347 msg->hello.max_version < MEMIF_VERSION)
349 DBG ("incompatible protocol version");
350 return MEMIF_ERR_PROTO;
353 c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
354 c->args.num_s2m_rings);
355 c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
356 c->args.num_m2s_rings);
357 c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
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));
363 return MEMIF_ERR_SUCCESS; /* 0 */
366 /* handle interface identification (id, secret (optional)) */
368 memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
370 memif_msg_init_t *i = &msg->init;
371 memif_list_elt_t *elt = NULL;
372 memif_list_elt_t elt2;
373 memif_connection_t *c = NULL;
374 libmemif_main_t *lm = &libmemif_main;
375 uint8_t err_string[96];
376 memset (err_string, 0, sizeof (char) * 96);
377 int err = MEMIF_ERR_SUCCESS; /* 0 */
379 if (i->version != MEMIF_VERSION)
381 DBG ("MEMIF_VER_ERR");
382 strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
383 err = MEMIF_ERR_PROTO;
387 get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
390 DBG ("MEMIF_ID_ERR");
391 strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
396 c = (memif_connection_t *) elt->data_struct;
398 if (!(c->args.is_master))
400 DBG ("MEMIF_SLAVE_ERR");
401 strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
402 strlen (MEMIF_SLAVE_ERR));
403 err = MEMIF_ERR_ACCSLAVE;
408 DBG ("MEMIF_CONN_ERR");
409 strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
410 err = MEMIF_ERR_ALRCONN;
416 if (i->mode != c->args.mode)
418 DBG ("MEMIF_MODE_ERR");
419 strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
420 err = MEMIF_ERR_MODE;
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,
436 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
437 err = MEMIF_ERR_SECRET;
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,
446 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
447 err = MEMIF_ERR_SECRET;
453 DBG ("MEMIF_NOSECRET_ERR");
454 strncpy ((char *) err_string,
455 MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
456 err = MEMIF_ERR_NOSECRET;
461 c->read_fn = memif_conn_fd_read_ready;
462 c->write_fn = memif_conn_fd_write_ready;
463 c->error_fn = memif_conn_fd_error;
466 elt2.data_struct = c;
468 add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
469 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
474 memif_msg_send_disconnect (fd, err_string, 0);
475 lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
476 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
482 /* receive region information and add new region to connection (if possible) */
484 memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
487 memif_msg_add_region_t *ar = &msg->add_region;
490 return MEMIF_ERR_NO_SHMFD;
492 if (ar->index > MEMIF_MAX_REGION)
493 return MEMIF_ERR_MAXREG;
496 (memif_region_t *) realloc (c->regions,
497 sizeof (memif_region_t) * (ar->index + 1));
499 return memif_syscall_error_handler (errno);
501 c->regions[ar->index].fd = fd;
502 c->regions[ar->index].region_size = ar->size;
503 c->regions[ar->index].shm = NULL;
505 return MEMIF_ERR_SUCCESS; /* 0 */
508 /* receive ring information and add new ring to connection queue
509 (based on direction S2M | M2S) */
511 memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
513 memif_msg_add_ring_t *ar = &msg->add_ring;
518 return MEMIF_ERR_NO_INTFD;
520 if (ar->private_hdr_size != 0)
521 return MEMIF_ERR_PRIVHDR;
523 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
525 if (ar->index > MEMIF_MAX_S2M_RING)
526 return MEMIF_ERR_MAXRING;
527 if (ar->index >= c->args.num_s2m_rings)
528 return MEMIF_ERR_MAXRING;
531 (memif_queue_t *) realloc (c->rx_queues,
532 sizeof (memif_queue_t) * (ar->index + 1));
533 memset (mq + ar->index, 0, sizeof (memif_queue_t));
535 return memif_syscall_error_handler (errno);
537 c->rx_queues[ar->index].int_fd = fd;
538 c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
539 c->rx_queues[ar->index].region = ar->region;
540 c->rx_queues[ar->index].offset = ar->offset;
541 c->run_args.num_s2m_rings++;
545 if (ar->index > MEMIF_MAX_M2S_RING)
546 return MEMIF_ERR_MAXRING;
547 if (ar->index >= c->args.num_m2s_rings)
548 return MEMIF_ERR_MAXRING;
551 (memif_queue_t *) realloc (c->tx_queues,
552 sizeof (memif_queue_t) * (ar->index + 1));
553 memset (mq + ar->index, 0, sizeof (memif_queue_t));
555 return memif_syscall_error_handler (errno);
557 c->tx_queues[ar->index].int_fd = fd;
558 c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
559 c->tx_queues[ar->index].region = ar->region;
560 c->tx_queues[ar->index].offset = ar->offset;
561 c->run_args.num_m2s_rings++;
564 return MEMIF_ERR_SUCCESS; /* 0 */
567 /* slave -> master */
569 memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
571 memif_msg_connect_t *cm = &msg->connect;
572 libmemif_main_t *lm = &libmemif_main;
573 memif_list_elt_t elt;
576 err = memif_connect1 (c);
577 if (err != MEMIF_ERR_SUCCESS)
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;
590 add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
592 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
597 c->on_connect ((void *) c, c->private_ctx);
602 /* master -> slave */
604 memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
606 memif_msg_connect_t *cm = &msg->connect;
607 libmemif_main_t *lm = &libmemif_main;
610 err = memif_connect1 (c);
611 if (err != MEMIF_ERR_SUCCESS)
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++)
622 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
626 c->on_connect ((void *) c, c->private_ctx);
632 memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
634 memif_msg_disconnect_t *d = &msg->disconnect;
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));
641 /* on returning error, handle function will call memif_disconnect () */
642 DBG ("disconnect received: %s, mode: %d",
643 c->remote_disconnect_string, c->args.mode);
644 return MEMIF_ERR_DISCONNECT;
648 memif_msg_receive (int ifd)
650 char ctl[CMSG_SPACE (sizeof (int)) +
651 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
652 struct msghdr mh = { 0 };
654 memif_msg_t msg = { 0 };
656 int err = MEMIF_ERR_SUCCESS; /* 0 */
659 libmemif_main_t *lm = &libmemif_main;
660 memif_connection_t *c = NULL;
661 memif_socket_t *ms = NULL;
662 memif_list_elt_t *elt = NULL;
664 iov[0].iov_base = (void *) &msg;
665 iov[0].iov_len = sizeof (memif_msg_t);
668 mh.msg_control = ctl;
669 mh.msg_controllen = sizeof (ctl);
671 DBG ("recvmsg fd %d", ifd);
672 size = recvmsg (ifd, &mh, 0);
674 if (size != sizeof (memif_msg_t))
677 return MEMIF_ERR_DISCONNECTED;
679 return MEMIF_ERR_MFMSG;
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);
705 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
707 c = (memif_connection_t *) elt->data_struct;
711 case MEMIF_MSG_TYPE_ACK:
714 case MEMIF_MSG_TYPE_HELLO:
715 if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
717 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
719 if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
721 if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
723 for (i = 0; i < c->run_args.num_s2m_rings; i++)
726 memif_msg_enq_add_ring (c, i,
727 MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
730 for (i = 0; i < c->run_args.num_m2s_rings; i++)
733 memif_msg_enq_add_ring (c, i,
734 MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
737 if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
741 case MEMIF_MSG_TYPE_INIT:
742 get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
745 ms = (memif_socket_t *) elt->data_struct;
746 if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
748 /* c->remote_pid = cr->pid */
749 /* c->remote_uid = cr->uid */
750 /* c->remote_gid = cr->gid */
751 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
754 c = (memif_connection_t *) elt->data_struct;
755 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
759 case MEMIF_MSG_TYPE_ADD_REGION:
761 memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
763 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
767 case MEMIF_MSG_TYPE_ADD_RING:
769 memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
771 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
775 case MEMIF_MSG_TYPE_CONNECT:
776 if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
778 if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
782 case MEMIF_MSG_TYPE_CONNECTED:
783 if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
787 case MEMIF_MSG_TYPE_DISCONNECT:
788 if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
793 return MEMIF_ERR_UNKNOWN_MSG;;
798 c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
799 /* libmemif_main_t *lm = &libmemif_main;
800 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
801 return MEMIF_ERR_SUCCESS; /* 0 */
805 memif_conn_fd_error (memif_connection_t * c)
807 DBG ("connection fd error");
808 strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
809 int err = memif_disconnect_internal (c);
813 /* calls memif_msg_receive to handle pending messages on socket */
815 memif_conn_fd_read_ready (memif_connection_t * c)
818 err = memif_msg_receive (c->fd);
821 err = memif_disconnect_internal (c);
826 /* get msg from msg queue buffer and send it to socket */
828 memif_conn_fd_write_ready (memif_connection_t * c)
830 libmemif_main_t *lm = &libmemif_main;
831 int err = MEMIF_ERR_SUCCESS; /* 0 */
834 if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
837 memif_msg_queue_elt_t *e = c->msg_queue;
841 c->msg_queue = c->msg_queue->next;
843 c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
845 libmemif_main_t *lm = &libmemif_main;
847 lm->control_fd_update (c->fd,
848 MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
850 err = memif_msg_send (c->fd, &e->msg, e->fd);
859 memif_conn_fd_accept_ready (memif_socket_t * ms)
862 struct sockaddr_un client;
864 libmemif_main_t *lm = &libmemif_main;
866 DBG ("accept called");
868 addr_len = sizeof (client);
870 accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
874 return memif_syscall_error_handler (errno);
876 DBG ("accept fd %d", ms->fd);
877 DBG ("conn fd %d", conn_fd);
879 memif_list_elt_t elt;
881 elt.data_struct = ms;
883 add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
884 lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
886 return memif_msg_send_hello (conn_fd);
890 memif_read_ready (int fd)
894 err = memif_msg_receive (fd);
895 DBG ("recv finished");