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 do_flush_shadow_used_ring_split(struct virtio_net *dev,
42 struct vhost_virtqueue *vq,
43 uint16_t to, uint16_t from, uint16_t size)
45 rte_memcpy(&vq->used->ring[to],
46 &vq->shadow_used_split[from],
47 size * sizeof(struct vring_used_elem));
48 vhost_log_cache_used_vring(dev, vq,
49 offsetof(struct vring_used, ring[to]),
50 size * sizeof(struct vring_used_elem));
53 static __rte_always_inline void
54 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
56 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
58 if (used_idx + vq->shadow_used_idx <= vq->size) {
59 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
64 /* update used ring interval [used_idx, vq->size] */
65 size = vq->size - used_idx;
66 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
68 /* update the left half used ring interval [0, left_size] */
69 do_flush_shadow_used_ring_split(dev, vq, 0, size,
70 vq->shadow_used_idx - size);
72 vq->last_used_idx += vq->shadow_used_idx;
76 vhost_log_cache_sync(dev, vq);
78 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
79 vq->shadow_used_idx = 0;
80 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
81 sizeof(vq->used->idx));
84 static __rte_always_inline void
85 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
86 uint16_t desc_idx, uint32_t len)
88 uint16_t i = vq->shadow_used_idx++;
90 vq->shadow_used_split[i].id = desc_idx;
91 vq->shadow_used_split[i].len = len;
94 static __rte_always_inline void
95 flush_shadow_used_ring_packed(struct virtio_net *dev,
96 struct vhost_virtqueue *vq)
99 uint16_t used_idx = vq->last_used_idx;
100 uint16_t head_idx = vq->last_used_idx;
101 uint16_t head_flags = 0;
103 /* Split loop in two to save memory barriers */
104 for (i = 0; i < vq->shadow_used_idx; i++) {
105 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
106 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
108 used_idx += vq->shadow_used_packed[i].count;
109 if (used_idx >= vq->size)
110 used_idx -= vq->size;
115 for (i = 0; i < vq->shadow_used_idx; i++) {
118 if (vq->shadow_used_packed[i].len)
119 flags = VRING_DESC_F_WRITE;
123 if (vq->used_wrap_counter) {
124 flags |= VRING_DESC_F_USED;
125 flags |= VRING_DESC_F_AVAIL;
127 flags &= ~VRING_DESC_F_USED;
128 flags &= ~VRING_DESC_F_AVAIL;
132 vq->desc_packed[vq->last_used_idx].flags = flags;
134 vhost_log_cache_used_vring(dev, vq,
136 sizeof(struct vring_packed_desc),
137 sizeof(struct vring_packed_desc));
139 head_idx = vq->last_used_idx;
143 vq->last_used_idx += vq->shadow_used_packed[i].count;
144 if (vq->last_used_idx >= vq->size) {
145 vq->used_wrap_counter ^= 1;
146 vq->last_used_idx -= vq->size;
150 vq->desc_packed[head_idx].flags = head_flags;
152 vhost_log_cache_used_vring(dev, vq,
154 sizeof(struct vring_packed_desc),
155 sizeof(struct vring_packed_desc));
157 vq->shadow_used_idx = 0;
158 vhost_log_cache_sync(dev, vq);
161 static __rte_always_inline void
162 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
163 uint16_t desc_idx, uint32_t len, uint16_t count)
165 uint16_t i = vq->shadow_used_idx++;
167 vq->shadow_used_packed[i].id = desc_idx;
168 vq->shadow_used_packed[i].len = len;
169 vq->shadow_used_packed[i].count = count;
173 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
175 struct batch_copy_elem *elem = vq->batch_copy_elems;
176 uint16_t count = vq->batch_copy_nb_elems;
179 for (i = 0; i < count; i++) {
180 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
181 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
182 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
185 vq->batch_copy_nb_elems = 0;
189 do_data_copy_dequeue(struct vhost_virtqueue *vq)
191 struct batch_copy_elem *elem = vq->batch_copy_elems;
192 uint16_t count = vq->batch_copy_nb_elems;
195 for (i = 0; i < count; i++)
196 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
198 vq->batch_copy_nb_elems = 0;
201 /* avoid write operation when necessary, to lessen cache issues */
202 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
203 if ((var) != (val)) \
207 static __rte_always_inline void
208 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
210 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
212 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
213 csum_l4 |= PKT_TX_TCP_CKSUM;
216 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
217 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
220 case PKT_TX_TCP_CKSUM:
221 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
224 case PKT_TX_UDP_CKSUM:
225 net_hdr->csum_offset = (offsetof(struct udp_hdr,
228 case PKT_TX_SCTP_CKSUM:
229 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
234 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
235 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
236 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
239 /* IP cksum verification cannot be bypassed, then calculate here */
240 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
241 struct ipv4_hdr *ipv4_hdr;
243 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
245 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
248 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
249 if (m_buf->ol_flags & PKT_TX_IPV4)
250 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
252 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
253 net_hdr->gso_size = m_buf->tso_segsz;
254 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
256 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
257 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
258 net_hdr->gso_size = m_buf->tso_segsz;
259 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
262 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
263 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
264 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
268 static __rte_always_inline int
269 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
270 struct buf_vector *buf_vec, uint16_t *vec_idx,
271 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
273 uint16_t vec_id = *vec_idx;
277 uint64_t desc_chunck_len = desc_len;
279 if (unlikely(vec_id >= BUF_VECTOR_MAX))
282 desc_addr = vhost_iova_to_vva(dev, vq,
286 if (unlikely(!desc_addr))
289 buf_vec[vec_id].buf_iova = desc_iova;
290 buf_vec[vec_id].buf_addr = desc_addr;
291 buf_vec[vec_id].buf_len = desc_chunck_len;
293 desc_len -= desc_chunck_len;
294 desc_iova += desc_chunck_len;
302 static __rte_always_inline int
303 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
304 uint32_t avail_idx, uint16_t *vec_idx,
305 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
306 uint32_t *desc_chain_len, uint8_t perm)
308 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
309 uint16_t vec_id = *vec_idx;
312 struct vring_desc *descs = vq->desc;
313 struct vring_desc *idesc = NULL;
315 *desc_chain_head = idx;
317 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
318 dlen = vq->desc[idx].len;
319 descs = (struct vring_desc *)(uintptr_t)
320 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
323 if (unlikely(!descs))
326 if (unlikely(dlen < vq->desc[idx].len)) {
328 * The indirect desc table is not contiguous
329 * in process VA space, we have to copy it.
331 idesc = alloc_copy_ind_table(dev, vq,
332 vq->desc[idx].addr, vq->desc[idx].len);
333 if (unlikely(!idesc))
343 if (unlikely(idx >= vq->size)) {
344 free_ind_table(idesc);
348 len += descs[idx].len;
350 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
351 descs[idx].addr, descs[idx].len,
353 free_ind_table(idesc);
357 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
360 idx = descs[idx].next;
363 *desc_chain_len = len;
366 if (unlikely(!!idesc))
367 free_ind_table(idesc);
373 * Returns -1 on fail, 0 on success
376 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
377 uint32_t size, struct buf_vector *buf_vec,
378 uint16_t *num_buffers, uint16_t avail_head,
382 uint16_t vec_idx = 0;
383 uint16_t max_tries, tries = 0;
385 uint16_t head_idx = 0;
389 cur_idx = vq->last_avail_idx;
391 if (rxvq_is_mergeable(dev))
392 max_tries = vq->size - 1;
397 if (unlikely(cur_idx == avail_head))
400 * if we tried all available ring items, and still
401 * can't get enough buf, it means something abnormal
404 if (unlikely(++tries > max_tries))
407 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
410 VHOST_ACCESS_RW) < 0))
412 len = RTE_MIN(len, size);
413 update_shadow_used_ring_split(vq, head_idx, len);
425 static __rte_always_inline int
426 fill_vec_buf_packed_indirect(struct virtio_net *dev,
427 struct vhost_virtqueue *vq,
428 struct vring_packed_desc *desc, uint16_t *vec_idx,
429 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
433 uint16_t vec_id = *vec_idx;
435 struct vring_packed_desc *descs, *idescs = NULL;
438 descs = (struct vring_packed_desc *)(uintptr_t)
439 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
440 if (unlikely(!descs))
443 if (unlikely(dlen < desc->len)) {
445 * The indirect desc table is not contiguous
446 * in process VA space, we have to copy it.
448 idescs = alloc_copy_ind_table(dev, vq, desc->addr, desc->len);
449 if (unlikely(!idescs))
455 nr_descs = desc->len / sizeof(struct vring_packed_desc);
456 if (unlikely(nr_descs >= vq->size)) {
457 free_ind_table(idescs);
461 for (i = 0; i < nr_descs; i++) {
462 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
463 free_ind_table(idescs);
467 *len += descs[i].len;
468 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
469 descs[i].addr, descs[i].len,
475 if (unlikely(!!idescs))
476 free_ind_table(idescs);
481 static __rte_always_inline int
482 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
483 uint16_t avail_idx, uint16_t *desc_count,
484 struct buf_vector *buf_vec, uint16_t *vec_idx,
485 uint16_t *buf_id, uint32_t *len, uint8_t perm)
487 bool wrap_counter = vq->avail_wrap_counter;
488 struct vring_packed_desc *descs = vq->desc_packed;
489 uint16_t vec_id = *vec_idx;
491 if (avail_idx < vq->last_avail_idx)
494 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
498 * The ordering between desc flags and desc
499 * content reads need to be enforced.
507 if (unlikely(vec_id >= BUF_VECTOR_MAX))
511 *buf_id = descs[avail_idx].id;
513 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
514 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
520 *len += descs[avail_idx].len;
522 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
523 descs[avail_idx].addr,
524 descs[avail_idx].len,
529 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
532 if (++avail_idx >= vq->size) {
533 avail_idx -= vq->size;
544 * Returns -1 on fail, 0 on success
547 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
548 uint32_t size, struct buf_vector *buf_vec,
549 uint16_t *nr_vec, uint16_t *num_buffers,
553 uint16_t vec_idx = 0;
554 uint16_t max_tries, tries = 0;
561 avail_idx = vq->last_avail_idx;
563 if (rxvq_is_mergeable(dev))
564 max_tries = vq->size - 1;
570 * if we tried all available ring items, and still
571 * can't get enough buf, it means something abnormal
574 if (unlikely(++tries > max_tries))
577 if (unlikely(fill_vec_buf_packed(dev, vq,
578 avail_idx, &desc_count,
581 VHOST_ACCESS_RW) < 0))
584 len = RTE_MIN(len, size);
585 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
588 avail_idx += desc_count;
589 if (avail_idx >= vq->size)
590 avail_idx -= vq->size;
592 *nr_descs += desc_count;
601 static __rte_always_inline int
602 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
603 struct rte_mbuf *m, struct buf_vector *buf_vec,
604 uint16_t nr_vec, uint16_t num_buffers)
606 uint32_t vec_idx = 0;
607 uint32_t mbuf_offset, mbuf_avail;
608 uint32_t buf_offset, buf_avail;
609 uint64_t buf_addr, buf_iova, buf_len;
612 struct rte_mbuf *hdr_mbuf;
613 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
614 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
617 if (unlikely(m == NULL)) {
622 buf_addr = buf_vec[vec_idx].buf_addr;
623 buf_iova = buf_vec[vec_idx].buf_iova;
624 buf_len = buf_vec[vec_idx].buf_len;
627 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
629 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
636 if (unlikely(buf_len < dev->vhost_hlen))
639 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
641 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
642 dev->vid, num_buffers);
644 if (unlikely(buf_len < dev->vhost_hlen)) {
645 buf_offset = dev->vhost_hlen - buf_len;
647 buf_addr = buf_vec[vec_idx].buf_addr;
648 buf_iova = buf_vec[vec_idx].buf_iova;
649 buf_len = buf_vec[vec_idx].buf_len;
650 buf_avail = buf_len - buf_offset;
652 buf_offset = dev->vhost_hlen;
653 buf_avail = buf_len - dev->vhost_hlen;
656 mbuf_avail = rte_pktmbuf_data_len(m);
658 while (mbuf_avail != 0 || m->next != NULL) {
659 /* done with current buf, get the next one */
660 if (buf_avail == 0) {
662 if (unlikely(vec_idx >= nr_vec)) {
667 buf_addr = buf_vec[vec_idx].buf_addr;
668 buf_iova = buf_vec[vec_idx].buf_iova;
669 buf_len = buf_vec[vec_idx].buf_len;
671 /* Prefetch next buffer address. */
672 if (vec_idx + 1 < nr_vec)
673 rte_prefetch0((void *)(uintptr_t)
674 buf_vec[vec_idx + 1].buf_addr);
679 /* done with current mbuf, get the next one */
680 if (mbuf_avail == 0) {
684 mbuf_avail = rte_pktmbuf_data_len(m);
688 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
689 if (rxvq_is_mergeable(dev))
690 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
693 if (unlikely(hdr == &tmp_hdr)) {
695 uint64_t remain = dev->vhost_hlen;
696 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
697 uint64_t iova = buf_vec[0].buf_iova;
698 uint16_t hdr_vec_idx = 0;
701 len = RTE_MIN(remain,
702 buf_vec[hdr_vec_idx].buf_len);
703 dst = buf_vec[hdr_vec_idx].buf_addr;
704 rte_memcpy((void *)(uintptr_t)dst,
705 (void *)(uintptr_t)src,
708 PRINT_PACKET(dev, (uintptr_t)dst,
710 vhost_log_cache_write(dev, vq,
719 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
721 vhost_log_cache_write(dev, vq,
729 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
731 if (likely(cpy_len > MAX_BATCH_LEN ||
732 vq->batch_copy_nb_elems >= vq->size)) {
733 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
734 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
736 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
738 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
741 batch_copy[vq->batch_copy_nb_elems].dst =
742 (void *)((uintptr_t)(buf_addr + buf_offset));
743 batch_copy[vq->batch_copy_nb_elems].src =
744 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
745 batch_copy[vq->batch_copy_nb_elems].log_addr =
746 buf_iova + buf_offset;
747 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
748 vq->batch_copy_nb_elems++;
751 mbuf_avail -= cpy_len;
752 mbuf_offset += cpy_len;
753 buf_avail -= cpy_len;
754 buf_offset += cpy_len;
762 static __rte_always_inline uint32_t
763 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
764 struct rte_mbuf **pkts, uint32_t count)
766 uint32_t pkt_idx = 0;
767 uint16_t num_buffers;
768 struct buf_vector buf_vec[BUF_VECTOR_MAX];
771 avail_head = *((volatile uint16_t *)&vq->avail->idx);
774 * The ordering between avail index and
775 * desc reads needs to be enforced.
779 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
781 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
782 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
785 if (unlikely(reserve_avail_buf_split(dev, vq,
786 pkt_len, buf_vec, &num_buffers,
787 avail_head, &nr_vec) < 0)) {
788 VHOST_LOG_DEBUG(VHOST_DATA,
789 "(%d) failed to get enough desc from vring\n",
791 vq->shadow_used_idx -= num_buffers;
795 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
797 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
798 dev->vid, vq->last_avail_idx,
799 vq->last_avail_idx + num_buffers);
801 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
804 vq->shadow_used_idx -= num_buffers;
808 vq->last_avail_idx += num_buffers;
811 do_data_copy_enqueue(dev, vq);
813 if (likely(vq->shadow_used_idx)) {
814 flush_shadow_used_ring_split(dev, vq);
815 vhost_vring_call_split(dev, vq);
821 static __rte_always_inline uint32_t
822 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
823 struct rte_mbuf **pkts, uint32_t count)
825 uint32_t pkt_idx = 0;
826 uint16_t num_buffers;
827 struct buf_vector buf_vec[BUF_VECTOR_MAX];
829 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
830 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
832 uint16_t nr_descs = 0;
834 if (unlikely(reserve_avail_buf_packed(dev, vq,
835 pkt_len, buf_vec, &nr_vec,
836 &num_buffers, &nr_descs) < 0)) {
837 VHOST_LOG_DEBUG(VHOST_DATA,
838 "(%d) failed to get enough desc from vring\n",
840 vq->shadow_used_idx -= num_buffers;
844 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
846 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
847 dev->vid, vq->last_avail_idx,
848 vq->last_avail_idx + num_buffers);
850 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
853 vq->shadow_used_idx -= num_buffers;
857 vq->last_avail_idx += nr_descs;
858 if (vq->last_avail_idx >= vq->size) {
859 vq->last_avail_idx -= vq->size;
860 vq->avail_wrap_counter ^= 1;
864 do_data_copy_enqueue(dev, vq);
866 if (likely(vq->shadow_used_idx)) {
867 flush_shadow_used_ring_packed(dev, vq);
868 vhost_vring_call_packed(dev, vq);
874 static __rte_always_inline uint32_t
875 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
876 struct rte_mbuf **pkts, uint32_t count)
878 struct vhost_virtqueue *vq;
881 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
882 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
883 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
884 dev->vid, __func__, queue_id);
888 vq = dev->virtqueue[queue_id];
890 rte_spinlock_lock(&vq->access_lock);
892 if (unlikely(vq->enabled == 0))
893 goto out_access_unlock;
895 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
896 vhost_user_iotlb_rd_lock(vq);
898 if (unlikely(vq->access_ok == 0))
899 if (unlikely(vring_translate(dev, vq) < 0))
902 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
906 if (vq_is_packed(dev))
907 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
909 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
912 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
913 vhost_user_iotlb_rd_unlock(vq);
916 rte_spinlock_unlock(&vq->access_lock);
922 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
923 struct rte_mbuf **pkts, uint16_t count)
925 struct virtio_net *dev = get_device(vid);
930 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
931 RTE_LOG(ERR, VHOST_DATA,
932 "(%d) %s: built-in vhost net backend is disabled.\n",
937 return virtio_dev_rx(dev, queue_id, pkts, count);
941 virtio_net_with_host_offload(struct virtio_net *dev)
944 ((1ULL << VIRTIO_NET_F_CSUM) |
945 (1ULL << VIRTIO_NET_F_HOST_ECN) |
946 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
947 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
948 (1ULL << VIRTIO_NET_F_HOST_UFO)))
955 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
957 struct ipv4_hdr *ipv4_hdr;
958 struct ipv6_hdr *ipv6_hdr;
960 struct ether_hdr *eth_hdr;
963 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
965 m->l2_len = sizeof(struct ether_hdr);
966 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
968 if (ethertype == ETHER_TYPE_VLAN) {
969 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
971 m->l2_len += sizeof(struct vlan_hdr);
972 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
975 l3_hdr = (char *)eth_hdr + m->l2_len;
978 case ETHER_TYPE_IPv4:
980 *l4_proto = ipv4_hdr->next_proto_id;
981 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
982 *l4_hdr = (char *)l3_hdr + m->l3_len;
983 m->ol_flags |= PKT_TX_IPV4;
985 case ETHER_TYPE_IPv6:
987 *l4_proto = ipv6_hdr->proto;
988 m->l3_len = sizeof(struct ipv6_hdr);
989 *l4_hdr = (char *)l3_hdr + m->l3_len;
990 m->ol_flags |= PKT_TX_IPV6;
1000 static __rte_always_inline void
1001 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1003 uint16_t l4_proto = 0;
1004 void *l4_hdr = NULL;
1005 struct tcp_hdr *tcp_hdr = NULL;
1007 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1010 parse_ethernet(m, &l4_proto, &l4_hdr);
1011 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1012 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1013 switch (hdr->csum_offset) {
1014 case (offsetof(struct tcp_hdr, cksum)):
1015 if (l4_proto == IPPROTO_TCP)
1016 m->ol_flags |= PKT_TX_TCP_CKSUM;
1018 case (offsetof(struct udp_hdr, dgram_cksum)):
1019 if (l4_proto == IPPROTO_UDP)
1020 m->ol_flags |= PKT_TX_UDP_CKSUM;
1022 case (offsetof(struct sctp_hdr, cksum)):
1023 if (l4_proto == IPPROTO_SCTP)
1024 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1032 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1033 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1034 case VIRTIO_NET_HDR_GSO_TCPV4:
1035 case VIRTIO_NET_HDR_GSO_TCPV6:
1037 m->ol_flags |= PKT_TX_TCP_SEG;
1038 m->tso_segsz = hdr->gso_size;
1039 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1041 case VIRTIO_NET_HDR_GSO_UDP:
1042 m->ol_flags |= PKT_TX_UDP_SEG;
1043 m->tso_segsz = hdr->gso_size;
1044 m->l4_len = sizeof(struct udp_hdr);
1047 RTE_LOG(WARNING, VHOST_DATA,
1048 "unsupported gso type %u.\n", hdr->gso_type);
1054 static __rte_always_inline void
1055 put_zmbuf(struct zcopy_mbuf *zmbuf)
1060 static __rte_always_inline int
1061 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1062 struct buf_vector *buf_vec, uint16_t nr_vec,
1063 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1065 uint32_t buf_avail, buf_offset;
1066 uint64_t buf_addr, buf_iova, buf_len;
1067 uint32_t mbuf_avail, mbuf_offset;
1069 struct rte_mbuf *cur = m, *prev = m;
1070 struct virtio_net_hdr tmp_hdr;
1071 struct virtio_net_hdr *hdr = NULL;
1072 /* A counter to avoid desc dead loop chain */
1073 uint16_t vec_idx = 0;
1074 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1077 buf_addr = buf_vec[vec_idx].buf_addr;
1078 buf_iova = buf_vec[vec_idx].buf_iova;
1079 buf_len = buf_vec[vec_idx].buf_len;
1081 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1086 if (likely(nr_vec > 1))
1087 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
1089 if (virtio_net_with_host_offload(dev)) {
1090 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1092 uint64_t remain = sizeof(struct virtio_net_hdr);
1094 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
1095 uint16_t hdr_vec_idx = 0;
1098 * No luck, the virtio-net header doesn't fit
1099 * in a contiguous virtual area.
1102 len = RTE_MIN(remain,
1103 buf_vec[hdr_vec_idx].buf_len);
1104 src = buf_vec[hdr_vec_idx].buf_addr;
1105 rte_memcpy((void *)(uintptr_t)dst,
1106 (void *)(uintptr_t)src, len);
1115 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1121 * A virtio driver normally uses at least 2 desc buffers
1122 * for Tx: the first for storing the header, and others
1123 * for storing the data.
1125 if (unlikely(buf_len < dev->vhost_hlen)) {
1126 buf_offset = dev->vhost_hlen - buf_len;
1128 buf_addr = buf_vec[vec_idx].buf_addr;
1129 buf_iova = buf_vec[vec_idx].buf_iova;
1130 buf_len = buf_vec[vec_idx].buf_len;
1131 buf_avail = buf_len - buf_offset;
1132 } else if (buf_len == dev->vhost_hlen) {
1133 if (unlikely(++vec_idx >= nr_vec))
1135 buf_addr = buf_vec[vec_idx].buf_addr;
1136 buf_iova = buf_vec[vec_idx].buf_iova;
1137 buf_len = buf_vec[vec_idx].buf_len;
1140 buf_avail = buf_len;
1142 buf_offset = dev->vhost_hlen;
1143 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1146 rte_prefetch0((void *)(uintptr_t)
1147 (buf_addr + buf_offset));
1150 (uintptr_t)(buf_addr + buf_offset),
1151 (uint32_t)buf_avail, 0);
1154 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1158 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1161 * A desc buf might across two host physical pages that are
1162 * not continuous. In such case (gpa_to_hpa returns 0), data
1163 * will be copied even though zero copy is enabled.
1165 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1166 buf_iova + buf_offset, cpy_len)))) {
1167 cur->data_len = cpy_len;
1170 (void *)(uintptr_t)(buf_addr + buf_offset);
1171 cur->buf_iova = hpa;
1174 * In zero copy mode, one mbuf can only reference data
1175 * for one or partial of one desc buff.
1177 mbuf_avail = cpy_len;
1179 if (likely(cpy_len > MAX_BATCH_LEN ||
1180 vq->batch_copy_nb_elems >= vq->size ||
1181 (hdr && cur == m))) {
1182 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1184 (void *)((uintptr_t)(buf_addr +
1188 batch_copy[vq->batch_copy_nb_elems].dst =
1189 rte_pktmbuf_mtod_offset(cur, void *,
1191 batch_copy[vq->batch_copy_nb_elems].src =
1192 (void *)((uintptr_t)(buf_addr +
1194 batch_copy[vq->batch_copy_nb_elems].len =
1196 vq->batch_copy_nb_elems++;
1200 mbuf_avail -= cpy_len;
1201 mbuf_offset += cpy_len;
1202 buf_avail -= cpy_len;
1203 buf_offset += cpy_len;
1205 /* This buf reaches to its end, get the next one */
1206 if (buf_avail == 0) {
1207 if (++vec_idx >= nr_vec)
1210 buf_addr = buf_vec[vec_idx].buf_addr;
1211 buf_iova = buf_vec[vec_idx].buf_iova;
1212 buf_len = buf_vec[vec_idx].buf_len;
1215 * Prefecth desc n + 1 buffer while
1216 * desc n buffer is processed.
1218 if (vec_idx + 1 < nr_vec)
1219 rte_prefetch0((void *)(uintptr_t)
1220 buf_vec[vec_idx + 1].buf_addr);
1223 buf_avail = buf_len;
1225 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1226 (uint32_t)buf_avail, 0);
1230 * This mbuf reaches to its end, get a new one
1231 * to hold more data.
1233 if (mbuf_avail == 0) {
1234 cur = rte_pktmbuf_alloc(mbuf_pool);
1235 if (unlikely(cur == NULL)) {
1236 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1237 "allocate memory for mbuf.\n");
1241 if (unlikely(dev->dequeue_zero_copy))
1242 rte_mbuf_refcnt_update(cur, 1);
1245 prev->data_len = mbuf_offset;
1247 m->pkt_len += mbuf_offset;
1251 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1255 prev->data_len = mbuf_offset;
1256 m->pkt_len += mbuf_offset;
1259 vhost_dequeue_offload(hdr, m);
1266 static __rte_always_inline struct zcopy_mbuf *
1267 get_zmbuf(struct vhost_virtqueue *vq)
1273 /* search [last_zmbuf_idx, zmbuf_size) */
1274 i = vq->last_zmbuf_idx;
1275 last = vq->zmbuf_size;
1278 for (; i < last; i++) {
1279 if (vq->zmbufs[i].in_use == 0) {
1280 vq->last_zmbuf_idx = i + 1;
1281 vq->zmbufs[i].in_use = 1;
1282 return &vq->zmbufs[i];
1288 /* search [0, last_zmbuf_idx) */
1290 last = vq->last_zmbuf_idx;
1297 static __rte_always_inline bool
1298 mbuf_is_consumed(struct rte_mbuf *m)
1301 if (rte_mbuf_refcnt_read(m) > 1)
1309 static __rte_always_inline void
1310 restore_mbuf(struct rte_mbuf *m)
1312 uint32_t mbuf_size, priv_size;
1315 priv_size = rte_pktmbuf_priv_size(m->pool);
1316 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1317 /* start of buffer is after mbuf structure and priv data */
1319 m->buf_addr = (char *)m + mbuf_size;
1320 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1325 static __rte_always_inline uint16_t
1326 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1327 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1330 uint16_t free_entries;
1332 if (unlikely(dev->dequeue_zero_copy)) {
1333 struct zcopy_mbuf *zmbuf, *next;
1335 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1336 zmbuf != NULL; zmbuf = next) {
1337 next = TAILQ_NEXT(zmbuf, next);
1339 if (mbuf_is_consumed(zmbuf->mbuf)) {
1340 update_shadow_used_ring_split(vq,
1341 zmbuf->desc_idx, 0);
1342 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1343 restore_mbuf(zmbuf->mbuf);
1344 rte_pktmbuf_free(zmbuf->mbuf);
1350 if (likely(vq->shadow_used_idx)) {
1351 flush_shadow_used_ring_split(dev, vq);
1352 vhost_vring_call_split(dev, vq);
1356 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1358 if (free_entries == 0)
1362 * The ordering between avail index and
1363 * desc reads needs to be enforced.
1367 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1369 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1371 count = RTE_MIN(count, MAX_PKT_BURST);
1372 count = RTE_MIN(count, free_entries);
1373 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1376 for (i = 0; i < count; i++) {
1377 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1380 uint16_t nr_vec = 0;
1383 if (unlikely(fill_vec_buf_split(dev, vq,
1384 vq->last_avail_idx + i,
1386 &head_idx, &dummy_len,
1387 VHOST_ACCESS_RO) < 0))
1390 if (likely(dev->dequeue_zero_copy == 0))
1391 update_shadow_used_ring_split(vq, head_idx, 0);
1393 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1395 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1396 if (unlikely(pkts[i] == NULL)) {
1397 RTE_LOG(ERR, VHOST_DATA,
1398 "Failed to allocate memory for mbuf.\n");
1402 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1404 if (unlikely(err)) {
1405 rte_pktmbuf_free(pkts[i]);
1409 if (unlikely(dev->dequeue_zero_copy)) {
1410 struct zcopy_mbuf *zmbuf;
1412 zmbuf = get_zmbuf(vq);
1414 rte_pktmbuf_free(pkts[i]);
1417 zmbuf->mbuf = pkts[i];
1418 zmbuf->desc_idx = head_idx;
1421 * Pin lock the mbuf; we will check later to see
1422 * whether the mbuf is freed (when we are the last
1423 * user) or not. If that's the case, we then could
1424 * update the used ring safely.
1426 rte_mbuf_refcnt_update(pkts[i], 1);
1429 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1432 vq->last_avail_idx += i;
1434 if (likely(dev->dequeue_zero_copy == 0)) {
1435 do_data_copy_dequeue(vq);
1436 if (unlikely(i < count))
1437 vq->shadow_used_idx = i;
1438 if (likely(vq->shadow_used_idx)) {
1439 flush_shadow_used_ring_split(dev, vq);
1440 vhost_vring_call_split(dev, vq);
1447 static __rte_always_inline uint16_t
1448 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1449 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1453 if (unlikely(dev->dequeue_zero_copy)) {
1454 struct zcopy_mbuf *zmbuf, *next;
1456 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1457 zmbuf != NULL; zmbuf = next) {
1458 next = TAILQ_NEXT(zmbuf, next);
1460 if (mbuf_is_consumed(zmbuf->mbuf)) {
1461 update_shadow_used_ring_packed(vq,
1466 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1467 restore_mbuf(zmbuf->mbuf);
1468 rte_pktmbuf_free(zmbuf->mbuf);
1474 if (likely(vq->shadow_used_idx)) {
1475 flush_shadow_used_ring_packed(dev, vq);
1476 vhost_vring_call_packed(dev, vq);
1480 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1482 count = RTE_MIN(count, MAX_PKT_BURST);
1483 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1486 for (i = 0; i < count; i++) {
1487 struct buf_vector buf_vec[BUF_VECTOR_MAX];
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_RO) < 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 if (likely(vq->shadow_used_idx)) {
1556 flush_shadow_used_ring_packed(dev, vq);
1557 vhost_vring_call_packed(dev, vq);
1565 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1566 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1568 struct virtio_net *dev;
1569 struct rte_mbuf *rarp_mbuf = NULL;
1570 struct vhost_virtqueue *vq;
1572 dev = get_device(vid);
1576 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1577 RTE_LOG(ERR, VHOST_DATA,
1578 "(%d) %s: built-in vhost net backend is disabled.\n",
1579 dev->vid, __func__);
1583 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1584 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1585 dev->vid, __func__, queue_id);
1589 vq = dev->virtqueue[queue_id];
1591 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1594 if (unlikely(vq->enabled == 0)) {
1596 goto out_access_unlock;
1599 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1600 vhost_user_iotlb_rd_lock(vq);
1602 if (unlikely(vq->access_ok == 0))
1603 if (unlikely(vring_translate(dev, vq) < 0)) {
1609 * Construct a RARP broadcast packet, and inject it to the "pkts"
1610 * array, to looks like that guest actually send such packet.
1612 * Check user_send_rarp() for more information.
1614 * broadcast_rarp shares a cacheline in the virtio_net structure
1615 * with some fields that are accessed during enqueue and
1616 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1617 * result in false sharing between enqueue and dequeue.
1619 * Prevent unnecessary false sharing by reading broadcast_rarp first
1620 * and only performing cmpset if the read indicates it is likely to
1623 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1624 rte_atomic16_cmpset((volatile uint16_t *)
1625 &dev->broadcast_rarp.cnt, 1, 0))) {
1627 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1628 if (rarp_mbuf == NULL) {
1629 RTE_LOG(ERR, VHOST_DATA,
1630 "Failed to make RARP packet.\n");
1637 if (vq_is_packed(dev))
1638 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1640 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1643 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1644 vhost_user_iotlb_rd_unlock(vq);
1647 rte_spinlock_unlock(&vq->access_lock);
1649 if (unlikely(rarp_mbuf != NULL)) {
1651 * Inject it to the head of "pkts" array, so that switch's mac
1652 * learning table will get updated first.
1654 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1655 pkts[0] = rarp_mbuf;