1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
11 #include <rte_vhost.h>
16 * A very simple vhost-user net driver implementation, without
17 * any extra features being enabled, such as TSO and mrg-Rx.
21 vs_vhost_net_setup(struct vhost_dev *dev)
25 struct vhost_queue *queue;
28 RTE_LOG(INFO, VHOST_CONFIG,
29 "setting builtin vhost-user net driver\n");
31 rte_vhost_get_negotiated_features(vid, &dev->features);
32 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
33 dev->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
35 dev->hdr_len = sizeof(struct virtio_net_hdr);
37 ret = rte_vhost_get_mem_table(vid, &dev->mem);
39 RTE_LOG(ERR, VHOST_CONFIG, "Failed to get "
40 "VM memory layout for device(%d)\n", vid);
44 dev->nr_vrings = rte_vhost_get_vring_num(vid);
45 for (i = 0; i < dev->nr_vrings; i++) {
46 queue = &dev->queues[i];
48 queue->last_used_idx = 0;
49 queue->last_avail_idx = 0;
50 rte_vhost_get_vhost_vring(vid, i, &queue->vr);
55 vs_vhost_net_remove(struct vhost_dev *dev)
60 static __rte_always_inline int
61 enqueue_pkt(struct vhost_dev *dev, struct rte_vhost_vring *vr,
62 struct rte_mbuf *m, uint16_t desc_idx)
64 uint32_t desc_avail, desc_offset;
65 uint64_t desc_chunck_len;
66 uint32_t mbuf_avail, mbuf_offset;
68 struct vring_desc *desc;
69 uint64_t desc_addr, desc_gaddr;
70 struct virtio_net_hdr virtio_hdr = {0, 0, 0, 0, 0, 0};
71 /* A counter to avoid desc dead loop chain */
74 desc = &vr->desc[desc_idx];
75 desc_chunck_len = desc->len;
76 desc_gaddr = desc->addr;
77 desc_addr = rte_vhost_va_from_guest_pa(
78 dev->mem, desc_gaddr, &desc_chunck_len);
80 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
81 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
82 * otherwise stores offset on the stack instead of in a register.
84 if (unlikely(desc->len < dev->hdr_len) || !desc_addr)
87 rte_prefetch0((void *)(uintptr_t)desc_addr);
89 /* write virtio-net header */
90 if (likely(desc_chunck_len >= dev->hdr_len)) {
91 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = virtio_hdr;
92 desc_offset = dev->hdr_len;
95 uint64_t remain = dev->hdr_len;
96 uint64_t src = (uint64_t)(uintptr_t)&virtio_hdr, dst;
97 uint64_t guest_addr = desc_gaddr;
101 dst = rte_vhost_va_from_guest_pa(dev->mem,
103 if (unlikely(!dst || !len))
106 rte_memcpy((void *)(uintptr_t)dst,
107 (void *)(uintptr_t)src,
115 desc_chunck_len = desc->len - dev->hdr_len;
116 desc_gaddr += dev->hdr_len;
117 desc_addr = rte_vhost_va_from_guest_pa(
118 dev->mem, desc_gaddr,
120 if (unlikely(!desc_addr))
126 desc_avail = desc->len - dev->hdr_len;
128 mbuf_avail = rte_pktmbuf_data_len(m);
130 while (mbuf_avail != 0 || m->next != NULL) {
131 /* done with current mbuf, fetch next */
132 if (mbuf_avail == 0) {
136 mbuf_avail = rte_pktmbuf_data_len(m);
139 /* done with current desc buf, fetch next */
140 if (desc_avail == 0) {
141 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
142 /* Room in vring buffer is not enough */
145 if (unlikely(desc->next >= vr->size ||
146 ++nr_desc > vr->size))
149 desc = &vr->desc[desc->next];
150 desc_chunck_len = desc->len;
151 desc_gaddr = desc->addr;
152 desc_addr = rte_vhost_va_from_guest_pa(
153 dev->mem, desc_gaddr, &desc_chunck_len);
154 if (unlikely(!desc_addr))
158 desc_avail = desc->len;
159 } else if (unlikely(desc_chunck_len == 0)) {
160 desc_chunck_len = desc_avail;
161 desc_gaddr += desc_offset;
162 desc_addr = rte_vhost_va_from_guest_pa(dev->mem,
165 if (unlikely(!desc_addr))
171 cpy_len = RTE_MIN(desc_chunck_len, mbuf_avail);
172 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
173 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
176 mbuf_avail -= cpy_len;
177 mbuf_offset += cpy_len;
178 desc_avail -= cpy_len;
179 desc_offset += cpy_len;
180 desc_chunck_len -= cpy_len;
187 vs_enqueue_pkts(struct vhost_dev *dev, uint16_t queue_id,
188 struct rte_mbuf **pkts, uint32_t count)
190 struct vhost_queue *queue;
191 struct rte_vhost_vring *vr;
192 uint16_t avail_idx, free_entries, start_idx;
193 uint16_t desc_indexes[MAX_PKT_BURST];
197 queue = &dev->queues[queue_id];
200 avail_idx = __atomic_load_n(&vr->avail->idx, __ATOMIC_ACQUIRE);
201 start_idx = queue->last_used_idx;
202 free_entries = avail_idx - start_idx;
203 count = RTE_MIN(count, free_entries);
204 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
208 /* Retrieve all of the desc indexes first to avoid caching issues. */
209 rte_prefetch0(&vr->avail->ring[start_idx & (vr->size - 1)]);
210 for (i = 0; i < count; i++) {
211 used_idx = (start_idx + i) & (vr->size - 1);
212 desc_indexes[i] = vr->avail->ring[used_idx];
213 vr->used->ring[used_idx].id = desc_indexes[i];
214 vr->used->ring[used_idx].len = pkts[i]->pkt_len +
218 rte_prefetch0(&vr->desc[desc_indexes[0]]);
219 for (i = 0; i < count; i++) {
220 uint16_t desc_idx = desc_indexes[i];
223 err = enqueue_pkt(dev, vr, pkts[i], desc_idx);
225 used_idx = (start_idx + i) & (vr->size - 1);
226 vr->used->ring[used_idx].len = dev->hdr_len;
230 rte_prefetch0(&vr->desc[desc_indexes[i+1]]);
233 __atomic_add_fetch(&vr->used->idx, count, __ATOMIC_RELEASE);
234 queue->last_used_idx += count;
236 rte_vhost_vring_call(dev->vid, queue_id);
241 static __rte_always_inline int
242 dequeue_pkt(struct vhost_dev *dev, struct rte_vhost_vring *vr,
243 struct rte_mbuf *m, uint16_t desc_idx,
244 struct rte_mempool *mbuf_pool)
246 struct vring_desc *desc;
247 uint64_t desc_addr, desc_gaddr;
248 uint32_t desc_avail, desc_offset;
249 uint64_t desc_chunck_len;
250 uint32_t mbuf_avail, mbuf_offset;
252 struct rte_mbuf *cur = m, *prev = m;
253 /* A counter to avoid desc dead loop chain */
254 uint32_t nr_desc = 1;
256 desc = &vr->desc[desc_idx];
257 if (unlikely((desc->len < dev->hdr_len)) ||
258 (desc->flags & VRING_DESC_F_INDIRECT))
261 desc_chunck_len = desc->len;
262 desc_gaddr = desc->addr;
263 desc_addr = rte_vhost_va_from_guest_pa(
264 dev->mem, desc_gaddr, &desc_chunck_len);
265 if (unlikely(!desc_addr))
269 * We don't support ANY_LAYOUT, neither VERSION_1, meaning
270 * a Tx packet from guest must have 2 desc buffers at least:
271 * the first for storing the header and the others for
274 * And since we don't support TSO, we could simply skip the
277 desc = &vr->desc[desc->next];
278 desc_chunck_len = desc->len;
279 desc_gaddr = desc->addr;
280 desc_addr = rte_vhost_va_from_guest_pa(
281 dev->mem, desc_gaddr, &desc_chunck_len);
282 if (unlikely(!desc_addr))
284 rte_prefetch0((void *)(uintptr_t)desc_addr);
287 desc_avail = desc->len;
291 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
293 cpy_len = RTE_MIN(desc_chunck_len, mbuf_avail);
294 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
296 (void *)((uintptr_t)(desc_addr + desc_offset)),
299 mbuf_avail -= cpy_len;
300 mbuf_offset += cpy_len;
301 desc_avail -= cpy_len;
302 desc_offset += cpy_len;
303 desc_chunck_len -= cpy_len;
305 /* This desc reaches to its end, get the next one */
306 if (desc_avail == 0) {
307 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
310 if (unlikely(desc->next >= vr->size ||
311 ++nr_desc > vr->size))
313 desc = &vr->desc[desc->next];
315 desc_chunck_len = desc->len;
316 desc_gaddr = desc->addr;
317 desc_addr = rte_vhost_va_from_guest_pa(
318 dev->mem, desc_gaddr, &desc_chunck_len);
319 if (unlikely(!desc_addr))
321 rte_prefetch0((void *)(uintptr_t)desc_addr);
324 desc_avail = desc->len;
325 } else if (unlikely(desc_chunck_len == 0)) {
326 desc_chunck_len = desc_avail;
327 desc_gaddr += desc_offset;
328 desc_addr = rte_vhost_va_from_guest_pa(dev->mem,
331 if (unlikely(!desc_addr))
338 * This mbuf reaches to its end, get a new one
341 if (mbuf_avail == 0) {
342 cur = rte_pktmbuf_alloc(mbuf_pool);
343 if (unlikely(cur == NULL)) {
344 RTE_LOG(ERR, VHOST_DATA, "Failed to "
345 "allocate memory for mbuf.\n");
350 prev->data_len = mbuf_offset;
352 m->pkt_len += mbuf_offset;
356 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
360 prev->data_len = mbuf_offset;
361 m->pkt_len += mbuf_offset;
367 vs_dequeue_pkts(struct vhost_dev *dev, uint16_t queue_id,
368 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
370 struct vhost_queue *queue;
371 struct rte_vhost_vring *vr;
372 uint32_t desc_indexes[MAX_PKT_BURST];
375 uint16_t free_entries;
378 queue = &dev->queues[queue_id];
381 free_entries = __atomic_load_n(&vr->avail->idx, __ATOMIC_ACQUIRE) -
382 queue->last_avail_idx;
383 if (free_entries == 0)
386 /* Prefetch available and used ring */
387 avail_idx = queue->last_avail_idx & (vr->size - 1);
388 used_idx = queue->last_used_idx & (vr->size - 1);
389 rte_prefetch0(&vr->avail->ring[avail_idx]);
390 rte_prefetch0(&vr->used->ring[used_idx]);
392 count = RTE_MIN(count, MAX_PKT_BURST);
393 count = RTE_MIN(count, free_entries);
395 if (unlikely(count == 0))
399 * Retrieve all of the head indexes first and pre-update used entries
400 * to avoid caching issues.
402 for (i = 0; i < count; i++) {
403 avail_idx = (queue->last_avail_idx + i) & (vr->size - 1);
404 used_idx = (queue->last_used_idx + i) & (vr->size - 1);
405 desc_indexes[i] = vr->avail->ring[avail_idx];
407 vr->used->ring[used_idx].id = desc_indexes[i];
408 vr->used->ring[used_idx].len = 0;
411 /* Prefetch descriptor index. */
412 rte_prefetch0(&vr->desc[desc_indexes[0]]);
413 for (i = 0; i < count; i++) {
416 if (likely(i + 1 < count))
417 rte_prefetch0(&vr->desc[desc_indexes[i + 1]]);
419 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
420 if (unlikely(pkts[i] == NULL)) {
421 RTE_LOG(ERR, VHOST_DATA,
422 "Failed to allocate memory for mbuf.\n");
426 err = dequeue_pkt(dev, vr, pkts[i], desc_indexes[i], mbuf_pool);
428 rte_pktmbuf_free(pkts[i]);
434 queue->last_avail_idx += i;
435 queue->last_used_idx += i;
437 __atomic_add_fetch(&vr->used->idx, i, __ATOMIC_ACQ_REL);
439 rte_vhost_vring_call(dev->vid, queue_id);