1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2014 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_memzone.h> 43 #include <rte_branch_prediction.h> 44 #include <rte_mempool.h> 45 #include <rte_malloc.h> 46 #include <rte_mbuf.h> 47 #include <rte_ether.h> 48 #include <rte_ethdev.h> 49 #include <rte_prefetch.h> 50 #include <rte_string_fns.h> 51 #include <rte_errno.h> 52 #include <rte_byteorder.h> 53 54 #include "virtio_logs.h" 55 #include "virtio_ethdev.h" 56 #include "virtio_pci.h" 57 #include "virtqueue.h" 58 #include "virtio_rxtx.h" 59 60 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP 61 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len) 62 #else 63 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0) 64 #endif 65 66 67 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \ 68 ETH_TXQ_FLAGS_NOOFFLOADS) 69 70 #ifdef RTE_MACHINE_CPUFLAG_SSSE3 71 static int use_simple_rxtx; 72 #endif 73 74 static void 75 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) 76 { 77 struct vring_desc *dp, *dp_tail; 78 struct vq_desc_extra *dxp; 79 uint16_t desc_idx_last = desc_idx; 80 81 dp = &vq->vq_ring.desc[desc_idx]; 82 dxp = &vq->vq_descx[desc_idx]; 83 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs); 84 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) { 85 while (dp->flags & VRING_DESC_F_NEXT) { 86 desc_idx_last = dp->next; 87 dp = &vq->vq_ring.desc[dp->next]; 88 } 89 } 90 dxp->ndescs = 0; 91 92 /* 93 * We must append the existing free chain, if any, to the end of 94 * newly freed chain. If the virtqueue was completely used, then 95 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above). 96 */ 97 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) { 98 vq->vq_desc_head_idx = desc_idx; 99 } else { 100 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx]; 101 dp_tail->next = desc_idx; 102 } 103 104 vq->vq_desc_tail_idx = desc_idx_last; 105 dp->next = VQ_RING_DESC_CHAIN_END; 106 } 107 108 static uint16_t 109 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts, 110 uint32_t *len, uint16_t num) 111 { 112 struct vring_used_elem *uep; 113 struct rte_mbuf *cookie; 114 uint16_t used_idx, desc_idx; 115 uint16_t i; 116 117 /* Caller does the check */ 118 for (i = 0; i < num ; i++) { 119 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1)); 120 uep = &vq->vq_ring.used->ring[used_idx]; 121 desc_idx = (uint16_t) uep->id; 122 len[i] = uep->len; 123 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie; 124 125 if (unlikely(cookie == NULL)) { 126 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n", 127 vq->vq_used_cons_idx); 128 break; 129 } 130 131 rte_prefetch0(cookie); 132 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *)); 133 rx_pkts[i] = cookie; 134 vq->vq_used_cons_idx++; 135 vq_ring_free_chain(vq, desc_idx); 136 vq->vq_descx[desc_idx].cookie = NULL; 137 } 138 139 return i; 140 } 141 142 #ifndef DEFAULT_TX_FREE_THRESH 143 #define DEFAULT_TX_FREE_THRESH 32 144 #endif 145 146 /* Cleanup from completed transmits. */ 147 static void 148 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num) 149 { 150 uint16_t i, used_idx, desc_idx; 151 for (i = 0; i < num; i++) { 152 struct vring_used_elem *uep; 153 struct vq_desc_extra *dxp; 154 155 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1)); 156 uep = &vq->vq_ring.used->ring[used_idx]; 157 158 desc_idx = (uint16_t) uep->id; 159 dxp = &vq->vq_descx[desc_idx]; 160 vq->vq_used_cons_idx++; 161 vq_ring_free_chain(vq, desc_idx); 162 163 if (dxp->cookie != NULL) { 164 rte_pktmbuf_free(dxp->cookie); 165 dxp->cookie = NULL; 166 } 167 } 168 } 169 170 171 static inline int 172 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie) 173 { 174 struct vq_desc_extra *dxp; 175 struct virtio_hw *hw = vq->hw; 176 struct vring_desc *start_dp; 177 uint16_t needed = 1; 178 uint16_t head_idx, idx; 179 180 if (unlikely(vq->vq_free_cnt == 0)) 181 return -ENOSPC; 182 if (unlikely(vq->vq_free_cnt < needed)) 183 return -EMSGSIZE; 184 185 head_idx = vq->vq_desc_head_idx; 186 if (unlikely(head_idx >= vq->vq_nentries)) 187 return -EFAULT; 188 189 idx = head_idx; 190 dxp = &vq->vq_descx[idx]; 191 dxp->cookie = (void *)cookie; 192 dxp->ndescs = needed; 193 194 start_dp = vq->vq_ring.desc; 195 start_dp[idx].addr = 196 MBUF_DATA_DMA_ADDR(cookie, vq->offset) - hw->vtnet_hdr_size; 197 start_dp[idx].len = 198 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size; 199 start_dp[idx].flags = VRING_DESC_F_WRITE; 200 idx = start_dp[idx].next; 201 vq->vq_desc_head_idx = idx; 202 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END) 203 vq->vq_desc_tail_idx = idx; 204 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed); 205 vq_update_avail_ring(vq, head_idx); 206 207 return 0; 208 } 209 210 static inline void 211 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie, 212 uint16_t needed, int use_indirect, int can_push) 213 { 214 struct vq_desc_extra *dxp; 215 struct virtqueue *vq = txvq->vq; 216 struct vring_desc *start_dp; 217 uint16_t seg_num = cookie->nb_segs; 218 uint16_t head_idx, idx; 219 uint16_t head_size = vq->hw->vtnet_hdr_size; 220 unsigned long offs; 221 222 head_idx = vq->vq_desc_head_idx; 223 idx = head_idx; 224 dxp = &vq->vq_descx[idx]; 225 dxp->cookie = (void *)cookie; 226 dxp->ndescs = needed; 227 228 start_dp = vq->vq_ring.desc; 229 230 if (can_push) { 231 /* put on zero'd transmit header (no offloads) */ 232 void *hdr = rte_pktmbuf_prepend(cookie, head_size); 233 234 memset(hdr, 0, head_size); 235 } else if (use_indirect) { 236 /* setup tx ring slot to point to indirect 237 * descriptor list stored in reserved region. 238 * 239 * the first slot in indirect ring is already preset 240 * to point to the header in reserved region 241 */ 242 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr; 243 244 offs = idx * sizeof(struct virtio_tx_region) 245 + offsetof(struct virtio_tx_region, tx_indir); 246 247 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs; 248 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc); 249 start_dp[idx].flags = VRING_DESC_F_INDIRECT; 250 251 /* loop below will fill in rest of the indirect elements */ 252 start_dp = txr[idx].tx_indir; 253 idx = 1; 254 } else { 255 /* setup first tx ring slot to point to header 256 * stored in reserved region. 257 */ 258 offs = idx * sizeof(struct virtio_tx_region) 259 + offsetof(struct virtio_tx_region, tx_hdr); 260 261 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs; 262 start_dp[idx].len = vq->hw->vtnet_hdr_size; 263 start_dp[idx].flags = VRING_DESC_F_NEXT; 264 idx = start_dp[idx].next; 265 } 266 267 do { 268 start_dp[idx].addr = MBUF_DATA_DMA_ADDR(cookie, vq->offset); 269 start_dp[idx].len = cookie->data_len; 270 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0; 271 idx = start_dp[idx].next; 272 } while ((cookie = cookie->next) != NULL); 273 274 if (use_indirect) 275 idx = vq->vq_ring.desc[head_idx].next; 276 277 vq->vq_desc_head_idx = idx; 278 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END) 279 vq->vq_desc_tail_idx = idx; 280 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed); 281 vq_update_avail_ring(vq, head_idx); 282 } 283 284 static void 285 virtio_dev_vring_start(struct virtqueue *vq) 286 { 287 int size = vq->vq_nentries; 288 struct vring *vr = &vq->vq_ring; 289 uint8_t *ring_mem = vq->vq_ring_virt_mem; 290 291 PMD_INIT_FUNC_TRACE(); 292 293 /* 294 * Reinitialise since virtio port might have been stopped and restarted 295 */ 296 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size); 297 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN); 298 vq->vq_used_cons_idx = 0; 299 vq->vq_desc_head_idx = 0; 300 vq->vq_avail_idx = 0; 301 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1); 302 vq->vq_free_cnt = vq->vq_nentries; 303 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries); 304 305 vring_desc_init(vr->desc, size); 306 307 /* 308 * Disable device(host) interrupting guest 309 */ 310 virtqueue_disable_intr(vq); 311 } 312 313 void 314 virtio_dev_cq_start(struct rte_eth_dev *dev) 315 { 316 struct virtio_hw *hw = dev->data->dev_private; 317 318 if (hw->cvq && hw->cvq->vq) { 319 virtio_dev_vring_start(hw->cvq->vq); 320 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq); 321 } 322 } 323 324 void 325 virtio_dev_rxtx_start(struct rte_eth_dev *dev) 326 { 327 /* 328 * Start receive and transmit vrings 329 * - Setup vring structure for all queues 330 * - Initialize descriptor for the rx vring 331 * - Allocate blank mbufs for the each rx descriptor 332 * 333 */ 334 uint16_t i; 335 uint16_t desc_idx; 336 337 PMD_INIT_FUNC_TRACE(); 338 339 /* Start rx vring. */ 340 for (i = 0; i < dev->data->nb_rx_queues; i++) { 341 struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 342 struct virtqueue *vq = rxvq->vq; 343 int error, nbufs; 344 struct rte_mbuf *m; 345 346 virtio_dev_vring_start(vq); 347 if (rxvq->mpool == NULL) { 348 rte_exit(EXIT_FAILURE, 349 "Cannot allocate mbufs for rx virtqueue"); 350 } 351 352 /* Allocate blank mbufs for the each rx descriptor */ 353 nbufs = 0; 354 error = ENOSPC; 355 356 #ifdef RTE_MACHINE_CPUFLAG_SSSE3 357 if (use_simple_rxtx) { 358 for (desc_idx = 0; desc_idx < vq->vq_nentries; 359 desc_idx++) { 360 vq->vq_ring.avail->ring[desc_idx] = desc_idx; 361 vq->vq_ring.desc[desc_idx].flags = 362 VRING_DESC_F_WRITE; 363 } 364 } 365 #endif 366 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf)); 367 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST; 368 desc_idx++) { 369 vq->sw_ring[vq->vq_nentries + desc_idx] = 370 &rxvq->fake_mbuf; 371 } 372 373 while (!virtqueue_full(vq)) { 374 m = rte_mbuf_raw_alloc(rxvq->mpool); 375 if (m == NULL) 376 break; 377 378 /****************************************** 379 * Enqueue allocated buffers * 380 *******************************************/ 381 #ifdef RTE_MACHINE_CPUFLAG_SSSE3 382 if (use_simple_rxtx) 383 error = virtqueue_enqueue_recv_refill_simple(vq, m); 384 else 385 #endif 386 error = virtqueue_enqueue_recv_refill(vq, m); 387 if (error) { 388 rte_pktmbuf_free(m); 389 break; 390 } 391 nbufs++; 392 } 393 394 vq_update_avail_idx(vq); 395 396 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs); 397 398 VIRTQUEUE_DUMP(vq); 399 } 400 401 /* Start tx vring. */ 402 for (i = 0; i < dev->data->nb_tx_queues; i++) { 403 struct virtnet_tx *txvq = dev->data->tx_queues[i]; 404 struct virtqueue *vq = txvq->vq; 405 406 virtio_dev_vring_start(vq); 407 #ifdef RTE_MACHINE_CPUFLAG_SSSE3 408 if (use_simple_rxtx) { 409 uint16_t mid_idx = vq->vq_nentries >> 1; 410 411 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) { 412 vq->vq_ring.avail->ring[desc_idx] = 413 desc_idx + mid_idx; 414 vq->vq_ring.desc[desc_idx + mid_idx].next = 415 desc_idx; 416 vq->vq_ring.desc[desc_idx + mid_idx].addr = 417 txvq->virtio_net_hdr_mem + 418 offsetof(struct virtio_tx_region, tx_hdr); 419 vq->vq_ring.desc[desc_idx + mid_idx].len = 420 vq->hw->vtnet_hdr_size; 421 vq->vq_ring.desc[desc_idx + mid_idx].flags = 422 VRING_DESC_F_NEXT; 423 vq->vq_ring.desc[desc_idx].flags = 0; 424 } 425 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries; 426 desc_idx++) 427 vq->vq_ring.avail->ring[desc_idx] = desc_idx; 428 } 429 #endif 430 VIRTQUEUE_DUMP(vq); 431 } 432 } 433 434 int 435 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev, 436 uint16_t queue_idx, 437 uint16_t nb_desc, 438 unsigned int socket_id, 439 __rte_unused const struct rte_eth_rxconf *rx_conf, 440 struct rte_mempool *mp) 441 { 442 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX; 443 struct virtnet_rx *rxvq; 444 int ret; 445 446 PMD_INIT_FUNC_TRACE(); 447 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx, 448 nb_desc, socket_id, (void **)&rxvq); 449 if (ret < 0) { 450 PMD_INIT_LOG(ERR, "rvq initialization failed"); 451 return ret; 452 } 453 454 /* Create mempool for rx mbuf allocation */ 455 rxvq->mpool = mp; 456 457 dev->data->rx_queues[queue_idx] = rxvq; 458 459 #ifdef RTE_MACHINE_CPUFLAG_SSSE3 460 virtio_rxq_vec_setup(rxvq); 461 #endif 462 463 return 0; 464 } 465 466 void 467 virtio_dev_rx_queue_release(void *rxq) 468 { 469 struct virtnet_rx *rxvq = rxq; 470 struct virtqueue *vq = rxvq->vq; 471 /* rxvq is freed when vq is freed, and as mz should be freed after the 472 * del_queue, so we reserve the mz pointer first. 473 */ 474 const struct rte_memzone *mz = rxvq->mz; 475 476 /* no need to free rxq as vq and rxq are allocated together */ 477 virtio_dev_queue_release(vq); 478 rte_memzone_free(mz); 479 } 480 481 /* 482 * struct rte_eth_dev *dev: Used to update dev 483 * uint16_t nb_desc: Defaults to values read from config space 484 * unsigned int socket_id: Used to allocate memzone 485 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine 486 * uint16_t queue_idx: Just used as an index in dev txq list 487 */ 488 int 489 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev, 490 uint16_t queue_idx, 491 uint16_t nb_desc, 492 unsigned int socket_id, 493 const struct rte_eth_txconf *tx_conf) 494 { 495 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX; 496 497 #ifdef RTE_MACHINE_CPUFLAG_SSSE3 498 struct virtio_hw *hw = dev->data->dev_private; 499 #endif 500 struct virtnet_tx *txvq; 501 struct virtqueue *vq; 502 uint16_t tx_free_thresh; 503 int ret; 504 505 PMD_INIT_FUNC_TRACE(); 506 507 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS) 508 != ETH_TXQ_FLAGS_NOXSUMS) { 509 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n"); 510 return -EINVAL; 511 } 512 513 #ifdef RTE_MACHINE_CPUFLAG_SSSE3 514 /* Use simple rx/tx func if single segment and no offloads */ 515 if ((tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS && 516 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) { 517 PMD_INIT_LOG(INFO, "Using simple rx/tx path"); 518 dev->tx_pkt_burst = virtio_xmit_pkts_simple; 519 dev->rx_pkt_burst = virtio_recv_pkts_vec; 520 use_simple_rxtx = 1; 521 } 522 #endif 523 524 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx, 525 nb_desc, socket_id, (void **)&txvq); 526 if (ret < 0) { 527 PMD_INIT_LOG(ERR, "tvq initialization failed"); 528 return ret; 529 } 530 vq = txvq->vq; 531 532 tx_free_thresh = tx_conf->tx_free_thresh; 533 if (tx_free_thresh == 0) 534 tx_free_thresh = 535 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH); 536 537 if (tx_free_thresh >= (vq->vq_nentries - 3)) { 538 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the " 539 "number of TX entries minus 3 (%u)." 540 " (tx_free_thresh=%u port=%u queue=%u)\n", 541 vq->vq_nentries - 3, 542 tx_free_thresh, dev->data->port_id, queue_idx); 543 return -EINVAL; 544 } 545 546 vq->vq_free_thresh = tx_free_thresh; 547 548 dev->data->tx_queues[queue_idx] = txvq; 549 return 0; 550 } 551 552 void 553 virtio_dev_tx_queue_release(void *txq) 554 { 555 struct virtnet_tx *txvq = txq; 556 struct virtqueue *vq = txvq->vq; 557 /* txvq is freed when vq is freed, and as mz should be freed after the 558 * del_queue, so we reserve the mz pointer first. 559 */ 560 const struct rte_memzone *hdr_mz = txvq->virtio_net_hdr_mz; 561 const struct rte_memzone *mz = txvq->mz; 562 563 virtio_dev_queue_release(vq); 564 rte_memzone_free(mz); 565 rte_memzone_free(hdr_mz); 566 } 567 568 static void 569 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m) 570 { 571 int error; 572 /* 573 * Requeue the discarded mbuf. This should always be 574 * successful since it was just dequeued. 575 */ 576 error = virtqueue_enqueue_recv_refill(vq, m); 577 if (unlikely(error)) { 578 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf"); 579 rte_pktmbuf_free(m); 580 } 581 } 582 583 static void 584 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf) 585 { 586 uint32_t s = mbuf->pkt_len; 587 struct ether_addr *ea; 588 589 if (s == 64) { 590 stats->size_bins[1]++; 591 } else if (s > 64 && s < 1024) { 592 uint32_t bin; 593 594 /* count zeros, and offset into correct bin */ 595 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5; 596 stats->size_bins[bin]++; 597 } else { 598 if (s < 64) 599 stats->size_bins[0]++; 600 else if (s < 1519) 601 stats->size_bins[6]++; 602 else if (s >= 1519) 603 stats->size_bins[7]++; 604 } 605 606 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *); 607 if (is_multicast_ether_addr(ea)) { 608 if (is_broadcast_ether_addr(ea)) 609 stats->broadcast++; 610 else 611 stats->multicast++; 612 } 613 } 614 615 #define VIRTIO_MBUF_BURST_SZ 64 616 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc)) 617 uint16_t 618 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts) 619 { 620 struct virtnet_rx *rxvq = rx_queue; 621 struct virtqueue *vq = rxvq->vq; 622 struct virtio_hw *hw; 623 struct rte_mbuf *rxm, *new_mbuf; 624 uint16_t nb_used, num, nb_rx; 625 uint32_t len[VIRTIO_MBUF_BURST_SZ]; 626 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ]; 627 int error; 628 uint32_t i, nb_enqueued; 629 uint32_t hdr_size; 630 631 nb_used = VIRTQUEUE_NUSED(vq); 632 633 virtio_rmb(); 634 635 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts); 636 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ); 637 if (likely(num > DESC_PER_CACHELINE)) 638 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE); 639 640 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num); 641 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num); 642 643 hw = vq->hw; 644 nb_rx = 0; 645 nb_enqueued = 0; 646 hdr_size = hw->vtnet_hdr_size; 647 648 for (i = 0; i < num ; i++) { 649 rxm = rcv_pkts[i]; 650 651 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]); 652 653 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) { 654 PMD_RX_LOG(ERR, "Packet drop"); 655 nb_enqueued++; 656 virtio_discard_rxbuf(vq, rxm); 657 rxvq->stats.errors++; 658 continue; 659 } 660 661 rxm->port = rxvq->port_id; 662 rxm->data_off = RTE_PKTMBUF_HEADROOM; 663 rxm->ol_flags = 0; 664 rxm->vlan_tci = 0; 665 666 rxm->nb_segs = 1; 667 rxm->next = NULL; 668 rxm->pkt_len = (uint32_t)(len[i] - hdr_size); 669 rxm->data_len = (uint16_t)(len[i] - hdr_size); 670 671 if (hw->vlan_strip) 672 rte_vlan_strip(rxm); 673 674 VIRTIO_DUMP_PACKET(rxm, rxm->data_len); 675 676 rx_pkts[nb_rx++] = rxm; 677 678 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len; 679 virtio_update_packet_stats(&rxvq->stats, rxm); 680 } 681 682 rxvq->stats.packets += nb_rx; 683 684 /* Allocate new mbuf for the used descriptor */ 685 error = ENOSPC; 686 while (likely(!virtqueue_full(vq))) { 687 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool); 688 if (unlikely(new_mbuf == NULL)) { 689 struct rte_eth_dev *dev 690 = &rte_eth_devices[rxvq->port_id]; 691 dev->data->rx_mbuf_alloc_failed++; 692 break; 693 } 694 error = virtqueue_enqueue_recv_refill(vq, new_mbuf); 695 if (unlikely(error)) { 696 rte_pktmbuf_free(new_mbuf); 697 break; 698 } 699 nb_enqueued++; 700 } 701 702 if (likely(nb_enqueued)) { 703 vq_update_avail_idx(vq); 704 705 if (unlikely(virtqueue_kick_prepare(vq))) { 706 virtqueue_notify(vq); 707 PMD_RX_LOG(DEBUG, "Notified"); 708 } 709 } 710 711 return nb_rx; 712 } 713 714 uint16_t 715 virtio_recv_mergeable_pkts(void *rx_queue, 716 struct rte_mbuf **rx_pkts, 717 uint16_t nb_pkts) 718 { 719 struct virtnet_rx *rxvq = rx_queue; 720 struct virtqueue *vq = rxvq->vq; 721 struct virtio_hw *hw; 722 struct rte_mbuf *rxm, *new_mbuf; 723 uint16_t nb_used, num, nb_rx; 724 uint32_t len[VIRTIO_MBUF_BURST_SZ]; 725 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ]; 726 struct rte_mbuf *prev; 727 int error; 728 uint32_t i, nb_enqueued; 729 uint32_t seg_num; 730 uint16_t extra_idx; 731 uint32_t seg_res; 732 uint32_t hdr_size; 733 734 nb_used = VIRTQUEUE_NUSED(vq); 735 736 virtio_rmb(); 737 738 PMD_RX_LOG(DEBUG, "used:%d", nb_used); 739 740 hw = vq->hw; 741 nb_rx = 0; 742 i = 0; 743 nb_enqueued = 0; 744 seg_num = 0; 745 extra_idx = 0; 746 seg_res = 0; 747 hdr_size = hw->vtnet_hdr_size; 748 749 while (i < nb_used) { 750 struct virtio_net_hdr_mrg_rxbuf *header; 751 752 if (nb_rx == nb_pkts) 753 break; 754 755 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1); 756 if (num != 1) 757 continue; 758 759 i++; 760 761 PMD_RX_LOG(DEBUG, "dequeue:%d", num); 762 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]); 763 764 rxm = rcv_pkts[0]; 765 766 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) { 767 PMD_RX_LOG(ERR, "Packet drop"); 768 nb_enqueued++; 769 virtio_discard_rxbuf(vq, rxm); 770 rxvq->stats.errors++; 771 continue; 772 } 773 774 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr + 775 RTE_PKTMBUF_HEADROOM - hdr_size); 776 seg_num = header->num_buffers; 777 778 if (seg_num == 0) 779 seg_num = 1; 780 781 rxm->data_off = RTE_PKTMBUF_HEADROOM; 782 rxm->nb_segs = seg_num; 783 rxm->next = NULL; 784 rxm->ol_flags = 0; 785 rxm->vlan_tci = 0; 786 rxm->pkt_len = (uint32_t)(len[0] - hdr_size); 787 rxm->data_len = (uint16_t)(len[0] - hdr_size); 788 789 rxm->port = rxvq->port_id; 790 rx_pkts[nb_rx] = rxm; 791 prev = rxm; 792 793 seg_res = seg_num - 1; 794 795 while (seg_res != 0) { 796 /* 797 * Get extra segments for current uncompleted packet. 798 */ 799 uint16_t rcv_cnt = 800 RTE_MIN(seg_res, RTE_DIM(rcv_pkts)); 801 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) { 802 uint32_t rx_num = 803 virtqueue_dequeue_burst_rx(vq, 804 rcv_pkts, len, rcv_cnt); 805 i += rx_num; 806 rcv_cnt = rx_num; 807 } else { 808 PMD_RX_LOG(ERR, 809 "No enough segments for packet."); 810 nb_enqueued++; 811 virtio_discard_rxbuf(vq, rxm); 812 rxvq->stats.errors++; 813 break; 814 } 815 816 extra_idx = 0; 817 818 while (extra_idx < rcv_cnt) { 819 rxm = rcv_pkts[extra_idx]; 820 821 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size; 822 rxm->next = NULL; 823 rxm->pkt_len = (uint32_t)(len[extra_idx]); 824 rxm->data_len = (uint16_t)(len[extra_idx]); 825 826 if (prev) 827 prev->next = rxm; 828 829 prev = rxm; 830 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len; 831 extra_idx++; 832 }; 833 seg_res -= rcv_cnt; 834 } 835 836 if (hw->vlan_strip) 837 rte_vlan_strip(rx_pkts[nb_rx]); 838 839 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx], 840 rx_pkts[nb_rx]->data_len); 841 842 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len; 843 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]); 844 nb_rx++; 845 } 846 847 rxvq->stats.packets += nb_rx; 848 849 /* Allocate new mbuf for the used descriptor */ 850 error = ENOSPC; 851 while (likely(!virtqueue_full(vq))) { 852 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool); 853 if (unlikely(new_mbuf == NULL)) { 854 struct rte_eth_dev *dev 855 = &rte_eth_devices[rxvq->port_id]; 856 dev->data->rx_mbuf_alloc_failed++; 857 break; 858 } 859 error = virtqueue_enqueue_recv_refill(vq, new_mbuf); 860 if (unlikely(error)) { 861 rte_pktmbuf_free(new_mbuf); 862 break; 863 } 864 nb_enqueued++; 865 } 866 867 if (likely(nb_enqueued)) { 868 vq_update_avail_idx(vq); 869 870 if (unlikely(virtqueue_kick_prepare(vq))) { 871 virtqueue_notify(vq); 872 PMD_RX_LOG(DEBUG, "Notified"); 873 } 874 } 875 876 return nb_rx; 877 } 878 879 uint16_t 880 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts) 881 { 882 struct virtnet_tx *txvq = tx_queue; 883 struct virtqueue *vq = txvq->vq; 884 struct virtio_hw *hw = vq->hw; 885 uint16_t hdr_size = hw->vtnet_hdr_size; 886 uint16_t nb_used, nb_tx; 887 int error; 888 889 if (unlikely(nb_pkts < 1)) 890 return nb_pkts; 891 892 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts); 893 nb_used = VIRTQUEUE_NUSED(vq); 894 895 virtio_rmb(); 896 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh)) 897 virtio_xmit_cleanup(vq, nb_used); 898 899 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) { 900 struct rte_mbuf *txm = tx_pkts[nb_tx]; 901 int can_push = 0, use_indirect = 0, slots, need; 902 903 /* Do VLAN tag insertion */ 904 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) { 905 error = rte_vlan_insert(&txm); 906 if (unlikely(error)) { 907 rte_pktmbuf_free(txm); 908 continue; 909 } 910 } 911 912 /* optimize ring usage */ 913 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) && 914 rte_mbuf_refcnt_read(txm) == 1 && 915 RTE_MBUF_DIRECT(txm) && 916 txm->nb_segs == 1 && 917 rte_pktmbuf_headroom(txm) >= hdr_size && 918 rte_is_aligned(rte_pktmbuf_mtod(txm, char *), 919 __alignof__(struct virtio_net_hdr_mrg_rxbuf))) 920 can_push = 1; 921 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) && 922 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT) 923 use_indirect = 1; 924 925 /* How many main ring entries are needed to this Tx? 926 * any_layout => number of segments 927 * indirect => 1 928 * default => number of segments + 1 929 */ 930 slots = use_indirect ? 1 : (txm->nb_segs + !can_push); 931 need = slots - vq->vq_free_cnt; 932 933 /* Positive value indicates it need free vring descriptors */ 934 if (unlikely(need > 0)) { 935 nb_used = VIRTQUEUE_NUSED(vq); 936 virtio_rmb(); 937 need = RTE_MIN(need, (int)nb_used); 938 939 virtio_xmit_cleanup(vq, need); 940 need = slots - vq->vq_free_cnt; 941 if (unlikely(need > 0)) { 942 PMD_TX_LOG(ERR, 943 "No free tx descriptors to transmit"); 944 break; 945 } 946 } 947 948 /* Enqueue Packet buffers */ 949 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push); 950 951 txvq->stats.bytes += txm->pkt_len; 952 virtio_update_packet_stats(&txvq->stats, txm); 953 } 954 955 txvq->stats.packets += nb_tx; 956 957 if (likely(nb_tx)) { 958 vq_update_avail_idx(vq); 959 960 if (unlikely(virtqueue_kick_prepare(vq))) { 961 virtqueue_notify(vq); 962 PMD_TX_LOG(DEBUG, "Notified backend after xmit"); 963 } 964 } 965 966 return nb_tx; 967 } 968