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
28 static __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
31 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
35 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
37 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
40 static __rte_always_inline void *
41 alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
42 uint64_t desc_addr, uint64_t desc_len)
46 uint64_t len, remain = desc_len;
48 idesc = rte_malloc(__func__, desc_len, 0);
52 dst = (uint64_t)(uintptr_t)idesc;
56 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
58 if (unlikely(!src || !len)) {
63 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
73 static __rte_always_inline void
74 free_ind_table(void *idesc)
79 static __rte_always_inline void
80 do_flush_shadow_used_ring_split(struct virtio_net *dev,
81 struct vhost_virtqueue *vq,
82 uint16_t to, uint16_t from, uint16_t size)
84 rte_memcpy(&vq->used->ring[to],
85 &vq->shadow_used_split[from],
86 size * sizeof(struct vring_used_elem));
87 vhost_log_cache_used_vring(dev, vq,
88 offsetof(struct vring_used, ring[to]),
89 size * sizeof(struct vring_used_elem));
92 static __rte_always_inline void
93 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
95 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
97 if (used_idx + vq->shadow_used_idx <= vq->size) {
98 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
103 /* update used ring interval [used_idx, vq->size] */
104 size = vq->size - used_idx;
105 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
107 /* update the left half used ring interval [0, left_size] */
108 do_flush_shadow_used_ring_split(dev, vq, 0, size,
109 vq->shadow_used_idx - size);
111 vq->last_used_idx += vq->shadow_used_idx;
115 vhost_log_cache_sync(dev, vq);
117 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
118 vq->shadow_used_idx = 0;
119 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
120 sizeof(vq->used->idx));
123 static __rte_always_inline void
124 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
125 uint16_t desc_idx, uint16_t len)
127 uint16_t i = vq->shadow_used_idx++;
129 vq->shadow_used_split[i].id = desc_idx;
130 vq->shadow_used_split[i].len = len;
133 static __rte_always_inline void
134 flush_shadow_used_ring_packed(struct virtio_net *dev,
135 struct vhost_virtqueue *vq)
138 uint16_t used_idx = vq->last_used_idx;
140 /* Split loop in two to save memory barriers */
141 for (i = 0; i < vq->shadow_used_idx; i++) {
142 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
143 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
145 used_idx += vq->shadow_used_packed[i].count;
146 if (used_idx >= vq->size)
147 used_idx -= vq->size;
152 for (i = 0; i < vq->shadow_used_idx; i++) {
155 if (vq->shadow_used_packed[i].len)
156 flags = VRING_DESC_F_WRITE;
160 if (vq->used_wrap_counter) {
161 flags |= VRING_DESC_F_USED;
162 flags |= VRING_DESC_F_AVAIL;
164 flags &= ~VRING_DESC_F_USED;
165 flags &= ~VRING_DESC_F_AVAIL;
168 vq->desc_packed[vq->last_used_idx].flags = flags;
170 vhost_log_cache_used_vring(dev, vq,
172 sizeof(struct vring_packed_desc),
173 sizeof(struct vring_packed_desc));
175 vq->last_used_idx += vq->shadow_used_packed[i].count;
176 if (vq->last_used_idx >= vq->size) {
177 vq->used_wrap_counter ^= 1;
178 vq->last_used_idx -= vq->size;
183 vq->shadow_used_idx = 0;
184 vhost_log_cache_sync(dev, vq);
187 static __rte_always_inline void
188 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
189 uint16_t desc_idx, uint16_t len, uint16_t count)
191 uint16_t i = vq->shadow_used_idx++;
193 vq->shadow_used_packed[i].id = desc_idx;
194 vq->shadow_used_packed[i].len = len;
195 vq->shadow_used_packed[i].count = count;
199 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
201 struct batch_copy_elem *elem = vq->batch_copy_elems;
202 uint16_t count = vq->batch_copy_nb_elems;
205 for (i = 0; i < count; i++) {
206 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
207 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
208 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
211 vq->batch_copy_nb_elems = 0;
215 do_data_copy_dequeue(struct vhost_virtqueue *vq)
217 struct batch_copy_elem *elem = vq->batch_copy_elems;
218 uint16_t count = vq->batch_copy_nb_elems;
221 for (i = 0; i < count; i++)
222 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
224 vq->batch_copy_nb_elems = 0;
227 /* avoid write operation when necessary, to lessen cache issues */
228 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
229 if ((var) != (val)) \
233 static __rte_always_inline void
234 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
236 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
238 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
239 csum_l4 |= PKT_TX_TCP_CKSUM;
242 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
243 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
246 case PKT_TX_TCP_CKSUM:
247 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
250 case PKT_TX_UDP_CKSUM:
251 net_hdr->csum_offset = (offsetof(struct udp_hdr,
254 case PKT_TX_SCTP_CKSUM:
255 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
260 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
261 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
262 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
265 /* IP cksum verification cannot be bypassed, then calculate here */
266 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
267 struct ipv4_hdr *ipv4_hdr;
269 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
271 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
274 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
275 if (m_buf->ol_flags & PKT_TX_IPV4)
276 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
278 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
279 net_hdr->gso_size = m_buf->tso_segsz;
280 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
282 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
283 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
284 net_hdr->gso_size = m_buf->tso_segsz;
285 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
288 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
289 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
290 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
294 static __rte_always_inline int
295 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
296 struct buf_vector *buf_vec, uint16_t *vec_idx,
297 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
299 uint16_t vec_id = *vec_idx;
303 uint64_t desc_chunck_len = desc_len;
305 if (unlikely(vec_id >= BUF_VECTOR_MAX))
308 desc_addr = vhost_iova_to_vva(dev, vq,
312 if (unlikely(!desc_addr))
315 buf_vec[vec_id].buf_iova = desc_iova;
316 buf_vec[vec_id].buf_addr = desc_addr;
317 buf_vec[vec_id].buf_len = desc_chunck_len;
319 desc_len -= desc_chunck_len;
320 desc_iova += desc_chunck_len;
328 static __rte_always_inline int
329 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
330 uint32_t avail_idx, uint16_t *vec_idx,
331 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
332 uint16_t *desc_chain_len, uint8_t perm)
334 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
335 uint16_t vec_id = *vec_idx;
338 struct vring_desc *descs = vq->desc;
339 struct vring_desc *idesc = NULL;
341 *desc_chain_head = idx;
343 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
344 dlen = vq->desc[idx].len;
345 descs = (struct vring_desc *)(uintptr_t)
346 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
349 if (unlikely(!descs))
352 if (unlikely(dlen < vq->desc[idx].len)) {
354 * The indirect desc table is not contiguous
355 * in process VA space, we have to copy it.
357 idesc = alloc_copy_ind_table(dev, vq,
358 vq->desc[idx].addr, vq->desc[idx].len);
359 if (unlikely(!idesc))
369 if (unlikely(idx >= vq->size)) {
370 free_ind_table(idesc);
374 len += descs[idx].len;
376 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
377 descs[idx].addr, descs[idx].len,
379 free_ind_table(idesc);
383 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
386 idx = descs[idx].next;
389 *desc_chain_len = len;
392 if (unlikely(!!idesc))
393 free_ind_table(idesc);
399 * Returns -1 on fail, 0 on success
402 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
403 uint32_t size, struct buf_vector *buf_vec,
404 uint16_t *num_buffers, uint16_t avail_head,
408 uint16_t vec_idx = 0;
409 uint16_t max_tries, tries = 0;
411 uint16_t head_idx = 0;
415 cur_idx = vq->last_avail_idx;
417 if (rxvq_is_mergeable(dev))
418 max_tries = vq->size - 1;
423 if (unlikely(cur_idx == avail_head))
426 * if we tried all available ring items, and still
427 * can't get enough buf, it means something abnormal
430 if (unlikely(++tries > max_tries))
433 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
436 VHOST_ACCESS_RW) < 0))
438 len = RTE_MIN(len, size);
439 update_shadow_used_ring_split(vq, head_idx, len);
451 static __rte_always_inline int
452 fill_vec_buf_packed_indirect(struct virtio_net *dev,
453 struct vhost_virtqueue *vq,
454 struct vring_packed_desc *desc, uint16_t *vec_idx,
455 struct buf_vector *buf_vec, uint16_t *len, uint8_t perm)
459 uint16_t vec_id = *vec_idx;
461 struct vring_packed_desc *descs, *idescs = NULL;
464 descs = (struct vring_packed_desc *)(uintptr_t)
465 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
466 if (unlikely(!descs))
469 if (unlikely(dlen < desc->len)) {
471 * The indirect desc table is not contiguous
472 * in process VA space, we have to copy it.
474 idescs = alloc_copy_ind_table(dev, vq, desc->addr, desc->len);
475 if (unlikely(!idescs))
481 nr_descs = desc->len / sizeof(struct vring_packed_desc);
482 if (unlikely(nr_descs >= vq->size)) {
483 free_ind_table(idescs);
487 for (i = 0; i < nr_descs; i++) {
488 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
489 free_ind_table(idescs);
493 *len += descs[i].len;
494 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
495 descs[i].addr, descs[i].len,
501 if (unlikely(!!idescs))
502 free_ind_table(idescs);
507 static __rte_always_inline int
508 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
509 uint16_t avail_idx, uint16_t *desc_count,
510 struct buf_vector *buf_vec, uint16_t *vec_idx,
511 uint16_t *buf_id, uint16_t *len, uint8_t perm)
513 bool wrap_counter = vq->avail_wrap_counter;
514 struct vring_packed_desc *descs = vq->desc_packed;
515 uint16_t vec_id = *vec_idx;
517 if (avail_idx < vq->last_avail_idx)
520 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
526 if (unlikely(vec_id >= BUF_VECTOR_MAX))
530 *buf_id = descs[avail_idx].id;
532 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
533 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
539 *len += descs[avail_idx].len;
541 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
542 descs[avail_idx].addr,
543 descs[avail_idx].len,
548 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
551 if (++avail_idx >= vq->size) {
552 avail_idx -= vq->size;
563 * Returns -1 on fail, 0 on success
566 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
567 uint32_t size, struct buf_vector *buf_vec,
568 uint16_t *nr_vec, uint16_t *num_buffers,
572 uint16_t vec_idx = 0;
573 uint16_t max_tries, tries = 0;
580 avail_idx = vq->last_avail_idx;
582 if (rxvq_is_mergeable(dev))
583 max_tries = vq->size - 1;
589 * if we tried all available ring items, and still
590 * can't get enough buf, it means something abnormal
593 if (unlikely(++tries > max_tries))
596 if (unlikely(fill_vec_buf_packed(dev, vq,
597 avail_idx, &desc_count,
600 VHOST_ACCESS_RO) < 0))
603 len = RTE_MIN(len, size);
604 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
607 avail_idx += desc_count;
608 if (avail_idx >= vq->size)
609 avail_idx -= vq->size;
611 *nr_descs += desc_count;
620 static __rte_always_inline int
621 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
622 struct rte_mbuf *m, struct buf_vector *buf_vec,
623 uint16_t nr_vec, uint16_t num_buffers)
625 uint32_t vec_idx = 0;
626 uint32_t mbuf_offset, mbuf_avail;
627 uint32_t buf_offset, buf_avail;
628 uint64_t buf_addr, buf_iova, buf_len;
631 struct rte_mbuf *hdr_mbuf;
632 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
633 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
636 if (unlikely(m == NULL)) {
641 buf_addr = buf_vec[vec_idx].buf_addr;
642 buf_iova = buf_vec[vec_idx].buf_iova;
643 buf_len = buf_vec[vec_idx].buf_len;
646 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
648 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
655 if (unlikely(buf_len < dev->vhost_hlen))
658 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
660 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
661 dev->vid, num_buffers);
663 if (unlikely(buf_len < dev->vhost_hlen)) {
664 buf_offset = dev->vhost_hlen - buf_len;
666 buf_addr = buf_vec[vec_idx].buf_addr;
667 buf_iova = buf_vec[vec_idx].buf_iova;
668 buf_len = buf_vec[vec_idx].buf_len;
669 buf_avail = buf_len - buf_offset;
671 buf_offset = dev->vhost_hlen;
672 buf_avail = buf_len - dev->vhost_hlen;
675 mbuf_avail = rte_pktmbuf_data_len(m);
677 while (mbuf_avail != 0 || m->next != NULL) {
678 /* done with current buf, get the next one */
679 if (buf_avail == 0) {
681 if (unlikely(vec_idx >= nr_vec)) {
686 buf_addr = buf_vec[vec_idx].buf_addr;
687 buf_iova = buf_vec[vec_idx].buf_iova;
688 buf_len = buf_vec[vec_idx].buf_len;
690 /* Prefetch next buffer address. */
691 if (vec_idx + 1 < nr_vec)
692 rte_prefetch0((void *)(uintptr_t)
693 buf_vec[vec_idx + 1].buf_addr);
698 /* done with current mbuf, get the next one */
699 if (mbuf_avail == 0) {
703 mbuf_avail = rte_pktmbuf_data_len(m);
707 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
708 if (rxvq_is_mergeable(dev))
709 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
712 if (unlikely(hdr == &tmp_hdr)) {
714 uint64_t remain = dev->vhost_hlen;
715 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
716 uint64_t iova = buf_vec[0].buf_iova;
717 uint16_t hdr_vec_idx = 0;
720 len = RTE_MIN(remain,
721 buf_vec[hdr_vec_idx].buf_len);
722 dst = buf_vec[hdr_vec_idx].buf_addr;
723 rte_memcpy((void *)(uintptr_t)dst,
724 (void *)(uintptr_t)src,
727 PRINT_PACKET(dev, (uintptr_t)dst,
729 vhost_log_cache_write(dev, vq,
738 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
740 vhost_log_cache_write(dev, vq,
748 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
750 if (likely(cpy_len > MAX_BATCH_LEN ||
751 vq->batch_copy_nb_elems >= vq->size)) {
752 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
753 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
755 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
757 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
760 batch_copy[vq->batch_copy_nb_elems].dst =
761 (void *)((uintptr_t)(buf_addr + buf_offset));
762 batch_copy[vq->batch_copy_nb_elems].src =
763 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
764 batch_copy[vq->batch_copy_nb_elems].log_addr =
765 buf_iova + buf_offset;
766 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
767 vq->batch_copy_nb_elems++;
770 mbuf_avail -= cpy_len;
771 mbuf_offset += cpy_len;
772 buf_avail -= cpy_len;
773 buf_offset += cpy_len;
781 static __rte_always_inline uint32_t
782 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
783 struct rte_mbuf **pkts, uint32_t count)
785 uint32_t pkt_idx = 0;
786 uint16_t num_buffers;
787 struct buf_vector buf_vec[BUF_VECTOR_MAX];
790 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
791 avail_head = *((volatile uint16_t *)&vq->avail->idx);
793 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
794 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
797 if (unlikely(reserve_avail_buf_split(dev, vq,
798 pkt_len, buf_vec, &num_buffers,
799 avail_head, &nr_vec) < 0)) {
800 VHOST_LOG_DEBUG(VHOST_DATA,
801 "(%d) failed to get enough desc from vring\n",
803 vq->shadow_used_idx -= num_buffers;
807 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
809 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
810 dev->vid, vq->last_avail_idx,
811 vq->last_avail_idx + num_buffers);
813 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
816 vq->shadow_used_idx -= num_buffers;
820 vq->last_avail_idx += num_buffers;
823 do_data_copy_enqueue(dev, vq);
825 if (likely(vq->shadow_used_idx)) {
826 flush_shadow_used_ring_split(dev, vq);
827 vhost_vring_call_split(dev, vq);
833 static __rte_always_inline uint32_t
834 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
835 struct rte_mbuf **pkts, uint32_t count)
837 uint32_t pkt_idx = 0;
838 uint16_t num_buffers;
839 struct buf_vector buf_vec[BUF_VECTOR_MAX];
841 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
842 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
844 uint16_t nr_descs = 0;
846 if (unlikely(reserve_avail_buf_packed(dev, vq,
847 pkt_len, buf_vec, &nr_vec,
848 &num_buffers, &nr_descs) < 0)) {
849 VHOST_LOG_DEBUG(VHOST_DATA,
850 "(%d) failed to get enough desc from vring\n",
852 vq->shadow_used_idx -= num_buffers;
856 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
858 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
859 dev->vid, vq->last_avail_idx,
860 vq->last_avail_idx + num_buffers);
862 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
865 vq->shadow_used_idx -= num_buffers;
869 vq->last_avail_idx += nr_descs;
870 if (vq->last_avail_idx >= vq->size) {
871 vq->last_avail_idx -= vq->size;
872 vq->avail_wrap_counter ^= 1;
876 do_data_copy_enqueue(dev, vq);
878 if (likely(vq->shadow_used_idx)) {
879 flush_shadow_used_ring_packed(dev, vq);
880 vhost_vring_call_packed(dev, vq);
886 static __rte_always_inline uint32_t
887 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
888 struct rte_mbuf **pkts, uint32_t count)
890 struct vhost_virtqueue *vq;
893 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
894 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
895 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
896 dev->vid, __func__, queue_id);
900 vq = dev->virtqueue[queue_id];
902 rte_spinlock_lock(&vq->access_lock);
904 if (unlikely(vq->enabled == 0))
905 goto out_access_unlock;
907 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
908 vhost_user_iotlb_rd_lock(vq);
910 if (unlikely(vq->access_ok == 0))
911 if (unlikely(vring_translate(dev, vq) < 0))
914 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
918 if (vq_is_packed(dev))
919 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
921 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
924 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
925 vhost_user_iotlb_rd_unlock(vq);
928 rte_spinlock_unlock(&vq->access_lock);
934 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
935 struct rte_mbuf **pkts, uint16_t count)
937 struct virtio_net *dev = get_device(vid);
942 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
943 RTE_LOG(ERR, VHOST_DATA,
944 "(%d) %s: built-in vhost net backend is disabled.\n",
949 return virtio_dev_rx(dev, queue_id, pkts, count);
953 virtio_net_with_host_offload(struct virtio_net *dev)
956 ((1ULL << VIRTIO_NET_F_CSUM) |
957 (1ULL << VIRTIO_NET_F_HOST_ECN) |
958 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
959 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
960 (1ULL << VIRTIO_NET_F_HOST_UFO)))
967 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
969 struct ipv4_hdr *ipv4_hdr;
970 struct ipv6_hdr *ipv6_hdr;
972 struct ether_hdr *eth_hdr;
975 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
977 m->l2_len = sizeof(struct ether_hdr);
978 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
980 if (ethertype == ETHER_TYPE_VLAN) {
981 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
983 m->l2_len += sizeof(struct vlan_hdr);
984 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
987 l3_hdr = (char *)eth_hdr + m->l2_len;
990 case ETHER_TYPE_IPv4:
992 *l4_proto = ipv4_hdr->next_proto_id;
993 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
994 *l4_hdr = (char *)l3_hdr + m->l3_len;
995 m->ol_flags |= PKT_TX_IPV4;
997 case ETHER_TYPE_IPv6:
999 *l4_proto = ipv6_hdr->proto;
1000 m->l3_len = sizeof(struct ipv6_hdr);
1001 *l4_hdr = (char *)l3_hdr + m->l3_len;
1002 m->ol_flags |= PKT_TX_IPV6;
1012 static __rte_always_inline void
1013 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1015 uint16_t l4_proto = 0;
1016 void *l4_hdr = NULL;
1017 struct tcp_hdr *tcp_hdr = NULL;
1019 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1022 parse_ethernet(m, &l4_proto, &l4_hdr);
1023 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1024 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1025 switch (hdr->csum_offset) {
1026 case (offsetof(struct tcp_hdr, cksum)):
1027 if (l4_proto == IPPROTO_TCP)
1028 m->ol_flags |= PKT_TX_TCP_CKSUM;
1030 case (offsetof(struct udp_hdr, dgram_cksum)):
1031 if (l4_proto == IPPROTO_UDP)
1032 m->ol_flags |= PKT_TX_UDP_CKSUM;
1034 case (offsetof(struct sctp_hdr, cksum)):
1035 if (l4_proto == IPPROTO_SCTP)
1036 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1044 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1045 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1046 case VIRTIO_NET_HDR_GSO_TCPV4:
1047 case VIRTIO_NET_HDR_GSO_TCPV6:
1049 m->ol_flags |= PKT_TX_TCP_SEG;
1050 m->tso_segsz = hdr->gso_size;
1051 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1053 case VIRTIO_NET_HDR_GSO_UDP:
1054 m->ol_flags |= PKT_TX_UDP_SEG;
1055 m->tso_segsz = hdr->gso_size;
1056 m->l4_len = sizeof(struct udp_hdr);
1059 RTE_LOG(WARNING, VHOST_DATA,
1060 "unsupported gso type %u.\n", hdr->gso_type);
1066 static __rte_always_inline void
1067 put_zmbuf(struct zcopy_mbuf *zmbuf)
1072 static __rte_always_inline int
1073 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1074 struct buf_vector *buf_vec, uint16_t nr_vec,
1075 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1077 uint32_t buf_avail, buf_offset;
1078 uint64_t buf_addr, buf_iova, buf_len;
1079 uint32_t mbuf_avail, mbuf_offset;
1081 struct rte_mbuf *cur = m, *prev = m;
1082 struct virtio_net_hdr tmp_hdr;
1083 struct virtio_net_hdr *hdr = NULL;
1084 /* A counter to avoid desc dead loop chain */
1085 uint16_t vec_idx = 0;
1086 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1089 buf_addr = buf_vec[vec_idx].buf_addr;
1090 buf_iova = buf_vec[vec_idx].buf_iova;
1091 buf_len = buf_vec[vec_idx].buf_len;
1093 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1098 if (likely(nr_vec > 1))
1099 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
1101 if (virtio_net_with_host_offload(dev)) {
1102 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1104 uint64_t remain = sizeof(struct virtio_net_hdr);
1106 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
1107 uint16_t hdr_vec_idx = 0;
1110 * No luck, the virtio-net header doesn't fit
1111 * in a contiguous virtual area.
1114 len = RTE_MIN(remain,
1115 buf_vec[hdr_vec_idx].buf_len);
1116 src = buf_vec[hdr_vec_idx].buf_addr;
1117 rte_memcpy((void *)(uintptr_t)dst,
1118 (void *)(uintptr_t)src, len);
1127 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1133 * A virtio driver normally uses at least 2 desc buffers
1134 * for Tx: the first for storing the header, and others
1135 * for storing the data.
1137 if (unlikely(buf_len < dev->vhost_hlen)) {
1138 buf_offset = dev->vhost_hlen - buf_len;
1140 buf_addr = buf_vec[vec_idx].buf_addr;
1141 buf_iova = buf_vec[vec_idx].buf_iova;
1142 buf_len = buf_vec[vec_idx].buf_len;
1143 buf_avail = buf_len - buf_offset;
1144 } else if (buf_len == dev->vhost_hlen) {
1145 if (unlikely(++vec_idx >= nr_vec))
1147 buf_addr = buf_vec[vec_idx].buf_addr;
1148 buf_iova = buf_vec[vec_idx].buf_iova;
1149 buf_len = buf_vec[vec_idx].buf_len;
1152 buf_avail = buf_len;
1154 buf_offset = dev->vhost_hlen;
1155 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1158 rte_prefetch0((void *)(uintptr_t)
1159 (buf_addr + buf_offset));
1162 (uintptr_t)(buf_addr + buf_offset),
1163 (uint32_t)buf_avail, 0);
1166 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1170 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1173 * A desc buf might across two host physical pages that are
1174 * not continuous. In such case (gpa_to_hpa returns 0), data
1175 * will be copied even though zero copy is enabled.
1177 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1178 buf_iova + buf_offset, cpy_len)))) {
1179 cur->data_len = cpy_len;
1182 (void *)(uintptr_t)(buf_addr + buf_offset);
1183 cur->buf_iova = hpa;
1186 * In zero copy mode, one mbuf can only reference data
1187 * for one or partial of one desc buff.
1189 mbuf_avail = cpy_len;
1191 if (likely(cpy_len > MAX_BATCH_LEN ||
1192 vq->batch_copy_nb_elems >= vq->size ||
1193 (hdr && cur == m))) {
1194 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1196 (void *)((uintptr_t)(buf_addr +
1200 batch_copy[vq->batch_copy_nb_elems].dst =
1201 rte_pktmbuf_mtod_offset(cur, void *,
1203 batch_copy[vq->batch_copy_nb_elems].src =
1204 (void *)((uintptr_t)(buf_addr +
1206 batch_copy[vq->batch_copy_nb_elems].len =
1208 vq->batch_copy_nb_elems++;
1212 mbuf_avail -= cpy_len;
1213 mbuf_offset += cpy_len;
1214 buf_avail -= cpy_len;
1215 buf_offset += cpy_len;
1217 /* This buf reaches to its end, get the next one */
1218 if (buf_avail == 0) {
1219 if (++vec_idx >= nr_vec)
1222 buf_addr = buf_vec[vec_idx].buf_addr;
1223 buf_iova = buf_vec[vec_idx].buf_iova;
1224 buf_len = buf_vec[vec_idx].buf_len;
1227 * Prefecth desc n + 1 buffer while
1228 * desc n buffer is processed.
1230 if (vec_idx + 1 < nr_vec)
1231 rte_prefetch0((void *)(uintptr_t)
1232 buf_vec[vec_idx + 1].buf_addr);
1235 buf_avail = buf_len;
1237 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1238 (uint32_t)buf_avail, 0);
1242 * This mbuf reaches to its end, get a new one
1243 * to hold more data.
1245 if (mbuf_avail == 0) {
1246 cur = rte_pktmbuf_alloc(mbuf_pool);
1247 if (unlikely(cur == NULL)) {
1248 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1249 "allocate memory for mbuf.\n");
1253 if (unlikely(dev->dequeue_zero_copy))
1254 rte_mbuf_refcnt_update(cur, 1);
1257 prev->data_len = mbuf_offset;
1259 m->pkt_len += mbuf_offset;
1263 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1267 prev->data_len = mbuf_offset;
1268 m->pkt_len += mbuf_offset;
1271 vhost_dequeue_offload(hdr, m);
1278 static __rte_always_inline struct zcopy_mbuf *
1279 get_zmbuf(struct vhost_virtqueue *vq)
1285 /* search [last_zmbuf_idx, zmbuf_size) */
1286 i = vq->last_zmbuf_idx;
1287 last = vq->zmbuf_size;
1290 for (; i < last; i++) {
1291 if (vq->zmbufs[i].in_use == 0) {
1292 vq->last_zmbuf_idx = i + 1;
1293 vq->zmbufs[i].in_use = 1;
1294 return &vq->zmbufs[i];
1300 /* search [0, last_zmbuf_idx) */
1302 last = vq->last_zmbuf_idx;
1309 static __rte_always_inline bool
1310 mbuf_is_consumed(struct rte_mbuf *m)
1313 if (rte_mbuf_refcnt_read(m) > 1)
1321 static __rte_always_inline void
1322 restore_mbuf(struct rte_mbuf *m)
1324 uint32_t mbuf_size, priv_size;
1327 priv_size = rte_pktmbuf_priv_size(m->pool);
1328 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1329 /* start of buffer is after mbuf structure and priv data */
1331 m->buf_addr = (char *)m + mbuf_size;
1332 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1337 static __rte_always_inline uint16_t
1338 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1339 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1342 uint16_t free_entries;
1344 if (unlikely(dev->dequeue_zero_copy)) {
1345 struct zcopy_mbuf *zmbuf, *next;
1347 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1348 zmbuf != NULL; zmbuf = next) {
1349 next = TAILQ_NEXT(zmbuf, next);
1351 if (mbuf_is_consumed(zmbuf->mbuf)) {
1352 update_shadow_used_ring_split(vq,
1353 zmbuf->desc_idx, 0);
1354 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1355 restore_mbuf(zmbuf->mbuf);
1356 rte_pktmbuf_free(zmbuf->mbuf);
1362 flush_shadow_used_ring_split(dev, vq);
1363 vhost_vring_call_split(dev, vq);
1366 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1368 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1370 if (free_entries == 0)
1373 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1375 count = RTE_MIN(count, MAX_PKT_BURST);
1376 count = RTE_MIN(count, free_entries);
1377 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1380 for (i = 0; i < count; i++) {
1381 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1382 uint16_t head_idx, dummy_len;
1383 uint16_t nr_vec = 0;
1386 if (unlikely(fill_vec_buf_split(dev, vq,
1387 vq->last_avail_idx + i,
1389 &head_idx, &dummy_len,
1390 VHOST_ACCESS_RO) < 0))
1393 if (likely(dev->dequeue_zero_copy == 0))
1394 update_shadow_used_ring_split(vq, head_idx, 0);
1396 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1398 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1399 if (unlikely(pkts[i] == NULL)) {
1400 RTE_LOG(ERR, VHOST_DATA,
1401 "Failed to allocate memory for mbuf.\n");
1405 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1407 if (unlikely(err)) {
1408 rte_pktmbuf_free(pkts[i]);
1412 if (unlikely(dev->dequeue_zero_copy)) {
1413 struct zcopy_mbuf *zmbuf;
1415 zmbuf = get_zmbuf(vq);
1417 rte_pktmbuf_free(pkts[i]);
1420 zmbuf->mbuf = pkts[i];
1421 zmbuf->desc_idx = head_idx;
1424 * Pin lock the mbuf; we will check later to see
1425 * whether the mbuf is freed (when we are the last
1426 * user) or not. If that's the case, we then could
1427 * update the used ring safely.
1429 rte_mbuf_refcnt_update(pkts[i], 1);
1432 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1435 vq->last_avail_idx += i;
1437 if (likely(dev->dequeue_zero_copy == 0)) {
1438 do_data_copy_dequeue(vq);
1439 if (unlikely(i < count))
1440 vq->shadow_used_idx = i;
1441 flush_shadow_used_ring_split(dev, vq);
1442 vhost_vring_call_split(dev, vq);
1448 static __rte_always_inline uint16_t
1449 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1450 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1454 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1456 if (unlikely(dev->dequeue_zero_copy)) {
1457 struct zcopy_mbuf *zmbuf, *next;
1459 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1460 zmbuf != NULL; zmbuf = next) {
1461 next = TAILQ_NEXT(zmbuf, next);
1463 if (mbuf_is_consumed(zmbuf->mbuf)) {
1464 update_shadow_used_ring_packed(vq,
1469 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1470 restore_mbuf(zmbuf->mbuf);
1471 rte_pktmbuf_free(zmbuf->mbuf);
1477 flush_shadow_used_ring_packed(dev, vq);
1478 vhost_vring_call_packed(dev, vq);
1481 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1483 count = RTE_MIN(count, MAX_PKT_BURST);
1484 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1487 for (i = 0; i < count; i++) {
1488 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1489 uint16_t buf_id, dummy_len;
1490 uint16_t desc_count, nr_vec = 0;
1493 if (unlikely(fill_vec_buf_packed(dev, vq,
1494 vq->last_avail_idx, &desc_count,
1496 &buf_id, &dummy_len,
1497 VHOST_ACCESS_RW) < 0))
1500 if (likely(dev->dequeue_zero_copy == 0))
1501 update_shadow_used_ring_packed(vq, buf_id, 0,
1504 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1506 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1507 if (unlikely(pkts[i] == NULL)) {
1508 RTE_LOG(ERR, VHOST_DATA,
1509 "Failed to allocate memory for mbuf.\n");
1513 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1515 if (unlikely(err)) {
1516 rte_pktmbuf_free(pkts[i]);
1520 if (unlikely(dev->dequeue_zero_copy)) {
1521 struct zcopy_mbuf *zmbuf;
1523 zmbuf = get_zmbuf(vq);
1525 rte_pktmbuf_free(pkts[i]);
1528 zmbuf->mbuf = pkts[i];
1529 zmbuf->desc_idx = buf_id;
1530 zmbuf->desc_count = desc_count;
1533 * Pin lock the mbuf; we will check later to see
1534 * whether the mbuf is freed (when we are the last
1535 * user) or not. If that's the case, we then could
1536 * update the used ring safely.
1538 rte_mbuf_refcnt_update(pkts[i], 1);
1541 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1544 vq->last_avail_idx += desc_count;
1545 if (vq->last_avail_idx >= vq->size) {
1546 vq->last_avail_idx -= vq->size;
1547 vq->avail_wrap_counter ^= 1;
1551 if (likely(dev->dequeue_zero_copy == 0)) {
1552 do_data_copy_dequeue(vq);
1553 if (unlikely(i < count))
1554 vq->shadow_used_idx = i;
1555 flush_shadow_used_ring_packed(dev, vq);
1556 vhost_vring_call_packed(dev, vq);
1563 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1564 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1566 struct virtio_net *dev;
1567 struct rte_mbuf *rarp_mbuf = NULL;
1568 struct vhost_virtqueue *vq;
1570 dev = get_device(vid);
1574 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1575 RTE_LOG(ERR, VHOST_DATA,
1576 "(%d) %s: built-in vhost net backend is disabled.\n",
1577 dev->vid, __func__);
1581 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1582 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1583 dev->vid, __func__, queue_id);
1587 vq = dev->virtqueue[queue_id];
1589 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1592 if (unlikely(vq->enabled == 0)) {
1594 goto out_access_unlock;
1597 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1598 vhost_user_iotlb_rd_lock(vq);
1600 if (unlikely(vq->access_ok == 0))
1601 if (unlikely(vring_translate(dev, vq) < 0)) {
1607 * Construct a RARP broadcast packet, and inject it to the "pkts"
1608 * array, to looks like that guest actually send such packet.
1610 * Check user_send_rarp() for more information.
1612 * broadcast_rarp shares a cacheline in the virtio_net structure
1613 * with some fields that are accessed during enqueue and
1614 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1615 * result in false sharing between enqueue and dequeue.
1617 * Prevent unnecessary false sharing by reading broadcast_rarp first
1618 * and only performing cmpset if the read indicates it is likely to
1621 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1622 rte_atomic16_cmpset((volatile uint16_t *)
1623 &dev->broadcast_rarp.cnt, 1, 0))) {
1625 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1626 if (rarp_mbuf == NULL) {
1627 RTE_LOG(ERR, VHOST_DATA,
1628 "Failed to make RARP packet.\n");
1635 if (vq_is_packed(dev))
1636 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1638 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1641 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1642 vhost_user_iotlb_rd_unlock(vq);
1645 rte_spinlock_unlock(&vq->access_lock);
1647 if (unlikely(rarp_mbuf != NULL)) {
1649 * Inject it to the head of "pkts" array, so that switch's mac
1650 * learning table will get updated first.
1652 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1653 pkts[0] = rarp_mbuf;