blob: 90adf97dc3c9324f8575103add47128940bb05dd [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{
67 clib_error_t *err = 0;
68 virtio_vring_t *vring;
Damjan Marion70749612017-12-04 11:23:58 +010069 struct vhost_vring_state state = { 0 };
70 struct vhost_vring_addr addr = { 0 };
71 struct vhost_vring_file file = { 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +020072 clib_file_t t = { 0 };
73 int i;
74
75 if (!is_pow2 (sz))
76 return clib_error_return (0, "ring size must be power of 2");
77
78 if (sz > 32768)
79 return clib_error_return (0, "ring size must be 32768 or lower");
80
81 if (sz == 0)
82 sz = 256;
83
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000084 if (idx % 2)
85 {
Mohsin Kazmi3f340172019-05-27 15:53:25 +020086 vlib_thread_main_t *thm = vlib_get_thread_main ();
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000087 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
88 CLIB_CACHE_LINE_BYTES);
89 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
Mohsin Kazmi3f340172019-05-27 15:53:25 +020090 if (thm->n_vlib_mains > 1)
91 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000092 }
93 else
94 {
95 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
96 CLIB_CACHE_LINE_BYTES);
97 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
98 }
Damjan Marion8389fb92017-10-13 18:29:53 +020099 i = sizeof (struct vring_desc) * sz;
100 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
101 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400102 clib_memset (vring->desc, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200103
104 i = sizeof (struct vring_avail) + sz * sizeof (vring->avail->ring[0]);
105 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
106 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400107 clib_memset (vring->avail, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200108 // tell kernel that we don't need interrupt
109 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
110
111 i = sizeof (struct vring_used) + sz * sizeof (struct vring_used_elem);
112 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
113 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400114 clib_memset (vring->used, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200115
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000116 vring->queue_id = idx;
Damjan Marion8389fb92017-10-13 18:29:53 +0200117 ASSERT (vring->buffers == 0);
Damjan Marionc58408c2018-01-18 14:54:04 +0100118 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
Damjan Marion8389fb92017-10-13 18:29:53 +0200119
120 vring->size = sz;
121 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
122 vring->kick_fd = eventfd (0, EFD_CLOEXEC);
123
124 t.read_function = call_read_ready;
125 t.file_descriptor = vring->call_fd;
126 t.private_data = vif->dev_instance << 16 | idx;
Damjan Marionceab7882018-01-19 20:56:12 +0100127 t.description = format (0, "%U vring %u", format_virtio_device_name,
128 vif->dev_instance, idx);
Damjan Marion8389fb92017-10-13 18:29:53 +0200129 vring->call_file_index = clib_file_add (&file_main, &t);
130
131 state.index = idx;
132 state.num = sz;
133 _IOCTL (vif->fd, VHOST_SET_VRING_NUM, &state);
134
135 addr.index = idx;
136 addr.flags = 0;
137 addr.desc_user_addr = pointer_to_uword (vring->desc);
138 addr.avail_user_addr = pointer_to_uword (vring->avail);
139 addr.used_user_addr = pointer_to_uword (vring->used);
140 _IOCTL (vif->fd, VHOST_SET_VRING_ADDR, &addr);
141
142 file.index = idx;
143 file.fd = vring->kick_fd;
144 _IOCTL (vif->fd, VHOST_SET_VRING_KICK, &file);
145 file.fd = vring->call_fd;
146 _IOCTL (vif->fd, VHOST_SET_VRING_CALL, &file);
147 file.fd = vif->tap_fd;
148 _IOCTL (vif->fd, VHOST_NET_SET_BACKEND, &file);
149
150error:
151 return err;
152}
153
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200154inline void
Stevena624dbe2018-01-09 11:13:29 -0800155virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring)
Damjan Marion8389fb92017-10-13 18:29:53 +0200156{
Stevena624dbe2018-01-09 11:13:29 -0800157 u16 used = vring->desc_in_use;
Steven074f6982018-03-30 22:18:11 -0700158 u16 last = vring->last_used_idx;
Stevena624dbe2018-01-09 11:13:29 -0800159 u16 mask = vring->size - 1;
160
161 while (used)
162 {
Steven074f6982018-03-30 22:18:11 -0700163 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
164 last++;
Stevena624dbe2018-01-09 11:13:29 -0800165 used--;
166 }
167}
168
169clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000170virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
Stevena624dbe2018-01-09 11:13:29 -0800171{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000172 virtio_vring_t *vring =
173 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
Stevena624dbe2018-01-09 11:13:29 -0800174
175 clib_file_del_by_index (&file_main, vring->call_file_index);
176 close (vring->kick_fd);
177 close (vring->call_fd);
178 if (vring->used)
179 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000180 virtio_free_rx_buffers (vm, vring);
Stevena624dbe2018-01-09 11:13:29 -0800181 clib_mem_free (vring->used);
182 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200183 if (vring->desc)
184 clib_mem_free (vring->desc);
185 if (vring->avail)
186 clib_mem_free (vring->avail);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000187 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000188 return 0;
189}
190
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200191inline void
192virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring)
193{
194 u16 used = vring->desc_in_use;
195 u16 sz = vring->size;
196 u16 mask = sz - 1;
197 u16 last = vring->last_used_idx;
198 u16 n_left = vring->used->idx - last;
199
200 if (n_left == 0)
201 return;
202
203 while (n_left)
204 {
205 struct vring_used_elem *e = &vring->used->ring[last & mask];
206 u16 slot = e->id;
207
208 vlib_buffer_free (vm, &vring->buffers[slot], 1);
209 used--;
210 last++;
211 n_left--;
212 }
213 vring->desc_in_use = used;
214 vring->last_used_idx = last;
215}
216
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000217clib_error_t *
218virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
219{
220 virtio_vring_t *vring =
221 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
222
223 clib_file_del_by_index (&file_main, vring->call_file_index);
224 close (vring->kick_fd);
225 close (vring->call_fd);
226 if (vring->used)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200227 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000228 virtio_free_used_desc (vm, vring);
229 clib_mem_free (vring->used);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200230 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000231 if (vring->desc)
232 clib_mem_free (vring->desc);
233 if (vring->avail)
234 clib_mem_free (vring->avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200235 vec_free (vring->buffers);
Mohsin Kazmi3f340172019-05-27 15:53:25 +0200236 clib_spinlock_free (&vring->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200237 return 0;
238}
239
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000240void
241virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
242{
243 vnet_main_t *vnm = vnet_get_main ();
244 u32 thread_index;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000245 virtio_vring_t *vring =
246 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000247 thread_index =
248 vnet_get_device_input_thread_index (vnm, vif->hw_if_index,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000249 RX_QUEUE_ACCESS (idx));
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000250 vring->buffer_pool_index =
251 vlib_buffer_pool_get_default_for_numa (vm,
252 vlib_mains
253 [thread_index]->numa_node);
254}
255
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200256inline void
257virtio_set_net_hdr_size (virtio_if_t * vif)
258{
259 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
260 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
261 vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr_v1);
262 else
263 vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr);
264}
265
266inline void
267virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
268{
269 u32 i, j, hw_if_index;
270 virtio_if_t *vif;
271 vnet_main_t *vnm = &vnet_main;
272 virtio_main_t *mm = &virtio_main;
273 virtio_vring_t *vring;
274 struct feat_struct
275 {
276 u8 bit;
277 char *str;
278 };
279 struct feat_struct *feat_entry;
280
281 static struct feat_struct feat_array[] = {
282#define _(s,b) { .str = #s, .bit = b, },
283 foreach_virtio_net_features
284#undef _
285 {.str = NULL}
286 };
287
288 struct feat_struct *flag_entry;
289 static struct feat_struct flags_array[] = {
290#define _(b,e,s) { .bit = b, .str = s, },
291 foreach_virtio_if_flag
292#undef _
293 {.str = NULL}
294 };
295
296 if (!hw_if_indices)
297 return;
298
299 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
300 {
301 vnet_hw_interface_t *hi =
302 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
303 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
304 if (vif->type != type)
305 continue;
306 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
307 format_vnet_hw_if_index_name, vnm,
308 hw_if_indices[hw_if_index], vif->hw_if_index);
309 if (type == VIRTIO_IF_TYPE_PCI)
310 {
311 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
312 &vif->pci_addr);
313 }
314 if (type == VIRTIO_IF_TYPE_TAP)
315 {
316 if (vif->host_if_name)
317 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
318 if (vif->net_ns)
319 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200320 if (vif->host_mtu_size)
321 vlib_cli_output (vm, " host-mtu-size \"%d\"",
322 vif->host_mtu_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200323 vlib_cli_output (vm, " fd %d", vif->fd);
324 vlib_cli_output (vm, " tap-fd %d", vif->tap_fd);
325 }
Chenmin Sun7c615ae2019-07-18 23:19:28 +0800326 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200327 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
328 vif->mac_addr);
329 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
330 vlib_cli_output (vm, " flags 0x%x", vif->flags);
331 flag_entry = (struct feat_struct *) &flags_array;
332 while (flag_entry->str)
333 {
334 if (vif->flags & (1ULL << flag_entry->bit))
335 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
336 flag_entry->bit);
337 flag_entry++;
338 }
339 if (type == VIRTIO_IF_TYPE_PCI)
340 {
341 device_status (vm, vif);
342 }
343 vlib_cli_output (vm, " features 0x%lx", vif->features);
344 feat_entry = (struct feat_struct *) &feat_array;
345 while (feat_entry->str)
346 {
347 if (vif->features & (1ULL << feat_entry->bit))
348 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
349 feat_entry->bit);
350 feat_entry++;
351 }
352 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
353 feat_entry = (struct feat_struct *) &feat_array;
354 while (feat_entry->str)
355 {
356 if (vif->remote_features & (1ULL << feat_entry->bit))
357 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
358 feat_entry->bit);
359 feat_entry++;
360 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000361 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
362 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
363 if (vif->cxq_vring != NULL
364 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
365 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
366 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200367 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000368 vring = vec_elt_at_index (vif->rxq_vrings, i);
369 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200370 vlib_cli_output (vm,
371 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
372 vring->size, vring->last_used_idx, vring->desc_next,
373 vring->desc_in_use);
374 vlib_cli_output (vm,
375 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
376 vring->avail->flags, vring->avail->idx,
377 vring->used->flags, vring->used->idx);
378 if (type == VIRTIO_IF_TYPE_TAP)
379 {
380 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
381 vring->call_fd);
382 }
383 if (show_descr)
384 {
385 vlib_cli_output (vm, "\n descriptor table:\n");
386 vlib_cli_output (vm,
387 " id addr len flags next user_addr\n");
388 vlib_cli_output (vm,
389 " ===== ================== ===== ====== ===== ==================\n");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200390 for (j = 0; j < vring->size; j++)
391 {
392 struct vring_desc *desc = &vring->desc[j];
393 vlib_cli_output (vm,
394 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
395 j, desc->addr,
396 desc->len,
397 desc->flags, desc->next, desc->addr);
398 }
399 }
400 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000401 vec_foreach_index (i, vif->txq_vrings)
402 {
403 vring = vec_elt_at_index (vif->txq_vrings, i);
404 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
405 vlib_cli_output (vm,
406 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
407 vring->size, vring->last_used_idx, vring->desc_next,
408 vring->desc_in_use);
409 vlib_cli_output (vm,
410 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
411 vring->avail->flags, vring->avail->idx,
412 vring->used->flags, vring->used->idx);
413 if (type == VIRTIO_IF_TYPE_TAP)
414 {
415 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
416 vring->call_fd);
417 }
418 if (show_descr)
419 {
420 vlib_cli_output (vm, "\n descriptor table:\n");
421 vlib_cli_output (vm,
422 " id addr len flags next user_addr\n");
423 vlib_cli_output (vm,
424 " ===== ================== ===== ====== ===== ==================\n");
425 for (j = 0; j < vring->size; j++)
426 {
427 struct vring_desc *desc = &vring->desc[j];
428 vlib_cli_output (vm,
429 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
430 j, desc->addr,
431 desc->len,
432 desc->flags, desc->next, desc->addr);
433 }
434 }
435 }
436 if (vif->cxq_vring != NULL
437 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
438 {
439 vring = vif->cxq_vring;
440 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
441 vlib_cli_output (vm,
442 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
443 vring->size, vring->last_used_idx,
444 vring->desc_next, vring->desc_in_use);
445 vlib_cli_output (vm,
446 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
447 vring->avail->flags, vring->avail->idx,
448 vring->used->flags, vring->used->idx);
449 if (type == VIRTIO_IF_TYPE_TAP)
450 {
451 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
452 vring->call_fd);
453 }
454 if (show_descr)
455 {
456 vlib_cli_output (vm, "\n descriptor table:\n");
457 vlib_cli_output (vm,
458 " id addr len flags next user_addr\n");
459 vlib_cli_output (vm,
460 " ===== ================== ===== ====== ===== ==================\n");
461 for (j = 0; j < vring->size; j++)
462 {
463 struct vring_desc *desc = &vring->desc[j];
464 vlib_cli_output (vm,
465 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
466 j, desc->addr,
467 desc->len,
468 desc->flags, desc->next, desc->addr);
469 }
470 }
471 }
472
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200473 }
474
475}
476
Damjan Marion8389fb92017-10-13 18:29:53 +0200477/*
478 * fd.io coding-style-patch-verification: ON
479 *
480 * Local Variables:
481 * eval: (c-set-style "gnu")
482 * End:
483 */