blob: b14bff43c06b5f3f5719ba69f88958e10b209a7b [file] [log] [blame]
Damjan Marion108c7312016-04-20 05:04:20 +02001/*
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 Fabian82e29c42016-05-11 04:49:46 -070018#include <stdint.h>
19#include <net/if.h>
20#include <sys/ioctl.h>
21
Damjan Marion108c7312016-04-20 05:04:20 +020022#include <vlib/vlib.h>
23#include <vlib/unix/unix.h>
24#include <vnet/ethernet/ethernet.h>
25
Matus Fabian82e29c42016-05-11 04:49:46 -070026#include <vnet/devices/netmap/net_netmap.h>
Damjan Marion108c7312016-04-20 05:04:20 +020027#include <vnet/devices/netmap/netmap.h>
28
29#define foreach_netmap_tx_func_error \
Mohsin Kazmi8f15e922016-05-02 14:04:57 +020030_(NO_FREE_SLOTS, "no free tx slots") \
31_(PENDING_MSGS, "pending msgs in tx ring")
Damjan Marion108c7312016-04-20 05:04:20 +020032
Damjan Marion00a9dca2016-08-17 17:05:46 +020033typedef enum
34{
Damjan Marion108c7312016-04-20 05:04:20 +020035#define _(f,s) NETMAP_TX_ERROR_##f,
36 foreach_netmap_tx_func_error
37#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +020038 NETMAP_TX_N_ERROR,
Damjan Marion108c7312016-04-20 05:04:20 +020039} netmap_tx_func_error_t;
40
Damjan Marion00a9dca2016-08-17 17:05:46 +020041static char *netmap_tx_func_error_strings[] = {
Damjan Marion108c7312016-04-20 05:04:20 +020042#define _(n,s) s,
Damjan Marion00a9dca2016-08-17 17:05:46 +020043 foreach_netmap_tx_func_error
Damjan Marion108c7312016-04-20 05:04:20 +020044#undef _
45};
46
47
Damjan Marion00a9dca2016-08-17 17:05:46 +020048static u8 *
49format_netmap_device_name (u8 * s, va_list * args)
Damjan Marion108c7312016-04-20 05:04:20 +020050{
51 u32 i = va_arg (*args, u32);
Damjan Marion00a9dca2016-08-17 17:05:46 +020052 netmap_main_t *apm = &netmap_main;
53 netmap_if_t *nif = pool_elt_at_index (apm->interfaces, i);
Damjan Marion108c7312016-04-20 05:04:20 +020054
55 s = format (s, "netmap-%s", nif->host_if_name);
56 return s;
57}
58
Damjan Marion00a9dca2016-08-17 17:05:46 +020059static u8 *
60format_netmap_device (u8 * s, va_list * args)
Damjan Marion108c7312016-04-20 05:04:20 +020061{
62 u32 dev_instance = va_arg (*args, u32);
63 int verbose = va_arg (*args, int);
Damjan Marion00a9dca2016-08-17 17:05:46 +020064 netmap_main_t *nm = &netmap_main;
65 netmap_if_t *nif = vec_elt_at_index (nm->interfaces, dev_instance);
Christophe Fontained3c008d2017-10-02 18:10:54 +020066 u32 indent = format_get_indent (s);
Damjan Marion108c7312016-04-20 05:04:20 +020067
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 Marion00a9dca2016-08-17 17:05:46 +020084 nif->req->nr_tx_rings, nif->req->nr_rx_rings);
Damjan Marion108c7312016-04-20 05:04:20 +020085 }
86 return s;
87}
88
Damjan Marion00a9dca2016-08-17 17:05:46 +020089static u8 *
90format_netmap_tx_trace (u8 * s, va_list * args)
Damjan Marion108c7312016-04-20 05:04:20 +020091{
92 s = format (s, "Unimplemented...");
93 return s;
94}
95
96static uword
97netmap_interface_tx (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +020098 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marion108c7312016-04-20 05:04:20 +020099{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200100 netmap_main_t *nm = &netmap_main;
Damjan Mariona3d59862018-11-10 10:23:00 +0100101 u32 *buffers = vlib_frame_vector_args (frame);
Damjan Marion108c7312016-04-20 05:04:20 +0200102 u32 n_left = frame->n_vectors;
Mohsin Kazmi8f15e922016-05-02 14:04:57 +0200103 f64 const time_constant = 1e3;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200104 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 Marion108c7312016-04-20 05:04:20 +0200106 int cur_ring;
107
Damjan Marion1927da22017-03-27 17:08:20 +0200108 clib_spinlock_lock_if_init (&nif->lockp);
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200109
Damjan Marion108c7312016-04-20 05:04:20 +0200110 cur_ring = nif->first_tx_ring;
111
Damjan Marion00a9dca2016-08-17 17:05:46 +0200112 while (n_left && cur_ring <= nif->last_tx_ring)
Damjan Marion108c7312016-04-20 05:04:20 +0200113 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200114 struct netmap_ring *ring = NETMAP_TXRING (nif->nifp, cur_ring);
115 int n_free_slots = nm_ring_space (ring);
Damjan Marion108c7312016-04-20 05:04:20 +0200116 uint cur = ring->cur;
117
Damjan Marion00a9dca2016-08-17 17:05:46 +0200118 if (nm_tx_pending (ring))
119 {
120 if (ioctl (nif->fd, NIOCTXSYNC, NULL) < 0)
121 clib_unix_warning ("NIOCTXSYNC");
122 clib_cpu_time_wait (time_constant);
123
124 if (nm_tx_pending (ring) && !n_free_slots)
125 {
126 cur_ring++;
127 continue;
128 }
129 }
Damjan Marion108c7312016-04-20 05:04:20 +0200130
131 while (n_left && n_free_slots)
132 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200133 vlib_buffer_t *b0 = 0;
Damjan Marion108c7312016-04-20 05:04:20 +0200134 u32 bi = buffers[0];
135 u32 len;
136 u32 offset = 0;
137 buffers++;
138
Damjan Marion00a9dca2016-08-17 17:05:46 +0200139 struct netmap_slot *slot = &ring->slot[cur];
Damjan Marion108c7312016-04-20 05:04:20 +0200140
141 do
142 {
143 b0 = vlib_get_buffer (vm, bi);
144 len = b0->current_length;
145 /* memcpy */
Dave Barach178cf492018-11-13 16:34:13 -0500146 clib_memcpy_fast ((u8 *) NETMAP_BUF (ring, slot->buf_idx) +
147 offset, vlib_buffer_get_current (b0), len);
Damjan Marion108c7312016-04-20 05:04:20 +0200148 offset += len;
149 }
Damjan Marion00a9dca2016-08-17 17:05:46 +0200150 while ((bi = b0->next_buffer));
Damjan Marion108c7312016-04-20 05:04:20 +0200151
152 slot->len = offset;
153 cur = (cur + 1) % ring->num_slots;
154 n_free_slots--;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200155 n_left--;
Damjan Marion108c7312016-04-20 05:04:20 +0200156 }
Damjan Marion00a9dca2016-08-17 17:05:46 +0200157 CLIB_MEMORY_BARRIER ();
Damjan Marion108c7312016-04-20 05:04:20 +0200158 ring->head = ring->cur = cur;
159 }
160
161 if (n_left < frame->n_vectors)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200162 ioctl (nif->fd, NIOCTXSYNC, NULL);
Damjan Marion108c7312016-04-20 05:04:20 +0200163
Damjan Marion1927da22017-03-27 17:08:20 +0200164 clib_spinlock_unlock_if_init (&nif->lockp);
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200165
Damjan Marion108c7312016-04-20 05:04:20 +0200166 if (n_left)
Mohsin Kazmi8f15e922016-05-02 14:04:57 +0200167 vlib_error_count (vm, node->node_index,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200168 (n_left ==
169 frame->n_vectors ? NETMAP_TX_ERROR_PENDING_MSGS :
170 NETMAP_TX_ERROR_NO_FREE_SLOTS), n_left);
Damjan Marion108c7312016-04-20 05:04:20 +0200171
Damjan Mariona3d59862018-11-10 10:23:00 +0100172 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
Damjan Marion108c7312016-04-20 05:04:20 +0200173 return frame->n_vectors;
174}
175
176static void
Damjan Marion00a9dca2016-08-17 17:05:46 +0200177netmap_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
178 u32 node_index)
Damjan Marion108c7312016-04-20 05:04:20 +0200179{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200180 netmap_main_t *apm = &netmap_main;
Damjan Marion108c7312016-04-20 05:04:20 +0200181 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200182 netmap_if_t *nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
Damjan Marion108c7312016-04-20 05:04:20 +0200183
184 /* Shut off redirection */
185 if (node_index == ~0)
186 {
187 nif->per_interface_next_index = node_index;
188 return;
189 }
190
191 nif->per_interface_next_index =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200192 vlib_node_add_next (vlib_get_main (), netmap_input_node.index,
193 node_index);
Damjan Marion108c7312016-04-20 05:04:20 +0200194}
195
Damjan Marion00a9dca2016-08-17 17:05:46 +0200196static void
197netmap_clear_hw_interface_counters (u32 instance)
Damjan Marion108c7312016-04-20 05:04:20 +0200198{
199 /* Nothing for now */
200}
201
202static clib_error_t *
203netmap_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
204{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200205 netmap_main_t *apm = &netmap_main;
Damjan Marion108c7312016-04-20 05:04:20 +0200206 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200207 netmap_if_t *nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
Damjan Marion108c7312016-04-20 05:04:20 +0200208 u32 hw_flags;
209
210 nif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
211
212 if (nif->is_admin_up)
213 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
214 else
215 hw_flags = 0;
216
Damjan Marion00a9dca2016-08-17 17:05:46 +0200217 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Damjan Marion108c7312016-04-20 05:04:20 +0200218
219 return 0;
220}
221
222static clib_error_t *
223netmap_subif_add_del_function (vnet_main_t * vnm,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200224 u32 hw_if_index,
225 struct vnet_sw_interface_t *st, int is_add)
Damjan Marion108c7312016-04-20 05:04:20 +0200226{
227 /* Nothing for now */
228 return 0;
229}
230
Damjan Marion00a9dca2016-08-17 17:05:46 +0200231/* *INDENT-OFF* */
Damjan Marion108c7312016-04-20 05:04:20 +0200232VNET_DEVICE_CLASS (netmap_device_class) = {
233 .name = "netmap",
234 .tx_function = netmap_interface_tx,
235 .format_device_name = format_netmap_device_name,
236 .format_device = format_netmap_device,
237 .format_tx_trace = format_netmap_tx_trace,
238 .tx_function_n_errors = NETMAP_TX_N_ERROR,
239 .tx_function_error_strings = netmap_tx_func_error_strings,
240 .rx_redirect_to_node = netmap_set_interface_next_node,
241 .clear_counters = netmap_clear_hw_interface_counters,
242 .admin_up_down_function = netmap_interface_admin_up_down,
243 .subif_add_del_function = netmap_subif_add_del_function,
Damjan Marion108c7312016-04-20 05:04:20 +0200244};
Damjan Marion1c80e832016-05-11 23:07:18 +0200245
246VLIB_DEVICE_TX_FUNCTION_MULTIARCH(netmap_device_class,
247 netmap_interface_tx)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200248/* *INDENT-ON* */
249
250/*
251 * fd.io coding-style-patch-verification: ON
252 *
253 * Local Variables:
254 * eval: (c-set-style "gnu")
255 * End:
256 */