1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <string.h> 7 #include <stdio.h> 8 #include <errno.h> 9 #include <unistd.h> 10 11 #include <rte_ethdev_driver.h> 12 #include <rte_ethdev_pci.h> 13 #include <rte_memcpy.h> 14 #include <rte_string_fns.h> 15 #include <rte_memzone.h> 16 #include <rte_malloc.h> 17 #include <rte_branch_prediction.h> 18 #include <rte_pci.h> 19 #include <rte_bus_pci.h> 20 #include <rte_ether.h> 21 #include <rte_ip.h> 22 #include <rte_arp.h> 23 #include <rte_common.h> 24 #include <rte_errno.h> 25 #include <rte_cpuflags.h> 26 27 #include <rte_memory.h> 28 #include <rte_eal.h> 29 #include <rte_dev.h> 30 #include <rte_cycles.h> 31 #include <rte_kvargs.h> 32 33 #include "virtio_ethdev.h" 34 #include "virtio_pci.h" 35 #include "virtio_logs.h" 36 #include "virtqueue.h" 37 #include "virtio_rxtx.h" 38 39 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev); 40 static int virtio_dev_configure(struct rte_eth_dev *dev); 41 static int virtio_dev_start(struct rte_eth_dev *dev); 42 static void virtio_dev_stop(struct rte_eth_dev *dev); 43 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev); 44 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev); 45 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev); 46 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev); 47 static void virtio_dev_info_get(struct rte_eth_dev *dev, 48 struct rte_eth_dev_info *dev_info); 49 static int virtio_dev_link_update(struct rte_eth_dev *dev, 50 int wait_to_complete); 51 static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask); 52 53 static void virtio_set_hwaddr(struct virtio_hw *hw); 54 static void virtio_get_hwaddr(struct virtio_hw *hw); 55 56 static int virtio_dev_stats_get(struct rte_eth_dev *dev, 57 struct rte_eth_stats *stats); 58 static int virtio_dev_xstats_get(struct rte_eth_dev *dev, 59 struct rte_eth_xstat *xstats, unsigned n); 60 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev, 61 struct rte_eth_xstat_name *xstats_names, 62 unsigned limit); 63 static void virtio_dev_stats_reset(struct rte_eth_dev *dev); 64 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev); 65 static int virtio_vlan_filter_set(struct rte_eth_dev *dev, 66 uint16_t vlan_id, int on); 67 static int virtio_mac_addr_add(struct rte_eth_dev *dev, 68 struct ether_addr *mac_addr, 69 uint32_t index, uint32_t vmdq); 70 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index); 71 static int virtio_mac_addr_set(struct rte_eth_dev *dev, 72 struct ether_addr *mac_addr); 73 74 static int virtio_intr_disable(struct rte_eth_dev *dev); 75 76 static int virtio_dev_queue_stats_mapping_set( 77 struct rte_eth_dev *eth_dev, 78 uint16_t queue_id, 79 uint8_t stat_idx, 80 uint8_t is_rx); 81 82 int virtio_logtype_init; 83 int virtio_logtype_driver; 84 85 static void virtio_notify_peers(struct rte_eth_dev *dev); 86 static void virtio_ack_link_announce(struct rte_eth_dev *dev); 87 88 /* 89 * The set of PCI devices this driver supports 90 */ 91 static const struct rte_pci_id pci_id_virtio_map[] = { 92 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) }, 93 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) }, 94 { .vendor_id = 0, /* sentinel */ }, 95 }; 96 97 struct rte_virtio_xstats_name_off { 98 char name[RTE_ETH_XSTATS_NAME_SIZE]; 99 unsigned offset; 100 }; 101 102 /* [rt]x_qX_ is prepended to the name string here */ 103 static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = { 104 {"good_packets", offsetof(struct virtnet_rx, stats.packets)}, 105 {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)}, 106 {"errors", offsetof(struct virtnet_rx, stats.errors)}, 107 {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)}, 108 {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)}, 109 {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])}, 110 {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])}, 111 {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])}, 112 {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])}, 113 {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])}, 114 {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])}, 115 {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])}, 116 {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])}, 117 }; 118 119 /* [rt]x_qX_ is prepended to the name string here */ 120 static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = { 121 {"good_packets", offsetof(struct virtnet_tx, stats.packets)}, 122 {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)}, 123 {"errors", offsetof(struct virtnet_tx, stats.errors)}, 124 {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)}, 125 {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)}, 126 {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])}, 127 {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])}, 128 {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])}, 129 {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])}, 130 {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])}, 131 {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])}, 132 {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])}, 133 {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])}, 134 }; 135 136 #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \ 137 sizeof(rte_virtio_rxq_stat_strings[0])) 138 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \ 139 sizeof(rte_virtio_txq_stat_strings[0])) 140 141 struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS]; 142 143 static struct virtio_pmd_ctrl * 144 virtio_send_command_packed(struct virtnet_ctl *cvq, 145 struct virtio_pmd_ctrl *ctrl, 146 int *dlen, int pkt_num) 147 { 148 struct virtqueue *vq = cvq->vq; 149 int head; 150 struct vring_packed_desc *desc = vq->ring_packed.desc_packed; 151 struct virtio_pmd_ctrl *result; 152 bool avail_wrap_counter; 153 int sum = 0; 154 int nb_descs = 0; 155 int k; 156 157 /* 158 * Format is enforced in qemu code: 159 * One TX packet for header; 160 * At least one TX packet per argument; 161 * One RX packet for ACK. 162 */ 163 head = vq->vq_avail_idx; 164 avail_wrap_counter = vq->avail_wrap_counter; 165 desc[head].addr = cvq->virtio_net_hdr_mem; 166 desc[head].len = sizeof(struct virtio_net_ctrl_hdr); 167 vq->vq_free_cnt--; 168 nb_descs++; 169 if (++vq->vq_avail_idx >= vq->vq_nentries) { 170 vq->vq_avail_idx -= vq->vq_nentries; 171 vq->avail_wrap_counter ^= 1; 172 } 173 174 for (k = 0; k < pkt_num; k++) { 175 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem 176 + sizeof(struct virtio_net_ctrl_hdr) 177 + sizeof(ctrl->status) + sizeof(uint8_t) * sum; 178 desc[vq->vq_avail_idx].len = dlen[k]; 179 desc[vq->vq_avail_idx].flags = VRING_DESC_F_NEXT | 180 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) | 181 VRING_DESC_F_USED(!vq->avail_wrap_counter); 182 sum += dlen[k]; 183 vq->vq_free_cnt--; 184 nb_descs++; 185 if (++vq->vq_avail_idx >= vq->vq_nentries) { 186 vq->vq_avail_idx -= vq->vq_nentries; 187 vq->avail_wrap_counter ^= 1; 188 } 189 } 190 191 desc[vq->vq_avail_idx].addr = cvq->virtio_net_hdr_mem 192 + sizeof(struct virtio_net_ctrl_hdr); 193 desc[vq->vq_avail_idx].len = sizeof(ctrl->status); 194 desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE | 195 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) | 196 VRING_DESC_F_USED(!vq->avail_wrap_counter); 197 vq->vq_free_cnt--; 198 nb_descs++; 199 if (++vq->vq_avail_idx >= vq->vq_nentries) { 200 vq->vq_avail_idx -= vq->vq_nentries; 201 vq->avail_wrap_counter ^= 1; 202 } 203 204 virtio_wmb(vq->hw->weak_barriers); 205 desc[head].flags = VRING_DESC_F_NEXT | 206 VRING_DESC_F_AVAIL(avail_wrap_counter) | 207 VRING_DESC_F_USED(!avail_wrap_counter); 208 209 virtio_wmb(vq->hw->weak_barriers); 210 virtqueue_notify(vq); 211 212 /* wait for used descriptors in virtqueue */ 213 while (!desc_is_used(&desc[head], vq)) 214 usleep(100); 215 216 virtio_rmb(vq->hw->weak_barriers); 217 218 /* now get used descriptors */ 219 vq->vq_free_cnt += nb_descs; 220 vq->vq_used_cons_idx += nb_descs; 221 if (vq->vq_used_cons_idx >= vq->vq_nentries) { 222 vq->vq_used_cons_idx -= vq->vq_nentries; 223 vq->used_wrap_counter ^= 1; 224 } 225 226 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\n" 227 "vq->vq_avail_idx=%d\n" 228 "vq->vq_used_cons_idx=%d\n" 229 "vq->avail_wrap_counter=%d\n" 230 "vq->used_wrap_counter=%d\n", 231 vq->vq_free_cnt, 232 vq->vq_avail_idx, 233 vq->vq_used_cons_idx, 234 vq->avail_wrap_counter, 235 vq->used_wrap_counter); 236 237 result = cvq->virtio_net_hdr_mz->addr; 238 return result; 239 } 240 241 static int 242 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl, 243 int *dlen, int pkt_num) 244 { 245 uint32_t head, i; 246 int k, sum = 0; 247 virtio_net_ctrl_ack status = ~0; 248 struct virtio_pmd_ctrl *result; 249 struct virtqueue *vq; 250 251 ctrl->status = status; 252 253 if (!cvq || !cvq->vq) { 254 PMD_INIT_LOG(ERR, "Control queue is not supported."); 255 return -1; 256 } 257 258 rte_spinlock_lock(&cvq->lock); 259 vq = cvq->vq; 260 head = vq->vq_desc_head_idx; 261 262 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, " 263 "vq->hw->cvq = %p vq = %p", 264 vq->vq_desc_head_idx, status, vq->hw->cvq, vq); 265 266 if (vq->vq_free_cnt < pkt_num + 2 || pkt_num < 1) { 267 rte_spinlock_unlock(&cvq->lock); 268 return -1; 269 } 270 271 memcpy(cvq->virtio_net_hdr_mz->addr, ctrl, 272 sizeof(struct virtio_pmd_ctrl)); 273 274 if (vtpci_packed_queue(vq->hw)) { 275 result = virtio_send_command_packed(cvq, ctrl, dlen, pkt_num); 276 goto out_unlock; 277 } 278 279 /* 280 * Format is enforced in qemu code: 281 * One TX packet for header; 282 * At least one TX packet per argument; 283 * One RX packet for ACK. 284 */ 285 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT; 286 vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem; 287 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr); 288 vq->vq_free_cnt--; 289 i = vq->vq_ring.desc[head].next; 290 291 for (k = 0; k < pkt_num; k++) { 292 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT; 293 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem 294 + sizeof(struct virtio_net_ctrl_hdr) 295 + sizeof(ctrl->status) + sizeof(uint8_t)*sum; 296 vq->vq_ring.desc[i].len = dlen[k]; 297 sum += dlen[k]; 298 vq->vq_free_cnt--; 299 i = vq->vq_ring.desc[i].next; 300 } 301 302 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE; 303 vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem 304 + sizeof(struct virtio_net_ctrl_hdr); 305 vq->vq_ring.desc[i].len = sizeof(ctrl->status); 306 vq->vq_free_cnt--; 307 308 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next; 309 310 vq_update_avail_ring(vq, head); 311 vq_update_avail_idx(vq); 312 313 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index); 314 315 virtqueue_notify(vq); 316 317 rte_rmb(); 318 while (VIRTQUEUE_NUSED(vq) == 0) { 319 rte_rmb(); 320 usleep(100); 321 } 322 323 while (VIRTQUEUE_NUSED(vq)) { 324 uint32_t idx, desc_idx, used_idx; 325 struct vring_used_elem *uep; 326 327 used_idx = (uint32_t)(vq->vq_used_cons_idx 328 & (vq->vq_nentries - 1)); 329 uep = &vq->vq_ring.used->ring[used_idx]; 330 idx = (uint32_t) uep->id; 331 desc_idx = idx; 332 333 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) { 334 desc_idx = vq->vq_ring.desc[desc_idx].next; 335 vq->vq_free_cnt++; 336 } 337 338 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx; 339 vq->vq_desc_head_idx = idx; 340 341 vq->vq_used_cons_idx++; 342 vq->vq_free_cnt++; 343 } 344 345 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d", 346 vq->vq_free_cnt, vq->vq_desc_head_idx); 347 348 result = cvq->virtio_net_hdr_mz->addr; 349 350 out_unlock: 351 rte_spinlock_unlock(&cvq->lock); 352 return result->status; 353 } 354 355 static int 356 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues) 357 { 358 struct virtio_hw *hw = dev->data->dev_private; 359 struct virtio_pmd_ctrl ctrl; 360 int dlen[1]; 361 int ret; 362 363 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ; 364 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; 365 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t)); 366 367 dlen[0] = sizeof(uint16_t); 368 369 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 370 if (ret) { 371 PMD_INIT_LOG(ERR, "Multiqueue configured but send command " 372 "failed, this is too late now..."); 373 return -EINVAL; 374 } 375 376 return 0; 377 } 378 379 static void 380 virtio_dev_queue_release(void *queue __rte_unused) 381 { 382 /* do nothing */ 383 } 384 385 static uint16_t 386 virtio_get_nr_vq(struct virtio_hw *hw) 387 { 388 uint16_t nr_vq = hw->max_queue_pairs * 2; 389 390 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) 391 nr_vq += 1; 392 393 return nr_vq; 394 } 395 396 static void 397 virtio_init_vring(struct virtqueue *vq) 398 { 399 int size = vq->vq_nentries; 400 struct vring *vr = &vq->vq_ring; 401 uint8_t *ring_mem = vq->vq_ring_virt_mem; 402 403 PMD_INIT_FUNC_TRACE(); 404 405 memset(ring_mem, 0, vq->vq_ring_size); 406 407 vq->vq_used_cons_idx = 0; 408 vq->vq_desc_head_idx = 0; 409 vq->vq_avail_idx = 0; 410 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1); 411 vq->vq_free_cnt = vq->vq_nentries; 412 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries); 413 if (vtpci_packed_queue(vq->hw)) { 414 vring_init_packed(&vq->ring_packed, ring_mem, 415 VIRTIO_PCI_VRING_ALIGN, size); 416 vring_desc_init_packed(vq, size); 417 } else { 418 vring_init_split(vr, ring_mem, VIRTIO_PCI_VRING_ALIGN, size); 419 vring_desc_init_split(vr->desc, size); 420 } 421 /* 422 * Disable device(host) interrupting guest 423 */ 424 virtqueue_disable_intr(vq); 425 } 426 427 static int 428 virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx) 429 { 430 char vq_name[VIRTQUEUE_MAX_NAME_SZ]; 431 char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ]; 432 const struct rte_memzone *mz = NULL, *hdr_mz = NULL; 433 unsigned int vq_size, size; 434 struct virtio_hw *hw = dev->data->dev_private; 435 struct virtnet_rx *rxvq = NULL; 436 struct virtnet_tx *txvq = NULL; 437 struct virtnet_ctl *cvq = NULL; 438 struct virtqueue *vq; 439 size_t sz_hdr_mz = 0; 440 void *sw_ring = NULL; 441 int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx); 442 int ret; 443 int numa_node = dev->device->numa_node; 444 445 PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d", 446 vtpci_queue_idx, numa_node); 447 448 /* 449 * Read the virtqueue size from the Queue Size field 450 * Always power of 2 and if 0 virtqueue does not exist 451 */ 452 vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx); 453 PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size); 454 if (vq_size == 0) { 455 PMD_INIT_LOG(ERR, "virtqueue does not exist"); 456 return -EINVAL; 457 } 458 459 if (!rte_is_power_of_2(vq_size)) { 460 PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2"); 461 return -EINVAL; 462 } 463 464 snprintf(vq_name, sizeof(vq_name), "port%d_vq%d", 465 dev->data->port_id, vtpci_queue_idx); 466 467 size = RTE_ALIGN_CEIL(sizeof(*vq) + 468 vq_size * sizeof(struct vq_desc_extra), 469 RTE_CACHE_LINE_SIZE); 470 if (queue_type == VTNET_TQ) { 471 /* 472 * For each xmit packet, allocate a virtio_net_hdr 473 * and indirect ring elements 474 */ 475 sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region); 476 } else if (queue_type == VTNET_CQ) { 477 /* Allocate a page for control vq command, data and status */ 478 sz_hdr_mz = PAGE_SIZE; 479 } 480 481 vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE, 482 numa_node); 483 if (vq == NULL) { 484 PMD_INIT_LOG(ERR, "can not allocate vq"); 485 return -ENOMEM; 486 } 487 hw->vqs[vtpci_queue_idx] = vq; 488 489 vq->hw = hw; 490 vq->vq_queue_index = vtpci_queue_idx; 491 vq->vq_nentries = vq_size; 492 vq->event_flags_shadow = 0; 493 if (vtpci_packed_queue(hw)) { 494 vq->avail_wrap_counter = 1; 495 vq->used_wrap_counter = 1; 496 vq->avail_used_flags = 497 VRING_DESC_F_AVAIL(vq->avail_wrap_counter) | 498 VRING_DESC_F_USED(!vq->avail_wrap_counter); 499 } 500 501 /* 502 * Reserve a memzone for vring elements 503 */ 504 size = vring_size(hw, vq_size, VIRTIO_PCI_VRING_ALIGN); 505 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN); 506 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", 507 size, vq->vq_ring_size); 508 509 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size, 510 numa_node, RTE_MEMZONE_IOVA_CONTIG, 511 VIRTIO_PCI_VRING_ALIGN); 512 if (mz == NULL) { 513 if (rte_errno == EEXIST) 514 mz = rte_memzone_lookup(vq_name); 515 if (mz == NULL) { 516 ret = -ENOMEM; 517 goto fail_q_alloc; 518 } 519 } 520 521 memset(mz->addr, 0, mz->len); 522 523 vq->vq_ring_mem = mz->iova; 524 vq->vq_ring_virt_mem = mz->addr; 525 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64, 526 (uint64_t)mz->iova); 527 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64, 528 (uint64_t)(uintptr_t)mz->addr); 529 530 virtio_init_vring(vq); 531 532 if (sz_hdr_mz) { 533 snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr", 534 dev->data->port_id, vtpci_queue_idx); 535 hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz, 536 numa_node, RTE_MEMZONE_IOVA_CONTIG, 537 RTE_CACHE_LINE_SIZE); 538 if (hdr_mz == NULL) { 539 if (rte_errno == EEXIST) 540 hdr_mz = rte_memzone_lookup(vq_hdr_name); 541 if (hdr_mz == NULL) { 542 ret = -ENOMEM; 543 goto fail_q_alloc; 544 } 545 } 546 } 547 548 if (queue_type == VTNET_RQ) { 549 size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) * 550 sizeof(vq->sw_ring[0]); 551 552 sw_ring = rte_zmalloc_socket("sw_ring", sz_sw, 553 RTE_CACHE_LINE_SIZE, numa_node); 554 if (!sw_ring) { 555 PMD_INIT_LOG(ERR, "can not allocate RX soft ring"); 556 ret = -ENOMEM; 557 goto fail_q_alloc; 558 } 559 560 vq->sw_ring = sw_ring; 561 rxvq = &vq->rxq; 562 rxvq->vq = vq; 563 rxvq->port_id = dev->data->port_id; 564 rxvq->mz = mz; 565 } else if (queue_type == VTNET_TQ) { 566 txvq = &vq->txq; 567 txvq->vq = vq; 568 txvq->port_id = dev->data->port_id; 569 txvq->mz = mz; 570 txvq->virtio_net_hdr_mz = hdr_mz; 571 txvq->virtio_net_hdr_mem = hdr_mz->iova; 572 } else if (queue_type == VTNET_CQ) { 573 cvq = &vq->cq; 574 cvq->vq = vq; 575 cvq->mz = mz; 576 cvq->virtio_net_hdr_mz = hdr_mz; 577 cvq->virtio_net_hdr_mem = hdr_mz->iova; 578 memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE); 579 580 hw->cvq = cvq; 581 } 582 583 /* For virtio_user case (that is when hw->dev is NULL), we use 584 * virtual address. And we need properly set _offset_, please see 585 * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information. 586 */ 587 if (!hw->virtio_user_dev) 588 vq->offset = offsetof(struct rte_mbuf, buf_iova); 589 else { 590 vq->vq_ring_mem = (uintptr_t)mz->addr; 591 vq->offset = offsetof(struct rte_mbuf, buf_addr); 592 if (queue_type == VTNET_TQ) 593 txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr; 594 else if (queue_type == VTNET_CQ) 595 cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr; 596 } 597 598 if (queue_type == VTNET_TQ) { 599 struct virtio_tx_region *txr; 600 unsigned int i; 601 602 txr = hdr_mz->addr; 603 memset(txr, 0, vq_size * sizeof(*txr)); 604 for (i = 0; i < vq_size; i++) { 605 struct vring_desc *start_dp = txr[i].tx_indir; 606 struct vring_packed_desc *start_dp_packed = 607 txr[i].tx_indir_pq; 608 609 /* first indirect descriptor is always the tx header */ 610 if (vtpci_packed_queue(hw)) { 611 start_dp_packed->addr = txvq->virtio_net_hdr_mem 612 + i * sizeof(*txr) 613 + offsetof(struct virtio_tx_region, 614 tx_hdr); 615 start_dp_packed->len = hw->vtnet_hdr_size; 616 } else { 617 vring_desc_init_split(start_dp, 618 RTE_DIM(txr[i].tx_indir)); 619 start_dp->addr = txvq->virtio_net_hdr_mem 620 + i * sizeof(*txr) 621 + offsetof(struct virtio_tx_region, 622 tx_hdr); 623 start_dp->len = hw->vtnet_hdr_size; 624 start_dp->flags = VRING_DESC_F_NEXT; 625 } 626 } 627 } 628 629 if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) { 630 PMD_INIT_LOG(ERR, "setup_queue failed"); 631 return -EINVAL; 632 } 633 634 return 0; 635 636 fail_q_alloc: 637 rte_free(sw_ring); 638 rte_memzone_free(hdr_mz); 639 rte_memzone_free(mz); 640 rte_free(vq); 641 642 return ret; 643 } 644 645 static void 646 virtio_free_queues(struct virtio_hw *hw) 647 { 648 uint16_t nr_vq = virtio_get_nr_vq(hw); 649 struct virtqueue *vq; 650 int queue_type; 651 uint16_t i; 652 653 if (hw->vqs == NULL) 654 return; 655 656 for (i = 0; i < nr_vq; i++) { 657 vq = hw->vqs[i]; 658 if (!vq) 659 continue; 660 661 queue_type = virtio_get_queue_type(hw, i); 662 if (queue_type == VTNET_RQ) { 663 rte_free(vq->sw_ring); 664 rte_memzone_free(vq->rxq.mz); 665 } else if (queue_type == VTNET_TQ) { 666 rte_memzone_free(vq->txq.mz); 667 rte_memzone_free(vq->txq.virtio_net_hdr_mz); 668 } else { 669 rte_memzone_free(vq->cq.mz); 670 rte_memzone_free(vq->cq.virtio_net_hdr_mz); 671 } 672 673 rte_free(vq); 674 hw->vqs[i] = NULL; 675 } 676 677 rte_free(hw->vqs); 678 hw->vqs = NULL; 679 } 680 681 static int 682 virtio_alloc_queues(struct rte_eth_dev *dev) 683 { 684 struct virtio_hw *hw = dev->data->dev_private; 685 uint16_t nr_vq = virtio_get_nr_vq(hw); 686 uint16_t i; 687 int ret; 688 689 hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0); 690 if (!hw->vqs) { 691 PMD_INIT_LOG(ERR, "failed to allocate vqs"); 692 return -ENOMEM; 693 } 694 695 for (i = 0; i < nr_vq; i++) { 696 ret = virtio_init_queue(dev, i); 697 if (ret < 0) { 698 virtio_free_queues(hw); 699 return ret; 700 } 701 } 702 703 return 0; 704 } 705 706 static void virtio_queues_unbind_intr(struct rte_eth_dev *dev); 707 708 static void 709 virtio_dev_close(struct rte_eth_dev *dev) 710 { 711 struct virtio_hw *hw = dev->data->dev_private; 712 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf; 713 714 PMD_INIT_LOG(DEBUG, "virtio_dev_close"); 715 716 if (!hw->opened) 717 return; 718 hw->opened = false; 719 720 /* reset the NIC */ 721 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 722 VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR); 723 if (intr_conf->rxq) 724 virtio_queues_unbind_intr(dev); 725 726 if (intr_conf->lsc || intr_conf->rxq) { 727 virtio_intr_disable(dev); 728 rte_intr_efd_disable(dev->intr_handle); 729 rte_free(dev->intr_handle->intr_vec); 730 dev->intr_handle->intr_vec = NULL; 731 } 732 733 vtpci_reset(hw); 734 virtio_dev_free_mbufs(dev); 735 virtio_free_queues(hw); 736 } 737 738 static void 739 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev) 740 { 741 struct virtio_hw *hw = dev->data->dev_private; 742 struct virtio_pmd_ctrl ctrl; 743 int dlen[1]; 744 int ret; 745 746 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 747 PMD_INIT_LOG(INFO, "host does not support rx control"); 748 return; 749 } 750 751 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 752 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 753 ctrl.data[0] = 1; 754 dlen[0] = 1; 755 756 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 757 if (ret) 758 PMD_INIT_LOG(ERR, "Failed to enable promisc"); 759 } 760 761 static void 762 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev) 763 { 764 struct virtio_hw *hw = dev->data->dev_private; 765 struct virtio_pmd_ctrl ctrl; 766 int dlen[1]; 767 int ret; 768 769 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 770 PMD_INIT_LOG(INFO, "host does not support rx control"); 771 return; 772 } 773 774 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 775 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 776 ctrl.data[0] = 0; 777 dlen[0] = 1; 778 779 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 780 if (ret) 781 PMD_INIT_LOG(ERR, "Failed to disable promisc"); 782 } 783 784 static void 785 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev) 786 { 787 struct virtio_hw *hw = dev->data->dev_private; 788 struct virtio_pmd_ctrl ctrl; 789 int dlen[1]; 790 int ret; 791 792 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 793 PMD_INIT_LOG(INFO, "host does not support rx control"); 794 return; 795 } 796 797 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 798 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 799 ctrl.data[0] = 1; 800 dlen[0] = 1; 801 802 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 803 if (ret) 804 PMD_INIT_LOG(ERR, "Failed to enable allmulticast"); 805 } 806 807 static void 808 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev) 809 { 810 struct virtio_hw *hw = dev->data->dev_private; 811 struct virtio_pmd_ctrl ctrl; 812 int dlen[1]; 813 int ret; 814 815 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 816 PMD_INIT_LOG(INFO, "host does not support rx control"); 817 return; 818 } 819 820 ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 821 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 822 ctrl.data[0] = 0; 823 dlen[0] = 1; 824 825 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 826 if (ret) 827 PMD_INIT_LOG(ERR, "Failed to disable allmulticast"); 828 } 829 830 #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */ 831 static int 832 virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu) 833 { 834 struct virtio_hw *hw = dev->data->dev_private; 835 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN + 836 hw->vtnet_hdr_size; 837 uint32_t frame_size = mtu + ether_hdr_len; 838 uint32_t max_frame_size = hw->max_mtu + ether_hdr_len; 839 840 max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN); 841 842 if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) { 843 PMD_INIT_LOG(ERR, "MTU should be between %d and %d", 844 ETHER_MIN_MTU, max_frame_size - ether_hdr_len); 845 return -EINVAL; 846 } 847 return 0; 848 } 849 850 static int 851 virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id) 852 { 853 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id]; 854 struct virtqueue *vq = rxvq->vq; 855 856 virtqueue_enable_intr(vq); 857 return 0; 858 } 859 860 static int 861 virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id) 862 { 863 struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id]; 864 struct virtqueue *vq = rxvq->vq; 865 866 virtqueue_disable_intr(vq); 867 return 0; 868 } 869 870 /* 871 * dev_ops for virtio, bare necessities for basic operation 872 */ 873 static const struct eth_dev_ops virtio_eth_dev_ops = { 874 .dev_configure = virtio_dev_configure, 875 .dev_start = virtio_dev_start, 876 .dev_stop = virtio_dev_stop, 877 .dev_close = virtio_dev_close, 878 .promiscuous_enable = virtio_dev_promiscuous_enable, 879 .promiscuous_disable = virtio_dev_promiscuous_disable, 880 .allmulticast_enable = virtio_dev_allmulticast_enable, 881 .allmulticast_disable = virtio_dev_allmulticast_disable, 882 .mtu_set = virtio_mtu_set, 883 .dev_infos_get = virtio_dev_info_get, 884 .stats_get = virtio_dev_stats_get, 885 .xstats_get = virtio_dev_xstats_get, 886 .xstats_get_names = virtio_dev_xstats_get_names, 887 .stats_reset = virtio_dev_stats_reset, 888 .xstats_reset = virtio_dev_stats_reset, 889 .link_update = virtio_dev_link_update, 890 .vlan_offload_set = virtio_dev_vlan_offload_set, 891 .rx_queue_setup = virtio_dev_rx_queue_setup, 892 .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable, 893 .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable, 894 .rx_queue_release = virtio_dev_queue_release, 895 .rx_descriptor_done = virtio_dev_rx_queue_done, 896 .tx_queue_setup = virtio_dev_tx_queue_setup, 897 .tx_queue_release = virtio_dev_queue_release, 898 /* collect stats per queue */ 899 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set, 900 .vlan_filter_set = virtio_vlan_filter_set, 901 .mac_addr_add = virtio_mac_addr_add, 902 .mac_addr_remove = virtio_mac_addr_remove, 903 .mac_addr_set = virtio_mac_addr_set, 904 }; 905 906 static void 907 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 908 { 909 unsigned i; 910 911 for (i = 0; i < dev->data->nb_tx_queues; i++) { 912 const struct virtnet_tx *txvq = dev->data->tx_queues[i]; 913 if (txvq == NULL) 914 continue; 915 916 stats->opackets += txvq->stats.packets; 917 stats->obytes += txvq->stats.bytes; 918 stats->oerrors += txvq->stats.errors; 919 920 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 921 stats->q_opackets[i] = txvq->stats.packets; 922 stats->q_obytes[i] = txvq->stats.bytes; 923 } 924 } 925 926 for (i = 0; i < dev->data->nb_rx_queues; i++) { 927 const struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 928 if (rxvq == NULL) 929 continue; 930 931 stats->ipackets += rxvq->stats.packets; 932 stats->ibytes += rxvq->stats.bytes; 933 stats->ierrors += rxvq->stats.errors; 934 935 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 936 stats->q_ipackets[i] = rxvq->stats.packets; 937 stats->q_ibytes[i] = rxvq->stats.bytes; 938 } 939 } 940 941 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; 942 } 943 944 static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev, 945 struct rte_eth_xstat_name *xstats_names, 946 __rte_unused unsigned limit) 947 { 948 unsigned i; 949 unsigned count = 0; 950 unsigned t; 951 952 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS + 953 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS; 954 955 if (xstats_names != NULL) { 956 /* Note: limit checked in rte_eth_xstats_names() */ 957 958 for (i = 0; i < dev->data->nb_rx_queues; i++) { 959 struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 960 if (rxvq == NULL) 961 continue; 962 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) { 963 snprintf(xstats_names[count].name, 964 sizeof(xstats_names[count].name), 965 "rx_q%u_%s", i, 966 rte_virtio_rxq_stat_strings[t].name); 967 count++; 968 } 969 } 970 971 for (i = 0; i < dev->data->nb_tx_queues; i++) { 972 struct virtnet_tx *txvq = dev->data->tx_queues[i]; 973 if (txvq == NULL) 974 continue; 975 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) { 976 snprintf(xstats_names[count].name, 977 sizeof(xstats_names[count].name), 978 "tx_q%u_%s", i, 979 rte_virtio_txq_stat_strings[t].name); 980 count++; 981 } 982 } 983 return count; 984 } 985 return nstats; 986 } 987 988 static int 989 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats, 990 unsigned n) 991 { 992 unsigned i; 993 unsigned count = 0; 994 995 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS + 996 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS; 997 998 if (n < nstats) 999 return nstats; 1000 1001 for (i = 0; i < dev->data->nb_rx_queues; i++) { 1002 struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 1003 1004 if (rxvq == NULL) 1005 continue; 1006 1007 unsigned t; 1008 1009 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) { 1010 xstats[count].value = *(uint64_t *)(((char *)rxvq) + 1011 rte_virtio_rxq_stat_strings[t].offset); 1012 xstats[count].id = count; 1013 count++; 1014 } 1015 } 1016 1017 for (i = 0; i < dev->data->nb_tx_queues; i++) { 1018 struct virtnet_tx *txvq = dev->data->tx_queues[i]; 1019 1020 if (txvq == NULL) 1021 continue; 1022 1023 unsigned t; 1024 1025 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) { 1026 xstats[count].value = *(uint64_t *)(((char *)txvq) + 1027 rte_virtio_txq_stat_strings[t].offset); 1028 xstats[count].id = count; 1029 count++; 1030 } 1031 } 1032 1033 return count; 1034 } 1035 1036 static int 1037 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 1038 { 1039 virtio_update_stats(dev, stats); 1040 1041 return 0; 1042 } 1043 1044 static void 1045 virtio_dev_stats_reset(struct rte_eth_dev *dev) 1046 { 1047 unsigned int i; 1048 1049 for (i = 0; i < dev->data->nb_tx_queues; i++) { 1050 struct virtnet_tx *txvq = dev->data->tx_queues[i]; 1051 if (txvq == NULL) 1052 continue; 1053 1054 txvq->stats.packets = 0; 1055 txvq->stats.bytes = 0; 1056 txvq->stats.errors = 0; 1057 txvq->stats.multicast = 0; 1058 txvq->stats.broadcast = 0; 1059 memset(txvq->stats.size_bins, 0, 1060 sizeof(txvq->stats.size_bins[0]) * 8); 1061 } 1062 1063 for (i = 0; i < dev->data->nb_rx_queues; i++) { 1064 struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 1065 if (rxvq == NULL) 1066 continue; 1067 1068 rxvq->stats.packets = 0; 1069 rxvq->stats.bytes = 0; 1070 rxvq->stats.errors = 0; 1071 rxvq->stats.multicast = 0; 1072 rxvq->stats.broadcast = 0; 1073 memset(rxvq->stats.size_bins, 0, 1074 sizeof(rxvq->stats.size_bins[0]) * 8); 1075 } 1076 } 1077 1078 static void 1079 virtio_set_hwaddr(struct virtio_hw *hw) 1080 { 1081 vtpci_write_dev_config(hw, 1082 offsetof(struct virtio_net_config, mac), 1083 &hw->mac_addr, ETHER_ADDR_LEN); 1084 } 1085 1086 static void 1087 virtio_get_hwaddr(struct virtio_hw *hw) 1088 { 1089 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) { 1090 vtpci_read_dev_config(hw, 1091 offsetof(struct virtio_net_config, mac), 1092 &hw->mac_addr, ETHER_ADDR_LEN); 1093 } else { 1094 eth_random_addr(&hw->mac_addr[0]); 1095 virtio_set_hwaddr(hw); 1096 } 1097 } 1098 1099 static int 1100 virtio_mac_table_set(struct virtio_hw *hw, 1101 const struct virtio_net_ctrl_mac *uc, 1102 const struct virtio_net_ctrl_mac *mc) 1103 { 1104 struct virtio_pmd_ctrl ctrl; 1105 int err, len[2]; 1106 1107 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 1108 PMD_DRV_LOG(INFO, "host does not support mac table"); 1109 return -1; 1110 } 1111 1112 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 1113 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET; 1114 1115 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries); 1116 memcpy(ctrl.data, uc, len[0]); 1117 1118 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries); 1119 memcpy(ctrl.data + len[0], mc, len[1]); 1120 1121 err = virtio_send_command(hw->cvq, &ctrl, len, 2); 1122 if (err != 0) 1123 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err); 1124 return err; 1125 } 1126 1127 static int 1128 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr, 1129 uint32_t index, uint32_t vmdq __rte_unused) 1130 { 1131 struct virtio_hw *hw = dev->data->dev_private; 1132 const struct ether_addr *addrs = dev->data->mac_addrs; 1133 unsigned int i; 1134 struct virtio_net_ctrl_mac *uc, *mc; 1135 1136 if (index >= VIRTIO_MAX_MAC_ADDRS) { 1137 PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 1138 return -EINVAL; 1139 } 1140 1141 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 1142 uc->entries = 0; 1143 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 1144 mc->entries = 0; 1145 1146 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 1147 const struct ether_addr *addr 1148 = (i == index) ? mac_addr : addrs + i; 1149 struct virtio_net_ctrl_mac *tbl 1150 = is_multicast_ether_addr(addr) ? mc : uc; 1151 1152 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN); 1153 } 1154 1155 return virtio_mac_table_set(hw, uc, mc); 1156 } 1157 1158 static void 1159 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index) 1160 { 1161 struct virtio_hw *hw = dev->data->dev_private; 1162 struct ether_addr *addrs = dev->data->mac_addrs; 1163 struct virtio_net_ctrl_mac *uc, *mc; 1164 unsigned int i; 1165 1166 if (index >= VIRTIO_MAX_MAC_ADDRS) { 1167 PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 1168 return; 1169 } 1170 1171 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 1172 uc->entries = 0; 1173 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 1174 mc->entries = 0; 1175 1176 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 1177 struct virtio_net_ctrl_mac *tbl; 1178 1179 if (i == index || is_zero_ether_addr(addrs + i)) 1180 continue; 1181 1182 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc; 1183 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN); 1184 } 1185 1186 virtio_mac_table_set(hw, uc, mc); 1187 } 1188 1189 static int 1190 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr) 1191 { 1192 struct virtio_hw *hw = dev->data->dev_private; 1193 1194 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN); 1195 1196 /* Use atomic update if available */ 1197 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 1198 struct virtio_pmd_ctrl ctrl; 1199 int len = ETHER_ADDR_LEN; 1200 1201 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 1202 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET; 1203 1204 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN); 1205 return virtio_send_command(hw->cvq, &ctrl, &len, 1); 1206 } 1207 1208 if (!vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) 1209 return -ENOTSUP; 1210 1211 virtio_set_hwaddr(hw); 1212 return 0; 1213 } 1214 1215 static int 1216 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on) 1217 { 1218 struct virtio_hw *hw = dev->data->dev_private; 1219 struct virtio_pmd_ctrl ctrl; 1220 int len; 1221 1222 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) 1223 return -ENOTSUP; 1224 1225 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN; 1226 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL; 1227 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id)); 1228 len = sizeof(vlan_id); 1229 1230 return virtio_send_command(hw->cvq, &ctrl, &len, 1); 1231 } 1232 1233 static int 1234 virtio_intr_enable(struct rte_eth_dev *dev) 1235 { 1236 struct virtio_hw *hw = dev->data->dev_private; 1237 1238 if (rte_intr_enable(dev->intr_handle) < 0) 1239 return -1; 1240 1241 if (!hw->virtio_user_dev) 1242 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev)); 1243 1244 return 0; 1245 } 1246 1247 static int 1248 virtio_intr_disable(struct rte_eth_dev *dev) 1249 { 1250 struct virtio_hw *hw = dev->data->dev_private; 1251 1252 if (rte_intr_disable(dev->intr_handle) < 0) 1253 return -1; 1254 1255 if (!hw->virtio_user_dev) 1256 hw->use_msix = vtpci_msix_detect(RTE_ETH_DEV_TO_PCI(dev)); 1257 1258 return 0; 1259 } 1260 1261 static int 1262 virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features) 1263 { 1264 uint64_t host_features; 1265 1266 /* Prepare guest_features: feature that driver wants to support */ 1267 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64, 1268 req_features); 1269 1270 /* Read device(host) feature bits */ 1271 host_features = VTPCI_OPS(hw)->get_features(hw); 1272 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64, 1273 host_features); 1274 1275 /* If supported, ensure MTU value is valid before acknowledging it. */ 1276 if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) { 1277 struct virtio_net_config config; 1278 1279 vtpci_read_dev_config(hw, 1280 offsetof(struct virtio_net_config, mtu), 1281 &config.mtu, sizeof(config.mtu)); 1282 1283 if (config.mtu < ETHER_MIN_MTU) 1284 req_features &= ~(1ULL << VIRTIO_NET_F_MTU); 1285 } 1286 1287 /* 1288 * Negotiate features: Subset of device feature bits are written back 1289 * guest feature bits. 1290 */ 1291 hw->guest_features = req_features; 1292 hw->guest_features = vtpci_negotiate_features(hw, host_features); 1293 PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64, 1294 hw->guest_features); 1295 1296 if (hw->modern) { 1297 if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) { 1298 PMD_INIT_LOG(ERR, 1299 "VIRTIO_F_VERSION_1 features is not enabled."); 1300 return -1; 1301 } 1302 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK); 1303 if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) { 1304 PMD_INIT_LOG(ERR, 1305 "failed to set FEATURES_OK status!"); 1306 return -1; 1307 } 1308 } 1309 1310 hw->req_guest_features = req_features; 1311 1312 return 0; 1313 } 1314 1315 int 1316 virtio_dev_pause(struct rte_eth_dev *dev) 1317 { 1318 struct virtio_hw *hw = dev->data->dev_private; 1319 1320 rte_spinlock_lock(&hw->state_lock); 1321 1322 if (hw->started == 0) { 1323 /* Device is just stopped. */ 1324 rte_spinlock_unlock(&hw->state_lock); 1325 return -1; 1326 } 1327 hw->started = 0; 1328 /* 1329 * Prevent the worker threads from touching queues to avoid contention, 1330 * 1 ms should be enough for the ongoing Tx function to finish. 1331 */ 1332 rte_delay_ms(1); 1333 return 0; 1334 } 1335 1336 /* 1337 * Recover hw state to let the worker threads continue. 1338 */ 1339 void 1340 virtio_dev_resume(struct rte_eth_dev *dev) 1341 { 1342 struct virtio_hw *hw = dev->data->dev_private; 1343 1344 hw->started = 1; 1345 rte_spinlock_unlock(&hw->state_lock); 1346 } 1347 1348 /* 1349 * Should be called only after device is paused. 1350 */ 1351 int 1352 virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts, 1353 int nb_pkts) 1354 { 1355 struct virtio_hw *hw = dev->data->dev_private; 1356 struct virtnet_tx *txvq = dev->data->tx_queues[0]; 1357 int ret; 1358 1359 hw->inject_pkts = tx_pkts; 1360 ret = dev->tx_pkt_burst(txvq, tx_pkts, nb_pkts); 1361 hw->inject_pkts = NULL; 1362 1363 return ret; 1364 } 1365 1366 static void 1367 virtio_notify_peers(struct rte_eth_dev *dev) 1368 { 1369 struct virtio_hw *hw = dev->data->dev_private; 1370 struct virtnet_rx *rxvq; 1371 struct rte_mbuf *rarp_mbuf; 1372 1373 if (!dev->data->rx_queues) 1374 return; 1375 1376 rxvq = dev->data->rx_queues[0]; 1377 if (!rxvq) 1378 return; 1379 1380 rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool, 1381 (struct ether_addr *)hw->mac_addr); 1382 if (rarp_mbuf == NULL) { 1383 PMD_DRV_LOG(ERR, "failed to make RARP packet."); 1384 return; 1385 } 1386 1387 /* If virtio port just stopped, no need to send RARP */ 1388 if (virtio_dev_pause(dev) < 0) { 1389 rte_pktmbuf_free(rarp_mbuf); 1390 return; 1391 } 1392 1393 virtio_inject_pkts(dev, &rarp_mbuf, 1); 1394 virtio_dev_resume(dev); 1395 } 1396 1397 static void 1398 virtio_ack_link_announce(struct rte_eth_dev *dev) 1399 { 1400 struct virtio_hw *hw = dev->data->dev_private; 1401 struct virtio_pmd_ctrl ctrl; 1402 1403 ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE; 1404 ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK; 1405 1406 virtio_send_command(hw->cvq, &ctrl, NULL, 0); 1407 } 1408 1409 /* 1410 * Process virtio config changed interrupt. Call the callback 1411 * if link state changed, generate gratuitous RARP packet if 1412 * the status indicates an ANNOUNCE. 1413 */ 1414 void 1415 virtio_interrupt_handler(void *param) 1416 { 1417 struct rte_eth_dev *dev = param; 1418 struct virtio_hw *hw = dev->data->dev_private; 1419 uint8_t isr; 1420 uint16_t status; 1421 1422 /* Read interrupt status which clears interrupt */ 1423 isr = vtpci_isr(hw); 1424 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr); 1425 1426 if (virtio_intr_enable(dev) < 0) 1427 PMD_DRV_LOG(ERR, "interrupt enable failed"); 1428 1429 if (isr & VIRTIO_PCI_ISR_CONFIG) { 1430 if (virtio_dev_link_update(dev, 0) == 0) 1431 _rte_eth_dev_callback_process(dev, 1432 RTE_ETH_EVENT_INTR_LSC, 1433 NULL); 1434 1435 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 1436 vtpci_read_dev_config(hw, 1437 offsetof(struct virtio_net_config, status), 1438 &status, sizeof(status)); 1439 if (status & VIRTIO_NET_S_ANNOUNCE) { 1440 virtio_notify_peers(dev); 1441 if (hw->cvq) 1442 virtio_ack_link_announce(dev); 1443 } 1444 } 1445 } 1446 } 1447 1448 /* set rx and tx handlers according to what is supported */ 1449 static void 1450 set_rxtx_funcs(struct rte_eth_dev *eth_dev) 1451 { 1452 struct virtio_hw *hw = eth_dev->data->dev_private; 1453 1454 if (vtpci_packed_queue(hw)) { 1455 PMD_INIT_LOG(INFO, 1456 "virtio: using packed ring %s Tx path on port %u", 1457 hw->use_inorder_tx ? "inorder" : "standard", 1458 eth_dev->data->port_id); 1459 eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed; 1460 } else { 1461 if (hw->use_inorder_tx) { 1462 PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u", 1463 eth_dev->data->port_id); 1464 eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder; 1465 } else { 1466 PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u", 1467 eth_dev->data->port_id); 1468 eth_dev->tx_pkt_burst = virtio_xmit_pkts; 1469 } 1470 } 1471 1472 if (vtpci_packed_queue(hw)) { 1473 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) { 1474 PMD_INIT_LOG(INFO, 1475 "virtio: using packed ring mergeable buffer Rx path on port %u", 1476 eth_dev->data->port_id); 1477 eth_dev->rx_pkt_burst = 1478 &virtio_recv_mergeable_pkts_packed; 1479 } else { 1480 PMD_INIT_LOG(INFO, 1481 "virtio: using packed ring standard Rx path on port %u", 1482 eth_dev->data->port_id); 1483 eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed; 1484 } 1485 } else { 1486 if (hw->use_simple_rx) { 1487 PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u", 1488 eth_dev->data->port_id); 1489 eth_dev->rx_pkt_burst = virtio_recv_pkts_vec; 1490 } else if (hw->use_inorder_rx) { 1491 PMD_INIT_LOG(INFO, 1492 "virtio: using inorder Rx path on port %u", 1493 eth_dev->data->port_id); 1494 eth_dev->rx_pkt_burst = &virtio_recv_pkts_inorder; 1495 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) { 1496 PMD_INIT_LOG(INFO, 1497 "virtio: using mergeable buffer Rx path on port %u", 1498 eth_dev->data->port_id); 1499 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts; 1500 } else { 1501 PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u", 1502 eth_dev->data->port_id); 1503 eth_dev->rx_pkt_burst = &virtio_recv_pkts; 1504 } 1505 } 1506 1507 } 1508 1509 /* Only support 1:1 queue/interrupt mapping so far. 1510 * TODO: support n:1 queue/interrupt mapping when there are limited number of 1511 * interrupt vectors (<N+1). 1512 */ 1513 static int 1514 virtio_queues_bind_intr(struct rte_eth_dev *dev) 1515 { 1516 uint32_t i; 1517 struct virtio_hw *hw = dev->data->dev_private; 1518 1519 PMD_INIT_LOG(INFO, "queue/interrupt binding"); 1520 for (i = 0; i < dev->data->nb_rx_queues; ++i) { 1521 dev->intr_handle->intr_vec[i] = i + 1; 1522 if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) == 1523 VIRTIO_MSI_NO_VECTOR) { 1524 PMD_DRV_LOG(ERR, "failed to set queue vector"); 1525 return -EBUSY; 1526 } 1527 } 1528 1529 return 0; 1530 } 1531 1532 static void 1533 virtio_queues_unbind_intr(struct rte_eth_dev *dev) 1534 { 1535 uint32_t i; 1536 struct virtio_hw *hw = dev->data->dev_private; 1537 1538 PMD_INIT_LOG(INFO, "queue/interrupt unbinding"); 1539 for (i = 0; i < dev->data->nb_rx_queues; ++i) 1540 VTPCI_OPS(hw)->set_queue_irq(hw, 1541 hw->vqs[i * VTNET_CQ], 1542 VIRTIO_MSI_NO_VECTOR); 1543 } 1544 1545 static int 1546 virtio_configure_intr(struct rte_eth_dev *dev) 1547 { 1548 struct virtio_hw *hw = dev->data->dev_private; 1549 1550 if (!rte_intr_cap_multiple(dev->intr_handle)) { 1551 PMD_INIT_LOG(ERR, "Multiple intr vector not supported"); 1552 return -ENOTSUP; 1553 } 1554 1555 if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) { 1556 PMD_INIT_LOG(ERR, "Fail to create eventfd"); 1557 return -1; 1558 } 1559 1560 if (!dev->intr_handle->intr_vec) { 1561 dev->intr_handle->intr_vec = 1562 rte_zmalloc("intr_vec", 1563 hw->max_queue_pairs * sizeof(int), 0); 1564 if (!dev->intr_handle->intr_vec) { 1565 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors", 1566 hw->max_queue_pairs); 1567 return -ENOMEM; 1568 } 1569 } 1570 1571 /* Re-register callback to update max_intr */ 1572 rte_intr_callback_unregister(dev->intr_handle, 1573 virtio_interrupt_handler, 1574 dev); 1575 rte_intr_callback_register(dev->intr_handle, 1576 virtio_interrupt_handler, 1577 dev); 1578 1579 /* DO NOT try to remove this! This function will enable msix, or QEMU 1580 * will encounter SIGSEGV when DRIVER_OK is sent. 1581 * And for legacy devices, this should be done before queue/vec binding 1582 * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR 1583 * (22) will be ignored. 1584 */ 1585 if (virtio_intr_enable(dev) < 0) { 1586 PMD_DRV_LOG(ERR, "interrupt enable failed"); 1587 return -1; 1588 } 1589 1590 if (virtio_queues_bind_intr(dev) < 0) { 1591 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt"); 1592 return -1; 1593 } 1594 1595 return 0; 1596 } 1597 1598 /* reset device and renegotiate features if needed */ 1599 static int 1600 virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features) 1601 { 1602 struct virtio_hw *hw = eth_dev->data->dev_private; 1603 struct virtio_net_config *config; 1604 struct virtio_net_config local_config; 1605 struct rte_pci_device *pci_dev = NULL; 1606 int ret; 1607 1608 /* Reset the device although not necessary at startup */ 1609 vtpci_reset(hw); 1610 1611 if (hw->vqs) { 1612 virtio_dev_free_mbufs(eth_dev); 1613 virtio_free_queues(hw); 1614 } 1615 1616 /* Tell the host we've noticed this device. */ 1617 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK); 1618 1619 /* Tell the host we've known how to drive the device. */ 1620 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER); 1621 if (virtio_negotiate_features(hw, req_features) < 0) 1622 return -1; 1623 1624 hw->weak_barriers = !vtpci_with_feature(hw, VIRTIO_F_ORDER_PLATFORM); 1625 1626 if (!hw->virtio_user_dev) { 1627 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev); 1628 rte_eth_copy_pci_info(eth_dev, pci_dev); 1629 } 1630 1631 /* If host does not support both status and MSI-X then disable LSC */ 1632 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) && 1633 hw->use_msix != VIRTIO_MSIX_NONE) 1634 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC; 1635 else 1636 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC; 1637 1638 /* Setting up rx_header size for the device */ 1639 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) || 1640 vtpci_with_feature(hw, VIRTIO_F_VERSION_1) || 1641 vtpci_with_feature(hw, VIRTIO_F_RING_PACKED)) 1642 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1643 else 1644 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr); 1645 1646 /* Copy the permanent MAC address to: virtio_hw */ 1647 virtio_get_hwaddr(hw); 1648 ether_addr_copy((struct ether_addr *) hw->mac_addr, 1649 ð_dev->data->mac_addrs[0]); 1650 PMD_INIT_LOG(DEBUG, 1651 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 1652 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2], 1653 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]); 1654 1655 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) { 1656 config = &local_config; 1657 1658 vtpci_read_dev_config(hw, 1659 offsetof(struct virtio_net_config, mac), 1660 &config->mac, sizeof(config->mac)); 1661 1662 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 1663 vtpci_read_dev_config(hw, 1664 offsetof(struct virtio_net_config, status), 1665 &config->status, sizeof(config->status)); 1666 } else { 1667 PMD_INIT_LOG(DEBUG, 1668 "VIRTIO_NET_F_STATUS is not supported"); 1669 config->status = 0; 1670 } 1671 1672 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) { 1673 vtpci_read_dev_config(hw, 1674 offsetof(struct virtio_net_config, max_virtqueue_pairs), 1675 &config->max_virtqueue_pairs, 1676 sizeof(config->max_virtqueue_pairs)); 1677 } else { 1678 PMD_INIT_LOG(DEBUG, 1679 "VIRTIO_NET_F_MQ is not supported"); 1680 config->max_virtqueue_pairs = 1; 1681 } 1682 1683 hw->max_queue_pairs = config->max_virtqueue_pairs; 1684 1685 if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) { 1686 vtpci_read_dev_config(hw, 1687 offsetof(struct virtio_net_config, mtu), 1688 &config->mtu, 1689 sizeof(config->mtu)); 1690 1691 /* 1692 * MTU value has already been checked at negotiation 1693 * time, but check again in case it has changed since 1694 * then, which should not happen. 1695 */ 1696 if (config->mtu < ETHER_MIN_MTU) { 1697 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)", 1698 config->mtu); 1699 return -1; 1700 } 1701 1702 hw->max_mtu = config->mtu; 1703 /* Set initial MTU to maximum one supported by vhost */ 1704 eth_dev->data->mtu = config->mtu; 1705 1706 } else { 1707 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN - 1708 VLAN_TAG_LEN - hw->vtnet_hdr_size; 1709 } 1710 1711 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d", 1712 config->max_virtqueue_pairs); 1713 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status); 1714 PMD_INIT_LOG(DEBUG, 1715 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 1716 config->mac[0], config->mac[1], 1717 config->mac[2], config->mac[3], 1718 config->mac[4], config->mac[5]); 1719 } else { 1720 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1"); 1721 hw->max_queue_pairs = 1; 1722 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN - 1723 VLAN_TAG_LEN - hw->vtnet_hdr_size; 1724 } 1725 1726 ret = virtio_alloc_queues(eth_dev); 1727 if (ret < 0) 1728 return ret; 1729 1730 if (eth_dev->data->dev_conf.intr_conf.rxq) { 1731 if (virtio_configure_intr(eth_dev) < 0) { 1732 PMD_INIT_LOG(ERR, "failed to configure interrupt"); 1733 return -1; 1734 } 1735 } 1736 1737 vtpci_reinit_complete(hw); 1738 1739 if (pci_dev) 1740 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x", 1741 eth_dev->data->port_id, pci_dev->id.vendor_id, 1742 pci_dev->id.device_id); 1743 1744 return 0; 1745 } 1746 1747 /* 1748 * Remap the PCI device again (IO port map for legacy device and 1749 * memory map for modern device), so that the secondary process 1750 * could have the PCI initiated correctly. 1751 */ 1752 static int 1753 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw) 1754 { 1755 if (hw->modern) { 1756 /* 1757 * We don't have to re-parse the PCI config space, since 1758 * rte_pci_map_device() makes sure the mapped address 1759 * in secondary process would equal to the one mapped in 1760 * the primary process: error will be returned if that 1761 * requirement is not met. 1762 * 1763 * That said, we could simply reuse all cap pointers 1764 * (such as dev_cfg, common_cfg, etc.) parsed from the 1765 * primary process, which is stored in shared memory. 1766 */ 1767 if (rte_pci_map_device(pci_dev)) { 1768 PMD_INIT_LOG(DEBUG, "failed to map pci device!"); 1769 return -1; 1770 } 1771 } else { 1772 if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0) 1773 return -1; 1774 } 1775 1776 return 0; 1777 } 1778 1779 static void 1780 virtio_set_vtpci_ops(struct virtio_hw *hw) 1781 { 1782 #ifdef RTE_VIRTIO_USER 1783 if (hw->virtio_user_dev) 1784 VTPCI_OPS(hw) = &virtio_user_ops; 1785 else 1786 #endif 1787 if (hw->modern) 1788 VTPCI_OPS(hw) = &modern_ops; 1789 else 1790 VTPCI_OPS(hw) = &legacy_ops; 1791 } 1792 1793 /* 1794 * This function is based on probe() function in virtio_pci.c 1795 * It returns 0 on success. 1796 */ 1797 int 1798 eth_virtio_dev_init(struct rte_eth_dev *eth_dev) 1799 { 1800 struct virtio_hw *hw = eth_dev->data->dev_private; 1801 int ret; 1802 1803 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf)); 1804 1805 eth_dev->dev_ops = &virtio_eth_dev_ops; 1806 1807 if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 1808 if (!hw->virtio_user_dev) { 1809 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw); 1810 if (ret) 1811 return ret; 1812 } 1813 1814 virtio_set_vtpci_ops(hw); 1815 set_rxtx_funcs(eth_dev); 1816 1817 return 0; 1818 } 1819 1820 /* Allocate memory for storing MAC addresses */ 1821 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0); 1822 if (eth_dev->data->mac_addrs == NULL) { 1823 PMD_INIT_LOG(ERR, 1824 "Failed to allocate %d bytes needed to store MAC addresses", 1825 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN); 1826 return -ENOMEM; 1827 } 1828 1829 hw->port_id = eth_dev->data->port_id; 1830 /* For virtio_user case the hw->virtio_user_dev is populated by 1831 * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called. 1832 */ 1833 if (!hw->virtio_user_dev) { 1834 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw); 1835 if (ret) 1836 goto out; 1837 } 1838 1839 /* reset device and negotiate default features */ 1840 ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES); 1841 if (ret < 0) 1842 goto out; 1843 1844 return 0; 1845 1846 out: 1847 rte_free(eth_dev->data->mac_addrs); 1848 return ret; 1849 } 1850 1851 static int 1852 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev) 1853 { 1854 PMD_INIT_FUNC_TRACE(); 1855 1856 if (rte_eal_process_type() == RTE_PROC_SECONDARY) 1857 return 0; 1858 1859 virtio_dev_stop(eth_dev); 1860 virtio_dev_close(eth_dev); 1861 1862 eth_dev->dev_ops = NULL; 1863 eth_dev->tx_pkt_burst = NULL; 1864 eth_dev->rx_pkt_burst = NULL; 1865 1866 if (eth_dev->device) 1867 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev)); 1868 1869 PMD_INIT_LOG(DEBUG, "dev_uninit completed"); 1870 1871 return 0; 1872 } 1873 1874 static int vdpa_check_handler(__rte_unused const char *key, 1875 const char *value, __rte_unused void *opaque) 1876 { 1877 if (strcmp(value, "1")) 1878 return -1; 1879 1880 return 0; 1881 } 1882 1883 static int 1884 vdpa_mode_selected(struct rte_devargs *devargs) 1885 { 1886 struct rte_kvargs *kvlist; 1887 const char *key = "vdpa"; 1888 int ret = 0; 1889 1890 if (devargs == NULL) 1891 return 0; 1892 1893 kvlist = rte_kvargs_parse(devargs->args, NULL); 1894 if (kvlist == NULL) 1895 return 0; 1896 1897 if (!rte_kvargs_count(kvlist, key)) 1898 goto exit; 1899 1900 /* vdpa mode selected when there's a key-value pair: vdpa=1 */ 1901 if (rte_kvargs_process(kvlist, key, 1902 vdpa_check_handler, NULL) < 0) { 1903 goto exit; 1904 } 1905 ret = 1; 1906 1907 exit: 1908 rte_kvargs_free(kvlist); 1909 return ret; 1910 } 1911 1912 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 1913 struct rte_pci_device *pci_dev) 1914 { 1915 if (rte_eal_iopl_init() != 0) { 1916 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD"); 1917 return 1; 1918 } 1919 1920 /* virtio pmd skips probe if device needs to work in vdpa mode */ 1921 if (vdpa_mode_selected(pci_dev->device.devargs)) 1922 return 1; 1923 1924 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw), 1925 eth_virtio_dev_init); 1926 } 1927 1928 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev) 1929 { 1930 return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit); 1931 } 1932 1933 static struct rte_pci_driver rte_virtio_pmd = { 1934 .driver = { 1935 .name = "net_virtio", 1936 }, 1937 .id_table = pci_id_virtio_map, 1938 .drv_flags = 0, 1939 .probe = eth_virtio_pci_probe, 1940 .remove = eth_virtio_pci_remove, 1941 }; 1942 1943 RTE_INIT(rte_virtio_pmd_init) 1944 { 1945 rte_eal_iopl_init(); 1946 rte_pci_register(&rte_virtio_pmd); 1947 } 1948 1949 static bool 1950 rx_offload_enabled(struct virtio_hw *hw) 1951 { 1952 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) || 1953 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) || 1954 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6); 1955 } 1956 1957 static bool 1958 tx_offload_enabled(struct virtio_hw *hw) 1959 { 1960 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) || 1961 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) || 1962 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6); 1963 } 1964 1965 /* 1966 * Configure virtio device 1967 * It returns 0 on success. 1968 */ 1969 static int 1970 virtio_dev_configure(struct rte_eth_dev *dev) 1971 { 1972 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode; 1973 const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode; 1974 struct virtio_hw *hw = dev->data->dev_private; 1975 uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN + 1976 hw->vtnet_hdr_size; 1977 uint64_t rx_offloads = rxmode->offloads; 1978 uint64_t tx_offloads = txmode->offloads; 1979 uint64_t req_features; 1980 int ret; 1981 1982 PMD_INIT_LOG(DEBUG, "configure"); 1983 req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES; 1984 1985 if (dev->data->dev_conf.intr_conf.rxq) { 1986 ret = virtio_init_device(dev, hw->req_guest_features); 1987 if (ret < 0) 1988 return ret; 1989 } 1990 1991 if (rxmode->max_rx_pkt_len > hw->max_mtu + ether_hdr_len) 1992 req_features &= ~(1ULL << VIRTIO_NET_F_MTU); 1993 1994 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM | 1995 DEV_RX_OFFLOAD_TCP_CKSUM)) 1996 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM); 1997 1998 if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) 1999 req_features |= 2000 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 2001 (1ULL << VIRTIO_NET_F_GUEST_TSO6); 2002 2003 if (tx_offloads & (DEV_TX_OFFLOAD_UDP_CKSUM | 2004 DEV_TX_OFFLOAD_TCP_CKSUM)) 2005 req_features |= (1ULL << VIRTIO_NET_F_CSUM); 2006 2007 if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO) 2008 req_features |= 2009 (1ULL << VIRTIO_NET_F_HOST_TSO4) | 2010 (1ULL << VIRTIO_NET_F_HOST_TSO6); 2011 2012 /* if request features changed, reinit the device */ 2013 if (req_features != hw->req_guest_features) { 2014 ret = virtio_init_device(dev, req_features); 2015 if (ret < 0) 2016 return ret; 2017 } 2018 2019 if ((rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM | 2020 DEV_RX_OFFLOAD_TCP_CKSUM)) && 2021 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) { 2022 PMD_DRV_LOG(ERR, 2023 "rx checksum not available on this host"); 2024 return -ENOTSUP; 2025 } 2026 2027 if ((rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) && 2028 (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) || 2029 !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) { 2030 PMD_DRV_LOG(ERR, 2031 "Large Receive Offload not available on this host"); 2032 return -ENOTSUP; 2033 } 2034 2035 /* start control queue */ 2036 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) 2037 virtio_dev_cq_start(dev); 2038 2039 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP) 2040 hw->vlan_strip = 1; 2041 2042 if ((rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER) 2043 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) { 2044 PMD_DRV_LOG(ERR, 2045 "vlan filtering not available on this host"); 2046 return -ENOTSUP; 2047 } 2048 2049 hw->has_tx_offload = tx_offload_enabled(hw); 2050 hw->has_rx_offload = rx_offload_enabled(hw); 2051 2052 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 2053 /* Enable vector (0) for Link State Intrerrupt */ 2054 if (VTPCI_OPS(hw)->set_config_irq(hw, 0) == 2055 VIRTIO_MSI_NO_VECTOR) { 2056 PMD_DRV_LOG(ERR, "failed to set config vector"); 2057 return -EBUSY; 2058 } 2059 2060 rte_spinlock_init(&hw->state_lock); 2061 2062 hw->use_simple_rx = 1; 2063 2064 if (vtpci_with_feature(hw, VIRTIO_F_IN_ORDER)) { 2065 hw->use_inorder_tx = 1; 2066 hw->use_inorder_rx = 1; 2067 hw->use_simple_rx = 0; 2068 } 2069 2070 if (vtpci_packed_queue(hw)) { 2071 hw->use_simple_rx = 0; 2072 hw->use_inorder_rx = 0; 2073 } 2074 2075 #if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM 2076 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) { 2077 hw->use_simple_rx = 0; 2078 } 2079 #endif 2080 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) { 2081 hw->use_simple_rx = 0; 2082 } 2083 2084 if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM | 2085 DEV_RX_OFFLOAD_TCP_CKSUM | 2086 DEV_RX_OFFLOAD_TCP_LRO | 2087 DEV_RX_OFFLOAD_VLAN_STRIP)) 2088 hw->use_simple_rx = 0; 2089 2090 hw->opened = true; 2091 2092 return 0; 2093 } 2094 2095 2096 static int 2097 virtio_dev_start(struct rte_eth_dev *dev) 2098 { 2099 uint16_t nb_queues, i; 2100 struct virtnet_rx *rxvq; 2101 struct virtnet_tx *txvq __rte_unused; 2102 struct virtio_hw *hw = dev->data->dev_private; 2103 int ret; 2104 2105 /* Finish the initialization of the queues */ 2106 for (i = 0; i < dev->data->nb_rx_queues; i++) { 2107 ret = virtio_dev_rx_queue_setup_finish(dev, i); 2108 if (ret < 0) 2109 return ret; 2110 } 2111 for (i = 0; i < dev->data->nb_tx_queues; i++) { 2112 ret = virtio_dev_tx_queue_setup_finish(dev, i); 2113 if (ret < 0) 2114 return ret; 2115 } 2116 2117 /* check if lsc interrupt feature is enabled */ 2118 if (dev->data->dev_conf.intr_conf.lsc) { 2119 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) { 2120 PMD_DRV_LOG(ERR, "link status not supported by host"); 2121 return -ENOTSUP; 2122 } 2123 } 2124 2125 /* Enable uio/vfio intr/eventfd mapping: althrough we already did that 2126 * in device configure, but it could be unmapped when device is 2127 * stopped. 2128 */ 2129 if (dev->data->dev_conf.intr_conf.lsc || 2130 dev->data->dev_conf.intr_conf.rxq) { 2131 virtio_intr_disable(dev); 2132 2133 /* Setup interrupt callback */ 2134 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 2135 rte_intr_callback_register(dev->intr_handle, 2136 virtio_interrupt_handler, 2137 dev); 2138 2139 if (virtio_intr_enable(dev) < 0) { 2140 PMD_DRV_LOG(ERR, "interrupt enable failed"); 2141 return -EIO; 2142 } 2143 } 2144 2145 /*Notify the backend 2146 *Otherwise the tap backend might already stop its queue due to fullness. 2147 *vhost backend will have no chance to be waked up 2148 */ 2149 nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues); 2150 if (hw->max_queue_pairs > 1) { 2151 if (virtio_set_multiple_queues(dev, nb_queues) != 0) 2152 return -EINVAL; 2153 } 2154 2155 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues); 2156 2157 for (i = 0; i < dev->data->nb_rx_queues; i++) { 2158 rxvq = dev->data->rx_queues[i]; 2159 /* Flush the old packets */ 2160 virtqueue_rxvq_flush(rxvq->vq); 2161 virtqueue_notify(rxvq->vq); 2162 } 2163 2164 for (i = 0; i < dev->data->nb_tx_queues; i++) { 2165 txvq = dev->data->tx_queues[i]; 2166 virtqueue_notify(txvq->vq); 2167 } 2168 2169 PMD_INIT_LOG(DEBUG, "Notified backend at initialization"); 2170 2171 for (i = 0; i < dev->data->nb_rx_queues; i++) { 2172 rxvq = dev->data->rx_queues[i]; 2173 VIRTQUEUE_DUMP(rxvq->vq); 2174 } 2175 2176 for (i = 0; i < dev->data->nb_tx_queues; i++) { 2177 txvq = dev->data->tx_queues[i]; 2178 VIRTQUEUE_DUMP(txvq->vq); 2179 } 2180 2181 set_rxtx_funcs(dev); 2182 hw->started = true; 2183 2184 /* Initialize Link state */ 2185 virtio_dev_link_update(dev, 0); 2186 2187 return 0; 2188 } 2189 2190 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev) 2191 { 2192 struct virtio_hw *hw = dev->data->dev_private; 2193 uint16_t nr_vq = virtio_get_nr_vq(hw); 2194 const char *type __rte_unused; 2195 unsigned int i, mbuf_num = 0; 2196 struct virtqueue *vq; 2197 struct rte_mbuf *buf; 2198 int queue_type; 2199 2200 if (hw->vqs == NULL) 2201 return; 2202 2203 for (i = 0; i < nr_vq; i++) { 2204 vq = hw->vqs[i]; 2205 if (!vq) 2206 continue; 2207 2208 queue_type = virtio_get_queue_type(hw, i); 2209 if (queue_type == VTNET_RQ) 2210 type = "rxq"; 2211 else if (queue_type == VTNET_TQ) 2212 type = "txq"; 2213 else 2214 continue; 2215 2216 PMD_INIT_LOG(DEBUG, 2217 "Before freeing %s[%d] used and unused buf", 2218 type, i); 2219 VIRTQUEUE_DUMP(vq); 2220 2221 while ((buf = virtqueue_detach_unused(vq)) != NULL) { 2222 rte_pktmbuf_free(buf); 2223 mbuf_num++; 2224 } 2225 2226 PMD_INIT_LOG(DEBUG, 2227 "After freeing %s[%d] used and unused buf", 2228 type, i); 2229 VIRTQUEUE_DUMP(vq); 2230 } 2231 2232 PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num); 2233 } 2234 2235 /* 2236 * Stop device: disable interrupt and mark link down 2237 */ 2238 static void 2239 virtio_dev_stop(struct rte_eth_dev *dev) 2240 { 2241 struct virtio_hw *hw = dev->data->dev_private; 2242 struct rte_eth_link link; 2243 struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf; 2244 2245 PMD_INIT_LOG(DEBUG, "stop"); 2246 2247 rte_spinlock_lock(&hw->state_lock); 2248 if (!hw->started) 2249 goto out_unlock; 2250 hw->started = false; 2251 2252 if (intr_conf->lsc || intr_conf->rxq) { 2253 virtio_intr_disable(dev); 2254 2255 /* Reset interrupt callback */ 2256 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) { 2257 rte_intr_callback_unregister(dev->intr_handle, 2258 virtio_interrupt_handler, 2259 dev); 2260 } 2261 } 2262 2263 memset(&link, 0, sizeof(link)); 2264 rte_eth_linkstatus_set(dev, &link); 2265 out_unlock: 2266 rte_spinlock_unlock(&hw->state_lock); 2267 } 2268 2269 static int 2270 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete) 2271 { 2272 struct rte_eth_link link; 2273 uint16_t status; 2274 struct virtio_hw *hw = dev->data->dev_private; 2275 2276 memset(&link, 0, sizeof(link)); 2277 link.link_duplex = ETH_LINK_FULL_DUPLEX; 2278 link.link_speed = ETH_SPEED_NUM_10G; 2279 link.link_autoneg = ETH_LINK_FIXED; 2280 2281 if (!hw->started) { 2282 link.link_status = ETH_LINK_DOWN; 2283 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 2284 PMD_INIT_LOG(DEBUG, "Get link status from hw"); 2285 vtpci_read_dev_config(hw, 2286 offsetof(struct virtio_net_config, status), 2287 &status, sizeof(status)); 2288 if ((status & VIRTIO_NET_S_LINK_UP) == 0) { 2289 link.link_status = ETH_LINK_DOWN; 2290 PMD_INIT_LOG(DEBUG, "Port %d is down", 2291 dev->data->port_id); 2292 } else { 2293 link.link_status = ETH_LINK_UP; 2294 PMD_INIT_LOG(DEBUG, "Port %d is up", 2295 dev->data->port_id); 2296 } 2297 } else { 2298 link.link_status = ETH_LINK_UP; 2299 } 2300 2301 return rte_eth_linkstatus_set(dev, &link); 2302 } 2303 2304 static int 2305 virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask) 2306 { 2307 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode; 2308 struct virtio_hw *hw = dev->data->dev_private; 2309 uint64_t offloads = rxmode->offloads; 2310 2311 if (mask & ETH_VLAN_FILTER_MASK) { 2312 if ((offloads & DEV_RX_OFFLOAD_VLAN_FILTER) && 2313 !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) { 2314 2315 PMD_DRV_LOG(NOTICE, 2316 "vlan filtering not available on this host"); 2317 2318 return -ENOTSUP; 2319 } 2320 } 2321 2322 if (mask & ETH_VLAN_STRIP_MASK) 2323 hw->vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP); 2324 2325 return 0; 2326 } 2327 2328 static void 2329 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 2330 { 2331 uint64_t tso_mask, host_features; 2332 struct virtio_hw *hw = dev->data->dev_private; 2333 2334 dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */ 2335 2336 dev_info->max_rx_queues = 2337 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES); 2338 dev_info->max_tx_queues = 2339 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES); 2340 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE; 2341 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN; 2342 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS; 2343 2344 host_features = VTPCI_OPS(hw)->get_features(hw); 2345 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP; 2346 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME; 2347 if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) { 2348 dev_info->rx_offload_capa |= 2349 DEV_RX_OFFLOAD_TCP_CKSUM | 2350 DEV_RX_OFFLOAD_UDP_CKSUM; 2351 } 2352 if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN)) 2353 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_VLAN_FILTER; 2354 tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 2355 (1ULL << VIRTIO_NET_F_GUEST_TSO6); 2356 if ((host_features & tso_mask) == tso_mask) 2357 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO; 2358 2359 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS | 2360 DEV_TX_OFFLOAD_VLAN_INSERT; 2361 if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) { 2362 dev_info->tx_offload_capa |= 2363 DEV_TX_OFFLOAD_UDP_CKSUM | 2364 DEV_TX_OFFLOAD_TCP_CKSUM; 2365 } 2366 tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) | 2367 (1ULL << VIRTIO_NET_F_HOST_TSO6); 2368 if ((host_features & tso_mask) == tso_mask) 2369 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO; 2370 } 2371 2372 /* 2373 * It enables testpmd to collect per queue stats. 2374 */ 2375 static int 2376 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev, 2377 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx, 2378 __rte_unused uint8_t is_rx) 2379 { 2380 return 0; 2381 } 2382 2383 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__); 2384 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map); 2385 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci"); 2386 2387 RTE_INIT(virtio_init_log) 2388 { 2389 virtio_logtype_init = rte_log_register("pmd.net.virtio.init"); 2390 if (virtio_logtype_init >= 0) 2391 rte_log_set_level(virtio_logtype_init, RTE_LOG_NOTICE); 2392 virtio_logtype_driver = rte_log_register("pmd.net.virtio.driver"); 2393 if (virtio_logtype_driver >= 0) 2394 rte_log_set_level(virtio_logtype_driver, RTE_LOG_NOTICE); 2395 } 2396