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