FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
memif.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 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 <stdint.h>
20 #include <net/if.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <sys/uio.h>
27 #include <sys/mman.h>
28 #include <sys/prctl.h>
29 #include <inttypes.h>
30 
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33 #include <vnet/plugin/plugin.h>
34 #include <vnet/ethernet/ethernet.h>
35 #include <vpp/app/version.h>
36 #include <memif/memif.h>
37 
38 #define MEMIF_DEBUG 1
39 
40 #if MEMIF_DEBUG == 1
41 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
42 #define DEBUG_UNIX_LOG(...) clib_unix_warning(__VA_ARGS__)
43 #else
44 #define DEBUG_LOG(...)
45 #endif
46 
48 
51 
52 static u32
54 {
55  /* nothing for now */
56  return 0;
57 }
58 
59 static void
61 {
62  memif_main_t *mm = &memif_main;
63 
65  unix_main.file_pool + pending_conn->connection.index);
66  pool_put (mm->pending_conns, pending_conn);
67 }
68 
69 static void
71 {
72  vnet_main_t *vnm = vnet_get_main ();
73  int num_rings = mif->num_s2m_rings + mif->num_m2s_rings;
74  memif_ring_data_t *rd = NULL;
75 
77  vec_foreach (rd, mif->ring_data)
78  {
79  rd->last_head = 0;
80  }
81 
86 }
87 
88 void
90 {
91  vnet_main_t *vnm = vnet_get_main ();
92 
94  if (mif->hw_if_index != ~0)
96 
97  if (mif->interrupt_line.index != ~0)
98  {
101  mif->interrupt_line.index = ~0;
102  mif->interrupt_line.fd = -1; /* closed in unix_file_del */
103  }
104  if (mif->connection.index != ~0)
105  {
107  mif->connection.index = ~0;
108  mif->connection.fd = -1; /* closed in unix_file_del */
109  }
110 
111  // TODO: properly munmap + close memif-owned shared memory segments
112  vec_free (mif->regions);
113 }
114 
115 static clib_error_t *
117  memif_msg_t * req, struct ucred *slave_cr,
118  int shm_fd, int int_fd)
119 {
120  memif_main_t *mm = &memif_main;
122  int fd = pending_conn->connection.fd;
123  unix_file_t *uf = 0;
124  memif_if_t *mif = 0;
125  memif_msg_t resp = { 0 };
126  unix_file_t template = { 0 };
127  void *shm;
128  uword *p;
129  u8 retval = 0;
130  static clib_error_t *error = 0;
131 
132  if (shm_fd == -1)
133  {
134  DEBUG_LOG
135  ("Connection request is missing shared memory file descriptor");
136  retval = 1;
137  goto response;
138  }
139 
140  if (int_fd == -1)
141  {
142  DEBUG_LOG
143  ("Connection request is missing interrupt line file descriptor");
144  retval = 2;
145  goto response;
146  }
147 
148  if (slave_cr == NULL)
149  {
150  DEBUG_LOG ("Connection request is missing slave credentials");
151  retval = 3;
152  goto response;
153  }
154 
155  p = mhash_get (&mm->if_index_by_key, &req->key);
156  if (!p)
157  {
158  DEBUG_LOG
159  ("Connection request with unmatched key (0x%" PRIx64 ")", req->key);
160  retval = 4;
161  goto response;
162  }
163 
164  mif = vec_elt_at_index (mm->interfaces, *p);
165  if (mif->listener_index != pending_conn->listener_index)
166  {
167  DEBUG_LOG
168  ("Connection request with non-matching listener (%d vs. %d)",
169  pending_conn->listener_index, mif->listener_index);
170  retval = 5;
171  goto response;
172  }
173 
174  if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
175  {
176  DEBUG_LOG ("Memif slave does not accept connection requests");
177  retval = 6;
178  goto response;
179  }
180 
181  if (mif->connection.fd != -1)
182  {
183  DEBUG_LOG
184  ("Memif with key 0x%" PRIx64 " is already connected", mif->key);
185  retval = 7;
186  goto response;
187  }
188 
189  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
190  {
191  /* just silently decline the request */
192  retval = 8;
193  goto response;
194  }
195 
196  if (req->shared_mem_size < sizeof (memif_shm_t))
197  {
198  DEBUG_LOG
199  ("Unexpectedly small shared memory segment received from slave.");
200  retval = 9;
201  goto response;
202  }
203 
204  if ((shm =
205  mmap (NULL, req->shared_mem_size, PROT_READ | PROT_WRITE, MAP_SHARED,
206  shm_fd, 0)) == MAP_FAILED)
207  {
209  ("Failed to map shared memory segment received from slave memif");
210  error = clib_error_return_unix (0, "mmap fd %d", shm_fd);
211  retval = 10;
212  goto response;
213  }
214 
215  if (((memif_shm_t *) shm)->cookie != 0xdeadbeef)
216  {
217  DEBUG_LOG
218  ("Possibly corrupted shared memory segment received from slave memif");
219  munmap (shm, req->shared_mem_size);
220  retval = 11;
221  goto response;
222  }
223 
224  mif->log2_ring_size = req->log2_ring_size;
225  mif->num_s2m_rings = req->num_s2m_rings;
226  mif->num_m2s_rings = req->num_m2s_rings;
227  mif->buffer_size = req->buffer_size;
228  mif->remote_pid = slave_cr->pid;
229  mif->remote_uid = slave_cr->uid;
230  vec_add1 (mif->regions, shm);
231 
232  /* register interrupt line */
233  mif->interrupt_line.fd = int_fd;
234  template.read_function = memif_int_fd_read_ready;
235  template.file_descriptor = int_fd;
236  template.private_data = mif->if_index;
237  mif->interrupt_line.index = unix_file_add (&unix_main, &template);
238 
239  /* change context for future messages */
240  uf = vec_elt_at_index (unix_main.file_pool, pending_conn->connection.index);
241  uf->private_data = mif->if_index << 1;
242  mif->connection = pending_conn->connection;
243  pool_put (mm->pending_conns, pending_conn);
244  pending_conn = 0;
245 
246  memif_connect (vm, mif);
247 
248 response:
249  resp.version = MEMIF_VERSION;
251  resp.retval = retval;
252  if (send (fd, &resp, sizeof (resp), 0) < 0)
253  {
254  DEBUG_UNIX_LOG ("Failed to send connection response");
255  error = clib_error_return_unix (0, "send fd %d", fd);
256  if (pending_conn)
257  memif_remove_pending_conn (pending_conn);
258  else
259  memif_disconnect (vm, mif);
260  }
261  if (retval > 0)
262  {
263  if (shm_fd >= 0)
264  close (shm_fd);
265  if (int_fd >= 0)
266  close (int_fd);
267  }
268  return error;
269 }
270 
271 static clib_error_t *
273 {
275 
276  if ((mif->flags & MEMIF_IF_FLAG_IS_SLAVE) == 0)
277  {
278  DEBUG_LOG ("Memif master does not accept connection responses");
279  return 0;
280  }
281 
282  if ((mif->flags & MEMIF_IF_FLAG_CONNECTING) == 0)
283  {
284  DEBUG_LOG ("Unexpected connection response");
285  return 0;
286  }
287 
288  if (resp->retval == 0)
289  memif_connect (vm, mif);
290  else
291  memif_disconnect (vm, mif);
292 
293  return 0;
294 }
295 
296 static clib_error_t *
298 {
299  memif_main_t *mm = &memif_main;
301  memif_if_t *mif = 0;
302  memif_pending_conn_t *pending_conn = 0;
303  int fd_array[2] = { -1, -1 };
304  char ctl[CMSG_SPACE (sizeof (fd_array)) +
305  CMSG_SPACE (sizeof (struct ucred))] = { 0 };
306  struct msghdr mh = { 0 };
307  struct iovec iov[1];
308  struct ucred *cr = 0;
309  memif_msg_t msg = { 0 };
310  struct cmsghdr *cmsg;
311  ssize_t size;
312  static clib_error_t *error = 0;
313 
314  iov[0].iov_base = (void *) &msg;
315  iov[0].iov_len = sizeof (memif_msg_t);
316  mh.msg_iov = iov;
317  mh.msg_iovlen = 1;
318  mh.msg_control = ctl;
319  mh.msg_controllen = sizeof (ctl);
320 
321  /* grab the appropriate context */
322  if (uf->private_data & 1)
323  pending_conn = vec_elt_at_index (mm->pending_conns,
324  uf->private_data >> 1);
325  else
326  mif = vec_elt_at_index (mm->interfaces, uf->private_data >> 1);
327 
328  /* receive the incoming message */
329  size = recvmsg (uf->file_descriptor, &mh, 0);
330  if (size != sizeof (memif_msg_t))
331  {
332  if (size != 0)
333  {
334  DEBUG_UNIX_LOG ("Malformed message received on fd %d",
335  uf->file_descriptor);
336  error = clib_error_return_unix (0, "recvmsg fd %d",
337  uf->file_descriptor);
338  }
339  goto disconnect;
340  }
341 
342  /* check version of the sender's memif plugin */
343  if (msg.version != MEMIF_VERSION)
344  {
345  DEBUG_LOG ("Memif version mismatch");
346  goto disconnect;
347  }
348 
349  /* process the message based on its type */
350  switch (msg.type)
351  {
353  if (pending_conn == 0)
354  {
355  DEBUG_LOG ("Received unexpected connection request");
356  return 0;
357  }
358 
359  /* Read anciliary data */
360  cmsg = CMSG_FIRSTHDR (&mh);
361  while (cmsg)
362  {
363  if (cmsg->cmsg_level == SOL_SOCKET
364  && cmsg->cmsg_type == SCM_CREDENTIALS)
365  {
366  cr = (struct ucred *) CMSG_DATA (cmsg);
367  }
368  else if (cmsg->cmsg_level == SOL_SOCKET
369  && cmsg->cmsg_type == SCM_RIGHTS)
370  {
371  memcpy (fd_array, CMSG_DATA (cmsg), sizeof (fd_array));
372  }
373  cmsg = CMSG_NXTHDR (&mh, cmsg);
374  }
375 
376  return memif_process_connect_req (pending_conn, &msg, cr,
377  fd_array[0], fd_array[1]);
378 
380  if (mif == 0)
381  {
382  DEBUG_LOG ("Received unexpected connection response");
383  return 0;
384  }
385  return memif_process_connect_resp (mif, &msg);
386 
388  goto disconnect;
389 
390  default:
391  DEBUG_LOG ("Received unknown message type");
392  goto disconnect;
393  }
394 
395  return 0;
396 
397 disconnect:
398  if (pending_conn)
399  memif_remove_pending_conn (pending_conn);
400  else
401  memif_disconnect (vm, mif);
402  return error;
403 }
404 
405 static clib_error_t *
407 {
408  memif_main_t *mm = &memif_main;
411  u8 b;
412  ssize_t size;
413 
414  size = read (uf->file_descriptor, &b, sizeof (b));
415  if (0 == size)
416  {
417  /* interrupt line was disconnected */
420  mif->interrupt_line.index = ~0;
421  mif->interrupt_line.fd = -1;
422  }
424  return 0;
425 }
426 
427 static clib_error_t *
429 {
430  memif_main_t *mm = &memif_main;
431  memif_listener_t *listener = 0;
432  memif_pending_conn_t *pending_conn = 0;
433  int addr_len;
434  struct sockaddr_un client;
435  int conn_fd;
436  unix_file_t template = { 0 };
437 
438  listener = pool_elt_at_index (mm->listeners, uf->private_data);
439 
440  addr_len = sizeof (client);
441  conn_fd = accept (uf->file_descriptor,
442  (struct sockaddr *) &client, (socklen_t *) & addr_len);
443 
444  if (conn_fd < 0)
445  return clib_error_return_unix (0, "accept fd %d", uf->file_descriptor);
446 
447  pool_get (mm->pending_conns, pending_conn);
448  pending_conn->index = pending_conn - mm->pending_conns;
449  pending_conn->listener_index = listener->index;
450  pending_conn->connection.fd = conn_fd;
451 
452  template.read_function = memif_conn_fd_read_ready;
453  template.file_descriptor = conn_fd;
454  template.private_data = (pending_conn->index << 1) | 1;
455  pending_conn->connection.index = unix_file_add (&unix_main, &template);
456 
457  return 0;
458 }
459 
460 static void
462 {
463  memif_msg_t msg;
464  struct msghdr mh = { 0 };
465  struct iovec iov[1];
466  struct cmsghdr *cmsg;
467  int mfd = -1;
468  int rv;
469  int fd_array[2] = { -1, -1 };
470  char ctl[CMSG_SPACE (sizeof (fd_array))];
471  memif_ring_t *ring = NULL;
472  int i, j;
473  void *shm = 0;
474  u64 buffer_offset;
475  unix_file_t template = { 0 };
476 
477  msg.version = MEMIF_VERSION;
479  msg.key = mif->key;
480  msg.log2_ring_size = mif->log2_ring_size;
481  msg.num_s2m_rings = mif->num_s2m_rings;
482  msg.num_m2s_rings = mif->num_m2s_rings;
483  msg.buffer_size = mif->buffer_size;
484 
485  buffer_offset = sizeof (memif_shm_t) +
486  (mif->num_s2m_rings + mif->num_m2s_rings) *
487  (sizeof (memif_ring_t) +
488  sizeof (memif_desc_t) * (1 << mif->log2_ring_size));
489 
490  msg.shared_mem_size = buffer_offset +
491  mif->buffer_size * (1 << mif->log2_ring_size) * (mif->num_s2m_rings +
492  mif->num_m2s_rings);
493 
494  if ((mfd = memfd_create ("shared mem", MFD_ALLOW_SEALING)) == -1)
495  {
496  DEBUG_LOG ("Failed to create anonymous file");
497  goto error;
498  }
499 
500  if ((fcntl (mfd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
501  {
502  DEBUG_UNIX_LOG ("Failed to seal an anonymous file off from truncating");
503  goto error;
504  }
505 
506  if ((ftruncate (mfd, msg.shared_mem_size)) == -1)
507  {
508  DEBUG_UNIX_LOG ("Failed to extend the size of an anonymous file");
509  goto error;
510  }
511 
512  if ((shm = mmap (NULL, msg.shared_mem_size, PROT_READ | PROT_WRITE,
513  MAP_SHARED, mfd, 0)) == MAP_FAILED)
514  {
515  DEBUG_UNIX_LOG ("Failed to map anonymous file into memory");
516  goto error;
517  }
518 
519  vec_add1 (mif->regions, shm);
520  ((memif_shm_t *) mif->regions[0])->cookie = 0xdeadbeef;
521 
522  for (i = 0; i < mif->num_s2m_rings; i++)
523  {
524  ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
525  ring->head = ring->tail = 0;
526  for (j = 0; j < (1 << mif->log2_ring_size); j++)
527  {
528  u16 slot = i * (1 << mif->log2_ring_size) + j;
529  ring->desc[j].region = 0;
530  ring->desc[j].offset =
531  buffer_offset + (u32) (slot * mif->buffer_size);
532  ring->desc[j].buffer_length = mif->buffer_size;
533  }
534  }
535  for (i = 0; i < mif->num_m2s_rings; i++)
536  {
537  ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
538  ring->head = ring->tail = 0;
539  for (j = 0; j < (1 << mif->log2_ring_size); j++)
540  {
541  u16 slot =
542  (i + mif->num_s2m_rings) * (1 << mif->log2_ring_size) + j;
543  ring->desc[j].region = 0;
544  ring->desc[j].offset =
545  buffer_offset + (u32) (slot * mif->buffer_size);
546  ring->desc[j].buffer_length = mif->buffer_size;
547  }
548  }
549 
550  iov[0].iov_base = (void *) &msg;
551  iov[0].iov_len = sizeof (memif_msg_t);
552  mh.msg_iov = iov;
553  mh.msg_iovlen = 1;
554 
555  /* create interrupt socket */
556  if (socketpair (AF_UNIX, SOCK_STREAM, 0, fd_array) < 0)
557  {
558  DEBUG_UNIX_LOG ("Failed to create a pair of connected sockets");
559  goto error;
560  }
561 
562  mif->interrupt_line.fd = fd_array[0];
563  template.read_function = memif_int_fd_read_ready;
564  template.file_descriptor = mif->interrupt_line.fd;
565  template.private_data = mif->if_index;
566  mif->interrupt_line.index = unix_file_add (&unix_main, &template);
567 
568  memset (&ctl, 0, sizeof (ctl));
569  mh.msg_control = ctl;
570  mh.msg_controllen = sizeof (ctl);
571  cmsg = CMSG_FIRSTHDR (&mh);
572  cmsg->cmsg_len = CMSG_LEN (sizeof (fd_array));
573  cmsg->cmsg_level = SOL_SOCKET;
574  cmsg->cmsg_type = SCM_RIGHTS;
575  fd_array[0] = mfd;
576  memcpy (CMSG_DATA (cmsg), fd_array, sizeof (fd_array));
577 
579  rv = sendmsg (mif->connection.fd, &mh, 0);
580  if (rv < 0)
581  {
582  DEBUG_UNIX_LOG ("Failed to send memif connection request");
583  goto error;
584  }
585 
586  /* No need to keep the descriptor open,
587  * mmap creates an extra reference to the underlying file */
588  close (mfd);
589  mfd = -1;
590  /* This FD is given to peer, so we can close it */
591  close (fd_array[1]);
592  fd_array[1] = -1;
593  return;
594 
595 error:
596  if (mfd > -1)
597  close (mfd);
598  if (fd_array[1] > -1)
599  close (fd_array[1]);
600  memif_disconnect (vm, mif);
601 }
602 
603 static uword
605 {
606  memif_main_t *mm = &memif_main;
607  memif_if_t *mif;
608  struct sockaddr_un sun;
609  int sockfd;
610  uword *event_data = 0, event_type;
611  unix_file_t template = { 0 };
612  u8 enabled = 0;
613  f64 start_time, last_run_duration = 0, now;
614 
615  sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
616  if (sockfd < 0)
617  {
618  DEBUG_UNIX_LOG ("socket AF_UNIX");
619  return 0;
620  }
621  sun.sun_family = AF_UNIX;
622  template.read_function = memif_conn_fd_read_ready;
623 
624  while (1)
625  {
626  if (enabled)
628  (f64) 3 - last_run_duration);
629  else
631 
632  event_type = vlib_process_get_events (vm, &event_data);
633  vec_reset_length (event_data);
634 
635  switch (event_type)
636  {
637  case ~0:
638  break;
640  enabled = 1;
641  break;
643  enabled = 0;
644  continue;
645  default:
646  ASSERT (0);
647  }
648 
649  last_run_duration = start_time = vlib_time_now (vm);
650  /* *INDENT-OFF* */
651  pool_foreach (mif, mm->interfaces,
652  ({
653  /* Allow no more than 10us without a pause */
654  now = vlib_time_now (vm);
655  if (now > start_time + 10e-6)
656  {
657  vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
658  start_time = vlib_time_now (vm);
659  }
660 
661  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
662  continue;
663 
664  if (mif->flags & MEMIF_IF_FLAG_CONNECTING)
665  continue;
666 
667  if (mif->flags & MEMIF_IF_FLAG_CONNECTED)
668  continue;
669 
670  if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
671  {
672  strncpy (sun.sun_path, (char *) mif->socket_filename,
673  sizeof (sun.sun_path) - 1);
674 
675  if (connect
676  (sockfd, (struct sockaddr *) &sun,
677  sizeof (struct sockaddr_un)) == 0)
678  {
679  mif->connection.fd = sockfd;
680  template.file_descriptor = sockfd;
681  template.private_data = mif->if_index << 1;
682  mif->connection.index = unix_file_add (&unix_main, &template);
683  memif_connect_master (vm, mif);
684 
685  /* grab another fd */
686  sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
687  if (sockfd < 0)
688  {
689  DEBUG_UNIX_LOG ("socket AF_UNIX");
690  return 0;
691  }
692  }
693  }
694  }));
695  /* *INDENT-ON* */
696  last_run_duration = vlib_time_now (vm) - last_run_duration;
697  }
698  return 0;
699 }
700 
701 /* *INDENT-OFF* */
703  .function = memif_process,
704  .type = VLIB_NODE_TYPE_PROCESS,
705  .name = "memif-process",
706 };
707 /* *INDENT-ON* */
708 
709 static void
711 {
713  memif_listener_t *listener = 0;
714  memif_pending_conn_t *pending_conn = 0;
715 
716  memif_disconnect (vm, mif);
717 
718  if (mif->listener_index != (uword) ~ 0)
719  {
720  listener = pool_elt_at_index (mm->listeners, mif->listener_index);
721  if (--listener->usage_counter == 0)
722  {
723  /* not used anymore -> remove the socket and pending connections */
724 
725  /* *INDENT-OFF* */
726  pool_foreach (pending_conn, mm->pending_conns,
727  ({
728  if (pending_conn->listener_index == mif->listener_index)
729  {
730  memif_remove_pending_conn (pending_conn);
731  }
732  }));
733  /* *INDENT-ON* */
734 
736  unix_main.file_pool + listener->socket.index);
737  pool_put (mm->listeners, listener);
738  unlink ((char *) mif->socket_filename);
739  }
740  }
741 
742  if (mif->lockp != 0)
743  {
744  clib_mem_free ((void *) mif->lockp);
745  mif->lockp = 0;
746  }
747 
748  mhash_unset (&mm->if_index_by_key, &mif->key, &mif->if_index);
749  vec_free (mif->socket_filename);
750  vec_free (mif->ring_data);
751 
752  memset (mif, 0, sizeof (*mif));
753  pool_put (mm->interfaces, mif);
754 }
755 
756 int
758 {
759  /* if worker threads are enabled, switch to polling mode */
760  /* *INDENT-OFF* */
762  {
763  vlib_node_set_state (this_vlib_main,
764  memif_input_node.index,
765  VLIB_NODE_STATE_POLLING);
766  }));
767  /* *INDENT-ON* */
768  return 0;
769 }
770 
771 int
773 {
774  /* *INDENT-OFF* */
776  {
777  vlib_node_set_state (this_vlib_main,
778  memif_input_node.index,
779  VLIB_NODE_STATE_INTERRUPT);
780  }));
781  /* *INDENT-ON* */
782  return 0;
783 }
784 
785 int
787 {
788  memif_main_t *mm = &memif_main;
790  vnet_main_t *vnm = vnet_get_main ();
791  memif_if_t *mif = 0;
793  clib_error_t *error = 0;
794  int ret = 0;
795  uword *p;
796 
797  p = mhash_get (&mm->if_index_by_key, &args->key);
798  if (p)
799  return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
800 
801  pool_get (mm->interfaces, mif);
802  memset (mif, 0, sizeof (*mif));
803  mif->key = args->key;
804  mif->if_index = mif - mm->interfaces;
805  mif->sw_if_index = mif->hw_if_index = mif->per_interface_next_index = ~0;
806  mif->listener_index = ~0;
807  mif->connection.index = mif->interrupt_line.index = ~0;
808  mif->connection.fd = mif->interrupt_line.fd = -1;
809 
810  if (tm->n_vlib_mains > 1)
811  {
814  memset ((void *) mif->lockp, 0, CLIB_CACHE_LINE_BYTES);
815  }
816 
817  if (!args->hw_addr_set)
818  {
819  f64 now = vlib_time_now (vm);
820  u32 rnd;
821  rnd = (u32) (now * 1e6);
822  rnd = random_u32 (&rnd);
823 
824  memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
825  args->hw_addr[0] = 2;
826  args->hw_addr[1] = 0xfe;
827  }
828 
830  mif->if_index, args->hw_addr,
831  &mif->hw_if_index,
833 
834  if (error)
835  {
836  clib_error_report (error);
837  ret = VNET_API_ERROR_SYSCALL_ERROR_1;
838  goto error;
839  }
840 
841  sw = vnet_get_hw_sw_interface (vnm, mif->hw_if_index);
842  mif->sw_if_index = sw->sw_if_index;
843 
844  mif->log2_ring_size = args->log2_ring_size;
845  mif->buffer_size = args->buffer_size;
846 
847  /* TODO: make configurable */
848  mif->num_s2m_rings = 1;
849  mif->num_m2s_rings = 1;
850 
851  mhash_set_mem (&mm->if_index_by_key, &args->key, &mif->if_index, 0);
852 
853  if (args->socket_filename != 0)
854  mif->socket_filename = args->socket_filename;
855  else
857 
858  args->sw_if_index = mif->sw_if_index;
859 
860  if (args->is_master)
861  {
862  struct sockaddr_un un = { 0 };
863  struct stat file_stat;
864  int on = 1;
865  memif_listener_t *listener = 0;
866 
867  if (stat ((char *) mif->socket_filename, &file_stat) == 0)
868  {
869  if (!S_ISSOCK (file_stat.st_mode))
870  {
871  errno = ENOTSOCK;
872  ret = VNET_API_ERROR_SYSCALL_ERROR_2;
873  goto error;
874  }
875  /* *INDENT-OFF* */
876  pool_foreach (listener, mm->listeners,
877  ({
878  if (listener->sock_dev == file_stat.st_dev &&
879  listener->sock_ino == file_stat.st_ino)
880  {
881  /* attach memif to the existing listener */
882  mif->listener_index = listener->index;
883  ++listener->usage_counter;
884  goto signal;
885  }
886  }));
887  /* *INDENT-ON* */
888  unlink ((char *) mif->socket_filename);
889  }
890 
891  pool_get (mm->listeners, listener);
892  memset (listener, 0, sizeof (*listener));
893  listener->socket.fd = -1;
894  listener->socket.index = ~0;
895  listener->index = listener - mm->listeners;
896  listener->usage_counter = 1;
897 
898  if ((listener->socket.fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
899  {
900  ret = VNET_API_ERROR_SYSCALL_ERROR_3;
901  goto error;
902  }
903 
904  un.sun_family = AF_UNIX;
905  strncpy ((char *) un.sun_path, (char *) mif->socket_filename,
906  sizeof (un.sun_path) - 1);
907 
908  if (setsockopt (listener->socket.fd, SOL_SOCKET, SO_PASSCRED,
909  &on, sizeof (on)) < 0)
910  {
911  ret = VNET_API_ERROR_SYSCALL_ERROR_4;
912  goto error;
913  }
914  if (bind (listener->socket.fd, (struct sockaddr *) &un,
915  sizeof (un)) == -1)
916  {
917  ret = VNET_API_ERROR_SYSCALL_ERROR_5;
918  goto error;
919  }
920  if (listen (listener->socket.fd, 1) == -1)
921  {
922  ret = VNET_API_ERROR_SYSCALL_ERROR_6;
923  goto error;
924  }
925 
926  if (stat ((char *) mif->socket_filename, &file_stat) == -1)
927  {
928  ret = VNET_API_ERROR_SYSCALL_ERROR_7;
929  goto error;
930  }
931 
932  listener->sock_dev = file_stat.st_dev;
933  listener->sock_ino = file_stat.st_ino;
934 
935  unix_file_t template = { 0 };
937  template.file_descriptor = listener->socket.fd;
938  template.private_data = listener->index;
939  listener->socket.index = unix_file_add (&unix_main, &template);
940 
941  mif->listener_index = listener->index;
942  }
943  else
944  {
945  mif->flags |= MEMIF_IF_FLAG_IS_SLAVE;
946  }
947 
948 #if 0
949  /* use configured or generate random MAC address */
950  if (!args->hw_addr_set &&
951  tm->n_vlib_mains > 1 && pool_elts (mm->interfaces) == 1)
953 #endif
954 
955 signal:
956  if (pool_elts (mm->interfaces) == 1)
957  {
960  }
961  return 0;
962 
963 error:
964  if (mif->hw_if_index != ~0)
965  {
966  ethernet_delete_interface (vnm, mif->hw_if_index);
967  mif->hw_if_index = ~0;
968  }
969  memif_close_if (mm, mif);
970  return ret;
971 }
972 
973 int
975 {
976  vnet_main_t *vnm = vnet_get_main ();
977  memif_main_t *mm = &memif_main;
978  memif_if_t *mif;
979  uword *p;
980 
981  p = mhash_get (&mm->if_index_by_key, &key);
982  if (p == NULL)
983  {
984  clib_warning ("Memory interface with key 0x%" PRIx64 " does not exist",
985  key);
986  return VNET_API_ERROR_SYSCALL_ERROR_1;
987  }
988  mif = pool_elt_at_index (mm->interfaces, p[0]);
990 
991  /* bring down the interface */
994 
995  /* remove the interface */
997  mif->hw_if_index = ~0;
998  memif_close_if (mm, mif);
999 
1000  if (pool_elts (mm->interfaces) == 0)
1001  {
1004  }
1005 
1006 #if 0
1007  if (tm->n_vlib_mains > 1 && pool_elts (mm->interfaces) == 0)
1009 #endif
1010 
1011  return 0;
1012 }
1013 
1014 static clib_error_t *
1016 {
1017  memif_main_t *mm = &memif_main;
1020  uword *p;
1021 
1022  memset (mm, 0, sizeof (memif_main_t));
1023 
1024  mm->input_cpu_first_index = 0;
1025  mm->input_cpu_count = 1;
1026 
1027  /* initialize binary API */
1029 
1030  /* find out which cpus will be used for input */
1031  p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1032  tr = p ? (vlib_thread_registration_t *) p[0] : 0;
1033 
1034  if (tr && tr->count > 0)
1035  {
1037  mm->input_cpu_count = tr->count;
1038  }
1039 
1040  mhash_init (&mm->if_index_by_key, sizeof (uword), sizeof (u64));
1041 
1044 
1045  /* set default socket filename */
1048  strncpy ((char *) mm->default_socket_filename,
1050  vec_len (mm->default_socket_filename) - 1);
1051 
1052  return 0;
1053 }
1054 
1056 
1057 /* *INDENT-OFF* */
1058 VLIB_PLUGIN_REGISTER () = {
1059  .version = VPP_BUILD_VER,
1060  .description = "Packet Memory Interface (experimetal)",
1061 };
1062 /* *INDENT-ON* */
1063 
1064 /*
1065  * fd.io coding-style-patch-verification: ON
1066  *
1067  * Local Variables:
1068  * eval: (c-set-style "gnu")
1069  * End:
1070  */
memif_if_t * interfaces
Definition: memif.h:143
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
unix_file_t * file_pool
Definition: unix.h:89
#define F_SEAL_SHRINK
Definition: memif.h:254
void memif_disconnect(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:89
vmrglw vmrglh hi
static int memfd_create(const char *name, unsigned int flags)
Definition: memif.h:213
u16 num_s2m_rings
Definition: memif.h:33
static clib_error_t * memif_conn_fd_accept_ready(unix_file_t *uf)
Definition: memif.c:428
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
memif_desc_t desc[0]
Definition: memif.h:61
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:530
u32 buffer_length
Definition: memif.h:48
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:683
unix_file_function_t * read_function
Definition: unix.h:62
volatile u32 * lockp
Definition: memif.h:101
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:603
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:295
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
u8 log2_ring_size
Definition: memif.h:31
u16 buffer_size
Definition: memif.h:35
static void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:181
unix_main_t unix_main
Definition: main.c:59
#define NULL
Definition: clib.h:55
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:358
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:185
#define MFD_ALLOW_SEALING
Definition: memif.h:249
#define MEMIF_MSG_TYPE_DISCONNECT
Definition: memif.h:27
u8 retval
Definition: memif.h:40
#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:1015
u16 head
Definition: memif.h:59
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:379
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:447
static void memif_close_if(memif_main_t *mm, memif_if_t *mif)
Definition: memif.c:710
#define DEBUG_UNIX_LOG(...)
Definition: memif.c:42
u8 num_m2s_rings
Definition: memif.h:125
u16 usage_counter
Definition: memif.h:88
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
memif_file_t connection
Definition: memif.h:117
uid_t remote_uid
Definition: memif.h:132
u8 type
Definition: memif.h:24
#define MEMIF_IF_FLAG_IS_SLAVE
Definition: memif.h:104
u32 input_cpu_count
Definition: memif.h:164
#define MEMIF_MSG_TYPE_CONNECT_RESP
Definition: memif.h:26
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
int memif_delete_if(vlib_main_t *vm, u64 key)
Definition: memif.c:974
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
memif_pending_conn_t * pending_conns
Definition: memif.h:149
static clib_error_t * memif_int_fd_read_ready(unix_file_t *uf)
Definition: memif.c:406
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:376
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
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:526
u16 region
Definition: memif.h:47
u16 buffer_size
Definition: memif.h:126
u8 log2_ring_size
Definition: memif.h:123
memif_file_t interrupt_line
Definition: memif.h:118
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u32 per_interface_next_index
Definition: memif.h:114
unsigned long u64
Definition: types.h:89
static u32 memif_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
Definition: memif.c:53
static void memif_connect_master(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:461
u32 input_cpu_first_index
Definition: memif.h:161
u8 * default_socket_filename
Definition: memif.h:167
#define F_ADD_SEALS
Definition: memif.h:250
memif_file_t socket
Definition: memif.h:87
static uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:136
void ** regions
Definition: memif.h:121
u64 offset
Definition: memif.h:51
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
u8 * socket_filename
Definition: memif.h:119
u32 file_descriptor
Definition: unix.h:52
#define DEBUG_LOG(...)
Definition: memif.c:41
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:930
uword private_data
Definition: unix.h:59
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:271
#define clib_error_return_unix(e, args...)
Definition: error.h:114
pid_t remote_pid
Definition: memif.h:131
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:293
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:241
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:374
uword listener_index
Definition: memif.h:116
u64 key
Definition: memif.h:109
#define MEMIF_IF_FLAG_CONNECTING
Definition: memif.h:105
u16 num_m2s_rings
Definition: memif.h:34
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:786
#define foreach_vlib_main(body)
Definition: threads.h:225
#define MEMIF_MSG_TYPE_CONNECT_REQ
Definition: memif.h:25
static clib_error_t * memif_conn_fd_read_ready(unix_file_t *uf)
Definition: memif.c:297
static clib_error_t * memif_process_connect_req(memif_pending_conn_t *pending_conn, memif_msg_t *req, struct ucred *slave_cr, int shm_fd, int int_fd)
Definition: memif.c:116
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:276
#define MEMIF_IF_FLAG_ADMIN_UP
Definition: memif.h:103
int memif_worker_thread_disable()
Definition: memif.c:772
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
static void memif_remove_pending_conn(memif_pending_conn_t *pending_conn)
Definition: memif.c:60
u64 key
Definition: memif.h:30
#define clib_warning(format, args...)
Definition: error.h:59
#define MEMIF_IF_FLAG_CONNECTED
Definition: memif.h:106
uword if_index
Definition: memif.h:110
u32 ** rx_buffers
Definition: memif.h:155
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
uword index
Definition: memif.h:84
u32 flags
Definition: memif.h:102
memif_ring_data_t * ring_data
Definition: memif.h:128
u64 size
Definition: vhost-user.h:77
static void clib_mem_free(void *p)
Definition: mem.h:176
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:246
#define clib_error_report(e)
Definition: error.h:125
#define MEMIF_DEFAULT_SOCKET_FILENAME
Definition: memif.h:168
u32 hw_if_index
Definition: memif.h:111
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:146
uword * thread_registrations_by_name
Definition: threads.h:276
memif_listener_t * listeners
Definition: memif.h:146
static void memif_connect(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:70
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
u8 num_s2m_rings
Definition: memif.h:124
vnet_device_class_t memif_device_class
VLIB_PLUGIN_REGISTER()
#define MEMIF_IF_FLAG_DELETING
Definition: memif.h:107
unsigned short u16
Definition: types.h:57
u32 index
Definition: memif.h:79
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
int memif_worker_thread_enable()
Definition: memif.c:757
unsigned char u8
Definition: types.h:56
Definition: unix.h:49
#define MEMIF_VERSION
Definition: memif.h:23
static_always_inline memif_ring_t * memif_get_ring(memif_if_t *mif, memif_ring_type_t type, u16 ring_num)
Definition: memif.h:225
u32 shared_mem_size
Definition: memif.h:37
mhash_t if_index_by_key
Definition: memif.h:158
#define hash_get_mem(h, key)
Definition: hash.h:268
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:117
u16 version
Definition: memif.h:20
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static vlib_node_registration_t memif_process_node
(constructor) VLIB_REGISTER_NODE (memif_process_node)
Definition: memif.c:702
static clib_error_t * memif_process_connect_resp(memif_if_t *mif, memif_msg_t *resp)
Definition: memif.c:272
#define vec_foreach(var, vec)
Vector iterator.
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:538
u16 tail
Definition: memif.h:60
int fd
Definition: memif.h:78
u32 flags
Definition: vhost-user.h:78
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
memif_main_t memif_main
Definition: memif.c:47
uword listener_index
Definition: memif.h:95
u32 sw_if_index
Definition: memif.h:112
static uword memif_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: memif.c:604
static void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:146
memif_file_t connection
Definition: memif.h:94
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109