1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
11 #include <rte_ether.h>
13 #include <rte_vhost.h>
18 #include <rte_spinlock.h>
19 #include <rte_malloc.h>
24 #define MAX_PKT_BURST 32
26 #define MAX_BATCH_LEN 256
29 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
31 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
34 static __rte_always_inline struct vring_desc *
35 alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
36 struct vring_desc *desc)
38 struct vring_desc *idesc;
40 uint64_t len, remain = desc->len;
41 uint64_t desc_addr = desc->addr;
43 idesc = rte_malloc(__func__, desc->len, 0);
47 dst = (uint64_t)(uintptr_t)idesc;
51 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
53 if (unlikely(!src || !len)) {
58 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
68 static __rte_always_inline void
69 free_ind_table(struct vring_desc *idesc)
74 static __rte_always_inline void
75 do_flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
76 uint16_t to, uint16_t from, uint16_t size)
78 rte_memcpy(&vq->used->ring[to],
79 &vq->shadow_used_ring[from],
80 size * sizeof(struct vring_used_elem));
81 vhost_log_cache_used_vring(dev, vq,
82 offsetof(struct vring_used, ring[to]),
83 size * sizeof(struct vring_used_elem));
86 static __rte_always_inline void
87 flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq)
89 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
91 if (used_idx + vq->shadow_used_idx <= vq->size) {
92 do_flush_shadow_used_ring(dev, vq, used_idx, 0,
97 /* update used ring interval [used_idx, vq->size] */
98 size = vq->size - used_idx;
99 do_flush_shadow_used_ring(dev, vq, used_idx, 0, size);
101 /* update the left half used ring interval [0, left_size] */
102 do_flush_shadow_used_ring(dev, vq, 0, size,
103 vq->shadow_used_idx - size);
105 vq->last_used_idx += vq->shadow_used_idx;
109 vhost_log_cache_sync(dev, vq);
111 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
112 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
113 sizeof(vq->used->idx));
116 static __rte_always_inline void
117 update_shadow_used_ring(struct vhost_virtqueue *vq,
118 uint16_t desc_idx, uint16_t len)
120 uint16_t i = vq->shadow_used_idx++;
122 vq->shadow_used_ring[i].id = desc_idx;
123 vq->shadow_used_ring[i].len = len;
127 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
129 struct batch_copy_elem *elem = vq->batch_copy_elems;
130 uint16_t count = vq->batch_copy_nb_elems;
133 for (i = 0; i < count; i++) {
134 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
135 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
136 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
141 do_data_copy_dequeue(struct vhost_virtqueue *vq)
143 struct batch_copy_elem *elem = vq->batch_copy_elems;
144 uint16_t count = vq->batch_copy_nb_elems;
147 for (i = 0; i < count; i++)
148 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
151 /* avoid write operation when necessary, to lessen cache issues */
152 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
153 if ((var) != (val)) \
158 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
160 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
162 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
163 csum_l4 |= PKT_TX_TCP_CKSUM;
166 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
167 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
170 case PKT_TX_TCP_CKSUM:
171 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
174 case PKT_TX_UDP_CKSUM:
175 net_hdr->csum_offset = (offsetof(struct udp_hdr,
178 case PKT_TX_SCTP_CKSUM:
179 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
184 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
185 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
186 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
189 /* IP cksum verification cannot be bypassed, then calculate here */
190 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
191 struct ipv4_hdr *ipv4_hdr;
193 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
195 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
198 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
199 if (m_buf->ol_flags & PKT_TX_IPV4)
200 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
202 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
203 net_hdr->gso_size = m_buf->tso_segsz;
204 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
206 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
207 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
208 net_hdr->gso_size = m_buf->tso_segsz;
209 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
212 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
213 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
214 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
218 static __rte_always_inline int
219 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
220 struct vring_desc *descs, struct rte_mbuf *m,
221 uint16_t desc_idx, uint32_t size)
223 uint32_t desc_avail, desc_offset;
224 uint32_t mbuf_avail, mbuf_offset;
226 uint64_t desc_chunck_len;
227 struct vring_desc *desc;
228 uint64_t desc_addr, desc_gaddr;
229 /* A counter to avoid desc dead loop chain */
230 uint16_t nr_desc = 1;
231 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
232 uint16_t copy_nb = vq->batch_copy_nb_elems;
235 desc = &descs[desc_idx];
236 desc_chunck_len = desc->len;
237 desc_gaddr = desc->addr;
238 desc_addr = vhost_iova_to_vva(dev, vq, desc_gaddr,
239 &desc_chunck_len, VHOST_ACCESS_RW);
241 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
242 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
243 * otherwise stores offset on the stack instead of in a register.
245 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr) {
250 rte_prefetch0((void *)(uintptr_t)desc_addr);
252 if (likely(desc_chunck_len >= dev->vhost_hlen)) {
253 virtio_enqueue_offload(m,
254 (struct virtio_net_hdr *)(uintptr_t)desc_addr);
255 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
256 vhost_log_cache_write(dev, vq, desc_gaddr, dev->vhost_hlen);
258 struct virtio_net_hdr vnet_hdr;
259 uint64_t remain = dev->vhost_hlen;
261 uint64_t src = (uint64_t)(uintptr_t)&vnet_hdr, dst;
262 uint64_t guest_addr = desc_gaddr;
264 virtio_enqueue_offload(m, &vnet_hdr);
268 dst = vhost_iova_to_vva(dev, vq, guest_addr,
269 &len, VHOST_ACCESS_RW);
270 if (unlikely(!dst || !len)) {
275 rte_memcpy((void *)(uintptr_t)dst,
276 (void *)(uintptr_t)src, len);
278 PRINT_PACKET(dev, (uintptr_t)dst, (uint32_t)len, 0);
279 vhost_log_cache_write(dev, vq, guest_addr, len);
286 desc_avail = desc->len - dev->vhost_hlen;
287 if (unlikely(desc_chunck_len < dev->vhost_hlen)) {
288 desc_chunck_len = desc_avail;
289 desc_gaddr = desc->addr + dev->vhost_hlen;
290 desc_addr = vhost_iova_to_vva(dev,
294 if (unlikely(!desc_addr)) {
301 desc_offset = dev->vhost_hlen;
302 desc_chunck_len -= dev->vhost_hlen;
305 mbuf_avail = rte_pktmbuf_data_len(m);
307 while (mbuf_avail != 0 || m->next != NULL) {
308 /* done with current mbuf, fetch next */
309 if (mbuf_avail == 0) {
313 mbuf_avail = rte_pktmbuf_data_len(m);
316 /* done with current desc buf, fetch next */
317 if (desc_avail == 0) {
318 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
319 /* Room in vring buffer is not enough */
323 if (unlikely(desc->next >= size || ++nr_desc > size)) {
328 desc = &descs[desc->next];
329 desc_chunck_len = desc->len;
330 desc_gaddr = desc->addr;
331 desc_addr = vhost_iova_to_vva(dev, vq, desc_gaddr,
334 if (unlikely(!desc_addr)) {
340 desc_avail = desc->len;
341 } else if (unlikely(desc_chunck_len == 0)) {
342 desc_chunck_len = desc_avail;
343 desc_gaddr += desc_offset;
344 desc_addr = vhost_iova_to_vva(dev,
346 &desc_chunck_len, VHOST_ACCESS_RW);
347 if (unlikely(!desc_addr)) {
354 cpy_len = RTE_MIN(desc_chunck_len, mbuf_avail);
355 if (likely(cpy_len > MAX_BATCH_LEN || copy_nb >= vq->size)) {
356 rte_memcpy((void *)((uintptr_t)(desc_addr +
358 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
360 vhost_log_cache_write(dev, vq, desc_gaddr + desc_offset,
362 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
365 batch_copy[copy_nb].dst =
366 (void *)((uintptr_t)(desc_addr + desc_offset));
367 batch_copy[copy_nb].src =
368 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
369 batch_copy[copy_nb].log_addr = desc_gaddr + desc_offset;
370 batch_copy[copy_nb].len = cpy_len;
374 mbuf_avail -= cpy_len;
375 mbuf_offset += cpy_len;
376 desc_avail -= cpy_len;
377 desc_offset += cpy_len;
378 desc_chunck_len -= cpy_len;
382 vq->batch_copy_nb_elems = copy_nb;
388 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
389 * be received from the physical port or from another virtio device. A packet
390 * count is returned to indicate the number of packets that are successfully
391 * added to the RX queue. This function works when the mbuf is scattered, but
392 * it doesn't support the mergeable feature.
394 static __rte_always_inline uint32_t
395 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
396 struct rte_mbuf **pkts, uint32_t count)
398 struct vhost_virtqueue *vq;
399 uint16_t avail_idx, free_entries, start_idx;
400 uint16_t desc_indexes[MAX_PKT_BURST];
401 struct vring_desc *descs;
405 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
406 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
407 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
408 dev->vid, __func__, queue_id);
412 vq = dev->virtqueue[queue_id];
414 rte_spinlock_lock(&vq->access_lock);
416 if (unlikely(vq->enabled == 0))
417 goto out_access_unlock;
419 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
420 vhost_user_iotlb_rd_lock(vq);
422 if (unlikely(vq->access_ok == 0)) {
423 if (unlikely(vring_translate(dev, vq) < 0)) {
429 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
430 start_idx = vq->last_used_idx;
431 free_entries = avail_idx - start_idx;
432 count = RTE_MIN(count, free_entries);
433 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
437 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
438 dev->vid, start_idx, start_idx + count);
440 vq->batch_copy_nb_elems = 0;
442 /* Retrieve all of the desc indexes first to avoid caching issues. */
443 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
444 for (i = 0; i < count; i++) {
445 used_idx = (start_idx + i) & (vq->size - 1);
446 desc_indexes[i] = vq->avail->ring[used_idx];
447 vq->used->ring[used_idx].id = desc_indexes[i];
448 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
450 vhost_log_cache_used_vring(dev, vq,
451 offsetof(struct vring_used, ring[used_idx]),
452 sizeof(vq->used->ring[used_idx]));
455 rte_prefetch0(&vq->desc[desc_indexes[0]]);
456 for (i = 0; i < count; i++) {
457 struct vring_desc *idesc = NULL;
458 uint16_t desc_idx = desc_indexes[i];
461 if (vq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT) {
462 uint64_t dlen = vq->desc[desc_idx].len;
463 descs = (struct vring_desc *)(uintptr_t)
464 vhost_iova_to_vva(dev,
465 vq, vq->desc[desc_idx].addr,
466 &dlen, VHOST_ACCESS_RO);
467 if (unlikely(!descs)) {
472 if (unlikely(dlen < vq->desc[desc_idx].len)) {
474 * The indirect desc table is not contiguous
475 * in process VA space, we have to copy it.
477 idesc = alloc_copy_ind_table(dev, vq,
478 &vq->desc[desc_idx]);
479 if (unlikely(!idesc))
486 sz = vq->desc[desc_idx].len / sizeof(*descs);
492 err = copy_mbuf_to_desc(dev, vq, descs, pkts[i], desc_idx, sz);
495 free_ind_table(idesc);
500 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
502 if (unlikely(!!idesc))
503 free_ind_table(idesc);
506 do_data_copy_enqueue(dev, vq);
510 vhost_log_cache_sync(dev, vq);
512 *(volatile uint16_t *)&vq->used->idx += count;
513 vq->last_used_idx += count;
514 vhost_log_used_vring(dev, vq,
515 offsetof(struct vring_used, idx),
516 sizeof(vq->used->idx));
518 vhost_vring_call(dev, vq);
520 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
521 vhost_user_iotlb_rd_unlock(vq);
524 rte_spinlock_unlock(&vq->access_lock);
529 static __rte_always_inline int
530 fill_vec_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
531 uint32_t avail_idx, uint32_t *vec_idx,
532 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
533 uint16_t *desc_chain_len)
535 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
536 uint32_t vec_id = *vec_idx;
539 struct vring_desc *descs = vq->desc;
540 struct vring_desc *idesc = NULL;
542 *desc_chain_head = idx;
544 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
545 dlen = vq->desc[idx].len;
546 descs = (struct vring_desc *)(uintptr_t)
547 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
550 if (unlikely(!descs))
553 if (unlikely(dlen < vq->desc[idx].len)) {
555 * The indirect desc table is not contiguous
556 * in process VA space, we have to copy it.
558 idesc = alloc_copy_ind_table(dev, vq, &vq->desc[idx]);
559 if (unlikely(!idesc))
569 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size)) {
570 free_ind_table(idesc);
574 len += descs[idx].len;
575 buf_vec[vec_id].buf_addr = descs[idx].addr;
576 buf_vec[vec_id].buf_len = descs[idx].len;
577 buf_vec[vec_id].desc_idx = idx;
580 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
583 idx = descs[idx].next;
586 *desc_chain_len = len;
589 if (unlikely(!!idesc))
590 free_ind_table(idesc);
596 * Returns -1 on fail, 0 on success
599 reserve_avail_buf_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
600 uint32_t size, struct buf_vector *buf_vec,
601 uint16_t *num_buffers, uint16_t avail_head)
604 uint32_t vec_idx = 0;
607 uint16_t head_idx = 0;
611 cur_idx = vq->last_avail_idx;
614 if (unlikely(cur_idx == avail_head))
617 if (unlikely(fill_vec_buf(dev, vq, cur_idx, &vec_idx, buf_vec,
618 &head_idx, &len) < 0))
620 len = RTE_MIN(len, size);
621 update_shadow_used_ring(vq, head_idx, len);
629 * if we tried all available ring items, and still
630 * can't get enough buf, it means something abnormal
633 if (unlikely(tries >= vq->size))
640 static __rte_always_inline int
641 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
642 struct rte_mbuf *m, struct buf_vector *buf_vec,
643 uint16_t num_buffers)
645 uint32_t vec_idx = 0;
646 uint64_t desc_addr, desc_gaddr;
647 uint32_t mbuf_offset, mbuf_avail;
648 uint32_t desc_offset, desc_avail;
650 uint64_t desc_chunck_len;
651 uint64_t hdr_addr, hdr_phys_addr;
652 struct rte_mbuf *hdr_mbuf;
653 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
654 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
655 uint16_t copy_nb = vq->batch_copy_nb_elems;
658 if (unlikely(m == NULL)) {
663 desc_chunck_len = buf_vec[vec_idx].buf_len;
664 desc_gaddr = buf_vec[vec_idx].buf_addr;
665 desc_addr = vhost_iova_to_vva(dev, vq,
669 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) {
675 hdr_addr = desc_addr;
676 if (unlikely(desc_chunck_len < dev->vhost_hlen))
679 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
680 hdr_phys_addr = desc_gaddr;
681 rte_prefetch0((void *)(uintptr_t)hdr_addr);
683 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
684 dev->vid, num_buffers);
686 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
687 if (unlikely(desc_chunck_len < dev->vhost_hlen)) {
688 desc_chunck_len = desc_avail;
689 desc_gaddr += dev->vhost_hlen;
690 desc_addr = vhost_iova_to_vva(dev, vq,
694 if (unlikely(!desc_addr)) {
701 desc_offset = dev->vhost_hlen;
702 desc_chunck_len -= dev->vhost_hlen;
706 mbuf_avail = rte_pktmbuf_data_len(m);
708 while (mbuf_avail != 0 || m->next != NULL) {
709 /* done with current desc buf, get the next one */
710 if (desc_avail == 0) {
712 desc_chunck_len = buf_vec[vec_idx].buf_len;
713 desc_gaddr = buf_vec[vec_idx].buf_addr;
715 vhost_iova_to_vva(dev, vq,
719 if (unlikely(!desc_addr)) {
724 /* Prefetch buffer address. */
725 rte_prefetch0((void *)(uintptr_t)desc_addr);
727 desc_avail = buf_vec[vec_idx].buf_len;
728 } else if (unlikely(desc_chunck_len == 0)) {
729 desc_chunck_len = desc_avail;
730 desc_gaddr += desc_offset;
731 desc_addr = vhost_iova_to_vva(dev, vq,
733 &desc_chunck_len, VHOST_ACCESS_RW);
734 if (unlikely(!desc_addr)) {
741 /* done with current mbuf, get the next one */
742 if (mbuf_avail == 0) {
746 mbuf_avail = rte_pktmbuf_data_len(m);
750 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
751 ASSIGN_UNLESS_EQUAL(hdr->num_buffers, num_buffers);
753 if (unlikely(hdr == &tmp_hdr)) {
755 uint64_t remain = dev->vhost_hlen;
756 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
757 uint64_t guest_addr = hdr_phys_addr;
761 dst = vhost_iova_to_vva(dev, vq,
764 if (unlikely(!dst || !len)) {
769 rte_memcpy((void *)(uintptr_t)dst,
770 (void *)(uintptr_t)src,
773 PRINT_PACKET(dev, (uintptr_t)dst,
775 vhost_log_cache_write(dev, vq,
783 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
785 vhost_log_cache_write(dev, vq, hdr_phys_addr,
792 cpy_len = RTE_MIN(desc_chunck_len, mbuf_avail);
794 if (likely(cpy_len > MAX_BATCH_LEN || copy_nb >= vq->size)) {
795 rte_memcpy((void *)((uintptr_t)(desc_addr +
797 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
799 vhost_log_cache_write(dev, vq, desc_gaddr + desc_offset,
801 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
804 batch_copy[copy_nb].dst =
805 (void *)((uintptr_t)(desc_addr + desc_offset));
806 batch_copy[copy_nb].src =
807 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
808 batch_copy[copy_nb].log_addr = desc_gaddr + desc_offset;
809 batch_copy[copy_nb].len = cpy_len;
813 mbuf_avail -= cpy_len;
814 mbuf_offset += cpy_len;
815 desc_avail -= cpy_len;
816 desc_offset += cpy_len;
817 desc_chunck_len -= cpy_len;
821 vq->batch_copy_nb_elems = copy_nb;
826 static __rte_always_inline uint32_t
827 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
828 struct rte_mbuf **pkts, uint32_t count)
830 struct vhost_virtqueue *vq;
831 uint32_t pkt_idx = 0;
832 uint16_t num_buffers;
833 struct buf_vector buf_vec[BUF_VECTOR_MAX];
836 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
837 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
838 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
839 dev->vid, __func__, queue_id);
843 vq = dev->virtqueue[queue_id];
845 rte_spinlock_lock(&vq->access_lock);
847 if (unlikely(vq->enabled == 0))
848 goto out_access_unlock;
850 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
851 vhost_user_iotlb_rd_lock(vq);
853 if (unlikely(vq->access_ok == 0))
854 if (unlikely(vring_translate(dev, vq) < 0))
857 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
861 vq->batch_copy_nb_elems = 0;
863 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
865 vq->shadow_used_idx = 0;
866 avail_head = *((volatile uint16_t *)&vq->avail->idx);
867 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
868 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
870 if (unlikely(reserve_avail_buf_mergeable(dev, vq,
871 pkt_len, buf_vec, &num_buffers,
873 VHOST_LOG_DEBUG(VHOST_DATA,
874 "(%d) failed to get enough desc from vring\n",
876 vq->shadow_used_idx -= num_buffers;
880 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
881 dev->vid, vq->last_avail_idx,
882 vq->last_avail_idx + num_buffers);
884 if (copy_mbuf_to_desc_mergeable(dev, vq, pkts[pkt_idx],
885 buf_vec, num_buffers) < 0) {
886 vq->shadow_used_idx -= num_buffers;
890 vq->last_avail_idx += num_buffers;
893 do_data_copy_enqueue(dev, vq);
895 if (likely(vq->shadow_used_idx)) {
896 flush_shadow_used_ring(dev, vq);
897 vhost_vring_call(dev, vq);
901 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
902 vhost_user_iotlb_rd_unlock(vq);
905 rte_spinlock_unlock(&vq->access_lock);
911 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
912 struct rte_mbuf **pkts, uint16_t count)
914 struct virtio_net *dev = get_device(vid);
919 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
920 RTE_LOG(ERR, VHOST_DATA,
921 "(%d) %s: built-in vhost net backend is disabled.\n",
926 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
927 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
929 return virtio_dev_rx(dev, queue_id, pkts, count);
933 virtio_net_with_host_offload(struct virtio_net *dev)
936 ((1ULL << VIRTIO_NET_F_CSUM) |
937 (1ULL << VIRTIO_NET_F_HOST_ECN) |
938 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
939 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
940 (1ULL << VIRTIO_NET_F_HOST_UFO)))
947 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
949 struct ipv4_hdr *ipv4_hdr;
950 struct ipv6_hdr *ipv6_hdr;
952 struct ether_hdr *eth_hdr;
955 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
957 m->l2_len = sizeof(struct ether_hdr);
958 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
960 if (ethertype == ETHER_TYPE_VLAN) {
961 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
963 m->l2_len += sizeof(struct vlan_hdr);
964 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
967 l3_hdr = (char *)eth_hdr + m->l2_len;
970 case ETHER_TYPE_IPv4:
972 *l4_proto = ipv4_hdr->next_proto_id;
973 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
974 *l4_hdr = (char *)l3_hdr + m->l3_len;
975 m->ol_flags |= PKT_TX_IPV4;
977 case ETHER_TYPE_IPv6:
979 *l4_proto = ipv6_hdr->proto;
980 m->l3_len = sizeof(struct ipv6_hdr);
981 *l4_hdr = (char *)l3_hdr + m->l3_len;
982 m->ol_flags |= PKT_TX_IPV6;
992 static __rte_always_inline void
993 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
995 uint16_t l4_proto = 0;
997 struct tcp_hdr *tcp_hdr = NULL;
999 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1002 parse_ethernet(m, &l4_proto, &l4_hdr);
1003 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1004 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1005 switch (hdr->csum_offset) {
1006 case (offsetof(struct tcp_hdr, cksum)):
1007 if (l4_proto == IPPROTO_TCP)
1008 m->ol_flags |= PKT_TX_TCP_CKSUM;
1010 case (offsetof(struct udp_hdr, dgram_cksum)):
1011 if (l4_proto == IPPROTO_UDP)
1012 m->ol_flags |= PKT_TX_UDP_CKSUM;
1014 case (offsetof(struct sctp_hdr, cksum)):
1015 if (l4_proto == IPPROTO_SCTP)
1016 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1024 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1025 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1026 case VIRTIO_NET_HDR_GSO_TCPV4:
1027 case VIRTIO_NET_HDR_GSO_TCPV6:
1029 m->ol_flags |= PKT_TX_TCP_SEG;
1030 m->tso_segsz = hdr->gso_size;
1031 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1033 case VIRTIO_NET_HDR_GSO_UDP:
1034 m->ol_flags |= PKT_TX_UDP_SEG;
1035 m->tso_segsz = hdr->gso_size;
1036 m->l4_len = sizeof(struct udp_hdr);
1039 RTE_LOG(WARNING, VHOST_DATA,
1040 "unsupported gso type %u.\n", hdr->gso_type);
1046 static __rte_always_inline void
1047 put_zmbuf(struct zcopy_mbuf *zmbuf)
1052 static __rte_always_inline int
1053 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1054 struct vring_desc *descs, uint16_t max_desc,
1055 struct rte_mbuf *m, uint16_t desc_idx,
1056 struct rte_mempool *mbuf_pool)
1058 struct vring_desc *desc;
1059 uint64_t desc_addr, desc_gaddr;
1060 uint32_t desc_avail, desc_offset;
1061 uint32_t mbuf_avail, mbuf_offset;
1063 uint64_t desc_chunck_len;
1064 struct rte_mbuf *cur = m, *prev = m;
1065 struct virtio_net_hdr tmp_hdr;
1066 struct virtio_net_hdr *hdr = NULL;
1067 /* A counter to avoid desc dead loop chain */
1068 uint32_t nr_desc = 1;
1069 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1070 uint16_t copy_nb = vq->batch_copy_nb_elems;
1073 desc = &descs[desc_idx];
1074 if (unlikely((desc->len < dev->vhost_hlen)) ||
1075 (desc->flags & VRING_DESC_F_INDIRECT)) {
1080 desc_chunck_len = desc->len;
1081 desc_gaddr = desc->addr;
1082 desc_addr = vhost_iova_to_vva(dev,
1086 if (unlikely(!desc_addr)) {
1091 if (virtio_net_with_host_offload(dev)) {
1092 if (unlikely(desc_chunck_len < sizeof(struct virtio_net_hdr))) {
1093 uint64_t len = desc_chunck_len;
1094 uint64_t remain = sizeof(struct virtio_net_hdr);
1095 uint64_t src = desc_addr;
1096 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
1097 uint64_t guest_addr = desc_gaddr;
1100 * No luck, the virtio-net header doesn't fit
1101 * in a contiguous virtual area.
1105 src = vhost_iova_to_vva(dev, vq,
1108 if (unlikely(!src || !len)) {
1113 rte_memcpy((void *)(uintptr_t)dst,
1114 (void *)(uintptr_t)src, len);
1123 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
1129 * A virtio driver normally uses at least 2 desc buffers
1130 * for Tx: the first for storing the header, and others
1131 * for storing the data.
1133 if (likely((desc->len == dev->vhost_hlen) &&
1134 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
1135 desc = &descs[desc->next];
1136 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT)) {
1141 desc_chunck_len = desc->len;
1142 desc_gaddr = desc->addr;
1143 desc_addr = vhost_iova_to_vva(dev,
1147 if (unlikely(!desc_addr)) {
1153 desc_avail = desc->len;
1156 desc_avail = desc->len - dev->vhost_hlen;
1158 if (unlikely(desc_chunck_len < dev->vhost_hlen)) {
1159 desc_chunck_len = desc_avail;
1160 desc_gaddr += dev->vhost_hlen;
1161 desc_addr = vhost_iova_to_vva(dev,
1165 if (unlikely(!desc_addr)) {
1172 desc_offset = dev->vhost_hlen;
1173 desc_chunck_len -= dev->vhost_hlen;
1177 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
1179 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
1180 (uint32_t)desc_chunck_len, 0);
1183 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1187 cpy_len = RTE_MIN(desc_chunck_len, mbuf_avail);
1190 * A desc buf might across two host physical pages that are
1191 * not continuous. In such case (gpa_to_hpa returns 0), data
1192 * will be copied even though zero copy is enabled.
1194 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1195 desc_gaddr + desc_offset, cpy_len)))) {
1196 cur->data_len = cpy_len;
1198 cur->buf_addr = (void *)(uintptr_t)(desc_addr
1200 cur->buf_iova = hpa;
1203 * In zero copy mode, one mbuf can only reference data
1204 * for one or partial of one desc buff.
1206 mbuf_avail = cpy_len;
1208 if (likely(cpy_len > MAX_BATCH_LEN ||
1209 copy_nb >= vq->size ||
1210 (hdr && cur == m) ||
1211 desc->len != desc_chunck_len)) {
1212 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1214 (void *)((uintptr_t)(desc_addr +
1218 batch_copy[copy_nb].dst =
1219 rte_pktmbuf_mtod_offset(cur, void *,
1221 batch_copy[copy_nb].src =
1222 (void *)((uintptr_t)(desc_addr +
1224 batch_copy[copy_nb].len = cpy_len;
1229 mbuf_avail -= cpy_len;
1230 mbuf_offset += cpy_len;
1231 desc_avail -= cpy_len;
1232 desc_chunck_len -= cpy_len;
1233 desc_offset += cpy_len;
1235 /* This desc reaches to its end, get the next one */
1236 if (desc_avail == 0) {
1237 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
1240 if (unlikely(desc->next >= max_desc ||
1241 ++nr_desc > max_desc)) {
1245 desc = &descs[desc->next];
1246 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT)) {
1251 desc_chunck_len = desc->len;
1252 desc_gaddr = desc->addr;
1253 desc_addr = vhost_iova_to_vva(dev,
1257 if (unlikely(!desc_addr)) {
1262 rte_prefetch0((void *)(uintptr_t)desc_addr);
1265 desc_avail = desc->len;
1267 PRINT_PACKET(dev, (uintptr_t)desc_addr,
1268 (uint32_t)desc_chunck_len, 0);
1269 } else if (unlikely(desc_chunck_len == 0)) {
1270 desc_chunck_len = desc_avail;
1271 desc_gaddr += desc_offset;
1272 desc_addr = vhost_iova_to_vva(dev, vq,
1276 if (unlikely(!desc_addr)) {
1282 PRINT_PACKET(dev, (uintptr_t)desc_addr,
1283 (uint32_t)desc_chunck_len, 0);
1287 * This mbuf reaches to its end, get a new one
1288 * to hold more data.
1290 if (mbuf_avail == 0) {
1291 cur = rte_pktmbuf_alloc(mbuf_pool);
1292 if (unlikely(cur == NULL)) {
1293 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1294 "allocate memory for mbuf.\n");
1298 if (unlikely(dev->dequeue_zero_copy))
1299 rte_mbuf_refcnt_update(cur, 1);
1302 prev->data_len = mbuf_offset;
1304 m->pkt_len += mbuf_offset;
1308 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1312 prev->data_len = mbuf_offset;
1313 m->pkt_len += mbuf_offset;
1316 vhost_dequeue_offload(hdr, m);
1319 vq->batch_copy_nb_elems = copy_nb;
1324 static __rte_always_inline void
1325 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
1326 uint32_t used_idx, uint32_t desc_idx)
1328 vq->used->ring[used_idx].id = desc_idx;
1329 vq->used->ring[used_idx].len = 0;
1330 vhost_log_cache_used_vring(dev, vq,
1331 offsetof(struct vring_used, ring[used_idx]),
1332 sizeof(vq->used->ring[used_idx]));
1335 static __rte_always_inline void
1336 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
1339 if (unlikely(count == 0))
1345 vhost_log_cache_sync(dev, vq);
1347 vq->used->idx += count;
1348 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
1349 sizeof(vq->used->idx));
1350 vhost_vring_call(dev, vq);
1353 static __rte_always_inline struct zcopy_mbuf *
1354 get_zmbuf(struct vhost_virtqueue *vq)
1360 /* search [last_zmbuf_idx, zmbuf_size) */
1361 i = vq->last_zmbuf_idx;
1362 last = vq->zmbuf_size;
1365 for (; i < last; i++) {
1366 if (vq->zmbufs[i].in_use == 0) {
1367 vq->last_zmbuf_idx = i + 1;
1368 vq->zmbufs[i].in_use = 1;
1369 return &vq->zmbufs[i];
1375 /* search [0, last_zmbuf_idx) */
1377 last = vq->last_zmbuf_idx;
1384 static __rte_always_inline bool
1385 mbuf_is_consumed(struct rte_mbuf *m)
1388 if (rte_mbuf_refcnt_read(m) > 1)
1396 static __rte_always_inline void
1397 restore_mbuf(struct rte_mbuf *m)
1399 uint32_t mbuf_size, priv_size;
1402 priv_size = rte_pktmbuf_priv_size(m->pool);
1403 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1404 /* start of buffer is after mbuf structure and priv data */
1406 m->buf_addr = (char *)m + mbuf_size;
1407 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1413 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1414 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1416 struct virtio_net *dev;
1417 struct rte_mbuf *rarp_mbuf = NULL;
1418 struct vhost_virtqueue *vq;
1419 uint32_t desc_indexes[MAX_PKT_BURST];
1422 uint16_t free_entries;
1425 dev = get_device(vid);
1429 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1430 RTE_LOG(ERR, VHOST_DATA,
1431 "(%d) %s: built-in vhost net backend is disabled.\n",
1432 dev->vid, __func__);
1436 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1437 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1438 dev->vid, __func__, queue_id);
1442 vq = dev->virtqueue[queue_id];
1444 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1447 if (unlikely(vq->enabled == 0))
1448 goto out_access_unlock;
1450 vq->batch_copy_nb_elems = 0;
1452 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1453 vhost_user_iotlb_rd_lock(vq);
1455 if (unlikely(vq->access_ok == 0))
1456 if (unlikely(vring_translate(dev, vq) < 0))
1459 if (unlikely(dev->dequeue_zero_copy)) {
1460 struct zcopy_mbuf *zmbuf, *next;
1463 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1464 zmbuf != NULL; zmbuf = next) {
1465 next = TAILQ_NEXT(zmbuf, next);
1467 if (mbuf_is_consumed(zmbuf->mbuf)) {
1468 used_idx = vq->last_used_idx++ & (vq->size - 1);
1469 update_used_ring(dev, vq, used_idx,
1473 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1474 restore_mbuf(zmbuf->mbuf);
1475 rte_pktmbuf_free(zmbuf->mbuf);
1481 update_used_idx(dev, vq, nr_updated);
1485 * Construct a RARP broadcast packet, and inject it to the "pkts"
1486 * array, to looks like that guest actually send such packet.
1488 * Check user_send_rarp() for more information.
1490 * broadcast_rarp shares a cacheline in the virtio_net structure
1491 * with some fields that are accessed during enqueue and
1492 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1493 * result in false sharing between enqueue and dequeue.
1495 * Prevent unnecessary false sharing by reading broadcast_rarp first
1496 * and only performing cmpset if the read indicates it is likely to
1500 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1501 rte_atomic16_cmpset((volatile uint16_t *)
1502 &dev->broadcast_rarp.cnt, 1, 0))) {
1504 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1505 if (rarp_mbuf == NULL) {
1506 RTE_LOG(ERR, VHOST_DATA,
1507 "Failed to make RARP packet.\n");
1513 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1515 if (free_entries == 0)
1518 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1520 /* Prefetch available and used ring */
1521 avail_idx = vq->last_avail_idx & (vq->size - 1);
1522 used_idx = vq->last_used_idx & (vq->size - 1);
1523 rte_prefetch0(&vq->avail->ring[avail_idx]);
1524 rte_prefetch0(&vq->used->ring[used_idx]);
1526 count = RTE_MIN(count, MAX_PKT_BURST);
1527 count = RTE_MIN(count, free_entries);
1528 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1531 /* Retrieve all of the head indexes first to avoid caching issues. */
1532 for (i = 0; i < count; i++) {
1533 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1534 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1535 desc_indexes[i] = vq->avail->ring[avail_idx];
1537 if (likely(dev->dequeue_zero_copy == 0))
1538 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1541 /* Prefetch descriptor index. */
1542 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1543 for (i = 0; i < count; i++) {
1544 struct vring_desc *desc, *idesc = NULL;
1549 if (likely(i + 1 < count))
1550 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1552 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1553 dlen = vq->desc[desc_indexes[i]].len;
1554 desc = (struct vring_desc *)(uintptr_t)
1555 vhost_iova_to_vva(dev, vq,
1556 vq->desc[desc_indexes[i]].addr,
1559 if (unlikely(!desc))
1562 if (unlikely(dlen < vq->desc[desc_indexes[i]].len)) {
1564 * The indirect desc table is not contiguous
1565 * in process VA space, we have to copy it.
1567 idesc = alloc_copy_ind_table(dev, vq,
1568 &vq->desc[desc_indexes[i]]);
1569 if (unlikely(!idesc))
1575 rte_prefetch0(desc);
1576 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1581 idx = desc_indexes[i];
1584 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1585 if (unlikely(pkts[i] == NULL)) {
1586 RTE_LOG(ERR, VHOST_DATA,
1587 "Failed to allocate memory for mbuf.\n");
1588 free_ind_table(idesc);
1592 err = copy_desc_to_mbuf(dev, vq, desc, sz, pkts[i], idx,
1594 if (unlikely(err)) {
1595 rte_pktmbuf_free(pkts[i]);
1596 free_ind_table(idesc);
1600 if (unlikely(dev->dequeue_zero_copy)) {
1601 struct zcopy_mbuf *zmbuf;
1603 zmbuf = get_zmbuf(vq);
1605 rte_pktmbuf_free(pkts[i]);
1606 free_ind_table(idesc);
1609 zmbuf->mbuf = pkts[i];
1610 zmbuf->desc_idx = desc_indexes[i];
1613 * Pin lock the mbuf; we will check later to see
1614 * whether the mbuf is freed (when we are the last
1615 * user) or not. If that's the case, we then could
1616 * update the used ring safely.
1618 rte_mbuf_refcnt_update(pkts[i], 1);
1621 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1624 if (unlikely(!!idesc))
1625 free_ind_table(idesc);
1627 vq->last_avail_idx += i;
1629 if (likely(dev->dequeue_zero_copy == 0)) {
1630 do_data_copy_dequeue(vq);
1631 vq->last_used_idx += i;
1632 update_used_idx(dev, vq, i);
1636 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1637 vhost_user_iotlb_rd_unlock(vq);
1640 rte_spinlock_unlock(&vq->access_lock);
1642 if (unlikely(rarp_mbuf != NULL)) {
1644 * Inject it to the head of "pkts" array, so that switch's mac
1645 * learning table will get updated first.
1647 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1648 pkts[0] = rarp_mbuf;