blob: 609ffb47de84f13047823a96b5e7df52f2fcf164 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +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
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020021
22#include <vlib/vlib.h>
23#include <vlib/unix/unix.h>
24#include <vnet/ethernet/ethernet.h>
Milan Lenco73e7f422017-12-14 10:04:25 +010025#include <vnet/ip/ip4_packet.h>
26#include <vnet/ip/ip6_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020027#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
35typedef enum
36{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020037#define _(f,s) VIRTIO_TX_ERROR_##f,
Damjan Marion8389fb92017-10-13 18:29:53 +020038 foreach_virtio_tx_func_error
39#undef _
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020040 VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +020041} virtio_tx_func_error_t;
42
43static char *virtio_tx_func_error_strings[] = {
44#define _(n,s) s,
45 foreach_virtio_tx_func_error
46#undef _
47};
48
49u8 *
50format_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)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020057 s = format (s, "tap%u", vif->id);
58 else if (vif->type == VIRTIO_IF_TYPE_PCI)
59 s = format (s, "virtio-%x/%x/%x/%x", vif->pci_addr.domain,
60 vif->pci_addr.bus, vif->pci_addr.slot,
61 vif->pci_addr.function);
Damjan Marion8389fb92017-10-13 18:29:53 +020062 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020063 s = format (s, "virtio-%lu", vif->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +020064
65 return s;
66}
67
68static u8 *
69format_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
84static u8 *
85format_virtio_tx_trace (u8 * s, va_list * args)
86{
87 s = format (s, "Unimplemented...");
88 return s;
89}
90
Stevena624dbe2018-01-09 11:13:29 -080091inline void
Damjan Marion8389fb92017-10-13 18:29:53 +020092virtio_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;
Damjan Marion8389fb92017-10-13 18:29:53 +0200107
108 vlib_buffer_free (vm, &vring->buffers[slot], 1);
109 used--;
110 last++;
111 n_left--;
112 }
113 vring->desc_in_use = used;
114 vring->last_used_idx = last;
115}
116
117static_always_inline u16
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200118add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
119 virtio_vring_t * vring, u32 bi, u16 avail, u16 next,
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200120 u16 mask, int do_gso)
Damjan Marion8389fb92017-10-13 18:29:53 +0200121{
122 u16 n_added = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200123 int hdr_sz = vif->virtio_net_hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200124 struct vring_desc *d;
125 d = &vring->desc[next];
126 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Damjan Marion508cabc2018-02-08 19:49:22 +0100127 struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
128
Dave Barachb7b92992018-10-17 10:38:51 -0400129 clib_memset (hdr, 0, hdr_sz);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200130 if (do_gso && (b->flags & VNET_BUFFER_F_GSO))
131 {
132 if (b->flags & VNET_BUFFER_F_IS_IP4)
133 {
134 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
135 hdr->gso_size = vnet_buffer2 (b)->gso_size;
136 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
137 hdr->csum_start = vnet_buffer (b)->l4_hdr_offset; // 0x22;
138 hdr->csum_offset = 0x10;
139 }
140 else
141 {
142 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
143 hdr->gso_size = vnet_buffer2 (b)->gso_size;
144 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
145 hdr->csum_start = vnet_buffer (b)->l4_hdr_offset; // 0x36;
146 hdr->csum_offset = 0x10;
147 }
148 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200149
150 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
151 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200152 d->addr =
153 ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
154 b) :
155 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200156 d->len = b->current_length + hdr_sz;
157 d->flags = 0;
158 }
159 else
160 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200161 /*
162 * We are using single vlib_buffer_t for indirect descriptor(s)
163 * chain. Single descriptor is 16 bytes and vlib_buffer_t
164 * has 2048 bytes space. So maximum long chain can have 128
165 * (=2048/16) indirect descriptors.
166 * It can easily support 65535 bytes of Jumbo frames with
167 * each data buffer size of 512 bytes minimum.
168 */
169 vlib_buffer_t *indirect_desc =
170 vlib_get_buffer (vm, vring->indirect_buffers[next]);
171 indirect_desc->current_data = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200172
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200173 struct vring_desc *id =
174 (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
175 u32 count = 1;
176 if (vif->type == VIRTIO_IF_TYPE_PCI)
Damjan Marion8389fb92017-10-13 18:29:53 +0200177 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200178 d->addr = vlib_physmem_get_pa (vm, id);
179 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200180
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200181 /*
182 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
183 * should be presented in separate descriptor and data will start
184 * from next descriptor.
185 */
186 if (PREDICT_TRUE
187 (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
188 id->len = b->current_length + hdr_sz;
189 else
190 {
191 id->len = hdr_sz;
192 id->flags = VRING_DESC_F_NEXT;
193 id->next = count;
194 count++;
195 id++;
196 id->addr = vlib_buffer_get_current_pa (vm, b);
197 id->len = b->current_length;
198 }
199 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
200 {
201 id->flags = VRING_DESC_F_NEXT;
202 id->next = count;
203 count++;
204 id++;
205 b = vlib_get_buffer (vm, b->next_buffer);
206 id->addr = vlib_buffer_get_current_pa (vm, b);
207 id->len = b->current_length;
208 }
209 }
210 else /* VIRTIO_IF_TYPE_TAP */
211 {
212 d->addr = pointer_to_uword (id);
213 /* first buffer in chain */
214 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
215 id->len = b->current_length + hdr_sz;
216
217 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
218 {
219 id->flags = VRING_DESC_F_NEXT;
220 id->next = count;
221 count++;
222 id++;
223 b = vlib_get_buffer (vm, b->next_buffer);
224 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
225 id->len = b->current_length;
226 }
227 }
228 id->flags = 0;
229 id->next = 0;
230 d->len = count * sizeof (struct vring_desc);
Damjan Marion8389fb92017-10-13 18:29:53 +0200231 d->flags = VRING_DESC_F_INDIRECT;
232 }
233 vring->buffers[next] = bi;
234 vring->avail->ring[avail & mask] = next;
235 n_added++;
236 return n_added;
237}
238
Damjan Marion8389fb92017-10-13 18:29:53 +0200239static_always_inline uword
240virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200241 vlib_frame_t * frame, virtio_if_t * vif,
242 int do_gso)
Damjan Marion8389fb92017-10-13 18:29:53 +0200243{
244 u8 qid = 0;
245 u16 n_left = frame->n_vectors;
246 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, (qid << 1) + 1);
247 u16 used, next, avail;
248 u16 sz = vring->size;
249 u16 mask = sz - 1;
Damjan Mariona3d59862018-11-10 10:23:00 +0100250 u32 *buffers = vlib_frame_vector_args (frame);
Damjan Marion8389fb92017-10-13 18:29:53 +0200251
Damjan Marion829ee532018-02-16 16:13:32 +0100252 clib_spinlock_lock_if_init (&vif->lockp);
253
Damjan Marione40231b2018-12-20 10:44:47 +0100254 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200255 (vring->last_kick_avail_idx != vring->avail->idx))
256 virtio_kick (vm, vring, vif);
Damjan Marione40231b2018-12-20 10:44:47 +0100257
Damjan Marion8389fb92017-10-13 18:29:53 +0200258 /* free consumed buffers */
259 virtio_free_used_desc (vm, vring);
260
261 used = vring->desc_in_use;
262 next = vring->desc_next;
263 avail = vring->avail->idx;
264
265 while (n_left && used < sz)
266 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200267 u16 n_added = 0;
268 n_added =
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200269 add_buffer_to_slot (vm, vif, vring, buffers[0], avail, next, mask,
270 do_gso);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200271 if (!n_added)
272 break;
Damjan Marion8389fb92017-10-13 18:29:53 +0200273 avail += n_added;
274 next = (next + n_added) & mask;
275 used += n_added;
276 buffers++;
277 n_left--;
278 }
279
280 if (n_left != frame->n_vectors)
281 {
282 CLIB_MEMORY_STORE_BARRIER ();
283 vring->avail->idx = avail;
284 vring->desc_next = next;
285 vring->desc_in_use = used;
286 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200287 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200288 }
289
Damjan Marion8389fb92017-10-13 18:29:53 +0200290 if (n_left)
291 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200292 vlib_error_count (vm, node->node_index, VIRTIO_TX_ERROR_NO_FREE_SLOTS,
Damjan Marion8389fb92017-10-13 18:29:53 +0200293 n_left);
294 vlib_buffer_free (vm, buffers, n_left);
295 }
296
Damjan Marion829ee532018-02-16 16:13:32 +0100297 clib_spinlock_unlock_if_init (&vif->lockp);
298
Damjan Marion8389fb92017-10-13 18:29:53 +0200299 return frame->n_vectors - n_left;
300}
301
302static uword
303virtio_interface_tx (vlib_main_t * vm,
304 vlib_node_runtime_t * node, vlib_frame_t * frame)
305{
306 virtio_main_t *nm = &virtio_main;
307 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
308 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
309
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200310 vnet_main_t *vnm = vnet_get_main ();
311 if (vnm->interface_main.gso_interface_count > 0)
312 return virtio_interface_tx_inline (vm, node, frame, vif, 1 /* do_gso */ );
313 else
314 return virtio_interface_tx_inline (vm, node, frame, vif,
315 0 /* no do_gso */ );
Damjan Marion8389fb92017-10-13 18:29:53 +0200316}
317
318static void
319virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
320 u32 node_index)
321{
322 virtio_main_t *apm = &virtio_main;
323 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
324 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
325
326 /* Shut off redirection */
327 if (node_index == ~0)
328 {
329 vif->per_interface_next_index = node_index;
330 return;
331 }
332
333 vif->per_interface_next_index =
334 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
335 node_index);
336}
337
338static void
339virtio_clear_hw_interface_counters (u32 instance)
340{
341 /* Nothing for now */
342}
343
344static clib_error_t *
345virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
346 vnet_hw_interface_rx_mode mode)
347{
348 virtio_main_t *mm = &virtio_main;
349 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
350 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
351 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, qid);
352
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000353 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
354 {
355 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
356 return clib_error_return (0, "interrupt mode is not supported");
357 }
358
Damjan Marion8389fb92017-10-13 18:29:53 +0200359 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
360 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
361 else
362 vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
363
364 return 0;
365}
366
367static clib_error_t *
368virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
369{
370 virtio_main_t *mm = &virtio_main;
371 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
372 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +0200373
374 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
375 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
376 else
377 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
378
Damjan Marion8389fb92017-10-13 18:29:53 +0200379 return 0;
380}
381
382static clib_error_t *
383virtio_subif_add_del_function (vnet_main_t * vnm,
384 u32 hw_if_index,
385 struct vnet_sw_interface_t *st, int is_add)
386{
387 /* Nothing for now */
388 return 0;
389}
390
391/* *INDENT-OFF* */
392VNET_DEVICE_CLASS (virtio_device_class) = {
393 .name = "virtio",
394 .tx_function = virtio_interface_tx,
395 .format_device_name = format_virtio_device_name,
396 .format_device = format_virtio_device,
397 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200398 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +0200399 .tx_function_error_strings = virtio_tx_func_error_strings,
400 .rx_redirect_to_node = virtio_set_interface_next_node,
401 .clear_counters = virtio_clear_hw_interface_counters,
402 .admin_up_down_function = virtio_interface_admin_up_down,
403 .subif_add_del_function = virtio_subif_add_del_function,
404 .rx_mode_change_function = virtio_interface_rx_mode_change,
405};
406
407VLIB_DEVICE_TX_FUNCTION_MULTIARCH(virtio_device_class,
408 virtio_interface_tx)
409/* *INDENT-ON* */
410
411/*
412 * fd.io coding-style-patch-verification: ON
413 *
414 * Local Variables:
415 * eval: (c-set-style "gnu")
416 * End:
417 */