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:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
21 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
28 #include <sys/prctl.h>
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>
41 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
42 #define DEBUG_UNIX_LOG(...) clib_unix_warning(__VA_ARGS__)
44 #define DEBUG_LOG(...)
47 memif_main_t memif_main;
49 static clib_error_t *memif_conn_fd_read_ready (unix_file_t * uf);
50 static clib_error_t *memif_int_fd_read_ready (unix_file_t * uf);
53 memif_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
60 memif_remove_pending_conn (memif_pending_conn_t * pending_conn)
62 memif_main_t *mm = &memif_main;
64 unix_file_del (&unix_main,
65 unix_main.file_pool + pending_conn->connection.index);
66 pool_put (mm->pending_conns, pending_conn);
70 memif_connect (vlib_main_t * vm, memif_if_t * mif)
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;
76 vec_validate_aligned (mif->ring_data, num_rings - 1, CLIB_CACHE_LINE_BYTES);
77 vec_foreach (rd, mif->ring_data)
82 mif->flags &= ~MEMIF_IF_FLAG_CONNECTING;
83 mif->flags |= MEMIF_IF_FLAG_CONNECTED;
84 vnet_hw_interface_set_flags (vnm, mif->hw_if_index,
85 VNET_HW_INTERFACE_FLAG_LINK_UP);
89 memif_disconnect_do (vlib_main_t * vm, memif_if_t * mif)
91 vnet_main_t *vnm = vnet_get_main ();
93 mif->flags &= ~(MEMIF_IF_FLAG_CONNECTED | MEMIF_IF_FLAG_CONNECTING);
94 if (mif->hw_if_index != ~0)
95 vnet_hw_interface_set_flags (vnm, mif->hw_if_index, 0);
97 if (mif->connection.index != ~0)
99 unix_file_del (&unix_main, unix_main.file_pool + mif->connection.index);
100 mif->connection.index = ~0;
101 mif->connection.fd = -1; /* closed in unix_file_del */
104 // TODO: properly munmap + close memif-owned shared memory segments
105 vec_free (mif->regions);
109 memif_disconnect (vlib_main_t * vm, memif_if_t * mif)
111 if (mif->interrupt_line.index != ~0)
113 unix_file_del (&unix_main,
114 unix_main.file_pool + mif->interrupt_line.index);
115 mif->interrupt_line.index = ~0;
116 mif->interrupt_line.fd = -1; /* closed in unix_file_del */
119 memif_disconnect_do (vm, mif);
122 static clib_error_t *
123 memif_process_connect_req (memif_pending_conn_t * pending_conn,
124 memif_msg_t * req, struct ucred *slave_cr,
125 int shm_fd, int int_fd)
127 memif_main_t *mm = &memif_main;
128 vlib_main_t *vm = vlib_get_main ();
129 int fd = pending_conn->connection.fd;
132 memif_msg_t resp = { 0 };
133 unix_file_t template = { 0 };
137 static clib_error_t *error = 0;
142 ("Connection request is missing shared memory file descriptor");
150 ("Connection request is missing interrupt line file descriptor");
155 if (slave_cr == NULL)
157 DEBUG_LOG ("Connection request is missing slave credentials");
162 p = mhash_get (&mm->if_index_by_key, &req->key);
166 ("Connection request with unmatched key (0x%" PRIx64 ")", req->key);
171 mif = vec_elt_at_index (mm->interfaces, *p);
172 if (mif->listener_index != pending_conn->listener_index)
175 ("Connection request with non-matching listener (%d vs. %d)",
176 pending_conn->listener_index, mif->listener_index);
181 if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
183 DEBUG_LOG ("Memif slave does not accept connection requests");
188 if (mif->connection.fd != -1)
191 ("Memif with key 0x%" PRIx64 " is already connected", mif->key);
196 if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
198 /* just silently decline the request */
203 if (req->shared_mem_size < sizeof (memif_shm_t))
206 ("Unexpectedly small shared memory segment received from slave.");
212 mmap (NULL, req->shared_mem_size, PROT_READ | PROT_WRITE, MAP_SHARED,
213 shm_fd, 0)) == MAP_FAILED)
216 ("Failed to map shared memory segment received from slave memif");
217 error = clib_error_return_unix (0, "mmap fd %d", shm_fd);
222 if (((memif_shm_t *) shm)->cookie != 0xdeadbeef)
225 ("Possibly corrupted shared memory segment received from slave memif");
226 munmap (shm, req->shared_mem_size);
231 mif->log2_ring_size = req->log2_ring_size;
232 mif->num_s2m_rings = req->num_s2m_rings;
233 mif->num_m2s_rings = req->num_m2s_rings;
234 mif->buffer_size = req->buffer_size;
235 mif->remote_pid = slave_cr->pid;
236 mif->remote_uid = slave_cr->uid;
237 vec_add1 (mif->regions, shm);
239 /* register interrupt line */
240 mif->interrupt_line.fd = int_fd;
241 template.read_function = memif_int_fd_read_ready;
242 template.file_descriptor = int_fd;
243 template.private_data = mif->if_index;
244 mif->interrupt_line.index = unix_file_add (&unix_main, &template);
246 /* change context for future messages */
247 uf = vec_elt_at_index (unix_main.file_pool, pending_conn->connection.index);
248 uf->private_data = mif->if_index << 1;
249 mif->connection = pending_conn->connection;
250 pool_put (mm->pending_conns, pending_conn);
253 memif_connect (vm, mif);
256 resp.version = MEMIF_VERSION;
257 resp.type = MEMIF_MSG_TYPE_CONNECT_RESP;
258 resp.retval = retval;
259 if (send (fd, &resp, sizeof (resp), 0) < 0)
261 DEBUG_UNIX_LOG ("Failed to send connection response");
262 error = clib_error_return_unix (0, "send fd %d", fd);
264 memif_remove_pending_conn (pending_conn);
266 memif_disconnect (vm, mif);
278 static clib_error_t *
279 memif_process_connect_resp (memif_if_t * mif, memif_msg_t * resp)
281 vlib_main_t *vm = vlib_get_main ();
283 if ((mif->flags & MEMIF_IF_FLAG_IS_SLAVE) == 0)
285 DEBUG_LOG ("Memif master does not accept connection responses");
289 if ((mif->flags & MEMIF_IF_FLAG_CONNECTING) == 0)
291 DEBUG_LOG ("Unexpected connection response");
295 if (resp->retval == 0)
296 memif_connect (vm, mif);
298 memif_disconnect (vm, mif);
303 static clib_error_t *
304 memif_conn_fd_read_ready (unix_file_t * uf)
306 memif_main_t *mm = &memif_main;
307 vlib_main_t *vm = vlib_get_main ();
309 memif_pending_conn_t *pending_conn = 0;
310 int fd_array[2] = { -1, -1 };
311 char ctl[CMSG_SPACE (sizeof (fd_array)) +
312 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
313 struct msghdr mh = { 0 };
315 struct ucred *cr = 0;
316 memif_msg_t msg = { 0 };
317 struct cmsghdr *cmsg;
319 static clib_error_t *error = 0;
321 iov[0].iov_base = (void *) &msg;
322 iov[0].iov_len = sizeof (memif_msg_t);
325 mh.msg_control = ctl;
326 mh.msg_controllen = sizeof (ctl);
328 /* grab the appropriate context */
329 if (uf->private_data & 1)
330 pending_conn = vec_elt_at_index (mm->pending_conns,
331 uf->private_data >> 1);
333 mif = vec_elt_at_index (mm->interfaces, uf->private_data >> 1);
335 /* receive the incoming message */
336 size = recvmsg (uf->file_descriptor, &mh, 0);
337 if (size != sizeof (memif_msg_t))
342 memif_remove_pending_conn (pending_conn);
344 memif_disconnect_do (vm, mif);
348 DEBUG_UNIX_LOG ("Malformed message received on fd %d",
349 uf->file_descriptor);
350 error = clib_error_return_unix (0, "recvmsg fd %d",
351 uf->file_descriptor);
355 /* check version of the sender's memif plugin */
356 if (msg.version != MEMIF_VERSION)
358 DEBUG_LOG ("Memif version mismatch");
362 /* process the message based on its type */
365 case MEMIF_MSG_TYPE_CONNECT_REQ:
366 if (pending_conn == 0)
368 DEBUG_LOG ("Received unexpected connection request");
372 /* Read anciliary data */
373 cmsg = CMSG_FIRSTHDR (&mh);
376 if (cmsg->cmsg_level == SOL_SOCKET
377 && cmsg->cmsg_type == SCM_CREDENTIALS)
379 cr = (struct ucred *) CMSG_DATA (cmsg);
381 else if (cmsg->cmsg_level == SOL_SOCKET
382 && cmsg->cmsg_type == SCM_RIGHTS)
384 memcpy (fd_array, CMSG_DATA (cmsg), sizeof (fd_array));
386 cmsg = CMSG_NXTHDR (&mh, cmsg);
389 return memif_process_connect_req (pending_conn, &msg, cr,
390 fd_array[0], fd_array[1]);
392 case MEMIF_MSG_TYPE_CONNECT_RESP:
395 DEBUG_LOG ("Received unexpected connection response");
398 return memif_process_connect_resp (mif, &msg);
400 case MEMIF_MSG_TYPE_DISCONNECT:
404 DEBUG_LOG ("Received unknown message type");
412 memif_remove_pending_conn (pending_conn);
414 memif_disconnect (vm, mif);
418 static clib_error_t *
419 memif_int_fd_read_ready (unix_file_t * uf)
421 memif_main_t *mm = &memif_main;
422 vnet_main_t *vnm = vnet_get_main ();
423 memif_if_t *mif = vec_elt_at_index (mm->interfaces, uf->private_data);
427 size = read (uf->file_descriptor, &b, sizeof (b));
430 /* interrupt line was disconnected */
431 unix_file_del (&unix_main,
432 unix_main.file_pool + mif->interrupt_line.index);
433 mif->interrupt_line.index = ~0;
434 mif->interrupt_line.fd = -1;
437 vnet_device_input_set_interrupt_pending (vnm, mif->hw_if_index, 0);
441 static clib_error_t *
442 memif_conn_fd_accept_ready (unix_file_t * uf)
444 memif_main_t *mm = &memif_main;
445 memif_listener_t *listener = 0;
446 memif_pending_conn_t *pending_conn = 0;
448 struct sockaddr_un client;
450 unix_file_t template = { 0 };
452 listener = pool_elt_at_index (mm->listeners, uf->private_data);
454 addr_len = sizeof (client);
455 conn_fd = accept (uf->file_descriptor,
456 (struct sockaddr *) &client, (socklen_t *) & addr_len);
459 return clib_error_return_unix (0, "accept fd %d", uf->file_descriptor);
461 pool_get (mm->pending_conns, pending_conn);
462 pending_conn->index = pending_conn - mm->pending_conns;
463 pending_conn->listener_index = listener->index;
464 pending_conn->connection.fd = conn_fd;
466 template.read_function = memif_conn_fd_read_ready;
467 template.file_descriptor = conn_fd;
468 template.private_data = (pending_conn->index << 1) | 1;
469 pending_conn->connection.index = unix_file_add (&unix_main, &template);
475 memif_connect_master (vlib_main_t * vm, memif_if_t * mif)
478 struct msghdr mh = { 0 };
480 struct cmsghdr *cmsg;
483 int fd_array[2] = { -1, -1 };
484 char ctl[CMSG_SPACE (sizeof (fd_array))];
485 memif_ring_t *ring = NULL;
489 unix_file_t template = { 0 };
491 msg.version = MEMIF_VERSION;
492 msg.type = MEMIF_MSG_TYPE_CONNECT_REQ;
494 msg.log2_ring_size = mif->log2_ring_size;
495 msg.num_s2m_rings = mif->num_s2m_rings;
496 msg.num_m2s_rings = mif->num_m2s_rings;
497 msg.buffer_size = mif->buffer_size;
499 buffer_offset = sizeof (memif_shm_t) +
500 (mif->num_s2m_rings + mif->num_m2s_rings) *
501 (sizeof (memif_ring_t) +
502 sizeof (memif_desc_t) * (1 << mif->log2_ring_size));
504 msg.shared_mem_size = buffer_offset +
505 mif->buffer_size * (1 << mif->log2_ring_size) * (mif->num_s2m_rings +
508 if ((mfd = memfd_create ("shared mem", MFD_ALLOW_SEALING)) == -1)
510 DEBUG_LOG ("Failed to create anonymous file");
514 if ((fcntl (mfd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
516 DEBUG_UNIX_LOG ("Failed to seal an anonymous file off from truncating");
520 if ((ftruncate (mfd, msg.shared_mem_size)) == -1)
522 DEBUG_UNIX_LOG ("Failed to extend the size of an anonymous file");
526 if ((shm = mmap (NULL, msg.shared_mem_size, PROT_READ | PROT_WRITE,
527 MAP_SHARED, mfd, 0)) == MAP_FAILED)
529 DEBUG_UNIX_LOG ("Failed to map anonymous file into memory");
533 vec_add1 (mif->regions, shm);
534 ((memif_shm_t *) mif->regions[0])->cookie = 0xdeadbeef;
536 for (i = 0; i < mif->num_s2m_rings; i++)
538 ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
539 ring->head = ring->tail = 0;
540 for (j = 0; j < (1 << mif->log2_ring_size); j++)
542 u16 slot = i * (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;
549 for (i = 0; i < mif->num_m2s_rings; i++)
551 ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
552 ring->head = ring->tail = 0;
553 for (j = 0; j < (1 << mif->log2_ring_size); j++)
556 (i + mif->num_s2m_rings) * (1 << mif->log2_ring_size) + j;
557 ring->desc[j].region = 0;
558 ring->desc[j].offset =
559 buffer_offset + (u32) (slot * mif->buffer_size);
560 ring->desc[j].buffer_length = mif->buffer_size;
564 iov[0].iov_base = (void *) &msg;
565 iov[0].iov_len = sizeof (memif_msg_t);
569 /* create interrupt socket */
570 if (socketpair (AF_UNIX, SOCK_STREAM, 0, fd_array) < 0)
572 DEBUG_UNIX_LOG ("Failed to create a pair of connected sockets");
576 mif->interrupt_line.fd = fd_array[0];
577 template.read_function = memif_int_fd_read_ready;
578 template.file_descriptor = mif->interrupt_line.fd;
579 template.private_data = mif->if_index;
580 mif->interrupt_line.index = unix_file_add (&unix_main, &template);
582 memset (&ctl, 0, sizeof (ctl));
583 mh.msg_control = ctl;
584 mh.msg_controllen = sizeof (ctl);
585 cmsg = CMSG_FIRSTHDR (&mh);
586 cmsg->cmsg_len = CMSG_LEN (sizeof (fd_array));
587 cmsg->cmsg_level = SOL_SOCKET;
588 cmsg->cmsg_type = SCM_RIGHTS;
590 memcpy (CMSG_DATA (cmsg), fd_array, sizeof (fd_array));
592 mif->flags |= MEMIF_IF_FLAG_CONNECTING;
593 rv = sendmsg (mif->connection.fd, &mh, 0);
596 DEBUG_UNIX_LOG ("Failed to send memif connection request");
600 /* No need to keep the descriptor open,
601 * mmap creates an extra reference to the underlying file */
604 /* This FD is given to peer, so we can close it */
612 if (fd_array[1] > -1)
614 memif_disconnect (vm, mif);
618 memif_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
620 memif_main_t *mm = &memif_main;
622 struct sockaddr_un sun;
624 uword *event_data = 0, event_type;
625 unix_file_t template = { 0 };
627 f64 start_time, last_run_duration = 0, now;
629 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
632 DEBUG_UNIX_LOG ("socket AF_UNIX");
635 sun.sun_family = AF_UNIX;
636 template.read_function = memif_conn_fd_read_ready;
641 vlib_process_wait_for_event_or_clock (vm,
642 (f64) 3 - last_run_duration);
644 vlib_process_wait_for_event (vm);
646 event_type = vlib_process_get_events (vm, &event_data);
647 vec_reset_length (event_data);
653 case MEMIF_PROCESS_EVENT_START:
656 case MEMIF_PROCESS_EVENT_STOP:
663 last_run_duration = start_time = vlib_time_now (vm);
665 pool_foreach (mif, mm->interfaces,
667 /* Allow no more than 10us without a pause */
668 now = vlib_time_now (vm);
669 if (now > start_time + 10e-6)
671 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
672 start_time = vlib_time_now (vm);
675 if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
678 if (mif->flags & MEMIF_IF_FLAG_CONNECTING)
681 if (mif->flags & MEMIF_IF_FLAG_CONNECTED)
684 if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
686 strncpy (sun.sun_path, (char *) mif->socket_filename,
687 sizeof (sun.sun_path) - 1);
690 (sockfd, (struct sockaddr *) &sun,
691 sizeof (struct sockaddr_un)) == 0)
693 mif->connection.fd = sockfd;
694 template.file_descriptor = sockfd;
695 template.private_data = mif->if_index << 1;
696 mif->connection.index = unix_file_add (&unix_main, &template);
697 memif_connect_master (vm, mif);
699 /* grab another fd */
700 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
703 DEBUG_UNIX_LOG ("socket AF_UNIX");
710 last_run_duration = vlib_time_now (vm) - last_run_duration;
716 VLIB_REGISTER_NODE (memif_process_node,static) = {
717 .function = memif_process,
718 .type = VLIB_NODE_TYPE_PROCESS,
719 .name = "memif-process",
724 memif_close_if (memif_main_t * mm, memif_if_t * mif)
726 vlib_main_t *vm = vlib_get_main ();
727 memif_listener_t *listener = 0;
728 memif_pending_conn_t *pending_conn = 0;
730 memif_disconnect (vm, mif);
732 if (mif->listener_index != (uword) ~ 0)
734 listener = pool_elt_at_index (mm->listeners, mif->listener_index);
735 if (--listener->usage_counter == 0)
737 /* not used anymore -> remove the socket and pending connections */
740 pool_foreach (pending_conn, mm->pending_conns,
742 if (pending_conn->listener_index == mif->listener_index)
744 memif_remove_pending_conn (pending_conn);
749 unix_file_del (&unix_main,
750 unix_main.file_pool + listener->socket.index);
751 pool_put (mm->listeners, listener);
752 unlink ((char *) mif->socket_filename);
756 clib_spinlock_free (&mif->lockp);
758 mhash_unset (&mm->if_index_by_key, &mif->key, &mif->if_index);
759 vec_free (mif->socket_filename);
760 vec_free (mif->ring_data);
762 memset (mif, 0, sizeof (*mif));
763 pool_put (mm->interfaces, mif);
767 memif_worker_thread_enable ()
769 /* if worker threads are enabled, switch to polling mode */
773 vlib_node_set_state (this_vlib_main,
774 memif_input_node.index,
775 VLIB_NODE_STATE_POLLING);
782 memif_worker_thread_disable ()
787 vlib_node_set_state (this_vlib_main,
788 memif_input_node.index,
789 VLIB_NODE_STATE_INTERRUPT);
796 memif_create_if (vlib_main_t * vm, memif_create_if_args_t * args)
798 memif_main_t *mm = &memif_main;
799 vlib_thread_main_t *tm = vlib_get_thread_main ();
800 vnet_main_t *vnm = vnet_get_main ();
802 vnet_sw_interface_t *sw;
803 clib_error_t *error = 0;
806 vnet_hw_interface_t *hw;
808 p = mhash_get (&mm->if_index_by_key, &args->key);
810 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
812 pool_get (mm->interfaces, mif);
813 memset (mif, 0, sizeof (*mif));
814 mif->key = args->key;
815 mif->if_index = mif - mm->interfaces;
816 mif->sw_if_index = mif->hw_if_index = mif->per_interface_next_index = ~0;
817 mif->listener_index = ~0;
818 mif->connection.index = mif->interrupt_line.index = ~0;
819 mif->connection.fd = mif->interrupt_line.fd = -1;
821 if (tm->n_vlib_mains > 1)
822 clib_spinlock_init (&mif->lockp);
824 if (!args->hw_addr_set)
826 f64 now = vlib_time_now (vm);
828 rnd = (u32) (now * 1e6);
829 rnd = random_u32 (&rnd);
831 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
832 args->hw_addr[0] = 2;
833 args->hw_addr[1] = 0xfe;
836 error = ethernet_register_interface (vnm, memif_device_class.index,
837 mif->if_index, args->hw_addr,
839 memif_eth_flag_change);
843 clib_error_report (error);
844 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
848 sw = vnet_get_hw_sw_interface (vnm, mif->hw_if_index);
849 mif->sw_if_index = sw->sw_if_index;
851 mif->log2_ring_size = args->log2_ring_size;
852 mif->buffer_size = args->buffer_size;
854 /* TODO: make configurable */
855 mif->num_s2m_rings = 1;
856 mif->num_m2s_rings = 1;
858 mhash_set_mem (&mm->if_index_by_key, &args->key, &mif->if_index, 0);
860 if (args->socket_filename != 0)
861 mif->socket_filename = args->socket_filename;
863 mif->socket_filename = vec_dup (mm->default_socket_filename);
865 args->sw_if_index = mif->sw_if_index;
869 struct sockaddr_un un = { 0 };
870 struct stat file_stat;
872 memif_listener_t *listener = 0;
874 if (stat ((char *) mif->socket_filename, &file_stat) == 0)
876 if (!S_ISSOCK (file_stat.st_mode))
879 ret = VNET_API_ERROR_SYSCALL_ERROR_2;
883 pool_foreach (listener, mm->listeners,
885 if (listener->sock_dev == file_stat.st_dev &&
886 listener->sock_ino == file_stat.st_ino)
888 /* attach memif to the existing listener */
889 mif->listener_index = listener->index;
890 ++listener->usage_counter;
895 unlink ((char *) mif->socket_filename);
898 pool_get (mm->listeners, listener);
899 memset (listener, 0, sizeof (*listener));
900 listener->socket.fd = -1;
901 listener->socket.index = ~0;
902 listener->index = listener - mm->listeners;
903 listener->usage_counter = 1;
905 if ((listener->socket.fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
907 ret = VNET_API_ERROR_SYSCALL_ERROR_3;
911 un.sun_family = AF_UNIX;
912 strncpy ((char *) un.sun_path, (char *) mif->socket_filename,
913 sizeof (un.sun_path) - 1);
915 if (setsockopt (listener->socket.fd, SOL_SOCKET, SO_PASSCRED,
916 &on, sizeof (on)) < 0)
918 ret = VNET_API_ERROR_SYSCALL_ERROR_4;
921 if (bind (listener->socket.fd, (struct sockaddr *) &un,
924 ret = VNET_API_ERROR_SYSCALL_ERROR_5;
927 if (listen (listener->socket.fd, 1) == -1)
929 ret = VNET_API_ERROR_SYSCALL_ERROR_6;
933 if (stat ((char *) mif->socket_filename, &file_stat) == -1)
935 ret = VNET_API_ERROR_SYSCALL_ERROR_7;
939 listener->sock_dev = file_stat.st_dev;
940 listener->sock_ino = file_stat.st_ino;
942 unix_file_t template = { 0 };
943 template.read_function = memif_conn_fd_accept_ready;
944 template.file_descriptor = listener->socket.fd;
945 template.private_data = listener->index;
946 listener->socket.index = unix_file_add (&unix_main, &template);
948 mif->listener_index = listener->index;
952 mif->flags |= MEMIF_IF_FLAG_IS_SLAVE;
955 hw = vnet_get_hw_interface (vnm, mif->hw_if_index);
956 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
957 vnet_hw_interface_set_input_node (vnm, mif->hw_if_index,
958 memif_input_node.index);
959 vnet_hw_interface_assign_rx_thread (vnm, mif->hw_if_index, 0, ~0);
960 ret = vnet_hw_interface_set_rx_mode (vnm, mif->hw_if_index, 0,
961 VNET_HW_INTERFACE_RX_MODE_INTERRUPT);
963 clib_warning ("Warning: unable to set rx mode for interface %d: "
964 "rc=%d", mif->hw_if_index, ret);
967 /* use configured or generate random MAC address */
968 if (!args->hw_addr_set &&
969 tm->n_vlib_mains > 1 && pool_elts (mm->interfaces) == 1)
970 memif_worker_thread_enable ();
974 if (pool_elts (mm->interfaces) == 1)
976 vlib_process_signal_event (vm, memif_process_node.index,
977 MEMIF_PROCESS_EVENT_START, 0);
982 if (mif->hw_if_index != ~0)
984 ethernet_delete_interface (vnm, mif->hw_if_index);
985 mif->hw_if_index = ~0;
987 memif_close_if (mm, mif);
992 memif_delete_if (vlib_main_t * vm, u64 key)
994 vnet_main_t *vnm = vnet_get_main ();
995 memif_main_t *mm = &memif_main;
1000 p = mhash_get (&mm->if_index_by_key, &key);
1003 clib_warning ("Memory interface with key 0x%" PRIx64 " does not exist",
1005 return VNET_API_ERROR_SYSCALL_ERROR_1;
1007 mif = pool_elt_at_index (mm->interfaces, p[0]);
1008 mif->flags |= MEMIF_IF_FLAG_DELETING;
1010 ret = vnet_hw_interface_unassign_rx_thread (vnm, mif->hw_if_index, 0);
1012 clib_warning ("Warning: unable to unassign interface %d: rc=%d",
1013 mif->hw_if_index, ret);
1015 /* bring down the interface */
1016 vnet_hw_interface_set_flags (vnm, mif->hw_if_index, 0);
1017 vnet_sw_interface_set_flags (vnm, mif->sw_if_index, 0);
1019 /* remove the interface */
1020 ethernet_delete_interface (vnm, mif->hw_if_index);
1021 mif->hw_if_index = ~0;
1022 memif_close_if (mm, mif);
1024 if (pool_elts (mm->interfaces) == 0)
1026 vlib_process_signal_event (vm, memif_process_node.index,
1027 MEMIF_PROCESS_EVENT_STOP, 0);
1031 if (tm->n_vlib_mains > 1 && pool_elts (mm->interfaces) == 0)
1032 memif_worker_thread_disable ();
1038 static clib_error_t *
1039 memif_init (vlib_main_t * vm)
1041 memif_main_t *mm = &memif_main;
1042 vlib_thread_main_t *tm = vlib_get_thread_main ();
1043 vlib_thread_registration_t *tr;
1046 memset (mm, 0, sizeof (memif_main_t));
1048 mm->input_cpu_first_index = 0;
1049 mm->input_cpu_count = 1;
1051 /* initialize binary API */
1052 memif_plugin_api_hookup (vm);
1054 /* find out which cpus will be used for input */
1055 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1056 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
1058 if (tr && tr->count > 0)
1060 mm->input_cpu_first_index = tr->first_index;
1061 mm->input_cpu_count = tr->count;
1064 mhash_init (&mm->if_index_by_key, sizeof (uword), sizeof (u64));
1066 vec_validate_aligned (mm->rx_buffers, tm->n_vlib_mains - 1,
1067 CLIB_CACHE_LINE_BYTES);
1069 /* set default socket filename */
1070 vec_validate (mm->default_socket_filename,
1071 strlen (MEMIF_DEFAULT_SOCKET_FILENAME));
1072 strncpy ((char *) mm->default_socket_filename,
1073 MEMIF_DEFAULT_SOCKET_FILENAME,
1074 vec_len (mm->default_socket_filename) - 1);
1079 VLIB_INIT_FUNCTION (memif_init);
1082 VLIB_PLUGIN_REGISTER () = {
1083 .version = VPP_BUILD_VER,
1084 .description = "Packet Memory Interface (experimetal)",
1089 * fd.io coding-style-patch-verification: ON
1092 * eval: (c-set-style "gnu")