1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <stdint.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <errno.h> 39 40 #include <rte_cycles.h> 41 #include <rte_memory.h> 42 #include <rte_branch_prediction.h> 43 #include <rte_mempool.h> 44 #include <rte_malloc.h> 45 #include <rte_mbuf.h> 46 #include <rte_ether.h> 47 #include <rte_ethdev.h> 48 #include <rte_prefetch.h> 49 #include <rte_string_fns.h> 50 #include <rte_errno.h> 51 #include <rte_byteorder.h> 52 53 #include "virtio_rxtx_simple.h" 54 55 #ifndef __INTEL_COMPILER 56 #pragma GCC diagnostic ignored "-Wcast-qual" 57 #endif 58 59 int __attribute__((cold)) 60 virtqueue_enqueue_recv_refill_simple(struct virtqueue *vq, 61 struct rte_mbuf *cookie) 62 { 63 struct vq_desc_extra *dxp; 64 struct vring_desc *start_dp; 65 uint16_t desc_idx; 66 67 cookie->port = vq->rxq.port_id; 68 69 desc_idx = vq->vq_avail_idx & (vq->vq_nentries - 1); 70 dxp = &vq->vq_descx[desc_idx]; 71 dxp->cookie = (void *)cookie; 72 vq->sw_ring[desc_idx] = cookie; 73 74 start_dp = vq->vq_ring.desc; 75 start_dp[desc_idx].addr = 76 VIRTIO_MBUF_ADDR(cookie, vq) + 77 RTE_PKTMBUF_HEADROOM - vq->hw->vtnet_hdr_size; 78 start_dp[desc_idx].len = cookie->buf_len - 79 RTE_PKTMBUF_HEADROOM + vq->hw->vtnet_hdr_size; 80 81 vq->vq_free_cnt--; 82 vq->vq_avail_idx++; 83 84 return 0; 85 } 86 87 uint16_t 88 virtio_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts, 89 uint16_t nb_pkts) 90 { 91 struct virtnet_tx *txvq = tx_queue; 92 struct virtqueue *vq = txvq->vq; 93 struct virtio_hw *hw = vq->hw; 94 uint16_t nb_used; 95 uint16_t desc_idx; 96 struct vring_desc *start_dp; 97 uint16_t nb_tail, nb_commit; 98 int i; 99 uint16_t desc_idx_max = (vq->vq_nentries >> 1) - 1; 100 uint16_t nb_tx = 0; 101 102 if (unlikely(hw->started == 0)) 103 return nb_tx; 104 105 nb_used = VIRTQUEUE_NUSED(vq); 106 rte_compiler_barrier(); 107 108 if (nb_used >= VIRTIO_TX_FREE_THRESH) 109 virtio_xmit_cleanup(vq); 110 111 nb_commit = nb_pkts = RTE_MIN((vq->vq_free_cnt >> 1), nb_pkts); 112 desc_idx = (uint16_t)(vq->vq_avail_idx & desc_idx_max); 113 start_dp = vq->vq_ring.desc; 114 nb_tail = (uint16_t) (desc_idx_max + 1 - desc_idx); 115 116 if (nb_commit >= nb_tail) { 117 for (i = 0; i < nb_tail; i++) 118 vq->vq_descx[desc_idx + i].cookie = tx_pkts[i]; 119 for (i = 0; i < nb_tail; i++) { 120 start_dp[desc_idx].addr = 121 VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq); 122 start_dp[desc_idx].len = (*tx_pkts)->pkt_len; 123 tx_pkts++; 124 desc_idx++; 125 } 126 nb_commit -= nb_tail; 127 desc_idx = 0; 128 } 129 for (i = 0; i < nb_commit; i++) 130 vq->vq_descx[desc_idx + i].cookie = tx_pkts[i]; 131 for (i = 0; i < nb_commit; i++) { 132 start_dp[desc_idx].addr = 133 VIRTIO_MBUF_DATA_DMA_ADDR(*tx_pkts, vq); 134 start_dp[desc_idx].len = (*tx_pkts)->pkt_len; 135 tx_pkts++; 136 desc_idx++; 137 } 138 139 rte_compiler_barrier(); 140 141 vq->vq_free_cnt -= (uint16_t)(nb_pkts << 1); 142 vq->vq_avail_idx += nb_pkts; 143 vq->vq_ring.avail->idx = vq->vq_avail_idx; 144 txvq->stats.packets += nb_pkts; 145 146 if (likely(nb_pkts)) { 147 if (unlikely(virtqueue_kick_prepare(vq))) 148 virtqueue_notify(vq); 149 } 150 151 return nb_pkts; 152 } 153 154 int __attribute__((cold)) 155 virtio_rxq_vec_setup(struct virtnet_rx *rxq) 156 { 157 uintptr_t p; 158 struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */ 159 160 mb_def.nb_segs = 1; 161 mb_def.data_off = RTE_PKTMBUF_HEADROOM; 162 mb_def.port = rxq->port_id; 163 rte_mbuf_refcnt_set(&mb_def, 1); 164 165 /* prevent compiler reordering: rearm_data covers previous fields */ 166 rte_compiler_barrier(); 167 p = (uintptr_t)&mb_def.rearm_data; 168 rxq->mbuf_initializer = *(uint64_t *)p; 169 170 return 0; 171 } 172 173 /* Stub for linkage when arch specific implementation is not available */ 174 uint16_t __attribute__((weak)) 175 virtio_recv_pkts_vec(void *rx_queue __rte_unused, 176 struct rte_mbuf **rx_pkts __rte_unused, 177 uint16_t nb_pkts __rte_unused) 178 { 179 rte_panic("Wrong weak function linked by linker\n"); 180 return 0; 181 } 182