a966ffef66888c93b85949de41eb7392b270dbd7
[vpp.git] / vnet / vnet / devices / netmap / device.c
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
18 #include <stdint.h>
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25
26 #include <vnet/devices/netmap/net_netmap.h>
27 #include <vnet/devices/netmap/netmap.h>
28
29 #define foreach_netmap_tx_func_error           \
30 _(NO_FREE_SLOTS, "no free tx slots")           \
31 _(PENDING_MSGS, "pending msgs in tx ring")
32
33 typedef enum {
34 #define _(f,s) NETMAP_TX_ERROR_##f,
35   foreach_netmap_tx_func_error
36 #undef _
37   NETMAP_TX_N_ERROR,
38 } netmap_tx_func_error_t;
39
40 static char * netmap_tx_func_error_strings[] = {
41 #define _(n,s) s,
42     foreach_netmap_tx_func_error
43 #undef _
44 };
45
46
47 static u8 * format_netmap_device_name (u8 * s, va_list * args)
48 {
49   u32 i = va_arg (*args, u32);
50   netmap_main_t * apm = &netmap_main;
51   netmap_if_t * nif = pool_elt_at_index (apm->interfaces, i);
52
53   s = format (s, "netmap-%s", nif->host_if_name);
54   return s;
55 }
56
57 static u8 * format_netmap_device (u8 * s, va_list * args)
58 {
59   u32 dev_instance = va_arg (*args, u32);
60   int verbose = va_arg (*args, int);
61   netmap_main_t * nm = &netmap_main;
62   netmap_if_t * nif = vec_elt_at_index (nm->interfaces, dev_instance);
63   uword indent = format_get_indent (s);
64
65   s = format (s, "NETMAP interface");
66   if (verbose)
67     {
68       s = format (s, "\n%U version %d flags 0x%x"
69                   "\n%U region %u memsize 0x%x offset 0x%x"
70                   "\n%U tx_slots %u rx_slots %u tx_rings %u rx_rings %u",
71                   format_white_space, indent + 2,
72                   nif->req->nr_version,
73                   nif->req->nr_flags,
74                   format_white_space, indent + 2,
75                   nif->mem_region,
76                   nif->req->nr_memsize,
77                   nif->req->nr_offset,
78                   format_white_space, indent + 2,
79                   nif->req->nr_tx_slots,
80                   nif->req->nr_rx_slots,
81                   nif->req->nr_tx_rings,
82                   nif->req->nr_rx_rings);
83     }
84   return s;
85 }
86
87 static u8 * format_netmap_tx_trace (u8 * s, va_list * args)
88 {
89   s = format (s, "Unimplemented...");
90   return s;
91 }
92
93 static uword
94 netmap_interface_tx (vlib_main_t * vm,
95                        vlib_node_runtime_t * node,
96                        vlib_frame_t * frame)
97 {
98   netmap_main_t * nm = &netmap_main;
99   u32 * buffers = vlib_frame_args (frame);
100   u32 n_left = frame->n_vectors;
101   f64 const time_constant = 1e3;
102   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
103   netmap_if_t * nif = pool_elt_at_index (nm->interfaces, rd->dev_instance);
104   int cur_ring;
105
106   cur_ring = nif->first_tx_ring;
107
108   while(n_left && cur_ring <= nif->last_tx_ring)
109     {
110       struct netmap_ring * ring = NETMAP_TXRING(nif->nifp, cur_ring);
111       int n_free_slots = nm_ring_space(ring);
112       uint cur = ring->cur;
113
114       if (nm_tx_pending(ring))
115         {
116          ioctl(nif->fd, NIOCTXSYNC, NULL);
117          clib_cpu_time_wait(time_constant);
118
119          if (nm_tx_pending(ring) && !n_free_slots)
120            {
121              cur_ring++;
122              continue;
123            }
124         }
125
126       while (n_left && n_free_slots)
127         {
128           vlib_buffer_t * b0 = 0;
129           u32 bi = buffers[0];
130           u32 len;
131           u32 offset = 0;
132           buffers++;
133
134           struct netmap_slot * slot = &ring->slot[cur];
135
136           do
137             {
138               b0 = vlib_get_buffer (vm, bi);
139               len = b0->current_length;
140               /* memcpy */
141               clib_memcpy ((u8 *) NETMAP_BUF(ring, slot->buf_idx) + offset,
142                       vlib_buffer_get_current(b0), len);
143               offset += len;
144             }
145           while ((bi = b0->next_buffer));
146
147           slot->len = offset;
148           cur = (cur + 1) % ring->num_slots;
149           n_free_slots--;
150           n_left--;
151         }
152       CLIB_MEMORY_BARRIER();
153       ring->head = ring->cur = cur;
154     }
155
156   if (n_left < frame->n_vectors)
157       ioctl(nif->fd, NIOCTXSYNC, NULL);
158
159   if (n_left)
160     vlib_error_count (vm, node->node_index,
161     (n_left == frame->n_vectors ? NETMAP_TX_ERROR_PENDING_MSGS : NETMAP_TX_ERROR_NO_FREE_SLOTS), n_left);
162
163   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
164   return frame->n_vectors;
165 }
166
167 static void
168 netmap_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
169                                   u32 node_index)
170 {
171   netmap_main_t * apm = &netmap_main;
172   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
173   netmap_if_t * nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
174
175   /* Shut off redirection */
176   if (node_index == ~0)
177     {
178       nif->per_interface_next_index = node_index;
179       return;
180     }
181
182   nif->per_interface_next_index =
183     vlib_node_add_next (vlib_get_main(), netmap_input_node.index, node_index);
184 }
185
186 static void netmap_clear_hw_interface_counters (u32 instance)
187 {
188   /* Nothing for now */
189 }
190
191 static clib_error_t *
192 netmap_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
193 {
194   netmap_main_t * apm = &netmap_main;
195   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
196   netmap_if_t * nif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
197   u32 hw_flags;
198
199   nif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
200
201   if (nif->is_admin_up)
202     hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
203   else
204     hw_flags = 0;
205
206   vnet_hw_interface_set_flags(vnm, hw_if_index, hw_flags);
207
208   return 0;
209 }
210
211 static clib_error_t *
212 netmap_subif_add_del_function (vnet_main_t * vnm,
213                                  u32 hw_if_index,
214                                  struct vnet_sw_interface_t * st,
215                                  int is_add)
216 {
217   /* Nothing for now */
218   return 0;
219 }
220
221 VNET_DEVICE_CLASS (netmap_device_class) = {
222   .name = "netmap",
223   .tx_function = netmap_interface_tx,
224   .format_device_name = format_netmap_device_name,
225   .format_device = format_netmap_device,
226   .format_tx_trace = format_netmap_tx_trace,
227   .tx_function_n_errors = NETMAP_TX_N_ERROR,
228   .tx_function_error_strings = netmap_tx_func_error_strings,
229   .rx_redirect_to_node = netmap_set_interface_next_node,
230   .clear_counters = netmap_clear_hw_interface_counters,
231   .admin_up_down_function = netmap_interface_admin_up_down,
232   .subif_add_del_function = netmap_subif_add_del_function,
233   .no_flatten_output_chains = 1,
234 };
235
236 VLIB_DEVICE_TX_FUNCTION_MULTIARCH(netmap_device_class,
237                                   netmap_interface_tx)