/* *------------------------------------------------------------------ * Copyright (c) 2017 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *------------------------------------------------------------------ */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include memif_main_t memif_main; static u32 memif_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags) { /* nothing for now */ return 0; } static void memif_queue_intfd_close (memif_queue_t * mq) { if (mq->int_clib_file_index != ~0) { memif_file_del_by_index (mq->int_clib_file_index); mq->int_clib_file_index = ~0; mq->int_fd = -1; } else if (mq->int_fd > -1) { close (mq->int_fd); mq->int_fd = -1; } } void memif_disconnect (memif_if_t * mif, clib_error_t * err) { memif_main_t *mm = &memif_main; vnet_main_t *vnm = vnet_get_main (); memif_region_t *mr; memif_queue_t *mq; int i; if (mif == 0) return; memif_log_debug (mif, "disconnect %u (%v)", mif->dev_instance, err ? err->what : 0); if (err) { clib_error_t *e = 0; mif->local_disc_string = vec_dup (err->what); if (mif->sock && clib_socket_is_connected (mif->sock)) e = memif_msg_send_disconnect (mif, err); clib_error_free (e); } /* set interface down */ mif->flags &= ~(MEMIF_IF_FLAG_CONNECTED | MEMIF_IF_FLAG_CONNECTING); if (mif->hw_if_index != ~0) vnet_hw_interface_set_flags (vnm, mif->hw_if_index, 0); /* close connection socket */ if (mif->sock && mif->sock->fd) { memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files, mif->socket_file_index); hash_unset (msf->dev_instance_by_fd, mif->sock->fd); memif_socket_close (&mif->sock); } else if (mif->sock) { clib_error_t *err; err = clib_socket_close (mif->sock); if (err) { memif_log_err (mif, "%U", format_clib_error, err); clib_error_free (err); } clib_mem_free (mif->sock); } /* *INDENT-OFF* */ vec_foreach_index (i, mif->rx_queues) { mq = vec_elt_at_index (mif->rx_queues, i); if (mq->ring) { int rv; rv = vnet_hw_interface_unassign_rx_thread (vnm, mif->hw_if_index, i); if (rv) memif_log_warn (mif, "Unable to unassign interface %d, queue %d: rc=%d", mif->hw_if_index, i, rv); mq->ring = 0; } } /* free tx and rx queues */ vec_foreach (mq, mif->rx_queues) memif_queue_intfd_close (mq); vec_free (mif->rx_queues); vec_foreach (mq, mif->tx_queues) memif_queue_intfd_close (mq); vec_free (mif->tx_queues); /* free memory regions */ vec_foreach (mr, mif->regions) { int rv; if (mr->is_external) continue; if ((rv = munmap (mr->shm, mr->region_size))) memif_log_err (mif, "munmap failed, rv = %d", rv); if (mr->fd > -1) close (mr->fd); } /* *INDENT-ON* */ vec_free (mif->regions); vec_free (mif->remote_name); vec_free (mif->remote_if_name); clib_fifo_free (mif->msg_queue); } static clib_error_t * memif_int_fd_read_ready (clib_file_t * uf) { memif_main_t *mm = &memif_main; vnet_main_t *vnm = vnet_get_main (); u16 qid = uf->private_data & 0xFFFF; memif_if_t *mif = vec_elt_at_index (mm->interfaces, uf->private_data >> 16); memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid); u64 b; ssize_t size; size = read (uf->file_descriptor, &b, sizeof (b)); if (size < 0) { memif_log_debug (mif, "Failed to read form socket"); return 0; } vnet_device_input_set_interrupt_pending (vnm, mif->hw_if_index, qid); mq->int_count++; return 0; } clib_error_t * memif_connect (memif_if_t * mif) { vnet_main_t *vnm = vnet_get_main (); clib_file_t template = { 0 }; memif_region_t *mr; int i; clib_error_t *err = NULL; memif_log_debug (mif, "connect %u", mif->dev_instance); vec_free (mif->local_disc_string); vec_free (mif->remote_disc_string); /* *INDENT-OFF* */ vec_foreach (mr, mif->regions) { if (mr->shm) continue; if (mr->fd < 0) { err = clib_error_return (0, "no memory region fd"); goto error; } if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, MAP_SHARED, mr->fd, 0)) == MAP_FAILED) { err = clib_error_return_unix (0, "mmap"); goto error; } } /* *INDENT-ON* */ template.read_function = memif_int_fd_read_ready; /* *INDENT-OFF* */ vec_foreach_index (i, mif->tx_queues) { memif_queue_t *mq = vec_elt_at_index (mif->tx_queues, i); mq->ring = mif->regions[mq->region].shm + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) { err = clib_error_return (0, "wrong cookie on tx ring %u", i); goto error; } } vec_foreach_index (i, mif->rx_queues) { memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, i); int rv; mq->ring = mif->regions[mq->region].shm + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) { err = clib_error_return (0, "wrong cookie on tx ring %u", i); goto error; } if (mq->int_fd > -1) { template.file_descriptor = mq->int_fd; template.private_data = (mif->dev_instance << 16) | (i & 0xFFFF); template.description = format (0, "%U rx %u int", format_memif_device_name, mif->dev_instance, i); memif_file_add (&mq->int_clib_file_index, &template); } vnet_hw_interface_assign_rx_threa
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __included_tw_timer_16t_1w_2048sl__
#define __included_tw_timer_16t_1w_2048sl__

