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") \
26 _(PENDING_MSGS, "pending msgs in tx ring")
29 #define _(f,s) NETMAP_TX_ERROR_##f,
30 foreach_netmap_tx_func_error
33 } netmap_tx_func_error_t;
35 static char * netmap_tx_func_error_strings[] = {
37 foreach_netmap_tx_func_error
42 static u8 * format_netmap_device_name (u8 * s, va_list * args)
44 u32 i = va_arg (*args, u32);
45 netmap_main_t * apm = &netmap_main;
46 netmap_if_t * nif = pool_elt_at_index (apm->interfaces, i);
48 s = format (s, "netmap-%s", nif->host_if_name);
52 static u8 * format_netmap_device (u8 * s, va_list * args)
54 u32 dev_instance = va_arg (*args, u32);
55 int verbose = va_arg (*args, int);
56 netmap_main_t * nm = &netmap_main;
57 netmap_if_t * nif = vec_elt_at_index (nm->interfaces, dev_instance);
58 uword indent = format_get_indent (s);
60 s = format (s, "NETMAP interface");
63 s = format (s, "\n%U version %d flags 0x%x"
64 "\n%U region %u memsize 0x%x offset 0x%x"
65 "\n%U tx_slots %u rx_slots %u tx_rings %u rx_rings %u",
66 format_white_space, indent + 2,
69 format_white_space, indent + 2,
73 format_white_space, indent + 2,
74 nif->req->nr_tx_slots,
75 nif->req->nr_rx_slots,
76 nif->req->nr_tx_rings,
77 nif->req->nr_rx_rings);
82 static u8 * format_netmap_tx_trace (u8 * s, va_list * args)
84 s = format (s, "Unimplemented...");
89 netmap_interface_tx (vlib_main_t * vm,
90 vlib_node_runtime_t * node,
93 netmap_main_t * nm = &netmap_main;
94 u32 * buffers = vlib_frame_args (frame);
95 u32 n_left = frame->n_vectors;
96 f64 const time_constant = 1e3;
97 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
98 netmap_if_t * nif = pool_elt_at_index (nm->interfaces, rd->dev_instance);
101 cur_ring = nif->first_tx_ring;
103 while(n_left && cur_ring <= nif->last_tx_ring)
105 struct netmap_ring * ring = NETMAP_TXRING(nif->nifp, cur_ring);
106 int n_free_slots = nm_ring_space(ring);
107 uint cur = ring->cur;
109 if (nm_tx_pending(ring))
111 ioctl(nif->fd, NIOCTXSYNC, NULL);
112 clib_cpu_time_wait(time_constant);
114 if (nm_tx_pending(ring) && !n_free_slots)
121 while (n_left && n_free_slots)
123 vlib_buffer_t * b0 = 0;
129 struct netmap_slot * slot = &ring->slot[cur];
133 b0 = vlib_get_buffer (vm, bi);
134 len = b0->current_length;
136 clib_memcpy ((u8 *) NETMAP_BUF(ring, slot->buf_idx) + offset,
137 vlib_buffer_get_current(b0), len);
140 while ((bi = b0->next_buffer));
143 cur = (cur + 1) % ring->num_slots;
147 CLIB_MEMORY_BARRIER();
148 ring->head = ring->cur = cur;
151 if (n_left < frame->n_vectors)
152 ioctl(nif->fd, NIOCTXSYNC, NULL);
155 vlib_error_count (vm, node->node_index,
156 (n_left == frame->n_vectors ? NETMAP_TX_ERROR_PENDING_MSGS : NETMAP_TX_ERROR_NO_FREE_SLOTS), n_left);
158 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
159 return frame->n_vectors;
163 netmap_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
166 netmap_main_t * apm = &netmap_main;
167 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
168 netmap_if_t * nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
170 /* Shut off redirection */
171 if (node_index == ~0)
173 nif->per_interface_next_index = node_index;
177 nif->per_interface_next_index =
178 vlib_node_add_next (vlib_get_main(), netmap_input_node.index, node_index);
181 static void netmap_clear_hw_interface_counters (u32 instance)
183 /* Nothing for now */
186 static clib_error_t *
187 netmap_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
189 netmap_main_t * apm = &netmap_main;
190 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
191 netmap_if_t * nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
194 nif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
196 if (nif->is_admin_up)
197 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
201 vnet_hw_interface_set_flags(vnm, hw_if_index, hw_flags);
206 static clib_error_t *
207 netmap_subif_add_del_function (vnet_main_t * vnm,
209 struct vnet_sw_interface_t * st,
212 /* Nothing for now */
216 VNET_DEVICE_CLASS (netmap_device_class) = {
218 .tx_function = netmap_interface_tx,
219 .format_device_name = format_netmap_device_name,
220 .format_device = format_netmap_device,
221 .format_tx_trace = format_netmap_tx_trace,
222 .tx_function_n_errors = NETMAP_TX_N_ERROR,
223 .tx_function_error_strings = netmap_tx_func_error_strings,
224 .rx_redirect_to_node = netmap_set_interface_next_node,
225 .clear_counters = netmap_clear_hw_interface_counters,
226 .admin_up_down_function = netmap_interface_admin_up_down,
227 .subif_add_del_function = netmap_subif_add_del_function,
228 .no_flatten_output_chains = 1,