Add native NETMAP driver
[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 <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vnet/ethernet/ethernet.h>
21
22 #include <vnet/devices/netmap/netmap.h>
23
24 #define foreach_netmap_tx_func_error           \
25 _(NO_FREE_SLOTS, "no free tx slots")
26
27 typedef enum {
28 #define _(f,s) NETMAP_TX_ERROR_##f,
29   foreach_netmap_tx_func_error
30 #undef _
31   NETMAP_TX_N_ERROR,
32 } netmap_tx_func_error_t;
33
34 static char * netmap_tx_func_error_strings[] = {
35 #define _(n,s) s,
36     foreach_netmap_tx_func_error
37 #undef _
38 };
39
40
41 static u8 * format_netmap_device_name (u8 * s, va_list * args)
42 {
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);
46
47   s = format (s, "netmap-%s", nif->host_if_name);
48   return s;
49 }
50
51 static u8 * format_netmap_device (u8 * s, va_list * args)
52 {
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);
58
59   s = format (s, "NETMAP interface");
60   if (verbose)
61     {
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,
66                   nif->req->nr_version,
67                   nif->req->nr_flags,
68                   format_white_space, indent + 2,
69                   nif->mem_region,
70                   nif->req->nr_memsize,
71                   nif->req->nr_offset,
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);
77     }
78   return s;
79 }
80
81 static u8 * format_netmap_tx_trace (u8 * s, va_list * args)
82 {
83   s = format (s, "Unimplemented...");
84   return s;
85 }
86
87 static uword
88 netmap_interface_tx (vlib_main_t * vm,
89                        vlib_node_runtime_t * node,
90                        vlib_frame_t * frame)
91 {
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);
97   int cur_ring;
98
99   cur_ring = nif->first_tx_ring;
100
101   while(n_left && cur_ring <= nif->last_tx_ring)
102     {
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;
106
107       if (!n_free_slots)
108         {
109           cur_ring++;
110           continue;
111         }
112
113       while (n_left && n_free_slots)
114         {
115           vlib_buffer_t * b0;
116           u32 bi = buffers[0];
117           u32 len;
118           u32 offset = 0;
119           buffers++;
120
121           struct netmap_slot * slot = &ring->slot[cur];
122
123           do
124             {
125               b0 = vlib_get_buffer (vm, bi);
126               len = b0->current_length;
127               /* memcpy */
128               clib_memcpy ((u8 *) NETMAP_BUF(ring, slot->buf_idx) + offset,
129                       vlib_buffer_get_current(b0), len);
130               offset += len;
131             }
132           while ((bi = b0->next_buffer));
133
134           slot->len = offset;
135           cur = (cur + 1) % ring->num_slots;
136           n_free_slots--;
137           n_left--;
138         }
139       CLIB_MEMORY_BARRIER();
140       ring->head = ring->cur = cur;
141     }
142
143   if (n_left < frame->n_vectors)
144       ioctl(nif->fd, NIOCTXSYNC, NULL);
145
146   if (n_left)
147     vlib_error_count (vm, node->node_index, NETMAP_TX_ERROR_NO_FREE_SLOTS,
148                       n_left);
149
150   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
151   return frame->n_vectors;
152 }
153
154 static void
155 netmap_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
156                                   u32 node_index)
157 {
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);
161
162   /* Shut off redirection */
163   if (node_index == ~0)
164     {
165       nif->per_interface_next_index = node_index;
166       return;
167     }
168
169   nif->per_interface_next_index =
170     vlib_node_add_next (vlib_get_main(), netmap_input_node.index, node_index);
171 }
172
173 static void netmap_clear_hw_interface_counters (u32 instance)
174 {
175   /* Nothing for now */
176 }
177
178 static clib_error_t *
179 netmap_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
180 {
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);
184   u32 hw_flags;
185
186   nif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
187
188   if (nif->is_admin_up)
189     hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
190   else
191     hw_flags = 0;
192
193   vnet_hw_interface_set_flags(vnm, hw_if_index, hw_flags);
194
195   return 0;
196 }
197
198 static clib_error_t *
199 netmap_subif_add_del_function (vnet_main_t * vnm,
200                                  u32 hw_if_index,
201                                  struct vnet_sw_interface_t * st,
202                                  int is_add)
203 {
204   /* Nothing for now */
205   return 0;
206 }
207
208 VNET_DEVICE_CLASS (netmap_device_class) = {
209   .name = "netmap",
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,
221 };