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(sizeof(struct vector_queue), GFP_KERNEL); 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_array(3 + num_extra_frags, 548 sizeof(struct iovec), 549 GFP_KERNEL 550 ); 551 else 552 iov = kmalloc_array(2 + num_extra_frags, 553 sizeof(struct iovec), 554 GFP_KERNEL 555 ); 556 if (iov == NULL) 557 goto out_fail; 558 mmsg_vector->msg_hdr.msg_iov = iov; 559 mmsg_vector->msg_hdr.msg_iovlen = 1; 560 mmsg_vector->msg_hdr.msg_control = NULL; 561 mmsg_vector->msg_hdr.msg_controllen = 0; 562 mmsg_vector->msg_hdr.msg_flags = MSG_DONTWAIT; 563 mmsg_vector->msg_hdr.msg_name = NULL; 564 mmsg_vector->msg_hdr.msg_namelen = 0; 565 if (vp->header_size > 0) { 566 iov->iov_base = kmalloc(header_size, GFP_KERNEL); 567 if (iov->iov_base == NULL) 568 goto out_fail; 569 iov->iov_len = header_size; 570 mmsg_vector->msg_hdr.msg_iovlen = 2; 571 iov++; 572 } 573 iov->iov_base = NULL; 574 iov->iov_len = 0; 575 mmsg_vector++; 576 } 577 spin_lock_init(&result->head_lock); 578 spin_lock_init(&result->tail_lock); 579 atomic_set(&result->queue_depth, 0); 580 result->head = 0; 581 result->tail = 0; 582 return result; 583 out_skb_fail: 584 kfree(result->mmsg_vector); 585 out_mmsg_fail: 586 kfree(result); 587 return NULL; 588 out_fail: 589 destroy_queue(result); 590 return NULL; 591 } 592 593 /* 594 * We do not use the RX queue as a proper wraparound queue for now 595 * This is not necessary because the consumption via napi_gro_receive() 596 * happens in-line. While we can try using the return code of 597 * netif_rx() for flow control there are no drivers doing this today. 598 * For this RX specific use we ignore the tail/head locks and 599 * just read into a prepared queue filled with skbuffs. 600 */ 601 602 static struct sk_buff *prep_skb( 603 struct vector_private *vp, 604 struct user_msghdr *msg) 605 { 606 int linear = vp->max_packet + vp->headroom + SAFETY_MARGIN; 607 struct sk_buff *result; 608 int iov_index = 0, len; 609 struct iovec *iov = msg->msg_iov; 610 int err, nr_frags, frag; 611 skb_frag_t *skb_frag; 612 613 if (vp->req_size <= linear) 614 len = linear; 615 else 616 len = vp->req_size; 617 result = alloc_skb_with_frags( 618 linear, 619 len - vp->max_packet, 620 3, 621 &err, 622 GFP_ATOMIC 623 ); 624 if (vp->header_size > 0) 625 iov_index++; 626 if (result == NULL) { 627 iov[iov_index].iov_base = NULL; 628 iov[iov_index].iov_len = 0; 629 goto done; 630 } 631 skb_reserve(result, vp->headroom); 632 result->dev = vp->dev; 633 skb_put(result, vp->max_packet); 634 result->data_len = len - vp->max_packet; 635 result->len += len - vp->max_packet; 636 skb_reset_mac_header(result); 637 result->ip_summed = CHECKSUM_NONE; 638 iov[iov_index].iov_base = result->data; 639 iov[iov_index].iov_len = vp->max_packet; 640 iov_index++; 641 642 nr_frags = skb_shinfo(result)->nr_frags; 643 for (frag = 0; frag < nr_frags; frag++) { 644 skb_frag = &skb_shinfo(result)->frags[frag]; 645 iov[iov_index].iov_base = skb_frag_address_safe(skb_frag); 646 if (iov[iov_index].iov_base != NULL) 647 iov[iov_index].iov_len = skb_frag_size(skb_frag); 648 else 649 iov[iov_index].iov_len = 0; 650 iov_index++; 651 } 652 done: 653 msg->msg_iovlen = iov_index; 654 return result; 655 } 656 657 658 /* Prepare queue for recvmmsg one-shot rx - fill with fresh sk_buffs */ 659 660 static void prep_queue_for_rx(struct vector_queue *qi) 661 { 662 struct vector_private *vp = netdev_priv(qi->dev); 663 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 664 void **skbuff_vector = qi->skbuff_vector; 665 int i, queue_depth; 666 667 queue_depth = atomic_read(&qi->queue_depth); 668 669 if (queue_depth == 0) 670 return; 671 672 /* RX is always emptied 100% during each cycle, so we do not 673 * have to do the tail wraparound math for it. 674 */ 675 676 qi->head = qi->tail = 0; 677 678 for (i = 0; i < queue_depth; i++) { 679 /* it is OK if allocation fails - recvmmsg with NULL data in 680 * iov argument still performs an RX, just drops the packet 681 * This allows us stop faffing around with a "drop buffer" 682 */ 683 684 *skbuff_vector = prep_skb(vp, &mmsg_vector->msg_hdr); 685 skbuff_vector++; 686 mmsg_vector++; 687 } 688 atomic_set(&qi->queue_depth, 0); 689 } 690 691 static struct vector_device *find_device(int n) 692 { 693 struct vector_device *device; 694 struct list_head *ele; 695 696 spin_lock(&vector_devices_lock); 697 list_for_each(ele, &vector_devices) { 698 device = list_entry(ele, struct vector_device, list); 699 if (device->unit == n) 700 goto out; 701 } 702 device = NULL; 703 out: 704 spin_unlock(&vector_devices_lock); 705 return device; 706 } 707 708 static int vector_parse(char *str, int *index_out, char **str_out, 709 char **error_out) 710 { 711 int n, err; 712 char *start = str; 713 714 while ((*str != ':') && (strlen(str) > 1)) 715 str++; 716 if (*str != ':') { 717 *error_out = "Expected ':' after device number"; 718 return -EINVAL; 719 } 720 *str = '\0'; 721 722 err = kstrtouint(start, 0, &n); 723 if (err < 0) { 724 *error_out = "Bad device number"; 725 return err; 726 } 727 728 str++; 729 if (find_device(n)) { 730 *error_out = "Device already configured"; 731 return -EINVAL; 732 } 733 734 *index_out = n; 735 *str_out = str; 736 return 0; 737 } 738 739 static int vector_config(char *str, char **error_out) 740 { 741 int err, n; 742 char *params; 743 struct arglist *parsed; 744 745 err = vector_parse(str, &n, ¶ms, error_out); 746 if (err != 0) 747 return err; 748 749 /* This string is broken up and the pieces used by the underlying 750 * driver. We should copy it to make sure things do not go wrong 751 * later. 752 */ 753 754 params = kstrdup(params, GFP_KERNEL); 755 if (params == NULL) { 756 *error_out = "vector_config failed to strdup string"; 757 return -ENOMEM; 758 } 759 760 parsed = uml_parse_vector_ifspec(params); 761 762 if (parsed == NULL) { 763 *error_out = "vector_config failed to parse parameters"; 764 kfree(params); 765 return -EINVAL; 766 } 767 768 vector_eth_configure(n, parsed); 769 return 0; 770 } 771 772 static int vector_id(char **str, int *start_out, int *end_out) 773 { 774 char *end; 775 int n; 776 777 n = simple_strtoul(*str, &end, 0); 778 if ((*end != '\0') || (end == *str)) 779 return -1; 780 781 *start_out = n; 782 *end_out = n; 783 *str = end; 784 return n; 785 } 786 787 static int vector_remove(int n, char **error_out) 788 { 789 struct vector_device *vec_d; 790 struct net_device *dev; 791 struct vector_private *vp; 792 793 vec_d = find_device(n); 794 if (vec_d == NULL) 795 return -ENODEV; 796 dev = vec_d->dev; 797 vp = netdev_priv(dev); 798 if (vp->fds != NULL) 799 return -EBUSY; 800 unregister_netdev(dev); 801 platform_device_unregister(&vec_d->pdev); 802 return 0; 803 } 804 805 /* 806 * There is no shared per-transport initialization code, so 807 * we will just initialize each interface one by one and 808 * add them to a list 809 */ 810 811 static struct platform_driver uml_net_driver = { 812 .driver = { 813 .name = DRIVER_NAME, 814 }, 815 }; 816 817 818 static void vector_device_release(struct device *dev) 819 { 820 struct vector_device *device = 821 container_of(dev, struct vector_device, pdev.dev); 822 struct net_device *netdev = device->dev; 823 824 list_del(&device->list); 825 kfree(device); 826 free_netdev(netdev); 827 } 828 829 /* Bog standard recv using recvmsg - not used normally unless the user 830 * explicitly specifies not to use recvmmsg vector RX. 831 */ 832 833 static int vector_legacy_rx(struct vector_private *vp) 834 { 835 int pkt_len; 836 struct user_msghdr hdr; 837 struct iovec iov[2 + MAX_IOV_SIZE]; /* header + data use case only */ 838 int iovpos = 0; 839 struct sk_buff *skb; 840 int header_check; 841 842 hdr.msg_name = NULL; 843 hdr.msg_namelen = 0; 844 hdr.msg_iov = (struct iovec *) &iov; 845 hdr.msg_control = NULL; 846 hdr.msg_controllen = 0; 847 hdr.msg_flags = 0; 848 849 if (vp->header_size > 0) { 850 iov[0].iov_base = vp->header_rxbuffer; 851 iov[0].iov_len = vp->header_size; 852 } 853 854 skb = prep_skb(vp, &hdr); 855 856 if (skb == NULL) { 857 /* Read a packet into drop_buffer and don't do 858 * anything with it. 859 */ 860 iov[iovpos].iov_base = drop_buffer; 861 iov[iovpos].iov_len = DROP_BUFFER_SIZE; 862 hdr.msg_iovlen = 1; 863 vp->dev->stats.rx_dropped++; 864 } 865 866 pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0); 867 if (pkt_len < 0) { 868 vp->in_error = true; 869 return pkt_len; 870 } 871 872 if (skb != NULL) { 873 if (pkt_len > vp->header_size) { 874 if (vp->header_size > 0) { 875 header_check = vp->verify_header( 876 vp->header_rxbuffer, skb, vp); 877 if (header_check < 0) { 878 dev_kfree_skb_irq(skb); 879 vp->dev->stats.rx_dropped++; 880 vp->estats.rx_encaps_errors++; 881 return 0; 882 } 883 if (header_check > 0) { 884 vp->estats.rx_csum_offload_good++; 885 skb->ip_summed = CHECKSUM_UNNECESSARY; 886 } 887 } 888 pskb_trim(skb, pkt_len - vp->rx_header_size); 889 skb->protocol = eth_type_trans(skb, skb->dev); 890 vp->dev->stats.rx_bytes += skb->len; 891 vp->dev->stats.rx_packets++; 892 napi_gro_receive(&vp->napi, skb); 893 } else { 894 dev_kfree_skb_irq(skb); 895 } 896 } 897 return pkt_len; 898 } 899 900 /* 901 * Packet at a time TX which falls back to vector TX if the 902 * underlying transport is busy. 903 */ 904 905 906 907 static int writev_tx(struct vector_private *vp, struct sk_buff *skb) 908 { 909 struct iovec iov[3 + MAX_IOV_SIZE]; 910 int iov_count, pkt_len = 0; 911 912 iov[0].iov_base = vp->header_txbuffer; 913 iov_count = prep_msg(vp, skb, (struct iovec *) &iov); 914 915 if (iov_count < 1) 916 goto drop; 917 918 pkt_len = uml_vector_writev( 919 vp->fds->tx_fd, 920 (struct iovec *) &iov, 921 iov_count 922 ); 923 924 if (pkt_len < 0) 925 goto drop; 926 927 netif_trans_update(vp->dev); 928 netif_wake_queue(vp->dev); 929 930 if (pkt_len > 0) { 931 vp->dev->stats.tx_bytes += skb->len; 932 vp->dev->stats.tx_packets++; 933 } else { 934 vp->dev->stats.tx_dropped++; 935 } 936 consume_skb(skb); 937 return pkt_len; 938 drop: 939 vp->dev->stats.tx_dropped++; 940 consume_skb(skb); 941 if (pkt_len < 0) 942 vp->in_error = true; 943 return pkt_len; 944 } 945 946 /* 947 * Receive as many messages as we can in one call using the special 948 * mmsg vector matched to an skb vector which we prepared earlier. 949 */ 950 951 static int vector_mmsg_rx(struct vector_private *vp, int budget) 952 { 953 int packet_count, i; 954 struct vector_queue *qi = vp->rx_queue; 955 struct sk_buff *skb; 956 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 957 void **skbuff_vector = qi->skbuff_vector; 958 int header_check; 959 960 /* Refresh the vector and make sure it is with new skbs and the 961 * iovs are updated to point to them. 962 */ 963 964 prep_queue_for_rx(qi); 965 966 /* Fire the Lazy Gun - get as many packets as we can in one go. */ 967 968 if (budget > qi->max_depth) 969 budget = qi->max_depth; 970 971 packet_count = uml_vector_recvmmsg( 972 vp->fds->rx_fd, qi->mmsg_vector, budget, 0); 973 974 if (packet_count < 0) 975 vp->in_error = true; 976 977 if (packet_count <= 0) 978 return packet_count; 979 980 /* We treat packet processing as enqueue, buffer refresh as dequeue 981 * The queue_depth tells us how many buffers have been used and how 982 * many do we need to prep the next time prep_queue_for_rx() is called. 983 */ 984 985 atomic_add(packet_count, &qi->queue_depth); 986 987 for (i = 0; i < packet_count; i++) { 988 skb = (*skbuff_vector); 989 if (mmsg_vector->msg_len > vp->header_size) { 990 if (vp->header_size > 0) { 991 header_check = vp->verify_header( 992 mmsg_vector->msg_hdr.msg_iov->iov_base, 993 skb, 994 vp 995 ); 996 if (header_check < 0) { 997 /* Overlay header failed to verify - discard. 998 * We can actually keep this skb and reuse it, 999 * but that will make the prep logic too 1000 * complex. 1001 */ 1002 dev_kfree_skb_irq(skb); 1003 vp->estats.rx_encaps_errors++; 1004 continue; 1005 } 1006 if (header_check > 0) { 1007 vp->estats.rx_csum_offload_good++; 1008 skb->ip_summed = CHECKSUM_UNNECESSARY; 1009 } 1010 } 1011 pskb_trim(skb, 1012 mmsg_vector->msg_len - vp->rx_header_size); 1013 skb->protocol = eth_type_trans(skb, skb->dev); 1014 /* 1015 * We do not need to lock on updating stats here 1016 * The interrupt loop is non-reentrant. 1017 */ 1018 vp->dev->stats.rx_bytes += skb->len; 1019 vp->dev->stats.rx_packets++; 1020 napi_gro_receive(&vp->napi, skb); 1021 } else { 1022 /* Overlay header too short to do anything - discard. 1023 * We can actually keep this skb and reuse it, 1024 * but that will make the prep logic too complex. 1025 */ 1026 if (skb != NULL) 1027 dev_kfree_skb_irq(skb); 1028 } 1029 (*skbuff_vector) = NULL; 1030 /* Move to the next buffer element */ 1031 mmsg_vector++; 1032 skbuff_vector++; 1033 } 1034 if (packet_count > 0) { 1035 if (vp->estats.rx_queue_max < packet_count) 1036 vp->estats.rx_queue_max = packet_count; 1037 vp->estats.rx_queue_running_average = 1038 (vp->estats.rx_queue_running_average + packet_count) >> 1; 1039 } 1040 return packet_count; 1041 } 1042 1043 static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev) 1044 { 1045 struct vector_private *vp = netdev_priv(dev); 1046 int queue_depth = 0; 1047 1048 if (vp->in_error) { 1049 deactivate_fd(vp->fds->rx_fd, vp->rx_irq); 1050 if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0)) 1051 deactivate_fd(vp->fds->tx_fd, vp->tx_irq); 1052 return NETDEV_TX_BUSY; 1053 } 1054 1055 if ((vp->options & VECTOR_TX) == 0) { 1056 writev_tx(vp, skb); 1057 return NETDEV_TX_OK; 1058 } 1059 1060 /* We do BQL only in the vector path, no point doing it in 1061 * packet at a time mode as there is no device queue 1062 */ 1063 1064 netdev_sent_queue(vp->dev, skb->len); 1065 queue_depth = vector_enqueue(vp->tx_queue, skb); 1066 1067 if (queue_depth < vp->tx_queue->max_depth && netdev_xmit_more()) { 1068 mod_timer(&vp->tl, vp->coalesce); 1069 return NETDEV_TX_OK; 1070 } else { 1071 queue_depth = vector_send(vp->tx_queue); 1072 if (queue_depth > 0) 1073 napi_schedule(&vp->napi); 1074 } 1075 1076 return NETDEV_TX_OK; 1077 } 1078 1079 static irqreturn_t vector_rx_interrupt(int irq, void *dev_id) 1080 { 1081 struct net_device *dev = dev_id; 1082 struct vector_private *vp = netdev_priv(dev); 1083 1084 if (!netif_running(dev)) 1085 return IRQ_NONE; 1086 napi_schedule(&vp->napi); 1087 return IRQ_HANDLED; 1088 1089 } 1090 1091 static irqreturn_t vector_tx_interrupt(int irq, void *dev_id) 1092 { 1093 struct net_device *dev = dev_id; 1094 struct vector_private *vp = netdev_priv(dev); 1095 1096 if (!netif_running(dev)) 1097 return IRQ_NONE; 1098 /* We need to pay attention to it only if we got 1099 * -EAGAIN or -ENOBUFFS from sendmmsg. Otherwise 1100 * we ignore it. In the future, it may be worth 1101 * it to improve the IRQ controller a bit to make 1102 * tweaking the IRQ mask less costly 1103 */ 1104 1105 napi_schedule(&vp->napi); 1106 return IRQ_HANDLED; 1107 1108 } 1109 1110 static int irq_rr; 1111 1112 static int vector_net_close(struct net_device *dev) 1113 { 1114 struct vector_private *vp = netdev_priv(dev); 1115 1116 netif_stop_queue(dev); 1117 timer_delete(&vp->tl); 1118 1119 vp->opened = false; 1120 1121 if (vp->fds == NULL) 1122 return 0; 1123 1124 /* Disable and free all IRQS */ 1125 if (vp->rx_irq > 0) { 1126 um_free_irq(vp->rx_irq, dev); 1127 vp->rx_irq = 0; 1128 } 1129 if (vp->tx_irq > 0) { 1130 um_free_irq(vp->tx_irq, dev); 1131 vp->tx_irq = 0; 1132 } 1133 napi_disable(&vp->napi); 1134 netif_napi_del(&vp->napi); 1135 if (vp->fds->rx_fd > 0) { 1136 if (vp->bpf) 1137 uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf); 1138 os_close_file(vp->fds->rx_fd); 1139 vp->fds->rx_fd = -1; 1140 } 1141 if (vp->fds->tx_fd > 0) { 1142 os_close_file(vp->fds->tx_fd); 1143 vp->fds->tx_fd = -1; 1144 } 1145 if (vp->bpf != NULL) 1146 kfree(vp->bpf->filter); 1147 kfree(vp->bpf); 1148 vp->bpf = NULL; 1149 kfree(vp->fds->remote_addr); 1150 kfree(vp->transport_data); 1151 kfree(vp->header_rxbuffer); 1152 kfree(vp->header_txbuffer); 1153 if (vp->rx_queue != NULL) 1154 destroy_queue(vp->rx_queue); 1155 if (vp->tx_queue != NULL) 1156 destroy_queue(vp->tx_queue); 1157 kfree(vp->fds); 1158 vp->fds = NULL; 1159 vp->in_error = false; 1160 return 0; 1161 } 1162 1163 static int vector_poll(struct napi_struct *napi, int budget) 1164 { 1165 struct vector_private *vp = container_of(napi, struct vector_private, napi); 1166 int work_done = 0; 1167 int err; 1168 bool tx_enqueued = false; 1169 1170 if ((vp->options & VECTOR_TX) != 0) 1171 tx_enqueued = (vector_send(vp->tx_queue) > 0); 1172 spin_lock(&vp->rx_queue->head_lock); 1173 if ((vp->options & VECTOR_RX) > 0) 1174 err = vector_mmsg_rx(vp, budget); 1175 else { 1176 err = vector_legacy_rx(vp); 1177 if (err > 0) 1178 err = 1; 1179 } 1180 spin_unlock(&vp->rx_queue->head_lock); 1181 if (err > 0) 1182 work_done += err; 1183 1184 if (tx_enqueued || err > 0) 1185 napi_schedule(napi); 1186 if (work_done <= budget) 1187 napi_complete_done(napi, work_done); 1188 return work_done; 1189 } 1190 1191 static void vector_reset_tx(struct work_struct *work) 1192 { 1193 struct vector_private *vp = 1194 container_of(work, struct vector_private, reset_tx); 1195 netdev_reset_queue(vp->dev); 1196 netif_start_queue(vp->dev); 1197 netif_wake_queue(vp->dev); 1198 } 1199 1200 static int vector_net_open(struct net_device *dev) 1201 { 1202 struct vector_private *vp = netdev_priv(dev); 1203 int err = -EINVAL; 1204 struct vector_device *vdevice; 1205 1206 if (vp->opened) 1207 return -ENXIO; 1208 vp->opened = true; 1209 1210 vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed)); 1211 1212 vp->fds = uml_vector_user_open(vp->unit, vp->parsed); 1213 1214 if (vp->fds == NULL) 1215 goto out_close; 1216 1217 if (build_transport_data(vp) < 0) 1218 goto out_close; 1219 1220 if ((vp->options & VECTOR_RX) > 0) { 1221 vp->rx_queue = create_queue( 1222 vp, 1223 get_depth(vp->parsed), 1224 vp->rx_header_size, 1225 MAX_IOV_SIZE 1226 ); 1227 atomic_set(&vp->rx_queue->queue_depth, get_depth(vp->parsed)); 1228 } else { 1229 vp->header_rxbuffer = kmalloc( 1230 vp->rx_header_size, 1231 GFP_KERNEL 1232 ); 1233 if (vp->header_rxbuffer == NULL) 1234 goto out_close; 1235 } 1236 if ((vp->options & VECTOR_TX) > 0) { 1237 vp->tx_queue = create_queue( 1238 vp, 1239 get_depth(vp->parsed), 1240 vp->header_size, 1241 MAX_IOV_SIZE 1242 ); 1243 } else { 1244 vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL); 1245 if (vp->header_txbuffer == NULL) 1246 goto out_close; 1247 } 1248 1249 netif_napi_add_weight(vp->dev, &vp->napi, vector_poll, 1250 get_depth(vp->parsed)); 1251 napi_enable(&vp->napi); 1252 1253 /* READ IRQ */ 1254 err = um_request_irq( 1255 irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd, 1256 IRQ_READ, vector_rx_interrupt, 1257 IRQF_SHARED, dev->name, dev); 1258 if (err < 0) { 1259 netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err); 1260 err = -ENETUNREACH; 1261 goto out_close; 1262 } 1263 vp->rx_irq = irq_rr + VECTOR_BASE_IRQ; 1264 dev->irq = irq_rr + VECTOR_BASE_IRQ; 1265 irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE; 1266 1267 /* WRITE IRQ - we need it only if we have vector TX */ 1268 if ((vp->options & VECTOR_TX) > 0) { 1269 err = um_request_irq( 1270 irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd, 1271 IRQ_WRITE, vector_tx_interrupt, 1272 IRQF_SHARED, dev->name, dev); 1273 if (err < 0) { 1274 netdev_err(dev, 1275 "vector_open: failed to get tx irq(%d)\n", err); 1276 err = -ENETUNREACH; 1277 goto out_close; 1278 } 1279 vp->tx_irq = irq_rr + VECTOR_BASE_IRQ; 1280 irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE; 1281 } 1282 1283 if ((vp->options & VECTOR_QDISC_BYPASS) != 0) { 1284 if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd)) 1285 vp->options |= VECTOR_BPF; 1286 } 1287 if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL)) 1288 vp->bpf = uml_vector_default_bpf(dev->dev_addr); 1289 1290 if (vp->bpf != NULL) 1291 uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf); 1292 1293 netif_start_queue(dev); 1294 vector_reset_stats(vp); 1295 1296 /* clear buffer - it can happen that the host side of the interface 1297 * is full when we get here. In this case, new data is never queued, 1298 * SIGIOs never arrive, and the net never works. 1299 */ 1300 1301 napi_schedule(&vp->napi); 1302 1303 vdevice = find_device(vp->unit); 1304 vdevice->opened = 1; 1305 1306 if ((vp->options & VECTOR_TX) != 0) 1307 add_timer(&vp->tl); 1308 return 0; 1309 out_close: 1310 vector_net_close(dev); 1311 return err; 1312 } 1313 1314 1315 static void vector_net_set_multicast_list(struct net_device *dev) 1316 { 1317 /* TODO: - we can do some BPF games here */ 1318 return; 1319 } 1320 1321 static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue) 1322 { 1323 struct vector_private *vp = netdev_priv(dev); 1324 1325 vp->estats.tx_timeout_count++; 1326 netif_trans_update(dev); 1327 schedule_work(&vp->reset_tx); 1328 } 1329 1330 static netdev_features_t vector_fix_features(struct net_device *dev, 1331 netdev_features_t features) 1332 { 1333 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); 1334 return features; 1335 } 1336 1337 static int vector_set_features(struct net_device *dev, 1338 netdev_features_t features) 1339 { 1340 struct vector_private *vp = netdev_priv(dev); 1341 /* Adjust buffer sizes for GSO/GRO. Unfortunately, there is 1342 * no way to negotiate it on raw sockets, so we can change 1343 * only our side. 1344 */ 1345 if (features & NETIF_F_GRO) 1346 /* All new frame buffers will be GRO-sized */ 1347 vp->req_size = 65536; 1348 else 1349 /* All new frame buffers will be normal sized */ 1350 vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN; 1351 return 0; 1352 } 1353 1354 #ifdef CONFIG_NET_POLL_CONTROLLER 1355 static void vector_net_poll_controller(struct net_device *dev) 1356 { 1357 disable_irq(dev->irq); 1358 vector_rx_interrupt(dev->irq, dev); 1359 enable_irq(dev->irq); 1360 } 1361 #endif 1362 1363 static void vector_net_get_drvinfo(struct net_device *dev, 1364 struct ethtool_drvinfo *info) 1365 { 1366 strscpy(info->driver, DRIVER_NAME); 1367 } 1368 1369 static int vector_net_load_bpf_flash(struct net_device *dev, 1370 struct ethtool_flash *efl) 1371 { 1372 struct vector_private *vp = netdev_priv(dev); 1373 struct vector_device *vdevice; 1374 const struct firmware *fw; 1375 int result = 0; 1376 1377 if (!(vp->options & VECTOR_BPF_FLASH)) { 1378 netdev_err(dev, "loading firmware not permitted: %s\n", efl->data); 1379 return -1; 1380 } 1381 1382 if (vp->bpf != NULL) { 1383 if (vp->opened) 1384 uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf); 1385 kfree(vp->bpf->filter); 1386 vp->bpf->filter = NULL; 1387 } else { 1388 vp->bpf = kmalloc(sizeof(struct sock_fprog), GFP_ATOMIC); 1389 if (vp->bpf == NULL) { 1390 netdev_err(dev, "failed to allocate memory for firmware\n"); 1391 goto flash_fail; 1392 } 1393 } 1394 1395 vdevice = find_device(vp->unit); 1396 1397 if (request_firmware(&fw, efl->data, &vdevice->pdev.dev)) 1398 goto flash_fail; 1399 1400 vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC); 1401 if (!vp->bpf->filter) 1402 goto free_buffer; 1403 1404 vp->bpf->len = fw->size / sizeof(struct sock_filter); 1405 release_firmware(fw); 1406 1407 if (vp->opened) 1408 result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf); 1409 1410 return result; 1411 1412 free_buffer: 1413 release_firmware(fw); 1414 1415 flash_fail: 1416 if (vp->bpf != NULL) 1417 kfree(vp->bpf->filter); 1418 kfree(vp->bpf); 1419 vp->bpf = NULL; 1420 return -1; 1421 } 1422 1423 static void vector_get_ringparam(struct net_device *netdev, 1424 struct ethtool_ringparam *ring, 1425 struct kernel_ethtool_ringparam *kernel_ring, 1426 struct netlink_ext_ack *extack) 1427 { 1428 struct vector_private *vp = netdev_priv(netdev); 1429 1430 ring->rx_max_pending = vp->rx_queue->max_depth; 1431 ring->tx_max_pending = vp->tx_queue->max_depth; 1432 ring->rx_pending = vp->rx_queue->max_depth; 1433 ring->tx_pending = vp->tx_queue->max_depth; 1434 } 1435 1436 static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf) 1437 { 1438 switch (stringset) { 1439 case ETH_SS_TEST: 1440 *buf = '\0'; 1441 break; 1442 case ETH_SS_STATS: 1443 memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys)); 1444 break; 1445 default: 1446 WARN_ON(1); 1447 break; 1448 } 1449 } 1450 1451 static int vector_get_sset_count(struct net_device *dev, int sset) 1452 { 1453 switch (sset) { 1454 case ETH_SS_TEST: 1455 return 0; 1456 case ETH_SS_STATS: 1457 return VECTOR_NUM_STATS; 1458 default: 1459 return -EOPNOTSUPP; 1460 } 1461 } 1462 1463 static void vector_get_ethtool_stats(struct net_device *dev, 1464 struct ethtool_stats *estats, 1465 u64 *tmp_stats) 1466 { 1467 struct vector_private *vp = netdev_priv(dev); 1468 1469 /* Stats are modified in the dequeue portions of 1470 * rx/tx which are protected by the head locks 1471 * grabbing these locks here ensures they are up 1472 * to date. 1473 */ 1474 1475 spin_lock(&vp->tx_queue->head_lock); 1476 spin_lock(&vp->rx_queue->head_lock); 1477 memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats)); 1478 spin_unlock(&vp->rx_queue->head_lock); 1479 spin_unlock(&vp->tx_queue->head_lock); 1480 } 1481 1482 static int vector_get_coalesce(struct net_device *netdev, 1483 struct ethtool_coalesce *ec, 1484 struct kernel_ethtool_coalesce *kernel_coal, 1485 struct netlink_ext_ack *extack) 1486 { 1487 struct vector_private *vp = netdev_priv(netdev); 1488 1489 ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ; 1490 return 0; 1491 } 1492 1493 static int vector_set_coalesce(struct net_device *netdev, 1494 struct ethtool_coalesce *ec, 1495 struct kernel_ethtool_coalesce *kernel_coal, 1496 struct netlink_ext_ack *extack) 1497 { 1498 struct vector_private *vp = netdev_priv(netdev); 1499 1500 vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000; 1501 if (vp->coalesce == 0) 1502 vp->coalesce = 1; 1503 return 0; 1504 } 1505 1506 static const struct ethtool_ops vector_net_ethtool_ops = { 1507 .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS, 1508 .get_drvinfo = vector_net_get_drvinfo, 1509 .get_link = ethtool_op_get_link, 1510 .get_ts_info = ethtool_op_get_ts_info, 1511 .get_ringparam = vector_get_ringparam, 1512 .get_strings = vector_get_strings, 1513 .get_sset_count = vector_get_sset_count, 1514 .get_ethtool_stats = vector_get_ethtool_stats, 1515 .get_coalesce = vector_get_coalesce, 1516 .set_coalesce = vector_set_coalesce, 1517 .flash_device = vector_net_load_bpf_flash, 1518 }; 1519 1520 1521 static const struct net_device_ops vector_netdev_ops = { 1522 .ndo_open = vector_net_open, 1523 .ndo_stop = vector_net_close, 1524 .ndo_start_xmit = vector_net_start_xmit, 1525 .ndo_set_rx_mode = vector_net_set_multicast_list, 1526 .ndo_tx_timeout = vector_net_tx_timeout, 1527 .ndo_set_mac_address = eth_mac_addr, 1528 .ndo_validate_addr = eth_validate_addr, 1529 .ndo_fix_features = vector_fix_features, 1530 .ndo_set_features = vector_set_features, 1531 #ifdef CONFIG_NET_POLL_CONTROLLER 1532 .ndo_poll_controller = vector_net_poll_controller, 1533 #endif 1534 }; 1535 1536 static void vector_timer_expire(struct timer_list *t) 1537 { 1538 struct vector_private *vp = timer_container_of(vp, t, tl); 1539 1540 vp->estats.tx_kicks++; 1541 napi_schedule(&vp->napi); 1542 } 1543 1544 static void vector_setup_etheraddr(struct net_device *dev, char *str) 1545 { 1546 u8 addr[ETH_ALEN]; 1547 1548 if (str == NULL) 1549 goto random; 1550 1551 if (!mac_pton(str, addr)) { 1552 netdev_err(dev, 1553 "Failed to parse '%s' as an ethernet address\n", str); 1554 goto random; 1555 } 1556 if (is_multicast_ether_addr(addr)) { 1557 netdev_err(dev, 1558 "Attempt to assign a multicast ethernet address to a device disallowed\n"); 1559 goto random; 1560 } 1561 if (!is_valid_ether_addr(addr)) { 1562 netdev_err(dev, 1563 "Attempt to assign an invalid ethernet address to a device disallowed\n"); 1564 goto random; 1565 } 1566 if (!is_local_ether_addr(addr)) { 1567 netdev_warn(dev, "Warning: Assigning a globally valid ethernet address to a device\n"); 1568 netdev_warn(dev, "You should set the 2nd rightmost bit in the first byte of the MAC,\n"); 1569 netdev_warn(dev, "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n", 1570 addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4], addr[5]); 1571 } 1572 eth_hw_addr_set(dev, addr); 1573 return; 1574 1575 random: 1576 netdev_info(dev, "Choosing a random ethernet address\n"); 1577 eth_hw_addr_random(dev); 1578 } 1579 1580 static void vector_eth_configure( 1581 int n, 1582 struct arglist *def 1583 ) 1584 { 1585 struct vector_device *device; 1586 struct net_device *dev; 1587 struct vector_private *vp; 1588 int err; 1589 1590 device = kzalloc(sizeof(*device), GFP_KERNEL); 1591 if (device == NULL) { 1592 pr_err("Failed to allocate struct vector_device for vec%d\n", n); 1593 return; 1594 } 1595 dev = alloc_etherdev(sizeof(struct vector_private)); 1596 if (dev == NULL) { 1597 pr_err("Failed to allocate struct net_device for vec%d\n", n); 1598 goto out_free_device; 1599 } 1600 1601 dev->mtu = get_mtu(def); 1602 1603 INIT_LIST_HEAD(&device->list); 1604 device->unit = n; 1605 1606 /* If this name ends up conflicting with an existing registered 1607 * netdevice, that is OK, register_netdev{,ice}() will notice this 1608 * and fail. 1609 */ 1610 snprintf(dev->name, sizeof(dev->name), "vec%d", n); 1611 vector_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac")); 1612 vp = netdev_priv(dev); 1613 1614 /* sysfs register */ 1615 if (!driver_registered) { 1616 platform_driver_register(¨_net_driver); 1617 driver_registered = 1; 1618 } 1619 device->pdev.id = n; 1620 device->pdev.name = DRIVER_NAME; 1621 device->pdev.dev.release = vector_device_release; 1622 dev_set_drvdata(&device->pdev.dev, device); 1623 if (platform_device_register(&device->pdev)) 1624 goto out_free_netdev; 1625 SET_NETDEV_DEV(dev, &device->pdev.dev); 1626 1627 device->dev = dev; 1628 1629 INIT_LIST_HEAD(&vp->list); 1630 vp->dev = dev; 1631 vp->unit = n; 1632 vp->options = get_transport_options(def); 1633 vp->parsed = def; 1634 vp->max_packet = get_mtu(def) + ETH_HEADER_OTHER; 1635 /* 1636 * TODO - we need to calculate headroom so that ip header 1637 * is 16 byte aligned all the time 1638 */ 1639 vp->headroom = get_headroom(def); 1640 vp->coalesce = 2; 1641 vp->req_size = get_req_size(def); 1642 1643 dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST); 1644 INIT_WORK(&vp->reset_tx, vector_reset_tx); 1645 1646 timer_setup(&vp->tl, vector_timer_expire, 0); 1647 1648 /* FIXME */ 1649 dev->netdev_ops = &vector_netdev_ops; 1650 dev->ethtool_ops = &vector_net_ethtool_ops; 1651 dev->watchdog_timeo = (HZ >> 1); 1652 /* primary IRQ - fixme */ 1653 dev->irq = 0; /* we will adjust this once opened */ 1654 1655 rtnl_lock(); 1656 err = register_netdevice(dev); 1657 rtnl_unlock(); 1658 if (err) 1659 goto out_undo_user_init; 1660 1661 spin_lock(&vector_devices_lock); 1662 list_add(&device->list, &vector_devices); 1663 spin_unlock(&vector_devices_lock); 1664 1665 return; 1666 1667 out_undo_user_init: 1668 return; 1669 out_free_netdev: 1670 free_netdev(dev); 1671 out_free_device: 1672 kfree(device); 1673 } 1674 1675 1676 1677 1678 /* 1679 * Invoked late in the init 1680 */ 1681 1682 static int __init vector_init(void) 1683 { 1684 struct list_head *ele; 1685 struct vector_cmd_line_arg *def; 1686 struct arglist *parsed; 1687 1688 list_for_each(ele, &vec_cmd_line) { 1689 def = list_entry(ele, struct vector_cmd_line_arg, list); 1690 parsed = uml_parse_vector_ifspec(def->arguments); 1691 if (parsed != NULL) 1692 vector_eth_configure(def->unit, parsed); 1693 } 1694 return 0; 1695 } 1696 1697 1698 /* Invoked at initial argument parsing, only stores 1699 * arguments until a proper vector_init is called 1700 * later 1701 */ 1702 1703 static int __init vector_setup(char *str) 1704 { 1705 char *error; 1706 int n, err; 1707 struct vector_cmd_line_arg *new; 1708 1709 err = vector_parse(str, &n, &str, &error); 1710 if (err) { 1711 pr_err("Couldn't parse '%s': %s\n", str, error); 1712 return 1; 1713 } 1714 new = memblock_alloc_or_panic(sizeof(*new), SMP_CACHE_BYTES); 1715 INIT_LIST_HEAD(&new->list); 1716 new->unit = n; 1717 new->arguments = str; 1718 list_add_tail(&new->list, &vec_cmd_line); 1719 return 1; 1720 } 1721 1722 __setup("vec", vector_setup); 1723 __uml_help(vector_setup, 1724 "vec[0-9]+:<option>=<value>,<option>=<value>\n" 1725 " Configure a vector io network device.\n\n" 1726 ); 1727 1728 late_initcall(vector_init); 1729 1730 static struct mc_device vector_mc = { 1731 .list = LIST_HEAD_INIT(vector_mc.list), 1732 .name = "vec", 1733 .config = vector_config, 1734 .get_config = NULL, 1735 .id = vector_id, 1736 .remove = vector_remove, 1737 }; 1738 1739 #ifdef CONFIG_INET 1740 static int vector_inetaddr_event( 1741 struct notifier_block *this, 1742 unsigned long event, 1743 void *ptr) 1744 { 1745 return NOTIFY_DONE; 1746 } 1747 1748 static struct notifier_block vector_inetaddr_notifier = { 1749 .notifier_call = vector_inetaddr_event, 1750 }; 1751 1752 static void inet_register(void) 1753 { 1754 register_inetaddr_notifier(&vector_inetaddr_notifier); 1755 } 1756 #else 1757 static inline void inet_register(void) 1758 { 1759 } 1760 #endif 1761 1762 static int vector_net_init(void) 1763 { 1764 mconsole_register_dev(&vector_mc); 1765 inet_register(); 1766 return 0; 1767 } 1768 1769 __initcall(vector_net_init); 1770 1771 1772 1773