blob: c0c163fc850c071361b6d87a0be9644b8156dea9 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 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 <net/if.h>
22#include <linux/if_tun.h>
23#include <sys/ioctl.h>
24#include <linux/virtio_net.h>
25#include <linux/vhost.h>
26#include <sys/eventfd.h>
27
28#include <vlib/vlib.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020029#include <vlib/pci/pci.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020030#include <vlib/unix/unix.h>
31#include <vnet/ethernet/ethernet.h>
Milan Lenco73e7f422017-12-14 10:04:25 +010032#include <vnet/ip/ip4_packet.h>
33#include <vnet/ip/ip6_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020034#include <vnet/devices/virtio/virtio.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020035#include <vnet/devices/virtio/pci.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020036
37virtio_main_t virtio_main;
38
39#define _IOCTL(fd,a,...) \
40 if (ioctl (fd, a, __VA_ARGS__) < 0) \
41 { \
42 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
43 goto error; \
44 }
45
46static clib_error_t *
47call_read_ready (clib_file_t * uf)
48{
49 virtio_main_t *nm = &virtio_main;
50 vnet_main_t *vnm = vnet_get_main ();
51 u16 qid = uf->private_data & 0xFFFF;
52 virtio_if_t *vif =
53 vec_elt_at_index (nm->interfaces, uf->private_data >> 16);
54 u64 b;
55
56 CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
57 if ((qid & 1) == 0)
58 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
59
60 return 0;
61}
62
63
64clib_error_t *
65virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
66{
Damjan Marion8389fb92017-10-13 18:29:53 +020067 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +020068 clib_file_t t = { 0 };
69 int i;
70
71 if (!is_pow2 (sz))
72 return clib_error_return (0, "ring size must be power of 2");
73
74 if (sz > 32768)
75 return clib_error_return (0, "ring size must be 32768 or lower");
76
77 if (sz == 0)
78 sz = 256;
79
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000080 if (idx % 2)
81 {
Mohsin Kazmi3f340172019-05-27 15:53:25 +020082 vlib_thread_main_t *thm = vlib_get_thread_main ();
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000083 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
84 CLIB_CACHE_LINE_BYTES);
85 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
Damjan Marion7c6102b2019-11-08 17:59:56 +010086 if (thm->n_vlib_mains > vif->num_txqs)
Mohsin Kazmi3f340172019-05-27 15:53:25 +020087 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000088 }
89 else
90 {
91 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
92 CLIB_CACHE_LINE_BYTES);
93 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
94 }
Damjan Marion8389fb92017-10-13 18:29:53 +020095 i = sizeof (struct vring_desc) * sz;
96 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
97 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040098 clib_memset (vring->desc, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +020099
100 i = sizeof (struct vring_avail) + sz * sizeof (vring->avail->ring[0]);
101 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
102 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400103 clib_memset (vring->avail, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200104 // tell kernel that we don't need interrupt
105 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
106
107 i = sizeof (struct vring_used) + sz * sizeof (struct vring_used_elem);
108 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
109 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400110 clib_memset (vring->used, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200111
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000112 vring->queue_id = idx;
Damjan Marion8389fb92017-10-13 18:29:53 +0200113 ASSERT (vring->buffers == 0);
Damjan Marionc58408c2018-01-18 14:54:04 +0100114 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
Damjan Marion8389fb92017-10-13 18:29:53 +0200115
116 vring->size = sz;
117 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100118 vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
119 virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx,
120 vring->size, vring->call_fd, vring->kick_fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200121
122 t.read_function = call_read_ready;
123 t.file_descriptor = vring->call_fd;
124 t.private_data = vif->dev_instance << 16 | idx;
Damjan Marionceab7882018-01-19 20:56:12 +0100125 t.description = format (0, "%U vring %u", format_virtio_device_name,
126 vif->dev_instance, idx);
Damjan Marion8389fb92017-10-13 18:29:53 +0200127 vring->call_file_index = clib_file_add (&file_main, &t);
128
Damjan Marion7c6102b2019-11-08 17:59:56 +0100129 return 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200130}
131
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200132inline void
Stevena624dbe2018-01-09 11:13:29 -0800133virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring)
Damjan Marion8389fb92017-10-13 18:29:53 +0200134{
Stevena624dbe2018-01-09 11:13:29 -0800135 u16 used = vring->desc_in_use;
Steven074f6982018-03-30 22:18:11 -0700136 u16 last = vring->last_used_idx;
Stevena624dbe2018-01-09 11:13:29 -0800137 u16 mask = vring->size - 1;
138
139 while (used)
140 {
Steven074f6982018-03-30 22:18:11 -0700141 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
142 last++;
Stevena624dbe2018-01-09 11:13:29 -0800143 used--;
144 }
145}
146
147clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000148virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
Stevena624dbe2018-01-09 11:13:29 -0800149{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000150 virtio_vring_t *vring =
151 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
Stevena624dbe2018-01-09 11:13:29 -0800152
153 clib_file_del_by_index (&file_main, vring->call_file_index);
154 close (vring->kick_fd);
155 close (vring->call_fd);
156 if (vring->used)
157 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000158 virtio_free_rx_buffers (vm, vring);
Stevena624dbe2018-01-09 11:13:29 -0800159 clib_mem_free (vring->used);
160 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200161 if (vring->desc)
162 clib_mem_free (vring->desc);
163 if (vring->avail)
164 clib_mem_free (vring->avail);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000165 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000166 return 0;
167}
168
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200169inline void
170virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring)
171{
172 u16 used = vring->desc_in_use;
173 u16 sz = vring->size;
174 u16 mask = sz - 1;
175 u16 last = vring->last_used_idx;
176 u16 n_left = vring->used->idx - last;
177
178 if (n_left == 0)
179 return;
180
181 while (n_left)
182 {
183 struct vring_used_elem *e = &vring->used->ring[last & mask];
184 u16 slot = e->id;
185
186 vlib_buffer_free (vm, &vring->buffers[slot], 1);
187 used--;
188 last++;
189 n_left--;
190 }
191 vring->desc_in_use = used;
192 vring->last_used_idx = last;
193}
194
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000195clib_error_t *
196virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
197{
198 virtio_vring_t *vring =
199 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
200
201 clib_file_del_by_index (&file_main, vring->call_file_index);
202 close (vring->kick_fd);
203 close (vring->call_fd);
204 if (vring->used)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200205 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000206 virtio_free_used_desc (vm, vring);
207 clib_mem_free (vring->used);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200208 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000209 if (vring->desc)
210 clib_mem_free (vring->desc);
211 if (vring->avail)
212 clib_mem_free (vring->avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200213 vec_free (vring->buffers);
Mohsin Kazmi3f340172019-05-27 15:53:25 +0200214 clib_spinlock_free (&vring->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200215 return 0;
216}
217
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000218void
219virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
220{
221 vnet_main_t *vnm = vnet_get_main ();
222 u32 thread_index;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000223 virtio_vring_t *vring =
224 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000225 thread_index =
226 vnet_get_device_input_thread_index (vnm, vif->hw_if_index,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000227 RX_QUEUE_ACCESS (idx));
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000228 vring->buffer_pool_index =
229 vlib_buffer_pool_get_default_for_numa (vm,
230 vlib_mains
231 [thread_index]->numa_node);
232}
233
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200234inline void
235virtio_set_net_hdr_size (virtio_if_t * vif)
236{
237 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
238 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
239 vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr_v1);
240 else
241 vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr);
242}
243
244inline void
245virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
246{
247 u32 i, j, hw_if_index;
248 virtio_if_t *vif;
249 vnet_main_t *vnm = &vnet_main;
250 virtio_main_t *mm = &virtio_main;
251 virtio_vring_t *vring;
252 struct feat_struct
253 {
254 u8 bit;
255 char *str;
256 };
257 struct feat_struct *feat_entry;
258
259 static struct feat_struct feat_array[] = {
260#define _(s,b) { .str = #s, .bit = b, },
261 foreach_virtio_net_features
262#undef _
263 {.str = NULL}
264 };
265
266 struct feat_struct *flag_entry;
267 static struct feat_struct flags_array[] = {
268#define _(b,e,s) { .bit = b, .str = s, },
269 foreach_virtio_if_flag
270#undef _
271 {.str = NULL}
272 };
273
274 if (!hw_if_indices)
275 return;
276
277 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
278 {
279 vnet_hw_interface_t *hi =
280 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
281 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
282 if (vif->type != type)
283 continue;
284 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
285 format_vnet_hw_if_index_name, vnm,
286 hw_if_indices[hw_if_index], vif->hw_if_index);
287 if (type == VIRTIO_IF_TYPE_PCI)
288 {
289 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
290 &vif->pci_addr);
291 }
292 if (type == VIRTIO_IF_TYPE_TAP)
293 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100294 u8 *str = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200295 if (vif->host_if_name)
296 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
297 if (vif->net_ns)
298 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200299 if (vif->host_mtu_size)
300 vlib_cli_output (vm, " host-mtu-size \"%d\"",
301 vif->host_mtu_size);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100302
303 vec_foreach_index (i, vif->vhost_fds)
304 str = format (str, " %d", vif->vhost_fds[i]);
305 vlib_cli_output (vm, " vhost-fds%v", str);
306 vec_free (str);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200307 vlib_cli_output (vm, " tap-fd %d", vif->tap_fd);
308 }
Chenmin Sun7c615ae2019-07-18 23:19:28 +0800309 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100310 vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200311 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
312 vif->mac_addr);
313 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
314 vlib_cli_output (vm, " flags 0x%x", vif->flags);
315 flag_entry = (struct feat_struct *) &flags_array;
316 while (flag_entry->str)
317 {
318 if (vif->flags & (1ULL << flag_entry->bit))
319 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
320 flag_entry->bit);
321 flag_entry++;
322 }
323 if (type == VIRTIO_IF_TYPE_PCI)
324 {
325 device_status (vm, vif);
326 }
327 vlib_cli_output (vm, " features 0x%lx", vif->features);
328 feat_entry = (struct feat_struct *) &feat_array;
329 while (feat_entry->str)
330 {
331 if (vif->features & (1ULL << feat_entry->bit))
332 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
333 feat_entry->bit);
334 feat_entry++;
335 }
336 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
337 feat_entry = (struct feat_struct *) &feat_array;
338 while (feat_entry->str)
339 {
340 if (vif->remote_features & (1ULL << feat_entry->bit))
341 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
342 feat_entry->bit);
343 feat_entry++;
344 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000345 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
346 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
347 if (vif->cxq_vring != NULL
348 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
349 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
350 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200351 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000352 vring = vec_elt_at_index (vif->rxq_vrings, i);
353 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200354 vlib_cli_output (vm,
355 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
356 vring->size, vring->last_used_idx, vring->desc_next,
357 vring->desc_in_use);
358 vlib_cli_output (vm,
359 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
360 vring->avail->flags, vring->avail->idx,
361 vring->used->flags, vring->used->idx);
362 if (type == VIRTIO_IF_TYPE_TAP)
363 {
364 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
365 vring->call_fd);
366 }
367 if (show_descr)
368 {
369 vlib_cli_output (vm, "\n descriptor table:\n");
370 vlib_cli_output (vm,
371 " id addr len flags next user_addr\n");
372 vlib_cli_output (vm,
373 " ===== ================== ===== ====== ===== ==================\n");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200374 for (j = 0; j < vring->size; j++)
375 {
376 struct vring_desc *desc = &vring->desc[j];
377 vlib_cli_output (vm,
378 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
379 j, desc->addr,
380 desc->len,
381 desc->flags, desc->next, desc->addr);
382 }
383 }
384 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000385 vec_foreach_index (i, vif->txq_vrings)
386 {
387 vring = vec_elt_at_index (vif->txq_vrings, i);
388 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
389 vlib_cli_output (vm,
390 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
391 vring->size, vring->last_used_idx, vring->desc_next,
392 vring->desc_in_use);
393 vlib_cli_output (vm,
394 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
395 vring->avail->flags, vring->avail->idx,
396 vring->used->flags, vring->used->idx);
397 if (type == VIRTIO_IF_TYPE_TAP)
398 {
399 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
400 vring->call_fd);
401 }
402 if (show_descr)
403 {
404 vlib_cli_output (vm, "\n descriptor table:\n");
405 vlib_cli_output (vm,
406 " id addr len flags next user_addr\n");
407 vlib_cli_output (vm,
408 " ===== ================== ===== ====== ===== ==================\n");
409 for (j = 0; j < vring->size; j++)
410 {
411 struct vring_desc *desc = &vring->desc[j];
412 vlib_cli_output (vm,
413 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
414 j, desc->addr,
415 desc->len,
416 desc->flags, desc->next, desc->addr);
417 }
418 }
419 }
420 if (vif->cxq_vring != NULL
421 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
422 {
423 vring = vif->cxq_vring;
424 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
425 vlib_cli_output (vm,
426 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
427 vring->size, vring->last_used_idx,
428 vring->desc_next, vring->desc_in_use);
429 vlib_cli_output (vm,
430 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
431 vring->avail->flags, vring->avail->idx,
432 vring->used->flags, vring->used->idx);
433 if (type == VIRTIO_IF_TYPE_TAP)
434 {
435 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
436 vring->call_fd);
437 }
438 if (show_descr)
439 {
440 vlib_cli_output (vm, "\n descriptor table:\n");
441 vlib_cli_output (vm,
442 " id addr len flags next user_addr\n");
443 vlib_cli_output (vm,
444 " ===== ================== ===== ====== ===== ==================\n");
445 for (j = 0; j < vring->size; j++)
446 {
447 struct vring_desc *desc = &vring->desc[j];
448 vlib_cli_output (vm,
449 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
450 j, desc->addr,
451 desc->len,
452 desc->flags, desc->next, desc->addr);
453 }
454 }
455 }
456
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200457 }
458
459}
460
Damjan Marionf41244f2019-11-08 17:41:06 +0100461static clib_error_t *
462virtio_init (vlib_main_t * vm)
463{
464 virtio_main_t *vim = &virtio_main;
465 clib_error_t *error = 0;
466
467 vim->log_default = vlib_log_register_class ("virtio", 0);
468 vlib_log_debug (vim->log_default, "initialized");
469
470 return error;
471}
472
473VLIB_INIT_FUNCTION (virtio_init);
474
Damjan Marion8389fb92017-10-13 18:29:53 +0200475/*
476 * fd.io coding-style-patch-verification: ON
477 *
478 * Local Variables:
479 * eval: (c-set-style "gnu")
480 * End:
481 */