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 #include <linux/memblock.h> 12 #include <linux/etherdevice.h> 13 #include <linux/ethtool.h> 14 #include <linux/inetdevice.h> 15 #include <linux/init.h> 16 #include <linux/list.h> 17 #include <linux/netdevice.h> 18 #include <linux/platform_device.h> 19 #include <linux/rtnetlink.h> 20 #include <linux/skbuff.h> 21 #include <linux/slab.h> 22 #include <linux/interrupt.h> 23 #include <linux/firmware.h> 24 #include <linux/fs.h> 25 #include <asm/atomic.h> 26 #include <uapi/linux/filter.h> 27 #include <init.h> 28 #include <irq_kern.h> 29 #include <irq_user.h> 30 #include <net_kern.h> 31 #include <os.h> 32 #include "mconsole_kern.h" 33 #include "vector_user.h" 34 #include "vector_kern.h" 35 36 /* 37 * Adapted from network devices with the following major changes: 38 * All transports are static - simplifies the code significantly 39 * Multiple FDs/IRQs per device 40 * Vector IO optionally used for read/write, falling back to legacy 41 * based on configuration and/or availability 42 * Configuration is no longer positional - L2TPv3 and GRE require up to 43 * 10 parameters, passing this as positional is not fit for purpose. 44 * Only socket transports are supported 45 */ 46 47 48 #define DRIVER_NAME "uml-vector" 49 struct vector_cmd_line_arg { 50 struct list_head list; 51 int unit; 52 char *arguments; 53 }; 54 55 struct vector_device { 56 struct list_head list; 57 struct net_device *dev; 58 struct platform_device pdev; 59 int unit; 60 int opened; 61 }; 62 63 static LIST_HEAD(vec_cmd_line); 64 65 static DEFINE_SPINLOCK(vector_devices_lock); 66 static LIST_HEAD(vector_devices); 67 68 static int driver_registered; 69 70 static void vector_eth_configure(int n, struct arglist *def); 71 static int vector_mmsg_rx(struct vector_private *vp, int budget); 72 73 /* Argument accessors to set variables (and/or set default values) 74 * mtu, buffer sizing, default headroom, etc 75 */ 76 77 #define DEFAULT_HEADROOM 2 78 #define SAFETY_MARGIN 32 79 #define DEFAULT_VECTOR_SIZE 64 80 #define TX_SMALL_PACKET 128 81 #define MAX_IOV_SIZE (MAX_SKB_FRAGS + 1) 82 83 static const struct { 84 const char string[ETH_GSTRING_LEN]; 85 } ethtool_stats_keys[] = { 86 { "rx_queue_max" }, 87 { "rx_queue_running_average" }, 88 { "tx_queue_max" }, 89 { "tx_queue_running_average" }, 90 { "rx_encaps_errors" }, 91 { "tx_timeout_count" }, 92 { "tx_restart_queue" }, 93 { "tx_kicks" }, 94 { "tx_flow_control_xon" }, 95 { "tx_flow_control_xoff" }, 96 { "rx_csum_offload_good" }, 97 { "rx_csum_offload_errors"}, 98 { "sg_ok"}, 99 { "sg_linearized"}, 100 }; 101 102 #define VECTOR_NUM_STATS ARRAY_SIZE(ethtool_stats_keys) 103 104 static void vector_reset_stats(struct vector_private *vp) 105 { 106 /* We reuse the existing queue locks for stats */ 107 108 /* RX stats are modified with RX head_lock held 109 * in vector_poll. 110 */ 111 112 spin_lock(&vp->rx_queue->head_lock); 113 vp->estats.rx_queue_max = 0; 114 vp->estats.rx_queue_running_average = 0; 115 vp->estats.rx_encaps_errors = 0; 116 vp->estats.sg_ok = 0; 117 vp->estats.sg_linearized = 0; 118 spin_unlock(&vp->rx_queue->head_lock); 119 120 /* TX stats are modified with TX head_lock held 121 * in vector_send. 122 */ 123 124 spin_lock(&vp->tx_queue->head_lock); 125 vp->estats.tx_timeout_count = 0; 126 vp->estats.tx_restart_queue = 0; 127 vp->estats.tx_kicks = 0; 128 vp->estats.tx_flow_control_xon = 0; 129 vp->estats.tx_flow_control_xoff = 0; 130 vp->estats.tx_queue_max = 0; 131 vp->estats.tx_queue_running_average = 0; 132 spin_unlock(&vp->tx_queue->head_lock); 133 } 134 135 static int get_mtu(struct arglist *def) 136 { 137 char *mtu = uml_vector_fetch_arg(def, "mtu"); 138 long result; 139 140 if (mtu != NULL) { 141 if (kstrtoul(mtu, 10, &result) == 0) 142 if ((result < (1 << 16) - 1) && (result >= 576)) 143 return result; 144 } 145 return ETH_MAX_PACKET; 146 } 147 148 static char *get_bpf_file(struct arglist *def) 149 { 150 return uml_vector_fetch_arg(def, "bpffile"); 151 } 152 153 static bool get_bpf_flash(struct arglist *def) 154 { 155 char *allow = uml_vector_fetch_arg(def, "bpfflash"); 156 long result; 157 158 if (allow != NULL) { 159 if (kstrtoul(allow, 10, &result) == 0) 160 return result > 0; 161 } 162 return false; 163 } 164 165 static int get_depth(struct arglist *def) 166 { 167 char *mtu = uml_vector_fetch_arg(def, "depth"); 168 long result; 169 170 if (mtu != NULL) { 171 if (kstrtoul(mtu, 10, &result) == 0) 172 return result; 173 } 174 return DEFAULT_VECTOR_SIZE; 175 } 176 177 static int get_headroom(struct arglist *def) 178 { 179 char *mtu = uml_vector_fetch_arg(def, "headroom"); 180 long result; 181 182 if (mtu != NULL) { 183 if (kstrtoul(mtu, 10, &result) == 0) 184 return result; 185 } 186 return DEFAULT_HEADROOM; 187 } 188 189 static int get_req_size(struct arglist *def) 190 { 191 char *gro = uml_vector_fetch_arg(def, "gro"); 192 long result; 193 194 if (gro != NULL) { 195 if (kstrtoul(gro, 10, &result) == 0) { 196 if (result > 0) 197 return 65536; 198 } 199 } 200 return get_mtu(def) + ETH_HEADER_OTHER + 201 get_headroom(def) + SAFETY_MARGIN; 202 } 203 204 205 static int get_transport_options(struct arglist *def) 206 { 207 char *transport = uml_vector_fetch_arg(def, "transport"); 208 char *vector = uml_vector_fetch_arg(def, "vec"); 209 210 int vec_rx = VECTOR_RX; 211 int vec_tx = VECTOR_TX; 212 long parsed; 213 int result = 0; 214 215 if (transport == NULL) 216 return -EINVAL; 217 218 if (vector != NULL) { 219 if (kstrtoul(vector, 10, &parsed) == 0) { 220 if (parsed == 0) { 221 vec_rx = 0; 222 vec_tx = 0; 223 } 224 } 225 } 226 227 if (get_bpf_flash(def)) 228 result = VECTOR_BPF_FLASH; 229 230 if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0) 231 return result; 232 if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0) 233 return (result | vec_rx | VECTOR_BPF); 234 if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0) 235 return (result | vec_rx | vec_tx | VECTOR_QDISC_BYPASS); 236 return (result | vec_rx | vec_tx); 237 } 238 239 240 /* A mini-buffer for packet drop read 241 * All of our supported transports are datagram oriented and we always 242 * read using recvmsg or recvmmsg. If we pass a buffer which is smaller 243 * than the packet size it still counts as full packet read and will 244 * clean the incoming stream to keep sigio/epoll happy 245 */ 246 247 #define DROP_BUFFER_SIZE 32 248 249 static char *drop_buffer; 250 251 252 /* 253 * Advance the mmsg queue head by n = advance. Resets the queue to 254 * maximum enqueue/dequeue-at-once capacity if possible. Called by 255 * dequeuers. Caller must hold the head_lock! 256 */ 257 258 static int vector_advancehead(struct vector_queue *qi, int advance) 259 { 260 qi->head = 261 (qi->head + advance) 262 % qi->max_depth; 263 264 265 atomic_sub(advance, &qi->queue_depth); 266 return atomic_read(&qi->queue_depth); 267 } 268 269 /* Advance the queue tail by n = advance. 270 * This is called by enqueuers which should hold the 271 * head lock already 272 */ 273 274 static int vector_advancetail(struct vector_queue *qi, int advance) 275 { 276 qi->tail = 277 (qi->tail + advance) 278 % qi->max_depth; 279 atomic_add(advance, &qi->queue_depth); 280 return atomic_read(&qi->queue_depth); 281 } 282 283 static int prep_msg(struct vector_private *vp, 284 struct sk_buff *skb, 285 struct iovec *iov) 286 { 287 int iov_index = 0; 288 int nr_frags, frag; 289 skb_frag_t *skb_frag; 290 291 nr_frags = skb_shinfo(skb)->nr_frags; 292 if (nr_frags > MAX_IOV_SIZE) { 293 if (skb_linearize(skb) != 0) 294 goto drop; 295 } 296 if (vp->header_size > 0) { 297 iov[iov_index].iov_len = vp->header_size; 298 vp->form_header(iov[iov_index].iov_base, skb, vp); 299 iov_index++; 300 } 301 iov[iov_index].iov_base = skb->data; 302 if (nr_frags > 0) { 303 iov[iov_index].iov_len = skb->len - skb->data_len; 304 vp->estats.sg_ok++; 305 } else 306 iov[iov_index].iov_len = skb->len; 307 iov_index++; 308 for (frag = 0; frag < nr_frags; frag++) { 309 skb_frag = &skb_shinfo(skb)->frags[frag]; 310 iov[iov_index].iov_base = skb_frag_address_safe(skb_frag); 311 iov[iov_index].iov_len = skb_frag_size(skb_frag); 312 iov_index++; 313 } 314 return iov_index; 315 drop: 316 return -1; 317 } 318 /* 319 * Generic vector enqueue with support for forming headers using transport 320 * specific callback. Allows GRE, L2TPv3, RAW and other transports 321 * to use a common enqueue procedure in vector mode 322 */ 323 324 static int vector_enqueue(struct vector_queue *qi, struct sk_buff *skb) 325 { 326 struct vector_private *vp = netdev_priv(qi->dev); 327 int queue_depth; 328 int packet_len; 329 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 330 int iov_count; 331 332 spin_lock(&qi->tail_lock); 333 queue_depth = atomic_read(&qi->queue_depth); 334 335 if (skb) 336 packet_len = skb->len; 337 338 if (queue_depth < qi->max_depth) { 339 340 *(qi->skbuff_vector + qi->tail) = skb; 341 mmsg_vector += qi->tail; 342 iov_count = prep_msg( 343 vp, 344 skb, 345 mmsg_vector->msg_hdr.msg_iov 346 ); 347 if (iov_count < 1) 348 goto drop; 349 mmsg_vector->msg_hdr.msg_iovlen = iov_count; 350 mmsg_vector->msg_hdr.msg_name = vp->fds->remote_addr; 351 mmsg_vector->msg_hdr.msg_namelen = vp->fds->remote_addr_size; 352 wmb(); /* Make the packet visible to the NAPI poll thread */ 353 queue_depth = vector_advancetail(qi, 1); 354 } else 355 goto drop; 356 spin_unlock(&qi->tail_lock); 357 return queue_depth; 358 drop: 359 qi->dev->stats.tx_dropped++; 360 if (skb != NULL) { 361 packet_len = skb->len; 362 dev_consume_skb_any(skb); 363 netdev_completed_queue(qi->dev, 1, packet_len); 364 } 365 spin_unlock(&qi->tail_lock); 366 return queue_depth; 367 } 368 369 static int consume_vector_skbs(struct vector_queue *qi, int count) 370 { 371 struct sk_buff *skb; 372 int skb_index; 373 int bytes_compl = 0; 374 375 for (skb_index = qi->head; skb_index < qi->head + count; skb_index++) { 376 skb = *(qi->skbuff_vector + skb_index); 377 /* mark as empty to ensure correct destruction if 378 * needed 379 */ 380 bytes_compl += skb->len; 381 *(qi->skbuff_vector + skb_index) = NULL; 382 dev_consume_skb_any(skb); 383 } 384 qi->dev->stats.tx_bytes += bytes_compl; 385 qi->dev->stats.tx_packets += count; 386 netdev_completed_queue(qi->dev, count, bytes_compl); 387 return vector_advancehead(qi, count); 388 } 389 390 /* 391 * Generic vector dequeue via sendmmsg with support for forming headers 392 * using transport specific callback. Allows GRE, L2TPv3, RAW and 393 * other transports to use a common dequeue procedure in vector mode 394 */ 395 396 397 static int vector_send(struct vector_queue *qi) 398 { 399 struct vector_private *vp = netdev_priv(qi->dev); 400 struct mmsghdr *send_from; 401 int result = 0, send_len; 402 403 if (spin_trylock(&qi->head_lock)) { 404 /* update queue_depth to current value */ 405 while (atomic_read(&qi->queue_depth) > 0) { 406 /* Calculate the start of the vector */ 407 send_len = atomic_read(&qi->queue_depth); 408 send_from = qi->mmsg_vector; 409 send_from += qi->head; 410 /* Adjust vector size if wraparound */ 411 if (send_len + qi->head > qi->max_depth) 412 send_len = qi->max_depth - qi->head; 413 /* Try to TX as many packets as possible */ 414 if (send_len > 0) { 415 result = uml_vector_sendmmsg( 416 vp->fds->tx_fd, 417 send_from, 418 send_len, 419 0 420 ); 421 vp->in_write_poll = 422 (result != send_len); 423 } 424 /* For some of the sendmmsg error scenarios 425 * we may end being unsure in the TX success 426 * for all packets. It is safer to declare 427 * them all TX-ed and blame the network. 428 */ 429 if (result < 0) { 430 if (net_ratelimit()) 431 netdev_err(vp->dev, "sendmmsg err=%i\n", 432 result); 433 vp->in_error = true; 434 result = send_len; 435 } 436 if (result > 0) { 437 consume_vector_skbs(qi, result); 438 /* This is equivalent to an TX IRQ. 439 * Restart the upper layers to feed us 440 * more packets. 441 */ 442 if (result > vp->estats.tx_queue_max) 443 vp->estats.tx_queue_max = result; 444 vp->estats.tx_queue_running_average = 445 (vp->estats.tx_queue_running_average + result) >> 1; 446 } 447 netif_wake_queue(qi->dev); 448 /* if TX is busy, break out of the send loop, 449 * poll write IRQ will reschedule xmit for us. 450 */ 451 if (result != send_len) { 452 vp->estats.tx_restart_queue++; 453 break; 454 } 455 } 456 spin_unlock(&qi->head_lock); 457 } 458 return atomic_read(&qi->queue_depth); 459 } 460 461 /* Queue destructor. Deliberately stateless so we can use 462 * it in queue cleanup if initialization fails. 463 */ 464 465 static void destroy_queue(struct vector_queue *qi) 466 { 467 int i; 468 struct iovec *iov; 469 struct vector_private *vp = netdev_priv(qi->dev); 470 struct mmsghdr *mmsg_vector; 471 472 if (qi == NULL) 473 return; 474 /* deallocate any skbuffs - we rely on any unused to be 475 * set to NULL. 476 */ 477 if (qi->skbuff_vector != NULL) { 478 for (i = 0; i < qi->max_depth; i++) { 479 if (*(qi->skbuff_vector + i) != NULL) 480 dev_kfree_skb_any(*(qi->skbuff_vector + i)); 481 } 482 kfree(qi->skbuff_vector); 483 } 484 /* deallocate matching IOV structures including header buffs */ 485 if (qi->mmsg_vector != NULL) { 486 mmsg_vector = qi->mmsg_vector; 487 for (i = 0; i < qi->max_depth; i++) { 488 iov = mmsg_vector->msg_hdr.msg_iov; 489 if (iov != NULL) { 490 if ((vp->header_size > 0) && 491 (iov->iov_base != NULL)) 492 kfree(iov->iov_base); 493 kfree(iov); 494 } 495 mmsg_vector++; 496 } 497 kfree(qi->mmsg_vector); 498 } 499 kfree(qi); 500 } 501 502 /* 503 * Queue constructor. Create a queue with a given side. 504 */ 505 static struct vector_queue *create_queue( 506 struct vector_private *vp, 507 int max_size, 508 int header_size, 509 int num_extra_frags) 510 { 511 struct vector_queue *result; 512 int i; 513 struct iovec *iov; 514 struct mmsghdr *mmsg_vector; 515 516 result = kmalloc(sizeof(struct vector_queue), GFP_KERNEL); 517 if (result == NULL) 518 return NULL; 519 result->max_depth = max_size; 520 result->dev = vp->dev; 521 result->mmsg_vector = kmalloc( 522 (sizeof(struct mmsghdr) * max_size), GFP_KERNEL); 523 if (result->mmsg_vector == NULL) 524 goto out_mmsg_fail; 525 result->skbuff_vector = kmalloc( 526 (sizeof(void *) * max_size), GFP_KERNEL); 527 if (result->skbuff_vector == NULL) 528 goto out_skb_fail; 529 530 /* further failures can be handled safely by destroy_queue*/ 531 532 mmsg_vector = result->mmsg_vector; 533 for (i = 0; i < max_size; i++) { 534 /* Clear all pointers - we use non-NULL as marking on 535 * what to free on destruction 536 */ 537 *(result->skbuff_vector + i) = NULL; 538 mmsg_vector->msg_hdr.msg_iov = NULL; 539 mmsg_vector++; 540 } 541 mmsg_vector = result->mmsg_vector; 542 result->max_iov_frags = num_extra_frags; 543 for (i = 0; i < max_size; i++) { 544 if (vp->header_size > 0) 545 iov = kmalloc_array(3 + num_extra_frags, 546 sizeof(struct iovec), 547 GFP_KERNEL 548 ); 549 else 550 iov = kmalloc_array(2 + num_extra_frags, 551 sizeof(struct iovec), 552 GFP_KERNEL 553 ); 554 if (iov == NULL) 555 goto out_fail; 556 mmsg_vector->msg_hdr.msg_iov = iov; 557 mmsg_vector->msg_hdr.msg_iovlen = 1; 558 mmsg_vector->msg_hdr.msg_control = NULL; 559 mmsg_vector->msg_hdr.msg_controllen = 0; 560 mmsg_vector->msg_hdr.msg_flags = MSG_DONTWAIT; 561 mmsg_vector->msg_hdr.msg_name = NULL; 562 mmsg_vector->msg_hdr.msg_namelen = 0; 563 if (vp->header_size > 0) { 564 iov->iov_base = kmalloc(header_size, GFP_KERNEL); 565 if (iov->iov_base == NULL) 566 goto out_fail; 567 iov->iov_len = header_size; 568 mmsg_vector->msg_hdr.msg_iovlen = 2; 569 iov++; 570 } 571 iov->iov_base = NULL; 572 iov->iov_len = 0; 573 mmsg_vector++; 574 } 575 spin_lock_init(&result->head_lock); 576 spin_lock_init(&result->tail_lock); 577 atomic_set(&result->queue_depth, 0); 578 result->head = 0; 579 result->tail = 0; 580 return result; 581 out_skb_fail: 582 kfree(result->mmsg_vector); 583 out_mmsg_fail: 584 kfree(result); 585 return NULL; 586 out_fail: 587 destroy_queue(result); 588 return NULL; 589 } 590 591 /* 592 * We do not use the RX queue as a proper wraparound queue for now 593 * This is not necessary because the consumption via napi_gro_receive() 594 * happens in-line. While we can try using the return code of 595 * netif_rx() for flow control there are no drivers doing this today. 596 * For this RX specific use we ignore the tail/head locks and 597 * just read into a prepared queue filled with skbuffs. 598 */ 599 600 static struct sk_buff *prep_skb( 601 struct vector_private *vp, 602 struct user_msghdr *msg) 603 { 604 int linear = vp->max_packet + vp->headroom + SAFETY_MARGIN; 605 struct sk_buff *result; 606 int iov_index = 0, len; 607 struct iovec *iov = msg->msg_iov; 608 int err, nr_frags, frag; 609 skb_frag_t *skb_frag; 610 611 if (vp->req_size <= linear) 612 len = linear; 613 else 614 len = vp->req_size; 615 result = alloc_skb_with_frags( 616 linear, 617 len - vp->max_packet, 618 3, 619 &err, 620 GFP_ATOMIC 621 ); 622 if (vp->header_size > 0) 623 iov_index++; 624 if (result == NULL) { 625 iov[iov_index].iov_base = NULL; 626 iov[iov_index].iov_len = 0; 627 goto done; 628 } 629 skb_reserve(result, vp->headroom); 630 result->dev = vp->dev; 631 skb_put(result, vp->max_packet); 632 result->data_len = len - vp->max_packet; 633 result->len += len - vp->max_packet; 634 skb_reset_mac_header(result); 635 result->ip_summed = CHECKSUM_NONE; 636 iov[iov_index].iov_base = result->data; 637 iov[iov_index].iov_len = vp->max_packet; 638 iov_index++; 639 640 nr_frags = skb_shinfo(result)->nr_frags; 641 for (frag = 0; frag < nr_frags; frag++) { 642 skb_frag = &skb_shinfo(result)->frags[frag]; 643 iov[iov_index].iov_base = skb_frag_address_safe(skb_frag); 644 if (iov[iov_index].iov_base != NULL) 645 iov[iov_index].iov_len = skb_frag_size(skb_frag); 646 else 647 iov[iov_index].iov_len = 0; 648 iov_index++; 649 } 650 done: 651 msg->msg_iovlen = iov_index; 652 return result; 653 } 654 655 656 /* Prepare queue for recvmmsg one-shot rx - fill with fresh sk_buffs */ 657 658 static void prep_queue_for_rx(struct vector_queue *qi) 659 { 660 struct vector_private *vp = netdev_priv(qi->dev); 661 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 662 void **skbuff_vector = qi->skbuff_vector; 663 int i, queue_depth; 664 665 queue_depth = atomic_read(&qi->queue_depth); 666 667 if (queue_depth == 0) 668 return; 669 670 /* RX is always emptied 100% during each cycle, so we do not 671 * have to do the tail wraparound math for it. 672 */ 673 674 qi->head = qi->tail = 0; 675 676 for (i = 0; i < queue_depth; i++) { 677 /* it is OK if allocation fails - recvmmsg with NULL data in 678 * iov argument still performs an RX, just drops the packet 679 * This allows us stop faffing around with a "drop buffer" 680 */ 681 682 *skbuff_vector = prep_skb(vp, &mmsg_vector->msg_hdr); 683 skbuff_vector++; 684 mmsg_vector++; 685 } 686 atomic_set(&qi->queue_depth, 0); 687 } 688 689 static struct vector_device *find_device(int n) 690 { 691 struct vector_device *device; 692 struct list_head *ele; 693 694 spin_lock(&vector_devices_lock); 695 list_for_each(ele, &vector_devices) { 696 device = list_entry(ele, struct vector_device, list); 697 if (device->unit == n) 698 goto out; 699 } 700 device = NULL; 701 out: 702 spin_unlock(&vector_devices_lock); 703 return device; 704 } 705 706 static int vector_parse(char *str, int *index_out, char **str_out, 707 char **error_out) 708 { 709 int n, err; 710 char *start = str; 711 712 while ((*str != ':') && (strlen(str) > 1)) 713 str++; 714 if (*str != ':') { 715 *error_out = "Expected ':' after device number"; 716 return -EINVAL; 717 } 718 *str = '\0'; 719 720 err = kstrtouint(start, 0, &n); 721 if (err < 0) { 722 *error_out = "Bad device number"; 723 return err; 724 } 725 726 str++; 727 if (find_device(n)) { 728 *error_out = "Device already configured"; 729 return -EINVAL; 730 } 731 732 *index_out = n; 733 *str_out = str; 734 return 0; 735 } 736 737 static int vector_config(char *str, char **error_out) 738 { 739 int err, n; 740 char *params; 741 struct arglist *parsed; 742 743 err = vector_parse(str, &n, ¶ms, error_out); 744 if (err != 0) 745 return err; 746 747 /* This string is broken up and the pieces used by the underlying 748 * driver. We should copy it to make sure things do not go wrong 749 * later. 750 */ 751 752 params = kstrdup(params, GFP_KERNEL); 753 if (params == NULL) { 754 *error_out = "vector_config failed to strdup string"; 755 return -ENOMEM; 756 } 757 758 parsed = uml_parse_vector_ifspec(params); 759 760 if (parsed == NULL) { 761 *error_out = "vector_config failed to parse parameters"; 762 kfree(params); 763 return -EINVAL; 764 } 765 766 vector_eth_configure(n, parsed); 767 return 0; 768 } 769 770 static int vector_id(char **str, int *start_out, int *end_out) 771 { 772 char *end; 773 int n; 774 775 n = simple_strtoul(*str, &end, 0); 776 if ((*end != '\0') || (end == *str)) 777 return -1; 778 779 *start_out = n; 780 *end_out = n; 781 *str = end; 782 return n; 783 } 784 785 static int vector_remove(int n, char **error_out) 786 { 787 struct vector_device *vec_d; 788 struct net_device *dev; 789 struct vector_private *vp; 790 791 vec_d = find_device(n); 792 if (vec_d == NULL) 793 return -ENODEV; 794 dev = vec_d->dev; 795 vp = netdev_priv(dev); 796 if (vp->fds != NULL) 797 return -EBUSY; 798 unregister_netdev(dev); 799 platform_device_unregister(&vec_d->pdev); 800 return 0; 801 } 802 803 /* 804 * There is no shared per-transport initialization code, so 805 * we will just initialize each interface one by one and 806 * add them to a list 807 */ 808 809 static struct platform_driver uml_net_driver = { 810 .driver = { 811 .name = DRIVER_NAME, 812 }, 813 }; 814 815 816 static void vector_device_release(struct device *dev) 817 { 818 struct vector_device *device = dev_get_drvdata(dev); 819 struct net_device *netdev = device->dev; 820 821 list_del(&device->list); 822 kfree(device); 823 free_netdev(netdev); 824 } 825 826 /* Bog standard recv using recvmsg - not used normally unless the user 827 * explicitly specifies not to use recvmmsg vector RX. 828 */ 829 830 static int vector_legacy_rx(struct vector_private *vp) 831 { 832 int pkt_len; 833 struct user_msghdr hdr; 834 struct iovec iov[2 + MAX_IOV_SIZE]; /* header + data use case only */ 835 int iovpos = 0; 836 struct sk_buff *skb; 837 int header_check; 838 839 hdr.msg_name = NULL; 840 hdr.msg_namelen = 0; 841 hdr.msg_iov = (struct iovec *) &iov; 842 hdr.msg_control = NULL; 843 hdr.msg_controllen = 0; 844 hdr.msg_flags = 0; 845 846 if (vp->header_size > 0) { 847 iov[0].iov_base = vp->header_rxbuffer; 848 iov[0].iov_len = vp->header_size; 849 } 850 851 skb = prep_skb(vp, &hdr); 852 853 if (skb == NULL) { 854 /* Read a packet into drop_buffer and don't do 855 * anything with it. 856 */ 857 iov[iovpos].iov_base = drop_buffer; 858 iov[iovpos].iov_len = DROP_BUFFER_SIZE; 859 hdr.msg_iovlen = 1; 860 vp->dev->stats.rx_dropped++; 861 } 862 863 pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0); 864 if (pkt_len < 0) { 865 vp->in_error = true; 866 return pkt_len; 867 } 868 869 if (skb != NULL) { 870 if (pkt_len > vp->header_size) { 871 if (vp->header_size > 0) { 872 header_check = vp->verify_header( 873 vp->header_rxbuffer, skb, vp); 874 if (header_check < 0) { 875 dev_kfree_skb_irq(skb); 876 vp->dev->stats.rx_dropped++; 877 vp->estats.rx_encaps_errors++; 878 return 0; 879 } 880 if (header_check > 0) { 881 vp->estats.rx_csum_offload_good++; 882 skb->ip_summed = CHECKSUM_UNNECESSARY; 883 } 884 } 885 pskb_trim(skb, pkt_len - vp->rx_header_size); 886 skb->protocol = eth_type_trans(skb, skb->dev); 887 vp->dev->stats.rx_bytes += skb->len; 888 vp->dev->stats.rx_packets++; 889 napi_gro_receive(&vp->napi, skb); 890 } else { 891 dev_kfree_skb_irq(skb); 892 } 893 } 894 return pkt_len; 895 } 896 897 /* 898 * Packet at a time TX which falls back to vector TX if the 899 * underlying transport is busy. 900 */ 901 902 903 904 static int writev_tx(struct vector_private *vp, struct sk_buff *skb) 905 { 906 struct iovec iov[3 + MAX_IOV_SIZE]; 907 int iov_count, pkt_len = 0; 908 909 iov[0].iov_base = vp->header_txbuffer; 910 iov_count = prep_msg(vp, skb, (struct iovec *) &iov); 911 912 if (iov_count < 1) 913 goto drop; 914 915 pkt_len = uml_vector_writev( 916 vp->fds->tx_fd, 917 (struct iovec *) &iov, 918 iov_count 919 ); 920 921 if (pkt_len < 0) 922 goto drop; 923 924 netif_trans_update(vp->dev); 925 netif_wake_queue(vp->dev); 926 927 if (pkt_len > 0) { 928 vp->dev->stats.tx_bytes += skb->len; 929 vp->dev->stats.tx_packets++; 930 } else { 931 vp->dev->stats.tx_dropped++; 932 } 933 consume_skb(skb); 934 return pkt_len; 935 drop: 936 vp->dev->stats.tx_dropped++; 937 consume_skb(skb); 938 if (pkt_len < 0) 939 vp->in_error = true; 940 return pkt_len; 941 } 942 943 /* 944 * Receive as many messages as we can in one call using the special 945 * mmsg vector matched to an skb vector which we prepared earlier. 946 */ 947 948 static int vector_mmsg_rx(struct vector_private *vp, int budget) 949 { 950 int packet_count, i; 951 struct vector_queue *qi = vp->rx_queue; 952 struct sk_buff *skb; 953 struct mmsghdr *mmsg_vector = qi->mmsg_vector; 954 void **skbuff_vector = qi->skbuff_vector; 955 int header_check; 956 957 /* Refresh the vector and make sure it is with new skbs and the 958 * iovs are updated to point to them. 959 */ 960 961 prep_queue_for_rx(qi); 962 963 /* Fire the Lazy Gun - get as many packets as we can in one go. */ 964 965 if (budget > qi->max_depth) 966 budget = qi->max_depth; 967 968 packet_count = uml_vector_recvmmsg( 969 vp->fds->rx_fd, qi->mmsg_vector, budget, 0); 970 971 if (packet_count < 0) 972 vp->in_error = true; 973 974 if (packet_count <= 0) 975 return packet_count; 976 977 /* We treat packet processing as enqueue, buffer refresh as dequeue 978 * The queue_depth tells us how many buffers have been used and how 979 * many do we need to prep the next time prep_queue_for_rx() is called. 980 */ 981 982 atomic_add(packet_count, &qi->queue_depth); 983 984 for (i = 0; i < packet_count; i++) { 985 skb = (*skbuff_vector); 986 if (mmsg_vector->msg_len > vp->header_size) { 987 if (vp->header_size > 0) { 988 header_check = vp->verify_header( 989 mmsg_vector->msg_hdr.msg_iov->iov_base, 990 skb, 991 vp 992 ); 993 if (header_check < 0) { 994 /* Overlay header failed to verify - discard. 995 * We can actually keep this skb and reuse it, 996 * but that will make the prep logic too 997 * complex. 998 */ 999 dev_kfree_skb_irq(skb); 1000 vp->estats.rx_encaps_errors++; 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 del_timer(&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(sizeof(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 = from_timer(vp, t, tl); 1536 1537 vp->estats.tx_kicks++; 1538 napi_schedule(&vp->napi); 1539 } 1540 1541 1542 1543 static void vector_eth_configure( 1544 int n, 1545 struct arglist *def 1546 ) 1547 { 1548 struct vector_device *device; 1549 struct net_device *dev; 1550 struct vector_private *vp; 1551 int err; 1552 1553 device = kzalloc(sizeof(*device), GFP_KERNEL); 1554 if (device == NULL) { 1555 printk(KERN_ERR "eth_configure failed to allocate struct " 1556 "vector_device\n"); 1557 return; 1558 } 1559 dev = alloc_etherdev(sizeof(struct vector_private)); 1560 if (dev == NULL) { 1561 printk(KERN_ERR "eth_configure: failed to allocate struct " 1562 "net_device for vec%d\n", n); 1563 goto out_free_device; 1564 } 1565 1566 dev->mtu = get_mtu(def); 1567 1568 INIT_LIST_HEAD(&device->list); 1569 device->unit = n; 1570 1571 /* If this name ends up conflicting with an existing registered 1572 * netdevice, that is OK, register_netdev{,ice}() will notice this 1573 * and fail. 1574 */ 1575 snprintf(dev->name, sizeof(dev->name), "vec%d", n); 1576 uml_net_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac")); 1577 vp = netdev_priv(dev); 1578 1579 /* sysfs register */ 1580 if (!driver_registered) { 1581 platform_driver_register(¨_net_driver); 1582 driver_registered = 1; 1583 } 1584 device->pdev.id = n; 1585 device->pdev.name = DRIVER_NAME; 1586 device->pdev.dev.release = vector_device_release; 1587 dev_set_drvdata(&device->pdev.dev, device); 1588 if (platform_device_register(&device->pdev)) 1589 goto out_free_netdev; 1590 SET_NETDEV_DEV(dev, &device->pdev.dev); 1591 1592 device->dev = dev; 1593 1594 *vp = ((struct vector_private) 1595 { 1596 .list = LIST_HEAD_INIT(vp->list), 1597 .dev = dev, 1598 .unit = n, 1599 .options = get_transport_options(def), 1600 .rx_irq = 0, 1601 .tx_irq = 0, 1602 .parsed = def, 1603 .max_packet = get_mtu(def) + ETH_HEADER_OTHER, 1604 /* TODO - we need to calculate headroom so that ip header 1605 * is 16 byte aligned all the time 1606 */ 1607 .headroom = get_headroom(def), 1608 .form_header = NULL, 1609 .verify_header = NULL, 1610 .header_rxbuffer = NULL, 1611 .header_txbuffer = NULL, 1612 .header_size = 0, 1613 .rx_header_size = 0, 1614 .rexmit_scheduled = false, 1615 .opened = false, 1616 .transport_data = NULL, 1617 .in_write_poll = false, 1618 .coalesce = 2, 1619 .req_size = get_req_size(def), 1620 .in_error = false, 1621 .bpf = NULL 1622 }); 1623 1624 dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST); 1625 INIT_WORK(&vp->reset_tx, vector_reset_tx); 1626 1627 timer_setup(&vp->tl, vector_timer_expire, 0); 1628 1629 /* FIXME */ 1630 dev->netdev_ops = &vector_netdev_ops; 1631 dev->ethtool_ops = &vector_net_ethtool_ops; 1632 dev->watchdog_timeo = (HZ >> 1); 1633 /* primary IRQ - fixme */ 1634 dev->irq = 0; /* we will adjust this once opened */ 1635 1636 rtnl_lock(); 1637 err = register_netdevice(dev); 1638 rtnl_unlock(); 1639 if (err) 1640 goto out_undo_user_init; 1641 1642 spin_lock(&vector_devices_lock); 1643 list_add(&device->list, &vector_devices); 1644 spin_unlock(&vector_devices_lock); 1645 1646 return; 1647 1648 out_undo_user_init: 1649 return; 1650 out_free_netdev: 1651 free_netdev(dev); 1652 out_free_device: 1653 kfree(device); 1654 } 1655 1656 1657 1658 1659 /* 1660 * Invoked late in the init 1661 */ 1662 1663 static int __init vector_init(void) 1664 { 1665 struct list_head *ele; 1666 struct vector_cmd_line_arg *def; 1667 struct arglist *parsed; 1668 1669 list_for_each(ele, &vec_cmd_line) { 1670 def = list_entry(ele, struct vector_cmd_line_arg, list); 1671 parsed = uml_parse_vector_ifspec(def->arguments); 1672 if (parsed != NULL) 1673 vector_eth_configure(def->unit, parsed); 1674 } 1675 return 0; 1676 } 1677 1678 1679 /* Invoked at initial argument parsing, only stores 1680 * arguments until a proper vector_init is called 1681 * later 1682 */ 1683 1684 static int __init vector_setup(char *str) 1685 { 1686 char *error; 1687 int n, err; 1688 struct vector_cmd_line_arg *new; 1689 1690 err = vector_parse(str, &n, &str, &error); 1691 if (err) { 1692 printk(KERN_ERR "vector_setup - Couldn't parse '%s' : %s\n", 1693 str, error); 1694 return 1; 1695 } 1696 new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES); 1697 if (!new) 1698 panic("%s: Failed to allocate %zu bytes\n", __func__, 1699 sizeof(*new)); 1700 INIT_LIST_HEAD(&new->list); 1701 new->unit = n; 1702 new->arguments = str; 1703 list_add_tail(&new->list, &vec_cmd_line); 1704 return 1; 1705 } 1706 1707 __setup("vec", vector_setup); 1708 __uml_help(vector_setup, 1709 "vec[0-9]+:<option>=<value>,<option>=<value>\n" 1710 " Configure a vector io network device.\n\n" 1711 ); 1712 1713 late_initcall(vector_init); 1714 1715 static struct mc_device vector_mc = { 1716 .list = LIST_HEAD_INIT(vector_mc.list), 1717 .name = "vec", 1718 .config = vector_config, 1719 .get_config = NULL, 1720 .id = vector_id, 1721 .remove = vector_remove, 1722 }; 1723 1724 #ifdef CONFIG_INET 1725 static int vector_inetaddr_event( 1726 struct notifier_block *this, 1727 unsigned long event, 1728 void *ptr) 1729 { 1730 return NOTIFY_DONE; 1731 } 1732 1733 static struct notifier_block vector_inetaddr_notifier = { 1734 .notifier_call = vector_inetaddr_event, 1735 }; 1736 1737 static void inet_register(void) 1738 { 1739 register_inetaddr_notifier(&vector_inetaddr_notifier); 1740 } 1741 #else 1742 static inline void inet_register(void) 1743 { 1744 } 1745 #endif 1746 1747 static int vector_net_init(void) 1748 { 1749 mconsole_register_dev(&vector_mc); 1750 inet_register(); 1751 return 0; 1752 } 1753 1754 __initcall(vector_net_init); 1755 1756 1757 1758