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