/* ... So that a client app can create multiple wheel geometries */
#undef TW_TIMER_WHEELS
#undef TW_SLOTS_PER_RING
#undef TW_RING_SHIFT
#undef TW_RING_MASK
#undef TW_TIMERS_PER_OBJECT
#undef LOG2_TW_TIMERS_PER_OBJECT
#undef TW_SUFFIX
#undef TW_OVERFLOW_VECTOR
#undef TW_FAST_WHEEL_BITMAP
#undef TW_TIMER_ALLOW_DUPLICATE_STOP
#undef TW_START_STOP_TRACE_SIZE

#define TW_TIMER_WHEELS 1
#define TW_SLOTS_PER_RING 2048
#define TW_RING_SHIFT 11
#define TW_RING_MASK (TW_SLOTS_PER_RING -1)
#define TW_TIMERS_PER_OBJECT 16
#define LOG2_TW_TIMERS_PER_OBJECT 4
#define TW_SUFFIX _16t_1w_2048sl
#define TW_FAST_WHEEL_BITMAP 0
#define TW_TIMER_ALLOW_DUPLICATE_STOP 0

#include <vppinfra/tw_timer_template.h>

#endif /* __included_tw_timer_16t_1w_2048sl__ */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
stance_by_id, &mif->id, 0); /* remove socket file */ if (--(msf->ref_cnt) == 0) { if (msf->is_listener) { int i; /* *INDENT-OFF* */ vec_foreach_index (i, msf->pending_clients) memif_socket_close (msf->pending_clients + i); /* *INDENT-ON* */ memif_socket_close (&msf->sock); vec_free (msf->pending_clients); } mhash_free (&msf->dev_instance_by_id); hash_free (msf->dev_instance_by_fd); if (msf->sock) { err = clib_socket_close (msf->sock); if (err) { memif_log_err (mif, "%U", format_clib_error, err); clib_error_free (err); } clib_mem_free (msf->sock); } } memset (mif, 0, sizeof (*mif)); pool_put (mm->interfaces, mif); if (pool_elts (mm->interfaces) == 0) vlib_process_signal_event (vm, memif_process_node.index, MEMIF_PROCESS_EVENT_STOP, 0); return 0; } /* *INDENT-OFF* */ VNET_HW_INTERFACE_CLASS (memif_ip_hw_if_class, static) = { .name = "memif-ip", .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, }; /* *INDENT-ON* */ int memif_create_if (vlib_main_t * vm, memif_create_if_args_t * args) { memif_main_t *mm = &memif_main; vlib_thread_main_t *tm = vlib_get_thread_main (); vnet_main_t *vnm = vnet_get_main (); memif_if_t *mif = 0; vnet_sw_interface_t *sw; clib_error_t *error = 0; int ret = 0; uword *p; vnet_hw_interface_t *hw; memif_socket_file_t *msf = 0; int rv = 0; p = hash_get (mm->socket_file_index_by_sock_id, args->socket_id); if (p == 0) { rv = VNET_API_ERROR_INVALID_ARGUMENT; goto done; } msf = vec_elt_at_index (mm->socket_files, p[0]); /* existing socket file can be either master or slave but cannot be both */ if (msf->ref_cnt > 0) { if ((!msf->is_listener != !args->is_master)) { rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS; goto done; } p = mhash_get (&msf->dev_instance_by_id, &args->id); if (p) { rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS; goto done; } } /* Create new socket file */ if (msf->ref_cnt == 0) { struct stat file_stat; /* If we are creating listener make sure file doesn't exist or if it * exists thn delete it if it is old socket file */ if (args->is_master && (stat ((char *) msf->filename, &file_stat) == 0)) { if (S_ISSOCK (file_stat.st_mode)) { unlink ((char *) msf->filename); } else { error = clib_error_return (0, "File exists for %s", msf->filename); rv = VNET_API_ERROR_VALUE_EXIST; goto done; } } mhash_init (&msf->dev_instance_by_id, sizeof (uword), sizeof (memif_interface_id_t)); msf->dev_instance_by_fd = hash_create (0, sizeof (uword)); msf->is_listener = (args->is_master != 0); memif_log_debug (0, "initializing socket file %s", msf->filename); } if (mm->per_thread_data == 0) { int i; vlib_buffer_free_list_t *fl; vec_validate_aligned (mm->per_thread_data, tm->n_vlib_mains - 1, CLIB_CACHE_LINE_BYTES); fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); for (i = 0; i < tm->n_vlib_mains; i++) { memif_per_thread_data_t *ptd = vec_elt_at_index (mm->per_thread_data, i); vlib_buffer_t *bt = &ptd->buffer_template; vlib_buffer_init_for_free_list (bt, fl); bt->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID; bt->total_length_not_including_first_buffer = 0; vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0; /* initially prealloc copy_ops so we can use _vec_len instead of vec_elen */ vec_validate_aligned (ptd->copy_ops, 0, CLIB_CACHE_LINE_BYTES); vec_reset_length (ptd->copy_ops); vec_validate_aligned (ptd->buffers, 0, CLIB_CACHE_LINE_BYTES); vec_reset_length (ptd->buffers); } } pool_get (mm->interfaces, mif); memset (mif, 0, sizeof (*mif)); mif->dev_instance = mif - mm->interfaces; mif->socket_file_index = msf - mm->socket_files; mif->id = args->id; mif->sw_if_index = mif->hw_if_index = mif->per_interface_next_index = ~0; mif->mode = args->mode; if (args->secret) mif->secret = vec_dup (args->secret); if (tm->n_vlib_mains > 1) clib_spinlock_init (&mif->lockp); if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET) { if (!args->hw_addr_set) { f64 now = vlib_time_now (vm); u32 rnd; rnd = (u32) (now * 1e6); rnd = random_u32 (&rnd); memcpy (args->hw_addr + 2, &rnd, sizeof (rnd)); args->hw_addr[0] = 2; args->hw_addr[1] = 0xfe; } error = ethernet_register_interface (vnm, memif_device_class.index, mif->dev_instance, args->hw_addr, &mif->hw_if_index, memif_eth_flag_change); } else if (mif->mode == MEMIF_INTERFACE_MODE_IP) { mif->hw_if_index = vnet_register_interface (vnm, memif_device_class.index, mif->dev_instance, memif_ip_hw_if_class.index, mif->dev_instance); } else error = clib_error_return (0, "unsupported interface mode"); if (error) { ret = VNET_API_ERROR_SYSCALL_ERROR_2; goto error; } sw = vnet_get_hw_sw_interface (vnm, mif->hw_if_index); mif->sw_if_index = sw->sw_if_index; mif->cfg.log2_ring_size = args->log2_ring_size; mif->cfg.buffer_size = args->buffer_size; mif->cfg.num_s2m_rings = args->is_master ? args->rx_queues : args->tx_queues; mif->cfg.num_m2s_rings = args->is_master ? args->tx_queues : args->rx_queues; args->sw_if_index = mif->sw_if_index; /* If this is new one, start listening */ if (msf->is_listener && msf->ref_cnt == 0) { struct stat file_stat; clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t)); ASSERT (msf->sock == 0); msf->sock = s; memset (s, 0, sizeof (clib_socket_t)); s->config = (char *) msf->filename; s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED; if ((error = clib_socket_init (s))) { ret = VNET_API_ERROR_SYSCALL_ERROR_4; goto error; } if (stat ((char *) msf->filename, &file_stat) == -1) { ret = VNET_API_ERROR_SYSCALL_ERROR_8; goto error; } clib_file_t template = { 0 }; template.read_function = memif_conn_fd_accept_ready; template.file_descriptor = msf->sock->fd; template.private_data = mif->socket_file_index; template.description = format (0, "memif listener %s", msf->filename); memif_file_add (&msf->sock->private_data, &template); } msf->ref_cnt++; if (args->is_master == 0) { mif->flags |= MEMIF_IF_FLAG_IS_SLAVE; if (args->is_zero_copy) mif->flags |= MEMIF_IF_FLAG_ZERO_COPY; } hw = vnet_get_hw_interface (vnm, mif->hw_if_index); hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; vnet_hw_interface_set_input_node (vnm, mif->hw_if_index, memif_input_node.index); mhash_set (&msf->dev_instance_by_id, &mif->id, mif->dev_instance, 0); if (pool_elts (mm->interfaces) == 1) { vlib_process_signal_event (vm, memif_process_node.index, MEMIF_PROCESS_EVENT_START, 0); } goto done; error: if (mif->hw_if_index != ~0) { if (mif->mode == MEMIF_INTERFACE_MODE_IP) vnet_delete_hw_interface (vnm, mif->hw_if_index); else ethernet_delete_interface (vnm, mif->hw_if_index); mif->hw_if_index = ~0; } memif_delete_if (vm, mif); if (error) { memif_log_err (mif, "%U", format_clib_error, error); clib_error_free (error); } return ret; done: return rv; } static clib_error_t * memif_init (vlib_main_t * vm) { memif_main_t *mm = &memif_main; memset (mm, 0, sizeof (memif_main_t)); mm->log_class = vlib_log_register_class ("memif_plugin", 0); memif_log_debug (0, "initialized"); /* initialize binary API */ memif_plugin_api_hookup (vm); /* * Pre-stuff socket filename pool with a non-modifieable mapping * for socket-id 0 to MEMIF_DEFAULT_SOCKET_FILENAME in the * default run-time directory. */ memif_socket_filename_add_del (1, 0, (u8 *) MEMIF_DEFAULT_SOCKET_FILENAME); return 0; } VLIB_INIT_FUNCTION (memif_init); /* *INDENT-OFF* */ VLIB_PLUGIN_REGISTER () = { .version = VPP_BUILD_VER, .description = "Packet Memory Interface (experimental)", }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */