Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
Matus Fabian | 82e29c4 | 2016-05-11 04:49:46 -0700 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <net/if.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 22 | #include <vlib/vlib.h> |
| 23 | #include <vlib/unix/unix.h> |
| 24 | #include <vnet/ethernet/ethernet.h> |
| 25 | |
Matus Fabian | 82e29c4 | 2016-05-11 04:49:46 -0700 | [diff] [blame] | 26 | #include <vnet/devices/netmap/net_netmap.h> |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 27 | #include <vnet/devices/netmap/netmap.h> |
| 28 | |
| 29 | #define foreach_netmap_tx_func_error \ |
Mohsin Kazmi | 8f15e92 | 2016-05-02 14:04:57 +0200 | [diff] [blame] | 30 | _(NO_FREE_SLOTS, "no free tx slots") \ |
| 31 | _(PENDING_MSGS, "pending msgs in tx ring") |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 32 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 33 | typedef enum |
| 34 | { |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 35 | #define _(f,s) NETMAP_TX_ERROR_##f, |
| 36 | foreach_netmap_tx_func_error |
| 37 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 38 | NETMAP_TX_N_ERROR, |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 39 | } netmap_tx_func_error_t; |
| 40 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 41 | static char *netmap_tx_func_error_strings[] = { |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 42 | #define _(n,s) s, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 43 | foreach_netmap_tx_func_error |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 44 | #undef _ |
| 45 | }; |
| 46 | |
| 47 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 48 | static u8 * |
| 49 | format_netmap_device_name (u8 * s, va_list * args) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 50 | { |
| 51 | u32 i = va_arg (*args, u32); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 52 | netmap_main_t *apm = &netmap_main; |
| 53 | netmap_if_t *nif = pool_elt_at_index (apm->interfaces, i); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 54 | |
| 55 | s = format (s, "netmap-%s", nif->host_if_name); |
| 56 | return s; |
| 57 | } |
| 58 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 59 | static u8 * |
| 60 | format_netmap_device (u8 * s, va_list * args) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 61 | { |
| 62 | u32 dev_instance = va_arg (*args, u32); |
| 63 | int verbose = va_arg (*args, int); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 64 | netmap_main_t *nm = &netmap_main; |
| 65 | netmap_if_t *nif = vec_elt_at_index (nm->interfaces, dev_instance); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 66 | uword indent = format_get_indent (s); |
| 67 | |
| 68 | s = format (s, "NETMAP interface"); |
| 69 | if (verbose) |
| 70 | { |
| 71 | s = format (s, "\n%U version %d flags 0x%x" |
| 72 | "\n%U region %u memsize 0x%x offset 0x%x" |
| 73 | "\n%U tx_slots %u rx_slots %u tx_rings %u rx_rings %u", |
| 74 | format_white_space, indent + 2, |
| 75 | nif->req->nr_version, |
| 76 | nif->req->nr_flags, |
| 77 | format_white_space, indent + 2, |
| 78 | nif->mem_region, |
| 79 | nif->req->nr_memsize, |
| 80 | nif->req->nr_offset, |
| 81 | format_white_space, indent + 2, |
| 82 | nif->req->nr_tx_slots, |
| 83 | nif->req->nr_rx_slots, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 84 | nif->req->nr_tx_rings, nif->req->nr_rx_rings); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 85 | } |
| 86 | return s; |
| 87 | } |
| 88 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 89 | static u8 * |
| 90 | format_netmap_tx_trace (u8 * s, va_list * args) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 91 | { |
| 92 | s = format (s, "Unimplemented..."); |
| 93 | return s; |
| 94 | } |
| 95 | |
| 96 | static uword |
| 97 | netmap_interface_tx (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 98 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 99 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 100 | netmap_main_t *nm = &netmap_main; |
| 101 | u32 *buffers = vlib_frame_args (frame); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 102 | u32 n_left = frame->n_vectors; |
Mohsin Kazmi | 8f15e92 | 2016-05-02 14:04:57 +0200 | [diff] [blame] | 103 | f64 const time_constant = 1e3; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 104 | vnet_interface_output_runtime_t *rd = (void *) node->runtime_data; |
| 105 | netmap_if_t *nif = pool_elt_at_index (nm->interfaces, rd->dev_instance); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 106 | int cur_ring; |
| 107 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 108 | if (PREDICT_FALSE (nif->lockp != 0)) |
Mohsin KAZMI | a7575ea | 2016-06-16 06:58:34 +0200 | [diff] [blame] | 109 | { |
| 110 | while (__sync_lock_test_and_set (nif->lockp, 1)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 111 | ; |
Mohsin KAZMI | a7575ea | 2016-06-16 06:58:34 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 114 | cur_ring = nif->first_tx_ring; |
| 115 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 116 | while (n_left && cur_ring <= nif->last_tx_ring) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 117 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 118 | struct netmap_ring *ring = NETMAP_TXRING (nif->nifp, cur_ring); |
| 119 | int n_free_slots = nm_ring_space (ring); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 120 | uint cur = ring->cur; |
| 121 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 122 | if (nm_tx_pending (ring)) |
| 123 | { |
| 124 | if (ioctl (nif->fd, NIOCTXSYNC, NULL) < 0) |
| 125 | clib_unix_warning ("NIOCTXSYNC"); |
| 126 | clib_cpu_time_wait (time_constant); |
| 127 | |
| 128 | if (nm_tx_pending (ring) && !n_free_slots) |
| 129 | { |
| 130 | cur_ring++; |
| 131 | continue; |
| 132 | } |
| 133 | } |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 134 | |
| 135 | while (n_left && n_free_slots) |
| 136 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 137 | vlib_buffer_t *b0 = 0; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 138 | u32 bi = buffers[0]; |
| 139 | u32 len; |
| 140 | u32 offset = 0; |
| 141 | buffers++; |
| 142 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 143 | struct netmap_slot *slot = &ring->slot[cur]; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 144 | |
| 145 | do |
| 146 | { |
| 147 | b0 = vlib_get_buffer (vm, bi); |
| 148 | len = b0->current_length; |
| 149 | /* memcpy */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 150 | clib_memcpy ((u8 *) NETMAP_BUF (ring, slot->buf_idx) + offset, |
| 151 | vlib_buffer_get_current (b0), len); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 152 | offset += len; |
| 153 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 154 | while ((bi = b0->next_buffer)); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 155 | |
| 156 | slot->len = offset; |
| 157 | cur = (cur + 1) % ring->num_slots; |
| 158 | n_free_slots--; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 159 | n_left--; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 160 | } |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 161 | CLIB_MEMORY_BARRIER (); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 162 | ring->head = ring->cur = cur; |
| 163 | } |
| 164 | |
| 165 | if (n_left < frame->n_vectors) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 166 | ioctl (nif->fd, NIOCTXSYNC, NULL); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 167 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 168 | if (PREDICT_FALSE (nif->lockp != 0)) |
| 169 | *nif->lockp = 0; |
Mohsin KAZMI | a7575ea | 2016-06-16 06:58:34 +0200 | [diff] [blame] | 170 | |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 171 | if (n_left) |
Mohsin Kazmi | 8f15e92 | 2016-05-02 14:04:57 +0200 | [diff] [blame] | 172 | vlib_error_count (vm, node->node_index, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 173 | (n_left == |
| 174 | frame->n_vectors ? NETMAP_TX_ERROR_PENDING_MSGS : |
| 175 | NETMAP_TX_ERROR_NO_FREE_SLOTS), n_left); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 176 | |
| 177 | vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors); |
| 178 | return frame->n_vectors; |
| 179 | } |
| 180 | |
| 181 | static void |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 182 | netmap_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index, |
| 183 | u32 node_index) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 184 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 185 | netmap_main_t *apm = &netmap_main; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 186 | 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] | 187 | netmap_if_t *nif = pool_elt_at_index (apm->interfaces, hw->dev_instance); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 188 | |
| 189 | /* Shut off redirection */ |
| 190 | if (node_index == ~0) |
| 191 | { |
| 192 | nif->per_interface_next_index = node_index; |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | nif->per_interface_next_index = |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 197 | vlib_node_add_next (vlib_get_main (), netmap_input_node.index, |
| 198 | node_index); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 201 | static void |
| 202 | netmap_clear_hw_interface_counters (u32 instance) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 203 | { |
| 204 | /* Nothing for now */ |
| 205 | } |
| 206 | |
| 207 | static clib_error_t * |
| 208 | netmap_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 209 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 210 | netmap_main_t *apm = &netmap_main; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 211 | 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] | 212 | netmap_if_t *nif = pool_elt_at_index (apm->interfaces, hw->dev_instance); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 213 | u32 hw_flags; |
| 214 | |
| 215 | nif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; |
| 216 | |
| 217 | if (nif->is_admin_up) |
| 218 | hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP; |
| 219 | else |
| 220 | hw_flags = 0; |
| 221 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 222 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static clib_error_t * |
| 228 | netmap_subif_add_del_function (vnet_main_t * vnm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 229 | u32 hw_if_index, |
| 230 | struct vnet_sw_interface_t *st, int is_add) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 231 | { |
| 232 | /* Nothing for now */ |
| 233 | return 0; |
| 234 | } |
| 235 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 236 | /* *INDENT-OFF* */ |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 237 | VNET_DEVICE_CLASS (netmap_device_class) = { |
| 238 | .name = "netmap", |
| 239 | .tx_function = netmap_interface_tx, |
| 240 | .format_device_name = format_netmap_device_name, |
| 241 | .format_device = format_netmap_device, |
| 242 | .format_tx_trace = format_netmap_tx_trace, |
| 243 | .tx_function_n_errors = NETMAP_TX_N_ERROR, |
| 244 | .tx_function_error_strings = netmap_tx_func_error_strings, |
| 245 | .rx_redirect_to_node = netmap_set_interface_next_node, |
| 246 | .clear_counters = netmap_clear_hw_interface_counters, |
| 247 | .admin_up_down_function = netmap_interface_admin_up_down, |
| 248 | .subif_add_del_function = netmap_subif_add_del_function, |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 249 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 250 | |
| 251 | VLIB_DEVICE_TX_FUNCTION_MULTIARCH(netmap_device_class, |
| 252 | netmap_interface_tx) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 253 | /* *INDENT-ON* */ |
| 254 | |
| 255 | /* |
| 256 | * fd.io coding-style-patch-verification: ON |
| 257 | * |
| 258 | * Local Variables: |
| 259 | * eval: (c-set-style "gnu") |
| 260 | * End: |
| 261 | */ |