blob: aa6a342f90b9c792774ade7c2be84965b149738a [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,
120 u16 mask)
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);
Damjan Marion8389fb92017-10-13 18:29:53 +0200130
131 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
132 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200133 d->addr =
134 ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
135 b) :
136 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200137 d->len = b->current_length + hdr_sz;
138 d->flags = 0;
139 }
140 else
141 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200142 /*
143 * We are using single vlib_buffer_t for indirect descriptor(s)
144 * chain. Single descriptor is 16 bytes and vlib_buffer_t
145 * has 2048 bytes space. So maximum long chain can have 128
146 * (=2048/16) indirect descriptors.
147 * It can easily support 65535 bytes of Jumbo frames with
148 * each data buffer size of 512 bytes minimum.
149 */
150 vlib_buffer_t *indirect_desc =
151 vlib_get_buffer (vm, vring->indirect_buffers[next]);
152 indirect_desc->current_data = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200153
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200154 struct vring_desc *id =
155 (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
156 u32 count = 1;
157 if (vif->type == VIRTIO_IF_TYPE_PCI)
Damjan Marion8389fb92017-10-13 18:29:53 +0200158 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200159 d->addr = vlib_physmem_get_pa (vm, id);
160 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200161
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200162 /*
163 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
164 * should be presented in separate descriptor and data will start
165 * from next descriptor.
166 */
167 if (PREDICT_TRUE
168 (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
169 id->len = b->current_length + hdr_sz;
170 else
171 {
172 id->len = hdr_sz;
173 id->flags = VRING_DESC_F_NEXT;
174 id->next = count;
175 count++;
176 id++;
177 id->addr = vlib_buffer_get_current_pa (vm, b);
178 id->len = b->current_length;
179 }
180 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
181 {
182 id->flags = VRING_DESC_F_NEXT;
183 id->next = count;
184 count++;
185 id++;
186 b = vlib_get_buffer (vm, b->next_buffer);
187 id->addr = vlib_buffer_get_current_pa (vm, b);
188 id->len = b->current_length;
189 }
190 }
191 else /* VIRTIO_IF_TYPE_TAP */
192 {
193 d->addr = pointer_to_uword (id);
194 /* first buffer in chain */
195 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
196 id->len = b->current_length + hdr_sz;
197
198 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
199 {
200 id->flags = VRING_DESC_F_NEXT;
201 id->next = count;
202 count++;
203 id++;
204 b = vlib_get_buffer (vm, b->next_buffer);
205 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
206 id->len = b->current_length;
207 }
208 }
209 id->flags = 0;
210 id->next = 0;
211 d->len = count * sizeof (struct vring_desc);
Damjan Marion8389fb92017-10-13 18:29:53 +0200212 d->flags = VRING_DESC_F_INDIRECT;
213 }
214 vring->buffers[next] = bi;
215 vring->avail->ring[avail & mask] = next;
216 n_added++;
217 return n_added;
218}
219
Damjan Marion8389fb92017-10-13 18:29:53 +0200220static_always_inline uword
221virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
222 vlib_frame_t * frame, virtio_if_t * vif)
223{
224 u8 qid = 0;
225 u16 n_left = frame->n_vectors;
226 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, (qid << 1) + 1);
227 u16 used, next, avail;
228 u16 sz = vring->size;
229 u16 mask = sz - 1;
Damjan Mariona3d59862018-11-10 10:23:00 +0100230 u32 *buffers = vlib_frame_vector_args (frame);
Damjan Marion8389fb92017-10-13 18:29:53 +0200231
Damjan Marion829ee532018-02-16 16:13:32 +0100232 clib_spinlock_lock_if_init (&vif->lockp);
233
Damjan Marione40231b2018-12-20 10:44:47 +0100234 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200235 (vring->last_kick_avail_idx != vring->avail->idx))
236 virtio_kick (vm, vring, vif);
Damjan Marione40231b2018-12-20 10:44:47 +0100237
Damjan Marion8389fb92017-10-13 18:29:53 +0200238 /* free consumed buffers */
239 virtio_free_used_desc (vm, vring);
240
241 used = vring->desc_in_use;
242 next = vring->desc_next;
243 avail = vring->avail->idx;
244
245 while (n_left && used < sz)
246 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200247 u16 n_added = 0;
248 n_added =
249 add_buffer_to_slot (vm, vif, vring, buffers[0], avail, next, mask);
250 if (!n_added)
251 break;
Damjan Marion8389fb92017-10-13 18:29:53 +0200252 avail += n_added;
253 next = (next + n_added) & mask;
254 used += n_added;
255 buffers++;
256 n_left--;
257 }
258
259 if (n_left != frame->n_vectors)
260 {
261 CLIB_MEMORY_STORE_BARRIER ();
262 vring->avail->idx = avail;
263 vring->desc_next = next;
264 vring->desc_in_use = used;
265 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200266 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200267 }
268
Damjan Marion8389fb92017-10-13 18:29:53 +0200269 if (n_left)
270 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200271 vlib_error_count (vm, node->node_index, VIRTIO_TX_ERROR_NO_FREE_SLOTS,
Damjan Marion8389fb92017-10-13 18:29:53 +0200272 n_left);
273 vlib_buffer_free (vm, buffers, n_left);
274 }
275
Damjan Marion829ee532018-02-16 16:13:32 +0100276 clib_spinlock_unlock_if_init (&vif->lockp);
277
Damjan Marion8389fb92017-10-13 18:29:53 +0200278 return frame->n_vectors - n_left;
279}
280
281static uword
282virtio_interface_tx (vlib_main_t * vm,
283 vlib_node_runtime_t * node, vlib_frame_t * frame)
284{
285 virtio_main_t *nm = &virtio_main;
286 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
287 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
288
289 return virtio_interface_tx_inline (vm, node, frame, vif);
290}
291
292static void
293virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
294 u32 node_index)
295{
296 virtio_main_t *apm = &virtio_main;
297 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
298 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
299
300 /* Shut off redirection */
301 if (node_index == ~0)
302 {
303 vif->per_interface_next_index = node_index;
304 return;
305 }
306
307 vif->per_interface_next_index =
308 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
309 node_index);
310}
311
312static void
313virtio_clear_hw_interface_counters (u32 instance)
314{
315 /* Nothing for now */
316}
317
318static clib_error_t *
319virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
320 vnet_hw_interface_rx_mode mode)
321{
322 virtio_main_t *mm = &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 (mm->interfaces, hw->dev_instance);
325 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, qid);
326
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000327 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
328 {
329 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
330 return clib_error_return (0, "interrupt mode is not supported");
331 }
332
Damjan Marion8389fb92017-10-13 18:29:53 +0200333 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
334 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
335 else
336 vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
337
338 return 0;
339}
340
341static clib_error_t *
342virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
343{
344 virtio_main_t *mm = &virtio_main;
345 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
346 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +0200347
348 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
349 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
350 else
351 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
352
Damjan Marion8389fb92017-10-13 18:29:53 +0200353 return 0;
354}
355
356static clib_error_t *
357virtio_subif_add_del_function (vnet_main_t * vnm,
358 u32 hw_if_index,
359 struct vnet_sw_interface_t *st, int is_add)
360{
361 /* Nothing for now */
362 return 0;
363}
364
365/* *INDENT-OFF* */
366VNET_DEVICE_CLASS (virtio_device_class) = {
367 .name = "virtio",
368 .tx_function = virtio_interface_tx,
369 .format_device_name = format_virtio_device_name,
370 .format_device = format_virtio_device,
371 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200372 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +0200373 .tx_function_error_strings = virtio_tx_func_error_strings,
374 .rx_redirect_to_node = virtio_set_interface_next_node,
375 .clear_counters = virtio_clear_hw_interface_counters,
376 .admin_up_down_function = virtio_interface_admin_up_down,
377 .subif_add_del_function = virtio_subif_add_del_function,
378 .rx_mode_change_function = virtio_interface_rx_mode_change,
379};
380
381VLIB_DEVICE_TX_FUNCTION_MULTIARCH(virtio_device_class,
382 virtio_interface_tx)
383/* *INDENT-ON* */
384
385/*
386 * fd.io coding-style-patch-verification: ON
387 *
388 * Local Variables:
389 * eval: (c-set-style "gnu")
390 * End:
391 */