rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* A network driver using virtio. |
| 2 | * |
| 3 | * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | //#define DEBUG |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/etherdevice.h> |
| 21 | #include <linux/ethtool.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/virtio.h> |
| 24 | #include <linux/virtio_net.h> |
| 25 | #include <linux/bpf.h> |
| 26 | #include <linux/bpf_trace.h> |
| 27 | #include <linux/scatterlist.h> |
| 28 | #include <linux/if_vlan.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/cpu.h> |
| 31 | #include <linux/average.h> |
| 32 | #include <net/route.h> |
| 33 | |
| 34 | static int napi_weight = NAPI_POLL_WEIGHT; |
| 35 | module_param(napi_weight, int, 0444); |
| 36 | |
| 37 | static bool csum = true, gso = true, napi_tx; |
| 38 | module_param(csum, bool, 0444); |
| 39 | module_param(gso, bool, 0444); |
| 40 | module_param(napi_tx, bool, 0644); |
| 41 | |
| 42 | /* FIXME: MTU in config. */ |
| 43 | #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) |
| 44 | #define GOOD_COPY_LEN 128 |
| 45 | |
| 46 | #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) |
| 47 | |
| 48 | /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */ |
| 49 | #define VIRTIO_XDP_HEADROOM 256 |
| 50 | |
| 51 | /* RX packet size EWMA. The average packet size is used to determine the packet |
| 52 | * buffer size when refilling RX rings. As the entire RX ring may be refilled |
| 53 | * at once, the weight is chosen so that the EWMA will be insensitive to short- |
| 54 | * term, transient changes in packet size. |
| 55 | */ |
| 56 | DECLARE_EWMA(pkt_len, 0, 64) |
| 57 | |
| 58 | #define VIRTNET_DRIVER_VERSION "1.0.0" |
| 59 | |
| 60 | static const unsigned long guest_offloads[] = { |
| 61 | VIRTIO_NET_F_GUEST_TSO4, |
| 62 | VIRTIO_NET_F_GUEST_TSO6, |
| 63 | VIRTIO_NET_F_GUEST_ECN, |
| 64 | VIRTIO_NET_F_GUEST_UFO, |
| 65 | VIRTIO_NET_F_GUEST_CSUM |
| 66 | }; |
| 67 | |
| 68 | struct virtnet_stats { |
| 69 | struct u64_stats_sync tx_syncp; |
| 70 | struct u64_stats_sync rx_syncp; |
| 71 | u64 tx_bytes; |
| 72 | u64 tx_packets; |
| 73 | |
| 74 | u64 rx_bytes; |
| 75 | u64 rx_packets; |
| 76 | }; |
| 77 | |
| 78 | /* Internal representation of a send virtqueue */ |
| 79 | struct send_queue { |
| 80 | /* Virtqueue associated with this send _queue */ |
| 81 | struct virtqueue *vq; |
| 82 | |
| 83 | /* TX: fragments + linear part + virtio header */ |
| 84 | struct scatterlist sg[MAX_SKB_FRAGS + 2]; |
| 85 | |
| 86 | /* Name of the send queue: output.$index */ |
| 87 | char name[40]; |
| 88 | |
| 89 | struct napi_struct napi; |
| 90 | }; |
| 91 | |
| 92 | /* Internal representation of a receive virtqueue */ |
| 93 | struct receive_queue { |
| 94 | /* Virtqueue associated with this receive_queue */ |
| 95 | struct virtqueue *vq; |
| 96 | |
| 97 | struct napi_struct napi; |
| 98 | |
| 99 | struct bpf_prog __rcu *xdp_prog; |
| 100 | |
| 101 | /* Chain pages by the private ptr. */ |
| 102 | struct page *pages; |
| 103 | |
| 104 | /* Average packet length for mergeable receive buffers. */ |
| 105 | struct ewma_pkt_len mrg_avg_pkt_len; |
| 106 | |
| 107 | /* Page frag for packet buffer allocation. */ |
| 108 | struct page_frag alloc_frag; |
| 109 | |
| 110 | /* RX: fragments + linear part + virtio header */ |
| 111 | struct scatterlist sg[MAX_SKB_FRAGS + 2]; |
| 112 | |
| 113 | /* Min single buffer size for mergeable buffers case. */ |
| 114 | unsigned int min_buf_len; |
| 115 | |
| 116 | /* Name of this receive queue: input.$index */ |
| 117 | char name[40]; |
| 118 | }; |
| 119 | |
| 120 | /* Control VQ buffers: protected by the rtnl lock */ |
| 121 | struct control_buf { |
| 122 | struct virtio_net_ctrl_hdr hdr; |
| 123 | virtio_net_ctrl_ack status; |
| 124 | struct virtio_net_ctrl_mq mq; |
| 125 | u8 promisc; |
| 126 | u8 allmulti; |
| 127 | __virtio16 vid; |
| 128 | u64 offloads; |
| 129 | }; |
| 130 | |
| 131 | struct virtnet_info { |
| 132 | struct virtio_device *vdev; |
| 133 | struct virtqueue *cvq; |
| 134 | struct net_device *dev; |
| 135 | struct send_queue *sq; |
| 136 | struct receive_queue *rq; |
| 137 | unsigned int status; |
| 138 | |
| 139 | /* Max # of queue pairs supported by the device */ |
| 140 | u16 max_queue_pairs; |
| 141 | |
| 142 | /* # of queue pairs currently used by the driver */ |
| 143 | u16 curr_queue_pairs; |
| 144 | |
| 145 | /* # of XDP queue pairs currently used by the driver */ |
| 146 | u16 xdp_queue_pairs; |
| 147 | |
| 148 | /* I like... big packets and I cannot lie! */ |
| 149 | bool big_packets; |
| 150 | |
| 151 | /* Host will merge rx buffers for big packets (shake it! shake it!) */ |
| 152 | bool mergeable_rx_bufs; |
| 153 | |
| 154 | /* Has control virtqueue */ |
| 155 | bool has_cvq; |
| 156 | |
| 157 | /* Host can handle any s/g split between our header and packet data */ |
| 158 | bool any_header_sg; |
| 159 | |
| 160 | /* Packet virtio header size */ |
| 161 | u8 hdr_len; |
| 162 | |
| 163 | /* Active statistics */ |
| 164 | struct virtnet_stats __percpu *stats; |
| 165 | |
| 166 | /* Work struct for refilling if we run low on memory. */ |
| 167 | struct delayed_work refill; |
| 168 | |
| 169 | /* Work struct for config space updates */ |
| 170 | struct work_struct config_work; |
| 171 | |
| 172 | /* Does the affinity hint is set for virtqueues? */ |
| 173 | bool affinity_hint_set; |
| 174 | |
| 175 | /* CPU hotplug instances for online & dead */ |
| 176 | struct hlist_node node; |
| 177 | struct hlist_node node_dead; |
| 178 | |
| 179 | struct control_buf *ctrl; |
| 180 | |
| 181 | /* Ethtool settings */ |
| 182 | u8 duplex; |
| 183 | u32 speed; |
| 184 | |
| 185 | unsigned long guest_offloads; |
| 186 | }; |
| 187 | |
| 188 | struct padded_vnet_hdr { |
| 189 | struct virtio_net_hdr_mrg_rxbuf hdr; |
| 190 | /* |
| 191 | * hdr is in a separate sg buffer, and data sg buffer shares same page |
| 192 | * with this header sg. This padding makes next sg 16 byte aligned |
| 193 | * after the header. |
| 194 | */ |
| 195 | char padding[4]; |
| 196 | }; |
| 197 | |
| 198 | /* Converting between virtqueue no. and kernel tx/rx queue no. |
| 199 | * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq |
| 200 | */ |
| 201 | static int vq2txq(struct virtqueue *vq) |
| 202 | { |
| 203 | return (vq->index - 1) / 2; |
| 204 | } |
| 205 | |
| 206 | static int txq2vq(int txq) |
| 207 | { |
| 208 | return txq * 2 + 1; |
| 209 | } |
| 210 | |
| 211 | static int vq2rxq(struct virtqueue *vq) |
| 212 | { |
| 213 | return vq->index / 2; |
| 214 | } |
| 215 | |
| 216 | static int rxq2vq(int rxq) |
| 217 | { |
| 218 | return rxq * 2; |
| 219 | } |
| 220 | |
| 221 | static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) |
| 222 | { |
| 223 | return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * private is used to chain pages for big packets, put the whole |
| 228 | * most recent used list in the beginning for reuse |
| 229 | */ |
| 230 | static void give_pages(struct receive_queue *rq, struct page *page) |
| 231 | { |
| 232 | struct page *end; |
| 233 | |
| 234 | /* Find end of list, sew whole thing into vi->rq.pages. */ |
| 235 | for (end = page; end->private; end = (struct page *)end->private); |
| 236 | end->private = (unsigned long)rq->pages; |
| 237 | rq->pages = page; |
| 238 | } |
| 239 | |
| 240 | static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) |
| 241 | { |
| 242 | struct page *p = rq->pages; |
| 243 | |
| 244 | if (p) { |
| 245 | rq->pages = (struct page *)p->private; |
| 246 | /* clear private here, it is used to chain pages */ |
| 247 | p->private = 0; |
| 248 | } else |
| 249 | p = alloc_page(gfp_mask); |
| 250 | return p; |
| 251 | } |
| 252 | |
| 253 | static void virtqueue_napi_schedule(struct napi_struct *napi, |
| 254 | struct virtqueue *vq) |
| 255 | { |
| 256 | if (napi_schedule_prep(napi)) { |
| 257 | virtqueue_disable_cb(vq); |
| 258 | __napi_schedule(napi); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void virtqueue_napi_complete(struct napi_struct *napi, |
| 263 | struct virtqueue *vq, int processed) |
| 264 | { |
| 265 | int opaque; |
| 266 | |
| 267 | opaque = virtqueue_enable_cb_prepare(vq); |
| 268 | if (napi_complete_done(napi, processed)) { |
| 269 | if (unlikely(virtqueue_poll(vq, opaque))) |
| 270 | virtqueue_napi_schedule(napi, vq); |
| 271 | } else { |
| 272 | virtqueue_disable_cb(vq); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void skb_xmit_done(struct virtqueue *vq) |
| 277 | { |
| 278 | struct virtnet_info *vi = vq->vdev->priv; |
| 279 | struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; |
| 280 | |
| 281 | /* Suppress further interrupts. */ |
| 282 | virtqueue_disable_cb(vq); |
| 283 | |
| 284 | if (napi->weight) |
| 285 | virtqueue_napi_schedule(napi, vq); |
| 286 | else |
| 287 | /* We were probably waiting for more output buffers. */ |
| 288 | netif_wake_subqueue(vi->dev, vq2txq(vq)); |
| 289 | } |
| 290 | |
| 291 | #define MRG_CTX_HEADER_SHIFT 22 |
| 292 | static void *mergeable_len_to_ctx(unsigned int truesize, |
| 293 | unsigned int headroom) |
| 294 | { |
| 295 | return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize); |
| 296 | } |
| 297 | |
| 298 | static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx) |
| 299 | { |
| 300 | return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT; |
| 301 | } |
| 302 | |
| 303 | static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx) |
| 304 | { |
| 305 | return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1); |
| 306 | } |
| 307 | |
| 308 | /* Called from bottom half context */ |
| 309 | static struct sk_buff *page_to_skb(struct virtnet_info *vi, |
| 310 | struct receive_queue *rq, |
| 311 | struct page *page, unsigned int offset, |
| 312 | unsigned int len, unsigned int truesize, |
| 313 | bool hdr_valid) |
| 314 | { |
| 315 | struct sk_buff *skb; |
| 316 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
| 317 | unsigned int copy, hdr_len, hdr_padded_len; |
| 318 | char *p; |
| 319 | |
| 320 | p = page_address(page) + offset; |
| 321 | |
| 322 | /* copy small packet so we can reuse these pages for small data */ |
| 323 | skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); |
| 324 | if (unlikely(!skb)) |
| 325 | return NULL; |
| 326 | |
| 327 | hdr = skb_vnet_hdr(skb); |
| 328 | |
| 329 | hdr_len = vi->hdr_len; |
| 330 | if (vi->mergeable_rx_bufs) |
| 331 | hdr_padded_len = sizeof(*hdr); |
| 332 | else |
| 333 | hdr_padded_len = sizeof(struct padded_vnet_hdr); |
| 334 | |
| 335 | if (hdr_valid) |
| 336 | memcpy(hdr, p, hdr_len); |
| 337 | |
| 338 | len -= hdr_len; |
| 339 | offset += hdr_padded_len; |
| 340 | p += hdr_padded_len; |
| 341 | |
| 342 | copy = len; |
| 343 | if (copy > skb_tailroom(skb)) |
| 344 | copy = skb_tailroom(skb); |
| 345 | skb_put_data(skb, p, copy); |
| 346 | |
| 347 | len -= copy; |
| 348 | offset += copy; |
| 349 | |
| 350 | if (vi->mergeable_rx_bufs) { |
| 351 | if (len) |
| 352 | skb_add_rx_frag(skb, 0, page, offset, len, truesize); |
| 353 | else |
| 354 | put_page(page); |
| 355 | return skb; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Verify that we can indeed put this data into a skb. |
| 360 | * This is here to handle cases when the device erroneously |
| 361 | * tries to receive more than is possible. This is usually |
| 362 | * the case of a broken device. |
| 363 | */ |
| 364 | if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { |
| 365 | net_dbg_ratelimited("%s: too much data\n", skb->dev->name); |
| 366 | dev_kfree_skb(skb); |
| 367 | return NULL; |
| 368 | } |
| 369 | BUG_ON(offset >= PAGE_SIZE); |
| 370 | while (len) { |
| 371 | unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); |
| 372 | skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, |
| 373 | frag_size, truesize); |
| 374 | len -= frag_size; |
| 375 | page = (struct page *)page->private; |
| 376 | offset = 0; |
| 377 | } |
| 378 | |
| 379 | if (page) |
| 380 | give_pages(rq, page); |
| 381 | |
| 382 | return skb; |
| 383 | } |
| 384 | |
| 385 | static bool virtnet_xdp_xmit(struct virtnet_info *vi, |
| 386 | struct receive_queue *rq, |
| 387 | struct xdp_buff *xdp) |
| 388 | { |
| 389 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
| 390 | unsigned int len; |
| 391 | struct send_queue *sq; |
| 392 | unsigned int qp; |
| 393 | void *xdp_sent; |
| 394 | int err; |
| 395 | |
| 396 | qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); |
| 397 | sq = &vi->sq[qp]; |
| 398 | |
| 399 | /* Free up any pending old buffers before queueing new ones. */ |
| 400 | while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) { |
| 401 | struct page *sent_page = virt_to_head_page(xdp_sent); |
| 402 | |
| 403 | put_page(sent_page); |
| 404 | } |
| 405 | |
| 406 | xdp->data -= vi->hdr_len; |
| 407 | /* Zero header and leave csum up to XDP layers */ |
| 408 | hdr = xdp->data; |
| 409 | memset(hdr, 0, vi->hdr_len); |
| 410 | |
| 411 | sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data); |
| 412 | |
| 413 | err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC); |
| 414 | if (unlikely(err)) { |
| 415 | struct page *page = virt_to_head_page(xdp->data); |
| 416 | |
| 417 | put_page(page); |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | virtqueue_kick(sq->vq); |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | static unsigned int virtnet_get_headroom(struct virtnet_info *vi) |
| 426 | { |
| 427 | return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0; |
| 428 | } |
| 429 | |
| 430 | /* We copy the packet for XDP in the following cases: |
| 431 | * |
| 432 | * 1) Packet is scattered across multiple rx buffers. |
| 433 | * 2) Headroom space is insufficient. |
| 434 | * |
| 435 | * This is inefficient but it's a temporary condition that |
| 436 | * we hit right after XDP is enabled and until queue is refilled |
| 437 | * with large buffers with sufficient headroom - so it should affect |
| 438 | * at most queue size packets. |
| 439 | * Afterwards, the conditions to enable |
| 440 | * XDP should preclude the underlying device from sending packets |
| 441 | * across multiple buffers (num_buf > 1), and we make sure buffers |
| 442 | * have enough headroom. |
| 443 | */ |
| 444 | static struct page *xdp_linearize_page(struct receive_queue *rq, |
| 445 | u16 *num_buf, |
| 446 | struct page *p, |
| 447 | int offset, |
| 448 | int page_off, |
| 449 | unsigned int *len) |
| 450 | { |
| 451 | struct page *page = alloc_page(GFP_ATOMIC); |
| 452 | |
| 453 | if (!page) |
| 454 | return NULL; |
| 455 | |
| 456 | memcpy(page_address(page) + page_off, page_address(p) + offset, *len); |
| 457 | page_off += *len; |
| 458 | |
| 459 | while (--*num_buf) { |
| 460 | unsigned int buflen; |
| 461 | void *buf; |
| 462 | int off; |
| 463 | |
| 464 | buf = virtqueue_get_buf(rq->vq, &buflen); |
| 465 | if (unlikely(!buf)) |
| 466 | goto err_buf; |
| 467 | |
| 468 | p = virt_to_head_page(buf); |
| 469 | off = buf - page_address(p); |
| 470 | |
| 471 | /* guard against a misconfigured or uncooperative backend that |
| 472 | * is sending packet larger than the MTU. |
| 473 | */ |
| 474 | if ((page_off + buflen) > PAGE_SIZE) { |
| 475 | put_page(p); |
| 476 | goto err_buf; |
| 477 | } |
| 478 | |
| 479 | memcpy(page_address(page) + page_off, |
| 480 | page_address(p) + off, buflen); |
| 481 | page_off += buflen; |
| 482 | put_page(p); |
| 483 | } |
| 484 | |
| 485 | /* Headroom does not contribute to packet length */ |
| 486 | *len = page_off - VIRTIO_XDP_HEADROOM; |
| 487 | return page; |
| 488 | err_buf: |
| 489 | __free_pages(page, 0); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | static struct sk_buff *receive_small(struct net_device *dev, |
| 494 | struct virtnet_info *vi, |
| 495 | struct receive_queue *rq, |
| 496 | void *buf, void *ctx, |
| 497 | unsigned int len) |
| 498 | { |
| 499 | struct sk_buff *skb; |
| 500 | struct bpf_prog *xdp_prog; |
| 501 | unsigned int xdp_headroom = (unsigned long)ctx; |
| 502 | unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom; |
| 503 | unsigned int headroom = vi->hdr_len + header_offset; |
| 504 | unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + |
| 505 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 506 | struct page *page = virt_to_head_page(buf); |
| 507 | unsigned int delta = 0; |
| 508 | struct page *xdp_page; |
| 509 | len -= vi->hdr_len; |
| 510 | |
| 511 | rcu_read_lock(); |
| 512 | xdp_prog = rcu_dereference(rq->xdp_prog); |
| 513 | if (xdp_prog) { |
| 514 | struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset; |
| 515 | struct xdp_buff xdp; |
| 516 | void *orig_data; |
| 517 | u32 act; |
| 518 | |
| 519 | if (unlikely(hdr->hdr.gso_type)) |
| 520 | goto err_xdp; |
| 521 | |
| 522 | if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { |
| 523 | int offset = buf - page_address(page) + header_offset; |
| 524 | unsigned int tlen = len + vi->hdr_len; |
| 525 | u16 num_buf = 1; |
| 526 | |
| 527 | xdp_headroom = virtnet_get_headroom(vi); |
| 528 | header_offset = VIRTNET_RX_PAD + xdp_headroom; |
| 529 | headroom = vi->hdr_len + header_offset; |
| 530 | buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) + |
| 531 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 532 | xdp_page = xdp_linearize_page(rq, &num_buf, page, |
| 533 | offset, header_offset, |
| 534 | &tlen); |
| 535 | if (!xdp_page) |
| 536 | goto err_xdp; |
| 537 | |
| 538 | buf = page_address(xdp_page); |
| 539 | put_page(page); |
| 540 | page = xdp_page; |
| 541 | } |
| 542 | |
| 543 | xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len; |
| 544 | xdp.data = xdp.data_hard_start + xdp_headroom; |
| 545 | xdp.data_end = xdp.data + len; |
| 546 | orig_data = xdp.data; |
| 547 | act = bpf_prog_run_xdp(xdp_prog, &xdp); |
| 548 | |
| 549 | switch (act) { |
| 550 | case XDP_PASS: |
| 551 | /* Recalculate length in case bpf program changed it */ |
| 552 | delta = orig_data - xdp.data; |
| 553 | break; |
| 554 | case XDP_TX: |
| 555 | if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp))) |
| 556 | trace_xdp_exception(vi->dev, xdp_prog, act); |
| 557 | rcu_read_unlock(); |
| 558 | goto xdp_xmit; |
| 559 | default: |
| 560 | bpf_warn_invalid_xdp_action(act); |
| 561 | case XDP_ABORTED: |
| 562 | trace_xdp_exception(vi->dev, xdp_prog, act); |
| 563 | case XDP_DROP: |
| 564 | goto err_xdp; |
| 565 | } |
| 566 | } |
| 567 | rcu_read_unlock(); |
| 568 | |
| 569 | skb = build_skb(buf, buflen); |
| 570 | if (!skb) { |
| 571 | put_page(page); |
| 572 | goto err; |
| 573 | } |
| 574 | skb_reserve(skb, headroom - delta); |
| 575 | skb_put(skb, len + delta); |
| 576 | if (!delta) { |
| 577 | buf += header_offset; |
| 578 | memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len); |
| 579 | } /* keep zeroed vnet hdr since packet was changed by bpf */ |
| 580 | |
| 581 | err: |
| 582 | return skb; |
| 583 | |
| 584 | err_xdp: |
| 585 | rcu_read_unlock(); |
| 586 | dev->stats.rx_dropped++; |
| 587 | put_page(page); |
| 588 | xdp_xmit: |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | static struct sk_buff *receive_big(struct net_device *dev, |
| 593 | struct virtnet_info *vi, |
| 594 | struct receive_queue *rq, |
| 595 | void *buf, |
| 596 | unsigned int len) |
| 597 | { |
| 598 | struct page *page = buf; |
| 599 | struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, |
| 600 | PAGE_SIZE, true); |
| 601 | |
| 602 | if (unlikely(!skb)) |
| 603 | goto err; |
| 604 | |
| 605 | return skb; |
| 606 | |
| 607 | err: |
| 608 | dev->stats.rx_dropped++; |
| 609 | give_pages(rq, page); |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | static struct sk_buff *receive_mergeable(struct net_device *dev, |
| 614 | struct virtnet_info *vi, |
| 615 | struct receive_queue *rq, |
| 616 | void *buf, |
| 617 | void *ctx, |
| 618 | unsigned int len) |
| 619 | { |
| 620 | struct virtio_net_hdr_mrg_rxbuf *hdr = buf; |
| 621 | u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); |
| 622 | struct page *page = virt_to_head_page(buf); |
| 623 | int offset = buf - page_address(page); |
| 624 | struct sk_buff *head_skb, *curr_skb; |
| 625 | struct bpf_prog *xdp_prog; |
| 626 | unsigned int truesize; |
| 627 | unsigned int headroom = mergeable_ctx_to_headroom(ctx); |
| 628 | |
| 629 | head_skb = NULL; |
| 630 | |
| 631 | rcu_read_lock(); |
| 632 | xdp_prog = rcu_dereference(rq->xdp_prog); |
| 633 | if (xdp_prog) { |
| 634 | struct page *xdp_page; |
| 635 | struct xdp_buff xdp; |
| 636 | void *data; |
| 637 | u32 act; |
| 638 | |
| 639 | /* Transient failure which in theory could occur if |
| 640 | * in-flight packets from before XDP was enabled reach |
| 641 | * the receive path after XDP is loaded. |
| 642 | */ |
| 643 | if (unlikely(hdr->hdr.gso_type)) |
| 644 | goto err_xdp; |
| 645 | |
| 646 | /* This happens when rx buffer size is underestimated */ |
| 647 | if (unlikely(num_buf > 1 || |
| 648 | headroom < virtnet_get_headroom(vi))) { |
| 649 | /* linearize data for XDP */ |
| 650 | xdp_page = xdp_linearize_page(rq, &num_buf, |
| 651 | page, offset, |
| 652 | VIRTIO_XDP_HEADROOM, |
| 653 | &len); |
| 654 | if (!xdp_page) |
| 655 | goto err_xdp; |
| 656 | offset = VIRTIO_XDP_HEADROOM; |
| 657 | } else { |
| 658 | xdp_page = page; |
| 659 | } |
| 660 | |
| 661 | /* Allow consuming headroom but reserve enough space to push |
| 662 | * the descriptor on if we get an XDP_TX return code. |
| 663 | */ |
| 664 | data = page_address(xdp_page) + offset; |
| 665 | xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len; |
| 666 | xdp.data = data + vi->hdr_len; |
| 667 | xdp.data_end = xdp.data + (len - vi->hdr_len); |
| 668 | act = bpf_prog_run_xdp(xdp_prog, &xdp); |
| 669 | |
| 670 | switch (act) { |
| 671 | case XDP_PASS: |
| 672 | /* recalculate offset to account for any header |
| 673 | * adjustments. Note other cases do not build an |
| 674 | * skb and avoid using offset |
| 675 | */ |
| 676 | offset = xdp.data - |
| 677 | page_address(xdp_page) - vi->hdr_len; |
| 678 | |
| 679 | /* We can only create skb based on xdp_page. */ |
| 680 | if (unlikely(xdp_page != page)) { |
| 681 | rcu_read_unlock(); |
| 682 | put_page(page); |
| 683 | head_skb = page_to_skb(vi, rq, xdp_page, |
| 684 | offset, len, |
| 685 | PAGE_SIZE, false); |
| 686 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); |
| 687 | return head_skb; |
| 688 | } |
| 689 | break; |
| 690 | case XDP_TX: |
| 691 | if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp))) |
| 692 | trace_xdp_exception(vi->dev, xdp_prog, act); |
| 693 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); |
| 694 | if (unlikely(xdp_page != page)) |
| 695 | put_page(page); |
| 696 | rcu_read_unlock(); |
| 697 | goto xdp_xmit; |
| 698 | default: |
| 699 | bpf_warn_invalid_xdp_action(act); |
| 700 | case XDP_ABORTED: |
| 701 | trace_xdp_exception(vi->dev, xdp_prog, act); |
| 702 | case XDP_DROP: |
| 703 | if (unlikely(xdp_page != page)) |
| 704 | __free_pages(xdp_page, 0); |
| 705 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); |
| 706 | goto err_xdp; |
| 707 | } |
| 708 | } |
| 709 | rcu_read_unlock(); |
| 710 | |
| 711 | truesize = mergeable_ctx_to_truesize(ctx); |
| 712 | if (unlikely(len > truesize)) { |
| 713 | pr_debug("%s: rx error: len %u exceeds truesize %lu\n", |
| 714 | dev->name, len, (unsigned long)ctx); |
| 715 | dev->stats.rx_length_errors++; |
| 716 | goto err_skb; |
| 717 | } |
| 718 | |
| 719 | head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog); |
| 720 | curr_skb = head_skb; |
| 721 | |
| 722 | if (unlikely(!curr_skb)) |
| 723 | goto err_skb; |
| 724 | while (--num_buf) { |
| 725 | int num_skb_frags; |
| 726 | |
| 727 | buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx); |
| 728 | if (unlikely(!buf)) { |
| 729 | pr_debug("%s: rx error: %d buffers out of %d missing\n", |
| 730 | dev->name, num_buf, |
| 731 | virtio16_to_cpu(vi->vdev, |
| 732 | hdr->num_buffers)); |
| 733 | dev->stats.rx_length_errors++; |
| 734 | goto err_buf; |
| 735 | } |
| 736 | |
| 737 | page = virt_to_head_page(buf); |
| 738 | |
| 739 | truesize = mergeable_ctx_to_truesize(ctx); |
| 740 | if (unlikely(len > truesize)) { |
| 741 | pr_debug("%s: rx error: len %u exceeds truesize %lu\n", |
| 742 | dev->name, len, (unsigned long)ctx); |
| 743 | dev->stats.rx_length_errors++; |
| 744 | goto err_skb; |
| 745 | } |
| 746 | |
| 747 | num_skb_frags = skb_shinfo(curr_skb)->nr_frags; |
| 748 | if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { |
| 749 | struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); |
| 750 | |
| 751 | if (unlikely(!nskb)) |
| 752 | goto err_skb; |
| 753 | if (curr_skb == head_skb) |
| 754 | skb_shinfo(curr_skb)->frag_list = nskb; |
| 755 | else |
| 756 | curr_skb->next = nskb; |
| 757 | curr_skb = nskb; |
| 758 | head_skb->truesize += nskb->truesize; |
| 759 | num_skb_frags = 0; |
| 760 | } |
| 761 | if (curr_skb != head_skb) { |
| 762 | head_skb->data_len += len; |
| 763 | head_skb->len += len; |
| 764 | head_skb->truesize += truesize; |
| 765 | } |
| 766 | offset = buf - page_address(page); |
| 767 | if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { |
| 768 | put_page(page); |
| 769 | skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, |
| 770 | len, truesize); |
| 771 | } else { |
| 772 | skb_add_rx_frag(curr_skb, num_skb_frags, page, |
| 773 | offset, len, truesize); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); |
| 778 | return head_skb; |
| 779 | |
| 780 | err_xdp: |
| 781 | rcu_read_unlock(); |
| 782 | err_skb: |
| 783 | put_page(page); |
| 784 | while (num_buf-- > 1) { |
| 785 | buf = virtqueue_get_buf(rq->vq, &len); |
| 786 | if (unlikely(!buf)) { |
| 787 | pr_debug("%s: rx error: %d buffers missing\n", |
| 788 | dev->name, num_buf); |
| 789 | dev->stats.rx_length_errors++; |
| 790 | break; |
| 791 | } |
| 792 | page = virt_to_head_page(buf); |
| 793 | put_page(page); |
| 794 | } |
| 795 | err_buf: |
| 796 | dev->stats.rx_dropped++; |
| 797 | dev_kfree_skb(head_skb); |
| 798 | xdp_xmit: |
| 799 | return NULL; |
| 800 | } |
| 801 | |
| 802 | static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq, |
| 803 | void *buf, unsigned int len, void **ctx) |
| 804 | { |
| 805 | struct net_device *dev = vi->dev; |
| 806 | struct sk_buff *skb; |
| 807 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
| 808 | int ret; |
| 809 | |
| 810 | if (unlikely(len < vi->hdr_len + ETH_HLEN)) { |
| 811 | pr_debug("%s: short packet %i\n", dev->name, len); |
| 812 | dev->stats.rx_length_errors++; |
| 813 | if (vi->mergeable_rx_bufs) { |
| 814 | put_page(virt_to_head_page(buf)); |
| 815 | } else if (vi->big_packets) { |
| 816 | give_pages(rq, buf); |
| 817 | } else { |
| 818 | put_page(virt_to_head_page(buf)); |
| 819 | } |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | if (vi->mergeable_rx_bufs) |
| 824 | skb = receive_mergeable(dev, vi, rq, buf, ctx, len); |
| 825 | else if (vi->big_packets) |
| 826 | skb = receive_big(dev, vi, rq, buf, len); |
| 827 | else |
| 828 | skb = receive_small(dev, vi, rq, buf, ctx, len); |
| 829 | |
| 830 | if (unlikely(!skb)) |
| 831 | return 0; |
| 832 | |
| 833 | hdr = skb_vnet_hdr(skb); |
| 834 | |
| 835 | ret = skb->len; |
| 836 | |
| 837 | if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) |
| 838 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 839 | |
| 840 | if (virtio_net_hdr_to_skb(skb, &hdr->hdr, |
| 841 | virtio_is_little_endian(vi->vdev))) { |
| 842 | net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", |
| 843 | dev->name, hdr->hdr.gso_type, |
| 844 | hdr->hdr.gso_size); |
| 845 | goto frame_err; |
| 846 | } |
| 847 | |
| 848 | skb->protocol = eth_type_trans(skb, dev); |
| 849 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", |
| 850 | ntohs(skb->protocol), skb->len, skb->pkt_type); |
| 851 | |
| 852 | napi_gro_receive(&rq->napi, skb); |
| 853 | return ret; |
| 854 | |
| 855 | frame_err: |
| 856 | dev->stats.rx_frame_errors++; |
| 857 | dev_kfree_skb(skb); |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | /* Unlike mergeable buffers, all buffers are allocated to the |
| 862 | * same size, except for the headroom. For this reason we do |
| 863 | * not need to use mergeable_len_to_ctx here - it is enough |
| 864 | * to store the headroom as the context ignoring the truesize. |
| 865 | */ |
| 866 | static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, |
| 867 | gfp_t gfp) |
| 868 | { |
| 869 | struct page_frag *alloc_frag = &rq->alloc_frag; |
| 870 | char *buf; |
| 871 | unsigned int xdp_headroom = virtnet_get_headroom(vi); |
| 872 | void *ctx = (void *)(unsigned long)xdp_headroom; |
| 873 | int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; |
| 874 | int err; |
| 875 | |
| 876 | len = SKB_DATA_ALIGN(len) + |
| 877 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 878 | if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) |
| 879 | return -ENOMEM; |
| 880 | |
| 881 | buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; |
| 882 | get_page(alloc_frag->page); |
| 883 | alloc_frag->offset += len; |
| 884 | sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom, |
| 885 | vi->hdr_len + GOOD_PACKET_LEN); |
| 886 | err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); |
| 887 | if (err < 0) |
| 888 | put_page(virt_to_head_page(buf)); |
| 889 | return err; |
| 890 | } |
| 891 | |
| 892 | static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, |
| 893 | gfp_t gfp) |
| 894 | { |
| 895 | struct page *first, *list = NULL; |
| 896 | char *p; |
| 897 | int i, err, offset; |
| 898 | |
| 899 | sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); |
| 900 | |
| 901 | /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ |
| 902 | for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { |
| 903 | first = get_a_page(rq, gfp); |
| 904 | if (!first) { |
| 905 | if (list) |
| 906 | give_pages(rq, list); |
| 907 | return -ENOMEM; |
| 908 | } |
| 909 | sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); |
| 910 | |
| 911 | /* chain new page in list head to match sg */ |
| 912 | first->private = (unsigned long)list; |
| 913 | list = first; |
| 914 | } |
| 915 | |
| 916 | first = get_a_page(rq, gfp); |
| 917 | if (!first) { |
| 918 | give_pages(rq, list); |
| 919 | return -ENOMEM; |
| 920 | } |
| 921 | p = page_address(first); |
| 922 | |
| 923 | /* rq->sg[0], rq->sg[1] share the same page */ |
| 924 | /* a separated rq->sg[0] for header - required in case !any_header_sg */ |
| 925 | sg_set_buf(&rq->sg[0], p, vi->hdr_len); |
| 926 | |
| 927 | /* rq->sg[1] for data packet, from offset */ |
| 928 | offset = sizeof(struct padded_vnet_hdr); |
| 929 | sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); |
| 930 | |
| 931 | /* chain first in list head */ |
| 932 | first->private = (unsigned long)list; |
| 933 | err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, |
| 934 | first, gfp); |
| 935 | if (err < 0) |
| 936 | give_pages(rq, first); |
| 937 | |
| 938 | return err; |
| 939 | } |
| 940 | |
| 941 | static unsigned int get_mergeable_buf_len(struct receive_queue *rq, |
| 942 | struct ewma_pkt_len *avg_pkt_len) |
| 943 | { |
| 944 | const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); |
| 945 | unsigned int len; |
| 946 | |
| 947 | len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), |
| 948 | rq->min_buf_len, PAGE_SIZE - hdr_len); |
| 949 | return ALIGN(len, L1_CACHE_BYTES); |
| 950 | } |
| 951 | |
| 952 | static int add_recvbuf_mergeable(struct virtnet_info *vi, |
| 953 | struct receive_queue *rq, gfp_t gfp) |
| 954 | { |
| 955 | struct page_frag *alloc_frag = &rq->alloc_frag; |
| 956 | unsigned int headroom = virtnet_get_headroom(vi); |
| 957 | char *buf; |
| 958 | void *ctx; |
| 959 | int err; |
| 960 | unsigned int len, hole; |
| 961 | |
| 962 | len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len); |
| 963 | if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp))) |
| 964 | return -ENOMEM; |
| 965 | |
| 966 | buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; |
| 967 | buf += headroom; /* advance address leaving hole at front of pkt */ |
| 968 | get_page(alloc_frag->page); |
| 969 | alloc_frag->offset += len + headroom; |
| 970 | hole = alloc_frag->size - alloc_frag->offset; |
| 971 | if (hole < len + headroom) { |
| 972 | /* To avoid internal fragmentation, if there is very likely not |
| 973 | * enough space for another buffer, add the remaining space to |
| 974 | * the current buffer. |
| 975 | */ |
| 976 | len += hole; |
| 977 | alloc_frag->offset += hole; |
| 978 | } |
| 979 | |
| 980 | sg_init_one(rq->sg, buf, len); |
| 981 | ctx = mergeable_len_to_ctx(len, headroom); |
| 982 | err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp); |
| 983 | if (err < 0) |
| 984 | put_page(virt_to_head_page(buf)); |
| 985 | |
| 986 | return err; |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * Returns false if we couldn't fill entirely (OOM). |
| 991 | * |
| 992 | * Normally run in the receive path, but can also be run from ndo_open |
| 993 | * before we're receiving packets, or from refill_work which is |
| 994 | * careful to disable receiving (using napi_disable). |
| 995 | */ |
| 996 | static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, |
| 997 | gfp_t gfp) |
| 998 | { |
| 999 | int err; |
| 1000 | bool oom; |
| 1001 | |
| 1002 | gfp |= __GFP_COLD; |
| 1003 | do { |
| 1004 | if (vi->mergeable_rx_bufs) |
| 1005 | err = add_recvbuf_mergeable(vi, rq, gfp); |
| 1006 | else if (vi->big_packets) |
| 1007 | err = add_recvbuf_big(vi, rq, gfp); |
| 1008 | else |
| 1009 | err = add_recvbuf_small(vi, rq, gfp); |
| 1010 | |
| 1011 | oom = err == -ENOMEM; |
| 1012 | if (err) |
| 1013 | break; |
| 1014 | } while (rq->vq->num_free); |
| 1015 | virtqueue_kick(rq->vq); |
| 1016 | return !oom; |
| 1017 | } |
| 1018 | |
| 1019 | static void skb_recv_done(struct virtqueue *rvq) |
| 1020 | { |
| 1021 | struct virtnet_info *vi = rvq->vdev->priv; |
| 1022 | struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; |
| 1023 | |
| 1024 | virtqueue_napi_schedule(&rq->napi, rvq); |
| 1025 | } |
| 1026 | |
| 1027 | static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi) |
| 1028 | { |
| 1029 | napi_enable(napi); |
| 1030 | |
| 1031 | /* If all buffers were filled by other side before we napi_enabled, we |
| 1032 | * won't get another interrupt, so process any outstanding packets now. |
| 1033 | * Call local_bh_enable after to trigger softIRQ processing. |
| 1034 | */ |
| 1035 | local_bh_disable(); |
| 1036 | virtqueue_napi_schedule(napi, vq); |
| 1037 | local_bh_enable(); |
| 1038 | } |
| 1039 | |
| 1040 | static void virtnet_napi_tx_enable(struct virtnet_info *vi, |
| 1041 | struct virtqueue *vq, |
| 1042 | struct napi_struct *napi) |
| 1043 | { |
| 1044 | if (!napi->weight) |
| 1045 | return; |
| 1046 | |
| 1047 | /* Tx napi touches cachelines on the cpu handling tx interrupts. Only |
| 1048 | * enable the feature if this is likely affine with the transmit path. |
| 1049 | */ |
| 1050 | if (!vi->affinity_hint_set) { |
| 1051 | napi->weight = 0; |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | return virtnet_napi_enable(vq, napi); |
| 1056 | } |
| 1057 | |
| 1058 | static void virtnet_napi_tx_disable(struct napi_struct *napi) |
| 1059 | { |
| 1060 | if (napi->weight) |
| 1061 | napi_disable(napi); |
| 1062 | } |
| 1063 | |
| 1064 | static void refill_work(struct work_struct *work) |
| 1065 | { |
| 1066 | struct virtnet_info *vi = |
| 1067 | container_of(work, struct virtnet_info, refill.work); |
| 1068 | bool still_empty; |
| 1069 | int i; |
| 1070 | |
| 1071 | for (i = 0; i < vi->curr_queue_pairs; i++) { |
| 1072 | struct receive_queue *rq = &vi->rq[i]; |
| 1073 | |
| 1074 | napi_disable(&rq->napi); |
| 1075 | still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); |
| 1076 | virtnet_napi_enable(rq->vq, &rq->napi); |
| 1077 | |
| 1078 | /* In theory, this can happen: if we don't get any buffers in |
| 1079 | * we will *never* try to fill again. |
| 1080 | */ |
| 1081 | if (still_empty) |
| 1082 | schedule_delayed_work(&vi->refill, HZ/2); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | static int virtnet_receive(struct receive_queue *rq, int budget) |
| 1087 | { |
| 1088 | struct virtnet_info *vi = rq->vq->vdev->priv; |
| 1089 | unsigned int len, received = 0, bytes = 0; |
| 1090 | void *buf; |
| 1091 | struct virtnet_stats *stats = this_cpu_ptr(vi->stats); |
| 1092 | |
| 1093 | if (!vi->big_packets || vi->mergeable_rx_bufs) { |
| 1094 | void *ctx; |
| 1095 | |
| 1096 | while (received < budget && |
| 1097 | (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) { |
| 1098 | bytes += receive_buf(vi, rq, buf, len, ctx); |
| 1099 | received++; |
| 1100 | } |
| 1101 | } else { |
| 1102 | while (received < budget && |
| 1103 | (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { |
| 1104 | bytes += receive_buf(vi, rq, buf, len, NULL); |
| 1105 | received++; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { |
| 1110 | if (!try_fill_recv(vi, rq, GFP_ATOMIC)) |
| 1111 | schedule_delayed_work(&vi->refill, 0); |
| 1112 | } |
| 1113 | |
| 1114 | u64_stats_update_begin(&stats->rx_syncp); |
| 1115 | stats->rx_bytes += bytes; |
| 1116 | stats->rx_packets += received; |
| 1117 | u64_stats_update_end(&stats->rx_syncp); |
| 1118 | |
| 1119 | return received; |
| 1120 | } |
| 1121 | |
| 1122 | static void free_old_xmit_skbs(struct send_queue *sq) |
| 1123 | { |
| 1124 | struct sk_buff *skb; |
| 1125 | unsigned int len; |
| 1126 | struct virtnet_info *vi = sq->vq->vdev->priv; |
| 1127 | struct virtnet_stats *stats = this_cpu_ptr(vi->stats); |
| 1128 | unsigned int packets = 0; |
| 1129 | unsigned int bytes = 0; |
| 1130 | |
| 1131 | while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { |
| 1132 | pr_debug("Sent skb %p\n", skb); |
| 1133 | |
| 1134 | bytes += skb->len; |
| 1135 | packets++; |
| 1136 | |
| 1137 | dev_consume_skb_any(skb); |
| 1138 | } |
| 1139 | |
| 1140 | /* Avoid overhead when no packets have been processed |
| 1141 | * happens when called speculatively from start_xmit. |
| 1142 | */ |
| 1143 | if (!packets) |
| 1144 | return; |
| 1145 | |
| 1146 | u64_stats_update_begin(&stats->tx_syncp); |
| 1147 | stats->tx_bytes += bytes; |
| 1148 | stats->tx_packets += packets; |
| 1149 | u64_stats_update_end(&stats->tx_syncp); |
| 1150 | } |
| 1151 | |
| 1152 | static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) |
| 1153 | { |
| 1154 | if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) |
| 1155 | return false; |
| 1156 | else if (q < vi->curr_queue_pairs) |
| 1157 | return true; |
| 1158 | else |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
| 1162 | static void virtnet_poll_cleantx(struct receive_queue *rq) |
| 1163 | { |
| 1164 | struct virtnet_info *vi = rq->vq->vdev->priv; |
| 1165 | unsigned int index = vq2rxq(rq->vq); |
| 1166 | struct send_queue *sq = &vi->sq[index]; |
| 1167 | struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); |
| 1168 | |
| 1169 | if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) |
| 1170 | return; |
| 1171 | |
| 1172 | if (__netif_tx_trylock(txq)) { |
| 1173 | free_old_xmit_skbs(sq); |
| 1174 | __netif_tx_unlock(txq); |
| 1175 | } |
| 1176 | |
| 1177 | if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) |
| 1178 | netif_tx_wake_queue(txq); |
| 1179 | } |
| 1180 | |
| 1181 | static int virtnet_poll(struct napi_struct *napi, int budget) |
| 1182 | { |
| 1183 | struct receive_queue *rq = |
| 1184 | container_of(napi, struct receive_queue, napi); |
| 1185 | unsigned int received; |
| 1186 | |
| 1187 | virtnet_poll_cleantx(rq); |
| 1188 | |
| 1189 | received = virtnet_receive(rq, budget); |
| 1190 | |
| 1191 | /* Out of packets? */ |
| 1192 | if (received < budget) |
| 1193 | virtqueue_napi_complete(napi, rq->vq, received); |
| 1194 | |
| 1195 | return received; |
| 1196 | } |
| 1197 | |
| 1198 | static int virtnet_open(struct net_device *dev) |
| 1199 | { |
| 1200 | struct virtnet_info *vi = netdev_priv(dev); |
| 1201 | int i; |
| 1202 | |
| 1203 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 1204 | if (i < vi->curr_queue_pairs) |
| 1205 | /* Make sure we have some buffers: if oom use wq. */ |
| 1206 | if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) |
| 1207 | schedule_delayed_work(&vi->refill, 0); |
| 1208 | virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); |
| 1209 | virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi); |
| 1210 | } |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static int virtnet_poll_tx(struct napi_struct *napi, int budget) |
| 1216 | { |
| 1217 | struct send_queue *sq = container_of(napi, struct send_queue, napi); |
| 1218 | struct virtnet_info *vi = sq->vq->vdev->priv; |
| 1219 | unsigned int index = vq2txq(sq->vq); |
| 1220 | struct netdev_queue *txq; |
| 1221 | |
| 1222 | if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { |
| 1223 | /* We don't need to enable cb for XDP */ |
| 1224 | napi_complete_done(napi, 0); |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | txq = netdev_get_tx_queue(vi->dev, index); |
| 1229 | __netif_tx_lock(txq, raw_smp_processor_id()); |
| 1230 | free_old_xmit_skbs(sq); |
| 1231 | __netif_tx_unlock(txq); |
| 1232 | |
| 1233 | virtqueue_napi_complete(napi, sq->vq, 0); |
| 1234 | |
| 1235 | if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) |
| 1236 | netif_tx_wake_queue(txq); |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) |
| 1242 | { |
| 1243 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
| 1244 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
| 1245 | struct virtnet_info *vi = sq->vq->vdev->priv; |
| 1246 | int num_sg; |
| 1247 | unsigned hdr_len = vi->hdr_len; |
| 1248 | bool can_push; |
| 1249 | |
| 1250 | pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); |
| 1251 | |
| 1252 | can_push = vi->any_header_sg && |
| 1253 | !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && |
| 1254 | !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; |
| 1255 | /* Even if we can, don't push here yet as this would skew |
| 1256 | * csum_start offset below. */ |
| 1257 | if (can_push) |
| 1258 | hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); |
| 1259 | else |
| 1260 | hdr = skb_vnet_hdr(skb); |
| 1261 | |
| 1262 | if (virtio_net_hdr_from_skb(skb, &hdr->hdr, |
| 1263 | virtio_is_little_endian(vi->vdev), false, |
| 1264 | 0)) |
| 1265 | BUG(); |
| 1266 | |
| 1267 | if (vi->mergeable_rx_bufs) |
| 1268 | hdr->num_buffers = 0; |
| 1269 | |
| 1270 | sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); |
| 1271 | if (can_push) { |
| 1272 | __skb_push(skb, hdr_len); |
| 1273 | num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); |
| 1274 | if (unlikely(num_sg < 0)) |
| 1275 | return num_sg; |
| 1276 | /* Pull header back to avoid skew in tx bytes calculations. */ |
| 1277 | __skb_pull(skb, hdr_len); |
| 1278 | } else { |
| 1279 | sg_set_buf(sq->sg, hdr, hdr_len); |
| 1280 | num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); |
| 1281 | if (unlikely(num_sg < 0)) |
| 1282 | return num_sg; |
| 1283 | num_sg++; |
| 1284 | } |
| 1285 | return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); |
| 1286 | } |
| 1287 | |
| 1288 | static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1289 | { |
| 1290 | struct virtnet_info *vi = netdev_priv(dev); |
| 1291 | int qnum = skb_get_queue_mapping(skb); |
| 1292 | struct send_queue *sq = &vi->sq[qnum]; |
| 1293 | int err; |
| 1294 | struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); |
| 1295 | bool kick = !skb->xmit_more; |
| 1296 | bool use_napi = sq->napi.weight; |
| 1297 | |
| 1298 | /* Free up any pending old buffers before queueing new ones. */ |
| 1299 | free_old_xmit_skbs(sq); |
| 1300 | |
| 1301 | if (use_napi && kick) |
| 1302 | virtqueue_enable_cb_delayed(sq->vq); |
| 1303 | |
| 1304 | /* timestamp packet in software */ |
| 1305 | skb_tx_timestamp(skb); |
| 1306 | |
| 1307 | /* Try to transmit */ |
| 1308 | err = xmit_skb(sq, skb); |
| 1309 | |
| 1310 | /* This should not happen! */ |
| 1311 | if (unlikely(err)) { |
| 1312 | dev->stats.tx_fifo_errors++; |
| 1313 | if (net_ratelimit()) |
| 1314 | dev_warn(&dev->dev, |
| 1315 | "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); |
| 1316 | dev->stats.tx_dropped++; |
| 1317 | dev_kfree_skb_any(skb); |
| 1318 | return NETDEV_TX_OK; |
| 1319 | } |
| 1320 | |
| 1321 | /* Don't wait up for transmitted skbs to be freed. */ |
| 1322 | if (!use_napi) { |
| 1323 | skb_orphan(skb); |
| 1324 | nf_reset(skb); |
| 1325 | } |
| 1326 | |
| 1327 | /* If running out of space, stop queue to avoid getting packets that we |
| 1328 | * are then unable to transmit. |
| 1329 | * An alternative would be to force queuing layer to requeue the skb by |
| 1330 | * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be |
| 1331 | * returned in a normal path of operation: it means that driver is not |
| 1332 | * maintaining the TX queue stop/start state properly, and causes |
| 1333 | * the stack to do a non-trivial amount of useless work. |
| 1334 | * Since most packets only take 1 or 2 ring slots, stopping the queue |
| 1335 | * early means 16 slots are typically wasted. |
| 1336 | */ |
| 1337 | if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { |
| 1338 | netif_stop_subqueue(dev, qnum); |
| 1339 | if (!use_napi && |
| 1340 | unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { |
| 1341 | /* More just got used, free them then recheck. */ |
| 1342 | free_old_xmit_skbs(sq); |
| 1343 | if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { |
| 1344 | netif_start_subqueue(dev, qnum); |
| 1345 | virtqueue_disable_cb(sq->vq); |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | if (kick || netif_xmit_stopped(txq)) |
| 1351 | virtqueue_kick(sq->vq); |
| 1352 | |
| 1353 | return NETDEV_TX_OK; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * Send command via the control virtqueue and check status. Commands |
| 1358 | * supported by the hypervisor, as indicated by feature bits, should |
| 1359 | * never fail unless improperly formatted. |
| 1360 | */ |
| 1361 | static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, |
| 1362 | struct scatterlist *out) |
| 1363 | { |
| 1364 | struct scatterlist *sgs[4], hdr, stat; |
| 1365 | unsigned out_num = 0, tmp; |
| 1366 | |
| 1367 | /* Caller should know better */ |
| 1368 | BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); |
| 1369 | |
| 1370 | vi->ctrl->status = ~0; |
| 1371 | vi->ctrl->hdr.class = class; |
| 1372 | vi->ctrl->hdr.cmd = cmd; |
| 1373 | /* Add header */ |
| 1374 | sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); |
| 1375 | sgs[out_num++] = &hdr; |
| 1376 | |
| 1377 | if (out) |
| 1378 | sgs[out_num++] = out; |
| 1379 | |
| 1380 | /* Add return status. */ |
| 1381 | sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); |
| 1382 | sgs[out_num] = &stat; |
| 1383 | |
| 1384 | BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); |
| 1385 | virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); |
| 1386 | |
| 1387 | if (unlikely(!virtqueue_kick(vi->cvq))) |
| 1388 | return vi->ctrl->status == VIRTIO_NET_OK; |
| 1389 | |
| 1390 | /* Spin for a response, the kick causes an ioport write, trapping |
| 1391 | * into the hypervisor, so the request should be handled immediately. |
| 1392 | */ |
| 1393 | while (!virtqueue_get_buf(vi->cvq, &tmp) && |
| 1394 | !virtqueue_is_broken(vi->cvq)) |
| 1395 | cpu_relax(); |
| 1396 | |
| 1397 | return vi->ctrl->status == VIRTIO_NET_OK; |
| 1398 | } |
| 1399 | |
| 1400 | static int virtnet_set_mac_address(struct net_device *dev, void *p) |
| 1401 | { |
| 1402 | struct virtnet_info *vi = netdev_priv(dev); |
| 1403 | struct virtio_device *vdev = vi->vdev; |
| 1404 | int ret; |
| 1405 | struct sockaddr *addr; |
| 1406 | struct scatterlist sg; |
| 1407 | |
| 1408 | addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); |
| 1409 | if (!addr) |
| 1410 | return -ENOMEM; |
| 1411 | |
| 1412 | ret = eth_prepare_mac_addr_change(dev, addr); |
| 1413 | if (ret) |
| 1414 | goto out; |
| 1415 | |
| 1416 | if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { |
| 1417 | sg_init_one(&sg, addr->sa_data, dev->addr_len); |
| 1418 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, |
| 1419 | VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { |
| 1420 | dev_warn(&vdev->dev, |
| 1421 | "Failed to set mac address by vq command.\n"); |
| 1422 | ret = -EINVAL; |
| 1423 | goto out; |
| 1424 | } |
| 1425 | } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && |
| 1426 | !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
| 1427 | unsigned int i; |
| 1428 | |
| 1429 | /* Naturally, this has an atomicity problem. */ |
| 1430 | for (i = 0; i < dev->addr_len; i++) |
| 1431 | virtio_cwrite8(vdev, |
| 1432 | offsetof(struct virtio_net_config, mac) + |
| 1433 | i, addr->sa_data[i]); |
| 1434 | } |
| 1435 | |
| 1436 | eth_commit_mac_addr_change(dev, p); |
| 1437 | ret = 0; |
| 1438 | |
| 1439 | out: |
| 1440 | kfree(addr); |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
| 1444 | static void virtnet_stats(struct net_device *dev, |
| 1445 | struct rtnl_link_stats64 *tot) |
| 1446 | { |
| 1447 | struct virtnet_info *vi = netdev_priv(dev); |
| 1448 | int cpu; |
| 1449 | unsigned int start; |
| 1450 | |
| 1451 | for_each_possible_cpu(cpu) { |
| 1452 | struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); |
| 1453 | u64 tpackets, tbytes, rpackets, rbytes; |
| 1454 | |
| 1455 | do { |
| 1456 | start = u64_stats_fetch_begin_irq(&stats->tx_syncp); |
| 1457 | tpackets = stats->tx_packets; |
| 1458 | tbytes = stats->tx_bytes; |
| 1459 | } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); |
| 1460 | |
| 1461 | do { |
| 1462 | start = u64_stats_fetch_begin_irq(&stats->rx_syncp); |
| 1463 | rpackets = stats->rx_packets; |
| 1464 | rbytes = stats->rx_bytes; |
| 1465 | } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); |
| 1466 | |
| 1467 | tot->rx_packets += rpackets; |
| 1468 | tot->tx_packets += tpackets; |
| 1469 | tot->rx_bytes += rbytes; |
| 1470 | tot->tx_bytes += tbytes; |
| 1471 | } |
| 1472 | |
| 1473 | tot->tx_dropped = dev->stats.tx_dropped; |
| 1474 | tot->tx_fifo_errors = dev->stats.tx_fifo_errors; |
| 1475 | tot->rx_dropped = dev->stats.rx_dropped; |
| 1476 | tot->rx_length_errors = dev->stats.rx_length_errors; |
| 1477 | tot->rx_frame_errors = dev->stats.rx_frame_errors; |
| 1478 | } |
| 1479 | |
| 1480 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1481 | static void virtnet_netpoll(struct net_device *dev) |
| 1482 | { |
| 1483 | struct virtnet_info *vi = netdev_priv(dev); |
| 1484 | int i; |
| 1485 | |
| 1486 | for (i = 0; i < vi->curr_queue_pairs; i++) |
| 1487 | napi_schedule(&vi->rq[i].napi); |
| 1488 | } |
| 1489 | #endif |
| 1490 | |
| 1491 | static void virtnet_ack_link_announce(struct virtnet_info *vi) |
| 1492 | { |
| 1493 | rtnl_lock(); |
| 1494 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, |
| 1495 | VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) |
| 1496 | dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); |
| 1497 | rtnl_unlock(); |
| 1498 | } |
| 1499 | |
| 1500 | static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) |
| 1501 | { |
| 1502 | struct scatterlist sg; |
| 1503 | struct net_device *dev = vi->dev; |
| 1504 | |
| 1505 | if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) |
| 1506 | return 0; |
| 1507 | |
| 1508 | vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); |
| 1509 | sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); |
| 1510 | |
| 1511 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, |
| 1512 | VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { |
| 1513 | dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", |
| 1514 | queue_pairs); |
| 1515 | return -EINVAL; |
| 1516 | } else { |
| 1517 | vi->curr_queue_pairs = queue_pairs; |
| 1518 | /* virtnet_open() will refill when device is going to up. */ |
| 1519 | if (dev->flags & IFF_UP) |
| 1520 | schedule_delayed_work(&vi->refill, 0); |
| 1521 | } |
| 1522 | |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) |
| 1527 | { |
| 1528 | int err; |
| 1529 | |
| 1530 | rtnl_lock(); |
| 1531 | err = _virtnet_set_queues(vi, queue_pairs); |
| 1532 | rtnl_unlock(); |
| 1533 | return err; |
| 1534 | } |
| 1535 | |
| 1536 | static int virtnet_close(struct net_device *dev) |
| 1537 | { |
| 1538 | struct virtnet_info *vi = netdev_priv(dev); |
| 1539 | int i; |
| 1540 | |
| 1541 | /* Make sure refill_work doesn't re-enable napi! */ |
| 1542 | cancel_delayed_work_sync(&vi->refill); |
| 1543 | |
| 1544 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 1545 | napi_disable(&vi->rq[i].napi); |
| 1546 | virtnet_napi_tx_disable(&vi->sq[i].napi); |
| 1547 | } |
| 1548 | |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | static void virtnet_set_rx_mode(struct net_device *dev) |
| 1553 | { |
| 1554 | struct virtnet_info *vi = netdev_priv(dev); |
| 1555 | struct scatterlist sg[2]; |
| 1556 | struct virtio_net_ctrl_mac *mac_data; |
| 1557 | struct netdev_hw_addr *ha; |
| 1558 | int uc_count; |
| 1559 | int mc_count; |
| 1560 | void *buf; |
| 1561 | int i; |
| 1562 | |
| 1563 | /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ |
| 1564 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) |
| 1565 | return; |
| 1566 | |
| 1567 | vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); |
| 1568 | vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); |
| 1569 | |
| 1570 | sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); |
| 1571 | |
| 1572 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, |
| 1573 | VIRTIO_NET_CTRL_RX_PROMISC, sg)) |
| 1574 | dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", |
| 1575 | vi->ctrl->promisc ? "en" : "dis"); |
| 1576 | |
| 1577 | sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); |
| 1578 | |
| 1579 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, |
| 1580 | VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) |
| 1581 | dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", |
| 1582 | vi->ctrl->allmulti ? "en" : "dis"); |
| 1583 | |
| 1584 | uc_count = netdev_uc_count(dev); |
| 1585 | mc_count = netdev_mc_count(dev); |
| 1586 | /* MAC filter - use one buffer for both lists */ |
| 1587 | buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + |
| 1588 | (2 * sizeof(mac_data->entries)), GFP_ATOMIC); |
| 1589 | mac_data = buf; |
| 1590 | if (!buf) |
| 1591 | return; |
| 1592 | |
| 1593 | sg_init_table(sg, 2); |
| 1594 | |
| 1595 | /* Store the unicast list and count in the front of the buffer */ |
| 1596 | mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); |
| 1597 | i = 0; |
| 1598 | netdev_for_each_uc_addr(ha, dev) |
| 1599 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); |
| 1600 | |
| 1601 | sg_set_buf(&sg[0], mac_data, |
| 1602 | sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); |
| 1603 | |
| 1604 | /* multicast list and count fill the end */ |
| 1605 | mac_data = (void *)&mac_data->macs[uc_count][0]; |
| 1606 | |
| 1607 | mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); |
| 1608 | i = 0; |
| 1609 | netdev_for_each_mc_addr(ha, dev) |
| 1610 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); |
| 1611 | |
| 1612 | sg_set_buf(&sg[1], mac_data, |
| 1613 | sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); |
| 1614 | |
| 1615 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, |
| 1616 | VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) |
| 1617 | dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); |
| 1618 | |
| 1619 | kfree(buf); |
| 1620 | } |
| 1621 | |
| 1622 | static int virtnet_vlan_rx_add_vid(struct net_device *dev, |
| 1623 | __be16 proto, u16 vid) |
| 1624 | { |
| 1625 | struct virtnet_info *vi = netdev_priv(dev); |
| 1626 | struct scatterlist sg; |
| 1627 | |
| 1628 | vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); |
| 1629 | sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); |
| 1630 | |
| 1631 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, |
| 1632 | VIRTIO_NET_CTRL_VLAN_ADD, &sg)) |
| 1633 | dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | static int virtnet_vlan_rx_kill_vid(struct net_device *dev, |
| 1638 | __be16 proto, u16 vid) |
| 1639 | { |
| 1640 | struct virtnet_info *vi = netdev_priv(dev); |
| 1641 | struct scatterlist sg; |
| 1642 | |
| 1643 | vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); |
| 1644 | sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); |
| 1645 | |
| 1646 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, |
| 1647 | VIRTIO_NET_CTRL_VLAN_DEL, &sg)) |
| 1648 | dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
| 1652 | static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) |
| 1653 | { |
| 1654 | int i; |
| 1655 | |
| 1656 | if (vi->affinity_hint_set) { |
| 1657 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 1658 | virtqueue_set_affinity(vi->rq[i].vq, -1); |
| 1659 | virtqueue_set_affinity(vi->sq[i].vq, -1); |
| 1660 | } |
| 1661 | |
| 1662 | vi->affinity_hint_set = false; |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | static void virtnet_set_affinity(struct virtnet_info *vi) |
| 1667 | { |
| 1668 | int i; |
| 1669 | int cpu; |
| 1670 | |
| 1671 | /* In multiqueue mode, when the number of cpu is equal to the number of |
| 1672 | * queue pairs, we let the queue pairs to be private to one cpu by |
| 1673 | * setting the affinity hint to eliminate the contention. |
| 1674 | */ |
| 1675 | if (vi->curr_queue_pairs == 1 || |
| 1676 | vi->max_queue_pairs != num_online_cpus()) { |
| 1677 | virtnet_clean_affinity(vi, -1); |
| 1678 | return; |
| 1679 | } |
| 1680 | |
| 1681 | i = 0; |
| 1682 | for_each_online_cpu(cpu) { |
| 1683 | virtqueue_set_affinity(vi->rq[i].vq, cpu); |
| 1684 | virtqueue_set_affinity(vi->sq[i].vq, cpu); |
| 1685 | netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); |
| 1686 | i++; |
| 1687 | } |
| 1688 | |
| 1689 | vi->affinity_hint_set = true; |
| 1690 | } |
| 1691 | |
| 1692 | static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) |
| 1693 | { |
| 1694 | struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, |
| 1695 | node); |
| 1696 | virtnet_set_affinity(vi); |
| 1697 | return 0; |
| 1698 | } |
| 1699 | |
| 1700 | static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) |
| 1701 | { |
| 1702 | struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, |
| 1703 | node_dead); |
| 1704 | virtnet_set_affinity(vi); |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
| 1708 | static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) |
| 1709 | { |
| 1710 | struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, |
| 1711 | node); |
| 1712 | |
| 1713 | virtnet_clean_affinity(vi, cpu); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | static enum cpuhp_state virtionet_online; |
| 1718 | |
| 1719 | static int virtnet_cpu_notif_add(struct virtnet_info *vi) |
| 1720 | { |
| 1721 | int ret; |
| 1722 | |
| 1723 | ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); |
| 1724 | if (ret) |
| 1725 | return ret; |
| 1726 | ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, |
| 1727 | &vi->node_dead); |
| 1728 | if (!ret) |
| 1729 | return ret; |
| 1730 | cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); |
| 1731 | return ret; |
| 1732 | } |
| 1733 | |
| 1734 | static void virtnet_cpu_notif_remove(struct virtnet_info *vi) |
| 1735 | { |
| 1736 | cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); |
| 1737 | cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, |
| 1738 | &vi->node_dead); |
| 1739 | } |
| 1740 | |
| 1741 | static void virtnet_get_ringparam(struct net_device *dev, |
| 1742 | struct ethtool_ringparam *ring) |
| 1743 | { |
| 1744 | struct virtnet_info *vi = netdev_priv(dev); |
| 1745 | |
| 1746 | ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); |
| 1747 | ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); |
| 1748 | ring->rx_pending = ring->rx_max_pending; |
| 1749 | ring->tx_pending = ring->tx_max_pending; |
| 1750 | } |
| 1751 | |
| 1752 | |
| 1753 | static void virtnet_get_drvinfo(struct net_device *dev, |
| 1754 | struct ethtool_drvinfo *info) |
| 1755 | { |
| 1756 | struct virtnet_info *vi = netdev_priv(dev); |
| 1757 | struct virtio_device *vdev = vi->vdev; |
| 1758 | |
| 1759 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
| 1760 | strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); |
| 1761 | strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); |
| 1762 | |
| 1763 | } |
| 1764 | |
| 1765 | /* TODO: Eliminate OOO packets during switching */ |
| 1766 | static int virtnet_set_channels(struct net_device *dev, |
| 1767 | struct ethtool_channels *channels) |
| 1768 | { |
| 1769 | struct virtnet_info *vi = netdev_priv(dev); |
| 1770 | u16 queue_pairs = channels->combined_count; |
| 1771 | int err; |
| 1772 | |
| 1773 | /* We don't support separate rx/tx channels. |
| 1774 | * We don't allow setting 'other' channels. |
| 1775 | */ |
| 1776 | if (channels->rx_count || channels->tx_count || channels->other_count) |
| 1777 | return -EINVAL; |
| 1778 | |
| 1779 | if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) |
| 1780 | return -EINVAL; |
| 1781 | |
| 1782 | /* For now we don't support modifying channels while XDP is loaded |
| 1783 | * also when XDP is loaded all RX queues have XDP programs so we only |
| 1784 | * need to check a single RX queue. |
| 1785 | */ |
| 1786 | if (vi->rq[0].xdp_prog) |
| 1787 | return -EINVAL; |
| 1788 | |
| 1789 | get_online_cpus(); |
| 1790 | err = _virtnet_set_queues(vi, queue_pairs); |
| 1791 | if (!err) { |
| 1792 | netif_set_real_num_tx_queues(dev, queue_pairs); |
| 1793 | netif_set_real_num_rx_queues(dev, queue_pairs); |
| 1794 | |
| 1795 | virtnet_set_affinity(vi); |
| 1796 | } |
| 1797 | put_online_cpus(); |
| 1798 | |
| 1799 | return err; |
| 1800 | } |
| 1801 | |
| 1802 | static void virtnet_get_channels(struct net_device *dev, |
| 1803 | struct ethtool_channels *channels) |
| 1804 | { |
| 1805 | struct virtnet_info *vi = netdev_priv(dev); |
| 1806 | |
| 1807 | channels->combined_count = vi->curr_queue_pairs; |
| 1808 | channels->max_combined = vi->max_queue_pairs; |
| 1809 | channels->max_other = 0; |
| 1810 | channels->rx_count = 0; |
| 1811 | channels->tx_count = 0; |
| 1812 | channels->other_count = 0; |
| 1813 | } |
| 1814 | |
| 1815 | /* Check if the user is trying to change anything besides speed/duplex */ |
| 1816 | static bool |
| 1817 | virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd) |
| 1818 | { |
| 1819 | struct ethtool_link_ksettings diff1 = *cmd; |
| 1820 | struct ethtool_link_ksettings diff2 = {}; |
| 1821 | |
| 1822 | /* cmd is always set so we need to clear it, validate the port type |
| 1823 | * and also without autonegotiation we can ignore advertising |
| 1824 | */ |
| 1825 | diff1.base.speed = 0; |
| 1826 | diff2.base.port = PORT_OTHER; |
| 1827 | ethtool_link_ksettings_zero_link_mode(&diff1, advertising); |
| 1828 | diff1.base.duplex = 0; |
| 1829 | diff1.base.cmd = 0; |
| 1830 | diff1.base.link_mode_masks_nwords = 0; |
| 1831 | |
| 1832 | return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) && |
| 1833 | bitmap_empty(diff1.link_modes.supported, |
| 1834 | __ETHTOOL_LINK_MODE_MASK_NBITS) && |
| 1835 | bitmap_empty(diff1.link_modes.advertising, |
| 1836 | __ETHTOOL_LINK_MODE_MASK_NBITS) && |
| 1837 | bitmap_empty(diff1.link_modes.lp_advertising, |
| 1838 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 1839 | } |
| 1840 | |
| 1841 | static int virtnet_set_link_ksettings(struct net_device *dev, |
| 1842 | const struct ethtool_link_ksettings *cmd) |
| 1843 | { |
| 1844 | struct virtnet_info *vi = netdev_priv(dev); |
| 1845 | u32 speed; |
| 1846 | |
| 1847 | speed = cmd->base.speed; |
| 1848 | /* don't allow custom speed and duplex */ |
| 1849 | if (!ethtool_validate_speed(speed) || |
| 1850 | !ethtool_validate_duplex(cmd->base.duplex) || |
| 1851 | !virtnet_validate_ethtool_cmd(cmd)) |
| 1852 | return -EINVAL; |
| 1853 | vi->speed = speed; |
| 1854 | vi->duplex = cmd->base.duplex; |
| 1855 | |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
| 1859 | static int virtnet_get_link_ksettings(struct net_device *dev, |
| 1860 | struct ethtool_link_ksettings *cmd) |
| 1861 | { |
| 1862 | struct virtnet_info *vi = netdev_priv(dev); |
| 1863 | |
| 1864 | cmd->base.speed = vi->speed; |
| 1865 | cmd->base.duplex = vi->duplex; |
| 1866 | cmd->base.port = PORT_OTHER; |
| 1867 | |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
| 1871 | static void virtnet_init_settings(struct net_device *dev) |
| 1872 | { |
| 1873 | struct virtnet_info *vi = netdev_priv(dev); |
| 1874 | |
| 1875 | vi->speed = SPEED_UNKNOWN; |
| 1876 | vi->duplex = DUPLEX_UNKNOWN; |
| 1877 | } |
| 1878 | |
| 1879 | static const struct ethtool_ops virtnet_ethtool_ops = { |
| 1880 | .get_drvinfo = virtnet_get_drvinfo, |
| 1881 | .get_link = ethtool_op_get_link, |
| 1882 | .get_ringparam = virtnet_get_ringparam, |
| 1883 | .set_channels = virtnet_set_channels, |
| 1884 | .get_channels = virtnet_get_channels, |
| 1885 | .get_ts_info = ethtool_op_get_ts_info, |
| 1886 | .get_link_ksettings = virtnet_get_link_ksettings, |
| 1887 | .set_link_ksettings = virtnet_set_link_ksettings, |
| 1888 | }; |
| 1889 | |
| 1890 | static void virtnet_freeze_down(struct virtio_device *vdev) |
| 1891 | { |
| 1892 | struct virtnet_info *vi = vdev->priv; |
| 1893 | int i; |
| 1894 | |
| 1895 | /* Make sure no work handler is accessing the device */ |
| 1896 | flush_work(&vi->config_work); |
| 1897 | |
| 1898 | netif_tx_lock_bh(vi->dev); |
| 1899 | netif_device_detach(vi->dev); |
| 1900 | netif_tx_unlock_bh(vi->dev); |
| 1901 | cancel_delayed_work_sync(&vi->refill); |
| 1902 | |
| 1903 | if (netif_running(vi->dev)) { |
| 1904 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 1905 | napi_disable(&vi->rq[i].napi); |
| 1906 | virtnet_napi_tx_disable(&vi->sq[i].napi); |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | static int init_vqs(struct virtnet_info *vi); |
| 1912 | |
| 1913 | static int virtnet_restore_up(struct virtio_device *vdev) |
| 1914 | { |
| 1915 | struct virtnet_info *vi = vdev->priv; |
| 1916 | int err, i; |
| 1917 | |
| 1918 | err = init_vqs(vi); |
| 1919 | if (err) |
| 1920 | return err; |
| 1921 | |
| 1922 | virtio_device_ready(vdev); |
| 1923 | |
| 1924 | if (netif_running(vi->dev)) { |
| 1925 | for (i = 0; i < vi->curr_queue_pairs; i++) |
| 1926 | if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) |
| 1927 | schedule_delayed_work(&vi->refill, 0); |
| 1928 | |
| 1929 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 1930 | virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); |
| 1931 | virtnet_napi_tx_enable(vi, vi->sq[i].vq, |
| 1932 | &vi->sq[i].napi); |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | netif_tx_lock_bh(vi->dev); |
| 1937 | netif_device_attach(vi->dev); |
| 1938 | netif_tx_unlock_bh(vi->dev); |
| 1939 | return err; |
| 1940 | } |
| 1941 | |
| 1942 | static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) |
| 1943 | { |
| 1944 | struct scatterlist sg; |
| 1945 | vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); |
| 1946 | |
| 1947 | sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); |
| 1948 | |
| 1949 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, |
| 1950 | VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { |
| 1951 | dev_warn(&vi->dev->dev, "Fail to set guest offload. \n"); |
| 1952 | return -EINVAL; |
| 1953 | } |
| 1954 | |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | static int virtnet_clear_guest_offloads(struct virtnet_info *vi) |
| 1959 | { |
| 1960 | u64 offloads = 0; |
| 1961 | |
| 1962 | if (!vi->guest_offloads) |
| 1963 | return 0; |
| 1964 | |
| 1965 | return virtnet_set_guest_offloads(vi, offloads); |
| 1966 | } |
| 1967 | |
| 1968 | static int virtnet_restore_guest_offloads(struct virtnet_info *vi) |
| 1969 | { |
| 1970 | u64 offloads = vi->guest_offloads; |
| 1971 | |
| 1972 | if (!vi->guest_offloads) |
| 1973 | return 0; |
| 1974 | |
| 1975 | return virtnet_set_guest_offloads(vi, offloads); |
| 1976 | } |
| 1977 | |
| 1978 | static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, |
| 1979 | struct netlink_ext_ack *extack) |
| 1980 | { |
| 1981 | unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); |
| 1982 | struct virtnet_info *vi = netdev_priv(dev); |
| 1983 | struct bpf_prog *old_prog; |
| 1984 | u16 xdp_qp = 0, curr_qp; |
| 1985 | int i, err; |
| 1986 | |
| 1987 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
| 1988 | && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || |
| 1989 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || |
| 1990 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || |
| 1991 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || |
| 1992 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) { |
| 1993 | NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first"); |
| 1994 | return -EOPNOTSUPP; |
| 1995 | } |
| 1996 | |
| 1997 | if (vi->mergeable_rx_bufs && !vi->any_header_sg) { |
| 1998 | NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required"); |
| 1999 | return -EINVAL; |
| 2000 | } |
| 2001 | |
| 2002 | if (dev->mtu > max_sz) { |
| 2003 | NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP"); |
| 2004 | netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); |
| 2005 | return -EINVAL; |
| 2006 | } |
| 2007 | |
| 2008 | curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; |
| 2009 | if (prog) |
| 2010 | xdp_qp = nr_cpu_ids; |
| 2011 | |
| 2012 | /* XDP requires extra queues for XDP_TX */ |
| 2013 | if (curr_qp + xdp_qp > vi->max_queue_pairs) { |
| 2014 | NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available"); |
| 2015 | netdev_warn(dev, "request %i queues but max is %i\n", |
| 2016 | curr_qp + xdp_qp, vi->max_queue_pairs); |
| 2017 | return -ENOMEM; |
| 2018 | } |
| 2019 | |
| 2020 | if (prog) { |
| 2021 | prog = bpf_prog_add(prog, vi->max_queue_pairs - 1); |
| 2022 | if (IS_ERR(prog)) |
| 2023 | return PTR_ERR(prog); |
| 2024 | } |
| 2025 | |
| 2026 | /* Make sure NAPI is not using any XDP TX queues for RX. */ |
| 2027 | if (netif_running(dev)) { |
| 2028 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2029 | napi_disable(&vi->rq[i].napi); |
| 2030 | virtnet_napi_tx_disable(&vi->sq[i].napi); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | err = _virtnet_set_queues(vi, curr_qp + xdp_qp); |
| 2035 | if (err) |
| 2036 | goto err; |
| 2037 | netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); |
| 2038 | vi->xdp_queue_pairs = xdp_qp; |
| 2039 | |
| 2040 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2041 | old_prog = rtnl_dereference(vi->rq[i].xdp_prog); |
| 2042 | rcu_assign_pointer(vi->rq[i].xdp_prog, prog); |
| 2043 | if (i == 0) { |
| 2044 | if (!old_prog) |
| 2045 | virtnet_clear_guest_offloads(vi); |
| 2046 | if (!prog) |
| 2047 | virtnet_restore_guest_offloads(vi); |
| 2048 | } |
| 2049 | if (old_prog) |
| 2050 | bpf_prog_put(old_prog); |
| 2051 | if (netif_running(dev)) { |
| 2052 | virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); |
| 2053 | virtnet_napi_tx_enable(vi, vi->sq[i].vq, |
| 2054 | &vi->sq[i].napi); |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | return 0; |
| 2059 | |
| 2060 | err: |
| 2061 | if (netif_running(dev)) { |
| 2062 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2063 | virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); |
| 2064 | virtnet_napi_tx_enable(vi, vi->sq[i].vq, |
| 2065 | &vi->sq[i].napi); |
| 2066 | } |
| 2067 | } |
| 2068 | if (prog) |
| 2069 | bpf_prog_sub(prog, vi->max_queue_pairs - 1); |
| 2070 | return err; |
| 2071 | } |
| 2072 | |
| 2073 | static u32 virtnet_xdp_query(struct net_device *dev) |
| 2074 | { |
| 2075 | struct virtnet_info *vi = netdev_priv(dev); |
| 2076 | const struct bpf_prog *xdp_prog; |
| 2077 | int i; |
| 2078 | |
| 2079 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2080 | xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog); |
| 2081 | if (xdp_prog) |
| 2082 | return xdp_prog->aux->id; |
| 2083 | } |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp) |
| 2088 | { |
| 2089 | switch (xdp->command) { |
| 2090 | case XDP_SETUP_PROG: |
| 2091 | return virtnet_xdp_set(dev, xdp->prog, xdp->extack); |
| 2092 | case XDP_QUERY_PROG: |
| 2093 | xdp->prog_id = virtnet_xdp_query(dev); |
| 2094 | xdp->prog_attached = !!xdp->prog_id; |
| 2095 | return 0; |
| 2096 | default: |
| 2097 | return -EINVAL; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | static const struct net_device_ops virtnet_netdev = { |
| 2102 | .ndo_open = virtnet_open, |
| 2103 | .ndo_stop = virtnet_close, |
| 2104 | .ndo_start_xmit = start_xmit, |
| 2105 | .ndo_validate_addr = eth_validate_addr, |
| 2106 | .ndo_set_mac_address = virtnet_set_mac_address, |
| 2107 | .ndo_set_rx_mode = virtnet_set_rx_mode, |
| 2108 | .ndo_get_stats64 = virtnet_stats, |
| 2109 | .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, |
| 2110 | .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, |
| 2111 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2112 | .ndo_poll_controller = virtnet_netpoll, |
| 2113 | #endif |
| 2114 | .ndo_xdp = virtnet_xdp, |
| 2115 | .ndo_features_check = passthru_features_check, |
| 2116 | }; |
| 2117 | |
| 2118 | static void virtnet_config_changed_work(struct work_struct *work) |
| 2119 | { |
| 2120 | struct virtnet_info *vi = |
| 2121 | container_of(work, struct virtnet_info, config_work); |
| 2122 | u16 v; |
| 2123 | |
| 2124 | if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, |
| 2125 | struct virtio_net_config, status, &v) < 0) |
| 2126 | return; |
| 2127 | |
| 2128 | if (v & VIRTIO_NET_S_ANNOUNCE) { |
| 2129 | netdev_notify_peers(vi->dev); |
| 2130 | virtnet_ack_link_announce(vi); |
| 2131 | } |
| 2132 | |
| 2133 | /* Ignore unknown (future) status bits */ |
| 2134 | v &= VIRTIO_NET_S_LINK_UP; |
| 2135 | |
| 2136 | if (vi->status == v) |
| 2137 | return; |
| 2138 | |
| 2139 | vi->status = v; |
| 2140 | |
| 2141 | if (vi->status & VIRTIO_NET_S_LINK_UP) { |
| 2142 | netif_carrier_on(vi->dev); |
| 2143 | netif_tx_wake_all_queues(vi->dev); |
| 2144 | } else { |
| 2145 | netif_carrier_off(vi->dev); |
| 2146 | netif_tx_stop_all_queues(vi->dev); |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | static void virtnet_config_changed(struct virtio_device *vdev) |
| 2151 | { |
| 2152 | struct virtnet_info *vi = vdev->priv; |
| 2153 | |
| 2154 | schedule_work(&vi->config_work); |
| 2155 | } |
| 2156 | |
| 2157 | static void virtnet_free_queues(struct virtnet_info *vi) |
| 2158 | { |
| 2159 | int i; |
| 2160 | |
| 2161 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2162 | napi_hash_del(&vi->rq[i].napi); |
| 2163 | netif_napi_del(&vi->rq[i].napi); |
| 2164 | netif_napi_del(&vi->sq[i].napi); |
| 2165 | } |
| 2166 | |
| 2167 | /* We called napi_hash_del() before netif_napi_del(), |
| 2168 | * we need to respect an RCU grace period before freeing vi->rq |
| 2169 | */ |
| 2170 | synchronize_net(); |
| 2171 | |
| 2172 | kfree(vi->rq); |
| 2173 | kfree(vi->sq); |
| 2174 | kfree(vi->ctrl); |
| 2175 | } |
| 2176 | |
| 2177 | static void _free_receive_bufs(struct virtnet_info *vi) |
| 2178 | { |
| 2179 | struct bpf_prog *old_prog; |
| 2180 | int i; |
| 2181 | |
| 2182 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2183 | while (vi->rq[i].pages) |
| 2184 | __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); |
| 2185 | |
| 2186 | old_prog = rtnl_dereference(vi->rq[i].xdp_prog); |
| 2187 | RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); |
| 2188 | if (old_prog) |
| 2189 | bpf_prog_put(old_prog); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | static void free_receive_bufs(struct virtnet_info *vi) |
| 2194 | { |
| 2195 | rtnl_lock(); |
| 2196 | _free_receive_bufs(vi); |
| 2197 | rtnl_unlock(); |
| 2198 | } |
| 2199 | |
| 2200 | static void free_receive_page_frags(struct virtnet_info *vi) |
| 2201 | { |
| 2202 | int i; |
| 2203 | for (i = 0; i < vi->max_queue_pairs; i++) |
| 2204 | if (vi->rq[i].alloc_frag.page) |
| 2205 | put_page(vi->rq[i].alloc_frag.page); |
| 2206 | } |
| 2207 | |
| 2208 | static void free_unused_bufs(struct virtnet_info *vi) |
| 2209 | { |
| 2210 | void *buf; |
| 2211 | int i; |
| 2212 | |
| 2213 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2214 | struct virtqueue *vq = vi->sq[i].vq; |
| 2215 | while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { |
| 2216 | if (!is_xdp_raw_buffer_queue(vi, i)) |
| 2217 | dev_kfree_skb(buf); |
| 2218 | else |
| 2219 | put_page(virt_to_head_page(buf)); |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2224 | struct virtqueue *vq = vi->rq[i].vq; |
| 2225 | |
| 2226 | while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { |
| 2227 | if (vi->mergeable_rx_bufs) { |
| 2228 | put_page(virt_to_head_page(buf)); |
| 2229 | } else if (vi->big_packets) { |
| 2230 | give_pages(&vi->rq[i], buf); |
| 2231 | } else { |
| 2232 | put_page(virt_to_head_page(buf)); |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | static void virtnet_del_vqs(struct virtnet_info *vi) |
| 2239 | { |
| 2240 | struct virtio_device *vdev = vi->vdev; |
| 2241 | |
| 2242 | virtnet_clean_affinity(vi, -1); |
| 2243 | |
| 2244 | vdev->config->del_vqs(vdev); |
| 2245 | |
| 2246 | virtnet_free_queues(vi); |
| 2247 | } |
| 2248 | |
| 2249 | /* How large should a single buffer be so a queue full of these can fit at |
| 2250 | * least one full packet? |
| 2251 | * Logic below assumes the mergeable buffer header is used. |
| 2252 | */ |
| 2253 | static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) |
| 2254 | { |
| 2255 | const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); |
| 2256 | unsigned int rq_size = virtqueue_get_vring_size(vq); |
| 2257 | unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; |
| 2258 | unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len; |
| 2259 | unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size); |
| 2260 | |
| 2261 | return max(max(min_buf_len, hdr_len) - hdr_len, |
| 2262 | (unsigned int)GOOD_PACKET_LEN); |
| 2263 | } |
| 2264 | |
| 2265 | static int virtnet_find_vqs(struct virtnet_info *vi) |
| 2266 | { |
| 2267 | vq_callback_t **callbacks; |
| 2268 | struct virtqueue **vqs; |
| 2269 | int ret = -ENOMEM; |
| 2270 | int i, total_vqs; |
| 2271 | const char **names; |
| 2272 | bool *ctx; |
| 2273 | |
| 2274 | /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by |
| 2275 | * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by |
| 2276 | * possible control vq. |
| 2277 | */ |
| 2278 | total_vqs = vi->max_queue_pairs * 2 + |
| 2279 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); |
| 2280 | |
| 2281 | /* Allocate space for find_vqs parameters */ |
| 2282 | vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); |
| 2283 | if (!vqs) |
| 2284 | goto err_vq; |
| 2285 | callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); |
| 2286 | if (!callbacks) |
| 2287 | goto err_callback; |
| 2288 | names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); |
| 2289 | if (!names) |
| 2290 | goto err_names; |
| 2291 | if (!vi->big_packets || vi->mergeable_rx_bufs) { |
| 2292 | ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL); |
| 2293 | if (!ctx) |
| 2294 | goto err_ctx; |
| 2295 | } else { |
| 2296 | ctx = NULL; |
| 2297 | } |
| 2298 | |
| 2299 | /* Parameters for control virtqueue, if any */ |
| 2300 | if (vi->has_cvq) { |
| 2301 | callbacks[total_vqs - 1] = NULL; |
| 2302 | names[total_vqs - 1] = "control"; |
| 2303 | } |
| 2304 | |
| 2305 | /* Allocate/initialize parameters for send/receive virtqueues */ |
| 2306 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2307 | callbacks[rxq2vq(i)] = skb_recv_done; |
| 2308 | callbacks[txq2vq(i)] = skb_xmit_done; |
| 2309 | sprintf(vi->rq[i].name, "input.%d", i); |
| 2310 | sprintf(vi->sq[i].name, "output.%d", i); |
| 2311 | names[rxq2vq(i)] = vi->rq[i].name; |
| 2312 | names[txq2vq(i)] = vi->sq[i].name; |
| 2313 | if (ctx) |
| 2314 | ctx[rxq2vq(i)] = true; |
| 2315 | } |
| 2316 | |
| 2317 | ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, |
| 2318 | names, ctx, NULL); |
| 2319 | if (ret) |
| 2320 | goto err_find; |
| 2321 | |
| 2322 | if (vi->has_cvq) { |
| 2323 | vi->cvq = vqs[total_vqs - 1]; |
| 2324 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) |
| 2325 | vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; |
| 2326 | } |
| 2327 | |
| 2328 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2329 | vi->rq[i].vq = vqs[rxq2vq(i)]; |
| 2330 | vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); |
| 2331 | vi->sq[i].vq = vqs[txq2vq(i)]; |
| 2332 | } |
| 2333 | |
| 2334 | kfree(names); |
| 2335 | kfree(callbacks); |
| 2336 | kfree(vqs); |
| 2337 | kfree(ctx); |
| 2338 | |
| 2339 | return 0; |
| 2340 | |
| 2341 | err_find: |
| 2342 | kfree(ctx); |
| 2343 | err_ctx: |
| 2344 | kfree(names); |
| 2345 | err_names: |
| 2346 | kfree(callbacks); |
| 2347 | err_callback: |
| 2348 | kfree(vqs); |
| 2349 | err_vq: |
| 2350 | return ret; |
| 2351 | } |
| 2352 | |
| 2353 | static int virtnet_alloc_queues(struct virtnet_info *vi) |
| 2354 | { |
| 2355 | int i; |
| 2356 | |
| 2357 | vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); |
| 2358 | if (!vi->ctrl) |
| 2359 | goto err_ctrl; |
| 2360 | vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); |
| 2361 | if (!vi->sq) |
| 2362 | goto err_sq; |
| 2363 | vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); |
| 2364 | if (!vi->rq) |
| 2365 | goto err_rq; |
| 2366 | |
| 2367 | INIT_DELAYED_WORK(&vi->refill, refill_work); |
| 2368 | for (i = 0; i < vi->max_queue_pairs; i++) { |
| 2369 | vi->rq[i].pages = NULL; |
| 2370 | netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, |
| 2371 | napi_weight); |
| 2372 | netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx, |
| 2373 | napi_tx ? napi_weight : 0); |
| 2374 | |
| 2375 | sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); |
| 2376 | ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); |
| 2377 | sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); |
| 2378 | } |
| 2379 | |
| 2380 | return 0; |
| 2381 | |
| 2382 | err_rq: |
| 2383 | kfree(vi->sq); |
| 2384 | err_sq: |
| 2385 | kfree(vi->ctrl); |
| 2386 | err_ctrl: |
| 2387 | return -ENOMEM; |
| 2388 | } |
| 2389 | |
| 2390 | static int init_vqs(struct virtnet_info *vi) |
| 2391 | { |
| 2392 | int ret; |
| 2393 | |
| 2394 | /* Allocate send & receive queues */ |
| 2395 | ret = virtnet_alloc_queues(vi); |
| 2396 | if (ret) |
| 2397 | goto err; |
| 2398 | |
| 2399 | ret = virtnet_find_vqs(vi); |
| 2400 | if (ret) |
| 2401 | goto err_free; |
| 2402 | |
| 2403 | get_online_cpus(); |
| 2404 | virtnet_set_affinity(vi); |
| 2405 | put_online_cpus(); |
| 2406 | |
| 2407 | return 0; |
| 2408 | |
| 2409 | err_free: |
| 2410 | virtnet_free_queues(vi); |
| 2411 | err: |
| 2412 | return ret; |
| 2413 | } |
| 2414 | |
| 2415 | #ifdef CONFIG_SYSFS |
| 2416 | static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, |
| 2417 | char *buf) |
| 2418 | { |
| 2419 | struct virtnet_info *vi = netdev_priv(queue->dev); |
| 2420 | unsigned int queue_index = get_netdev_rx_queue_index(queue); |
| 2421 | struct ewma_pkt_len *avg; |
| 2422 | |
| 2423 | BUG_ON(queue_index >= vi->max_queue_pairs); |
| 2424 | avg = &vi->rq[queue_index].mrg_avg_pkt_len; |
| 2425 | return sprintf(buf, "%u\n", |
| 2426 | get_mergeable_buf_len(&vi->rq[queue_index], avg)); |
| 2427 | } |
| 2428 | |
| 2429 | static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = |
| 2430 | __ATTR_RO(mergeable_rx_buffer_size); |
| 2431 | |
| 2432 | static struct attribute *virtio_net_mrg_rx_attrs[] = { |
| 2433 | &mergeable_rx_buffer_size_attribute.attr, |
| 2434 | NULL |
| 2435 | }; |
| 2436 | |
| 2437 | static const struct attribute_group virtio_net_mrg_rx_group = { |
| 2438 | .name = "virtio_net", |
| 2439 | .attrs = virtio_net_mrg_rx_attrs |
| 2440 | }; |
| 2441 | #endif |
| 2442 | |
| 2443 | static bool virtnet_fail_on_feature(struct virtio_device *vdev, |
| 2444 | unsigned int fbit, |
| 2445 | const char *fname, const char *dname) |
| 2446 | { |
| 2447 | if (!virtio_has_feature(vdev, fbit)) |
| 2448 | return false; |
| 2449 | |
| 2450 | dev_err(&vdev->dev, "device advertises feature %s but not %s", |
| 2451 | fname, dname); |
| 2452 | |
| 2453 | return true; |
| 2454 | } |
| 2455 | |
| 2456 | #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ |
| 2457 | virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) |
| 2458 | |
| 2459 | static bool virtnet_validate_features(struct virtio_device *vdev) |
| 2460 | { |
| 2461 | if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && |
| 2462 | (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, |
| 2463 | "VIRTIO_NET_F_CTRL_VQ") || |
| 2464 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, |
| 2465 | "VIRTIO_NET_F_CTRL_VQ") || |
| 2466 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, |
| 2467 | "VIRTIO_NET_F_CTRL_VQ") || |
| 2468 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || |
| 2469 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, |
| 2470 | "VIRTIO_NET_F_CTRL_VQ"))) { |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
| 2474 | return true; |
| 2475 | } |
| 2476 | |
| 2477 | #define MIN_MTU ETH_MIN_MTU |
| 2478 | #define MAX_MTU ETH_MAX_MTU |
| 2479 | |
| 2480 | static int virtnet_validate(struct virtio_device *vdev) |
| 2481 | { |
| 2482 | if (!vdev->config->get) { |
| 2483 | dev_err(&vdev->dev, "%s failure: config access disabled\n", |
| 2484 | __func__); |
| 2485 | return -EINVAL; |
| 2486 | } |
| 2487 | |
| 2488 | if (!virtnet_validate_features(vdev)) |
| 2489 | return -EINVAL; |
| 2490 | |
| 2491 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { |
| 2492 | int mtu = virtio_cread16(vdev, |
| 2493 | offsetof(struct virtio_net_config, |
| 2494 | mtu)); |
| 2495 | if (mtu < MIN_MTU) |
| 2496 | __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); |
| 2497 | } |
| 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
| 2502 | static int virtnet_probe(struct virtio_device *vdev) |
| 2503 | { |
| 2504 | int i, err; |
| 2505 | struct net_device *dev; |
| 2506 | struct virtnet_info *vi; |
| 2507 | u16 max_queue_pairs; |
| 2508 | int mtu; |
| 2509 | |
| 2510 | /* Find if host supports multiqueue virtio_net device */ |
| 2511 | err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, |
| 2512 | struct virtio_net_config, |
| 2513 | max_virtqueue_pairs, &max_queue_pairs); |
| 2514 | |
| 2515 | /* We need at least 2 queue's */ |
| 2516 | if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || |
| 2517 | max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || |
| 2518 | !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) |
| 2519 | max_queue_pairs = 1; |
| 2520 | |
| 2521 | /* Allocate ourselves a network device with room for our info */ |
| 2522 | dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); |
| 2523 | if (!dev) |
| 2524 | return -ENOMEM; |
| 2525 | |
| 2526 | /* Set up network device as normal. */ |
| 2527 | dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; |
| 2528 | dev->netdev_ops = &virtnet_netdev; |
| 2529 | dev->features = NETIF_F_HIGHDMA; |
| 2530 | |
| 2531 | dev->ethtool_ops = &virtnet_ethtool_ops; |
| 2532 | SET_NETDEV_DEV(dev, &vdev->dev); |
| 2533 | |
| 2534 | /* Do we support "hardware" checksums? */ |
| 2535 | if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
| 2536 | /* This opens up the world of extra features. */ |
| 2537 | dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; |
| 2538 | if (csum) |
| 2539 | dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; |
| 2540 | |
| 2541 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
| 2542 | dev->hw_features |= NETIF_F_TSO |
| 2543 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; |
| 2544 | } |
| 2545 | /* Individual feature bits: what can host handle? */ |
| 2546 | if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
| 2547 | dev->hw_features |= NETIF_F_TSO; |
| 2548 | if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
| 2549 | dev->hw_features |= NETIF_F_TSO6; |
| 2550 | if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
| 2551 | dev->hw_features |= NETIF_F_TSO_ECN; |
| 2552 | |
| 2553 | dev->features |= NETIF_F_GSO_ROBUST; |
| 2554 | |
| 2555 | if (gso) |
| 2556 | dev->features |= dev->hw_features & NETIF_F_ALL_TSO; |
| 2557 | /* (!csum && gso) case will be fixed by register_netdev() */ |
| 2558 | } |
| 2559 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) |
| 2560 | dev->features |= NETIF_F_RXCSUM; |
| 2561 | |
| 2562 | dev->vlan_features = dev->features; |
| 2563 | |
| 2564 | /* MTU range: 68 - 65535 */ |
| 2565 | dev->min_mtu = MIN_MTU; |
| 2566 | dev->max_mtu = MAX_MTU; |
| 2567 | |
| 2568 | /* Configuration may specify what MAC to use. Otherwise random. */ |
| 2569 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) |
| 2570 | virtio_cread_bytes(vdev, |
| 2571 | offsetof(struct virtio_net_config, mac), |
| 2572 | dev->dev_addr, dev->addr_len); |
| 2573 | else |
| 2574 | eth_hw_addr_random(dev); |
| 2575 | |
| 2576 | /* Set up our device-specific information */ |
| 2577 | vi = netdev_priv(dev); |
| 2578 | vi->dev = dev; |
| 2579 | vi->vdev = vdev; |
| 2580 | vdev->priv = vi; |
| 2581 | vi->stats = alloc_percpu(struct virtnet_stats); |
| 2582 | err = -ENOMEM; |
| 2583 | if (vi->stats == NULL) |
| 2584 | goto free; |
| 2585 | |
| 2586 | for_each_possible_cpu(i) { |
| 2587 | struct virtnet_stats *virtnet_stats; |
| 2588 | virtnet_stats = per_cpu_ptr(vi->stats, i); |
| 2589 | u64_stats_init(&virtnet_stats->tx_syncp); |
| 2590 | u64_stats_init(&virtnet_stats->rx_syncp); |
| 2591 | } |
| 2592 | |
| 2593 | INIT_WORK(&vi->config_work, virtnet_config_changed_work); |
| 2594 | |
| 2595 | /* If we can receive ANY GSO packets, we must allocate large ones. */ |
| 2596 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || |
| 2597 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || |
| 2598 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || |
| 2599 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) |
| 2600 | vi->big_packets = true; |
| 2601 | |
| 2602 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) |
| 2603 | vi->mergeable_rx_bufs = true; |
| 2604 | |
| 2605 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || |
| 2606 | virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) |
| 2607 | vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); |
| 2608 | else |
| 2609 | vi->hdr_len = sizeof(struct virtio_net_hdr); |
| 2610 | |
| 2611 | if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || |
| 2612 | virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) |
| 2613 | vi->any_header_sg = true; |
| 2614 | |
| 2615 | if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) |
| 2616 | vi->has_cvq = true; |
| 2617 | |
| 2618 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { |
| 2619 | mtu = virtio_cread16(vdev, |
| 2620 | offsetof(struct virtio_net_config, |
| 2621 | mtu)); |
| 2622 | if (mtu < dev->min_mtu) { |
| 2623 | /* Should never trigger: MTU was previously validated |
| 2624 | * in virtnet_validate. |
| 2625 | */ |
| 2626 | dev_err(&vdev->dev, "device MTU appears to have changed " |
| 2627 | "it is now %d < %d", mtu, dev->min_mtu); |
| 2628 | goto free_stats; |
| 2629 | } |
| 2630 | |
| 2631 | dev->mtu = mtu; |
| 2632 | dev->max_mtu = mtu; |
| 2633 | |
| 2634 | /* TODO: size buffers correctly in this case. */ |
| 2635 | if (dev->mtu > ETH_DATA_LEN) |
| 2636 | vi->big_packets = true; |
| 2637 | } |
| 2638 | |
| 2639 | if (vi->any_header_sg) |
| 2640 | dev->needed_headroom = vi->hdr_len; |
| 2641 | |
| 2642 | /* Enable multiqueue by default */ |
| 2643 | if (num_online_cpus() >= max_queue_pairs) |
| 2644 | vi->curr_queue_pairs = max_queue_pairs; |
| 2645 | else |
| 2646 | vi->curr_queue_pairs = num_online_cpus(); |
| 2647 | vi->max_queue_pairs = max_queue_pairs; |
| 2648 | |
| 2649 | /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ |
| 2650 | err = init_vqs(vi); |
| 2651 | if (err) |
| 2652 | goto free_stats; |
| 2653 | |
| 2654 | #ifdef CONFIG_SYSFS |
| 2655 | if (vi->mergeable_rx_bufs) |
| 2656 | dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; |
| 2657 | #endif |
| 2658 | netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); |
| 2659 | netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); |
| 2660 | |
| 2661 | virtnet_init_settings(dev); |
| 2662 | |
| 2663 | err = register_netdev(dev); |
| 2664 | if (err) { |
| 2665 | pr_debug("virtio_net: registering device failed\n"); |
| 2666 | goto free_vqs; |
| 2667 | } |
| 2668 | |
| 2669 | virtio_device_ready(vdev); |
| 2670 | |
| 2671 | err = virtnet_cpu_notif_add(vi); |
| 2672 | if (err) { |
| 2673 | pr_debug("virtio_net: registering cpu notifier failed\n"); |
| 2674 | goto free_unregister_netdev; |
| 2675 | } |
| 2676 | |
| 2677 | virtnet_set_queues(vi, vi->curr_queue_pairs); |
| 2678 | |
| 2679 | /* Assume link up if device can't report link status, |
| 2680 | otherwise get link status from config. */ |
| 2681 | netif_carrier_off(dev); |
| 2682 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { |
| 2683 | schedule_work(&vi->config_work); |
| 2684 | } else { |
| 2685 | vi->status = VIRTIO_NET_S_LINK_UP; |
| 2686 | netif_carrier_on(dev); |
| 2687 | } |
| 2688 | |
| 2689 | for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) |
| 2690 | if (virtio_has_feature(vi->vdev, guest_offloads[i])) |
| 2691 | set_bit(guest_offloads[i], &vi->guest_offloads); |
| 2692 | |
| 2693 | pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", |
| 2694 | dev->name, max_queue_pairs); |
| 2695 | |
| 2696 | return 0; |
| 2697 | |
| 2698 | free_unregister_netdev: |
| 2699 | vi->vdev->config->reset(vdev); |
| 2700 | |
| 2701 | unregister_netdev(dev); |
| 2702 | free_vqs: |
| 2703 | cancel_delayed_work_sync(&vi->refill); |
| 2704 | free_receive_page_frags(vi); |
| 2705 | virtnet_del_vqs(vi); |
| 2706 | free_stats: |
| 2707 | free_percpu(vi->stats); |
| 2708 | free: |
| 2709 | free_netdev(dev); |
| 2710 | return err; |
| 2711 | } |
| 2712 | |
| 2713 | static void remove_vq_common(struct virtnet_info *vi) |
| 2714 | { |
| 2715 | vi->vdev->config->reset(vi->vdev); |
| 2716 | |
| 2717 | /* Free unused buffers in both send and recv, if any. */ |
| 2718 | free_unused_bufs(vi); |
| 2719 | |
| 2720 | free_receive_bufs(vi); |
| 2721 | |
| 2722 | free_receive_page_frags(vi); |
| 2723 | |
| 2724 | virtnet_del_vqs(vi); |
| 2725 | } |
| 2726 | |
| 2727 | static void virtnet_remove(struct virtio_device *vdev) |
| 2728 | { |
| 2729 | struct virtnet_info *vi = vdev->priv; |
| 2730 | |
| 2731 | virtnet_cpu_notif_remove(vi); |
| 2732 | |
| 2733 | /* Make sure no work handler is accessing the device. */ |
| 2734 | flush_work(&vi->config_work); |
| 2735 | |
| 2736 | unregister_netdev(vi->dev); |
| 2737 | |
| 2738 | remove_vq_common(vi); |
| 2739 | |
| 2740 | free_percpu(vi->stats); |
| 2741 | free_netdev(vi->dev); |
| 2742 | } |
| 2743 | |
| 2744 | static __maybe_unused int virtnet_freeze(struct virtio_device *vdev) |
| 2745 | { |
| 2746 | struct virtnet_info *vi = vdev->priv; |
| 2747 | |
| 2748 | virtnet_cpu_notif_remove(vi); |
| 2749 | virtnet_freeze_down(vdev); |
| 2750 | remove_vq_common(vi); |
| 2751 | |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
| 2755 | static __maybe_unused int virtnet_restore(struct virtio_device *vdev) |
| 2756 | { |
| 2757 | struct virtnet_info *vi = vdev->priv; |
| 2758 | int err; |
| 2759 | |
| 2760 | err = virtnet_restore_up(vdev); |
| 2761 | if (err) |
| 2762 | return err; |
| 2763 | virtnet_set_queues(vi, vi->curr_queue_pairs); |
| 2764 | |
| 2765 | err = virtnet_cpu_notif_add(vi); |
| 2766 | if (err) |
| 2767 | return err; |
| 2768 | |
| 2769 | return 0; |
| 2770 | } |
| 2771 | |
| 2772 | static struct virtio_device_id id_table[] = { |
| 2773 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, |
| 2774 | { 0 }, |
| 2775 | }; |
| 2776 | |
| 2777 | #define VIRTNET_FEATURES \ |
| 2778 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ |
| 2779 | VIRTIO_NET_F_MAC, \ |
| 2780 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ |
| 2781 | VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ |
| 2782 | VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ |
| 2783 | VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ |
| 2784 | VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ |
| 2785 | VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ |
| 2786 | VIRTIO_NET_F_CTRL_MAC_ADDR, \ |
| 2787 | VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS |
| 2788 | |
| 2789 | static unsigned int features[] = { |
| 2790 | VIRTNET_FEATURES, |
| 2791 | }; |
| 2792 | |
| 2793 | static unsigned int features_legacy[] = { |
| 2794 | VIRTNET_FEATURES, |
| 2795 | VIRTIO_NET_F_GSO, |
| 2796 | VIRTIO_F_ANY_LAYOUT, |
| 2797 | }; |
| 2798 | |
| 2799 | static struct virtio_driver virtio_net_driver = { |
| 2800 | .feature_table = features, |
| 2801 | .feature_table_size = ARRAY_SIZE(features), |
| 2802 | .feature_table_legacy = features_legacy, |
| 2803 | .feature_table_size_legacy = ARRAY_SIZE(features_legacy), |
| 2804 | .driver.name = KBUILD_MODNAME, |
| 2805 | .driver.owner = THIS_MODULE, |
| 2806 | .id_table = id_table, |
| 2807 | .validate = virtnet_validate, |
| 2808 | .probe = virtnet_probe, |
| 2809 | .remove = virtnet_remove, |
| 2810 | .config_changed = virtnet_config_changed, |
| 2811 | #ifdef CONFIG_PM_SLEEP |
| 2812 | .freeze = virtnet_freeze, |
| 2813 | .restore = virtnet_restore, |
| 2814 | #endif |
| 2815 | }; |
| 2816 | |
| 2817 | static __init int virtio_net_driver_init(void) |
| 2818 | { |
| 2819 | int ret; |
| 2820 | |
| 2821 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", |
| 2822 | virtnet_cpu_online, |
| 2823 | virtnet_cpu_down_prep); |
| 2824 | if (ret < 0) |
| 2825 | goto out; |
| 2826 | virtionet_online = ret; |
| 2827 | ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", |
| 2828 | NULL, virtnet_cpu_dead); |
| 2829 | if (ret) |
| 2830 | goto err_dead; |
| 2831 | |
| 2832 | ret = register_virtio_driver(&virtio_net_driver); |
| 2833 | if (ret) |
| 2834 | goto err_virtio; |
| 2835 | return 0; |
| 2836 | err_virtio: |
| 2837 | cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); |
| 2838 | err_dead: |
| 2839 | cpuhp_remove_multi_state(virtionet_online); |
| 2840 | out: |
| 2841 | return ret; |
| 2842 | } |
| 2843 | module_init(virtio_net_driver_init); |
| 2844 | |
| 2845 | static __exit void virtio_net_driver_exit(void) |
| 2846 | { |
| 2847 | unregister_virtio_driver(&virtio_net_driver); |
| 2848 | cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); |
| 2849 | cpuhp_remove_multi_state(virtionet_online); |
| 2850 | } |
| 2851 | module_exit(virtio_net_driver_exit); |
| 2852 | |
| 2853 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 2854 | MODULE_DESCRIPTION("Virtio network driver"); |
| 2855 | MODULE_LICENSE("GPL"); |