FD.io VPP  v18.07-rc0-415-g6c78436
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  vlib_log_debug (mm->log_class, "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  vlib_log_err (mm->log_class, "%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)
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  clib_warning ("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  vlib_log_debug (mm->log_class, "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 {
188  memif_main_t *mm = &memif_main;
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  vlib_log_debug (mm->log_class, "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  int rv;
240 
241  mq->ring = mif->regions[mq->region].shm + mq->offset;
242  if (mq->ring->cookie != MEMIF_COOKIE)
243  {
244  err = clib_error_return (0, "wrong cookie on tx ring %u", i);
245  goto error;
246  }
247 
248  if (mq->int_fd > -1)
249  {
250  template.file_descriptor = mq->int_fd;
251  template.private_data = (mif->dev_instance << 16) | (i & 0xFFFF);
252  template.description = format (0, "%U rx %u int",
254  mif->dev_instance, i);
255  memif_file_add (&mq->int_clib_file_index, &template);
256  }
258  rv = vnet_hw_interface_set_rx_mode (vnm, mif->hw_if_index, i,
260  if (rv)
262  ("Warning: unable to set rx mode for interface %d queue %d: "
263  "rc=%d", mif->hw_if_index, i, rv);
264  else
265  {
267  vnet_hw_interface_get_rx_mode (vnm, mif->hw_if_index, i, &rxmode);
268 
269  if (rxmode == VNET_HW_INTERFACE_RX_MODE_POLLING)
271  else
273  }
274  }
275  /* *INDENT-ON* */
276 
277  mif->flags &= ~MEMIF_IF_FLAG_CONNECTING;
278  mif->flags |= MEMIF_IF_FLAG_CONNECTED;
279 
282  return 0;
283 
284 error:
285  vlib_log_err (mm->log_class, "%U", format_clib_error, err);
286  return err;
287 }
288 
291 {
292  if (vec_len (mif->regions) == 0)
293  return NULL;
294  void *p = mif->regions[0].shm;
295  int ring_size =
296  sizeof (memif_ring_t) +
297  sizeof (memif_desc_t) * (1 << mif->run.log2_ring_size);
298  p += (ring_num + type * mif->run.num_s2m_rings) * ring_size;
299 
300  return (memif_ring_t *) p;
301 }
302 
303 clib_error_t *
305 {
307  memif_main_t *mm = &memif_main;
308  memif_ring_t *ring = NULL;
309  int i, j;
310  u64 buffer_offset;
311  memif_region_t *r;
312  clib_mem_vm_alloc_t alloc = { 0 };
313  clib_error_t *err;
314 
315  ASSERT (vec_len (mif->regions) == 0);
317 
318  buffer_offset = (mif->run.num_s2m_rings + mif->run.num_m2s_rings) *
319  (sizeof (memif_ring_t) +
320  sizeof (memif_desc_t) * (1 << mif->run.log2_ring_size));
321 
322  r->region_size = buffer_offset;
323 
324  if ((mif->flags & MEMIF_IF_FLAG_ZERO_COPY) == 0)
325  r->region_size += mif->run.buffer_size * (1 << mif->run.log2_ring_size) *
326  (mif->run.num_s2m_rings + mif->run.num_m2s_rings);
327 
328  alloc.name = "memif region";
329  alloc.size = r->region_size;
330  alloc.flags = CLIB_MEM_VM_F_SHARED;
331 
332  err = clib_mem_vm_ext_alloc (&alloc);
333  if (err)
334  goto error;
335 
336  r->fd = alloc.fd;
337  r->shm = alloc.addr;
338 
339  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
340  {
341  vlib_buffer_pool_t *bp;
342  /* *INDENT-OFF* */
344  {
348  r->fd = pr->fd;
349  r->region_size = pr->size;
350  r->shm = pr->mem;
351  r->is_external = 1;
352  }
353  /* *INDENT-ON* */
354  }
355 
356  for (i = 0; i < mif->run.num_s2m_rings; i++)
357  {
358  ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
359  ring->head = ring->tail = 0;
360  ring->cookie = MEMIF_COOKIE;
361 
362  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
363  continue;
364 
365  for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
366  {
367  u16 slot = i * (1 << mif->run.log2_ring_size) + j;
368  ring->desc[j].region = 0;
369  ring->desc[j].offset =
370  buffer_offset + (u32) (slot * mif->run.buffer_size);
371  ring->desc[j].length = mif->run.buffer_size;
372  }
373  }
374  for (i = 0; i < mif->run.num_m2s_rings; i++)
375  {
376  ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
377  ring->head = ring->tail = 0;
378  ring->cookie = MEMIF_COOKIE;
379 
380  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
381  continue;
382 
383  for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
384  {
385  u16 slot =
386  (i + mif->run.num_s2m_rings) * (1 << mif->run.log2_ring_size) + j;
387  ring->desc[j].region = 0;
388  ring->desc[j].offset =
389  buffer_offset + (u32) (slot * mif->run.buffer_size);
390  ring->desc[j].length = mif->run.buffer_size;
391  }
392  }
393 
394  ASSERT (mif->tx_queues == 0);
397 
398  /* *INDENT-OFF* */
399  vec_foreach_index (i, mif->tx_queues)
400  {
401  memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i);
402  if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
403  {
404  err = clib_error_return_unix (0, "eventfd[tx queue %u]", i);
405  goto error;
406  }
407  mq->int_clib_file_index = ~0;
408  mq->ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
409  mq->log2_ring_size = mif->cfg.log2_ring_size;
410  mq->region = 0;
411  mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
412  mq->last_head = 0;
413  mq->type = MEMIF_RING_S2M;
414  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
417  }
418  /* *INDENT-ON* */
419 
420  ASSERT (mif->rx_queues == 0);
423 
424  /* *INDENT-OFF* */
425  vec_foreach_index (i, mif->rx_queues)
426  {
427  memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i);
428  if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
429  {
430  err = clib_error_return_unix (0, "eventfd[rx queue %u]", i);
431  goto error;
432  }
433  mq->int_clib_file_index = ~0;
434  mq->ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
435  mq->log2_ring_size = mif->cfg.log2_ring_size;
436  mq->region = 0;
437  mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
438  mq->last_head = 0;
439  mq->type = MEMIF_RING_M2S;
440  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
443  }
444  /* *INDENT-ON* */
445 
446  return 0;
447 
448 error:
449  vlib_log_err (mm->log_class, "%U", format_clib_error, err);
450  return err;
451 }
452 
453 static uword
455 {
456  memif_main_t *mm = &memif_main;
457  memif_if_t *mif;
458  clib_socket_t *sock;
459  uword *event_data = 0, event_type;
460  u8 enabled = 0;
461  f64 start_time, last_run_duration = 0, now;
462  clib_error_t *err;
463 
464  sock = clib_mem_alloc (sizeof (clib_socket_t));
465  memset (sock, 0, sizeof (clib_socket_t));
466 
467  while (1)
468  {
469  if (enabled)
471  last_run_duration);
472  else
474 
475  event_type = vlib_process_get_events (vm, &event_data);
476  vec_reset_length (event_data);
477 
478  switch (event_type)
479  {
480  case ~0:
481  break;
483  enabled = 1;
484  break;
486  enabled = 0;
487  continue;
488  default:
489  ASSERT (0);
490  }
491 
492  last_run_duration = start_time = vlib_time_now (vm);
493  /* *INDENT-OFF* */
494  pool_foreach (mif, mm->interfaces,
495  ({
496  memif_socket_file_t * msf = vec_elt_at_index (mm->socket_files, mif->socket_file_index);
497  /* Allow no more than 10us without a pause */
498  now = vlib_time_now (vm);
499  if (now > start_time + 10e-6)
500  {
501  vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
502  start_time = vlib_time_now (vm);
503  }
504 
505  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
506  continue;
507 
508  if (mif->flags & MEMIF_IF_FLAG_CONNECTING)
509  continue;
510 
511  if (mif->flags & MEMIF_IF_FLAG_CONNECTED)
512  continue;
513 
514  if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
515  {
516  memset (sock, 0, sizeof(clib_socket_t));
517  sock->config = (char *) msf->filename;
518  sock->flags = CLIB_SOCKET_F_IS_CLIENT| CLIB_SOCKET_F_SEQPACKET;
519 
520  if ((err = clib_socket_init (sock)))
521  {
522  clib_error_free (err);
523  }
524  else
525  {
526  clib_file_t t = { 0 };
527 
528  t.read_function = memif_slave_conn_fd_read_ready;
529  t.write_function = memif_slave_conn_fd_write_ready;
530  t.error_function = memif_slave_conn_fd_error;
531  t.file_descriptor = sock->fd;
532  t.private_data = mif->dev_instance;
533  memif_file_add (&sock->private_data, &t);
534  t.description = format (0, "%U ctl",
536  mif->dev_instance);
537  hash_set (msf->dev_instance_by_fd, sock->fd, mif->dev_instance);
538 
539  mif->flags |= MEMIF_IF_FLAG_CONNECTING;
540  mif->sock = sock;
541  sock = clib_mem_alloc (sizeof(clib_socket_t));
542  }
543  }
544  }));
545  /* *INDENT-ON* */
546  last_run_duration = vlib_time_now (vm) - last_run_duration;
547  }
548  return 0;
549 }
550 
551 /* *INDENT-OFF* */
553  .function = memif_process,
554  .type = VLIB_NODE_TYPE_PROCESS,
555  .name = "memif-process",
556 };
557 /* *INDENT-ON* */
558 
559 static int
560 memif_add_socket_file (u32 sock_id, u8 * socket_filename)
561 {
562  memif_main_t *mm = &memif_main;
563  uword *p;
564  memif_socket_file_t *msf;
565 
566  p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
567  if (p)
568  {
569  msf = pool_elt_at_index (mm->socket_files, *p);
570  if (strcmp ((char *) msf->filename, (char *) socket_filename) == 0)
571  {
572  /* Silently accept identical "add". */
573  return 0;
574  }
575 
576  /* But don't allow a direct add of a different filename. */
577  return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
578  }
579 
580  pool_get (mm->socket_files, msf);
581  memset (msf, 0, sizeof (memif_socket_file_t));
582 
583  msf->filename = socket_filename;
584  msf->socket_id = sock_id;
585 
587  msf - mm->socket_files);
588 
589  return 0;
590 }
591 
592 static int
594 {
595  memif_main_t *mm = &memif_main;
596  uword *p;
597  memif_socket_file_t *msf;
598 
599  p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
600  if (!p)
601  {
602  /* Don't delete non-existent entries. */
603  return VNET_API_ERROR_INVALID_ARGUMENT;
604  }
605 
606  msf = pool_elt_at_index (mm->socket_files, *p);
607  if (msf->ref_cnt > 0)
608  {
609  return VNET_API_ERROR_UNEXPECTED_INTF_STATE;
610  }
611 
612  vec_free (msf->filename);
613  pool_put (mm->socket_files, msf);
614 
616 
617  return 0;
618 }
619 
620 int
621 memif_socket_filename_add_del (u8 is_add, u32 sock_id, u8 * sock_filename)
622 {
623  if (sock_id == 0 || sock_id == ~0)
624  {
625  return VNET_API_ERROR_INVALID_ARGUMENT;
626  }
627 
628  if (is_add == 0)
629  {
630  return memif_delete_socket_file (sock_id);
631  }
632 
633  if (sock_filename == 0 || sock_filename[0] == 0)
634  {
635  return VNET_API_ERROR_INVALID_ARGUMENT;
636  }
637 
638  if (sock_filename[0] != '/')
639  {
640  clib_error_t *error;
642  if (error)
643  {
644  clib_error_free (error);
645  return VNET_API_ERROR_SYSCALL_ERROR_1;
646  }
647 
648  sock_filename = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
649  sock_filename, 0);
650  }
651  else
652  {
653  sock_filename = vec_dup (sock_filename);
654  }
655 
656  return memif_add_socket_file (sock_id, sock_filename);
657 }
658 
659 int
661 {
662  vnet_main_t *vnm = vnet_get_main ();
663  memif_main_t *mm = &memif_main;
664  memif_socket_file_t *msf =
666  clib_error_t *err;
667 
668  mif->flags |= MEMIF_IF_FLAG_DELETING;
671 
672  /* bring down the interface */
675 
676  err = clib_error_return (0, "interface deleted");
677  memif_disconnect (mif, err);
678  clib_error_free (err);
679 
680  if (mif->hw_if_index != ~0)
681  {
682  /* remove the interface */
683  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
685  else
687  mif->hw_if_index = ~0;
688  }
689 
690  /* free interface data structures */
691  clib_spinlock_free (&mif->lockp);
692  mhash_unset (&msf->dev_instance_by_id, &mif->id, 0);
693 
694  /* remove socket file */
695  if (--(msf->ref_cnt) == 0)
696  {
697  if (msf->is_listener)
698  {
699  int i;
700  /* *INDENT-OFF* */
703  /* *INDENT-ON* */
704  memif_socket_close (&msf->sock);
705  vec_free (msf->pending_clients);
706  }
709  if (msf->sock)
710  {
711  err = clib_socket_close (msf->sock);
712  if (err)
713  {
714  vlib_log_err (mm->log_class, "%U", format_clib_error, err);
715  clib_error_free (err);
716  }
717  clib_mem_free (msf->sock);
718  }
719  }
720 
721  memset (mif, 0, sizeof (*mif));
722  pool_put (mm->interfaces, mif);
723 
724  if (pool_elts (mm->interfaces) == 0)
727 
728  return 0;
729 }
730 
731 /* *INDENT-OFF* */
732 VNET_HW_INTERFACE_CLASS (memif_ip_hw_if_class, static) =
733 {
734  .name = "memif-ip",
736 };
737 /* *INDENT-ON* */
738 
739 int
741 {
742  memif_main_t *mm = &memif_main;
744  vnet_main_t *vnm = vnet_get_main ();
745  memif_if_t *mif = 0;
747  clib_error_t *error = 0;
748  int ret = 0;
749  uword *p;
751  memif_socket_file_t *msf = 0;
752  int rv = 0;
753 
755  if (p == 0)
756  {
757  rv = VNET_API_ERROR_INVALID_ARGUMENT;
758  goto done;
759  }
760 
761  msf = vec_elt_at_index (mm->socket_files, p[0]);
762 
763  /* existing socket file can be either master or slave but cannot be both */
764  if (msf->ref_cnt > 0)
765  {
766  if ((!msf->is_listener != !args->is_master))
767  {
768  rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
769  goto done;
770  }
771 
772  p = mhash_get (&msf->dev_instance_by_id, &args->id);
773  if (p)
774  {
775  rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
776  goto done;
777  }
778  }
779 
780  /* Create new socket file */
781  if (msf->ref_cnt == 0)
782  {
783  struct stat file_stat;
784 
785  /* If we are creating listener make sure file doesn't exist or if it
786  * exists thn delete it if it is old socket file */
787  if (args->is_master && (stat ((char *) msf->filename, &file_stat) == 0))
788  {
789  if (S_ISSOCK (file_stat.st_mode))
790  {
791  unlink ((char *) msf->filename);
792  }
793  else
794  {
795  error = clib_error_return (0, "File exists for %s",
796  msf->filename);
797  rv = VNET_API_ERROR_VALUE_EXIST;
798  goto done;
799  }
800  }
801 
802  mhash_init (&msf->dev_instance_by_id, sizeof (uword),
803  sizeof (memif_interface_id_t));
804  msf->dev_instance_by_fd = hash_create (0, sizeof (uword));
805  msf->is_listener = (args->is_master != 0);
806 
807  vlib_log_debug (mm->log_class, "initializing socket file %s",
808  msf->filename);
809  }
810 
811  if (mm->per_thread_data == 0)
812  {
813  int i;
815 
818 
819  fl =
821  for (i = 0; i < tm->n_vlib_mains; i++)
822  {
825  vlib_buffer_t *bt = &ptd->buffer_template;
827  bt->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
829  vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
830 
831  /* initially prealloc copy_ops so we can use
832  _vec_len instead of vec_elen */
834  vec_reset_length (ptd->copy_ops);
836  vec_reset_length (ptd->buffers);
837  }
838  }
839 
840  pool_get (mm->interfaces, mif);
841  memset (mif, 0, sizeof (*mif));
842  mif->dev_instance = mif - mm->interfaces;
843  mif->socket_file_index = msf - mm->socket_files;
844  mif->id = args->id;
845  mif->sw_if_index = mif->hw_if_index = mif->per_interface_next_index = ~0;
846  mif->mode = args->mode;
847  if (args->secret)
848  mif->secret = vec_dup (args->secret);
849 
850  if (tm->n_vlib_mains > 1)
851  clib_spinlock_init (&mif->lockp);
852 
854  {
855 
856  if (!args->hw_addr_set)
857  {
858  f64 now = vlib_time_now (vm);
859  u32 rnd;
860  rnd = (u32) (now * 1e6);
861  rnd = random_u32 (&rnd);
862 
863  memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
864  args->hw_addr[0] = 2;
865  args->hw_addr[1] = 0xfe;
866  }
868  mif->dev_instance, args->hw_addr,
869  &mif->hw_if_index,
871  }
872  else if (mif->mode == MEMIF_INTERFACE_MODE_IP)
873  {
874  mif->hw_if_index =
876  mif->dev_instance,
877  memif_ip_hw_if_class.index,
878  mif->dev_instance);
879  }
880  else
881  error = clib_error_return (0, "unsupported interface mode");
882 
883  if (error)
884  {
885  ret = VNET_API_ERROR_SYSCALL_ERROR_2;
886  goto error;
887  }
888 
889  sw = vnet_get_hw_sw_interface (vnm, mif->hw_if_index);
890  mif->sw_if_index = sw->sw_if_index;
891 
892  mif->cfg.log2_ring_size = args->log2_ring_size;
893  mif->cfg.buffer_size = args->buffer_size;
894  mif->cfg.num_s2m_rings =
895  args->is_master ? args->rx_queues : args->tx_queues;
896  mif->cfg.num_m2s_rings =
897  args->is_master ? args->tx_queues : args->rx_queues;
898 
899  args->sw_if_index = mif->sw_if_index;
900 
901  /* If this is new one, start listening */
902  if (msf->is_listener && msf->ref_cnt == 0)
903  {
904  struct stat file_stat;
906 
907  ASSERT (msf->sock == 0);
908  msf->sock = s;
909 
910  memset (s, 0, sizeof (clib_socket_t));
911  s->config = (char *) msf->filename;
912  s->flags = CLIB_SOCKET_F_IS_SERVER |
915 
916  if ((error = clib_socket_init (s)))
917  {
918  ret = VNET_API_ERROR_SYSCALL_ERROR_4;
919  goto error;
920  }
921 
922  if (stat ((char *) msf->filename, &file_stat) == -1)
923  {
924  ret = VNET_API_ERROR_SYSCALL_ERROR_8;
925  goto error;
926  }
927 
928  clib_file_t template = { 0 };
930  template.file_descriptor = msf->sock->fd;
931  template.private_data = mif->socket_file_index;
932  template.description = format (0, "memif listener %s", msf->filename);
933  memif_file_add (&msf->sock->private_data, &template);
934  }
935 
936  msf->ref_cnt++;
937 
938  if (args->is_master == 0)
939  {
940  mif->flags |= MEMIF_IF_FLAG_IS_SLAVE;
941  if (args->is_zero_copy)
942  mif->flags |= MEMIF_IF_FLAG_ZERO_COPY;
943  }
944 
945  hw = vnet_get_hw_interface (vnm, mif->hw_if_index);
948  memif_input_node.index);
949 
950  mhash_set (&msf->dev_instance_by_id, &mif->id, mif->dev_instance, 0);
951 
952  if (pool_elts (mm->interfaces) == 1)
953  {
956  }
957  goto done;
958 
959 error:
960  if (mif->hw_if_index != ~0)
961  {
962  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
964  else
966  mif->hw_if_index = ~0;
967  }
968  memif_delete_if (vm, mif);
969  if (error)
970  {
971  vlib_log_err (mm->log_class, "%U", format_clib_error, error);
972  clib_error_free (error);
973  }
974  return ret;
975 
976 done:
977  return rv;
978 }
979 
980 static clib_error_t *
982 {
983  memif_main_t *mm = &memif_main;
984  u8 *filename;
985 
986  memset (mm, 0, sizeof (memif_main_t));
987 
988  mm->log_class = vlib_log_register_class ("memif_plugin", 0);
989  vlib_log_debug (mm->log_class, "initialized");
990 
991  /* initialize binary API */
993 
994  /*
995  * Pre-stuff socket filename pool with a non-modifieable mapping
996  * for socket-id 0 to MEMIF_DEFAULT_SOCKET_FILENAME in the
997  * default run-time directory.
998  */
999  filename = format (0, "%s/%s%c",
1002  memif_add_socket_file (0, filename);
1003 
1004  return 0;
1005 }
1006 
1008 
1009 /* *INDENT-OFF* */
1010 VLIB_PLUGIN_REGISTER () = {
1011  .version = VPP_BUILD_VER,
1012  .description = "Packet Memory Interface (experimetal)",
1013 };
1014 /* *INDENT-ON* */
1015 
1016 /*
1017  * fd.io coding-style-patch-verification: ON
1018  *
1019  * Local Variables:
1020  * eval: (c-set-style "gnu")
1021  * End:
1022  */
memif_if_t * interfaces
Definition: private.h:207
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:221
vlib_log_class_t log_class
Definition: private.h:216
vlib_physmem_region_index_t physmem_region
Definition: buffer.h:413
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
#define vlib_log_warn(...)
Definition: log.h:51
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:541
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:699
#define hash_unset(h, key)
Definition: hash.h:261
u8 * secret
Definition: private.h:137
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:619
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:319
clib_socket_t ** pending_clients
Definition: private.h:56
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
unsigned long u64
Definition: types.h:89
memif_socket_file_t * socket_files
Definition: private.h:210
memif_log2_ring_size_t log2_ring_size
Definition: private.h:150
#define NULL
Definition: clib.h:55
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
void * addr
Pointer to allocated memory, set on successful allocation.
Definition: mem.h:346
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:225
#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:574
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 file_descriptor
Definition: file.h:54
static clib_error_t * memif_init(vlib_main_t *vm)
Definition: memif.c:981
int i
clib_error_t * clib_socket_init(clib_socket_t *s)
Definition: socket.c:376
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:237
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:458
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:448
u32 * buffers
Definition: private.h:93
u8 num_m2s_rings
Definition: private.h:152
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:227
unsigned char u8
Definition: types.h:56
#define memif_file_del_by_index(a)
Definition: private.h:46
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 CLIB_SOCKET_F_IS_SERVER
Definition: socket.h:58
vnet_hw_interface_rx_mode
Definition: interface.h:51
#define static_always_inline
Definition: clib.h:93
clib_error_t * clib_mem_vm_ext_alloc(clib_mem_vm_alloc_t *a)
Definition: mem.c:80
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:440
u8 * remote_name
Definition: private.h:145
static int memif_add_socket_file(u32 sock_id, u8 *socket_filename)
Definition: memif.c:560
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
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:542
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:408
uword socket_file_index
Definition: private.h:135
static char * vlib_unix_get_runtime_dir(void)
Definition: unix.h:141
uint32_t cookie
Definition: memif.h:166
char * name
Name for memory allocation, set by caller.
Definition: mem.h:343
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:304
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:153
memif_log2_ring_size_t log2_ring_size
Definition: private.h:238
#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:131
static int memif_delete_socket_file(u32 sock_id)
Definition: memif.c:593
#define clib_error_return(e, args...)
Definition: error.h:99
memif_region_offset_t offset
Definition: private.h:89
uword size
Allocation size, set by caller.
Definition: mem.h:344
#define memif_file_add(a, b)
Definition: private.h:33
unsigned int u32
Definition: types.h:88
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:758
static u32 memif_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
Definition: memif.c:46
#define vlib_log_debug(...)
Definition: log.h:54
mhash_t dev_instance_by_id
Definition: private.h:61
#define fl(x, y)
static vlib_physmem_region_t * vlib_physmem_get_region(vlib_main_t *vm, u8 index)
Definition: physmem_funcs.h:44
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:57
#define CLIB_MEM_VM_F_SHARED
Definition: mem.h:326
memif_region_index_t region
Definition: memif.h:151
u16 last_head
Definition: private.h:91
int int_fd
Definition: private.h:96
#define hash_get(h, key)
Definition: hash.h:249
memif_copy_op_t * copy_ops
Definition: private.h:191
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
uword size
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:915
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:952
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:660
int fd
File descriptor, set on successful allocation if CLIB_MEM_VM_F_SHARED is set.
Definition: mem.h:347
uword dev_instance
Definition: private.h:128
static clib_error_t * clib_socket_close(clib_socket_t *sock)
Definition: socket.h:175
clib_spinlock_t lockp
Definition: private.h:123
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:312
#define hash_free(h)
Definition: hash.h:310
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:439
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:273
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:373
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:433
memif_interface_id_t id
Definition: private.h:232
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:740
uword int_clib_file_index
Definition: private.h:97
#define VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE
Definition: interface.h:496
u8 is_external
Definition: private.h:74
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:142
clib_error_t * memif_connect(memif_if_t *mif)
Definition: memif.c:186
struct memif_if_t::@445 cfg
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:153
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:294
u8 * local_disc_string
Definition: private.h:165
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define clib_warning(format, args...)
Definition: error.h:59
struct memif_if_t::@446 run
int memif_socket_filename_add_del(u8 is_add, u32 sock_id, u8 *sock_filename)
Definition: memif.c:621
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:332
memif_ring_type_t type
Definition: private.h:101
memif_region_t * regions
Definition: private.h:139
#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:138
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
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:124
memif_ring_t * ring
Definition: private.h:86
static void clib_mem_free(void *p)
Definition: mem.h:179
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:273
vlib_buffer_t buffer_template
Definition: private.h:195
u32 hw_if_index
Definition: private.h:126
struct _socket_t clib_socket_t
clib_error_t * memif_slave_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:589
#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:112
clib_error_t * memif_slave_conn_fd_error(clib_file_t *uf)
Definition: socket.c:597
#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:546
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 int_count
Definition: private.h:98
void vnet_delete_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:968
memif_region_offset_t offset
Definition: memif.h:153
uword * dev_instance_by_fd
Definition: private.h:64
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:152
u8 num_s2m_rings
Definition: private.h:151
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:290
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define MEMIF_DEFAULT_SOCKET_FILENAME
Definition: private.h:21
#define CLIB_SOCKET_F_PASSCRED
Definition: socket.h:64
u64 uword
Definition: types.h:112
a point 2 point interface
Definition: interface.h:345
void memif_socket_close(clib_socket_t **sock)
Definition: socket.c:43
#define clib_error_free(e)
Definition: error.h:86
void * shm
Definition: private.h:71
u8 * remote_if_name
Definition: private.h:146
memif_interface_id_t id
Definition: private.h:125
memif_log2_ring_size_t log2_ring_size
Definition: private.h:87
int vnet_hw_interface_unassign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.c:187
static void vlib_buffer_init_for_free_list(vlib_buffer_t *dst, vlib_buffer_free_list_t *fl)
#define vnet_buffer(b)
Definition: buffer.h:360
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
uint16_t flags
Definition: memif.h:167
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:214
static vlib_node_registration_t memif_process_node
(constructor) VLIB_REGISTER_NODE (memif_process_node)
Definition: memif.c:552
vlib_buffer_main_t buffer_main
Definition: buffer.c:52
u8 * remote_disc_string
Definition: private.h:166
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
#define vlib_log_err(...)
Definition: log.h:50
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:549
Definition: file.h:51
clib_socket_t * sock
Definition: private.h:134
memif_queue_t * rx_queues
Definition: private.h:141
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:252
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
Definition: socket.c:650
uword * socket_file_index_by_sock_id
Definition: private.h:211
static vlib_buffer_free_list_t * vlib_buffer_get_free_list(vlib_main_t *vm, vlib_buffer_free_list_index_t free_list_index)
Definition: buffer_funcs.h:662
u32 flags
Definition: vhost-user.h:77
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:62
#define CLIB_SOCKET_F_ALLOW_GROUP_WRITE
Definition: socket.h:62
memif_msg_fifo_elt_t * msg_queue
Definition: private.h:136
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:111
memif_main_t memif_main
Definition: memif.c:43
memif_region_index_t region
Definition: private.h:88
clib_socket_t * sock
Definition: private.h:55
#define MEMIF_COOKIE
Definition: memif.h:25
u32 sw_if_index
Definition: private.h:127
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:129
static uword memif_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: memif.c:454
u8 * format_memif_device_name(u8 *s, va_list *args)
Definition: device.c:51
memif_region_size_t region_size
Definition: private.h:72
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128