1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2017 - 2019 Cambridge Greys Limited 4 * Copyright (C) 2011 - 2014 Cisco Systems Inc 5 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 6 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and 7 * James Leu (jleu@mindspring.net). 8 * Copyright (C) 2001 by various other people who didn't put their name here. 9 */ 10 11 #define pr_fmt(fmt) "uml-vector: " fmt 12 13 #include <linux/memblock.h> 14 #include <linux/etherdevice.h> 15 #include <linux/ethtool.h> 16 #include <linux/hex.h> 17 #include <linux/inetdevice.h> 18 #include <linux/init.h> 19 #include <linux/list.h> 20 #include <linux/netdevice.h> 21 #include <linux/platform_device.h> 22 #include <linux/rtnetlink.h> 23 #include <linux/skbuff.h> 24 #include <linux/slab.h> 25 #include <linux/interrupt.h> 26 #include <linux/firmware.h> 27 #include <linux/fs.h> 28 #include <asm/atomic.h> 29 #include <uapi/linux/filter.h> 30 #include <init.h> 31 #include <irq_kern.h> 32 #include <irq_user.h> 33 #include <os.h> 34 #include "mconsole_kern.h" 35 #include "vector_user.h" 36 #include "vector_kern.h" 37 38 /* 39 * Adapted from network devices with the following major changes: 40 * All transports are static - simplifies the code significantly 41 * Multiple FDs/IRQs per device 42 * Vector IO optionally used for read/write, falling back to legacy 43 * based on configuration and/or availability 44 * Configuration is no longer positional - L2TPv3 and GRE require up to 45 * 10 parameters, passing this as positional is not fit for purpose. 46 * Only socket transports are supported 47 */ 48 49 50 #define DRIVER_NAME "uml-vector" 51 struct vector_cmd_line_arg { 52 struct list_head list; 53 int unit; 54 char *arguments; 55 }; 56 57 struct vector_device { 58 struct list_head list; 59 struct net_device *dev; 60 struct platform_device pdev; 61 int unit; 62 int opened; 63 }; 64 65 static LIST_HEAD(vec_cmd_line); 66 67 static DEFINE_SPINLOCK(vector_devices_lock); 68 static LIST_HEAD(vector_devices); 69 70 static int driver_registered; 71 72 static void vector_eth_configure(int n, struct arglist *def); 73 static int vector_mmsg_rx(struct vector_private *vp, int budget); 74 75 /* Argument accessors to set variables (and/or set default values) 76 * mtu, buffer sizing, default headroom, etc 77 */ 78 79 #define DEFAULT_HEADROOM 2 80 #define SAFETY_MARGIN 32 81 #define DEFAULT_VECTOR_SIZE 64 82 #define TX_SMALL_PACKET 128 83 #define MAX_IOV_SIZE (MAX_SKB_FRAGS + 1) 84 85 static const struct { 86 const char string[ETH_GSTRING_LEN]; 87 } ethtool_stats_keys[] = { 88 { "rx_queue_max" }, 89 { "rx_queue_running_average" }, 90 { "tx_queue_max" }, 91 { "tx_queue_running_average" }, 92 { "rx_encaps_errors" }, 93 { "tx_timeout_count" }, 94 { "tx_restart_queue" }, 95 { "tx_kicks" }, 96 { "tx_flow_control_xon" }, 97 { "tx_flow_control_xoff" }, 98 { "rx_csum_offload_good" }, 99 { "rx_csum_offload_errors"}, 100 { "sg_ok"}, 101 { "sg_linearized"}, 102 }; 103 104 #define VECTOR_NUM_STATS ARRAY_SIZE(ethtool_stats_keys) 105 106 static void vector_reset_stats(struct vector_private *vp) 107 { 108 /* We reuse the existing queue locks for stats */ 109 110 /* RX stats are modified with RX head_lock held 111 * in vector_poll. 112 */ 113 114 spin_lock(&vp->rx_queue->head_lock); 115 vp->estats.rx_queue_max = 0; 116 vp->estats.rx_queue_running_average = 0; 117 vp->estats.rx_encaps_errors = 0; 118 vp->estats.sg_ok = 0; 119 vp->estats.sg_linearized = 0; 120 spin_unlock(&vp->rx_queue->head_lock); 121 122 /* TX stats are modified with TX head_lock held 123 * in vector_send. 124 */ 125 126 spin_lock(&vp->tx_queue->head_lock); 127 vp->estats.tx_timeout_count = 0; 128 vp->estats.tx_restart_queue = 0; 129 vp->estats.tx_kicks = 0; 130 vp->estats.tx_flow_control_xon = 0; 131 vp->estats.tx_flow_control_xoff = 0; 132 vp->estats.tx_queue_max = 0; 133 vp->estats.tx_queue_running_average = 0; 134 spin_unlock(&vp->tx_queue->head_lock); 135 } 136 137 static int get_mtu(struct arglist *def) 138 { 139 char *mtu = uml_vector_fetch_arg(def, "mtu"); 140 long result; 141 142 if (mtu != NULL) { 143 if (kstrtoul(mtu, 10, &result) == 0) 144 if ((result < (1 << 16) - 1) && (result >= 576)) 145 return result; 146 } 147 return ETH_MAX_PACKET; 148 } 149 150 static char *get_bpf_file(struct arglist *def) 151 { 152 return uml_vector_fetch_arg(def, "bpffile"); 153 } 154 155 static bool get_bpf_flash(struct arglist *def) 156 { 157 char *allow = uml_vector_fetch_arg(def, "bpfflash"); 158 long result; 159 160 if (allow != NULL) { 161 if (kstrtoul(allow, 10, &result) == 0) 162 return result > 0; 163 } 164 return false; 165 } 166 167 static int get_depth(struct arglist *def) 168 { 169 char *mtu = uml_vector_fetch_arg(def, "depth"); 170 long result; 171 172 if (mtu != NULL) { 173 if (kstrtoul(mtu, 10, &result) == 0) 174 return result; 175 } 176 return DEFAULT_VECTOR_SIZE; 177 } 178 179 static int get_headroom(struct arglist *def) 180 { 181 char *mtu = uml_vector_fetch_arg(def, "headroom"); 182 long result; 183 184 if (mtu != NULL) { 185 if (kstrtoul(mtu, 10, &result) == 0) 186 return result; 187 } 188 return DEFAULT_HEADROOM; 189 } 190 191 static int get_req_size(struct arglist *def) 192 { 193 char *gro = uml_vector_fetch_arg(def, "gro"); 194 long result; 195 196 if (gro != NULL) { 197 if (kstrtoul(gro, 10, &result) == 0) { 198 if (result > 0) 199 return 65536; 200 } 201 } 202 return get_mtu(def) + ETH_HEADER_OTHER + 203 get_headroom(def) + SAFETY_MARGIN; 204 } 205 206 207 static int get_transport_options(struct arglist *def) 208 { 209 char *transport = uml_vector_fetch_arg(def, "transport"); 210 char *vector = uml_vector_fetch_arg(def, "vec"); 211 212 int vec_rx = VECTOR_RX; 213 int vec_tx = VECTOR_TX; 214 long parsed; 215 int result = 0; 216 217 if (transport == NULL) 218 return -EINVAL; 219 220 if (vector != NULL) { 221 if (kstrtoul(vector, 10, &parsed) == 0) { 222 if (parsed == 0) { 223 vec_rx = 0; 224 vec_tx = 0; 225 } 226 } 227 } 228 229 if (get_bpf_flash(def)) 230 result = VECTOR_BPF_FLASH; 231 232 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0) 233 return result; 234 if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0) 235 return (result | vec_rx | VECTOR_BPF); 236 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0) 237 return (result | vec_rx | vec_tx | VECTOR_QDISC_BYPASS); 238 return (result | vec_rx | vec_tx); 239 } 240 241 242 /* A mini-buffer for packet drop read 243 * All of our supported transports are datagram oriented and we always 244 * read using recvmsg or recvmmsg. If we pass a buffer which is smaller 245 * than the packet size it still counts as full packet read and will 246 * clean the incoming stream to keep sigio/epoll happy 247 */ 248 249 #define DROP_BUFFER_SIZE 32 250 251 static char *drop_buffer; 252 253 254 /* 255 * Advance the mmsg queue head by n = advance. Resets the queue to 256 * maximum enqueue/dequeue-at-once capacity if possible. Called by 257 * dequeuers. Caller must hold the head_lock! 258 */ 259 260 static int vector_advancehead(struct vector_queue *qi, int advance) 261 { 262 qi->head = 263 (qi->head + advance) 264 % qi->max_depth; 265 266 267 atomic_sub(advance, &qi->queue_depth); 268 return atomic_read(&qi->queue_depth); 269 } 270 271 /* Advance the queue tail by n = advance. 272 * This is called by enqueuers which should hold the 273 * head lock already 274 */ 275 276 static int vector_advancetail(struct vector_queue *qi, int advance) 277 { 278 qi->tail = 279 (qi->tail + advance) 280 % qi->max_depth; 281 atomic_add(advance, &qi->queue_depth); 282 return atomic_read(&qi->queue_depth); 283 } 284 285 static int prep_msg(struct vector_private *vp, 286 struct sk_buff *skb, 287 struct iovec *iov) 288 { 289 int iov_index = 0; 290 int nr_frags, frag; 291 skb_frag_t *skb_frag; 292 293 nr_frags = skb_shinfo(skb)->nr_frags; 294 if (nr_frags > MAX_IOV_SIZE) { 295 if (skb_linearize(skb) != 0) 296 goto drop; 297 } 298 if (vp->header_size > 0) { 299 iov[iov_index].iov_len = vp->header_size; 300 vp->form_header(iov[iov_index].iov_base, skb, vp); 301 iov_index++; 302 } 303 iov[iov_index].iov_base = skb->data; 304 if (nr_frags > 0) { 305 iov[iov_index].iov_len = skb->len - skb->data_len; 306 vp->estats.sg_ok++; 307 } else 308 iov[iov_index].iov_len = skb->len; 309 iov_index++; 310 for (frag = 0; frag < nr_frags; frag++) { 311 skb_frag = &skb_shinfo(skb)->frags[frag]; 312 iov[iov_index].iov_base = skb_frag_address_safe(skb_frag); 313 iov[iov_index].iov_len = skb_frag_size(skb_frag); 314 iov_index++; 315 } 316 return iov_index; 317 drop: 318 return -1; 319 } 320 /* 321 * Generic vector enqueue with support for forming headers using transport 322 * specific callback. Allows GRE, L2TPv3, RAW and other transports 323 * to use a common enqueue procedure in vector mode 324 */ 325 326 static int vector_enqueue(struct vector_queue *qi, struct sk_buff *skb) 327 { 328 struct vector_private *vp = netdev_priv(qi->dev); 329 int queue_depth; 330 int packet_len; 331 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 332 int iov_count; 333 334 spin_lock(&qi->tail_lock); 335 queue_depth = atomic_read(&qi->queue_depth); 336 337 if (skb) 338 packet_len = skb->len; 339 340 if (queue_depth < qi->max_depth) { 341 342 *(qi->skbuff_vector + qi->tail) = skb; 343 mmsg_vector += qi->tail; 344 iov_count = prep_msg( 345 vp, 346 skb, 347 mmsg_vector->msg_hdr.msg_iov 348 ); 349 if (iov_count < 1) 350 goto drop; 351 mmsg_vector->msg_hdr.msg_iovlen = iov_count; 352 mmsg_vector->msg_hdr.msg_name = vp->fds->remote_addr; 353 mmsg_vector->msg_hdr.msg_namelen = vp->fds->remote_addr_size; 354 wmb(); /* Make the packet visible to the NAPI poll thread */ 355 queue_depth = vector_advancetail(qi, 1); 356 } else 357 goto drop; 358 spin_unlock(&qi->tail_lock); 359 return queue_depth; 360 drop: 361 qi->dev->stats.tx_dropped++; 362 if (skb != NULL) { 363 packet_len = skb->len; 364 dev_consume_skb_any(skb); 365 netdev_completed_queue(qi->dev, 1, packet_len); 366 } 367 spin_unlock(&qi->tail_lock); 368 return queue_depth; 369 } 370 371 static int consume_vector_skbs(struct vector_queue *qi, int count) 372 { 373 struct sk_buff *skb; 374 int skb_index; 375 int bytes_compl = 0; 376 377 for (skb_index = qi->head; skb_index < qi->head + count; skb_index++) { 378 skb = *(qi->skbuff_vector + skb_index); 379 /* mark as empty to ensure correct destruction if 380 * needed 381 */ 382 bytes_compl += skb->len; 383 *(qi->skbuff_vector + skb_index) = NULL; 384 dev_consume_skb_any(skb); 385 } 386 qi->dev->stats.tx_bytes += bytes_compl; 387 qi->dev->stats.tx_packets += count; 388 netdev_completed_queue(qi->dev, count, bytes_compl); 389 return vector_advancehead(qi, count); 390 } 391 392 /* 393 * Generic vector dequeue via sendmmsg with support for forming headers 394 * using transport specific callback. Allows GRE, L2TPv3, RAW and 395 * other transports to use a common dequeue procedure in vector mode 396 */ 397 398 399 static int vector_send(struct vector_queue *qi) 400 { 401 struct vector_private *vp = netdev_priv(qi->dev); 402 struct mmsghdr *send_from; 403 int result = 0, send_len; 404 405 if (spin_trylock(&qi->head_lock)) { 406 /* update queue_depth to current value */ 407 while (atomic_read(&qi->queue_depth) > 0) { 408 /* Calculate the start of the vector */ 409 send_len = atomic_read(&qi->queue_depth); 410 send_from = qi->mmsg_vector; 411 send_from += qi->head; 412 /* Adjust vector size if wraparound */ 413 if (send_len + qi->head > qi->max_depth) 414 send_len = qi->max_depth - qi->head; 415 /* Try to TX as many packets as possible */ 416 if (send_len > 0) { 417 result = uml_vector_sendmmsg( 418 vp->fds->tx_fd, 419 send_from, 420 send_len, 421 0 422 ); 423 vp->in_write_poll = 424 (result != send_len); 425 } 426 /* For some of the sendmmsg error scenarios 427 * we may end being unsure in the TX success 428 * for all packets. It is safer to declare 429 * them all TX-ed and blame the network. 430 */ 431 if (result < 0) { 432 if (net_ratelimit()) 433 netdev_err(vp->dev, "sendmmsg err=%i\n", 434 result); 435 vp->in_error = true; 436 result = send_len; 437 } 438 if (result > 0) { 439 consume_vector_skbs(qi, result); 440 /* This is equivalent to an TX IRQ. 441 * Restart the upper layers to feed us 442 * more packets. 443 */ 444 if (result > vp->estats.tx_queue_max) 445 vp->estats.tx_queue_max = result; 446 vp->estats.tx_queue_running_average = 447 (vp->estats.tx_queue_running_average + result) >> 1; 448 } 449 netif_wake_queue(qi->dev); 450 /* if TX is busy, break out of the send loop, 451 * poll write IRQ will reschedule xmit for us. 452 */ 453 if (result != send_len) { 454 vp->estats.tx_restart_queue++; 455 break; 456 } 457 } 458 spin_unlock(&qi->head_lock); 459 } 460 return atomic_read(&qi->queue_depth); 461 } 462 463 /* Queue destructor. Deliberately stateless so we can use 464 * it in queue cleanup if initialization fails. 465 */ 466 467 static void destroy_queue(struct vector_queue *qi) 468 { 469 int i; 470 struct iovec *iov; 471 struct vector_private *vp = netdev_priv(qi->dev); 472 struct mmsghdr *mmsg_vector; 473 474 if (qi == NULL) 475 return; 476 /* deallocate any skbuffs - we rely on any unused to be 477 * set to NULL. 478 */ 479 if (qi->skbuff_vector != NULL) { 480 for (i = 0; i < qi->max_depth; i++) { 481 if (*(qi->skbuff_vector + i) != NULL) 482 dev_kfree_skb_any(*(qi->skbuff_vector + i)); 483 } 484 kfree(qi->skbuff_vector); 485 } 486 /* deallocate matching IOV structures including header buffs */ 487 if (qi->mmsg_vector != NULL) { 488 mmsg_vector = qi->mmsg_vector; 489 for (i = 0; i < qi->max_depth; i++) { 490 iov = mmsg_vector->msg_hdr.msg_iov; 491 if (iov != NULL) { 492 if ((vp->header_size > 0) && 493 (iov->iov_base != NULL)) 494 kfree(iov->iov_base); 495 kfree(iov); 496 } 497 mmsg_vector++; 498 } 499 kfree(qi->mmsg_vector); 500 } 501 kfree(qi); 502 } 503 504 /* 505 * Queue constructor. Create a queue with a given side. 506 */ 507 static struct vector_queue *create_queue( 508 struct vector_private *vp, 509 int max_size, 510 int header_size, 511 int num_extra_frags) 512 { 513 struct vector_queue *result; 514 int i; 515 struct iovec *iov; 516 struct mmsghdr *mmsg_vector; 517 518 result = kmalloc_obj(struct vector_queue); 519 if (result == NULL) 520 return NULL; 521 result->max_depth = max_size; 522 result->dev = vp->dev; 523 result->mmsg_vector = kmalloc( 524 (sizeof(struct mmsghdr) * max_size), GFP_KERNEL); 525 if (result->mmsg_vector == NULL) 526 goto out_mmsg_fail; 527 result->skbuff_vector = kmalloc( 528 (sizeof(void *) * max_size), GFP_KERNEL); 529 if (result->skbuff_vector == NULL) 530 goto out_skb_fail; 531 532 /* further failures can be handled safely by destroy_queue*/ 533 534 mmsg_vector = result->mmsg_vector; 535 for (i = 0; i < max_size; i++) { 536 /* Clear all pointers - we use non-NULL as marking on 537 * what to free on destruction 538 */ 539 *(result->skbuff_vector + i) = NULL; 540 mmsg_vector->msg_hdr.msg_iov = NULL; 541 mmsg_vector++; 542 } 543 mmsg_vector = result->mmsg_vector; 544 result->max_iov_frags = num_extra_frags; 545 for (i = 0; i < max_size; i++) { 546 if (vp->header_size > 0) 547 iov = kmalloc_objs(struct iovec, 3 + num_extra_frags); 548 else 549 iov = kmalloc_objs(struct iovec, 2 + num_extra_frags); 550 if (iov == NULL) 551 goto out_fail; 552 mmsg_vector->msg_hdr.msg_iov = iov; 553 mmsg_vector->msg_hdr.msg_iovlen = 1; 554 mmsg_vector->msg_hdr.msg_control = NULL; 555 mmsg_vector->msg_hdr.msg_controllen = 0; 556 mmsg_vector->msg_hdr.msg_flags = MSG_DONTWAIT; 557 mmsg_vector->msg_hdr.msg_name = NULL; 558 mmsg_vector->msg_hdr.msg_namelen = 0; 559 if (vp->header_size > 0) { 560 iov->iov_base = kmalloc(header_size, GFP_KERNEL); 561 if (iov->iov_base == NULL) 562 goto out_fail; 563 iov->iov_len = header_size; 564 mmsg_vector->msg_hdr.msg_iovlen = 2; 565 iov++; 566 } 567 iov->iov_base = NULL; 568 iov->iov_len = 0; 569 mmsg_vector++; 570 } 571 spin_lock_init(&result->head_lock); 572 spin_lock_init(&result->tail_lock); 573 atomic_set(&result->queue_depth, 0); 574 result->head = 0; 575 result->tail = 0; 576 return result; 577 out_skb_fail: 578 kfree(result->mmsg_vector); 579 out_mmsg_fail: 580 kfree(result); 581 return NULL; 582 out_fail: 583 destroy_queue(result); 584 return NULL; 585 } 586 587 /* 588 * We do not use the RX queue as a proper wraparound queue for now 589 * This is not necessary because the consumption via napi_gro_receive() 590 * happens in-line. While we can try using the return code of 591 * netif_rx() for flow control there are no drivers doing this today. 592 * For this RX specific use we ignore the tail/head locks and 593 * just read into a prepared queue filled with skbuffs. 594 */ 595 596 static struct sk_buff *prep_skb( 597 struct vector_private *vp, 598 struct user_msghdr *msg) 599 { 600 int linear = vp->max_packet + vp->headroom + SAFETY_MARGIN; 601 struct sk_buff *result; 602 int iov_index = 0, len; 603 struct iovec *iov = msg->msg_iov; 604 int err, nr_frags, frag; 605 skb_frag_t *skb_frag; 606 607 if (vp->req_size <= linear) 608 len = linear; 609 else 610 len = vp->req_size; 611 result = alloc_skb_with_frags( 612 linear, 613 len - vp->max_packet, 614 3, 615 &err, 616 GFP_ATOMIC 617 ); 618 if (vp->header_size > 0) 619 iov_index++; 620 if (result == NULL) { 621 iov[iov_index].iov_base = NULL; 622 iov[iov_index].iov_len = 0; 623 goto done; 624 } 625 skb_reserve(result, vp->headroom); 626 result->dev = vp->dev; 627 skb_put(result, vp->max_packet); 628 result->data_len = len - vp->max_packet; 629 result->len += len - vp->max_packet; 630 skb_reset_mac_header(result); 631 result->ip_summed = CHECKSUM_NONE; 632 iov[iov_index].iov_base = result->data; 633 iov[iov_index].iov_len = vp->max_packet; 634 iov_index++; 635 636 nr_frags = skb_shinfo(result)->nr_frags; 637 for (frag = 0; frag < nr_frags; frag++) { 638 skb_frag = &skb_shinfo(result)->frags[frag]; 639 iov[iov_index].iov_base = skb_frag_address_safe(skb_frag); 640 if (iov[iov_index].iov_base != NULL) 641 iov[iov_index].iov_len = skb_frag_size(skb_frag); 642 else 643 iov[iov_index].iov_len = 0; 644 iov_index++; 645 } 646 done: 647 msg->msg_iovlen = iov_index; 648 return result; 649 } 650 651 652 /* Prepare queue for recvmmsg one-shot rx - fill with fresh sk_buffs */ 653 654 static void prep_queue_for_rx(struct vector_queue *qi) 655 { 656 struct vector_private *vp = netdev_priv(qi->dev); 657 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 658 void **skbuff_vector = qi->skbuff_vector; 659 int i, queue_depth; 660 661 queue_depth = atomic_read(&qi->queue_depth); 662 663 if (queue_depth == 0) 664 return; 665 666 /* RX is always emptied 100% during each cycle, so we do not 667 * have to do the tail wraparound math for it. 668 */ 669 670 qi->head = qi->tail = 0; 671 672 for (i = 0; i < queue_depth; i++) { 673 /* it is OK if allocation fails - recvmmsg with NULL data in 674 * iov argument still performs an RX, just drops the packet 675 * This allows us stop faffing around with a "drop buffer" 676 */ 677 678 *skbuff_vector = prep_skb(vp, &mmsg_vector->msg_hdr); 679 skbuff_vector++; 680 mmsg_vector++; 681 } 682 atomic_set(&qi->queue_depth, 0); 683 } 684 685 static struct vector_device *find_device(int n) 686 { 687 struct vector_device *device; 688 struct list_head *ele; 689 690 spin_lock(&vector_devices_lock); 691 list_for_each(ele, &vector_devices) { 692 device = list_entry(ele, struct vector_device, list); 693 if (device->unit == n) 694 goto out; 695 } 696 device = NULL; 697 out: 698 spin_unlock(&vector_devices_lock); 699 return device; 700 } 701 702 static int vector_parse(char *str, int *index_out, char **str_out, 703 char **error_out) 704 { 705 int n, err; 706 char *start = str; 707 708 while ((*str != ':') && (strlen(str) > 1)) 709 str++; 710 if (*str != ':') { 711 *error_out = "Expected ':' after device number"; 712 return -EINVAL; 713 } 714 *str = '\0'; 715 716 err = kstrtouint(start, 0, &n); 717 if (err < 0) { 718 *error_out = "Bad device number"; 719 return err; 720 } 721 722 str++; 723 if (find_device(n)) { 724 *error_out = "Device already configured"; 725 return -EINVAL; 726 } 727 728 *index_out = n; 729 *str_out = str; 730 return 0; 731 } 732 733 static int vector_config(char *str, char **error_out) 734 { 735 int err, n; 736 char *params; 737 struct arglist *parsed; 738 739 err = vector_parse(str, &n, ¶ms, error_out); 740 if (err != 0) 741 return err; 742 743 /* This string is broken up and the pieces used by the underlying 744 * driver. We should copy it to make sure things do not go wrong 745 * later. 746 */ 747 748 params = kstrdup(params, GFP_KERNEL); 749 if (params == NULL) { 750 *error_out = "vector_config failed to strdup string"; 751 return -ENOMEM; 752 } 753 754 parsed = uml_parse_vector_ifspec(params); 755 756 if (parsed == NULL) { 757 *error_out = "vector_config failed to parse parameters"; 758 kfree(params); 759 return -EINVAL; 760 } 761 762 vector_eth_configure(n, parsed); 763 return 0; 764 } 765 766 static int vector_id(char **str, int *start_out, int *end_out) 767 { 768 char *end; 769 int n; 770 771 n = simple_strtoul(*str, &end, 0); 772 if ((*end != '\0') || (end == *str)) 773 return -1; 774 775 *start_out = n; 776 *end_out = n; 777 *str = end; 778 return n; 779 } 780 781 static int vector_remove(int n, char **error_out) 782 { 783 struct vector_device *vec_d; 784 struct net_device *dev; 785 struct vector_private *vp; 786 787 vec_d = find_device(n); 788 if (vec_d == NULL) 789 return -ENODEV; 790 dev = vec_d->dev; 791 vp = netdev_priv(dev); 792 if (vp->fds != NULL) 793 return -EBUSY; 794 unregister_netdev(dev); 795 platform_device_unregister(&vec_d->pdev); 796 return 0; 797 } 798 799 /* 800 * There is no shared per-transport initialization code, so 801 * we will just initialize each interface one by one and 802 * add them to a list 803 */ 804 805 static struct platform_driver uml_net_driver = { 806 .driver = { 807 .name = DRIVER_NAME, 808 }, 809 }; 810 811 812 static void vector_device_release(struct device *dev) 813 { 814 struct vector_device *device = 815 container_of(dev, struct vector_device, pdev.dev); 816 struct net_device *netdev = device->dev; 817 818 list_del(&device->list); 819 kfree(device); 820 free_netdev(netdev); 821 } 822 823 /* Bog standard recv using recvmsg - not used normally unless the user 824 * explicitly specifies not to use recvmmsg vector RX. 825 */ 826 827 static int vector_legacy_rx(struct vector_private *vp) 828 { 829 int pkt_len; 830 struct user_msghdr hdr; 831 struct iovec iov[2 + MAX_IOV_SIZE]; /* header + data use case only */ 832 int iovpos = 0; 833 struct sk_buff *skb; 834 int header_check; 835 836 hdr.msg_name = NULL; 837 hdr.msg_namelen = 0; 838 hdr.msg_iov = (struct iovec *) &iov; 839 hdr.msg_control = NULL; 840 hdr.msg_controllen = 0; 841 hdr.msg_flags = 0; 842 843 if (vp->header_size > 0) { 844 iov[0].iov_base = vp->header_rxbuffer; 845 iov[0].iov_len = vp->header_size; 846 } 847 848 skb = prep_skb(vp, &hdr); 849 850 if (skb == NULL) { 851 /* Read a packet into drop_buffer and don't do 852 * anything with it. 853 */ 854 iov[iovpos].iov_base = drop_buffer; 855 iov[iovpos].iov_len = DROP_BUFFER_SIZE; 856 hdr.msg_iovlen = 1; 857 vp->dev->stats.rx_dropped++; 858 } 859 860 pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0); 861 if (pkt_len < 0) { 862 vp->in_error = true; 863 return pkt_len; 864 } 865 866 if (skb != NULL) { 867 if (pkt_len > vp->header_size) { 868 if (vp->header_size > 0) { 869 header_check = vp->verify_header( 870 vp->header_rxbuffer, skb, vp); 871 if (header_check < 0) { 872 dev_kfree_skb_irq(skb); 873 vp->dev->stats.rx_dropped++; 874 vp->estats.rx_encaps_errors++; 875 return 0; 876 } 877 if (header_check > 0) { 878 vp->estats.rx_csum_offload_good++; 879 skb->ip_summed = CHECKSUM_UNNECESSARY; 880 } 881 } 882 pskb_trim(skb, pkt_len - vp->rx_header_size); 883 skb->protocol = eth_type_trans(skb, skb->dev); 884 vp->dev->stats.rx_bytes += skb->len; 885 vp->dev->stats.rx_packets++; 886 napi_gro_receive(&vp->napi, skb); 887 } else { 888 dev_kfree_skb_irq(skb); 889 } 890 } 891 return pkt_len; 892 } 893 894 /* 895 * Packet at a time TX which falls back to vector TX if the 896 * underlying transport is busy. 897 */ 898 899 900 901 static int writev_tx(struct vector_private *vp, struct sk_buff *skb) 902 { 903 struct iovec iov[3 + MAX_IOV_SIZE]; 904 int iov_count, pkt_len = 0; 905 906 iov[0].iov_base = vp->header_txbuffer; 907 iov_count = prep_msg(vp, skb, (struct iovec *) &iov); 908 909 if (iov_count < 1) 910 goto drop; 911 912 pkt_len = uml_vector_writev( 913 vp->fds->tx_fd, 914 (struct iovec *) &iov, 915 iov_count 916 ); 917 918 if (pkt_len < 0) 919 goto drop; 920 921 netif_trans_update(vp->dev); 922 netif_wake_queue(vp->dev); 923 924 if (pkt_len > 0) { 925 vp->dev->stats.tx_bytes += skb->len; 926 vp->dev->stats.tx_packets++; 927 } else { 928 vp->dev->stats.tx_dropped++; 929 } 930 consume_skb(skb); 931 return pkt_len; 932 drop: 933 vp->dev->stats.tx_dropped++; 934 consume_skb(skb); 935 if (pkt_len < 0) 936 vp->in_error = true; 937 return pkt_len; 938 } 939 940 /* 941 * Receive as many messages as we can in one call using the special 942 * mmsg vector matched to an skb vector which we prepared earlier. 943 */ 944 945 static int vector_mmsg_rx(struct vector_private *vp, int budget) 946 { 947 int packet_count, i; 948 struct vector_queue *qi = vp->rx_queue; 949 struct sk_buff *skb; 950 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 951 void **skbuff_vector = qi->skbuff_vector; 952 int header_check; 953 954 /* Refresh the vector and make sure it is with new skbs and the 955 * iovs are updated to point to them. 956 */ 957 958 prep_queue_for_rx(qi); 959 960 /* Fire the Lazy Gun - get as many packets as we can in one go. */ 961 962 if (budget > qi->max_depth) 963 budget = qi->max_depth; 964 965 packet_count = uml_vector_recvmmsg( 966 vp->fds->rx_fd, qi->mmsg_vector, budget, 0); 967 968 if (packet_count < 0) 969 vp->in_error = true; 970 971 if (packet_count <= 0) 972 return packet_count; 973 974 /* We treat packet processing as enqueue, buffer refresh as dequeue 975 * The queue_depth tells us how many buffers have been used and how 976 * many do we need to prep the next time prep_queue_for_rx() is called. 977 */ 978 979 atomic_add(packet_count, &qi->queue_depth); 980 981 for (i = 0; i < packet_count; i++) { 982 skb = (*skbuff_vector); 983 if (mmsg_vector->msg_len > vp->header_size) { 984 if (vp->header_size > 0) { 985 header_check = vp->verify_header( 986 mmsg_vector->msg_hdr.msg_iov->iov_base, 987 skb, 988 vp 989 ); 990 if (header_check < 0) { 991 /* Overlay header failed to verify - discard. 992 * We can actually keep this skb and reuse it, 993 * but that will make the prep logic too 994 * complex. 995 */ 996 dev_kfree_skb_irq(skb); 997 vp->estats.rx_encaps_errors++; 998 continue; 999 } 1000 if (header_check > 0) { 1001 vp->estats.rx_csum_offload_good++; 1002 skb->ip_summed = CHECKSUM_UNNECESSARY; 1003 } 1004 } 1005 pskb_trim(skb, 1006 mmsg_vector->msg_len - vp->rx_header_size); 1007 skb->protocol = eth_type_trans(skb, skb->dev); 1008 /* 1009 * We do not need to lock on updating stats here 1010 * The interrupt loop is non-reentrant. 1011 */ 1012 vp->dev->stats.rx_bytes += skb->len; 1013 vp->dev->stats.rx_packets++; 1014 napi_gro_receive(&vp->napi, skb); 1015 } else { 1016 /* Overlay header too short to do anything - discard. 1017 * We can actually keep this skb and reuse it, 1018 * but that will make the prep logic too complex. 1019 */ 1020 if (skb != NULL) 1021 dev_kfree_skb_irq(skb); 1022 } 1023 (*skbuff_vector) = NULL; 1024 /* Move to the next buffer element */ 1025 mmsg_vector++; 1026 skbuff_vector++; 1027 } 1028 if (packet_count > 0) { 1029 if (vp->estats.rx_queue_max < packet_count) 1030 vp->estats.rx_queue_max = packet_count; 1031 vp->estats.rx_queue_running_average = 1032 (vp->estats.rx_queue_running_average + packet_count) >> 1; 1033 } 1034 return packet_count; 1035 } 1036 1037 static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev) 1038 { 1039 struct vector_private *vp = netdev_priv(dev); 1040 int queue_depth = 0; 1041 1042 if (vp->in_error) { 1043 deactivate_fd(vp->fds->rx_fd, vp->rx_irq); 1044 if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0)) 1045 deactivate_fd(vp->fds->tx_fd, vp->tx_irq); 1046 return NETDEV_TX_BUSY; 1047 } 1048 1049 if ((vp->options & VECTOR_TX) == 0) { 1050 writev_tx(vp, skb); 1051 return NETDEV_TX_OK; 1052 } 1053 1054 /* We do BQL only in the vector path, no point doing it in 1055 * packet at a time mode as there is no device queue 1056 */ 1057 1058 netdev_sent_queue(vp->dev, skb->len); 1059 queue_depth = vector_enqueue(vp->tx_queue, skb); 1060 1061 if (queue_depth < vp->tx_queue->max_depth && netdev_xmit_more()) { 1062 mod_timer(&vp->tl, vp->coalesce); 1063 return NETDEV_TX_OK; 1064 } else { 1065 queue_depth = vector_send(vp->tx_queue); 1066 if (queue_depth > 0) 1067 napi_schedule(&vp->napi); 1068 } 1069 1070 return NETDEV_TX_OK; 1071 } 1072 1073 static irqreturn_t vector_rx_interrupt(int irq, void *dev_id) 1074 { 1075 struct net_device *dev = dev_id; 1076 struct vector_private *vp = netdev_priv(dev); 1077 1078 if (!netif_running(dev)) 1079 return IRQ_NONE; 1080 napi_schedule(&vp->napi); 1081 return IRQ_HANDLED; 1082 1083 } 1084 1085 static irqreturn_t vector_tx_interrupt(int irq, void *dev_id) 1086 { 1087 struct net_device *dev = dev_id; 1088 struct vector_private *vp = netdev_priv(dev); 1089 1090 if (!netif_running(dev)) 1091 return IRQ_NONE; 1092 /* We need to pay attention to it only if we got 1093 * -EAGAIN or -ENOBUFFS from sendmmsg. Otherwise 1094 * we ignore it. In the future, it may be worth 1095 * it to improve the IRQ controller a bit to make 1096 * tweaking the IRQ mask less costly 1097 */ 1098 1099 napi_schedule(&vp->napi); 1100 return IRQ_HANDLED; 1101 1102 } 1103 1104 static int irq_rr; 1105 1106 static int vector_net_close(struct net_device *dev) 1107 { 1108 struct vector_private *vp = netdev_priv(dev); 1109 1110 netif_stop_queue(dev); 1111 timer_delete(&vp->tl); 1112 1113 vp->opened = false; 1114 1115 if (vp->fds == NULL) 1116 return 0; 1117 1118 /* Disable and free all IRQS */ 1119 if (vp->rx_irq > 0) { 1120 um_free_irq(vp->rx_irq, dev); 1121 vp->rx_irq = 0; 1122 } 1123 if (vp->tx_irq > 0) { 1124 um_free_irq(vp->tx_irq, dev); 1125 vp->tx_irq = 0; 1126 } 1127 napi_disable(&vp->napi); 1128 netif_napi_del(&vp->napi); 1129 if (vp->fds->rx_fd > 0) { 1130 if (vp->bpf) 1131 uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf); 1132 os_close_file(vp->fds->rx_fd); 1133 vp->fds->rx_fd = -1; 1134 } 1135 if (vp->fds->tx_fd > 0) { 1136 os_close_file(vp->fds->tx_fd); 1137 vp->fds->tx_fd = -1; 1138 } 1139 if (vp->bpf != NULL) 1140 kfree(vp->bpf->filter); 1141 kfree(vp->bpf); 1142 vp->bpf = NULL; 1143 kfree(vp->fds->remote_addr); 1144 kfree(vp->transport_data); 1145 kfree(vp->header_rxbuffer); 1146 kfree(vp->header_txbuffer); 1147 if (vp->rx_queue != NULL) 1148 destroy_queue(vp->rx_queue); 1149 if (vp->tx_queue != NULL) 1150 destroy_queue(vp->tx_queue); 1151 kfree(vp->fds); 1152 vp->fds = NULL; 1153 vp->in_error = false; 1154 return 0; 1155 } 1156 1157 static int vector_poll(struct napi_struct *napi, int budget) 1158 { 1159 struct vector_private *vp = container_of(napi, struct vector_private, napi); 1160 int work_done = 0; 1161 int err; 1162 bool tx_enqueued = false; 1163 1164 if ((vp->options & VECTOR_TX) != 0) 1165 tx_enqueued = (vector_send(vp->tx_queue) > 0); 1166 spin_lock(&vp->rx_queue->head_lock); 1167 if ((vp->options & VECTOR_RX) > 0) 1168 err = vector_mmsg_rx(vp, budget); 1169 else { 1170 err = vector_legacy_rx(vp); 1171 if (err > 0) 1172 err = 1; 1173 } 1174 spin_unlock(&vp->rx_queue->head_lock); 1175 if (err > 0) 1176 work_done += err; 1177 1178 if (tx_enqueued || err > 0) 1179 napi_schedule(napi); 1180 if (work_done <= budget) 1181 napi_complete_done(napi, work_done); 1182 return work_done; 1183 } 1184 1185 static void vector_reset_tx(struct work_struct *work) 1186 { 1187 struct vector_private *vp = 1188 container_of(work, struct vector_private, reset_tx); 1189 netdev_reset_queue(vp->dev); 1190 netif_start_queue(vp->dev); 1191 netif_wake_queue(vp->dev); 1192 } 1193 1194 static int vector_net_open(struct net_device *dev) 1195 { 1196 struct vector_private *vp = netdev_priv(dev); 1197 int err = -EINVAL; 1198 struct vector_device *vdevice; 1199 1200 if (vp->opened) 1201 return -ENXIO; 1202 vp->opened = true; 1203 1204 vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed)); 1205 1206 vp->fds = uml_vector_user_open(vp->unit, vp->parsed); 1207 1208 if (vp->fds == NULL) 1209 goto out_close; 1210 1211 if (build_transport_data(vp) < 0) 1212 goto out_close; 1213 1214 if ((vp->options & VECTOR_RX) > 0) { 1215 vp->rx_queue = create_queue( 1216 vp, 1217 get_depth(vp->parsed), 1218 vp->rx_header_size, 1219 MAX_IOV_SIZE 1220 ); 1221 atomic_set(&vp->rx_queue->queue_depth, get_depth(vp->parsed)); 1222 } else { 1223 vp->header_rxbuffer = kmalloc( 1224 vp->rx_header_size, 1225 GFP_KERNEL 1226 ); 1227 if (vp->header_rxbuffer == NULL) 1228 goto out_close; 1229 } 1230 if ((vp->options & VECTOR_TX) > 0) { 1231 vp->tx_queue = create_queue( 1232 vp, 1233 get_depth(vp->parsed), 1234 vp->header_size, 1235 MAX_IOV_SIZE 1236 ); 1237 } else { 1238 vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL); 1239 if (vp->header_txbuffer == NULL) 1240 goto out_close; 1241 } 1242 1243 netif_napi_add_weight(vp->dev, &vp->napi, vector_poll, 1244 get_depth(vp->parsed)); 1245 napi_enable(&vp->napi); 1246 1247 /* READ IRQ */ 1248 err = um_request_irq( 1249 irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd, 1250 IRQ_READ, vector_rx_interrupt, 1251 IRQF_SHARED, dev->name, dev); 1252 if (err < 0) { 1253 netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err); 1254 err = -ENETUNREACH; 1255 goto out_close; 1256 } 1257 vp->rx_irq = irq_rr + VECTOR_BASE_IRQ; 1258 dev->irq = irq_rr + VECTOR_BASE_IRQ; 1259 irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE; 1260 1261 /* WRITE IRQ - we need it only if we have vector TX */ 1262 if ((vp->options & VECTOR_TX) > 0) { 1263 err = um_request_irq( 1264 irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd, 1265 IRQ_WRITE, vector_tx_interrupt, 1266 IRQF_SHARED, dev->name, dev); 1267 if (err < 0) { 1268 netdev_err(dev, 1269 "vector_open: failed to get tx irq(%d)\n", err); 1270 err = -ENETUNREACH; 1271 goto out_close; 1272 } 1273 vp->tx_irq = irq_rr + VECTOR_BASE_IRQ; 1274 irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE; 1275 } 1276 1277 if ((vp->options & VECTOR_QDISC_BYPASS) != 0) { 1278 if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd)) 1279 vp->options |= VECTOR_BPF; 1280 } 1281 if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL)) 1282 vp->bpf = uml_vector_default_bpf(dev->dev_addr); 1283 1284 if (vp->bpf != NULL) 1285 uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf); 1286 1287 netif_start_queue(dev); 1288 vector_reset_stats(vp); 1289 1290 /* clear buffer - it can happen that the host side of the interface 1291 * is full when we get here. In this case, new data is never queued, 1292 * SIGIOs never arrive, and the net never works. 1293 */ 1294 1295 napi_schedule(&vp->napi); 1296 1297 vdevice = find_device(vp->unit); 1298 vdevice->opened = 1; 1299 1300 if ((vp->options & VECTOR_TX) != 0) 1301 add_timer(&vp->tl); 1302 return 0; 1303 out_close: 1304 vector_net_close(dev); 1305 return err; 1306 } 1307 1308 1309 static void vector_net_set_multicast_list(struct net_device *dev) 1310 { 1311 /* TODO: - we can do some BPF games here */ 1312 return; 1313 } 1314 1315 static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue) 1316 { 1317 struct vector_private *vp = netdev_priv(dev); 1318 1319 vp->estats.tx_timeout_count++; 1320 netif_trans_update(dev); 1321 schedule_work(&vp->reset_tx); 1322 } 1323 1324 static netdev_features_t vector_fix_features(struct net_device *dev, 1325 netdev_features_t features) 1326 { 1327 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); 1328 return features; 1329 } 1330 1331 static int vector_set_features(struct net_device *dev, 1332 netdev_features_t features) 1333 { 1334 struct vector_private *vp = netdev_priv(dev); 1335 /* Adjust buffer sizes for GSO/GRO. Unfortunately, there is 1336 * no way to negotiate it on raw sockets, so we can change 1337 * only our side. 1338 */ 1339 if (features & NETIF_F_GRO) 1340 /* All new frame buffers will be GRO-sized */ 1341 vp->req_size = 65536; 1342 else 1343 /* All new frame buffers will be normal sized */ 1344 vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN; 1345 return 0; 1346 } 1347 1348 #ifdef CONFIG_NET_POLL_CONTROLLER 1349 static void vector_net_poll_controller(struct net_device *dev) 1350 { 1351 disable_irq(dev->irq); 1352 vector_rx_interrupt(dev->irq, dev); 1353 enable_irq(dev->irq); 1354 } 1355 #endif 1356 1357 static void vector_net_get_drvinfo(struct net_device *dev, 1358 struct ethtool_drvinfo *info) 1359 { 1360 strscpy(info->driver, DRIVER_NAME); 1361 } 1362 1363 static int vector_net_load_bpf_flash(struct net_device *dev, 1364 struct ethtool_flash *efl) 1365 { 1366 struct vector_private *vp = netdev_priv(dev); 1367 struct vector_device *vdevice; 1368 const struct firmware *fw; 1369 int result = 0; 1370 1371 if (!(vp->options & VECTOR_BPF_FLASH)) { 1372 netdev_err(dev, "loading firmware not permitted: %s\n", efl->data); 1373 return -1; 1374 } 1375 1376 if (vp->bpf != NULL) { 1377 if (vp->opened) 1378 uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf); 1379 kfree(vp->bpf->filter); 1380 vp->bpf->filter = NULL; 1381 } else { 1382 vp->bpf = kmalloc_obj(struct sock_fprog, GFP_ATOMIC); 1383 if (vp->bpf == NULL) { 1384 netdev_err(dev, "failed to allocate memory for firmware\n"); 1385 goto flash_fail; 1386 } 1387 } 1388 1389 vdevice = find_device(vp->unit); 1390 1391 if (request_firmware(&fw, efl->data, &vdevice->pdev.dev)) 1392 goto flash_fail; 1393 1394 vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC); 1395 if (!vp->bpf->filter) 1396 goto free_buffer; 1397 1398 vp->bpf->len = fw->size / sizeof(struct sock_filter); 1399 release_firmware(fw); 1400 1401 if (vp->opened) 1402 result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf); 1403 1404 return result; 1405 1406 free_buffer: 1407 release_firmware(fw); 1408 1409 flash_fail: 1410 if (vp->bpf != NULL) 1411 kfree(vp->bpf->filter); 1412 kfree(vp->bpf); 1413 vp->bpf = NULL; 1414 return -1; 1415 } 1416 1417 static void vector_get_ringparam(struct net_device *netdev, 1418 struct ethtool_ringparam *ring, 1419 struct kernel_ethtool_ringparam *kernel_ring, 1420 struct netlink_ext_ack *extack) 1421 { 1422 struct vector_private *vp = netdev_priv(netdev); 1423 1424 ring->rx_max_pending = vp->rx_queue->max_depth; 1425 ring->tx_max_pending = vp->tx_queue->max_depth; 1426 ring->rx_pending = vp->rx_queue->max_depth; 1427 ring->tx_pending = vp->tx_queue->max_depth; 1428 } 1429 1430 static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf) 1431 { 1432 switch (stringset) { 1433 case ETH_SS_TEST: 1434 *buf = '\0'; 1435 break; 1436 case ETH_SS_STATS: 1437 memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys)); 1438 break; 1439 default: 1440 WARN_ON(1); 1441 break; 1442 } 1443 } 1444 1445 static int vector_get_sset_count(struct net_device *dev, int sset) 1446 { 1447 switch (sset) { 1448 case ETH_SS_TEST: 1449 return 0; 1450 case ETH_SS_STATS: 1451 return VECTOR_NUM_STATS; 1452 default: 1453 return -EOPNOTSUPP; 1454 } 1455 } 1456 1457 static void vector_get_ethtool_stats(struct net_device *dev, 1458 struct ethtool_stats *estats, 1459 u64 *tmp_stats) 1460 { 1461 struct vector_private *vp = netdev_priv(dev); 1462 1463 /* Stats are modified in the dequeue portions of 1464 * rx/tx which are protected by the head locks 1465 * grabbing these locks here ensures they are up 1466 * to date. 1467 */ 1468 1469 spin_lock(&vp->tx_queue->head_lock); 1470 spin_lock(&vp->rx_queue->head_lock); 1471 memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats)); 1472 spin_unlock(&vp->rx_queue->head_lock); 1473 spin_unlock(&vp->tx_queue->head_lock); 1474 } 1475 1476 static int vector_get_coalesce(struct net_device *netdev, 1477 struct ethtool_coalesce *ec, 1478 struct kernel_ethtool_coalesce *kernel_coal, 1479 struct netlink_ext_ack *extack) 1480 { 1481 struct vector_private *vp = netdev_priv(netdev); 1482 1483 ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ; 1484 return 0; 1485 } 1486 1487 static int vector_set_coalesce(struct net_device *netdev, 1488 struct ethtool_coalesce *ec, 1489 struct kernel_ethtool_coalesce *kernel_coal, 1490 struct netlink_ext_ack *extack) 1491 { 1492 struct vector_private *vp = netdev_priv(netdev); 1493 1494 vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000; 1495 if (vp->coalesce == 0) 1496 vp->coalesce = 1; 1497 return 0; 1498 } 1499 1500 static const struct ethtool_ops vector_net_ethtool_ops = { 1501 .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS, 1502 .get_drvinfo = vector_net_get_drvinfo, 1503 .get_link = ethtool_op_get_link, 1504 .get_ts_info = ethtool_op_get_ts_info, 1505 .get_ringparam = vector_get_ringparam, 1506 .get_strings = vector_get_strings, 1507 .get_sset_count = vector_get_sset_count, 1508 .get_ethtool_stats = vector_get_ethtool_stats, 1509 .get_coalesce = vector_get_coalesce, 1510 .set_coalesce = vector_set_coalesce, 1511 .flash_device = vector_net_load_bpf_flash, 1512 }; 1513 1514 1515 static const struct net_device_ops vector_netdev_ops = { 1516 .ndo_open = vector_net_open, 1517 .ndo_stop = vector_net_close, 1518 .ndo_start_xmit = vector_net_start_xmit, 1519 .ndo_set_rx_mode = vector_net_set_multicast_list, 1520 .ndo_tx_timeout = vector_net_tx_timeout, 1521 .ndo_set_mac_address = eth_mac_addr, 1522 .ndo_validate_addr = eth_validate_addr, 1523 .ndo_fix_features = vector_fix_features, 1524 .ndo_set_features = vector_set_features, 1525 #ifdef CONFIG_NET_POLL_CONTROLLER 1526 .ndo_poll_controller = vector_net_poll_controller, 1527 #endif 1528 }; 1529 1530 static void vector_timer_expire(struct timer_list *t) 1531 { 1532 struct vector_private *vp = timer_container_of(vp, t, tl); 1533 1534 vp->estats.tx_kicks++; 1535 napi_schedule(&vp->napi); 1536 } 1537 1538 static void vector_setup_etheraddr(struct net_device *dev, char *str) 1539 { 1540 u8 addr[ETH_ALEN]; 1541 1542 if (str == NULL) 1543 goto random; 1544 1545 if (!mac_pton(str, addr)) { 1546 netdev_err(dev, 1547 "Failed to parse '%s' as an ethernet address\n", str); 1548 goto random; 1549 } 1550 if (is_multicast_ether_addr(addr)) { 1551 netdev_err(dev, 1552 "Attempt to assign a multicast ethernet address to a device disallowed\n"); 1553 goto random; 1554 } 1555 if (!is_valid_ether_addr(addr)) { 1556 netdev_err(dev, 1557 "Attempt to assign an invalid ethernet address to a device disallowed\n"); 1558 goto random; 1559 } 1560 if (!is_local_ether_addr(addr)) { 1561 netdev_warn(dev, "Warning: Assigning a globally valid ethernet address to a device\n"); 1562 netdev_warn(dev, "You should set the 2nd rightmost bit in the first byte of the MAC,\n"); 1563 netdev_warn(dev, "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n", 1564 addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4], addr[5]); 1565 } 1566 eth_hw_addr_set(dev, addr); 1567 return; 1568 1569 random: 1570 netdev_info(dev, "Choosing a random ethernet address\n"); 1571 eth_hw_addr_random(dev); 1572 } 1573 1574 static void vector_eth_configure( 1575 int n, 1576 struct arglist *def 1577 ) 1578 { 1579 struct vector_device *device; 1580 struct net_device *dev; 1581 struct vector_private *vp; 1582 int err; 1583 1584 device = kzalloc_obj(*device); 1585 if (device == NULL) { 1586 pr_err("Failed to allocate struct vector_device for vec%d\n", n); 1587 return; 1588 } 1589 dev = alloc_etherdev(sizeof(struct vector_private)); 1590 if (dev == NULL) { 1591 pr_err("Failed to allocate struct net_device for vec%d\n", n); 1592 goto out_free_device; 1593 } 1594 1595 dev->mtu = get_mtu(def); 1596 1597 INIT_LIST_HEAD(&device->list); 1598 device->unit = n; 1599 1600 /* If this name ends up conflicting with an existing registered 1601 * netdevice, that is OK, register_netdev{,ice}() will notice this 1602 * and fail. 1603 */ 1604 snprintf(dev->name, sizeof(dev->name), "vec%d", n); 1605 vector_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac")); 1606 vp = netdev_priv(dev); 1607 1608 /* sysfs register */ 1609 if (!driver_registered) { 1610 platform_driver_register(¨_net_driver); 1611 driver_registered = 1; 1612 } 1613 device->pdev.id = n; 1614 device->pdev.name = DRIVER_NAME; 1615 device->pdev.dev.release = vector_device_release; 1616 dev_set_drvdata(&device->pdev.dev, device); 1617 if (platform_device_register(&device->pdev)) 1618 goto out_free_netdev; 1619 SET_NETDEV_DEV(dev, &device->pdev.dev); 1620 1621 device->dev = dev; 1622 1623 INIT_LIST_HEAD(&vp->list); 1624 vp->dev = dev; 1625 vp->unit = n; 1626 vp->options = get_transport_options(def); 1627 vp->parsed = def; 1628 vp->max_packet = get_mtu(def) + ETH_HEADER_OTHER; 1629 /* 1630 * TODO - we need to calculate headroom so that ip header 1631 * is 16 byte aligned all the time 1632 */ 1633 vp->headroom = get_headroom(def); 1634 vp->coalesce = 2; 1635 vp->req_size = get_req_size(def); 1636 1637 dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST); 1638 INIT_WORK(&vp->reset_tx, vector_reset_tx); 1639 1640 timer_setup(&vp->tl, vector_timer_expire, 0); 1641 1642 /* FIXME */ 1643 dev->netdev_ops = &vector_netdev_ops; 1644 dev->ethtool_ops = &vector_net_ethtool_ops; 1645 dev->watchdog_timeo = (HZ >> 1); 1646 /* primary IRQ - fixme */ 1647 dev->irq = 0; /* we will adjust this once opened */ 1648 1649 rtnl_lock(); 1650 err = register_netdevice(dev); 1651 rtnl_unlock(); 1652 if (err) 1653 goto out_undo_user_init; 1654 1655 spin_lock(&vector_devices_lock); 1656 list_add(&device->list, &vector_devices); 1657 spin_unlock(&vector_devices_lock); 1658 1659 return; 1660 1661 out_undo_user_init: 1662 return; 1663 out_free_netdev: 1664 free_netdev(dev); 1665 out_free_device: 1666 kfree(device); 1667 } 1668 1669 1670 1671 1672 /* 1673 * Invoked late in the init 1674 */ 1675 1676 static int __init vector_init(void) 1677 { 1678 struct list_head *ele; 1679 struct vector_cmd_line_arg *def; 1680 struct arglist *parsed; 1681 1682 list_for_each(ele, &vec_cmd_line) { 1683 def = list_entry(ele, struct vector_cmd_line_arg, list); 1684 parsed = uml_parse_vector_ifspec(def->arguments); 1685 if (parsed != NULL) 1686 vector_eth_configure(def->unit, parsed); 1687 } 1688 return 0; 1689 } 1690 1691 1692 /* Invoked at initial argument parsing, only stores 1693 * arguments until a proper vector_init is called 1694 * later 1695 */ 1696 1697 static int __init vector_setup(char *str) 1698 { 1699 char *error; 1700 int n, err; 1701 struct vector_cmd_line_arg *new; 1702 1703 err = vector_parse(str, &n, &str, &error); 1704 if (err) { 1705 pr_err("Couldn't parse '%s': %s\n", str, error); 1706 return 1; 1707 } 1708 new = memblock_alloc_or_panic(sizeof(*new), SMP_CACHE_BYTES); 1709 INIT_LIST_HEAD(&new->list); 1710 new->unit = n; 1711 new->arguments = str; 1712 list_add_tail(&new->list, &vec_cmd_line); 1713 return 1; 1714 } 1715 1716 __setup("vec", vector_setup); 1717 __uml_help(vector_setup, 1718 "vec[0-9]+:<option>=<value>,<option>=<value>\n" 1719 " Configure a vector io network device.\n\n" 1720 ); 1721 1722 late_initcall(vector_init); 1723 1724 static struct mc_device vector_mc = { 1725 .list = LIST_HEAD_INIT(vector_mc.list), 1726 .name = "vec", 1727 .config = vector_config, 1728 .get_config = NULL, 1729 .id = vector_id, 1730 .remove = vector_remove, 1731 }; 1732 1733 #ifdef CONFIG_INET 1734 static int vector_inetaddr_event( 1735 struct notifier_block *this, 1736 unsigned long event, 1737 void *ptr) 1738 { 1739 return NOTIFY_DONE; 1740 } 1741 1742 static struct notifier_block vector_inetaddr_notifier = { 1743 .notifier_call = vector_inetaddr_event, 1744 }; 1745 1746 static void inet_register(void) 1747 { 1748 register_inetaddr_notifier(&vector_inetaddr_notifier); 1749 } 1750 #else 1751 static inline void inet_register(void) 1752 { 1753 } 1754 #endif 1755 1756 static int vector_net_init(void) 1757 { 1758 mconsole_register_dev(&vector_mc); 1759 inet_register(); 1760 return 0; 1761 } 1762 1763 __initcall(vector_net_init); 1764 1765 1766 1767