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 <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>
43 memif_main_t memif_main;
46 memif_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
53 memif_queue_intfd_close (memif_queue_t * mq)
55 if (mq->int_clib_file_index != ~0)
57 memif_file_del_by_index (mq->int_clib_file_index);
58 mq->int_clib_file_index = ~0;
61 else if (mq->int_fd > -1)
69 memif_disconnect (memif_if_t * mif, clib_error_t * err)
71 memif_main_t *mm = &memif_main;
72 vnet_main_t *vnm = vnet_get_main ();
80 vlib_log_debug (mm->log_class, "disconnect %u (%v)", mif->dev_instance,
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);
92 /* set interface down */
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 /* close connection socket */
98 if (mif->sock && mif->sock->fd)
100 memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
101 mif->socket_file_index);
102 hash_unset (msf->dev_instance_by_fd, mif->sock->fd);
103 memif_socket_close (&mif->sock);
108 err = clib_socket_close (mif->sock);
111 vlib_log_err (mm->log_class, "%U", format_clib_error, err);
112 clib_error_free (err);
114 clib_mem_free (mif->sock);
118 vec_foreach_index (i, mif->rx_queues)
120 mq = vec_elt_at_index (mif->rx_queues, i);
124 rv = vnet_hw_interface_unassign_rx_thread (vnm, mif->hw_if_index, i);
126 vlib_log_warn (mm->log_class,
127 "Unable to unassign interface %d, queue %d: rc=%d",
128 mif->hw_if_index, i, rv);
133 /* free tx and rx queues */
134 vec_foreach (mq, mif->rx_queues)
135 memif_queue_intfd_close (mq);
136 vec_free (mif->rx_queues);
138 vec_foreach (mq, mif->tx_queues)
139 memif_queue_intfd_close (mq);
140 vec_free (mif->tx_queues);
142 /* free memory regions */
143 vec_foreach (mr, mif->regions)
148 if ((rv = munmap (mr->shm, mr->region_size)))
149 clib_warning ("munmap failed, rv = %d", rv);
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);
160 static clib_error_t *
161 memif_int_fd_read_ready (clib_file_t * uf)
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);
171 size = read (uf->file_descriptor, &b, sizeof (b));
174 vlib_log_debug (mm->log_class, "Failed to read form socket");
178 vnet_device_input_set_interrupt_pending (vnm, mif->hw_if_index, qid);
186 memif_connect (memif_if_t * mif)
188 memif_main_t *mm = &memif_main;
189 vnet_main_t *vnm = vnet_get_main ();
190 clib_file_t template = { 0 };
193 clib_error_t *err = NULL;
195 vlib_log_debug (mm->log_class, "connect %u", mif->dev_instance);
197 vec_free (mif->local_disc_string);
198 vec_free (mif->remote_disc_string);
201 vec_foreach (mr, mif->regions)
208 err = clib_error_return (0, "no memory region fd");
212 if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
213 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
215 err = clib_error_return_unix (0, "mmap");
221 template.read_function = memif_int_fd_read_ready;
224 vec_foreach_index (i, mif->tx_queues)
226 memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i);
228 mq->ring = mif->regions[mq->region].shm + mq->offset;
229 if (mq->ring->cookie != MEMIF_COOKIE)
231 err = clib_error_return (0, "wrong cookie on tx ring %u", i);
236 vec_foreach_index (i, mif->rx_queues)
238 memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i);
241 mq->ring = mif->regions[mq->region].shm + mq->offset;
242 if (mq->ring->cookie != MEMIF_COOKIE)
244 err = clib_error_return (0, "wrong cookie on tx ring %u", i);
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",
253 format_memif_device_name,
254 mif->dev_instance, i);
255 memif_file_add (&mq->int_clib_file_index, &template);
257 vnet_hw_interface_assign_rx_thread (vnm, mif->hw_if_index, i, ~0);
258 rv = vnet_hw_interface_set_rx_mode (vnm, mif->hw_if_index, i,
259 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
262 ("Warning: unable to set rx mode for interface %d queue %d: "
263 "rc=%d", mif->hw_if_index, i, rv);
266 vnet_hw_interface_rx_mode rxmode;
267 vnet_hw_interface_get_rx_mode (vnm, mif->hw_if_index, i, &rxmode);
269 if (rxmode == VNET_HW_INTERFACE_RX_MODE_POLLING)
270 mq->ring->flags |= MEMIF_RING_FLAG_MASK_INT;
272 vnet_device_input_set_interrupt_pending (vnm, mif->hw_if_index, i);
277 mif->flags &= ~MEMIF_IF_FLAG_CONNECTING;
278 mif->flags |= MEMIF_IF_FLAG_CONNECTED;
280 vnet_hw_interface_set_flags (vnm, mif->hw_if_index,
281 VNET_HW_INTERFACE_FLAG_LINK_UP);
285 vlib_log_err (mm->log_class, "%U", format_clib_error, err);
289 static_always_inline memif_ring_t *
290 memif_get_ring (memif_if_t * mif, memif_ring_type_t type, u16 ring_num)
292 if (vec_len (mif->regions) == 0)
294 void *p = mif->regions[0].shm;
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;
300 return (memif_ring_t *) p;
304 memif_init_regions_and_queues (memif_if_t * mif)
306 vlib_main_t *vm = vlib_get_main ();
307 memif_main_t *mm = &memif_main;
308 memif_ring_t *ring = NULL;
312 clib_mem_vm_alloc_t alloc = { 0 };
315 ASSERT (vec_len (mif->regions) == 0);
316 vec_add2_aligned (mif->regions, r, 1, CLIB_CACHE_LINE_BYTES);
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));
322 r->region_size = buffer_offset;
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);
328 alloc.name = "memif region";
329 alloc.size = r->region_size;
330 alloc.flags = CLIB_MEM_VM_F_SHARED;
332 err = clib_mem_vm_ext_alloc (&alloc);
339 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
341 vlib_buffer_pool_t *bp;
343 vec_foreach (bp, buffer_main.buffer_pools)
345 vlib_physmem_region_t *pr;
346 pr = vlib_physmem_get_region (vm, bp->physmem_region);
347 vec_add2_aligned (mif->regions, r, 1, CLIB_CACHE_LINE_BYTES);
349 r->region_size = pr->size;
356 for (i = 0; i < mif->run.num_s2m_rings; i++)
358 ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
359 ring->head = ring->tail = 0;
360 ring->cookie = MEMIF_COOKIE;
362 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
365 for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
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;
374 for (i = 0; i < mif->run.num_m2s_rings; i++)
376 ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
377 ring->head = ring->tail = 0;
378 ring->cookie = MEMIF_COOKIE;
380 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
383 for (j = 0; j < (1 << mif->run.log2_ring_size); j++)
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;
394 ASSERT (mif->tx_queues == 0);
395 vec_validate_aligned (mif->tx_queues, mif->run.num_s2m_rings - 1,
396 CLIB_CACHE_LINE_BYTES);
399 vec_foreach_index (i, mif->tx_queues)
401 memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i);
402 if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
404 err = clib_error_return_unix (0, "eventfd[tx queue %u]", i);
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;
411 mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
413 mq->type = MEMIF_RING_S2M;
414 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
415 vec_validate_aligned (mq->buffers, 1 << mq->log2_ring_size,
416 CLIB_CACHE_LINE_BYTES);
420 ASSERT (mif->rx_queues == 0);
421 vec_validate_aligned (mif->rx_queues, mif->run.num_m2s_rings - 1,
422 CLIB_CACHE_LINE_BYTES);
425 vec_foreach_index (i, mif->rx_queues)
427 memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i);
428 if ((mq->int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
430 err = clib_error_return_unix (0, "eventfd[rx queue %u]", i);
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;
437 mq->offset = (void *) mq->ring - (void *) mif->regions[mq->region].shm;
439 mq->type = MEMIF_RING_M2S;
440 if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
441 vec_validate_aligned (mq->buffers, 1 << mq->log2_ring_size,
442 CLIB_CACHE_LINE_BYTES);
449 vlib_log_err (mm->log_class, "%U", format_clib_error, err);
454 memif_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
456 memif_main_t *mm = &memif_main;
459 uword *event_data = 0, event_type;
461 f64 start_time, last_run_duration = 0, now;
464 sock = clib_mem_alloc (sizeof (clib_socket_t));
465 memset (sock, 0, sizeof (clib_socket_t));
470 vlib_process_wait_for_event_or_clock (vm, (f64) 3 -
473 vlib_process_wait_for_event (vm);
475 event_type = vlib_process_get_events (vm, &event_data);
476 vec_reset_length (event_data);
482 case MEMIF_PROCESS_EVENT_START:
485 case MEMIF_PROCESS_EVENT_STOP:
492 last_run_duration = start_time = vlib_time_now (vm);
494 pool_foreach (mif, mm->interfaces,
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)
501 vlib_process_suspend (vm, 100e-6); /* suspend for 100 us */
502 start_time = vlib_time_now (vm);
505 if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) == 0)
508 if (mif->flags & MEMIF_IF_FLAG_CONNECTING)
511 if (mif->flags & MEMIF_IF_FLAG_CONNECTED)
514 if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
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;
520 if ((err = clib_socket_init (sock)))
522 clib_error_free (err);
526 clib_file_t t = { 0 };
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",
535 format_memif_device_name,
537 hash_set (msf->dev_instance_by_fd, sock->fd, mif->dev_instance);
539 mif->flags |= MEMIF_IF_FLAG_CONNECTING;
541 sock = clib_mem_alloc (sizeof(clib_socket_t));
546 last_run_duration = vlib_time_now (vm) - last_run_duration;
552 VLIB_REGISTER_NODE (memif_process_node,static) = {
553 .function = memif_process,
554 .type = VLIB_NODE_TYPE_PROCESS,
555 .name = "memif-process",
560 memif_add_socket_file (u32 sock_id, u8 * socket_filename)
562 memif_main_t *mm = &memif_main;
564 memif_socket_file_t *msf;
566 p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
569 msf = pool_elt_at_index (mm->socket_files, *p);
570 if (strcmp ((char *) msf->filename, (char *) socket_filename) == 0)
572 /* Silently accept identical "add". */
576 /* But don't allow a direct add of a different filename. */
577 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
580 pool_get (mm->socket_files, msf);
581 memset (msf, 0, sizeof (memif_socket_file_t));
583 msf->filename = socket_filename;
584 msf->socket_id = sock_id;
586 hash_set (mm->socket_file_index_by_sock_id, sock_id,
587 msf - mm->socket_files);
593 memif_delete_socket_file (u32 sock_id)
595 memif_main_t *mm = &memif_main;
597 memif_socket_file_t *msf;
599 p = hash_get (mm->socket_file_index_by_sock_id, sock_id);
602 /* Don't delete non-existent entries. */
603 return VNET_API_ERROR_INVALID_ARGUMENT;
606 msf = pool_elt_at_index (mm->socket_files, *p);
607 if (msf->ref_cnt > 0)
609 return VNET_API_ERROR_UNEXPECTED_INTF_STATE;
612 vec_free (msf->filename);
613 pool_put (mm->socket_files, msf);
615 hash_unset (mm->socket_file_index_by_sock_id, sock_id);
621 memif_socket_filename_add_del (u8 is_add, u32 sock_id, u8 * sock_filename)
623 if (sock_id == 0 || sock_id == ~0)
625 return VNET_API_ERROR_INVALID_ARGUMENT;
630 return memif_delete_socket_file (sock_id);
633 if (sock_filename == 0 || sock_filename[0] == 0)
635 return VNET_API_ERROR_INVALID_ARGUMENT;
638 if (sock_filename[0] != '/')
641 error = vlib_unix_recursive_mkdir (vlib_unix_get_runtime_dir ());
644 clib_error_free (error);
645 return VNET_API_ERROR_SYSCALL_ERROR_1;
648 sock_filename = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
653 sock_filename = vec_dup (sock_filename);
656 return memif_add_socket_file (sock_id, sock_filename);
660 memif_delete_if (vlib_main_t * vm, memif_if_t * mif)
662 vnet_main_t *vnm = vnet_get_main ();
663 memif_main_t *mm = &memif_main;
664 memif_socket_file_t *msf =
665 vec_elt_at_index (mm->socket_files, mif->socket_file_index);
668 mif->flags |= MEMIF_IF_FLAG_DELETING;
669 vec_free (mif->local_disc_string);
670 vec_free (mif->remote_disc_string);
672 /* bring down the interface */
673 vnet_hw_interface_set_flags (vnm, mif->hw_if_index, 0);
674 vnet_sw_interface_set_flags (vnm, mif->sw_if_index, 0);
676 err = clib_error_return (0, "interface deleted");
677 memif_disconnect (mif, err);
678 clib_error_free (err);
680 if (mif->hw_if_index != ~0)
682 /* remove the interface */
683 if (mif->mode == MEMIF_INTERFACE_MODE_IP)
684 vnet_delete_hw_interface (vnm, mif->hw_if_index);
686 ethernet_delete_interface (vnm, mif->hw_if_index);
687 mif->hw_if_index = ~0;
690 /* free interface data structures */
691 clib_spinlock_free (&mif->lockp);
692 mhash_unset (&msf->dev_instance_by_id, &mif->id, 0);
694 /* remove socket file */
695 if (--(msf->ref_cnt) == 0)
697 if (msf->is_listener)
701 vec_foreach_index (i, msf->pending_clients)
702 memif_socket_close (msf->pending_clients + i);
704 memif_socket_close (&msf->sock);
705 vec_free (msf->pending_clients);
707 mhash_free (&msf->dev_instance_by_id);
708 hash_free (msf->dev_instance_by_fd);
711 err = clib_socket_close (msf->sock);
714 vlib_log_err (mm->log_class, "%U", format_clib_error, err);
715 clib_error_free (err);
717 clib_mem_free (msf->sock);
721 memset (mif, 0, sizeof (*mif));
722 pool_put (mm->interfaces, mif);
724 if (pool_elts (mm->interfaces) == 0)
725 vlib_process_signal_event (vm, memif_process_node.index,
726 MEMIF_PROCESS_EVENT_STOP, 0);
732 VNET_HW_INTERFACE_CLASS (memif_ip_hw_if_class, static) =
735 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
740 memif_create_if (vlib_main_t * vm, memif_create_if_args_t * args)
742 memif_main_t *mm = &memif_main;
743 vlib_thread_main_t *tm = vlib_get_thread_main ();
744 vnet_main_t *vnm = vnet_get_main ();
746 vnet_sw_interface_t *sw;
747 clib_error_t *error = 0;
750 vnet_hw_interface_t *hw;
751 memif_socket_file_t *msf = 0;
754 p = hash_get (mm->socket_file_index_by_sock_id, args->socket_id);
757 rv = VNET_API_ERROR_INVALID_ARGUMENT;
761 msf = vec_elt_at_index (mm->socket_files, p[0]);
763 /* existing socket file can be either master or slave but cannot be both */
764 if (msf->ref_cnt > 0)
766 if ((!msf->is_listener != !args->is_master))
768 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
772 p = mhash_get (&msf->dev_instance_by_id, &args->id);
775 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
780 /* Create new socket file */
781 if (msf->ref_cnt == 0)
783 struct stat file_stat;
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))
789 if (S_ISSOCK (file_stat.st_mode))
791 unlink ((char *) msf->filename);
795 error = clib_error_return (0, "File exists for %s",
797 rv = VNET_API_ERROR_VALUE_EXIST;
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);
807 vlib_log_debug (mm->log_class, "initializing socket file %s",
811 if (mm->per_thread_data == 0)
814 vlib_buffer_free_list_t *fl;
816 vec_validate_aligned (mm->per_thread_data, tm->n_vlib_mains - 1,
817 CLIB_CACHE_LINE_BYTES);
820 vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
821 for (i = 0; i < tm->n_vlib_mains; i++)
823 memif_per_thread_data_t *ptd =
824 vec_elt_at_index (mm->per_thread_data, i);
825 vlib_buffer_t *bt = &ptd->buffer_template;
826 vlib_buffer_init_for_free_list (bt, fl);
827 bt->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
828 bt->total_length_not_including_first_buffer = 0;
829 vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
831 /* initially prealloc copy_ops so we can use
832 _vec_len instead of vec_elen */
833 vec_validate_aligned (ptd->copy_ops, 0, CLIB_CACHE_LINE_BYTES);
834 vec_reset_length (ptd->copy_ops);
835 vec_validate_aligned (ptd->buffers, 0, CLIB_CACHE_LINE_BYTES);
836 vec_reset_length (ptd->buffers);
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;
845 mif->sw_if_index = mif->hw_if_index = mif->per_interface_next_index = ~0;
846 mif->mode = args->mode;
848 mif->secret = vec_dup (args->secret);
850 if (tm->n_vlib_mains > 1)
851 clib_spinlock_init (&mif->lockp);
853 if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
856 if (!args->hw_addr_set)
858 f64 now = vlib_time_now (vm);
860 rnd = (u32) (now * 1e6);
861 rnd = random_u32 (&rnd);
863 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
864 args->hw_addr[0] = 2;
865 args->hw_addr[1] = 0xfe;
867 error = ethernet_register_interface (vnm, memif_device_class.index,
868 mif->dev_instance, args->hw_addr,
870 memif_eth_flag_change);
872 else if (mif->mode == MEMIF_INTERFACE_MODE_IP)
875 vnet_register_interface (vnm, memif_device_class.index,
877 memif_ip_hw_if_class.index,
881 error = clib_error_return (0, "unsupported interface mode");
885 ret = VNET_API_ERROR_SYSCALL_ERROR_2;
889 sw = vnet_get_hw_sw_interface (vnm, mif->hw_if_index);
890 mif->sw_if_index = sw->sw_if_index;
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;
899 args->sw_if_index = mif->sw_if_index;
901 /* If this is new one, start listening */
902 if (msf->is_listener && msf->ref_cnt == 0)
904 struct stat file_stat;
905 clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
907 ASSERT (msf->sock == 0);
910 memset (s, 0, sizeof (clib_socket_t));
911 s->config = (char *) msf->filename;
912 s->flags = CLIB_SOCKET_F_IS_SERVER |
913 CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
914 CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
916 if ((error = clib_socket_init (s)))
918 ret = VNET_API_ERROR_SYSCALL_ERROR_4;
922 if (stat ((char *) msf->filename, &file_stat) == -1)
924 ret = VNET_API_ERROR_SYSCALL_ERROR_8;
928 clib_file_t template = { 0 };
929 template.read_function = memif_conn_fd_accept_ready;
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);
938 if (args->is_master == 0)
940 mif->flags |= MEMIF_IF_FLAG_IS_SLAVE;
941 if (args->is_zero_copy)
942 mif->flags |= MEMIF_IF_FLAG_ZERO_COPY;
945 hw = vnet_get_hw_interface (vnm, mif->hw_if_index);
946 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
947 vnet_hw_interface_set_input_node (vnm, mif->hw_if_index,
948 memif_input_node.index);
950 mhash_set (&msf->dev_instance_by_id, &mif->id, mif->dev_instance, 0);
952 if (pool_elts (mm->interfaces) == 1)
954 vlib_process_signal_event (vm, memif_process_node.index,
955 MEMIF_PROCESS_EVENT_START, 0);
960 if (mif->hw_if_index != ~0)
962 if (mif->mode == MEMIF_INTERFACE_MODE_IP)
963 vnet_delete_hw_interface (vnm, mif->hw_if_index);
965 ethernet_delete_interface (vnm, mif->hw_if_index);
966 mif->hw_if_index = ~0;
968 memif_delete_if (vm, mif);
971 vlib_log_err (mm->log_class, "%U", format_clib_error, error);
972 clib_error_free (error);
980 static clib_error_t *
981 memif_init (vlib_main_t * vm)
983 memif_main_t *mm = &memif_main;
986 memset (mm, 0, sizeof (memif_main_t));
988 mm->log_class = vlib_log_register_class ("memif_plugin", 0);
989 vlib_log_debug (mm->log_class, "initialized");
991 /* initialize binary API */
992 memif_plugin_api_hookup (vm);
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.
999 filename = format (0, "%s/%s%c",
1000 vlib_unix_get_runtime_dir (),
1001 MEMIF_DEFAULT_SOCKET_FILENAME, 0);
1002 memif_add_socket_file (0, filename);
1007 VLIB_INIT_FUNCTION (memif_init);
1010 VLIB_PLUGIN_REGISTER () = {
1011 .version = VPP_BUILD_VER,
1012 .description = "Packet Memory Interface (experimetal)",
1017 * fd.io coding-style-patch-verification: ON
1020 * eval: (c-set-style "gnu")