1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright(c) 2010-2014 Intel Corporation. 4 */ 5 6 /* 7 * This code is inspired from the book "Linux Device Drivers" by 8 * Alessandro Rubini and Jonathan Corbet, published by O'Reilly & Associates 9 */ 10 11 #include <linux/device.h> 12 #include <linux/module.h> 13 #include <linux/version.h> 14 #include <linux/netdevice.h> 15 #include <linux/etherdevice.h> /* eth_type_trans */ 16 #include <linux/ethtool.h> 17 #include <linux/skbuff.h> 18 #include <linux/kthread.h> 19 #include <linux/delay.h> 20 #include <linux/rtnetlink.h> 21 22 #include <rte_kni_common.h> 23 #include <kni_fifo.h> 24 25 #include "compat.h" 26 #include "kni_dev.h" 27 28 #define WD_TIMEOUT 5 /*jiffies */ 29 30 #define KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */ 31 32 /* typedef for rx function */ 33 typedef void (*kni_net_rx_t)(struct kni_dev *kni); 34 35 static void kni_net_rx_normal(struct kni_dev *kni); 36 37 /* kni rx function pointer, with default to normal rx */ 38 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal; 39 40 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT 41 /* iova to kernel virtual address */ 42 static inline void * 43 iova2kva(struct kni_dev *kni, void *iova) 44 { 45 return phys_to_virt(iova_to_phys(kni->usr_tsk, (unsigned long)iova)); 46 } 47 48 static inline void * 49 iova2data_kva(struct kni_dev *kni, struct rte_kni_mbuf *m) 50 { 51 return phys_to_virt(iova_to_phys(kni->usr_tsk, m->buf_iova) + 52 m->data_off); 53 } 54 #endif 55 56 /* physical address to kernel virtual address */ 57 static void * 58 pa2kva(void *pa) 59 { 60 return phys_to_virt((unsigned long)pa); 61 } 62 63 /* physical address to virtual address */ 64 static void * 65 pa2va(void *pa, struct rte_kni_mbuf *m) 66 { 67 void *va; 68 69 va = (void *)((unsigned long)pa + 70 (unsigned long)m->buf_addr - 71 (unsigned long)m->buf_iova); 72 return va; 73 } 74 75 /* mbuf data kernel virtual address from mbuf kernel virtual address */ 76 static void * 77 kva2data_kva(struct rte_kni_mbuf *m) 78 { 79 return phys_to_virt(m->buf_iova + m->data_off); 80 } 81 82 static inline void * 83 get_kva(struct kni_dev *kni, void *pa) 84 { 85 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT 86 if (kni->iova_mode == 1) 87 return iova2kva(kni, pa); 88 #endif 89 return pa2kva(pa); 90 } 91 92 static inline void * 93 get_data_kva(struct kni_dev *kni, void *pkt_kva) 94 { 95 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT 96 if (kni->iova_mode == 1) 97 return iova2data_kva(kni, pkt_kva); 98 #endif 99 return kva2data_kva(pkt_kva); 100 } 101 102 /* 103 * It can be called to process the request. 104 */ 105 static int 106 kni_net_process_request(struct net_device *dev, struct rte_kni_request *req) 107 { 108 struct kni_dev *kni = netdev_priv(dev); 109 int ret = -1; 110 void *resp_va; 111 uint32_t num; 112 int ret_val; 113 114 ASSERT_RTNL(); 115 116 if (bifurcated_support) { 117 /* If we need to wait and RTNL mutex is held 118 * drop the mutex and hold reference to keep device 119 */ 120 if (req->async == 0) { 121 dev_hold(dev); 122 rtnl_unlock(); 123 } 124 } 125 126 mutex_lock(&kni->sync_lock); 127 128 /* Construct data */ 129 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request)); 130 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1); 131 if (num < 1) { 132 pr_err("Cannot send to req_q\n"); 133 ret = -EBUSY; 134 goto fail; 135 } 136 137 if (bifurcated_support) { 138 /* No result available since request is handled 139 * asynchronously. set response to success. 140 */ 141 if (req->async != 0) { 142 req->result = 0; 143 goto async; 144 } 145 } 146 147 ret_val = wait_event_interruptible_timeout(kni->wq, 148 kni_fifo_count(kni->resp_q), 3 * HZ); 149 if (signal_pending(current) || ret_val <= 0) { 150 ret = -ETIME; 151 goto fail; 152 } 153 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1); 154 if (num != 1 || resp_va != kni->sync_va) { 155 /* This should never happen */ 156 pr_err("No data in resp_q\n"); 157 ret = -ENODATA; 158 goto fail; 159 } 160 161 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request)); 162 async: 163 ret = 0; 164 165 fail: 166 mutex_unlock(&kni->sync_lock); 167 if (bifurcated_support) { 168 if (req->async == 0) { 169 rtnl_lock(); 170 dev_put(dev); 171 } 172 } 173 return ret; 174 } 175 176 /* 177 * Open and close 178 */ 179 static int 180 kni_net_open(struct net_device *dev) 181 { 182 int ret; 183 struct rte_kni_request req; 184 185 netif_start_queue(dev); 186 if (kni_dflt_carrier == 1) 187 netif_carrier_on(dev); 188 else 189 netif_carrier_off(dev); 190 191 memset(&req, 0, sizeof(req)); 192 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF; 193 194 /* Setting if_up to non-zero means up */ 195 req.if_up = 1; 196 ret = kni_net_process_request(dev, &req); 197 198 return (ret == 0) ? req.result : ret; 199 } 200 201 static int 202 kni_net_release(struct net_device *dev) 203 { 204 int ret; 205 struct rte_kni_request req; 206 207 netif_stop_queue(dev); /* can't transmit any more */ 208 netif_carrier_off(dev); 209 210 memset(&req, 0, sizeof(req)); 211 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF; 212 213 /* Setting if_up to 0 means down */ 214 req.if_up = 0; 215 216 if (bifurcated_support) { 217 /* request async because of the deadlock problem */ 218 req.async = 1; 219 } 220 221 ret = kni_net_process_request(dev, &req); 222 223 return (ret == 0) ? req.result : ret; 224 } 225 226 static void 227 kni_fifo_trans_pa2va(struct kni_dev *kni, 228 struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va) 229 { 230 uint32_t ret, i, num_dst, num_rx; 231 struct rte_kni_mbuf *kva, *prev_kva; 232 int nb_segs; 233 int kva_nb_segs; 234 235 do { 236 num_dst = kni_fifo_free_count(dst_va); 237 if (num_dst == 0) 238 return; 239 240 num_rx = min_t(uint32_t, num_dst, MBUF_BURST_SZ); 241 242 num_rx = kni_fifo_get(src_pa, kni->pa, num_rx); 243 if (num_rx == 0) 244 return; 245 246 for (i = 0; i < num_rx; i++) { 247 kva = get_kva(kni, kni->pa[i]); 248 kni->va[i] = pa2va(kni->pa[i], kva); 249 250 kva_nb_segs = kva->nb_segs; 251 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) { 252 if (!kva->next) 253 break; 254 255 prev_kva = kva; 256 kva = get_kva(kni, kva->next); 257 /* Convert physical address to virtual address */ 258 prev_kva->next = pa2va(prev_kva->next, kva); 259 } 260 } 261 262 ret = kni_fifo_put(dst_va, kni->va, num_rx); 263 if (ret != num_rx) { 264 /* Failing should not happen */ 265 pr_err("Fail to enqueue entries into dst_va\n"); 266 return; 267 } 268 } while (1); 269 } 270 271 /* Try to release mbufs when kni release */ 272 void kni_net_release_fifo_phy(struct kni_dev *kni) 273 { 274 /* release rx_q first, because it can't release in userspace */ 275 kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q); 276 /* release alloc_q for speeding up kni release in userspace */ 277 kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q); 278 } 279 280 /* 281 * Configuration changes (passed on by ifconfig) 282 */ 283 static int 284 kni_net_config(struct net_device *dev, struct ifmap *map) 285 { 286 if (dev->flags & IFF_UP) /* can't act on a running interface */ 287 return -EBUSY; 288 289 /* ignore other fields */ 290 return 0; 291 } 292 293 /* 294 * Transmit a packet (called by the kernel) 295 */ 296 static int 297 kni_net_tx(struct sk_buff *skb, struct net_device *dev) 298 { 299 int len = 0; 300 uint32_t ret; 301 struct kni_dev *kni = netdev_priv(dev); 302 struct rte_kni_mbuf *pkt_kva = NULL; 303 void *pkt_pa = NULL; 304 void *pkt_va = NULL; 305 306 /* save the timestamp */ 307 #ifdef HAVE_TRANS_START_HELPER 308 netif_trans_update(dev); 309 #else 310 dev->trans_start = jiffies; 311 #endif 312 313 /* Check if the length of skb is less than mbuf size */ 314 if (skb->len > kni->mbuf_size) 315 goto drop; 316 317 /** 318 * Check if it has at least one free entry in tx_q and 319 * one entry in alloc_q. 320 */ 321 if (kni_fifo_free_count(kni->tx_q) == 0 || 322 kni_fifo_count(kni->alloc_q) == 0) { 323 /** 324 * If no free entry in tx_q or no entry in alloc_q, 325 * drops skb and goes out. 326 */ 327 goto drop; 328 } 329 330 /* dequeue a mbuf from alloc_q */ 331 ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1); 332 if (likely(ret == 1)) { 333 void *data_kva; 334 335 pkt_kva = get_kva(kni, pkt_pa); 336 data_kva = get_data_kva(kni, pkt_kva); 337 pkt_va = pa2va(pkt_pa, pkt_kva); 338 339 len = skb->len; 340 memcpy(data_kva, skb->data, len); 341 if (unlikely(len < ETH_ZLEN)) { 342 memset(data_kva + len, 0, ETH_ZLEN - len); 343 len = ETH_ZLEN; 344 } 345 pkt_kva->pkt_len = len; 346 pkt_kva->data_len = len; 347 348 /* enqueue mbuf into tx_q */ 349 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1); 350 if (unlikely(ret != 1)) { 351 /* Failing should not happen */ 352 pr_err("Fail to enqueue mbuf into tx_q\n"); 353 goto drop; 354 } 355 } else { 356 /* Failing should not happen */ 357 pr_err("Fail to dequeue mbuf from alloc_q\n"); 358 goto drop; 359 } 360 361 /* Free skb and update statistics */ 362 dev_kfree_skb(skb); 363 dev->stats.tx_bytes += len; 364 dev->stats.tx_packets++; 365 366 return NETDEV_TX_OK; 367 368 drop: 369 /* Free skb and update statistics */ 370 dev_kfree_skb(skb); 371 dev->stats.tx_dropped++; 372 373 return NETDEV_TX_OK; 374 } 375 376 /* 377 * RX: normal working mode 378 */ 379 static void 380 kni_net_rx_normal(struct kni_dev *kni) 381 { 382 uint32_t ret; 383 uint32_t len; 384 uint32_t i, num_rx, num_fq; 385 struct rte_kni_mbuf *kva, *prev_kva; 386 void *data_kva; 387 struct sk_buff *skb; 388 struct net_device *dev = kni->net_dev; 389 390 /* Get the number of free entries in free_q */ 391 num_fq = kni_fifo_free_count(kni->free_q); 392 if (num_fq == 0) { 393 /* No room on the free_q, bail out */ 394 return; 395 } 396 397 /* Calculate the number of entries to dequeue from rx_q */ 398 num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ); 399 400 /* Burst dequeue from rx_q */ 401 num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx); 402 if (num_rx == 0) 403 return; 404 405 /* Transfer received packets to netif */ 406 for (i = 0; i < num_rx; i++) { 407 kva = get_kva(kni, kni->pa[i]); 408 len = kva->pkt_len; 409 data_kva = get_data_kva(kni, kva); 410 kni->va[i] = pa2va(kni->pa[i], kva); 411 412 skb = netdev_alloc_skb(dev, len); 413 if (!skb) { 414 /* Update statistics */ 415 dev->stats.rx_dropped++; 416 continue; 417 } 418 419 if (kva->nb_segs == 1) { 420 memcpy(skb_put(skb, len), data_kva, len); 421 } else { 422 int nb_segs; 423 int kva_nb_segs = kva->nb_segs; 424 425 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) { 426 memcpy(skb_put(skb, kva->data_len), 427 data_kva, kva->data_len); 428 429 if (!kva->next) 430 break; 431 432 prev_kva = kva; 433 kva = get_kva(kni, kva->next); 434 data_kva = kva2data_kva(kva); 435 /* Convert physical address to virtual address */ 436 prev_kva->next = pa2va(prev_kva->next, kva); 437 } 438 } 439 440 skb->protocol = eth_type_trans(skb, dev); 441 skb->ip_summed = CHECKSUM_UNNECESSARY; 442 443 /* Call netif interface */ 444 netif_rx_ni(skb); 445 446 /* Update statistics */ 447 dev->stats.rx_bytes += len; 448 dev->stats.rx_packets++; 449 } 450 451 /* Burst enqueue mbufs into free_q */ 452 ret = kni_fifo_put(kni->free_q, kni->va, num_rx); 453 if (ret != num_rx) 454 /* Failing should not happen */ 455 pr_err("Fail to enqueue entries into free_q\n"); 456 } 457 458 /* 459 * RX: loopback with enqueue/dequeue fifos. 460 */ 461 static void 462 kni_net_rx_lo_fifo(struct kni_dev *kni) 463 { 464 uint32_t ret; 465 uint32_t len; 466 uint32_t i, num, num_rq, num_tq, num_aq, num_fq; 467 struct rte_kni_mbuf *kva, *next_kva; 468 void *data_kva; 469 struct rte_kni_mbuf *alloc_kva; 470 void *alloc_data_kva; 471 struct net_device *dev = kni->net_dev; 472 473 /* Get the number of entries in rx_q */ 474 num_rq = kni_fifo_count(kni->rx_q); 475 476 /* Get the number of free entries in tx_q */ 477 num_tq = kni_fifo_free_count(kni->tx_q); 478 479 /* Get the number of entries in alloc_q */ 480 num_aq = kni_fifo_count(kni->alloc_q); 481 482 /* Get the number of free entries in free_q */ 483 num_fq = kni_fifo_free_count(kni->free_q); 484 485 /* Calculate the number of entries to be dequeued from rx_q */ 486 num = min(num_rq, num_tq); 487 num = min(num, num_aq); 488 num = min(num, num_fq); 489 num = min_t(uint32_t, num, MBUF_BURST_SZ); 490 491 /* Return if no entry to dequeue from rx_q */ 492 if (num == 0) 493 return; 494 495 /* Burst dequeue from rx_q */ 496 ret = kni_fifo_get(kni->rx_q, kni->pa, num); 497 if (ret == 0) 498 return; /* Failing should not happen */ 499 500 /* Dequeue entries from alloc_q */ 501 ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num); 502 if (ret) { 503 num = ret; 504 /* Copy mbufs */ 505 for (i = 0; i < num; i++) { 506 kva = get_kva(kni, kni->pa[i]); 507 len = kva->data_len; 508 data_kva = get_data_kva(kni, kva); 509 kni->va[i] = pa2va(kni->pa[i], kva); 510 511 while (kva->next) { 512 next_kva = get_kva(kni, kva->next); 513 /* Convert physical address to virtual address */ 514 kva->next = pa2va(kva->next, next_kva); 515 kva = next_kva; 516 } 517 518 alloc_kva = get_kva(kni, kni->alloc_pa[i]); 519 alloc_data_kva = get_data_kva(kni, alloc_kva); 520 kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva); 521 522 memcpy(alloc_data_kva, data_kva, len); 523 alloc_kva->pkt_len = len; 524 alloc_kva->data_len = len; 525 526 dev->stats.tx_bytes += len; 527 dev->stats.rx_bytes += len; 528 } 529 530 /* Burst enqueue mbufs into tx_q */ 531 ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num); 532 if (ret != num) 533 /* Failing should not happen */ 534 pr_err("Fail to enqueue mbufs into tx_q\n"); 535 } 536 537 /* Burst enqueue mbufs into free_q */ 538 ret = kni_fifo_put(kni->free_q, kni->va, num); 539 if (ret != num) 540 /* Failing should not happen */ 541 pr_err("Fail to enqueue mbufs into free_q\n"); 542 543 /** 544 * Update statistic, and enqueue/dequeue failure is impossible, 545 * as all queues are checked at first. 546 */ 547 dev->stats.tx_packets += num; 548 dev->stats.rx_packets += num; 549 } 550 551 /* 552 * RX: loopback with enqueue/dequeue fifos and sk buffer copies. 553 */ 554 static void 555 kni_net_rx_lo_fifo_skb(struct kni_dev *kni) 556 { 557 uint32_t ret; 558 uint32_t len; 559 uint32_t i, num_rq, num_fq, num; 560 struct rte_kni_mbuf *kva, *prev_kva; 561 void *data_kva; 562 struct sk_buff *skb; 563 struct net_device *dev = kni->net_dev; 564 565 /* Get the number of entries in rx_q */ 566 num_rq = kni_fifo_count(kni->rx_q); 567 568 /* Get the number of free entries in free_q */ 569 num_fq = kni_fifo_free_count(kni->free_q); 570 571 /* Calculate the number of entries to dequeue from rx_q */ 572 num = min(num_rq, num_fq); 573 num = min_t(uint32_t, num, MBUF_BURST_SZ); 574 575 /* Return if no entry to dequeue from rx_q */ 576 if (num == 0) 577 return; 578 579 /* Burst dequeue mbufs from rx_q */ 580 ret = kni_fifo_get(kni->rx_q, kni->pa, num); 581 if (ret == 0) 582 return; 583 584 /* Copy mbufs to sk buffer and then call tx interface */ 585 for (i = 0; i < num; i++) { 586 kva = get_kva(kni, kni->pa[i]); 587 len = kva->pkt_len; 588 data_kva = get_data_kva(kni, kva); 589 kni->va[i] = pa2va(kni->pa[i], kva); 590 591 skb = netdev_alloc_skb(dev, len); 592 if (skb) { 593 memcpy(skb_put(skb, len), data_kva, len); 594 skb->ip_summed = CHECKSUM_UNNECESSARY; 595 dev_kfree_skb(skb); 596 } 597 598 /* Simulate real usage, allocate/copy skb twice */ 599 skb = netdev_alloc_skb(dev, len); 600 if (skb == NULL) { 601 dev->stats.rx_dropped++; 602 continue; 603 } 604 605 if (kva->nb_segs == 1) { 606 memcpy(skb_put(skb, len), data_kva, len); 607 } else { 608 int nb_segs; 609 int kva_nb_segs = kva->nb_segs; 610 611 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) { 612 memcpy(skb_put(skb, kva->data_len), 613 data_kva, kva->data_len); 614 615 if (!kva->next) 616 break; 617 618 prev_kva = kva; 619 kva = get_kva(kni, kva->next); 620 data_kva = get_data_kva(kni, kva); 621 /* Convert physical address to virtual address */ 622 prev_kva->next = pa2va(prev_kva->next, kva); 623 } 624 } 625 626 skb->ip_summed = CHECKSUM_UNNECESSARY; 627 628 dev->stats.rx_bytes += len; 629 dev->stats.rx_packets++; 630 631 /* call tx interface */ 632 kni_net_tx(skb, dev); 633 } 634 635 /* enqueue all the mbufs from rx_q into free_q */ 636 ret = kni_fifo_put(kni->free_q, kni->va, num); 637 if (ret != num) 638 /* Failing should not happen */ 639 pr_err("Fail to enqueue mbufs into free_q\n"); 640 } 641 642 /* rx interface */ 643 void 644 kni_net_rx(struct kni_dev *kni) 645 { 646 /** 647 * It doesn't need to check if it is NULL pointer, 648 * as it has a default value 649 */ 650 (*kni_net_rx_func)(kni); 651 } 652 653 /* 654 * Deal with a transmit timeout. 655 */ 656 #ifdef HAVE_TX_TIMEOUT_TXQUEUE 657 static void 658 kni_net_tx_timeout(struct net_device *dev, unsigned int txqueue) 659 #else 660 static void 661 kni_net_tx_timeout(struct net_device *dev) 662 #endif 663 { 664 pr_debug("Transmit timeout at %ld, latency %ld\n", jiffies, 665 jiffies - dev_trans_start(dev)); 666 667 dev->stats.tx_errors++; 668 netif_wake_queue(dev); 669 } 670 671 static int 672 kni_net_change_mtu(struct net_device *dev, int new_mtu) 673 { 674 int ret; 675 struct rte_kni_request req; 676 677 pr_debug("kni_net_change_mtu new mtu %d to be set\n", new_mtu); 678 679 memset(&req, 0, sizeof(req)); 680 req.req_id = RTE_KNI_REQ_CHANGE_MTU; 681 req.new_mtu = new_mtu; 682 ret = kni_net_process_request(dev, &req); 683 if (ret == 0 && req.result == 0) 684 dev->mtu = new_mtu; 685 686 return (ret == 0) ? req.result : ret; 687 } 688 689 static void 690 kni_net_change_rx_flags(struct net_device *netdev, int flags) 691 { 692 struct rte_kni_request req; 693 694 memset(&req, 0, sizeof(req)); 695 696 if (flags & IFF_ALLMULTI) { 697 req.req_id = RTE_KNI_REQ_CHANGE_ALLMULTI; 698 699 if (netdev->flags & IFF_ALLMULTI) 700 req.allmulti = 1; 701 else 702 req.allmulti = 0; 703 } 704 705 if (flags & IFF_PROMISC) { 706 req.req_id = RTE_KNI_REQ_CHANGE_PROMISC; 707 708 if (netdev->flags & IFF_PROMISC) 709 req.promiscusity = 1; 710 else 711 req.promiscusity = 0; 712 } 713 714 kni_net_process_request(netdev, &req); 715 } 716 717 /* 718 * Checks if the user space application provided the resp message 719 */ 720 void 721 kni_net_poll_resp(struct kni_dev *kni) 722 { 723 if (kni_fifo_count(kni->resp_q)) 724 wake_up_interruptible(&kni->wq); 725 } 726 727 /* 728 * Fill the eth header 729 */ 730 static int 731 kni_net_header(struct sk_buff *skb, struct net_device *dev, 732 unsigned short type, const void *daddr, 733 const void *saddr, uint32_t len) 734 { 735 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); 736 737 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len); 738 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len); 739 eth->h_proto = htons(type); 740 741 return dev->hard_header_len; 742 } 743 744 /* 745 * Re-fill the eth header 746 */ 747 #ifdef HAVE_REBUILD_HEADER 748 static int 749 kni_net_rebuild_header(struct sk_buff *skb) 750 { 751 struct net_device *dev = skb->dev; 752 struct ethhdr *eth = (struct ethhdr *) skb->data; 753 754 memcpy(eth->h_source, dev->dev_addr, dev->addr_len); 755 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len); 756 757 return 0; 758 } 759 #endif /* < 4.1.0 */ 760 761 /** 762 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC 763 * @netdev: network interface device structure 764 * @p: pointer to an address structure 765 * 766 * Returns 0 on success, negative on failure 767 **/ 768 static int 769 kni_net_set_mac(struct net_device *netdev, void *p) 770 { 771 int ret; 772 struct rte_kni_request req; 773 struct sockaddr *addr = p; 774 775 memset(&req, 0, sizeof(req)); 776 req.req_id = RTE_KNI_REQ_CHANGE_MAC_ADDR; 777 778 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data))) 779 return -EADDRNOTAVAIL; 780 781 memcpy(req.mac_addr, addr->sa_data, netdev->addr_len); 782 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); 783 784 ret = kni_net_process_request(netdev, &req); 785 786 return (ret == 0 ? req.result : ret); 787 } 788 789 #ifdef HAVE_CHANGE_CARRIER_CB 790 static int 791 kni_net_change_carrier(struct net_device *dev, bool new_carrier) 792 { 793 if (new_carrier) 794 netif_carrier_on(dev); 795 else 796 netif_carrier_off(dev); 797 return 0; 798 } 799 #endif 800 801 static const struct header_ops kni_net_header_ops = { 802 .create = kni_net_header, 803 .parse = eth_header_parse, 804 #ifdef HAVE_REBUILD_HEADER 805 .rebuild = kni_net_rebuild_header, 806 #endif /* < 4.1.0 */ 807 .cache = NULL, /* disable caching */ 808 }; 809 810 static const struct net_device_ops kni_net_netdev_ops = { 811 .ndo_open = kni_net_open, 812 .ndo_stop = kni_net_release, 813 .ndo_set_config = kni_net_config, 814 .ndo_change_rx_flags = kni_net_change_rx_flags, 815 .ndo_start_xmit = kni_net_tx, 816 .ndo_change_mtu = kni_net_change_mtu, 817 .ndo_tx_timeout = kni_net_tx_timeout, 818 .ndo_set_mac_address = kni_net_set_mac, 819 #ifdef HAVE_CHANGE_CARRIER_CB 820 .ndo_change_carrier = kni_net_change_carrier, 821 #endif 822 }; 823 824 static void kni_get_drvinfo(struct net_device *dev, 825 struct ethtool_drvinfo *info) 826 { 827 strlcpy(info->version, KNI_VERSION, sizeof(info->version)); 828 strlcpy(info->driver, "kni", sizeof(info->driver)); 829 } 830 831 static const struct ethtool_ops kni_net_ethtool_ops = { 832 .get_drvinfo = kni_get_drvinfo, 833 .get_link = ethtool_op_get_link, 834 }; 835 836 void 837 kni_net_init(struct net_device *dev) 838 { 839 struct kni_dev *kni = netdev_priv(dev); 840 841 init_waitqueue_head(&kni->wq); 842 mutex_init(&kni->sync_lock); 843 844 ether_setup(dev); /* assign some of the fields */ 845 dev->netdev_ops = &kni_net_netdev_ops; 846 dev->header_ops = &kni_net_header_ops; 847 dev->ethtool_ops = &kni_net_ethtool_ops; 848 dev->watchdog_timeo = WD_TIMEOUT; 849 } 850 851 void 852 kni_net_config_lo_mode(char *lo_str) 853 { 854 if (!lo_str) { 855 pr_debug("loopback disabled"); 856 return; 857 } 858 859 if (!strcmp(lo_str, "lo_mode_none")) 860 pr_debug("loopback disabled"); 861 else if (!strcmp(lo_str, "lo_mode_fifo")) { 862 pr_debug("loopback mode=lo_mode_fifo enabled"); 863 kni_net_rx_func = kni_net_rx_lo_fifo; 864 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) { 865 pr_debug("loopback mode=lo_mode_fifo_skb enabled"); 866 kni_net_rx_func = kni_net_rx_lo_fifo_skb; 867 } else { 868 pr_debug("Unknown loopback parameter, disabled"); 869 } 870 } 871