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:
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 *------------------------------------------------------------------
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/netmap/netmap.h>
24 #define foreach_netmap_tx_func_error \
25 _(NO_FREE_SLOTS, "no free tx slots")
28 #define _(f,s) NETMAP_TX_ERROR_##f,
29 foreach_netmap_tx_func_error
32 } netmap_tx_func_error_t;
34 static char * netmap_tx_func_error_strings[] = {
36 foreach_netmap_tx_func_error
41 static u8 * format_netmap_device_name (u8 * s, va_list * args)
43 u32 i = va_arg (*args, u32);
44 netmap_main_t * apm = &netmap_main;
45 netmap_if_t * nif = pool_elt_at_index (apm->interfaces, i);
47 s = format (s, "netmap-%s", nif->host_if_name);
51 static u8 * format_netmap_device (u8 * s, va_list * args)
53 u32 dev_instance = va_arg (*args, u32);
54 int verbose = va_arg (*args, int);
55 netmap_main_t * nm = &netmap_main;
56 netmap_if_t * nif = vec_elt_at_index (nm->interfaces, dev_instance);
57 uword indent = format_get_indent (s);
59 s = format (s, "NETMAP interface");
62 s = format (s, "\n%U version %d flags 0x%x"
63 "\n%U region %u memsize 0x%x offset 0x%x"
64 "\n%U tx_slots %u rx_slots %u tx_rings %u rx_rings %u",
65 format_white_space, indent + 2,
68 format_white_space, indent + 2,
72 format_white_space, indent + 2,
73 nif->req->nr_tx_slots,
74 nif->req->nr_rx_slots,
75 nif->req->nr_tx_rings,
76 nif->req->nr_rx_rings);
81 static u8 * format_netmap_tx_trace (u8 * s, va_list * args)
83 s = format (s, "Unimplemented...");
88 netmap_interface_tx (vlib_main_t * vm,
89 vlib_node_runtime_t * node,
92 netmap_main_t * nm = &netmap_main;
93 u32 * buffers = vlib_frame_args (frame);
94 u32 n_left = frame->n_vectors;
95 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
96 netmap_if_t * nif = pool_elt_at_index (nm->interfaces, rd->dev_instance);
99 cur_ring = nif->first_tx_ring;
101 while(n_left && cur_ring <= nif->last_tx_ring)
103 struct netmap_ring * ring = NETMAP_TXRING(nif->nifp, cur_ring);
104 int n_free_slots = nm_ring_space(ring);
105 uint cur = ring->cur;
113 while (n_left && n_free_slots)
121 struct netmap_slot * slot = &ring->slot[cur];
125 b0 = vlib_get_buffer (vm, bi);
126 len = b0->current_length;
128 clib_memcpy ((u8 *) NETMAP_BUF(ring, slot->buf_idx) + offset,
129 vlib_buffer_get_current(b0), len);
132 while ((bi = b0->next_buffer));
135 cur = (cur + 1) % ring->num_slots;
139 CLIB_MEMORY_BARRIER();
140 ring->head = ring->cur = cur;
143 if (n_left < frame->n_vectors)
144 ioctl(nif->fd, NIOCTXSYNC, NULL);
147 vlib_error_count (vm, node->node_index, NETMAP_TX_ERROR_NO_FREE_SLOTS,
150 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
151 return frame->n_vectors;
155 netmap_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
158 netmap_main_t * apm = &netmap_main;
159 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
160 netmap_if_t * nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
162 /* Shut off redirection */
163 if (node_index == ~0)
165 nif->per_interface_next_index = node_index;
169 nif->per_interface_next_index =
170 vlib_node_add_next (vlib_get_main(), netmap_input_node.index, node_index);
173 static void netmap_clear_hw_interface_counters (u32 instance)
175 /* Nothing for now */
178 static clib_error_t *
179 netmap_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
181 netmap_main_t * apm = &netmap_main;
182 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
183 netmap_if_t * nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
186 nif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
188 if (nif->is_admin_up)
189 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
193 vnet_hw_interface_set_flags(vnm, hw_if_index, hw_flags);
198 static clib_error_t *
199 netmap_subif_add_del_function (vnet_main_t * vnm,
201 struct vnet_sw_interface_t * st,
204 /* Nothing for now */
208 VNET_DEVICE_CLASS (netmap_device_class) = {
210 .tx_function = netmap_interface_tx,
211 .format_device_name = format_netmap_device_name,
212 .format_device = format_netmap_device,
213 .format_tx_trace = format_netmap_tx_trace,
214 .tx_function_n_errors = NETMAP_TX_N_ERROR,
215 .tx_function_error_strings = netmap_tx_func_error_strings,
216 .rx_redirect_to_node = netmap_set_interface_next_node,
217 .clear_counters = netmap_clear_hw_interface_counters,
218 .admin_up_down_function = netmap_interface_admin_up_down,
219 .subif_add_del_function = netmap_subif_add_del_function,
220 .no_flatten_output_chains = 1,