blob: 02ded3f5f52d569ec82c0911be488835b2a46228 [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>
29#include <vlib/unix/unix.h>
30#include <vnet/ethernet/ethernet.h>
Milan Lenco73e7f422017-12-14 10:04:25 +010031#include <vnet/ip/ip4_packet.h>
32#include <vnet/ip/ip6_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020033#include <vnet/devices/virtio/virtio.h>
34
35virtio_main_t virtio_main;
36
37#define _IOCTL(fd,a,...) \
38 if (ioctl (fd, a, __VA_ARGS__) < 0) \
39 { \
40 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
41 goto error; \
42 }
43
44static clib_error_t *
45call_read_ready (clib_file_t * uf)
46{
47 virtio_main_t *nm = &virtio_main;
48 vnet_main_t *vnm = vnet_get_main ();
49 u16 qid = uf->private_data & 0xFFFF;
50 virtio_if_t *vif =
51 vec_elt_at_index (nm->interfaces, uf->private_data >> 16);
52 u64 b;
53
54 CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
55 if ((qid & 1) == 0)
56 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
57
58 return 0;
59}
60
61
62clib_error_t *
63virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
64{
65 clib_error_t *err = 0;
66 virtio_vring_t *vring;
Damjan Marion70749612017-12-04 11:23:58 +010067 struct vhost_vring_state state = { 0 };
68 struct vhost_vring_addr addr = { 0 };
69 struct vhost_vring_file file = { 0 };
Damjan Marion8389fb92017-10-13 18:29:53 +020070 clib_file_t t = { 0 };
71 int i;
72
73 if (!is_pow2 (sz))
74 return clib_error_return (0, "ring size must be power of 2");
75
76 if (sz > 32768)
77 return clib_error_return (0, "ring size must be 32768 or lower");
78
79 if (sz == 0)
80 sz = 256;
81
82 vec_validate_aligned (vif->vrings, idx, CLIB_CACHE_LINE_BYTES);
83 vring = vec_elt_at_index (vif->vrings, idx);
84
85 i = sizeof (struct vring_desc) * sz;
86 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
87 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
88 memset (vring->desc, 0, i);
89
90 i = sizeof (struct vring_avail) + sz * sizeof (vring->avail->ring[0]);
91 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
92 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
93 memset (vring->avail, 0, i);
94 // tell kernel that we don't need interrupt
95 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
96
97 i = sizeof (struct vring_used) + sz * sizeof (struct vring_used_elem);
98 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
99 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
100 memset (vring->used, 0, i);
101
102 ASSERT (vring->buffers == 0);
Damjan Marionc58408c2018-01-18 14:54:04 +0100103 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
Damjan Marion8389fb92017-10-13 18:29:53 +0200104
105 vring->size = sz;
106 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
107 vring->kick_fd = eventfd (0, EFD_CLOEXEC);
108
109 t.read_function = call_read_ready;
110 t.file_descriptor = vring->call_fd;
111 t.private_data = vif->dev_instance << 16 | idx;
112 vring->call_file_index = clib_file_add (&file_main, &t);
113
114 state.index = idx;
115 state.num = sz;
116 _IOCTL (vif->fd, VHOST_SET_VRING_NUM, &state);
117
118 addr.index = idx;
119 addr.flags = 0;
120 addr.desc_user_addr = pointer_to_uword (vring->desc);
121 addr.avail_user_addr = pointer_to_uword (vring->avail);
122 addr.used_user_addr = pointer_to_uword (vring->used);
123 _IOCTL (vif->fd, VHOST_SET_VRING_ADDR, &addr);
124
125 file.index = idx;
126 file.fd = vring->kick_fd;
127 _IOCTL (vif->fd, VHOST_SET_VRING_KICK, &file);
128 file.fd = vring->call_fd;
129 _IOCTL (vif->fd, VHOST_SET_VRING_CALL, &file);
130 file.fd = vif->tap_fd;
131 _IOCTL (vif->fd, VHOST_NET_SET_BACKEND, &file);
132
133error:
134 return err;
135}
136
Stevena624dbe2018-01-09 11:13:29 -0800137static_always_inline void
138virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring)
Damjan Marion8389fb92017-10-13 18:29:53 +0200139{
Stevena624dbe2018-01-09 11:13:29 -0800140 u16 used = vring->desc_in_use;
141 u16 next = vring->desc_next;
142 u16 mask = vring->size - 1;
143
144 while (used)
145 {
146 vlib_buffer_free (vm, &vring->buffers[next], 1);
147 next = (next + 1) & mask;
148 used--;
149 }
150}
151
152clib_error_t *
153virtio_vring_free (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
154{
Damjan Marion8389fb92017-10-13 18:29:53 +0200155 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, idx);
Stevena624dbe2018-01-09 11:13:29 -0800156
157 clib_file_del_by_index (&file_main, vring->call_file_index);
158 close (vring->kick_fd);
159 close (vring->call_fd);
160 if (vring->used)
161 {
162 if ((idx & 1) == 1)
163 virtio_free_used_desc (vm, vring);
164 else
165 virtio_free_rx_buffers (vm, vring);
166 clib_mem_free (vring->used);
167 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200168 if (vring->desc)
169 clib_mem_free (vring->desc);
170 if (vring->avail)
171 clib_mem_free (vring->avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200172 vec_free (vring->buffers);
173 return 0;
174}
175
176/*
177 * fd.io coding-style-patch-verification: ON
178 *
179 * Local Variables:
180 * eval: (c-set-style "gnu")
181 * End:
182 */