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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <sys/types.h>
21 #include <linux/virtio_net.h>
22 #include <linux/vhost.h>
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/devices/virtio/virtio.h>
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")
37 #define _(f,s) TAP_TX_ERROR_##f,
38 foreach_virtio_tx_func_error
41 } virtio_tx_func_error_t;
43 static char *virtio_tx_func_error_strings[] = {
45 foreach_virtio_tx_func_error
50 format_virtio_device_name (u8 * s, va_list * args)
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);
56 if (vif->type == VIRTIO_IF_TYPE_TAP)
58 s = format (s, "tap-%s", vif->name);
61 s = format (s, "virtio%lu", vif->dev_instance);
67 format_virtio_device (u8 * s, va_list * args)
69 u32 dev_instance = va_arg (*args, u32);
70 int verbose = va_arg (*args, int);
71 u32 indent = format_get_indent (s);
73 s = format (s, "VIRTIO interface");
76 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
83 format_virtio_tx_trace (u8 * s, va_list * args)
85 s = format (s, "Unimplemented...");
89 static_always_inline void
90 virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring)
92 u16 used = vring->desc_in_use;
95 u16 last = vring->last_used_idx;
96 u16 n_left = vring->used->idx - last;
103 struct vring_used_elem *e = &vring->used->ring[last & mask];
105 struct vring_desc *d = &vring->desc[slot];
107 if (PREDICT_FALSE (d->flags & VRING_DESC_F_INDIRECT))
109 d = uword_to_pointer (d->addr, struct vring_desc *);
113 vlib_buffer_free (vm, &vring->buffers[slot], 1);
118 vring->desc_in_use = used;
119 vring->last_used_idx = last;
122 static_always_inline u16
123 add_buffer_to_slot (vlib_main_t * vm, virtio_vring_t * vring, u32 bi,
124 u16 avail, u16 next, u16 mask)
127 const int hdr_sz = sizeof (struct virtio_net_hdr_v1);
128 struct vring_desc *d;
129 d = &vring->desc[next];
130 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
132 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
134 d->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
135 d->len = b->current_length + hdr_sz;
140 struct vring_desc *id, *descs = 0;
142 /* first buffer in chain */
143 vec_add2_aligned (descs, id, 1, CLIB_CACHE_LINE_BYTES);
144 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
145 id->len = b->current_length + hdr_sz;
147 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
149 id->flags = VRING_DESC_F_NEXT;
150 id->next = vec_len (descs);
151 vec_add2_aligned (descs, id, 1, CLIB_CACHE_LINE_BYTES);
152 b = vlib_get_buffer (vm, b->next_buffer);
153 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
154 id->len = b->current_length;
157 d->addr = pointer_to_uword (descs);
158 d->len = vec_len (descs) * sizeof (struct vring_desc);
159 d->flags = VRING_DESC_F_INDIRECT;
161 vring->buffers[next] = bi;
162 vring->avail->ring[avail & mask] = next;
168 static_always_inline uword
169 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
170 vlib_frame_t * frame, virtio_if_t * vif)
173 u16 n_left = frame->n_vectors;
174 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, (qid << 1) + 1);
175 u16 used, next, avail;
176 u16 sz = vring->size;
178 u32 *buffers = vlib_frame_args (frame);
180 /* free consumed buffers */
181 virtio_free_used_desc (vm, vring);
183 used = vring->desc_in_use;
184 next = vring->desc_next;
185 avail = vring->avail->idx;
187 while (n_left && used < sz)
190 n_added = add_buffer_to_slot (vm, vring, buffers[0], avail, next, mask);
192 next = (next + n_added) & mask;
198 if (n_left != frame->n_vectors)
200 CLIB_MEMORY_STORE_BARRIER ();
201 vring->avail->idx = avail;
202 vring->desc_next = next;
203 vring->desc_in_use = used;
204 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
207 CLIB_UNUSED (int r) = write (vring->kick_fd, &x, sizeof (x));
214 vlib_error_count (vm, node->node_index, TAP_TX_ERROR_NO_FREE_SLOTS,
216 vlib_buffer_free (vm, buffers, n_left);
219 return frame->n_vectors - n_left;
223 virtio_interface_tx (vlib_main_t * vm,
224 vlib_node_runtime_t * node, vlib_frame_t * frame)
226 virtio_main_t *nm = &virtio_main;
227 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
228 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
230 return virtio_interface_tx_inline (vm, node, frame, vif);
234 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
237 virtio_main_t *apm = &virtio_main;
238 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
239 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
241 /* Shut off redirection */
242 if (node_index == ~0)
244 vif->per_interface_next_index = node_index;
248 vif->per_interface_next_index =
249 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
254 virtio_clear_hw_interface_counters (u32 instance)
256 /* Nothing for now */
259 static clib_error_t *
260 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
261 vnet_hw_interface_rx_mode mode)
263 virtio_main_t *mm = &virtio_main;
264 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
265 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
266 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, qid);
268 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
269 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
271 vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
276 static clib_error_t *
277 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
279 virtio_main_t *mm = &virtio_main;
280 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
281 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
282 static clib_error_t *error = 0;
284 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
285 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
287 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
293 static clib_error_t *
294 virtio_subif_add_del_function (vnet_main_t * vnm,
296 struct vnet_sw_interface_t *st, int is_add)
298 /* Nothing for now */
303 VNET_DEVICE_CLASS (virtio_device_class) = {
305 .tx_function = virtio_interface_tx,
306 .format_device_name = format_virtio_device_name,
307 .format_device = format_virtio_device,
308 .format_tx_trace = format_virtio_tx_trace,
309 .tx_function_n_errors = TAP_TX_N_ERROR,
310 .tx_function_error_strings = virtio_tx_func_error_strings,
311 .rx_redirect_to_node = virtio_set_interface_next_node,
312 .clear_counters = virtio_clear_hw_interface_counters,
313 .admin_up_down_function = virtio_interface_admin_up_down,
314 .subif_add_del_function = virtio_subif_add_del_function,
315 .rx_mode_change_function = virtio_interface_rx_mode_change,
318 VLIB_DEVICE_TX_FUNCTION_MULTIARCH(virtio_device_class,
323 * fd.io coding-style-patch-verification: ON
326 * eval: (c-set-style "gnu")