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:
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 *------------------------------------------------------------------
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
29 #include <sys/prctl.h>
30 #include <sys/eventfd.h>
34 #include <vlib/vlib.h>
35 #include <vlib/unix/unix.h>
36 #include <vnet/plugin/plugin.h>
37 #include <vnet/ethernet/ethernet.h>
38 #include <vnet/interface/rx_queue_funcs.h>
39 #include <vnet/interface/tx_queue_funcs.h>
40 #include <vpp/app/version.h>
41 #include <memif/memif.h>
42 #include <memif/private.h>
44 memif_main_t memif_main;
47 memif_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
54 memif_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
62 memif_queue_intfd_close (memif_queue_t * mq)
64 if (mq->int_clib_file_index != ~0)
66 memif_file_del_by_index (mq->int_clib_file_index);
67 mq->int_clib_file_index = ~0;
70 else if (mq->int_fd > -1)
78 memif_disconnect_free_zc_queue_buffer (memif_queue_t * mq, u8 is_rx)
80 vlib_main_t *vm = vlib_get_main ();
81 u16 ring_size, n_slots, mask, start;
83 ring_size = 1 << mq->log2_ring_size;
85 n_slots = mq->ring->head - mq->last_tail;
86 start = mq->last_tail & mask;
88 vlib_buffer_free_from_ring (vm, mq->buffers, start, ring_size, n_slots);
90 vlib_buffer_free_from_ring_no_next (vm, mq->buffers, start, ring_size,
92 vec_free (mq->buffers);
96 memif_disconnect (memif_if_t * mif, clib_error_t * err)
98 memif_main_t *mm = &memif_main;
99 vnet_main_t *vnm = vnet_get_main ();
107 memif_log_debug (mif, "disconnect %u (%v)", mif->dev_instance,
108 err ? err->what : 0);
113 mif->local_disc_string = vec_dup (err->what);
114 if (mif->sock && clib_socket_is_connected (mif->sock))
115 e = memif_msg_send_disconnect (mif, err);
119 /* set interface down */
120 mif->flags &= ~(MEMIF_IF_FLAG_CONNECTED | MEMIF_IF_FLAG_CONNECTING);
121 if (mif->hw_if_index != ~0)
122 vnet_hw_interface_set_flags (vnm, mif->hw_if_index, 0);
124 /* close connection socket */
125 if (mif->sock && mif->sock->fd)
127 memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
128 mif->socket_file_index);
129 hash_unset (msf->dev_instance_by_fd, mif->sock->fd);
130 memif_socket_close (&mif->sock);
135 err = clib_socket_close (mif->sock);
138 memif_log_err (mif, "%U", format_clib_error, err);
139 clib_error_free (err);
141 clib_mem_free (mif->sock);
145 vec_foreach_index (i, mif->rx_queues)
147 mq = vec_elt_at_index (mif->rx_queues, i);
150 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
152 memif_disconnect_free_zc_queue_buffer(mq, 1);
157 vnet_hw_if_unregister_all_rx_queues (vnm, mif->hw_if_index);
160 vec_foreach_index (i, mif->tx_queues)
162 mq = vec_elt_at_index (mif->tx_queues, i);
165 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
167 memif_disconnect_free_zc_queue_buffer(mq, 0);
169 clib_spinlock_free (&mq->lockp);
173 vnet_hw_if_unregister_all_tx_queues (vnm, mif->hw_if_index);
174 vnet_hw_if_update_runtime_data (vnm, mif->hw_if_index);
176 /* free tx and rx queues */
177 vec_foreach (mq, mif->rx_queues)
178 memif_queue_intfd_close (mq);
179 vec_free (mif->rx_queues);
181 vec_foreach (mq, mif->tx_queues)
182 memif_queue_intfd_close (mq);
183 vec_free (mif->tx_queues);
185 /* free memory regions */
186 vec_foreach (mr, mif->regions)
191 if ((rv = munmap (mr->shm, mr->region_size)))
192 memif_log_err (mif, "munmap failed, rv = %d", rv);
197 vec_free (mif->regions);
198 vec_free (mif->remote_name);
199 vec_free (mif->remote_if_name);
200 clib_fifo_free (mif->msg_queue);
203 static clib_error_t *
204 memif_int_fd_write_ready (clib_file_t * uf)
206 memif_main_t *mm = &memif_main;
207 u16 qid = uf->private_data & 0xFFFF;
208 memif_if_t *mif = vec_elt_at_index (mm->interfaces, uf->private_data >> 16);
210 memif_log_warn (mif, "unexpected EPOLLOUT on RX for queue %u", qid);
214 static clib_error_t *
215 memif_int_fd_read_ready (clib_file_t * uf)
217 memif_main_t *mm = &memif_main;
218 vnet_main_t *vnm = vnet_get_main ();
219 u16 qid = uf->private_data & 0xFFFF;
220 memif_if_t *mif = vec_elt_at_index (mm->interfaces, uf->private_data >> 16);
221 memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid);
225 size = read (uf->file_descriptor, &b, sizeof (b));
228 memif_log_debug (mif, "Failed to read from socket");
232 vnet_hw_if_rx_queue_set_int_pending (vnm, mq->queue_index);
240 memif_connect (memif_if_t * mif)
242 memif_main_t *mm = &memif_main;
243 vlib_main_t *vm = vlib_get_main ();
244 vnet_main_t *vnm = vnet_get_main ();
245 clib_file_t template = { 0 };
248 u32 n_txqs = 0, n_threads = vlib_get_n_threads ();
249 clib_error_t *err = NULL;
250 u8 max_log2_ring_sz = 0;
251 int with_barrier = 0;
253 memif_log_debug (mif, "connect %u", mif->dev_instance);
255 vec_free (mif->local_disc_string);
256 vec_free (mif->remote_disc_string);
259 vec_foreach (mr, mif->regions)
266 err = clib_error_return (0, "no memory region fd");
270 if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
271 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
273 err = clib_error_return_unix (0, "mmap");
279 template.read_function = memif_int_fd_read_ready;
280 template.write_function = memif_int_fd_write_ready;
283 if (vlib_worker_thread_barrier_held ())
287 vlib_worker_thread_barrier_sync (vm);
290 vec_foreach_index (i, mif->tx_queues)
292 memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i);
293 max_log2_ring_sz = clib_max (max_log2_ring_sz, mq->log2_ring_size);
295 mq->ring = mif->regions[mq->region].shm + mq->offset;
296 if (mq->ring->cookie != MEMIF_COOKIE)
298 err = clib_error_return (0, "wrong cookie on tx ring %u", i);
302 vnet_hw_if_register_tx_queue (vnm, mif->hw_if_index, i);
303 clib_spinlock_init (&mq->lockp);
306 if (vec_len (mif->tx_queues) > 0)
308 n_txqs = vec_len (mif->tx_queues);
309 for (j = 0; j < n_threads; j++)
311 u32 qi = mif->tx_queues[j % n_txqs].queue_index;
312 vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
316 vec_foreach_index (i, mif->rx_queues)
318 memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i);
323 max_log2_ring_sz = clib_max (max_log2_ring_sz, mq->log2_ring_size);
325 mq->ring = mif->regions[mq->region].shm + mq->offset;
326 if (mq->ring->cookie != MEMIF_COOKIE)
328 err = clib_error_return (0, "wrong cookie on tx ring %u", i);
331 qi = vnet_hw_if_register_rx_queue (vnm, mif->hw_if_index, i,
332 VNET_HW_IF_RXQ_THREAD_ANY);
333 mq->queue_index = qi;
336 template.file_descriptor = mq->int_fd;
337 template.private_data = (mif->dev_instance << 16) | (i & 0xFFFF);
338 template.description = format (0, "%U rx %u int",
339 format_memif_device_name,
340 mif->dev_instance, i);
341 memif_file_add (&mq->int_clib_file_index, &template);
342 vnet_hw_if_set_rx_queue_file_index (vnm, qi,
343 mq->int_clib_file_index);
345 ti = vnet_hw_if_get_rx_queue_thread_index (vnm, qi);
346 mq->buffer_pool_index = vlib_buffer_pool_get_default_for_numa (
347 vm, vlib_get_main_by_index (ti)->numa_node);
348 rv = vnet_hw_if_set_rx_queue_mode (vnm, qi, VNET_HW_IF_RX_MODE_DEFAULT);
349 vnet_hw_if_update_runtime_data (vnm, mif->hw_if_index);
353 (mif, "Warning: unable to set rx mode for interface %d queue %d: "
354 "rc=%d", mif->hw_if_index, i, rv);
357 vnet_hw_if_rx_mode rxmode = vnet_hw_if_get_rx_queue_mode (vnm, qi);
359 if (rxmode == VNET_HW_IF_RX_MODE_POLLING)
360 mq->ring->flags |= MEMIF_RING_FLAG_MASK_INT;
362 vnet_hw_if_rx_queue_set_int_pending (vnm, qi);
367 if (1 << max_log2_ring_sz > vec_len (mm->per_thread_data[0].desc_data))
369 memif_per_thread_data_t *ptd;
371 vec_foreach (ptd, mm->per_thread_data)
373 vec_validate_aligned (ptd->desc_data, pow2_mask (max_log2_ring_sz),
374 CLIB_CACHE_LINE_BYTES);
375 vec_validate_aligned (ptd->desc_len, pow2_mask (max_log2_ring_sz),
376 CLIB_CACHE_LINE_BYTES);
377 vec_validate_aligned (ptd->desc_status, pow2_mask (max_log2_ring_sz),
378 CLIB_CACHE_LINE_BYTES);
382 vlib_worker_thread_barrier_release (vm);
384 mif->flags &= ~MEMIF_IF_FLAG_CONNECTING;
385 mif->flags |= MEMIF_IF_FLAG_CONNECTED;
387 vnet_hw_interface_set_flags (vnm, mif->hw_if_index,
388 VNET_HW_INTERFACE_FLAG_LINK_UP);
393 vlib_worker_thread_barrier_release (vm);
394 memif_log_err (mif, "%U", format_clib_error, err);
398 static_always_inline memif_ring_t *
399 memif_get_ring (memif_if_t * mif, memif_ring_type_t type, u16 ring_num)
401 if (vec_len (mif->regions) == 0)
403 void *p = mif->regions[0].shm;
405 sizeof (memif_ring_t) +
406 sizeof (memif_desc_t) * (1 << mif->run.log2_ring_size);
407 p += (ring_num + type * mif->run.num_s2m_rings) * ring_size;
409 return (memif_ring_t *) p;
413 memif_init_regions_and_queues (memif_if_t * mif)
415 vlib_main_t *vm = vlib_get_main ();
416 memif_socket_file_t *msf;
417 memif_ring_t *ring = NULL;
423 ASSERT (vec_len (mif->regions) == 0);
424 vec_add2_aligned (mif->regions, r, 1, CLIB_CACHE_LINE_BYTES);
426 buffer_offset = (mif->run.num_s2m_rings + mif->run.num_m2s_rings) *
427 (sizeof (memif_ring_t) +
428 sizeof (memif_desc_t) * (1 << mif->run.log2_ring_size));
430 r->region_size = buffer_offset;
432 if ((mif->flags & MEMIF_IF_FLAG_ZERO_COPY) == 0)
433 r->region_size += mif->run.buffer_size * (1 << mif->run.log2_ring_size) *
434 (mif->run.num_s2m_rings + mif->run.num_m2s_rings);
436 if ((fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, "%U region 0",
437 format_memif_device_name,
438 mif->dev_instance)) == -1)
440 err = clib_mem_get_last_error ();
444 if ((ftruncate (fd, r->region_size)) == -1)
446 err = clib_error_return_unix (0, "ftruncate");
450 msf = pool_elt_at_index (memif_main.socket_files, mif->socket_file_index);
451 r->shm = clib_mem_vm_map_shared (0, r->region_size, fd, 0, "memif%lu/%lu:0",
452 msf->socket_id, mif->id);
454 if (r->shm == CLIB_MEM_VM_MAP_FAILED)
456 err = clib_error_return_unix (0, "memif shared region map failed");
462 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
464 vlib_buffer_pool_t *bp;
466 vec_foreach (bp, vm->buffer_main->buffer_pools)
468 vlib_physmem_map_t *pm;
469 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
470 vec_add2_aligned (mif->regions, r, 1, CLIB_CACHE_LINE_BYTES);
472 r->region_size = pm->n_pages << pm->log2_page_size;
479 for (i = 0; i < mif->run.num_s2m_rings; i++)
481 ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
482 ring->head = ring->tail = 0;
483 ring->cookie = MEMIF_COOKIE;
485 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
488 for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
490 u16 slot = i * (1 << mif->run.log2_ring_size) + j;
491 ring->desc[j].region = 0;
492 ring->desc[j].offset =
493 buffer_offset + (u32) (slot * mif->run.buffer_size);
494 ring->desc[j].length = mif->run.buffer_size;
497 for (i = 0; i < mif->run.num_m2s_rings; i++)
499 ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
500 ring->head = ring->tail = 0;
501 ring->cookie = MEMIF_COOKIE;
503 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
506 for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
509 (i + mif->run.num_s2m_rings) * (1 << mif->run.log2_ring_size) + j;
510 ring->desc[j].region = 0;
511 ring->desc[j].offset =
512 buffer_offset + (u32) (slot * mif->run.buffer_size);
513 ring->desc[j].length = mif->run.buffer_size;
517 ASSERT (mif->tx_queues == 0);
518 vec_validate_aligned (mif->tx_queues, mif->run.num_s2m_rings - 1,
519 CLIB_CACHE_LINE_BYTES);
522 vec_foreach_index (i, mif->tx_queues)
524 memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i);
525 if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
527 err = clib_error_return_unix (0, "eventfd[tx queue %u]", i);
531 mq->int_clib_file_index = ~0;
532 mq->ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
533 mq->log2_ring_size = mif->cfg.log2_ring_size;
535 mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
537 mq->type = MEMIF_RING_S2M;
538 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
539 vec_validate_aligned (mq->buffers, 1 << mq->log2_ring_size,
540 CLIB_CACHE_LINE_BYTES);
544 ASSERT (mif->rx_queues == 0);
545 vec_validate_aligned (mif->rx_queues, mif->run.num_m2s_rings - 1,
546 CLIB_CACHE_LINE_BYTES);
549 vec_foreach_index (i, mif->rx_queues)
551 memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i);
552 if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
554 err = clib_error_return_unix (0, "eventfd[rx queue %u]", i);
557 mq->int_clib_file_index = ~0;
558 mq->ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
559 mq->log2_ring_size = mif->cfg.log2_ring_size;
561 mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
563 mq->type = MEMIF_RING_M2S;
564 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
565 vec_validate_aligned (mq->buffers, 1 << mq->log2_ring_size,
566 CLIB_CACHE_LINE_BYTES);
573 memif_log_err (mif, "%U", format_clib_error, err);
578 memif_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
580 memif_main_t *mm = &memif_main;
583 uword *event_data = 0, event_type;
585 f64 start_time, last_run_duration = 0, now;
588 sock = clib_mem_alloc (sizeof (clib_socket_t));
589 clib_memset (sock, 0, sizeof (clib_socket_t));
594 vlib_process_wait_for_event_or_clock (vm, (f64) 3 -
597 vlib_process_wait_for_event (vm);
599 event_type = vlib_process_get_events (vm, &event_data);
600 vec_reset_length (event_data);
606 case MEMIF_PROCESS_EVENT_START:
609 case MEMIF_PROCESS_EVENT_STOP:
612 case MEMIF_PROCESS_EVENT_ADMIN_UP_DOWN:
618 last_run_duration = start_time = vlib_time_now (vm);
620 pool_foreach (mif, mm->interfaces)
622 memif_socket_file_t * msf = vec_elt_at_index (mm->socket_files, mif->socket_file_index);
623 /* Allow no more than 10us without a pause */
624 now = vlib_time_now (vm);
625 if (now > start_time + 10e-6)
627 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
628 start_time = vlib_time_now (vm);
631 if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
634 if (mif->flags & MEMIF_IF_FLAG_CONNECTING)
637 if (mif->flags & MEMIF_IF_FLAG_CONNECTED)
640 if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
642 clib_memset (sock, 0, sizeof(clib_socket_t));
643 sock->config = (char *) msf->filename;
644 sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET |
645 CLIB_SOCKET_F_BLOCKING;
647 if ((err = clib_socket_init (sock)))
649 clib_error_free (err);
653 clib_file_t t = { 0 };
655 t.read_function = memif_slave_conn_fd_read_ready;
656 t.write_function = memif_slave_conn_fd_write_ready;
657 t.error_function = memif_slave_conn_fd_error;
658 t.file_descriptor = sock->fd;
659 t.private_data = mif->dev_instance;
660 memif_file_add (&sock->private_data, &t);
661 t.description = format (0, "%U ctl",
662 format_memif_device_name,
664 hash_set (msf->dev_instance_by_fd, sock->fd, mif->dev_instance);
666 mif->flags |= MEMIF_IF_FLAG_CONNECTING;
668 sock = clib_mem_alloc (sizeof(clib_socket_t));
673 last_run_duration = vlib_time_now (vm) - last_run_duration;
679 VLIB_REGISTER_NODE (memif_process_node,static) = {
680 .function = memif_process,
681 .type = VLIB_NODE_TYPE_PROCESS,
682 .name = "memif-process",
686 static clib_error_t *
687 memif_add_socket_file (u32 sock_id, u8 *socket_filename)
689 memif_main_t *mm = &memif_main;
691 memif_socket_file_t *msf;
693 p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
696 msf = pool_elt_at_index (mm->socket_files, *p);
697 if (strcmp ((char *) msf->filename, (char *) socket_filename) == 0)
699 /* Silently accept identical "add". */
703 /* But don't allow a direct add of a different filename. */
704 return vnet_error (VNET_ERR_ENTRY_ALREADY_EXISTS,
705 "entry already exists");
708 pool_get (mm->socket_files, msf);
709 clib_memset (msf, 0, sizeof (memif_socket_file_t));
711 msf->filename = socket_filename;
712 msf->socket_id = sock_id;
714 hash_set (mm->socket_file_index_by_sock_id, sock_id,
715 msf - mm->socket_files);
720 static clib_error_t *
721 memif_delete_socket_file (u32 sock_id)
723 memif_main_t *mm = &memif_main;
725 memif_socket_file_t *msf;
727 p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
729 /* Don't delete non-existent entries. */
730 return vnet_error (VNET_ERR_INVALID_ARGUMENT,
731 "socket file with id %u does not exist", sock_id);
733 msf = pool_elt_at_index (mm->socket_files, *p);
734 if (msf->ref_cnt > 0)
735 return vnet_error (VNET_ERR_UNEXPECTED_INTF_STATE,
736 "socket file '%s' is in use", msf->filename);
738 vec_free (msf->filename);
739 pool_put (mm->socket_files, msf);
741 hash_unset (mm->socket_file_index_by_sock_id, sock_id);
747 memif_socket_filename_add_del (u8 is_add, u32 sock_id, u8 *sock_filename)
752 /* allow adding socket id 0 */
753 if (sock_id == 0 && is_add == 0)
754 return vnet_error (VNET_ERR_INVALID_ARGUMENT, "cannot delete socket id 0");
757 return vnet_error (VNET_ERR_INVALID_ARGUMENT,
758 "socked id is not specified");
761 return memif_delete_socket_file (sock_id);
763 if (sock_filename == 0 || sock_filename[0] == 0)
764 return vnet_error (VNET_ERR_INVALID_ARGUMENT,
765 "socket filename not specified");
767 if (sock_filename[0] != '/')
771 /* copy runtime dir path */
772 vec_add (dir, vlib_unix_get_runtime_dir (),
773 strlen (vlib_unix_get_runtime_dir ()));
776 /* if sock_filename contains dirs, add them to path */
777 tmp = strrchr ((char *) sock_filename, '/');
780 idx = tmp - (char *) sock_filename;
781 vec_add (dir, sock_filename, idx);
784 vec_add1 (dir, '\0');
785 /* create socket dir */
786 error = vlib_unix_recursive_mkdir (dir);
789 clib_error_free (error);
790 return vnet_error (VNET_ERR_SYSCALL_ERROR_1,
791 "unable to create socket dir");
794 sock_filename = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
799 sock_filename = vec_dup (sock_filename);
801 /* check if directory exists */
802 tmp = strrchr ((char *) sock_filename, '/');
805 idx = tmp - (char *) sock_filename;
806 vec_add (dir, sock_filename, idx);
807 vec_add1 (dir, '\0');
810 /* check dir existance and access rights for effective user/group IDs */
813 (faccessat ( /* ignored */ -1, dir, F_OK | R_OK | W_OK, AT_EACCESS)
818 VNET_ERR_INVALID_ARGUMENT,
819 "directory doesn't exist or no access permissions");
824 return memif_add_socket_file (sock_id, sock_filename);
828 memif_delete_if (vlib_main_t *vm, memif_if_t *mif)
830 vnet_main_t *vnm = vnet_get_main ();
831 memif_main_t *mm = &memif_main;
832 memif_socket_file_t *msf =
833 vec_elt_at_index (mm->socket_files, mif->socket_file_index);
836 mif->flags |= MEMIF_IF_FLAG_DELETING;
837 vec_free (mif->local_disc_string);
838 vec_free (mif->remote_disc_string);
840 /* bring down the interface */
841 vnet_hw_interface_set_flags (vnm, mif->hw_if_index, 0);
842 vnet_sw_interface_set_flags (vnm, mif->sw_if_index, 0);
844 err = clib_error_return (0, "interface deleted");
845 memif_disconnect (mif, err);
846 clib_error_free (err);
848 if (mif->hw_if_index != ~0)
850 /* remove the interface */
851 if (mif->mode == MEMIF_INTERFACE_MODE_IP)
852 vnet_delete_hw_interface (vnm, mif->hw_if_index);
854 ethernet_delete_interface (vnm, mif->hw_if_index);
855 mif->hw_if_index = ~0;
858 /* free interface data structures */
859 mhash_unset (&msf->dev_instance_by_id, &mif->id, 0);
861 /* remove socket file */
862 if (--(msf->ref_cnt) == 0)
864 if (msf->is_listener)
868 vec_foreach_index (i, msf->pending_clients)
869 memif_socket_close (msf->pending_clients + i);
871 memif_socket_close (&msf->sock);
872 vec_free (msf->pending_clients);
874 mhash_free (&msf->dev_instance_by_id);
875 hash_free (msf->dev_instance_by_fd);
878 err = clib_socket_close (msf->sock);
881 memif_log_err (mif, "%U", format_clib_error, err);
882 clib_error_free (err);
884 clib_mem_free (msf->sock);
888 vec_free (mif->local_disc_string);
889 clib_memset (mif, 0, sizeof (*mif));
890 pool_put (mm->interfaces, mif);
892 if (pool_elts (mm->interfaces) == 0)
893 vlib_process_signal_event (vm, memif_process_node.index,
894 MEMIF_PROCESS_EVENT_STOP, 0);
900 VNET_HW_INTERFACE_CLASS (memif_ip_hw_if_class, static) = {
902 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
903 .tx_hash_fn_type = VNET_HASH_FN_TYPE_IP,
908 memif_create_if (vlib_main_t *vm, memif_create_if_args_t *args)
910 memif_main_t *mm = &memif_main;
911 vlib_thread_main_t *tm = vlib_get_thread_main ();
912 vnet_main_t *vnm = vnet_get_main ();
913 vnet_eth_interface_registration_t eir = {};
915 vnet_sw_interface_t *sw;
917 memif_socket_file_t *msf = 0;
918 clib_error_t *err = 0;
920 p = hash_get (mm->socket_file_index_by_sock_id, args->socket_id);
923 err = vnet_error (VNET_ERR_INVALID_ARGUMENT, "unknown socket id");
927 msf = vec_elt_at_index (mm->socket_files, p[0]);
929 /* existing socket file can be either master or slave but cannot be both */
930 if (msf->ref_cnt > 0)
932 if ((!msf->is_listener != !args->is_master))
935 vnet_error (VNET_ERR_SUBIF_ALREADY_EXISTS,
936 "socket file cannot be used by both master and slave");
940 p = mhash_get (&msf->dev_instance_by_id, &args->id);
943 err = vnet_error (VNET_ERR_SUBIF_ALREADY_EXISTS,
944 "interface already exists");
949 /* Create new socket file */
950 if (msf->ref_cnt == 0)
952 struct stat file_stat;
954 /* If we are creating listener make sure file doesn't exist or if it
955 * exists thn delete it if it is old socket file */
956 if (args->is_master && (stat ((char *) msf->filename, &file_stat) == 0))
958 if (S_ISSOCK (file_stat.st_mode))
960 unlink ((char *) msf->filename);
964 err = vnet_error (VNET_ERR_VALUE_EXIST, "File exists for %s",
970 mhash_init (&msf->dev_instance_by_id, sizeof (uword),
971 sizeof (memif_interface_id_t));
972 msf->dev_instance_by_fd = hash_create (0, sizeof (uword));
973 msf->is_listener = (args->is_master != 0);
975 memif_log_debug (0, "initializing socket file %s", msf->filename);
978 if (mm->per_thread_data == 0)
982 vec_validate_aligned (mm->per_thread_data, tm->n_vlib_mains - 1,
983 CLIB_CACHE_LINE_BYTES);
985 for (i = 0; i < tm->n_vlib_mains; i++)
987 memif_per_thread_data_t *ptd =
988 vec_elt_at_index (mm->per_thread_data, i);
989 vlib_buffer_t *bt = &ptd->buffer_template;
990 clib_memset (bt, 0, sizeof (vlib_buffer_t));
991 bt->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
992 bt->total_length_not_including_first_buffer = 0;
993 vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
995 vec_validate_aligned (ptd->copy_ops, 0, CLIB_CACHE_LINE_BYTES);
996 vec_reset_length (ptd->copy_ops);
997 vec_validate_aligned (ptd->buffers, 0, CLIB_CACHE_LINE_BYTES);
998 vec_reset_length (ptd->buffers);
1002 pool_get (mm->interfaces, mif);
1003 clib_memset (mif, 0, sizeof (*mif));
1004 mif->dev_instance = mif - mm->interfaces;
1005 mif->socket_file_index = msf - mm->socket_files;
1007 mif->sw_if_index = mif->hw_if_index = mif->per_interface_next_index = ~0;
1008 mif->mode = args->mode;
1010 mif->secret = vec_dup (args->secret);
1012 if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
1015 if (!args->hw_addr_set)
1017 f64 now = vlib_time_now (vm);
1019 rnd = (u32) (now * 1e6);
1020 rnd = random_u32 (&rnd);
1022 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
1023 args->hw_addr[0] = 2;
1024 args->hw_addr[1] = 0xfe;
1027 eir.dev_class_index = memif_device_class.index;
1028 eir.dev_instance = mif->dev_instance;
1029 eir.address = args->hw_addr;
1030 eir.cb.flag_change = memif_eth_flag_change;
1031 eir.cb.set_max_frame_size = memif_eth_set_max_frame_size;
1032 mif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
1034 else if (mif->mode == MEMIF_INTERFACE_MODE_IP)
1037 vnet_register_interface (vnm, memif_device_class.index,
1039 memif_ip_hw_if_class.index,
1045 vnet_error (VNET_ERR_SYSCALL_ERROR_2, "unsupported interface mode");
1049 sw = vnet_get_hw_sw_interface (vnm, mif->hw_if_index);
1050 mif->sw_if_index = sw->sw_if_index;
1052 mif->cfg.log2_ring_size = args->log2_ring_size;
1053 mif->cfg.buffer_size = args->buffer_size;
1054 mif->cfg.num_s2m_rings =
1055 args->is_master ? args->rx_queues : args->tx_queues;
1056 mif->cfg.num_m2s_rings =
1057 args->is_master ? args->tx_queues : args->rx_queues;
1059 args->sw_if_index = mif->sw_if_index;
1061 /* If this is new one, start listening */
1062 if (msf->is_listener && msf->ref_cnt == 0)
1064 clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
1066 ASSERT (msf->sock == 0);
1069 clib_memset (s, 0, sizeof (clib_socket_t));
1070 s->config = (char *) msf->filename;
1071 s->flags = CLIB_SOCKET_F_IS_SERVER |
1072 CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
1073 CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
1075 if ((err = clib_socket_init (s)))
1077 err->code = VNET_ERR_SYSCALL_ERROR_4;
1081 clib_file_t template = { 0 };
1082 template.read_function = memif_conn_fd_accept_ready;
1083 template.file_descriptor = msf->sock->fd;
1084 template.private_data = mif->socket_file_index;
1085 template.description = format (0, "memif listener %s", msf->filename);
1086 memif_file_add (&msf->sock->private_data, &template);
1091 if (args->is_master == 0)
1093 mif->flags |= MEMIF_IF_FLAG_IS_SLAVE;
1094 if (args->is_zero_copy)
1095 mif->flags |= MEMIF_IF_FLAG_ZERO_COPY;
1098 vnet_hw_if_set_caps (vnm, mif->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
1099 vnet_hw_if_set_input_node (vnm, mif->hw_if_index, memif_input_node.index);
1100 mhash_set (&msf->dev_instance_by_id, &mif->id, mif->dev_instance, 0);
1102 if (pool_elts (mm->interfaces) == 1)
1104 vlib_process_signal_event (vm, memif_process_node.index,
1105 MEMIF_PROCESS_EVENT_START, 0);
1110 memif_delete_if (vm, mif);
1112 memif_log_err (mif, "%U", format_clib_error, err);
1120 memif_interface_admin_up_down (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
1122 memif_main_t *mm = &memif_main;
1123 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1124 memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
1125 static clib_error_t *error = 0;
1127 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
1129 if (mif->flags & MEMIF_IF_FLAG_CONNECTED)
1131 vnet_hw_interface_set_flags (vnm, mif->hw_if_index,
1132 VNET_HW_INTERFACE_FLAG_LINK_UP);
1134 mif->flags |= MEMIF_IF_FLAG_ADMIN_UP;
1137 mif->flags &= ~MEMIF_IF_FLAG_ADMIN_UP;
1139 vlib_process_signal_event (vnm->vlib_main, memif_process_node.index,
1140 MEMIF_PROCESS_EVENT_ADMIN_UP_DOWN, 0);
1144 static clib_error_t *
1145 memif_init (vlib_main_t * vm)
1147 memif_main_t *mm = &memif_main;
1149 clib_memset (mm, 0, sizeof (memif_main_t));
1151 mm->log_class = vlib_log_register_class ("memif_plugin", 0);
1152 memif_log_debug (0, "initialized");
1154 /* initialize binary API */
1155 memif_plugin_api_hookup (vm);
1158 * Pre-stuff socket filename pool with a non-modifieable mapping
1159 * for socket-id 0 to MEMIF_DEFAULT_SOCKET_FILENAME in the
1160 * default run-time directory.
1162 memif_socket_filename_add_del (1, 0, (u8 *) MEMIF_DEFAULT_SOCKET_FILENAME);
1167 VLIB_INIT_FUNCTION (memif_init);
1170 VLIB_PLUGIN_REGISTER () = {
1171 .version = VPP_BUILD_VER,
1172 .description = "Packet Memory Interface (memif) -- Experimental",
1177 * fd.io coding-style-patch-verification: ON
1180 * eval: (c-set-style "gnu")