Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #include "ssvm_eth.h" |
| 16 | |
| 17 | ssvm_eth_main_t ssvm_eth_main; |
| 18 | |
| 19 | #define foreach_ssvm_eth_tx_func_error \ |
| 20 | _(RING_FULL, "Tx packet drops (ring full)") \ |
| 21 | _(NO_BUFFERS, "Tx packet drops (no buffers)") \ |
| 22 | _(ADMIN_DOWN, "Tx packet drops (admin down)") |
| 23 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 24 | typedef enum |
| 25 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | #define _(f,s) SSVM_ETH_TX_ERROR_##f, |
| 27 | foreach_ssvm_eth_tx_func_error |
| 28 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 29 | SSVM_ETH_TX_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | } ssvm_eth_tx_func_error_t; |
| 31 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 32 | static u32 ssvm_eth_flag_change (vnet_main_t * vnm, |
| 33 | vnet_hw_interface_t * hi, u32 flags); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 35 | int |
| 36 | ssvm_eth_create (ssvm_eth_main_t * em, u8 * name, int is_master) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 38 | ssvm_private_t *intfc; |
| 39 | void *oldheap; |
| 40 | clib_error_t *e; |
| 41 | unix_shared_memory_queue_t *q; |
| 42 | ssvm_shared_header_t *sh; |
| 43 | ssvm_eth_queue_elt_t *elts; |
| 44 | u32 *elt_indices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | u8 enet_addr[6]; |
| 46 | int i, rv; |
| 47 | |
| 48 | vec_add2 (em->intfcs, intfc, 1); |
| 49 | |
| 50 | intfc->ssvm_size = em->segment_size; |
| 51 | intfc->i_am_master = 1; |
| 52 | intfc->name = name; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 53 | intfc->my_pid = getpid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | if (is_master == 0) |
| 55 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 56 | rv = ssvm_slave_init (intfc, 20 /* timeout in seconds */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | if (rv < 0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 58 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | goto create_vnet_interface; |
| 60 | } |
| 61 | |
| 62 | intfc->requested_va = em->next_base_va; |
| 63 | em->next_base_va += em->segment_size; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 64 | rv = ssvm_master_init (intfc, intfc - em->intfcs /* master index */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
| 66 | if (rv < 0) |
| 67 | return rv; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 68 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | /* OK, segment created, set up queues and so forth. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 70 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | sh = intfc->sh; |
| 72 | oldheap = ssvm_push_heap (sh); |
| 73 | |
| 74 | q = unix_shared_memory_queue_init (em->queue_elts, sizeof (u32), |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 75 | 0 /* consumer pid not interesting */ , |
| 76 | 0 /* signal not sent */ ); |
| 77 | sh->opaque[TO_MASTER_Q_INDEX] = (void *) q; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | q = unix_shared_memory_queue_init (em->queue_elts, sizeof (u32), |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 79 | 0 /* consumer pid not interesting */ , |
| 80 | 0 /* signal not sent */ ); |
| 81 | sh->opaque[TO_SLAVE_Q_INDEX] = (void *) q; |
| 82 | |
| 83 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | * Preallocate the requested number of buffer chunks |
| 85 | * There must be a better way to do this, etc. |
| 86 | * Add some slop to avoid pool reallocation, which will not go well |
| 87 | */ |
| 88 | elts = 0; |
| 89 | elt_indices = 0; |
| 90 | |
| 91 | vec_validate_aligned (elts, em->nbuffers - 1, CLIB_CACHE_LINE_BYTES); |
| 92 | vec_validate_aligned (elt_indices, em->nbuffers - 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 93 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | for (i = 0; i < em->nbuffers; i++) |
| 95 | elt_indices[i] = i; |
| 96 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 97 | sh->opaque[CHUNK_POOL_INDEX] = (void *) elts; |
| 98 | sh->opaque[CHUNK_POOL_FREELIST_INDEX] = (void *) elt_indices; |
| 99 | sh->opaque[CHUNK_POOL_NFREE] = (void *) (uword) em->nbuffers; |
| 100 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | ssvm_pop_heap (oldheap); |
| 102 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 103 | create_vnet_interface: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
| 105 | sh = intfc->sh; |
| 106 | |
| 107 | memset (enet_addr, 0, sizeof (enet_addr)); |
| 108 | enet_addr[0] = 2; |
| 109 | enet_addr[1] = 0xFE; |
| 110 | enet_addr[2] = is_master; |
| 111 | enet_addr[5] = sh->master_index; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 112 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | e = ethernet_register_interface |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 114 | (em->vnet_main, ssvm_eth_device_class.index, intfc - em->intfcs, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | /* ethernet address */ enet_addr, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 116 | &intfc->vlib_hw_if_index, ssvm_eth_flag_change); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
| 118 | if (e) |
| 119 | { |
| 120 | clib_error_report (e); |
| 121 | /* $$$$ unmap offending region? */ |
| 122 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 123 | } |
| 124 | |
| 125 | /* Declare link up */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 126 | vnet_hw_interface_set_flags (em->vnet_main, intfc->vlib_hw_if_index, |
| 127 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
| 129 | /* Let the games begin... */ |
| 130 | if (is_master) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 131 | sh->ready = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static clib_error_t * |
| 136 | ssvm_config (vlib_main_t * vm, unformat_input_t * input) |
| 137 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 138 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | int is_master = 1; |
| 140 | int i, rv; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 141 | ssvm_eth_main_t *em = &ssvm_eth_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 143 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | { |
| 145 | if (unformat (input, "base-va %llx", &em->next_base_va)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 146 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | else if (unformat (input, "segment-size %lld", &em->segment_size)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 148 | em->segment_size = 1ULL << (max_log2 (em->segment_size)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | else if (unformat (input, "nbuffers %lld", &em->nbuffers)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 150 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | else if (unformat (input, "queue-elts %lld", &em->queue_elts)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 152 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | else if (unformat (input, "slave")) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 154 | is_master = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | else if (unformat (input, "%s", &name)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 156 | vec_add1 (em->names, name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 158 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* No configured instances, we're done... */ |
| 162 | if (vec_len (em->names) == 0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 163 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
| 165 | for (i = 0; i < vec_len (em->names); i++) |
| 166 | { |
| 167 | rv = ssvm_eth_create (em, em->names[i], is_master); |
| 168 | if (rv < 0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 169 | return clib_error_return (0, "ssvm_eth_create '%s' failed, error %d", |
| 170 | em->names[i], rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 173 | vlib_node_set_state (vm, ssvm_eth_input_node.index, |
| 174 | VLIB_NODE_STATE_POLLING); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | VLIB_CONFIG_FUNCTION (ssvm_config, "ssvm_eth"); |
| 180 | |
| 181 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 182 | static clib_error_t * |
| 183 | ssvm_eth_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 185 | ssvm_eth_main_t *em = &ssvm_eth_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 187 | if (((sizeof (ssvm_eth_queue_elt_t) / CLIB_CACHE_LINE_BYTES) |
| 188 | * CLIB_CACHE_LINE_BYTES) != sizeof (ssvm_eth_queue_elt_t)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | clib_warning ("ssvm_eth_queue_elt_t size %d not a multiple of %d", |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 190 | sizeof (ssvm_eth_queue_elt_t), CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
| 192 | em->vlib_main = vm; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 193 | em->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | em->elog_main = &vm->elog_main; |
| 195 | |
| 196 | /* default config param values... */ |
| 197 | |
| 198 | em->next_base_va = 0x600000000ULL; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 199 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | * Allocate 2 full superframes in each dir (256 x 2 x 2 x 2048 bytes), |
| 201 | * 2mb; double that so we have plenty of space... 4mb |
| 202 | */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 203 | em->segment_size = 8 << 20; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | em->nbuffers = 1024; |
| 205 | em->queue_elts = 512; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | VLIB_INIT_FUNCTION (ssvm_eth_init); |
| 210 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 211 | static char *ssvm_eth_tx_func_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | #define _(n,s) s, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 213 | foreach_ssvm_eth_tx_func_error |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | #undef _ |
| 215 | }; |
| 216 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 217 | static u8 * |
| 218 | format_ssvm_eth_device_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | { |
| 220 | u32 i = va_arg (*args, u32); |
| 221 | |
| 222 | s = format (s, "ssvmEthernet%d", i); |
| 223 | return s; |
| 224 | } |
| 225 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 226 | static u8 * |
| 227 | format_ssvm_eth_device (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | { |
| 229 | s = format (s, "SSVM Ethernet"); |
| 230 | return s; |
| 231 | } |
| 232 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 233 | static u8 * |
| 234 | format_ssvm_eth_tx_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | { |
| 236 | s = format (s, "Unimplemented..."); |
| 237 | return s; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | static uword |
| 242 | ssvm_eth_interface_tx (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 243 | vlib_node_runtime_t * node, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 245 | ssvm_eth_main_t *em = &ssvm_eth_main; |
| 246 | vnet_interface_output_runtime_t *rd = (void *) node->runtime_data; |
| 247 | ssvm_private_t *intfc = vec_elt_at_index (em->intfcs, rd->dev_instance); |
| 248 | ssvm_shared_header_t *sh = intfc->sh; |
| 249 | unix_shared_memory_queue_t *q; |
| 250 | u32 *from; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | u32 n_left; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 252 | ssvm_eth_queue_elt_t *elts, *elt, *prev_elt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | u32 my_pid = intfc->my_pid; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 254 | vlib_buffer_t *b0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | u32 bi0; |
| 256 | u32 size_this_buffer; |
| 257 | u32 chunks_this_buffer; |
| 258 | u8 i_am_master = intfc->i_am_master; |
| 259 | u32 elt_index; |
| 260 | int is_ring_full, interface_down; |
| 261 | int i; |
| 262 | volatile u32 *queue_lock; |
| 263 | u32 n_to_alloc = VLIB_FRAME_SIZE; |
| 264 | u32 n_allocated, n_present_in_cache, n_available; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 265 | u32 *elt_indices; |
| 266 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | if (i_am_master) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 268 | q = (unix_shared_memory_queue_t *) sh->opaque[TO_SLAVE_Q_INDEX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 270 | q = (unix_shared_memory_queue_t *) sh->opaque[TO_MASTER_Q_INDEX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
| 272 | queue_lock = (u32 *) q; |
| 273 | |
| 274 | from = vlib_frame_vector_args (f); |
| 275 | n_left = f->n_vectors; |
| 276 | is_ring_full = 0; |
| 277 | interface_down = 0; |
| 278 | |
| 279 | n_present_in_cache = vec_len (em->chunk_cache); |
| 280 | |
| 281 | /* admin / link up/down check */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 282 | if (sh->opaque[MASTER_ADMIN_STATE_INDEX] == 0 || |
| 283 | sh->opaque[SLAVE_ADMIN_STATE_INDEX] == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | { |
| 285 | interface_down = 1; |
| 286 | goto out; |
| 287 | } |
| 288 | |
| 289 | ssvm_lock (sh, my_pid, 1); |
| 290 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 291 | elts = (ssvm_eth_queue_elt_t *) (sh->opaque[CHUNK_POOL_INDEX]); |
| 292 | elt_indices = (u32 *) (sh->opaque[CHUNK_POOL_FREELIST_INDEX]); |
| 293 | n_available = (u32) pointer_to_uword (sh->opaque[CHUNK_POOL_NFREE]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 295 | if (n_present_in_cache < n_left * 2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 297 | vec_validate (em->chunk_cache, n_to_alloc + n_present_in_cache - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | |
| 299 | n_allocated = n_to_alloc < n_available ? n_to_alloc : n_available; |
| 300 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 301 | if (PREDICT_TRUE (n_allocated > 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | { |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 303 | clib_memcpy (&em->chunk_cache[n_present_in_cache], |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 304 | &elt_indices[n_available - n_allocated], |
| 305 | sizeof (u32) * n_allocated); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | n_present_in_cache += n_allocated; |
| 309 | n_available -= n_allocated; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 310 | sh->opaque[CHUNK_POOL_NFREE] = uword_to_pointer (n_available, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | _vec_len (em->chunk_cache) = n_present_in_cache; |
| 312 | } |
| 313 | |
| 314 | ssvm_unlock (sh); |
| 315 | |
| 316 | while (n_left) |
| 317 | { |
| 318 | bi0 = from[0]; |
| 319 | b0 = vlib_get_buffer (vm, bi0); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 320 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | size_this_buffer = vlib_buffer_length_in_chain (vm, b0); |
| 322 | chunks_this_buffer = (size_this_buffer + (SSVM_BUFFER_SIZE - 1)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 323 | / SSVM_BUFFER_SIZE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
| 325 | /* If we're not going to be able to enqueue the buffer, tail drop. */ |
| 326 | if (q->cursize >= q->maxsize) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 327 | { |
| 328 | is_ring_full = 1; |
| 329 | break; |
| 330 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | |
| 332 | prev_elt = 0; |
| 333 | elt_index = ~0; |
| 334 | for (i = 0; i < chunks_this_buffer; i++) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 335 | { |
| 336 | if (PREDICT_FALSE (n_present_in_cache == 0)) |
| 337 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 339 | elt_index = em->chunk_cache[--n_present_in_cache]; |
| 340 | elt = elts + elt_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 342 | elt->type = SSVM_PACKET_TYPE; |
| 343 | elt->flags = 0; |
| 344 | elt->total_length_not_including_first_buffer = |
| 345 | b0->total_length_not_including_first_buffer; |
| 346 | elt->length_this_buffer = b0->current_length; |
| 347 | elt->current_data_hint = b0->current_data; |
| 348 | elt->owner = !i_am_master; |
| 349 | elt->tag = 1; |
| 350 | |
| 351 | clib_memcpy (elt->data, b0->data + b0->current_data, |
| 352 | b0->current_length); |
| 353 | |
| 354 | if (PREDICT_FALSE (prev_elt != 0)) |
| 355 | prev_elt->next_index = elt - elts; |
| 356 | |
| 357 | if (PREDICT_FALSE (i < (chunks_this_buffer - 1))) |
| 358 | { |
| 359 | elt->flags = SSVM_BUFFER_NEXT_PRESENT; |
| 360 | ASSERT (b0->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 361 | b0 = vlib_get_buffer (vm, b0->next_buffer); |
| 362 | } |
| 363 | prev_elt = elt; |
| 364 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | |
| 366 | while (__sync_lock_test_and_set (queue_lock, 1)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 367 | ; |
| 368 | |
| 369 | unix_shared_memory_queue_add_raw (q, (u8 *) & elt_index); |
| 370 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 371 | *queue_lock = 0; |
| 372 | |
| 373 | from++; |
| 374 | n_left--; |
| 375 | } |
| 376 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 377 | out: |
| 378 | if (PREDICT_FALSE (n_left)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | { |
| 380 | if (is_ring_full) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 381 | vlib_error_count (vm, node->node_index, SSVM_ETH_TX_ERROR_RING_FULL, |
| 382 | n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | else if (interface_down) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 384 | vlib_error_count (vm, node->node_index, SSVM_ETH_TX_ERROR_ADMIN_DOWN, |
| 385 | n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 387 | vlib_error_count (vm, node->node_index, SSVM_ETH_TX_ERROR_NO_BUFFERS, |
| 388 | n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | |
| 390 | vlib_buffer_free (vm, from, n_left); |
| 391 | } |
| 392 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 393 | vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 395 | if (PREDICT_TRUE (vec_len (em->chunk_cache))) |
| 396 | _vec_len (em->chunk_cache) = n_present_in_cache; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | |
| 398 | return f->n_vectors; |
| 399 | } |
| 400 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 401 | static void |
| 402 | ssvm_eth_clear_hw_interface_counters (u32 instance) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | { |
| 404 | /* Nothing for now */ |
| 405 | } |
| 406 | |
| 407 | static clib_error_t * |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 408 | ssvm_eth_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, |
| 409 | u32 flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 411 | vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 413 | ssvm_eth_main_t *em = &ssvm_eth_main; |
| 414 | ssvm_private_t *intfc = vec_elt_at_index (em->intfcs, hif->dev_instance); |
| 415 | ssvm_shared_header_t *sh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | |
| 417 | /* publish link-state in shared-memory, to discourage buffer-wasting */ |
| 418 | sh = intfc->sh; |
| 419 | if (intfc->i_am_master) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 420 | sh->opaque[MASTER_ADMIN_STATE_INDEX] = (void *) is_up; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 422 | sh->opaque[SLAVE_ADMIN_STATE_INDEX] = (void *) is_up; |
| 423 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static clib_error_t * |
| 428 | ssvm_eth_subif_add_del_function (vnet_main_t * vnm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 429 | u32 hw_if_index, |
| 430 | struct vnet_sw_interface_t *st, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | { |
| 432 | /* Nothing for now */ |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Dynamically redirect all pkts from a specific interface |
| 438 | * to the specified node |
| 439 | */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 440 | static void |
| 441 | ssvm_eth_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index, |
| 442 | u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 444 | ssvm_eth_main_t *em = &ssvm_eth_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 446 | ssvm_private_t *intfc = pool_elt_at_index (em->intfcs, hw->dev_instance); |
| 447 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | /* Shut off redirection */ |
| 449 | if (node_index == ~0) |
| 450 | { |
| 451 | intfc->per_interface_next_index = node_index; |
| 452 | return; |
| 453 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 454 | |
| 455 | intfc->per_interface_next_index = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 456 | vlib_node_add_next (em->vlib_main, ssvm_eth_input_node.index, node_index); |
| 457 | } |
| 458 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 459 | static u32 |
| 460 | ssvm_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 462 | /* nothing for now */ |
| 463 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 466 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | VNET_DEVICE_CLASS (ssvm_eth_device_class) = { |
| 468 | .name = "ssvm-eth", |
| 469 | .tx_function = ssvm_eth_interface_tx, |
| 470 | .tx_function_n_errors = SSVM_ETH_TX_N_ERROR, |
| 471 | .tx_function_error_strings = ssvm_eth_tx_func_error_strings, |
| 472 | .format_device_name = format_ssvm_eth_device_name, |
| 473 | .format_device = format_ssvm_eth_device, |
| 474 | .format_tx_trace = format_ssvm_eth_tx_trace, |
| 475 | .clear_counters = ssvm_eth_clear_hw_interface_counters, |
| 476 | .admin_up_down_function = ssvm_eth_interface_admin_up_down, |
| 477 | .subif_add_del_function = ssvm_eth_subif_add_del_function, |
| 478 | .rx_redirect_to_node = ssvm_eth_set_interface_next_node, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 480 | |
| 481 | VLIB_DEVICE_TX_FUNCTION_MULTIARCH (ssvm_eth_device_class, |
| 482 | ssvm_eth_interface_tx) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 483 | /* *INDENT-ON* */ |
| 484 | |
| 485 | /* |
| 486 | * fd.io coding-style-patch-verification: ON |
| 487 | * |
| 488 | * Local Variables: |
| 489 | * eval: (c-set-style "gnu") |
| 490 | * End: |
| 491 | */ |