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