FD.io VPP  v18.01.1-37-g7ea3975
Vector Packet Processing
socket.c
Go to the documentation of this file.
1 /*
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:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *------------------------------------------------------------------
16  */
17 
18 #define _GNU_SOURCE
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <sys/un.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <net/if.h>
26 #include <sys/ioctl.h>
27 #include <sys/uio.h>
28 #include <sys/mman.h>
29 #include <sys/prctl.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 
33 #include <socket.h>
34 #include <memif.h>
35 
36 /* sends msg to socket */
37 static_fn int
38 memif_msg_send (int fd, memif_msg_t * msg, int afd)
39 {
40  struct msghdr mh = { 0 };
41  struct iovec iov[1];
42  char ctl[CMSG_SPACE (sizeof (int))];
43  int rv, err = MEMIF_ERR_SUCCESS; /* 0 */
44 
45  iov[0].iov_base = (void *) msg;
46  iov[0].iov_len = sizeof (memif_msg_t);
47  mh.msg_iov = iov;
48  mh.msg_iovlen = 1;
49 
50  if (afd > 0)
51  {
52  struct cmsghdr *cmsg;
53  memset (&ctl, 0, sizeof (ctl));
54  mh.msg_control = 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));
61  }
62  rv = sendmsg (fd, &mh, 0);
63  if (rv < 0)
64  err = memif_syscall_error_handler (errno);
65  DBG ("Message type %u sent", msg->type);
66  return err;
67 }
68 
69 /* response from memif master - master is ready to handle next message */
70 static_fn int
72 {
74  (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
75  if (e == NULL)
76  return memif_syscall_error_handler (errno);
77 
78  memset (&e->msg, 0, sizeof (e->msg));
80  e->fd = -1;
81 
82  e->next = NULL;
83  if (c->msg_queue == NULL)
84  {
85  c->msg_queue = e;
86  return MEMIF_ERR_SUCCESS; /* 0 */
87  }
88 
89  memif_msg_queue_elt_t *cur = c->msg_queue;
90  while (cur->next != NULL)
91  {
92  cur = cur->next;
93  }
94  cur->next = e;
95 
96  return MEMIF_ERR_SUCCESS; /* 0 */
97 }
98 
99 static_fn int
101 {
103  memif_msg_t msg = { 0 };
104  memif_msg_hello_t *h = &msg.hello;
112 
113  strncpy ((char *) h->name, lm->app_name, strlen (lm->app_name));
114 
115  /* msg hello is not enqueued but sent directly,
116  because it is the first msg to be sent */
117  return memif_msg_send (fd, &msg, -1);
118 }
119 
120 /* send id and secret (optional) for interface identification */
121 static_fn int
123 {
125  (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
126  if (e == NULL)
127  return memif_syscall_error_handler (errno);
128  memset (e, 0, sizeof (memif_msg_queue_elt_t));
129 
130  memset (&e->msg, 0, sizeof (e->msg));
131  memif_msg_init_t *i = &e->msg.init;
132 
134  e->fd = -1;
135  i->version = MEMIF_VERSION;
136  i->id = c->args.interface_id;
137  i->mode = c->args.mode;
138 
139  strncpy ((char *) i->name, (char *) c->args.instance_name,
140  strlen ((char *) c->args.instance_name));
141  if (c->args.secret)
142  strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
143 
144  e->next = NULL;
145  if (c->msg_queue == NULL)
146  {
147  c->msg_queue = e;
148  return MEMIF_ERR_SUCCESS; /* 0 */
149  }
150 
151  memif_msg_queue_elt_t *cur = c->msg_queue;
152  while (cur->next != NULL)
153  {
154  cur = cur->next;
155  }
156  cur->next = e;
157 
158  return MEMIF_ERR_SUCCESS; /* 0 */
159 }
160 
161 /* send information about region specified by region_index */
162 static_fn int
164 {
165  /* maybe check if region is valid? */
166  memif_region_t *mr = &c->regions[region_index];
167 
169  (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
170  if (e == NULL)
171  return memif_syscall_error_handler (errno);
172 
173  memset (&e->msg, 0, sizeof (e->msg));
175 
177  e->fd = mr->fd;
178  ar->index = region_index;
179  ar->size = mr->region_size;
180 
181  e->next = NULL;
182  if (c->msg_queue == NULL)
183  {
184  c->msg_queue = e;
185  return MEMIF_ERR_SUCCESS; /* 0 */
186  }
187 
188  memif_msg_queue_elt_t *cur = c->msg_queue;
189  while (cur->next != NULL)
190  {
191  cur = cur->next;
192  }
193  cur->next = e;
194 
195  return MEMIF_ERR_SUCCESS; /* 0 */
196 }
197 
198 /* send information about ring specified by direction (S2M | M2S) and index */
199 static_fn int
200 memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
201 {
203  (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
204  if (e == NULL)
205  return memif_syscall_error_handler (errno);
206 
207  memset (&e->msg, 0, sizeof (e->msg));
209 
211 
212  /* TODO: support multiple rings */
213  memif_queue_t *mq;
214  if (dir == MEMIF_RING_M2S)
215  mq = &c->rx_queues[index];
216  else
217  mq = &c->tx_queues[index];
218 
219  e->fd = mq->int_fd;
220  ar->index = index;
221  ar->offset = mq->offset;
222  ar->region = mq->region;
223  ar->log2_ring_size = mq->log2_ring_size;
224  ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
225 
226  e->next = NULL;
227  if (c->msg_queue == NULL)
228  {
229  c->msg_queue = e;
230  return MEMIF_ERR_SUCCESS; /* 0 */
231  }
232 
233  memif_msg_queue_elt_t *cur = c->msg_queue;
234  while (cur->next != NULL)
235  {
236  cur = cur->next;
237  }
238  cur->next = e;
239 
240  return MEMIF_ERR_SUCCESS; /* 0 */
241 }
242 
243 /* used as connection request from slave */
244 static_fn int
246 {
248  (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
249  if (e == NULL)
250  return memif_syscall_error_handler (errno);
251 
252  memset (&e->msg, 0, sizeof (e->msg));
253  memif_msg_connect_t *cm = &e->msg.connect;
254 
256  e->fd = -1;
257  strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
258  strlen ((char *) c->args.interface_name));
259 
260  e->next = NULL;
261  if (c->msg_queue == NULL)
262  {
263  c->msg_queue = e;
264  return MEMIF_ERR_SUCCESS; /* 0 */
265  }
266 
267  memif_msg_queue_elt_t *cur = c->msg_queue;
268  while (cur->next != NULL)
269  {
270  cur = cur->next;
271  }
272  cur->next = e;
273 
274  return MEMIF_ERR_SUCCESS; /* 0 */
275 }
276 
277 /* used as confirmation of connection by master */
278 static_fn int
280 {
282  (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
283  if (e == NULL)
284  return memif_syscall_error_handler (errno);
285 
286  memset (&e->msg, 0, sizeof (e->msg));
288 
290  e->fd = -1;
291  strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
292  strlen ((char *) c->args.interface_name));
293 
294  e->next = NULL;
295  if (c->msg_queue == NULL)
296  {
297  c->msg_queue = e;
298  return MEMIF_ERR_SUCCESS; /* 0 */
299  }
300 
301  memif_msg_queue_elt_t *cur = c->msg_queue;
302  while (cur->next != NULL)
303  {
304  cur = cur->next;
305  }
306  cur->next = e;
307 
308  return MEMIF_ERR_SUCCESS; /* 0 */
309 }
310 
311 /* immediately send disconnect msg */
312  /* specifie protocol for disconnect msg err_code
313  so that it will be compatible with VPP? (header/doc) */
314 int
315 memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
316 {
317  memif_msg_t msg = { 0 };
319 
321  d->code = err_code;
322  uint16_t l = strlen ((char *) err_string);
323  if (l > 96)
324  {
325  DBG ("Disconnect string too long. Sending first 96 characters.");
326  l = 96;
327  }
328  strncpy ((char *) d->string, (char *) err_string, l);
329 
330  return memif_msg_send (fd, &msg, -1);
331 }
332 
333 static_fn int
335 {
336  memif_msg_hello_t *h = &msg->hello;
337 
338  if (msg->hello.min_version > MEMIF_VERSION ||
340  {
341  DBG ("incompatible protocol version");
342  return MEMIF_ERR_PROTO;
343  }
344 
345  c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
346  c->args.num_s2m_rings);
347  c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
348  c->args.num_m2s_rings);
349  c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
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));
354 
355  return MEMIF_ERR_SUCCESS; /* 0 */
356 }
357 
358 /* handle interface identification (id, secret (optional)) */
359 static_fn int
361 {
362  memif_msg_init_t *i = &msg->init;
363  memif_list_elt_t *elt = NULL;
364  memif_list_elt_t elt2;
367  uint8_t err_string[96];
368  memset (err_string, 0, sizeof (char) * 96);
369  int err = MEMIF_ERR_SUCCESS; /* 0 */
370  int err_disc;
371  if (i->version != MEMIF_VERSION)
372  {
373  DBG ("MEMIF_VER_ERR");
374  strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
375  err = MEMIF_ERR_PROTO;
376  goto error;
377  }
378 
379  get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
380  if (elt == NULL)
381  {
382  DBG ("MEMIF_ID_ERR");
383  strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
384  err = MEMIF_ERR_ID;
385  goto error;
386  }
387 
388  c = (memif_connection_t *) elt->data_struct;
389 
390  if (!(c->args.is_master))
391  {
392  DBG ("MEMIF_SLAVE_ERR");
393  strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
394  strlen (MEMIF_SLAVE_ERR));
395  err = MEMIF_ERR_ACCSLAVE;
396  goto error;
397  }
398  if (c->fd != -1)
399  {
400  DBG ("MEMIF_CONN_ERR");
401  strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
402  err = MEMIF_ERR_ALRCONN;
403  goto error;
404  }
405 
406  c->fd = fd;
407 
408  if (i->mode != c->args.mode)
409  {
410  DBG ("MEMIF_MODE_ERR");
411  strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
412  err = MEMIF_ERR_MODE;
413  goto error;
414  }
415 
416  strncpy ((char *) c->remote_name, (char *) i->name,
417  strlen ((char *) i->name));
418 
419  if (c->args.secret)
420  {
421  int r;
422  if (i->secret)
423  {
424  if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
425  {
426  DBG ("MEMIF_SECRET_ERR");
427  strncpy ((char *) err_string,
429  err = MEMIF_ERR_SECRET;
430  goto error;
431  }
432  r = strncmp ((char *) i->secret, (char *) c->args.secret,
433  strlen ((char *) c->args.secret));
434  if (r != 0)
435  {
436  DBG ("MEMIF_SECRET_ERR");
437  strncpy ((char *) err_string,
439  err = MEMIF_ERR_SECRET;
440  goto error;
441  }
442  }
443  else
444  {
445  DBG ("MEMIF_NOSECRET_ERR");
446  strncpy ((char *) err_string,
448  err = MEMIF_ERR_NOSECRET;
449  goto error;
450  }
451  }
452 
453  c->read_fn = memif_conn_fd_read_ready;
454  c->write_fn = memif_conn_fd_write_ready;
455  c->error_fn = memif_conn_fd_error;
456 
457  elt2.key = c->fd;
458  elt2.data_struct = c;
459 
460  add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
462 
463  return err;
464 
465 error:
466  memif_msg_send_disconnect (fd, err_string, 0);
469  close (fd);
470  fd = -1;
471  return err;
472 }
473 
474 /* receive region information and add new region to connection (if possible) */
475 static_fn int
477  int fd)
478 {
480  memif_region_t *mr;
481  if (fd < 0)
482  return MEMIF_ERR_NO_SHMFD;
483 
484  if (ar->index > MEMIF_MAX_REGION)
485  return MEMIF_ERR_MAXREG;
486 
487  mr =
488  (memif_region_t *) realloc (c->regions,
489  sizeof (memif_region_t) * (ar->index + 1));
490  if (mr == NULL)
491  return memif_syscall_error_handler (errno);
492  c->regions = mr;
493  c->regions[ar->index].fd = fd;
494  c->regions[ar->index].region_size = ar->size;
495  c->regions[ar->index].shm = NULL;
496 
497  return MEMIF_ERR_SUCCESS; /* 0 */
498 }
499 
500 /* receive ring information and add new ring to connection queue
501  (based on direction S2M | M2S) */
502 static_fn int
504 {
505  memif_msg_add_ring_t *ar = &msg->add_ring;
506 
507  memif_queue_t *mq;
508 
509  if (fd < 0)
510  return MEMIF_ERR_NO_INTFD;
511 
513  {
514  if (ar->index > MEMIF_MAX_S2M_RING)
515  return MEMIF_ERR_MAXRING;
516  if (ar->index >= c->args.num_s2m_rings)
517  return MEMIF_ERR_MAXRING;
518 
519  mq =
520  (memif_queue_t *) realloc (c->rx_queues,
521  sizeof (memif_queue_t) * (ar->index + 1));
522  memset(mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
523  if (mq == NULL)
524  return memif_syscall_error_handler (errno);
525  c->rx_queues = mq;
526  c->rx_queues[ar->index].int_fd = fd;
527  c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
528  c->rx_queues[ar->index].region = ar->region;
529  c->rx_queues[ar->index].offset = ar->offset;
530  c->run_args.num_s2m_rings++;
531  }
532  else
533  {
534  if (ar->index > MEMIF_MAX_M2S_RING)
535  return MEMIF_ERR_MAXRING;
536  if (ar->index >= c->args.num_m2s_rings)
537  return MEMIF_ERR_MAXRING;
538 
539  mq =
540  (memif_queue_t *) realloc (c->tx_queues,
541  sizeof (memif_queue_t) * (ar->index + 1));
542  memset(mq, 0, sizeof (memif_queue_t) * (ar->index + 1));
543  if (mq == NULL)
544  return memif_syscall_error_handler (errno);
545  c->tx_queues = mq;
546  c->tx_queues[ar->index].int_fd = fd;
547  c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
548  c->tx_queues[ar->index].region = ar->region;
549  c->tx_queues[ar->index].offset = ar->offset;
550  c->run_args.num_m2s_rings++;
551  }
552 
553  return MEMIF_ERR_SUCCESS; /* 0 */
554 }
555 
556 /* slave -> master */
557 static_fn int
559 {
560  memif_msg_connect_t *cm = &msg->connect;
562  memif_list_elt_t elt;
563 
564  int err;
565  err = memif_connect1 (c);
566  if (err != MEMIF_ERR_SUCCESS)
567  return err;
568 
569  strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
570  strlen ((char *) cm->if_name));
571 
572  int i;
573  if (c->on_interrupt != NULL)
574  {
575  for (i = 0; i < c->run_args.num_m2s_rings; i++)
576  {
577  elt.key = c->rx_queues[i].int_fd;
578  elt.data_struct = c;
580 
581  lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
582  }
583 
584  }
585 
586  c->on_connect ((void *) c, c->private_ctx);
587 
588  return err;
589 }
590 
591 /* master -> slave */
592 static_fn int
594 {
595  memif_msg_connect_t *cm = &msg->connect;
597 
598  int err;
599  err = memif_connect1 (c);
600  if (err != MEMIF_ERR_SUCCESS)
601  return err;
602 
603  strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
604  strlen ((char *) cm->if_name));
605 
606  int i;
607  if (c->on_interrupt != NULL)
608  {
609  for (i = 0; i < c->run_args.num_s2m_rings; i++)
610  lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
611  }
612 
613  c->on_connect ((void *) c, c->private_ctx);
614 
615  return err;
616 }
617 
618 static_fn int
620 {
622 
623  memset (c->remote_disconnect_string, 0,
624  sizeof (c->remote_disconnect_string));
625  strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
626  strlen ((char *) d->string));
627 
628  /* on returning error, handle function will call memif_disconnect () */
629  DBG ("disconnect received: %s, mode: %d",
630  c->remote_disconnect_string, c->args.mode);
631  return MEMIF_ERR_DISCONNECT;
632 }
633 
634 static_fn int
636 {
637  char ctl[CMSG_SPACE (sizeof (int)) +
638  CMSG_SPACE (sizeof (struct ucred))] = { 0 };
639  struct msghdr mh = { 0 };
640  struct iovec iov[1];
641  memif_msg_t msg = { 0 };
642  ssize_t size;
643  int err = MEMIF_ERR_SUCCESS; /* 0 */
644  int fd = -1;
645  int i;
648  memif_socket_t *ms = NULL;
649  memif_list_elt_t *elt = NULL;
650 
651  iov[0].iov_base = (void *) &msg;
652  iov[0].iov_len = sizeof (memif_msg_t);
653  mh.msg_iov = iov;
654  mh.msg_iovlen = 1;
655  mh.msg_control = ctl;
656  mh.msg_controllen = sizeof (ctl);
657 
658  DBG ("recvmsg fd %d", ifd);
659  size = recvmsg (ifd, &mh, 0);
660  DBG ("done");
661  if (size != sizeof (memif_msg_t))
662  {
663  if (size == 0)
664  return MEMIF_ERR_DISCONNECTED;
665  else
666  return MEMIF_ERR_MFMSG;
667  }
668 
669  struct ucred *cr = 0;
670  struct cmsghdr *cmsg;
671 
672  cmsg = CMSG_FIRSTHDR (&mh);
673  while (cmsg)
674  {
675  if (cmsg->cmsg_level == SOL_SOCKET)
676  {
677  if (cmsg->cmsg_type == SCM_CREDENTIALS)
678  {
679  cr = (struct ucred *) CMSG_DATA (cmsg);
680  }
681  else if (cmsg->cmsg_type == SCM_RIGHTS)
682  {
683  int *fdp = (int *) CMSG_DATA (cmsg);
684  fd = *fdp;
685  }
686  }
687  cmsg = CMSG_NXTHDR (&mh, cmsg);
688  }
689 
690  DBG ("Message type %u received", msg.type);
691 
692  get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
693  if (elt != NULL)
694  c = (memif_connection_t *) elt->data_struct;
695 
696  switch (msg.type)
697  {
698  case MEMIF_MSG_TYPE_ACK:
699  break;
700 
702  if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
703  return err;
705  return err;
706  if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
707  return err;
708  if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
709  return err;
710  for (i = 0; i < c->run_args.num_s2m_rings; i++)
711  {
712  if ((err =
715  return err;
716  }
717  for (i = 0; i < c->run_args.num_m2s_rings; i++)
718  {
719  if ((err =
722  return err;
723  }
724  if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
725  return err;
726  break;
727 
728  case MEMIF_MSG_TYPE_INIT:
729  get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
730  if (elt == NULL)
731  return -1;
732  ms = (memif_socket_t *) elt->data_struct;
733  if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
734  return err;
735  /* c->remote_pid = cr->pid */
736  /* c->remote_uid = cr->uid */
737  /* c->remote_gid = cr->gid */
738  get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
739  if (elt == NULL)
740  return -1;
741  c = (memif_connection_t *) elt->data_struct;
742  if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
743  return err;
744  break;
745 
747  if ((err =
749  return err;
750  if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
751  return err;
752  break;
753 
755  if ((err =
757  return err;
758  if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
759  return err;
760  break;
761 
763  if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
764  return err;
765  if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
766  return err;
767  break;
768 
770  if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
771  return err;
772  break;
773 
775  if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
776  return err;
777  break;
778 
779  default:
780  return MEMIF_ERR_UNKNOWN_MSG;;
781  break;
782  }
783 
784  if (c != NULL)
785  c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
786 /* libmemif_main_t *lm = &libmemif_main;
787  lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
788  return MEMIF_ERR_SUCCESS; /* 0 */
789 }
790 
791 int
793 {
794  DBG ("connection fd error");
795  strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
796  int err = memif_disconnect_internal (c);
797  return err;
798 }
799 
800 /* calls memif_msg_receive to handle pending messages on socket */
801 int
803 {
804  int err;
805  err = memif_msg_receive (c->fd);
806  if (err != 0)
807  {
808  err = memif_disconnect_internal (c);
809  }
810  return err;
811 }
812 
813 /* get msg from msg queue buffer and send it to socket */
814 int
816 {
817  int err = MEMIF_ERR_SUCCESS; /* 0 */
818 
819 
820  if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
821  goto done;
822 
823  memif_msg_queue_elt_t *e = c->msg_queue;
824  if (e == NULL)
825  goto done;
826 
827  c->msg_queue = c->msg_queue->next;
828 
829  c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
830 /*
831  libmemif_main_t *lm = &libmemif_main;
832 
833  lm->control_fd_update (c->fd,
834  MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
835 */
836  err = memif_msg_send (c->fd, &e->msg, e->fd);
837  free (e);
838  goto done;
839 
840 done:
841  return err;
842 }
843 
844 int
846 {
847  int addr_len;
848  struct sockaddr_un client;
849  int conn_fd;
851 
852  DBG ("accept called");
853 
854  addr_len = sizeof (client);
855  conn_fd =
856  accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
857 
858  if (conn_fd < 0)
859  {
860  return memif_syscall_error_handler (errno);
861  }
862  DBG ("accept fd %d", ms->fd);
863  DBG ("conn fd %d", conn_fd);
864 
865  memif_list_elt_t elt;
866  elt.key = conn_fd;
867  elt.data_struct = ms;
868 
869  add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
871 
872  return memif_msg_send_hello (conn_fd);
873 }
874 
875 int
877 {
878  int err;
879  DBG ("call recv");
880  err = memif_msg_receive (fd);
881  DBG ("recv finished");
882  return err;
883 }
static_fn int memif_msg_receive_disconnect(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:619
memif_version_t max_version
Definition: memif.h:76
memif_interface_mode_t mode
Definition: memif.h:87
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
int memif_conn_fd_write_ready(memif_connection_t *c)
Definition: socket.c:815
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
Definition: socket.c:633
memif_msg_add_region_t add_region
Definition: memif.h:131
#define MEMIF_FD_EVENT_READ
user needs to set events that occured on fd and pass them to memif_control_fd_handler ...
Definition: libmemif.h:88
#define MEMIF_SECRET_ERR
Definition: socket.h:29
uint8_t string[96]
Definition: memif.h:121
#define MEMIF_SLAVE_ERR
Definition: socket.h:26
#define NULL
Definition: clib.h:55
memif_msg_add_ring_t add_ring
Definition: memif.h:132
#define MEMIF_MSG_ADD_RING_FLAG_S2M
Definition: memif.h:101
#define MEMIF_MAX_REGION
Definition: private.h:28
memif_list_elt_t * pending_list
int memif_conn_fd_read_ready(memif_connection_t *c)
Definition: socket.c:802
static_fn int memif_msg_send(int fd, memif_msg_t *msg, int afd)
Definition: socket.c:38
int memif_disconnect_internal(memif_connection_t *c)
Definition: main.c:1004
#define MEMIF_CONNECTION_FLAG_WRITE
memif_control_fd_update_t * control_fd_update
memif_region_index_t region
Definition: memif.h:103
memif_version_t min_version
Definition: memif.h:75
uint8_t secret[24]
Definition: memif.h:88
memif_list_elt_t * interface_list
memif_interface_id_t id
Definition: memif.h:86
uint8_t name[32]
Definition: memif.h:74
#define static_fn
Definition: socket.h:86
memif_ring_index_t max_m2s_ring
Definition: memif.h:78
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:266
#define MEMIF_ID_ERR
Definition: socket.h:25
#define MEMIF_NOSECRET_ERR
Definition: socket.h:30
memif_region_offset_t offset
Definition: private.h:99
#define MEMIF_MAX_M2S_RING
Definition: private.h:26
memif_region_offset_t offset
Definition: memif.h:104
static_fn int memif_msg_receive_connect(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:558
#define MEMIF_MAX_LOG2_RING_SIZE
Definition: private.h:29
static_fn int memif_msg_receive_init(memif_socket_t *ms, int fd, memif_msg_t *msg)
Definition: socket.c:360
memif_msg_init_t init
Definition: memif.h:130
int memif_syscall_error_handler(int err_code)
Definition: main.c:192
memif_ring_index_t index
Definition: memif.h:102
memif_list_elt_t * interrupt_list
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: socket.c:189
#define MEMIF_CONN_ERR
Definition: socket.h:27
static_fn int memif_msg_enq_connected(memif_connection_t *c)
Definition: socket.c:279
static_fn int memif_msg_enq_init(memif_connection_t *c)
Definition: socket.c:122
#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) ...
Definition: libmemif.h:93
uint8_t * app_name
memif_msg_disconnect_t disconnect
Definition: memif.h:135
struct memif_msg_queue_elt * next
static_fn int memif_msg_enq_ack(memif_connection_t *c)
Definition: socket.c:71
static_fn int memif_msg_send_hello(int fd)
Definition: socket.c:100
memif_msg_hello_t hello
Definition: memif.h:129
memif_log2_ring_size_t max_log2_ring_size
Definition: memif.h:80
int add_list_elt(memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
Definition: main.c:303
uint8_t if_name[32]
Definition: memif.h:110
uint8_t name[32]
Definition: memif.h:89
svmdb_client_t * c
vec_header_t h
Definition: buffer.c:282
uint8_t if_name[32]
Definition: memif.h:115
int get_list_elt(memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:338
#define MEMIF_MODE_ERR
Definition: socket.h:28
#define DBG(...)
memif_region_index_t index
Definition: memif.h:94
int memif_read_ready(int fd)
Definition: socket.c:876
memif_log2_ring_size_t log2_ring_size
Definition: memif.h:105
memif_version_t version
Definition: memif.h:85
#define memif_min(a, b)
Definition: memif_private.h:47
memif_ring_index_t max_s2m_ring
Definition: memif.h:79
static_fn int memif_msg_enq_add_region(memif_connection_t *c, uint8_t region_index)
Definition: socket.c:163
memif_msg_connect_t connect
Definition: memif.h:133
#define MEMIF_VERSION
Definition: memif.h:28
u64 size
Definition: vhost-user.h:76
uint16_t interrupt_list_len
static_fn int memif_msg_receive_add_ring(memif_connection_t *c, memif_msg_t *msg, int fd)
Definition: socket.c:503
static_fn int memif_msg_receive(int ifd)
Definition: socket.c:635
uint16_t flags
Definition: memif.h:100
static_fn int memif_msg_receive_add_region(memif_connection_t *c, memif_msg_t *msg, int fd)
Definition: socket.c:476
#define MEMIF_MAX_S2M_RING
Definition: private.h:27
memif_region_size_t size
Definition: memif.h:95
memif_region_index_t max_region
Definition: memif.h:77
memif_msg_type_t type
Definition: memif.h:126
uint16_t pending_list_len
memif_list_elt_t * control_list
static_fn int memif_msg_enq_connect(memif_connection_t *c)
Definition: socket.c:245
memif_log2_ring_size_t log2_ring_size
Definition: private.h:97
static_fn int memif_msg_receive_hello(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:334
memif_msg_connected_t connected
Definition: memif.h:134
uint16_t control_list_len
#define MEMIF_VER_ERR
Definition: socket.h:24
static_fn int memif_msg_receive_connected(memif_connection_t *c, memif_msg_t *msg)
Definition: socket.c:593
int memif_conn_fd_error(memif_connection_t *c)
Definition: socket.c:792
int memif_connect1(memif_connection_t *c)
Definition: main.c:1186
libmemif_main_t libmemif_main
Definition: main.c:66
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:361
#define MEMIF_FD_EVENT_WRITE
Definition: libmemif.h:89
memif_region_index_t region
Definition: private.h:98
static_fn int memif_msg_enq_add_ring(memif_connection_t *c, uint8_t index, uint8_t dir)
Definition: socket.c:200
uint16_t interface_list_len
memif_region_size_t region_size
Definition: private.h:83