virtio: fast TAP interfaces with vhost-net backend
[vpp.git] / src / vnet / devices / virtio / 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 <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <linux/virtio_net.h>
22 #include <linux/vhost.h>
23
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/devices/virtio/virtio.h>
28
29 #define foreach_virtio_tx_func_error           \
30 _(NO_FREE_SLOTS, "no free tx slots")           \
31 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
32 _(PENDING_MSGS, "pending msgs in tx ring") \
33 _(NO_TX_QUEUES, "no tx queues")
34
35 typedef enum
36 {
37 #define _(f,s) TAP_TX_ERROR_##f,
38   foreach_virtio_tx_func_error
39 #undef _
40     TAP_TX_N_ERROR,
41 } virtio_tx_func_error_t;
42
43 static char *virtio_tx_func_error_strings[] = {
44 #define _(n,s) s,
45   foreach_virtio_tx_func_error
46 #undef _
47 };
48
49 u8 *
50 format_virtio_device_name (u8 * s, va_list * args)
51 {
52   u32 dev_instance = va_arg (*args, u32);
53   virtio_main_t *mm = &virtio_main;
54   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, dev_instance);
55
56   if (vif->type == VIRTIO_IF_TYPE_TAP)
57     {
58       s = format (s, "tap-%s", vif->name);
59     }
60   else
61     s = format (s, "virtio%lu", vif->dev_instance);
62
63   return s;
64 }
65
66 static u8 *
67 format_virtio_device (u8 * s, va_list * args)
68 {
69   u32 dev_instance = va_arg (*args, u32);
70   int verbose = va_arg (*args, int);
71   u32 indent = format_get_indent (s);
72
73   s = format (s, "VIRTIO interface");
74   if (verbose)
75     {
76       s = format (s, "\n%U instance %u", format_white_space, indent + 2,
77                   dev_instance);
78     }
79   return s;
80 }
81
82 static u8 *
83 format_virtio_tx_trace (u8 * s, va_list * args)
84 {
85   s = format (s, "Unimplemented...");
86   return s;
87 }
88
89 static_always_inline void
90 virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring)
91 {
92   u16 used = vring->desc_in_use;
93   u16 sz = vring->size;
94   u16 mask = sz - 1;
95   u16 last = vring->last_used_idx;
96   u16 n_left = vring->used->idx - last;
97
98   if (n_left == 0)
99     return;
100
101   while (n_left)
102     {
103       struct vring_used_elem *e = &vring->used->ring[last & mask];
104       u16 slot = e->id;
105       struct vring_desc *d = &vring->desc[slot];
106
107       if (PREDICT_FALSE (d->flags & VRING_DESC_F_INDIRECT))
108         {
109           d = uword_to_pointer (d->addr, struct vring_desc *);
110           vec_free (d);
111         }
112
113       vlib_buffer_free (vm, &vring->buffers[slot], 1);
114       used--;
115       last++;
116       n_left--;
117     }
118   vring->desc_in_use = used;
119   vring->last_used_idx = last;
120 }
121
122 static_always_inline u16
123 add_buffer_to_slot (vlib_main_t * vm, virtio_vring_t * vring, u32 bi,
124                     u16 avail, u16 next, u16 mask)
125 {
126   u16 n_added = 0;
127   const int hdr_sz = sizeof (struct virtio_net_hdr_v1);
128   struct vring_desc *d;
129   d = &vring->desc[next];
130   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
131
132   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
133     {
134       d->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
135       d->len = b->current_length + hdr_sz;
136       d->flags = 0;
137     }
138   else
139     {
140       struct vring_desc *id, *descs = 0;
141
142       /* first buffer in chain */
143       vec_add2_aligned (descs, id, 1, CLIB_CACHE_LINE_BYTES);
144       id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
145       id->len = b->current_length + hdr_sz;
146
147       while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
148         {
149           id->flags = VRING_DESC_F_NEXT;
150           id->next = vec_len (descs);
151           vec_add2_aligned (descs, id, 1, CLIB_CACHE_LINE_BYTES);
152           b = vlib_get_buffer (vm, b->next_buffer);
153           id->addr = pointer_to_uword (vlib_buffer_get_current (b));
154           id->len = b->current_length;
155         }
156
157       d->addr = pointer_to_uword (descs);
158       d->len = vec_len (descs) * sizeof (struct vring_desc);
159       d->flags = VRING_DESC_F_INDIRECT;
160     }
161   vring->buffers[next] = bi;
162   vring->avail->ring[avail & mask] = next;
163   n_added++;
164   return n_added;
165 }
166
167
168 static_always_inline uword
169 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
170                             vlib_frame_t * frame, virtio_if_t * vif)
171 {
172   u8 qid = 0;
173   u16 n_left = frame->n_vectors;
174   virtio_vring_t *vring = vec_elt_at_index (vif->vrings, (qid << 1) + 1);
175   u16 used, next, avail;
176   u16 sz = vring->size;
177   u16 mask = sz - 1;
178   u32 *buffers = vlib_frame_args (frame);
179
180   /* free consumed buffers */
181   virtio_free_used_desc (vm, vring);
182
183   used = vring->desc_in_use;
184   next = vring->desc_next;
185   avail = vring->avail->idx;
186
187   while (n_left && used < sz)
188     {
189       u16 n_added;
190       n_added = add_buffer_to_slot (vm, vring, buffers[0], avail, next, mask);
191       avail += n_added;
192       next = (next + n_added) & mask;
193       used += n_added;
194       buffers++;
195       n_left--;
196     }
197
198   if (n_left != frame->n_vectors)
199     {
200       CLIB_MEMORY_STORE_BARRIER ();
201       vring->avail->idx = avail;
202       vring->desc_next = next;
203       vring->desc_in_use = used;
204       if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
205         {
206           u64 x = 1;
207           CLIB_UNUSED (int r) = write (vring->kick_fd, &x, sizeof (x));
208         }
209     }
210
211
212   if (n_left)
213     {
214       vlib_error_count (vm, node->node_index, TAP_TX_ERROR_NO_FREE_SLOTS,
215                         n_left);
216       vlib_buffer_free (vm, buffers, n_left);
217     }
218
219   return frame->n_vectors - n_left;
220 }
221
222 static uword
223 virtio_interface_tx (vlib_main_t * vm,
224                      vlib_node_runtime_t * node, vlib_frame_t * frame)
225 {
226   virtio_main_t *nm = &virtio_main;
227   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
228   virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
229
230   return virtio_interface_tx_inline (vm, node, frame, vif);
231 }
232
233 static void
234 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
235                                 u32 node_index)
236 {
237   virtio_main_t *apm = &virtio_main;
238   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
239   virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
240
241   /* Shut off redirection */
242   if (node_index == ~0)
243     {
244       vif->per_interface_next_index = node_index;
245       return;
246     }
247
248   vif->per_interface_next_index =
249     vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
250                         node_index);
251 }
252
253 static void
254 virtio_clear_hw_interface_counters (u32 instance)
255 {
256   /* Nothing for now */
257 }
258
259 static clib_error_t *
260 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
261                                  vnet_hw_interface_rx_mode mode)
262 {
263   virtio_main_t *mm = &virtio_main;
264   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
265   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
266   virtio_vring_t *vring = vec_elt_at_index (vif->vrings, qid);
267
268   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
269     vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
270   else
271     vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
272
273   return 0;
274 }
275
276 static clib_error_t *
277 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
278 {
279   virtio_main_t *mm = &virtio_main;
280   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
281   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
282   static clib_error_t *error = 0;
283
284   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
285     vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
286   else
287     vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
288
289   return error;
290   return 0;
291 }
292
293 static clib_error_t *
294 virtio_subif_add_del_function (vnet_main_t * vnm,
295                                u32 hw_if_index,
296                                struct vnet_sw_interface_t *st, int is_add)
297 {
298   /* Nothing for now */
299   return 0;
300 }
301
302 /* *INDENT-OFF* */
303 VNET_DEVICE_CLASS (virtio_device_class) = {
304   .name = "virtio",
305   .tx_function = virtio_interface_tx,
306   .format_device_name = format_virtio_device_name,
307   .format_device = format_virtio_device,
308   .format_tx_trace = format_virtio_tx_trace,
309   .tx_function_n_errors = TAP_TX_N_ERROR,
310   .tx_function_error_strings = virtio_tx_func_error_strings,
311   .rx_redirect_to_node = virtio_set_interface_next_node,
312   .clear_counters = virtio_clear_hw_interface_counters,
313   .admin_up_down_function = virtio_interface_admin_up_down,
314   .subif_add_del_function = virtio_subif_add_del_function,
315   .rx_mode_change_function = virtio_interface_rx_mode_change,
316 };
317
318 VLIB_DEVICE_TX_FUNCTION_MULTIARCH(virtio_device_class,
319                                   virtio_interface_tx)
320 /* *INDENT-ON* */
321
322 /*
323  * fd.io coding-style-patch-verification: ON
324  *
325  * Local Variables:
326  * eval: (c-set-style "gnu")
327  * End:
328  */