FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
memif.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 
19 #define _GNU_SOURCE
20 #include <stdint.h>
21 #include <net/if.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <sys/uio.h>
28 #include <sys/mman.h>
29 #include <sys/prctl.h>
30 #include <sys/eventfd.h>
31 #include <inttypes.h>
32 #include <limits.h>
33 
34 #include <vlib/vlib.h>
35 #include <vlib/unix/unix.h>
36 #include <vppinfra/linux/syscall.h>
37 #include <vnet/plugin/plugin.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vpp/app/version.h>
40 #include <memif/memif.h>
41 #include <memif/private.h>
42 
44 
45 static u32
47 {
48  /* nothing for now */
49  return 0;
50 }
51 
52 static void
54 {
55  if (mq->int_clib_file_index != ~0)
56  {
58  mq->int_clib_file_index = ~0;
59  mq->int_fd = -1;
60  }
61  else if (mq->int_fd > -1)
62  {
63  close (mq->int_fd);
64  mq->int_fd = -1;
65  }
66 }
67 
68 void
70 {
71  memif_main_t *mm = &memif_main;
72  vnet_main_t *vnm = vnet_get_main ();
73  memif_region_t *mr;
74  memif_queue_t *mq;
75  int i;
76 
77  if (mif == 0)
78  return;
79 
80  memif_log_debug (mif, "disconnect %u (%v)", mif->dev_instance,
81  err ? err->what : 0);
82 
83  if (err)
84  {
85  clib_error_t *e = 0;
86  mif->local_disc_string = vec_dup (err->what);
87  if (mif->sock && clib_socket_is_connected (mif->sock))
88  e = memif_msg_send_disconnect (mif, err);
89  clib_error_free (e);
90  }
91 
92  /* set interface down */
93  mif->flags &= ~(MEMIF_IF_FLAG_CONNECTED | MEMIF_IF_FLAG_CONNECTING);
94  if (mif->hw_if_index != ~0)
96 
97  /* close connection socket */
98  if (mif->sock && mif->sock->fd)
99  {
101  mif->socket_file_index);
102  hash_unset (msf->dev_instance_by_fd, mif->sock->fd);
103  memif_socket_close (&mif->sock);
104  }
105  else if (mif->sock)
106  {
107  clib_error_t *err;
108  err = clib_socket_close (mif->sock);
109  if (err)
110  {
111  memif_log_err (mif, "%U", format_clib_error, err);
112  clib_error_free (err);
113  }
114  clib_mem_free (mif->sock);
115  }
116 
117  /* *INDENT-OFF* */
118  vec_foreach_index (i, mif->rx_queues)
119  {
120  mq = vec_elt_at_index (mif->rx_queues, i);
121  if (mq->ring)
122  {
123  int rv;
125  if (rv)
126  memif_log_warn (mif,
127  "Unable to unassign interface %d, queue %d: rc=%d",
128  mif->hw_if_index, i, rv);
129  mq->ring = 0;
130  }
131  }
132 
133  /* free tx and rx queues */
134  vec_foreach (mq, mif->rx_queues)
136  vec_free (mif->rx_queues);
137 
138  vec_foreach (mq, mif->tx_queues)
140  vec_free (mif->tx_queues);
141 
142  /* free memory regions */
143  vec_foreach (mr, mif->regions)
144  {
145  int rv;
146  if (mr->is_external)
147  continue;
148  if ((rv = munmap (mr->shm, mr->region_size)))
149  memif_log_err (mif, "munmap failed, rv = %d", rv);
150  if (mr->fd > -1)
151  close (mr->fd);
152  }
153  /* *INDENT-ON* */
154  vec_free (mif->regions);
155  vec_free (mif->remote_name);
156  vec_free (mif->remote_if_name);
157  clib_fifo_free (mif->msg_queue);
158 }
159 
160 static clib_error_t *
162 {
163  memif_main_t *mm = &memif_main;
164  vnet_main_t *vnm = vnet_get_main ();
165  u16 qid = uf->private_data & 0xFFFF;
166  memif_if_t *mif = vec_elt_at_index (mm->interfaces, uf->private_data >> 16);
167  memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid);
168  u64 b;
169  ssize_t size;
170 
171  size = read (uf->file_descriptor, &b, sizeof (b));
172  if (size < 0)
173  {
174  memif_log_debug (mif, "Failed to read form socket");
175  return 0;
176  }
177 
179  mq->int_count++;
180 
181  return 0;
182 }
183 
184 
185 clib_error_t *
187 {
189  vnet_main_t *vnm = vnet_get_main ();
190  clib_file_t template = { 0 };
191  memif_region_t *mr;
192  int i;
193  clib_error_t *err = NULL;
194 
195  memif_log_debug (mif, "connect %u", mif->dev_instance);
196 
199 
200  /* *INDENT-OFF* */
201  vec_foreach (mr, mif->regions)
202  {
203  if (mr->shm)
204  continue;
205 
206  if (mr->fd < 0)
207  {
208  err = clib_error_return (0, "no memory region fd");
209  goto error;
210  }
211 
212  if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
213  MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
214  {
215  err = clib_error_return_unix (0, "mmap");
216  goto error;
217  }
218  }
219  /* *INDENT-ON* */
220 
221  template.read_function = memif_int_fd_read_ready;
222 
223  /* *INDENT-OFF* */
224  vec_foreach_index (i, mif->tx_queues)
225  {
226  memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i);
227 
228  mq->ring = mif->regions[mq->region].shm + mq->offset;
229  if (mq->ring->cookie != MEMIF_COOKIE)
230  {
231  err = clib_error_return (0, "wrong cookie on tx ring %u", i);
232  goto error;
233  }
234  }
235 
236  vec_foreach_index (i, mif->rx_queues)
237  {
238  memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i);
239  u32 ti;
240  int rv;
241 
242  mq->ring = mif->regions[mq->region].shm + mq->offset;
243  if (mq->ring->cookie != MEMIF_COOKIE)
244  {
245  err = clib_error_return (0, "wrong cookie on tx ring %u", i);
246  goto error;
247  }
248 
249  if (mq->int_fd > -1)
250  {
251  template.file_descriptor = mq->int_fd;
252  template.private_data = (mif->dev_instance << 16) | (i & 0xFFFF);
253  template.description = format (0, "%U rx %u int",
255  mif->dev_instance, i);
256  memif_file_add (&mq->int_clib_file_index, &template);
257  }
260  mq->buffer_pool_index =
262  rv = vnet_hw_interface_set_rx_mode (vnm, mif->hw_if_index, i,
264  if (rv)
266  (mif, "Warning: unable to set rx mode for interface %d queue %d: "
267  "rc=%d", mif->hw_if_index, i, rv);
268  else
269  {
271  vnet_hw_interface_get_rx_mode (vnm, mif->hw_if_index, i, &rxmode);
272 
273  if (rxmode == VNET_HW_INTERFACE_RX_MODE_POLLING)
275  else
277  }
278  }
279  /* *INDENT-ON* */
280 
281  mif->flags &= ~MEMIF_IF_FLAG_CONNECTING;
282  mif->flags |= MEMIF_IF_FLAG_CONNECTED;
283 
286  return 0;
287 
288 error:
289  memif_log_err (mif, "%U", format_clib_error, err);
290  return err;
291 }
292 
295 {
296  if (vec_len (mif->regions) == 0)
297  return NULL;
298  void *p = mif->regions[0].shm;
299  int ring_size =
300  sizeof (memif_ring_t) +
301  sizeof (memif_desc_t) * (1 << mif->run.log2_ring_size);
302  p += (ring_num + type * mif->run.num_s2m_rings) * ring_size;
303 
304  return (memif_ring_t *) p;
305 }
306 
307 clib_error_t *
309 {
311  memif_ring_t *ring = NULL;
312  int i, j;
313  u64 buffer_offset;
314  memif_region_t *r;
315  clib_mem_vm_alloc_t alloc = { 0 };
316  clib_error_t *err;
317 
318  ASSERT (vec_len (mif->regions) == 0);
320 
321  buffer_offset = (mif->run.num_s2m_rings + mif->run.num_m2s_rings) *
322  (sizeof (memif_ring_t) +
323  sizeof (memif_desc_t) * (1 << mif->run.log2_ring_size));
324 
325  r->region_size = buffer_offset;
326 
327  if ((mif->flags & MEMIF_IF_FLAG_ZERO_COPY) == 0)
328  r->region_size += mif->run.buffer_size * (1 << mif->run.log2_ring_size) *
329  (mif->run.num_s2m_rings + mif->run.num_m2s_rings);
330 
331  alloc.name = "memif region";
332  alloc.size = r->region_size;
333  alloc.flags = CLIB_MEM_VM_F_SHARED;
334 
335  err = clib_mem_vm_ext_alloc (&alloc);
336  if (err)
337  goto error;
338 
339  r->fd = alloc.fd;
340  r->shm = alloc.addr;
341 
342  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
343  {
344  vlib_buffer_pool_t *bp;
345  /* *INDENT-OFF* */
347  {
348  vlib_physmem_map_t *pm;
351  r->fd = pm->fd;
352  r->region_size = pm->n_pages << pm->log2_page_size;
353  r->shm = pm->base;
354  r->is_external = 1;
355  }
356  /* *INDENT-ON* */
357  }
358 
359  for (i = 0; i < mif->run.num_s2m_rings; i++)
360  {
361  ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
362  ring->head = ring->tail = 0;
363  ring->cookie = MEMIF_COOKIE;
364 
365  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
366  continue;
367 
368  for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
369  {
370  u16 slot = i * (1 << mif->run.log2_ring_size) + j;
371  ring->desc[j].region = 0;
372  ring->desc[j].offset =
373  buffer_offset + (u32) (slot * mif->run.buffer_size);
374  ring->desc[j].length = mif->run.buffer_size;
375  }
376  }
377  for (i = 0; i < mif->run.num_m2s_rings; i++)
378  {
379  ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
380  ring->head = ring->tail = 0;
381  ring->cookie = MEMIF_COOKIE;
382 
383  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
384  continue;
385 
386  for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
387  {
388  u16 slot =
389  (i + mif->run.num_s2m_rings) * (1 << mif->run.log2_ring_size) + j;
390  ring->desc[j].region = 0;
391  ring->desc[j].offset =
392  buffer_offset + (u32) (slot * mif->run.buffer_size);
393  ring->desc[j].length = mif->run.buffer_size;
394  }
395  }
396 
397  ASSERT (mif->tx_queues == 0);
400 
401  /* *INDENT-OFF* */
402  vec_foreach_index (i, mif->tx_queues)
403  {
404  memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i);
405  if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
406  {
407  err = clib_error_return_unix (0, "eventfd[tx queue %u]", i);
408  goto error;
409  }
410  mq->int_clib_file_index = ~0;
411  mq->ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
412  mq->log2_ring_size = mif->cfg.log2_ring_size;
413  mq->region = 0;
414  mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
415  mq->last_head = 0;
416  mq->type = MEMIF_RING_S2M;
417  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
420  }
421  /* *INDENT-ON* */
422 
423  ASSERT (mif->rx_queues == 0);
426 
427  /* *INDENT-OFF* */
428  vec_foreach_index (i, mif->rx_queues)
429  {
430  memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i);
431  if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
432  {
433  err = clib_error_return_unix (0, "eventfd[rx queue %u]", i);
434  goto error;
435  }
436  mq->int_clib_file_index = ~0;
437  mq->ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
438  mq->log2_ring_size = mif->cfg.log2_ring_size;
439  mq->region = 0;
440  mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
441  mq->last_head = 0;
442  mq->type = MEMIF_RING_M2S;
443  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
446  }
447  /* *INDENT-ON* */
448 
449  return 0;
450 
451 error:
452  memif_log_err (mif, "%U", format_clib_error, err);
453  return err;
454 }
455 
456 static uword
458 {
459  memif_main_t *mm = &memif_main;
460  memif_if_t *mif;
461  clib_socket_t *sock;
462  uword *event_data = 0, event_type;
463  u8 enabled = 0;
464  f64 start_time, last_run_duration = 0, now;
465  clib_error_t *err;
466 
467  sock = clib_mem_alloc (sizeof (clib_socket_t));
468  clib_memset (sock, 0, sizeof (clib_socket_t));
469 
470  while (1)
471  {
472  if (enabled)
474  last_run_duration);
475  else
477 
478  event_type = vlib_process_get_events (vm, &event_data);
479  vec_reset_length (event_data);
480 
481  switch (event_type)
482  {
483  case ~0:
484  break;
486  enabled = 1;
487  break;
489  enabled = 0;
490  continue;
491  default:
492  ASSERT (0);
493  }
494 
495  last_run_duration = start_time = vlib_time_now (vm);
496  /* *INDENT-OFF* */
497  pool_foreach (mif, mm->interfaces,
498  ({
499  memif_socket_file_t * msf = vec_elt_at_index (mm->socket_files, mif->socket_file_index);
500  /* Allow no more than 10us without a pause */
501  now = vlib_time_now (vm);
502  if (now > start_time + 10e-6)
503  {
504  vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
505  start_time = vlib_time_now (vm);
506  }
507 
508  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
509  continue;
510 
511  if (mif->flags & MEMIF_IF_FLAG_CONNECTING)
512  continue;
513 
514  if (mif->flags & MEMIF_IF_FLAG_CONNECTED)
515  continue;
516 
517  if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
518  {
519  clib_memset (sock, 0, sizeof(clib_socket_t));
520  sock->config = (char *) msf->filename;
521  sock->flags = CLIB_SOCKET_F_IS_CLIENT| CLIB_SOCKET_F_SEQPACKET;
522 
523  if ((err = clib_socket_init (sock)))
524  {
525  clib_error_free (err);
526  }
527  else
528  {
529  clib_file_t t = { 0 };
530 
531  t.read_function = memif_slave_conn_fd_read_ready;
532  t.write_function = memif_slave_conn_fd_write_ready;
533  t.error_function = memif_slave_conn_fd_error;
534  t.file_descriptor = sock->fd;
535  t.private_data = mif->dev_instance;
536  memif_file_add (&sock->private_data, &t);
537  t.description = format (0, "%U ctl",
539  mif->dev_instance);
540  hash_set (msf->dev_instance_by_fd, sock->fd, mif->dev_instance);
541 
542  mif->flags |= MEMIF_IF_FLAG_CONNECTING;
543  mif->sock = sock;
544  sock = clib_mem_alloc (sizeof(clib_socket_t));
545  }
546  }
547  }));
548  /* *INDENT-ON* */
549  last_run_duration = vlib_time_now (vm) - last_run_duration;
550  }
551  return 0;
552 }
553 
554 /* *INDENT-OFF* */
556  .function = memif_process,
557  .type = VLIB_NODE_TYPE_PROCESS,
558  .name = "memif-process",
559 };
560 /* *INDENT-ON* */
561 
562 static int
563 memif_add_socket_file (u32 sock_id, u8 * socket_filename)
564 {
565  memif_main_t *mm = &memif_main;
566  uword *p;
567  memif_socket_file_t *msf;
568 
569  p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
570  if (p)
571  {
572  msf = pool_elt_at_index (mm->socket_files, *p);
573  if (strcmp ((char *) msf->filename, (char *) socket_filename) == 0)
574  {
575  /* Silently accept identical "add". */
576  return 0;
577  }
578 
579  /* But don't allow a direct add of a different filename. */
580  return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
581  }
582 
583  pool_get (mm->socket_files, msf);
584  clib_memset (msf, 0, sizeof (memif_socket_file_t));
585 
586  msf->filename = socket_filename;
587  msf->socket_id = sock_id;
588 
590  msf - mm->socket_files);
591 
592  return 0;
593 }
594 
595 static int
597 {
598  memif_main_t *mm = &memif_main;
599  uword *p;
600  memif_socket_file_t *msf;
601 
602  p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
603  if (!p)
604  {
605  /* Don't delete non-existent entries. */
606  return VNET_API_ERROR_INVALID_ARGUMENT;
607  }
608 
609  msf = pool_elt_at_index (mm->socket_files, *p);
610  if (msf->ref_cnt > 0)
611  {
612  return VNET_API_ERROR_UNEXPECTED_INTF_STATE;
613  }
614 
615  vec_free (msf->filename);
616  pool_put (mm->socket_files, msf);
617 
619 
620  return 0;
621 }
622 
623 int
624 memif_socket_filename_add_del (u8 is_add, u32 sock_id, u8 * sock_filename)
625 {
626  char *dir = 0, *tmp;
627  u32 idx = 0;
628 
629  /* allow adding socket id 0 */
630  if ((sock_id == 0 && is_add == 0) || sock_id == ~0)
631  {
632  return VNET_API_ERROR_INVALID_ARGUMENT;
633  }
634 
635  if (is_add == 0)
636  {
637  return memif_delete_socket_file (sock_id);
638  }
639 
640  if (sock_filename == 0 || sock_filename[0] == 0)
641  {
642  return VNET_API_ERROR_INVALID_ARGUMENT;
643  }
644 
645  if (sock_filename[0] != '/')
646  {
647  clib_error_t *error;
648 
649  /* copy runtime dir path */
651  strlen (vlib_unix_get_runtime_dir ()));
652  vec_add1 (dir, '/');
653 
654  /* if sock_filename contains dirs, add them to path */
655  tmp = strrchr ((char *) sock_filename, '/');
656  if (tmp)
657  {
658  idx = tmp - (char *) sock_filename;
659  vec_add (dir, sock_filename, idx);
660  }
661 
662  vec_add1 (dir, '\0');
663  /* create socket dir */
664  error = vlib_unix_recursive_mkdir (dir);
665  if (error)
666  {
667  clib_error_free (error);
668  return VNET_API_ERROR_SYSCALL_ERROR_1;
669  }
670 
671  sock_filename = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
672  sock_filename, 0);
673  }
674  else
675  {
676  sock_filename = vec_dup (sock_filename);
677 
678  /* check if directory exists */
679  tmp = strrchr ((char *) sock_filename, '/');
680  if (tmp)
681  {
682  idx = tmp - (char *) sock_filename;
683  vec_add (dir, sock_filename, idx);
684  vec_add1 (dir, '\0');
685  }
686 
687  /* check dir existance and access rights for effective user/group IDs */
688  if ((dir == NULL)
689  ||
690  (faccessat ( /* ignored */ -1, dir, F_OK | R_OK | W_OK, AT_EACCESS)
691  < 0))
692  {
693  vec_free (dir);
694  return VNET_API_ERROR_INVALID_ARGUMENT;
695  }
696  }
697  vec_free (dir);
698 
699  return memif_add_socket_file (sock_id, sock_filename);
700 }
701 
702 int
704 {
705  vnet_main_t *vnm = vnet_get_main ();
706  memif_main_t *mm = &memif_main;
707  memif_socket_file_t *msf =
709  clib_error_t *err;
710 
711  mif->flags |= MEMIF_IF_FLAG_DELETING;
714 
715  /* bring down the interface */
718 
719  err = clib_error_return (0, "interface deleted");
720  memif_disconnect (mif, err);
721  clib_error_free (err);
722 
723  if (mif->hw_if_index != ~0)
724  {
725  /* remove the interface */
726  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
728  else
730  mif->hw_if_index = ~0;
731  }
732 
733  /* free interface data structures */
734  clib_spinlock_free (&mif->lockp);
735  mhash_unset (&msf->dev_instance_by_id, &mif->id, 0);
736 
737  /* remove socket file */
738  if (--(msf->ref_cnt) == 0)
739  {
740  if (msf->is_listener)
741  {
742  int i;
743  /* *INDENT-OFF* */
746  /* *INDENT-ON* */
747  memif_socket_close (&msf->sock);
748  vec_free (msf->pending_clients);
749  }
752  if (msf->sock)
753  {
754  err = clib_socket_close (msf->sock);
755  if (err)
756  {
757  memif_log_err (mif, "%U", format_clib_error, err);
758  clib_error_free (err);
759  }
760  clib_mem_free (msf->sock);
761  }
762  }
763 
764  clib_memset (mif, 0, sizeof (*mif));
765  pool_put (mm->interfaces, mif);
766 
767  if (pool_elts (mm->interfaces) == 0)
770 
771  return 0;
772 }
773 
774 /* *INDENT-OFF* */
775 VNET_HW_INTERFACE_CLASS (memif_ip_hw_if_class, static) =
776 {
777  .name = "memif-ip",
779 };
780 /* *INDENT-ON* */
781 
782 int
784 {
785  memif_main_t *mm = &memif_main;
787  vnet_main_t *vnm = vnet_get_main ();
788  memif_if_t *mif = 0;
790  clib_error_t *error = 0;
791  int ret = 0;
792  uword *p;
794  memif_socket_file_t *msf = 0;
795  int rv = 0;
796 
798  if (p == 0)
799  {
800  rv = VNET_API_ERROR_INVALID_ARGUMENT;
801  goto done;
802  }
803 
804  msf = vec_elt_at_index (mm->socket_files, p[0]);
805 
806  /* existing socket file can be either master or slave but cannot be both */
807  if (msf->ref_cnt > 0)
808  {
809  if ((!msf->is_listener != !args->is_master))
810  {
811  rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
812  goto done;
813  }
814 
815  p = mhash_get (&msf->dev_instance_by_id, &args->id);
816  if (p)
817  {
818  rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
819  goto done;
820  }
821  }
822 
823  /* Create new socket file */
824  if (msf->ref_cnt == 0)
825  {
826  struct stat file_stat;
827 
828  /* If we are creating listener make sure file doesn't exist or if it
829  * exists thn delete it if it is old socket file */
830  if (args->is_master && (stat ((char *) msf->filename, &file_stat) == 0))
831  {
832  if (S_ISSOCK (file_stat.st_mode))
833  {
834  unlink ((char *) msf->filename);
835  }
836  else
837  {
838  error = clib_error_return (0, "File exists for %s",
839  msf->filename);
840  rv = VNET_API_ERROR_VALUE_EXIST;
841  goto done;
842  }
843  }
844 
845  mhash_init (&msf->dev_instance_by_id, sizeof (uword),
846  sizeof (memif_interface_id_t));
847  msf->dev_instance_by_fd = hash_create (0, sizeof (uword));
848  msf->is_listener = (args->is_master != 0);
849 
850  memif_log_debug (0, "initializing socket file %s", msf->filename);
851  }
852 
853  if (mm->per_thread_data == 0)
854  {
855  int i;
856 
859 
860  for (i = 0; i < tm->n_vlib_mains; i++)
861  {
864  vlib_buffer_t *bt = &ptd->buffer_template;
865  clib_memset (bt, 0, sizeof (vlib_buffer_t));
866  bt->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
868  vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
869 
870  /* initially prealloc copy_ops so we can use
871  _vec_len instead of vec_elen */
873  vec_reset_length (ptd->copy_ops);
875  vec_reset_length (ptd->buffers);
876  }
877  }
878 
879  pool_get (mm->interfaces, mif);
880  clib_memset (mif, 0, sizeof (*mif));
881  mif->dev_instance = mif - mm->interfaces;
882  mif->socket_file_index = msf - mm->socket_files;
883  mif->id = args->id;
884  mif->sw_if_index = mif->hw_if_index = mif->per_interface_next_index = ~0;
885  mif->mode = args->mode;
886  if (args->secret)
887  mif->secret = vec_dup (args->secret);
888 
889  if (tm->n_vlib_mains > 1)
890  clib_spinlock_init (&mif->lockp);
891 
893  {
894 
895  if (!args->hw_addr_set)
896  {
897  f64 now = vlib_time_now (vm);
898  u32 rnd;
899  rnd = (u32) (now * 1e6);
900  rnd = random_u32 (&rnd);
901 
902  memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
903  args->hw_addr[0] = 2;
904  args->hw_addr[1] = 0xfe;
905  }
907  mif->dev_instance, args->hw_addr,
908  &mif->hw_if_index,
910  }
911  else if (mif->mode == MEMIF_INTERFACE_MODE_IP)
912  {
913  mif->hw_if_index =
915  mif->dev_instance,
916  memif_ip_hw_if_class.index,
917  mif->dev_instance);
918  }
919  else
920  error = clib_error_return (0, "unsupported interface mode");
921 
922  if (error)
923  {
924  ret = VNET_API_ERROR_SYSCALL_ERROR_2;
925  goto error;
926  }
927 
928  sw = vnet_get_hw_sw_interface (vnm, mif->hw_if_index);
929  mif->sw_if_index = sw->sw_if_index;
930 
931  mif->cfg.log2_ring_size = args->log2_ring_size;
932  mif->cfg.buffer_size = args->buffer_size;
933  mif->cfg.num_s2m_rings =
934  args->is_master ? args->rx_queues : args->tx_queues;
935  mif->cfg.num_m2s_rings =
936  args->is_master ? args->tx_queues : args->rx_queues;
937 
938  args->sw_if_index = mif->sw_if_index;
939 
940  /* If this is new one, start listening */
941  if (msf->is_listener && msf->ref_cnt == 0)
942  {
943  struct stat file_stat;
945 
946  ASSERT (msf->sock == 0);
947  msf->sock = s;
948 
949  clib_memset (s, 0, sizeof (clib_socket_t));
950  s->config = (char *) msf->filename;
951  s->flags = CLIB_SOCKET_F_IS_SERVER |
954 
955  if ((error = clib_socket_init (s)))
956  {
957  ret = VNET_API_ERROR_SYSCALL_ERROR_4;
958  goto error;
959  }
960 
961  if (stat ((char *) msf->filename, &file_stat) == -1)
962  {
963  ret = VNET_API_ERROR_SYSCALL_ERROR_8;
964  goto error;
965  }
966 
967  clib_file_t template = { 0 };
969  template.file_descriptor = msf->sock->fd;
970  template.private_data = mif->socket_file_index;
971  template.description = format (0, "memif listener %s", msf->filename);
972  memif_file_add (&msf->sock->private_data, &template);
973  }
974 
975  msf->ref_cnt++;
976 
977  if (args->is_master == 0)
978  {
979  mif->flags |= MEMIF_IF_FLAG_IS_SLAVE;
980  if (args->is_zero_copy)
981  mif->flags |= MEMIF_IF_FLAG_ZERO_COPY;
982  }
983 
984  hw = vnet_get_hw_interface (vnm, mif->hw_if_index);
987  memif_input_node.index);
988 
989  mhash_set (&msf->dev_instance_by_id, &mif->id, mif->dev_instance, 0);
990 
991  if (pool_elts (mm->interfaces) == 1)
992  {
995  }
996  goto done;
997 
998 error:
999  if (mif->hw_if_index != ~0)
1000  {
1001  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
1003  else
1005  mif->hw_if_index = ~0;
1006  }
1007  memif_delete_if (vm, mif);
1008  if (error)
1009  {
1010  memif_log_err (mif, "%U", format_clib_error, error);
1011  clib_error_free (error);
1012  }
1013  return ret;
1014 
1015 done:
1016  return rv;
1017 }
1018 
1019 static clib_error_t *
1021 {
1022  memif_main_t *mm = &memif_main;
1023 
1024  clib_memset (mm, 0, sizeof (memif_main_t));
1025 
1026  mm->log_class = vlib_log_register_class ("memif_plugin", 0);
1027  memif_log_debug (0, "initialized");
1028 
1029  /* initialize binary API */
1031 
1032  /*
1033  * Pre-stuff socket filename pool with a non-modifieable mapping
1034  * for socket-id 0 to MEMIF_DEFAULT_SOCKET_FILENAME in the
1035  * default run-time directory.
1036  */
1038 
1039  return 0;
1040 }
1041 
1043 
1044 /* *INDENT-OFF* */
1045 VLIB_PLUGIN_REGISTER () = {
1046  .version = VPP_BUILD_VER,
1047  .description = "Packet Memory Interface (memif) -- Experimental",
1048 };
1049 /* *INDENT-ON* */
1050 
1051 /*
1052  * fd.io coding-style-patch-verification: ON
1053  *
1054  * Local Variables:
1055  * eval: (c-set-style "gnu")
1056  * End:
1057  */
memif_if_t * interfaces
Definition: private.h:241
#define memif_log_err(dev, f,...)
Definition: private.h:55
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:227
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
vlib_log_class_t log_class
Definition: private.h:250
vmrglw vmrglh hi
#define vec_foreach_index(var, v)
Iterate over vector indices.
u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
#define hash_set(h, key, value)
Definition: hash.h:255
u32 flags
Definition: vhost_user.h:115
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:673
#define hash_unset(h, key)
Definition: hash.h:261
u8 * secret
Definition: private.h:170
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:593
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:324
clib_socket_t ** pending_clients
Definition: private.h:87
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static uword clib_socket_is_connected(clib_socket_t *sock)
Definition: socket.h:112
struct memif_if_t::@509 run
unsigned long u64
Definition: types.h:89
memif_socket_file_t * socket_files
Definition: private.h:244
memif_log2_ring_size_t log2_ring_size
Definition: private.h:183
#define NULL
Definition: clib.h:58
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
void * addr
Pointer to allocated memory, set on successful allocation.
Definition: mem.h:384
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:572
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 physmem_map_index
Definition: buffer.h:390
u32 file_descriptor
Definition: file.h:54
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
static clib_error_t * memif_init(vlib_main_t *vm)
Definition: memif.c:1020
int i
vlib_buffer_main_t * buffer_main
Definition: main.h:129
clib_error_t * clib_socket_init(clib_socket_t *s)
Definition: socket.c:384
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: socket.c:190
memif_interface_mode_t mode
Definition: private.h:271
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
Definition: physmem.c:87
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:450
u32 * buffers
Definition: private.h:125
u8 num_m2s_rings
Definition: private.h:185
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
vlib_main_t ** vlib_mains
Definition: buffer.c:321
unsigned char u8
Definition: types.h:56
#define memif_file_del_by_index(a)
Definition: private.h:77
uint32_t length
Definition: memif.h:152
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
Definition: file.h:67
double f64
Definition: types.h:142
static void clib_spinlock_free(clib_spinlock_t *p)
Definition: lock.h:64
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:598
#define CLIB_SOCKET_F_IS_SERVER
Definition: socket.h:58
vnet_hw_interface_rx_mode
Definition: interface.h:52
#define static_always_inline
Definition: clib.h:99
clib_error_t * clib_mem_vm_ext_alloc(clib_mem_vm_alloc_t *a)
Definition: mem.c:193
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
u8 * remote_name
Definition: private.h:178
static int memif_add_socket_file(u32 sock_id, u8 *socket_filename)
Definition: memif.c:563
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:377
uword socket_file_index
Definition: private.h:168
static char * vlib_unix_get_runtime_dir(void)
Definition: unix.h:139
uint32_t cookie
Definition: memif.h:166
char * name
Name for memory allocation, set by caller.
Definition: mem.h:381
u8 buffer_pool_index
Definition: private.h:126
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:308
static_always_inline void vnet_device_input_set_interrupt_pending(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:136
u16 buffer_size
Definition: private.h:186
memif_log2_ring_size_t log2_ring_size
Definition: private.h:272
vnet_hw_interface_flags_t flags
Definition: interface.h:494
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
vnet_device_class_t memif_device_class
u32 per_interface_next_index
Definition: private.h:164
static int memif_delete_socket_file(u32 sock_id)
Definition: memif.c:596
#define clib_error_return(e, args...)
Definition: error.h:99
memif_region_offset_t offset
Definition: private.h:121
uword size
Allocation size, set by caller.
Definition: mem.h:382
#define memif_file_add(a, b)
Definition: private.h:66
unsigned int u32
Definition: types.h:88
void * shm
Definition: private.h:102
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
Definition: interface.c:740
static u32 memif_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
Definition: memif.c:46
mhash_t dev_instance_by_id
Definition: private.h:92
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:57
#define CLIB_MEM_VM_F_SHARED
Definition: mem.h:364
memif_region_index_t region
Definition: memif.h:151
u16 last_head
Definition: private.h:123
#define hash_get(h, key)
Definition: hash.h:249
memif_copy_op_t * copy_ops
Definition: private.h:225
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
uword size
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:925
static_always_inline uword vnet_get_device_input_thread_index(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:127
memif_desc_t desc[0]
Definition: memif.h:173
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:703
int fd
File descriptor, set on successful allocation if CLIB_MEM_VM_F_SHARED is set.
Definition: mem.h:385
uword dev_instance
Definition: private.h:161
static clib_error_t * clib_socket_close(clib_socket_t *sock)
Definition: socket.h:175
clib_spinlock_t lockp
Definition: private.h:156
unsigned short u16
Definition: types.h:57
#define clib_error_return_unix(e, args...)
Definition: error.h:102
int vnet_hw_interface_get_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode *mode)
Definition: devices.c:313
#define hash_free(h)
Definition: hash.h:310
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:375
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:413
memif_interface_id_t id
Definition: private.h:266
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:783
uword int_clib_file_index
Definition: private.h:130
VNET_HW_INTERFACE_CLASS(memif_ip_hw_if_class, static)
static uword mhash_set(mhash_t *h, void *key, uword new_value, uword *old_value)
Definition: mhash.h:117
memif_queue_t * tx_queues
Definition: private.h:175
clib_error_t * memif_connect(memif_if_t *mif)
Definition: memif.c:186
struct memif_if_t::@508 cfg
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
void mhash_init(mhash_t *h, uword n_value_bytes, uword n_key_bytes)
Definition: mhash.c:168
vlib_main_t * vm
Definition: buffer.c:312
u8 * local_disc_string
Definition: private.h:198
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
int memif_socket_filename_add_del(u8 is_add, u32 sock_id, u8 *sock_filename)
Definition: memif.c:624
uint32_t memif_interface_id_t
Definition: memif.h:64
void memif_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: memif.c:69
u32 flags
vm allocation flags: CLIB_MEM_VM_F_SHARED: request shared memory, file descriptor will be provided ...
Definition: mem.h:370
memif_ring_type_t type
Definition: private.h:134
memif_region_t * regions
Definition: private.h:172
#define hash_create(elts, value_bytes)
Definition: hash.h:696
#define ASSERT(truth)
void vnet_hw_interface_assign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, uword thread_index)
Definition: devices.c:139
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
u8 is_add
Definition: ipsec_gre.api:36
static void memif_queue_intfd_close(memif_queue_t *mq)
Definition: memif.c:53
static void mhash_free(mhash_t *h)
Definition: mhash.h:149
static clib_error_t * memif_int_fd_read_ready(clib_file_t *uf)
Definition: memif.c:161
u32 flags
Definition: private.h:157
memif_ring_t * ring
Definition: private.h:118
static void clib_mem_free(void *p)
Definition: mem.h:205
vlib_buffer_t buffer_template
Definition: private.h:229
u32 hw_if_index
Definition: private.h:159
struct _socket_t clib_socket_t
clib_error_t * memif_slave_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:588
#define clib_fifo_free(f)
Definition: fifo.h:257
#define MEMIF_RING_FLAG_MASK_INT
Definition: memif.h:168
static void * clib_mem_alloc(uword size)
Definition: mem.h:132
clib_error_t * memif_slave_conn_fd_error(clib_file_t *uf)
Definition: socket.c:596
#define CLIB_SOCKET_F_SEQPACKET
Definition: socket.h:63
clib_error_t * memif_slave_conn_fd_read_ready(clib_file_t *uf)
Definition: socket.c:545
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 int_count
Definition: private.h:131
void vnet_delete_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:951
memif_region_offset_t offset
Definition: memif.h:153
uword * dev_instance_by_fd
Definition: private.h:95
u8 num_s2m_rings
Definition: private.h:184
VLIB_PLUGIN_REGISTER()
Definition: defs.h:47
clib_error_t * vlib_unix_recursive_mkdir(char *path)
Definition: util.c:103
static_always_inline memif_ring_t * memif_get_ring(memif_if_t *mif, memif_ring_type_t type, u16 ring_num)
Definition: memif.c:294
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define MEMIF_DEFAULT_SOCKET_FILENAME
Definition: private.h:21
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, const u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:278
#define CLIB_SOCKET_F_PASSCRED
Definition: socket.h:64
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:504
#define memif_log_warn(dev, f,...)
Definition: private.h:44
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
a point 2 point interface
Definition: interface.h:357
void memif_socket_close(clib_socket_t **sock)
Definition: socket.c:43
#define clib_error_free(e)
Definition: error.h:86
u8 * remote_if_name
Definition: private.h:179
memif_interface_id_t id
Definition: private.h:158
memif_log2_ring_size_t log2_ring_size
Definition: private.h:119
int vnet_hw_interface_unassign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.c:188
#define vnet_buffer(b)
Definition: buffer.h:369
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
uint16_t flags
Definition: memif.h:167
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, vnet_sw_interface_flags_t flags)
Definition: interface.c:513
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
memif_per_thread_data_t * per_thread_data
Definition: private.h:248
static vlib_node_registration_t memif_process_node
(constructor) VLIB_REGISTER_NODE (memif_process_node)
Definition: memif.c:555
#define memif_log_debug(dev, f,...)
Definition: private.h:33
u8 * remote_disc_string
Definition: private.h:199
memif_ring_type_t
Definition: memif.h:47
#define vec_foreach(var, vec)
Vector iterator.
volatile uint16_t head
Definition: memif.h:169
uword private_data
Definition: file.h:64
Definition: file.h:51
clib_socket_t * sock
Definition: private.h:167
memif_queue_t * rx_queues
Definition: private.h:174
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode mode)
Definition: devices.c:253
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
Definition: socket.c:649
uword * socket_file_index_by_sock_id
Definition: private.h:245
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:167
#define CLIB_SOCKET_F_ALLOW_GROUP_WRITE
Definition: socket.h:62
memif_msg_fifo_elt_t * msg_queue
Definition: private.h:169
memif_main_t memif_main
Definition: memif.c:43
static u8 vlib_buffer_pool_get_default_for_numa(vlib_main_t *vm, u32 numa_node)
Definition: buffer_funcs.h:163
memif_region_index_t region
Definition: private.h:120
clib_socket_t * sock
Definition: private.h:86
#define MEMIF_COOKIE
Definition: memif.h:25
u32 sw_if_index
Definition: private.h:160
static void vnet_hw_interface_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: devices.h:79
volatile uint16_t tail
Definition: memif.h:171
memif_interface_mode_t mode
Definition: private.h:162
static uword memif_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: memif.c:457
u8 * format_memif_device_name(u8 *s, va_list *args)
Definition: device.c:51
memif_region_size_t region_size
Definition: private.h:103
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128