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