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 (*skbuff_vector) = NULL; 999 mmsg_vector++; 1000 skbuff_vector++; 1001 continue; 1002 } 1003 if (header_check > 0) { 1004 vp->estats.rx_csum_offload_good++; 1005 skb->ip_summed = CHECKSUM_UNNECESSARY; 1006 } 1007 } 1008 pskb_trim(skb, 1009 mmsg_vector->msg_len - vp->rx_header_size); 1010 skb->protocol = eth_type_trans(skb, skb->dev); 1011 /* 1012 * We do not need to lock on updating stats here 1013 * The interrupt loop is non-reentrant. 1014 */ 1015 vp->dev->stats.rx_bytes += skb->len; 1016 vp->dev->stats.rx_packets++; 1017 napi_gro_receive(&vp->napi, skb); 1018 } else { 1019 /* Overlay header too short to do anything - discard. 1020 * We can actually keep this skb and reuse it, 1021 * but that will make the prep logic too complex. 1022 */ 1023 if (skb != NULL) 1024 dev_kfree_skb_irq(skb); 1025 } 1026 (*skbuff_vector) = NULL; 1027 /* Move to the next buffer element */ 1028 mmsg_vector++; 1029 skbuff_vector++; 1030 } 1031 if (packet_count > 0) { 1032 if (vp->estats.rx_queue_max < packet_count) 1033 vp->estats.rx_queue_max = packet_count; 1034 vp->estats.rx_queue_running_average = 1035 (vp->estats.rx_queue_running_average + packet_count) >> 1; 1036 } 1037 return packet_count; 1038 } 1039 1040 static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev) 1041 { 1042 struct vector_private *vp = netdev_priv(dev); 1043 int queue_depth = 0; 1044 1045 if (vp->in_error) { 1046 deactivate_fd(vp->fds->rx_fd, vp->rx_irq); 1047 if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0)) 1048 deactivate_fd(vp->fds->tx_fd, vp->tx_irq); 1049 return NETDEV_TX_BUSY; 1050 } 1051 1052 if ((vp->options & VECTOR_TX) == 0) { 1053 writev_tx(vp, skb); 1054 return NETDEV_TX_OK; 1055 } 1056 1057 /* We do BQL only in the vector path, no point doing it in 1058 * packet at a time mode as there is no device queue 1059 */ 1060 1061 netdev_sent_queue(vp->dev, skb->len); 1062 queue_depth = vector_enqueue(vp->tx_queue, skb); 1063 1064 if (queue_depth < vp->tx_queue->max_depth && netdev_xmit_more()) { 1065 mod_timer(&vp->tl, vp->coalesce); 1066 return NETDEV_TX_OK; 1067 } else { 1068 queue_depth = vector_send(vp->tx_queue); 1069 if (queue_depth > 0) 1070 napi_schedule(&vp->napi); 1071 } 1072 1073 return NETDEV_TX_OK; 1074 } 1075 1076 static irqreturn_t vector_rx_interrupt(int irq, void *dev_id) 1077 { 1078 struct net_device *dev = dev_id; 1079 struct vector_private *vp = netdev_priv(dev); 1080 1081 if (!netif_running(dev)) 1082 return IRQ_NONE; 1083 napi_schedule(&vp->napi); 1084 return IRQ_HANDLED; 1085 1086 } 1087 1088 static irqreturn_t vector_tx_interrupt(int irq, void *dev_id) 1089 { 1090 struct net_device *dev = dev_id; 1091 struct vector_private *vp = netdev_priv(dev); 1092 1093 if (!netif_running(dev)) 1094 return IRQ_NONE; 1095 /* We need to pay attention to it only if we got 1096 * -EAGAIN or -ENOBUFFS from sendmmsg. Otherwise 1097 * we ignore it. In the future, it may be worth 1098 * it to improve the IRQ controller a bit to make 1099 * tweaking the IRQ mask less costly 1100 */ 1101 1102 napi_schedule(&vp->napi); 1103 return IRQ_HANDLED; 1104 1105 } 1106 1107 static int irq_rr; 1108 1109 static int vector_net_close(struct net_device *dev) 1110 { 1111 struct vector_private *vp = netdev_priv(dev); 1112 1113 netif_stop_queue(dev); 1114 timer_delete(&vp->tl); 1115 1116 vp->opened = false; 1117 1118 if (vp->fds == NULL) 1119 return 0; 1120 1121 /* Disable and free all IRQS */ 1122 if (vp->rx_irq > 0) { 1123 um_free_irq(vp->rx_irq, dev); 1124 vp->rx_irq = 0; 1125 } 1126 if (vp->tx_irq > 0) { 1127 um_free_irq(vp->tx_irq, dev); 1128 vp->tx_irq = 0; 1129 } 1130 napi_disable(&vp->napi); 1131 netif_napi_del(&vp->napi); 1132 if (vp->fds->rx_fd > 0) { 1133 if (vp->bpf) 1134 uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf); 1135 os_close_file(vp->fds->rx_fd); 1136 vp->fds->rx_fd = -1; 1137 } 1138 if (vp->fds->tx_fd > 0) { 1139 os_close_file(vp->fds->tx_fd); 1140 vp->fds->tx_fd = -1; 1141 } 1142 if (vp->bpf != NULL) 1143 kfree(vp->bpf->filter); 1144 kfree(vp->bpf); 1145 vp->bpf = NULL; 1146 kfree(vp->fds->remote_addr); 1147 kfree(vp->transport_data); 1148 kfree(vp->header_rxbuffer); 1149 kfree(vp->header_txbuffer); 1150 if (vp->rx_queue != NULL) 1151 destroy_queue(vp->rx_queue); 1152 if (vp->tx_queue != NULL) 1153 destroy_queue(vp->tx_queue); 1154 kfree(vp->fds); 1155 vp->fds = NULL; 1156 vp->in_error = false; 1157 return 0; 1158 } 1159 1160 static int vector_poll(struct napi_struct *napi, int budget) 1161 { 1162 struct vector_private *vp = container_of(napi, struct vector_private, napi); 1163 int work_done = 0; 1164 int err; 1165 bool tx_enqueued = false; 1166 1167 if ((vp->options & VECTOR_TX) != 0) 1168 tx_enqueued = (vector_send(vp->tx_queue) > 0); 1169 spin_lock(&vp->rx_queue->head_lock); 1170 if ((vp->options & VECTOR_RX) > 0) 1171 err = vector_mmsg_rx(vp, budget); 1172 else { 1173 err = vector_legacy_rx(vp); 1174 if (err > 0) 1175 err = 1; 1176 } 1177 spin_unlock(&vp->rx_queue->head_lock); 1178 if (err > 0) 1179 work_done += err; 1180 1181 if (tx_enqueued || err > 0) 1182 napi_schedule(napi); 1183 if (work_done <= budget) 1184 napi_complete_done(napi, work_done); 1185 return work_done; 1186 } 1187 1188 static void vector_reset_tx(struct work_struct *work) 1189 { 1190 struct vector_private *vp = 1191 container_of(work, struct vector_private, reset_tx); 1192 netdev_reset_queue(vp->dev); 1193 netif_start_queue(vp->dev); 1194 netif_wake_queue(vp->dev); 1195 } 1196 1197 static int vector_net_open(struct net_device *dev) 1198 { 1199 struct vector_private *vp = netdev_priv(dev); 1200 int err = -EINVAL; 1201 struct vector_device *vdevice; 1202 1203 if (vp->opened) 1204 return -ENXIO; 1205 vp->opened = true; 1206 1207 vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed)); 1208 1209 vp->fds = uml_vector_user_open(vp->unit, vp->parsed); 1210 1211 if (vp->fds == NULL) 1212 goto out_close; 1213 1214 if (build_transport_data(vp) < 0) 1215 goto out_close; 1216 1217 if ((vp->options & VECTOR_RX) > 0) { 1218 vp->rx_queue = create_queue( 1219 vp, 1220 get_depth(vp->parsed), 1221 vp->rx_header_size, 1222 MAX_IOV_SIZE 1223 ); 1224 atomic_set(&vp->rx_queue->queue_depth, get_depth(vp->parsed)); 1225 } else { 1226 vp->header_rxbuffer = kmalloc( 1227 vp->rx_header_size, 1228 GFP_KERNEL 1229 ); 1230 if (vp->header_rxbuffer == NULL) 1231 goto out_close; 1232 } 1233 if ((vp->options & VECTOR_TX) > 0) { 1234 vp->tx_queue = create_queue( 1235 vp, 1236 get_depth(vp->parsed), 1237 vp->header_size, 1238 MAX_IOV_SIZE 1239 ); 1240 } else { 1241 vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL); 1242 if (vp->header_txbuffer == NULL) 1243 goto out_close; 1244 } 1245 1246 netif_napi_add_weight(vp->dev, &vp->napi, vector_poll, 1247 get_depth(vp->parsed)); 1248 napi_enable(&vp->napi); 1249 1250 /* READ IRQ */ 1251 err = um_request_irq( 1252 irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd, 1253 IRQ_READ, vector_rx_interrupt, 1254 IRQF_SHARED, dev->name, dev); 1255 if (err < 0) { 1256 netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err); 1257 err = -ENETUNREACH; 1258 goto out_close; 1259 } 1260 vp->rx_irq = irq_rr + VECTOR_BASE_IRQ; 1261 dev->irq = irq_rr + VECTOR_BASE_IRQ; 1262 irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE; 1263 1264 /* WRITE IRQ - we need it only if we have vector TX */ 1265 if ((vp->options & VECTOR_TX) > 0) { 1266 err = um_request_irq( 1267 irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd, 1268 IRQ_WRITE, vector_tx_interrupt, 1269 IRQF_SHARED, dev->name, dev); 1270 if (err < 0) { 1271 netdev_err(dev, 1272 "vector_open: failed to get tx irq(%d)\n", err); 1273 err = -ENETUNREACH; 1274 goto out_close; 1275 } 1276 vp->tx_irq = irq_rr + VECTOR_BASE_IRQ; 1277 irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE; 1278 } 1279 1280 if ((vp->options & VECTOR_QDISC_BYPASS) != 0) { 1281 if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd)) 1282 vp->options |= VECTOR_BPF; 1283 } 1284 if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL)) 1285 vp->bpf = uml_vector_default_bpf(dev->dev_addr); 1286 1287 if (vp->bpf != NULL) 1288 uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf); 1289 1290 netif_start_queue(dev); 1291 vector_reset_stats(vp); 1292 1293 /* clear buffer - it can happen that the host side of the interface 1294 * is full when we get here. In this case, new data is never queued, 1295 * SIGIOs never arrive, and the net never works. 1296 */ 1297 1298 napi_schedule(&vp->napi); 1299 1300 vdevice = find_device(vp->unit); 1301 vdevice->opened = 1; 1302 1303 if ((vp->options & VECTOR_TX) != 0) 1304 add_timer(&vp->tl); 1305 return 0; 1306 out_close: 1307 vector_net_close(dev); 1308 return err; 1309 } 1310 1311 1312 static void vector_net_set_multicast_list(struct net_device *dev) 1313 { 1314 /* TODO: - we can do some BPF games here */ 1315 return; 1316 } 1317 1318 static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue) 1319 { 1320 struct vector_private *vp = netdev_priv(dev); 1321 1322 vp->estats.tx_timeout_count++; 1323 netif_trans_update(dev); 1324 schedule_work(&vp->reset_tx); 1325 } 1326 1327 static netdev_features_t vector_fix_features(struct net_device *dev, 1328 netdev_features_t features) 1329 { 1330 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); 1331 return features; 1332 } 1333 1334 static int vector_set_features(struct net_device *dev, 1335 netdev_features_t features) 1336 { 1337 struct vector_private *vp = netdev_priv(dev); 1338 /* Adjust buffer sizes for GSO/GRO. Unfortunately, there is 1339 * no way to negotiate it on raw sockets, so we can change 1340 * only our side. 1341 */ 1342 if (features & NETIF_F_GRO) 1343 /* All new frame buffers will be GRO-sized */ 1344 vp->req_size = 65536; 1345 else 1346 /* All new frame buffers will be normal sized */ 1347 vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN; 1348 return 0; 1349 } 1350 1351 #ifdef CONFIG_NET_POLL_CONTROLLER 1352 static void vector_net_poll_controller(struct net_device *dev) 1353 { 1354 disable_irq(dev->irq); 1355 vector_rx_interrupt(dev->irq, dev); 1356 enable_irq(dev->irq); 1357 } 1358 #endif 1359 1360 static void vector_net_get_drvinfo(struct net_device *dev, 1361 struct ethtool_drvinfo *info) 1362 { 1363 strscpy(info->driver, DRIVER_NAME); 1364 } 1365 1366 static int vector_net_load_bpf_flash(struct net_device *dev, 1367 struct ethtool_flash *efl) 1368 { 1369 struct vector_private *vp = netdev_priv(dev); 1370 struct vector_device *vdevice; 1371 const struct firmware *fw; 1372 int result = 0; 1373 1374 if (!(vp->options & VECTOR_BPF_FLASH)) { 1375 netdev_err(dev, "loading firmware not permitted: %s\n", efl->data); 1376 return -1; 1377 } 1378 1379 if (vp->bpf != NULL) { 1380 if (vp->opened) 1381 uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf); 1382 kfree(vp->bpf->filter); 1383 vp->bpf->filter = NULL; 1384 } else { 1385 vp->bpf = kmalloc_obj(struct sock_fprog, GFP_ATOMIC); 1386 if (vp->bpf == NULL) { 1387 netdev_err(dev, "failed to allocate memory for firmware\n"); 1388 goto flash_fail; 1389 } 1390 } 1391 1392 vdevice = find_device(vp->unit); 1393 1394 if (request_firmware(&fw, efl->data, &vdevice->pdev.dev)) 1395 goto flash_fail; 1396 1397 vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC); 1398 if (!vp->bpf->filter) 1399 goto free_buffer; 1400 1401 vp->bpf->len = fw->size / sizeof(struct sock_filter); 1402 release_firmware(fw); 1403 1404 if (vp->opened) 1405 result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf); 1406 1407 return result; 1408 1409 free_buffer: 1410 release_firmware(fw); 1411 1412 flash_fail: 1413 if (vp->bpf != NULL) 1414 kfree(vp->bpf->filter); 1415 kfree(vp->bpf); 1416 vp->bpf = NULL; 1417 return -1; 1418 } 1419 1420 static void vector_get_ringparam(struct net_device *netdev, 1421 struct ethtool_ringparam *ring, 1422 struct kernel_ethtool_ringparam *kernel_ring, 1423 struct netlink_ext_ack *extack) 1424 { 1425 struct vector_private *vp = netdev_priv(netdev); 1426 1427 ring->rx_max_pending = vp->rx_queue->max_depth; 1428 ring->tx_max_pending = vp->tx_queue->max_depth; 1429 ring->rx_pending = vp->rx_queue->max_depth; 1430 ring->tx_pending = vp->tx_queue->max_depth; 1431 } 1432 1433 static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf) 1434 { 1435 switch (stringset) { 1436 case ETH_SS_TEST: 1437 *buf = '\0'; 1438 break; 1439 case ETH_SS_STATS: 1440 memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys)); 1441 break; 1442 default: 1443 WARN_ON(1); 1444 break; 1445 } 1446 } 1447 1448 static int vector_get_sset_count(struct net_device *dev, int sset) 1449 { 1450 switch (sset) { 1451 case ETH_SS_TEST: 1452 return 0; 1453 case ETH_SS_STATS: 1454 return VECTOR_NUM_STATS; 1455 default: 1456 return -EOPNOTSUPP; 1457 } 1458 } 1459 1460 static void vector_get_ethtool_stats(struct net_device *dev, 1461 struct ethtool_stats *estats, 1462 u64 *tmp_stats) 1463 { 1464 struct vector_private *vp = netdev_priv(dev); 1465 1466 /* Stats are modified in the dequeue portions of 1467 * rx/tx which are protected by the head locks 1468 * grabbing these locks here ensures they are up 1469 * to date. 1470 */ 1471 1472 spin_lock(&vp->tx_queue->head_lock); 1473 spin_lock(&vp->rx_queue->head_lock); 1474 memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats)); 1475 spin_unlock(&vp->rx_queue->head_lock); 1476 spin_unlock(&vp->tx_queue->head_lock); 1477 } 1478 1479 static int vector_get_coalesce(struct net_device *netdev, 1480 struct ethtool_coalesce *ec, 1481 struct kernel_ethtool_coalesce *kernel_coal, 1482 struct netlink_ext_ack *extack) 1483 { 1484 struct vector_private *vp = netdev_priv(netdev); 1485 1486 ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ; 1487 return 0; 1488 } 1489 1490 static int vector_set_coalesce(struct net_device *netdev, 1491 struct ethtool_coalesce *ec, 1492 struct kernel_ethtool_coalesce *kernel_coal, 1493 struct netlink_ext_ack *extack) 1494 { 1495 struct vector_private *vp = netdev_priv(netdev); 1496 1497 vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000; 1498 if (vp->coalesce == 0) 1499 vp->coalesce = 1; 1500 return 0; 1501 } 1502 1503 static const struct ethtool_ops vector_net_ethtool_ops = { 1504 .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS, 1505 .get_drvinfo = vector_net_get_drvinfo, 1506 .get_link = ethtool_op_get_link, 1507 .get_ts_info = ethtool_op_get_ts_info, 1508 .get_ringparam = vector_get_ringparam, 1509 .get_strings = vector_get_strings, 1510 .get_sset_count = vector_get_sset_count, 1511 .get_ethtool_stats = vector_get_ethtool_stats, 1512 .get_coalesce = vector_get_coalesce, 1513 .set_coalesce = vector_set_coalesce, 1514 .flash_device = vector_net_load_bpf_flash, 1515 }; 1516 1517 1518 static const struct net_device_ops vector_netdev_ops = { 1519 .ndo_open = vector_net_open, 1520 .ndo_stop = vector_net_close, 1521 .ndo_start_xmit = vector_net_start_xmit, 1522 .ndo_set_rx_mode = vector_net_set_multicast_list, 1523 .ndo_tx_timeout = vector_net_tx_timeout, 1524 .ndo_set_mac_address = eth_mac_addr, 1525 .ndo_validate_addr = eth_validate_addr, 1526 .ndo_fix_features = vector_fix_features, 1527 .ndo_set_features = vector_set_features, 1528 #ifdef CONFIG_NET_POLL_CONTROLLER 1529 .ndo_poll_controller = vector_net_poll_controller, 1530 #endif 1531 }; 1532 1533 static void vector_timer_expire(struct timer_list *t) 1534 { 1535 struct vector_private *vp = timer_container_of(vp, t, tl); 1536 1537 vp->estats.tx_kicks++; 1538 napi_schedule(&vp->napi); 1539 } 1540 1541 static void vector_setup_etheraddr(struct net_device *dev, char *str) 1542 { 1543 u8 addr[ETH_ALEN]; 1544 1545 if (str == NULL) 1546 goto random; 1547 1548 if (!mac_pton(str, addr)) { 1549 netdev_err(dev, 1550 "Failed to parse '%s' as an ethernet address\n", str); 1551 goto random; 1552 } 1553 if (is_multicast_ether_addr(addr)) { 1554 netdev_err(dev, 1555 "Attempt to assign a multicast ethernet address to a device disallowed\n"); 1556 goto random; 1557 } 1558 if (!is_valid_ether_addr(addr)) { 1559 netdev_err(dev, 1560 "Attempt to assign an invalid ethernet address to a device disallowed\n"); 1561 goto random; 1562 } 1563 if (!is_local_ether_addr(addr)) { 1564 netdev_warn(dev, "Warning: Assigning a globally valid ethernet address to a device\n"); 1565 netdev_warn(dev, "You should set the 2nd rightmost bit in the first byte of the MAC,\n"); 1566 netdev_warn(dev, "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n", 1567 addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4], addr[5]); 1568 } 1569 eth_hw_addr_set(dev, addr); 1570 return; 1571 1572 random: 1573 netdev_info(dev, "Choosing a random ethernet address\n"); 1574 eth_hw_addr_random(dev); 1575 } 1576 1577 static void vector_eth_configure( 1578 int n, 1579 struct arglist *def 1580 ) 1581 { 1582 struct vector_device *device; 1583 struct net_device *dev; 1584 struct vector_private *vp; 1585 int err; 1586 1587 device = kzalloc_obj(*device); 1588 if (device == NULL) { 1589 pr_err("Failed to allocate struct vector_device for vec%d\n", n); 1590 return; 1591 } 1592 dev = alloc_etherdev(sizeof(struct vector_private)); 1593 if (dev == NULL) { 1594 pr_err("Failed to allocate struct net_device for vec%d\n", n); 1595 goto out_free_device; 1596 } 1597 1598 dev->mtu = get_mtu(def); 1599 1600 INIT_LIST_HEAD(&device->list); 1601 device->unit = n; 1602 1603 /* If this name ends up conflicting with an existing registered 1604 * netdevice, that is OK, register_netdev{,ice}() will notice this 1605 * and fail. 1606 */ 1607 snprintf(dev->name, sizeof(dev->name), "vec%d", n); 1608 vector_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac")); 1609 vp = netdev_priv(dev); 1610 1611 /* sysfs register */ 1612 if (!driver_registered) { 1613 platform_driver_register(¨_net_driver); 1614 driver_registered = 1; 1615 } 1616 device->pdev.id = n; 1617 device->pdev.name = DRIVER_NAME; 1618 device->pdev.dev.release = vector_device_release; 1619 dev_set_drvdata(&device->pdev.dev, device); 1620 if (platform_device_register(&device->pdev)) 1621 goto out_free_netdev; 1622 SET_NETDEV_DEV(dev, &device->pdev.dev); 1623 1624 device->dev = dev; 1625 1626 INIT_LIST_HEAD(&vp->list); 1627 vp->dev = dev; 1628 vp->unit = n; 1629 vp->options = get_transport_options(def); 1630 vp->parsed = def; 1631 vp->max_packet = get_mtu(def) + ETH_HEADER_OTHER; 1632 /* 1633 * TODO - we need to calculate headroom so that ip header 1634 * is 16 byte aligned all the time 1635 */ 1636 vp->headroom = get_headroom(def); 1637 vp->coalesce = 2; 1638 vp->req_size = get_req_size(def); 1639 1640 dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST); 1641 INIT_WORK(&vp->reset_tx, vector_reset_tx); 1642 1643 timer_setup(&vp->tl, vector_timer_expire, 0); 1644 1645 /* FIXME */ 1646 dev->netdev_ops = &vector_netdev_ops; 1647 dev->ethtool_ops = &vector_net_ethtool_ops; 1648 dev->watchdog_timeo = (HZ >> 1); 1649 /* primary IRQ - fixme */ 1650 dev->irq = 0; /* we will adjust this once opened */ 1651 1652 rtnl_lock(); 1653 err = register_netdevice(dev); 1654 rtnl_unlock(); 1655 if (err) 1656 goto out_undo_user_init; 1657 1658 spin_lock(&vector_devices_lock); 1659 list_add(&device->list, &vector_devices); 1660 spin_unlock(&vector_devices_lock); 1661 1662 return; 1663 1664 out_undo_user_init: 1665 return; 1666 out_free_netdev: 1667 free_netdev(dev); 1668 out_free_device: 1669 kfree(device); 1670 } 1671 1672 1673 1674 1675 /* 1676 * Invoked late in the init 1677 */ 1678 1679 static int __init vector_init(void) 1680 { 1681 struct list_head *ele; 1682 struct vector_cmd_line_arg *def; 1683 struct arglist *parsed; 1684 1685 list_for_each(ele, &vec_cmd_line) { 1686 def = list_entry(ele, struct vector_cmd_line_arg, list); 1687 parsed = uml_parse_vector_ifspec(def->arguments); 1688 if (parsed != NULL) 1689 vector_eth_configure(def->unit, parsed); 1690 } 1691 return 0; 1692 } 1693 1694 1695 /* Invoked at initial argument parsing, only stores 1696 * arguments until a proper vector_init is called 1697 * later 1698 */ 1699 1700 static int __init vector_setup(char *str) 1701 { 1702 char *error; 1703 int n, err; 1704 struct vector_cmd_line_arg *new; 1705 1706 err = vector_parse(str, &n, &str, &error); 1707 if (err) { 1708 pr_err("Couldn't parse '%s': %s\n", str, error); 1709 return 1; 1710 } 1711 new = memblock_alloc_or_panic(sizeof(*new), SMP_CACHE_BYTES); 1712 INIT_LIST_HEAD(&new->list); 1713 new->unit = n; 1714 new->arguments = str; 1715 list_add_tail(&new->list, &vec_cmd_line); 1716 return 1; 1717 } 1718 1719 __setup("vec", vector_setup); 1720 __uml_help(vector_setup, 1721 "vec[0-9]+:<option>=<value>,<option>=<value>\n" 1722 " Configure a vector io network device.\n\n" 1723 ); 1724 1725 late_initcall(vector_init); 1726 1727 static struct mc_device vector_mc = { 1728 .list = LIST_HEAD_INIT(vector_mc.list), 1729 .name = "vec", 1730 .config = vector_config, 1731 .get_config = NULL, 1732 .id = vector_id, 1733 .remove = vector_remove, 1734 }; 1735 1736 #ifdef CONFIG_INET 1737 static int vector_inetaddr_event( 1738 struct notifier_block *this, 1739 unsigned long event, 1740 void *ptr) 1741 { 1742 return NOTIFY_DONE; 1743 } 1744 1745 static struct notifier_block vector_inetaddr_notifier = { 1746 .notifier_call = vector_inetaddr_event, 1747 }; 1748 1749 static void inet_register(void) 1750 { 1751 register_inetaddr_notifier(&vector_inetaddr_notifier); 1752 } 1753 #else 1754 static inline void inet_register(void) 1755 { 1756 } 1757 #endif 1758 1759 static int vector_net_init(void) 1760 { 1761 mconsole_register_dev(&vector_mc); 1762 inet_register(); 1763 return 0; 1764 } 1765 1766 __initcall(vector_net_init); 1767 1768 1769 1770