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