]>
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> |
296f96fc | 32 | |
d34710e3 | 33 | static int napi_weight = NAPI_POLL_WEIGHT; |
6c0cd7c0 DL |
34 | module_param(napi_weight, int, 0444); |
35 | ||
eb939922 | 36 | static bool csum = true, gso = true; |
34a48579 RR |
37 | module_param(csum, bool, 0444); |
38 | module_param(gso, bool, 0444); | |
39 | ||
296f96fc | 40 | /* FIXME: MTU in config. */ |
5061de36 | 41 | #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) |
3f2c31d9 | 42 | #define GOOD_COPY_LEN 128 |
296f96fc | 43 | |
5377d758 JB |
44 | /* RX packet size EWMA. The average packet size is used to determine the packet |
45 | * buffer size when refilling RX rings. As the entire RX ring may be refilled | |
46 | * at once, the weight is chosen so that the EWMA will be insensitive to short- | |
47 | * term, transient changes in packet size. | |
ab7db917 | 48 | */ |
5377d758 | 49 | DECLARE_EWMA(pkt_len, 1, 64) |
ab7db917 | 50 | |
d0fa28f0 MT |
51 | /* With mergeable buffers we align buffer address and use the low bits to |
52 | * encode its true size. Buffer size is up to 1 page so we need to align to | |
53 | * square root of page size to ensure we reserve enough bits to encode the true | |
54 | * size. | |
55 | */ | |
56 | #define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2) | |
57 | ||
ab7db917 | 58 | /* Minimum alignment for mergeable packet buffers. */ |
d0fa28f0 MT |
59 | #define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \ |
60 | 1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT) | |
ab7db917 | 61 | |
66846048 | 62 | #define VIRTNET_DRIVER_VERSION "1.0.0" |
2a41f71d | 63 | |
3fa2a1df | 64 | struct virtnet_stats { |
83a27052 ED |
65 | struct u64_stats_sync tx_syncp; |
66 | struct u64_stats_sync rx_syncp; | |
3fa2a1df | 67 | u64 tx_bytes; |
68 | u64 tx_packets; | |
69 | ||
70 | u64 rx_bytes; | |
71 | u64 rx_packets; | |
72 | }; | |
73 | ||
e9d7417b JW |
74 | /* Internal representation of a send virtqueue */ |
75 | struct send_queue { | |
76 | /* Virtqueue associated with this send _queue */ | |
77 | struct virtqueue *vq; | |
78 | ||
79 | /* TX: fragments + linear part + virtio header */ | |
80 | struct scatterlist sg[MAX_SKB_FRAGS + 2]; | |
986a4f4d JW |
81 | |
82 | /* Name of the send queue: output.$index */ | |
83 | char name[40]; | |
e9d7417b JW |
84 | }; |
85 | ||
86 | /* Internal representation of a receive virtqueue */ | |
87 | struct receive_queue { | |
88 | /* Virtqueue associated with this receive_queue */ | |
89 | struct virtqueue *vq; | |
90 | ||
296f96fc RR |
91 | struct napi_struct napi; |
92 | ||
f600b690 JF |
93 | struct bpf_prog __rcu *xdp_prog; |
94 | ||
e9d7417b JW |
95 | /* Chain pages by the private ptr. */ |
96 | struct page *pages; | |
97 | ||
ab7db917 | 98 | /* Average packet length for mergeable receive buffers. */ |
5377d758 | 99 | struct ewma_pkt_len mrg_avg_pkt_len; |
ab7db917 | 100 | |
fb51879d MD |
101 | /* Page frag for packet buffer allocation. */ |
102 | struct page_frag alloc_frag; | |
103 | ||
e9d7417b JW |
104 | /* RX: fragments + linear part + virtio header */ |
105 | struct scatterlist sg[MAX_SKB_FRAGS + 2]; | |
986a4f4d JW |
106 | |
107 | /* Name of this receive queue: input.$index */ | |
108 | char name[40]; | |
e9d7417b JW |
109 | }; |
110 | ||
111 | struct virtnet_info { | |
112 | struct virtio_device *vdev; | |
113 | struct virtqueue *cvq; | |
114 | struct net_device *dev; | |
986a4f4d JW |
115 | struct send_queue *sq; |
116 | struct receive_queue *rq; | |
e9d7417b JW |
117 | unsigned int status; |
118 | ||
986a4f4d JW |
119 | /* Max # of queue pairs supported by the device */ |
120 | u16 max_queue_pairs; | |
121 | ||
122 | /* # of queue pairs currently used by the driver */ | |
123 | u16 curr_queue_pairs; | |
124 | ||
672aafd5 JF |
125 | /* # of XDP queue pairs currently used by the driver */ |
126 | u16 xdp_queue_pairs; | |
127 | ||
97402b96 HX |
128 | /* I like... big packets and I cannot lie! */ |
129 | bool big_packets; | |
130 | ||
3f2c31d9 MM |
131 | /* Host will merge rx buffers for big packets (shake it! shake it!) */ |
132 | bool mergeable_rx_bufs; | |
133 | ||
986a4f4d JW |
134 | /* Has control virtqueue */ |
135 | bool has_cvq; | |
136 | ||
e7428e95 MT |
137 | /* Host can handle any s/g split between our header and packet data */ |
138 | bool any_header_sg; | |
139 | ||
012873d0 MT |
140 | /* Packet virtio header size */ |
141 | u8 hdr_len; | |
142 | ||
3fa2a1df | 143 | /* Active statistics */ |
144 | struct virtnet_stats __percpu *stats; | |
145 | ||
3161e453 RR |
146 | /* Work struct for refilling if we run low on memory. */ |
147 | struct delayed_work refill; | |
148 | ||
586d17c5 JW |
149 | /* Work struct for config space updates */ |
150 | struct work_struct config_work; | |
151 | ||
986a4f4d JW |
152 | /* Does the affinity hint is set for virtqueues? */ |
153 | bool affinity_hint_set; | |
47be2479 | 154 | |
8017c279 SAS |
155 | /* CPU hotplug instances for online & dead */ |
156 | struct hlist_node node; | |
157 | struct hlist_node node_dead; | |
2ac46030 MT |
158 | |
159 | /* Control VQ buffers: protected by the rtnl lock */ | |
160 | struct virtio_net_ctrl_hdr ctrl_hdr; | |
161 | virtio_net_ctrl_ack ctrl_status; | |
a725ee3e | 162 | struct virtio_net_ctrl_mq ctrl_mq; |
2ac46030 MT |
163 | u8 ctrl_promisc; |
164 | u8 ctrl_allmulti; | |
a725ee3e | 165 | u16 ctrl_vid; |
16032be5 NA |
166 | |
167 | /* Ethtool settings */ | |
168 | u8 duplex; | |
169 | u32 speed; | |
296f96fc RR |
170 | }; |
171 | ||
9ab86bbc | 172 | struct padded_vnet_hdr { |
012873d0 | 173 | struct virtio_net_hdr_mrg_rxbuf hdr; |
9ab86bbc | 174 | /* |
012873d0 MT |
175 | * hdr is in a separate sg buffer, and data sg buffer shares same page |
176 | * with this header sg. This padding makes next sg 16 byte aligned | |
177 | * after the header. | |
9ab86bbc | 178 | */ |
012873d0 | 179 | char padding[4]; |
9ab86bbc SM |
180 | }; |
181 | ||
986a4f4d JW |
182 | /* Converting between virtqueue no. and kernel tx/rx queue no. |
183 | * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq | |
184 | */ | |
185 | static int vq2txq(struct virtqueue *vq) | |
186 | { | |
9d0ca6ed | 187 | return (vq->index - 1) / 2; |
986a4f4d JW |
188 | } |
189 | ||
190 | static int txq2vq(int txq) | |
191 | { | |
192 | return txq * 2 + 1; | |
193 | } | |
194 | ||
195 | static int vq2rxq(struct virtqueue *vq) | |
196 | { | |
9d0ca6ed | 197 | return vq->index / 2; |
986a4f4d JW |
198 | } |
199 | ||
200 | static int rxq2vq(int rxq) | |
201 | { | |
202 | return rxq * 2; | |
203 | } | |
204 | ||
012873d0 | 205 | static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) |
296f96fc | 206 | { |
012873d0 | 207 | return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; |
296f96fc RR |
208 | } |
209 | ||
9ab86bbc SM |
210 | /* |
211 | * private is used to chain pages for big packets, put the whole | |
212 | * most recent used list in the beginning for reuse | |
213 | */ | |
e9d7417b | 214 | static void give_pages(struct receive_queue *rq, struct page *page) |
0a888fd1 | 215 | { |
9ab86bbc | 216 | struct page *end; |
0a888fd1 | 217 | |
e9d7417b | 218 | /* Find end of list, sew whole thing into vi->rq.pages. */ |
9ab86bbc | 219 | for (end = page; end->private; end = (struct page *)end->private); |
e9d7417b JW |
220 | end->private = (unsigned long)rq->pages; |
221 | rq->pages = page; | |
0a888fd1 MM |
222 | } |
223 | ||
e9d7417b | 224 | static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) |
fb6813f4 | 225 | { |
e9d7417b | 226 | struct page *p = rq->pages; |
fb6813f4 | 227 | |
9ab86bbc | 228 | if (p) { |
e9d7417b | 229 | rq->pages = (struct page *)p->private; |
9ab86bbc SM |
230 | /* clear private here, it is used to chain pages */ |
231 | p->private = 0; | |
232 | } else | |
fb6813f4 RR |
233 | p = alloc_page(gfp_mask); |
234 | return p; | |
235 | } | |
236 | ||
e9d7417b | 237 | static void skb_xmit_done(struct virtqueue *vq) |
296f96fc | 238 | { |
e9d7417b | 239 | struct virtnet_info *vi = vq->vdev->priv; |
296f96fc | 240 | |
2cb9c6ba | 241 | /* Suppress further interrupts. */ |
e9d7417b | 242 | virtqueue_disable_cb(vq); |
11a3a154 | 243 | |
363f1514 | 244 | /* We were probably waiting for more output buffers. */ |
986a4f4d | 245 | netif_wake_subqueue(vi->dev, vq2txq(vq)); |
296f96fc RR |
246 | } |
247 | ||
ab7db917 MD |
248 | static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx) |
249 | { | |
250 | unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1); | |
251 | return (truesize + 1) * MERGEABLE_BUFFER_ALIGN; | |
252 | } | |
253 | ||
254 | static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx) | |
255 | { | |
256 | return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN); | |
257 | ||
258 | } | |
259 | ||
260 | static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize) | |
261 | { | |
262 | unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN; | |
263 | return (unsigned long)buf | (size - 1); | |
264 | } | |
265 | ||
3464645a | 266 | /* Called from bottom half context */ |
946fa564 MT |
267 | static struct sk_buff *page_to_skb(struct virtnet_info *vi, |
268 | struct receive_queue *rq, | |
2613af0e MD |
269 | struct page *page, unsigned int offset, |
270 | unsigned int len, unsigned int truesize) | |
9ab86bbc SM |
271 | { |
272 | struct sk_buff *skb; | |
012873d0 | 273 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
2613af0e | 274 | unsigned int copy, hdr_len, hdr_padded_len; |
9ab86bbc | 275 | char *p; |
fb6813f4 | 276 | |
2613af0e | 277 | p = page_address(page) + offset; |
3f2c31d9 | 278 | |
9ab86bbc | 279 | /* copy small packet so we can reuse these pages for small data */ |
c67f5db8 | 280 | skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); |
9ab86bbc SM |
281 | if (unlikely(!skb)) |
282 | return NULL; | |
3f2c31d9 | 283 | |
9ab86bbc | 284 | hdr = skb_vnet_hdr(skb); |
3f2c31d9 | 285 | |
012873d0 MT |
286 | hdr_len = vi->hdr_len; |
287 | if (vi->mergeable_rx_bufs) | |
288 | hdr_padded_len = sizeof *hdr; | |
289 | else | |
2613af0e | 290 | hdr_padded_len = sizeof(struct padded_vnet_hdr); |
3f2c31d9 | 291 | |
9ab86bbc | 292 | memcpy(hdr, p, hdr_len); |
3f2c31d9 | 293 | |
9ab86bbc | 294 | len -= hdr_len; |
2613af0e MD |
295 | offset += hdr_padded_len; |
296 | p += hdr_padded_len; | |
3f2c31d9 | 297 | |
9ab86bbc SM |
298 | copy = len; |
299 | if (copy > skb_tailroom(skb)) | |
300 | copy = skb_tailroom(skb); | |
301 | memcpy(skb_put(skb, copy), p, copy); | |
3f2c31d9 | 302 | |
9ab86bbc SM |
303 | len -= copy; |
304 | offset += copy; | |
3f2c31d9 | 305 | |
2613af0e MD |
306 | if (vi->mergeable_rx_bufs) { |
307 | if (len) | |
308 | skb_add_rx_frag(skb, 0, page, offset, len, truesize); | |
309 | else | |
310 | put_page(page); | |
311 | return skb; | |
312 | } | |
313 | ||
e878d78b SL |
314 | /* |
315 | * Verify that we can indeed put this data into a skb. | |
316 | * This is here to handle cases when the device erroneously | |
317 | * tries to receive more than is possible. This is usually | |
318 | * the case of a broken device. | |
319 | */ | |
320 | if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { | |
be443899 | 321 | net_dbg_ratelimited("%s: too much data\n", skb->dev->name); |
e878d78b SL |
322 | dev_kfree_skb(skb); |
323 | return NULL; | |
324 | } | |
2613af0e | 325 | BUG_ON(offset >= PAGE_SIZE); |
9ab86bbc | 326 | while (len) { |
2613af0e MD |
327 | unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); |
328 | skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, | |
329 | frag_size, truesize); | |
330 | len -= frag_size; | |
9ab86bbc SM |
331 | page = (struct page *)page->private; |
332 | offset = 0; | |
333 | } | |
3f2c31d9 | 334 | |
9ab86bbc | 335 | if (page) |
e9d7417b | 336 | give_pages(rq, page); |
3f2c31d9 | 337 | |
9ab86bbc SM |
338 | return skb; |
339 | } | |
3f2c31d9 | 340 | |
a67edbf4 | 341 | static bool virtnet_xdp_xmit(struct virtnet_info *vi, |
56434a01 | 342 | struct receive_queue *rq, |
bb91accf JW |
343 | struct xdp_buff *xdp, |
344 | void *data) | |
56434a01 | 345 | { |
56434a01 JF |
346 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
347 | unsigned int num_sg, len; | |
722d8283 JF |
348 | struct send_queue *sq; |
349 | unsigned int qp; | |
56434a01 JF |
350 | void *xdp_sent; |
351 | int err; | |
352 | ||
722d8283 JF |
353 | qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); |
354 | sq = &vi->sq[qp]; | |
355 | ||
56434a01 JF |
356 | /* Free up any pending old buffers before queueing new ones. */ |
357 | while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) { | |
bb91accf JW |
358 | if (vi->mergeable_rx_bufs) { |
359 | struct page *sent_page = virt_to_head_page(xdp_sent); | |
360 | ||
361 | put_page(sent_page); | |
362 | } else { /* small buffer */ | |
363 | struct sk_buff *skb = xdp_sent; | |
56434a01 | 364 | |
bb91accf JW |
365 | kfree_skb(skb); |
366 | } | |
367 | } | |
56434a01 | 368 | |
bb91accf JW |
369 | if (vi->mergeable_rx_bufs) { |
370 | /* Zero header and leave csum up to XDP layers */ | |
371 | hdr = xdp->data; | |
372 | memset(hdr, 0, vi->hdr_len); | |
373 | ||
374 | num_sg = 1; | |
375 | sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data); | |
376 | } else { /* small buffer */ | |
377 | struct sk_buff *skb = data; | |
378 | ||
379 | /* Zero header and leave csum up to XDP layers */ | |
380 | hdr = skb_vnet_hdr(skb); | |
381 | memset(hdr, 0, vi->hdr_len); | |
382 | ||
383 | num_sg = 2; | |
384 | sg_init_table(sq->sg, 2); | |
385 | sg_set_buf(sq->sg, hdr, vi->hdr_len); | |
386 | skb_to_sgvec(skb, sq->sg + 1, 0, skb->len); | |
387 | } | |
56434a01 | 388 | err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, |
bb91accf | 389 | data, GFP_ATOMIC); |
56434a01 | 390 | if (unlikely(err)) { |
bb91accf JW |
391 | if (vi->mergeable_rx_bufs) { |
392 | struct page *page = virt_to_head_page(xdp->data); | |
393 | ||
394 | put_page(page); | |
395 | } else /* small buffer */ | |
396 | kfree_skb(data); | |
a67edbf4 DB |
397 | /* On error abort to avoid unnecessary kick */ |
398 | return false; | |
56434a01 JF |
399 | } |
400 | ||
401 | virtqueue_kick(sq->vq); | |
a67edbf4 | 402 | return true; |
56434a01 JF |
403 | } |
404 | ||
bb91accf JW |
405 | static struct sk_buff *receive_small(struct net_device *dev, |
406 | struct virtnet_info *vi, | |
407 | struct receive_queue *rq, | |
408 | void *buf, unsigned int len) | |
f121159d MT |
409 | { |
410 | struct sk_buff * skb = buf; | |
bb91accf | 411 | struct bpf_prog *xdp_prog; |
f121159d | 412 | |
012873d0 | 413 | len -= vi->hdr_len; |
f121159d MT |
414 | skb_trim(skb, len); |
415 | ||
bb91accf JW |
416 | rcu_read_lock(); |
417 | xdp_prog = rcu_dereference(rq->xdp_prog); | |
418 | if (xdp_prog) { | |
419 | struct virtio_net_hdr_mrg_rxbuf *hdr = buf; | |
0354e4d1 | 420 | struct xdp_buff xdp; |
bb91accf JW |
421 | u32 act; |
422 | ||
423 | if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags)) | |
424 | goto err_xdp; | |
0354e4d1 JF |
425 | |
426 | xdp.data = skb->data; | |
427 | xdp.data_end = xdp.data + len; | |
428 | act = bpf_prog_run_xdp(xdp_prog, &xdp); | |
429 | ||
bb91accf JW |
430 | switch (act) { |
431 | case XDP_PASS: | |
432 | break; | |
433 | case XDP_TX: | |
722d8283 | 434 | if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp, skb))) |
0354e4d1 | 435 | trace_xdp_exception(vi->dev, xdp_prog, act); |
bb91accf JW |
436 | rcu_read_unlock(); |
437 | goto xdp_xmit; | |
bb91accf | 438 | default: |
0354e4d1 JF |
439 | bpf_warn_invalid_xdp_action(act); |
440 | case XDP_ABORTED: | |
441 | trace_xdp_exception(vi->dev, xdp_prog, act); | |
442 | case XDP_DROP: | |
bb91accf JW |
443 | goto err_xdp; |
444 | } | |
445 | } | |
446 | rcu_read_unlock(); | |
447 | ||
f121159d | 448 | return skb; |
bb91accf JW |
449 | |
450 | err_xdp: | |
451 | rcu_read_unlock(); | |
452 | dev->stats.rx_dropped++; | |
453 | kfree_skb(skb); | |
454 | xdp_xmit: | |
455 | return NULL; | |
f121159d MT |
456 | } |
457 | ||
458 | static struct sk_buff *receive_big(struct net_device *dev, | |
946fa564 | 459 | struct virtnet_info *vi, |
f121159d MT |
460 | struct receive_queue *rq, |
461 | void *buf, | |
462 | unsigned int len) | |
463 | { | |
464 | struct page *page = buf; | |
c47a43d3 | 465 | struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); |
f600b690 | 466 | |
f121159d MT |
467 | if (unlikely(!skb)) |
468 | goto err; | |
469 | ||
470 | return skb; | |
471 | ||
472 | err: | |
473 | dev->stats.rx_dropped++; | |
474 | give_pages(rq, page); | |
475 | return NULL; | |
476 | } | |
477 | ||
72979a6c JF |
478 | /* The conditions to enable XDP should preclude the underlying device from |
479 | * sending packets across multiple buffers (num_buf > 1). However per spec | |
480 | * it does not appear to be illegal to do so but rather just against convention. | |
481 | * So in order to avoid making a system unresponsive the packets are pushed | |
482 | * into a page and the XDP program is run. This will be extremely slow and we | |
483 | * push a warning to the user to fix this as soon as possible. Fixing this may | |
484 | * require resolving the underlying hardware to determine why multiple buffers | |
485 | * are being received or simply loading the XDP program in the ingress stack | |
486 | * after the skb is built because there is no advantage to running it here | |
487 | * anymore. | |
488 | */ | |
489 | static struct page *xdp_linearize_page(struct receive_queue *rq, | |
56a86f84 | 490 | u16 *num_buf, |
72979a6c JF |
491 | struct page *p, |
492 | int offset, | |
493 | unsigned int *len) | |
494 | { | |
495 | struct page *page = alloc_page(GFP_ATOMIC); | |
496 | unsigned int page_off = 0; | |
497 | ||
498 | if (!page) | |
499 | return NULL; | |
500 | ||
501 | memcpy(page_address(page) + page_off, page_address(p) + offset, *len); | |
502 | page_off += *len; | |
503 | ||
56a86f84 | 504 | while (--*num_buf) { |
72979a6c JF |
505 | unsigned int buflen; |
506 | unsigned long ctx; | |
507 | void *buf; | |
508 | int off; | |
509 | ||
510 | ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen); | |
511 | if (unlikely(!ctx)) | |
512 | goto err_buf; | |
513 | ||
56a86f84 JW |
514 | buf = mergeable_ctx_to_buf_address(ctx); |
515 | p = virt_to_head_page(buf); | |
516 | off = buf - page_address(p); | |
517 | ||
72979a6c JF |
518 | /* guard against a misconfigured or uncooperative backend that |
519 | * is sending packet larger than the MTU. | |
520 | */ | |
56a86f84 JW |
521 | if ((page_off + buflen) > PAGE_SIZE) { |
522 | put_page(p); | |
72979a6c | 523 | goto err_buf; |
56a86f84 | 524 | } |
72979a6c JF |
525 | |
526 | memcpy(page_address(page) + page_off, | |
527 | page_address(p) + off, buflen); | |
528 | page_off += buflen; | |
56a86f84 | 529 | put_page(p); |
72979a6c JF |
530 | } |
531 | ||
532 | *len = page_off; | |
533 | return page; | |
534 | err_buf: | |
535 | __free_pages(page, 0); | |
536 | return NULL; | |
537 | } | |
538 | ||
8fc3b9e9 | 539 | static struct sk_buff *receive_mergeable(struct net_device *dev, |
fdd819b2 | 540 | struct virtnet_info *vi, |
8fc3b9e9 | 541 | struct receive_queue *rq, |
ab7db917 | 542 | unsigned long ctx, |
8fc3b9e9 | 543 | unsigned int len) |
9ab86bbc | 544 | { |
ab7db917 | 545 | void *buf = mergeable_ctx_to_buf_address(ctx); |
012873d0 MT |
546 | struct virtio_net_hdr_mrg_rxbuf *hdr = buf; |
547 | u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); | |
8fc3b9e9 MT |
548 | struct page *page = virt_to_head_page(buf); |
549 | int offset = buf - page_address(page); | |
f600b690 JF |
550 | struct sk_buff *head_skb, *curr_skb; |
551 | struct bpf_prog *xdp_prog; | |
552 | unsigned int truesize; | |
553 | ||
56434a01 JF |
554 | head_skb = NULL; |
555 | ||
f600b690 JF |
556 | rcu_read_lock(); |
557 | xdp_prog = rcu_dereference(rq->xdp_prog); | |
558 | if (xdp_prog) { | |
72979a6c | 559 | struct page *xdp_page; |
0354e4d1 | 560 | struct xdp_buff xdp; |
0354e4d1 | 561 | void *data; |
f600b690 JF |
562 | u32 act; |
563 | ||
73b62bd0 | 564 | /* This happens when rx buffer size is underestimated */ |
f600b690 | 565 | if (unlikely(num_buf > 1)) { |
72979a6c | 566 | /* linearize data for XDP */ |
56a86f84 | 567 | xdp_page = xdp_linearize_page(rq, &num_buf, |
72979a6c JF |
568 | page, offset, &len); |
569 | if (!xdp_page) | |
570 | goto err_xdp; | |
571 | offset = 0; | |
572 | } else { | |
573 | xdp_page = page; | |
f600b690 JF |
574 | } |
575 | ||
576 | /* Transient failure which in theory could occur if | |
577 | * in-flight packets from before XDP was enabled reach | |
578 | * the receive path after XDP is loaded. In practice I | |
579 | * was not able to create this condition. | |
580 | */ | |
b00f70b0 | 581 | if (unlikely(hdr->hdr.gso_type)) |
f600b690 JF |
582 | goto err_xdp; |
583 | ||
0354e4d1 JF |
584 | data = page_address(xdp_page) + offset; |
585 | xdp.data = data + vi->hdr_len; | |
586 | xdp.data_end = xdp.data + (len - vi->hdr_len); | |
587 | act = bpf_prog_run_xdp(xdp_prog, &xdp); | |
588 | ||
56434a01 JF |
589 | switch (act) { |
590 | case XDP_PASS: | |
1830f893 JW |
591 | /* We can only create skb based on xdp_page. */ |
592 | if (unlikely(xdp_page != page)) { | |
593 | rcu_read_unlock(); | |
594 | put_page(page); | |
595 | head_skb = page_to_skb(vi, rq, xdp_page, | |
596 | 0, len, PAGE_SIZE); | |
5c33474d | 597 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); |
1830f893 JW |
598 | return head_skb; |
599 | } | |
56434a01 JF |
600 | break; |
601 | case XDP_TX: | |
722d8283 | 602 | if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp, data))) |
0354e4d1 | 603 | trace_xdp_exception(vi->dev, xdp_prog, act); |
5c33474d | 604 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); |
72979a6c JF |
605 | if (unlikely(xdp_page != page)) |
606 | goto err_xdp; | |
56434a01 JF |
607 | rcu_read_unlock(); |
608 | goto xdp_xmit; | |
56434a01 | 609 | default: |
0354e4d1 JF |
610 | bpf_warn_invalid_xdp_action(act); |
611 | case XDP_ABORTED: | |
612 | trace_xdp_exception(vi->dev, xdp_prog, act); | |
613 | case XDP_DROP: | |
72979a6c JF |
614 | if (unlikely(xdp_page != page)) |
615 | __free_pages(xdp_page, 0); | |
5c33474d | 616 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len); |
f600b690 | 617 | goto err_xdp; |
56434a01 | 618 | } |
f600b690 JF |
619 | } |
620 | rcu_read_unlock(); | |
ab7db917 | 621 | |
f600b690 JF |
622 | truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); |
623 | head_skb = page_to_skb(vi, rq, page, offset, len, truesize); | |
624 | curr_skb = head_skb; | |
9ab86bbc | 625 | |
8fc3b9e9 MT |
626 | if (unlikely(!curr_skb)) |
627 | goto err_skb; | |
9ab86bbc | 628 | while (--num_buf) { |
8fc3b9e9 MT |
629 | int num_skb_frags; |
630 | ||
ab7db917 MD |
631 | ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); |
632 | if (unlikely(!ctx)) { | |
8fc3b9e9 | 633 | pr_debug("%s: rx error: %d buffers out of %d missing\n", |
fdd819b2 | 634 | dev->name, num_buf, |
012873d0 MT |
635 | virtio16_to_cpu(vi->vdev, |
636 | hdr->num_buffers)); | |
8fc3b9e9 MT |
637 | dev->stats.rx_length_errors++; |
638 | goto err_buf; | |
3f2c31d9 | 639 | } |
8fc3b9e9 | 640 | |
ab7db917 | 641 | buf = mergeable_ctx_to_buf_address(ctx); |
8fc3b9e9 | 642 | page = virt_to_head_page(buf); |
8fc3b9e9 MT |
643 | |
644 | num_skb_frags = skb_shinfo(curr_skb)->nr_frags; | |
2613af0e MD |
645 | if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { |
646 | struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); | |
8fc3b9e9 MT |
647 | |
648 | if (unlikely(!nskb)) | |
649 | goto err_skb; | |
2613af0e MD |
650 | if (curr_skb == head_skb) |
651 | skb_shinfo(curr_skb)->frag_list = nskb; | |
652 | else | |
653 | curr_skb->next = nskb; | |
654 | curr_skb = nskb; | |
655 | head_skb->truesize += nskb->truesize; | |
656 | num_skb_frags = 0; | |
657 | } | |
ab7db917 | 658 | truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); |
2613af0e MD |
659 | if (curr_skb != head_skb) { |
660 | head_skb->data_len += len; | |
661 | head_skb->len += len; | |
fb51879d | 662 | head_skb->truesize += truesize; |
2613af0e | 663 | } |
8fc3b9e9 | 664 | offset = buf - page_address(page); |
ba275241 JW |
665 | if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { |
666 | put_page(page); | |
667 | skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, | |
fb51879d | 668 | len, truesize); |
ba275241 JW |
669 | } else { |
670 | skb_add_rx_frag(curr_skb, num_skb_frags, page, | |
fb51879d | 671 | offset, len, truesize); |
ba275241 | 672 | } |
8fc3b9e9 MT |
673 | } |
674 | ||
5377d758 | 675 | ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); |
8fc3b9e9 MT |
676 | return head_skb; |
677 | ||
f600b690 JF |
678 | err_xdp: |
679 | rcu_read_unlock(); | |
8fc3b9e9 MT |
680 | err_skb: |
681 | put_page(page); | |
682 | while (--num_buf) { | |
ab7db917 MD |
683 | ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); |
684 | if (unlikely(!ctx)) { | |
8fc3b9e9 MT |
685 | pr_debug("%s: rx error: %d buffers missing\n", |
686 | dev->name, num_buf); | |
687 | dev->stats.rx_length_errors++; | |
688 | break; | |
689 | } | |
ab7db917 | 690 | page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx)); |
8fc3b9e9 | 691 | put_page(page); |
9ab86bbc | 692 | } |
8fc3b9e9 MT |
693 | err_buf: |
694 | dev->stats.rx_dropped++; | |
695 | dev_kfree_skb(head_skb); | |
56434a01 | 696 | xdp_xmit: |
8fc3b9e9 | 697 | return NULL; |
9ab86bbc SM |
698 | } |
699 | ||
946fa564 MT |
700 | static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, |
701 | void *buf, unsigned int len) | |
9ab86bbc | 702 | { |
e9d7417b | 703 | struct net_device *dev = vi->dev; |
58472a76 | 704 | struct virtnet_stats *stats = this_cpu_ptr(vi->stats); |
9ab86bbc | 705 | struct sk_buff *skb; |
012873d0 | 706 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
3f2c31d9 | 707 | |
bcff3162 | 708 | if (unlikely(len < vi->hdr_len + ETH_HLEN)) { |
9ab86bbc SM |
709 | pr_debug("%s: short packet %i\n", dev->name, len); |
710 | dev->stats.rx_length_errors++; | |
ab7db917 MD |
711 | if (vi->mergeable_rx_bufs) { |
712 | unsigned long ctx = (unsigned long)buf; | |
713 | void *base = mergeable_ctx_to_buf_address(ctx); | |
714 | put_page(virt_to_head_page(base)); | |
715 | } else if (vi->big_packets) { | |
98bfd23c | 716 | give_pages(rq, buf); |
ab7db917 | 717 | } else { |
9ab86bbc | 718 | dev_kfree_skb(buf); |
ab7db917 | 719 | } |
9ab86bbc SM |
720 | return; |
721 | } | |
3f2c31d9 | 722 | |
f121159d | 723 | if (vi->mergeable_rx_bufs) |
fdd819b2 | 724 | skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len); |
f121159d | 725 | else if (vi->big_packets) |
946fa564 | 726 | skb = receive_big(dev, vi, rq, buf, len); |
f121159d | 727 | else |
bb91accf | 728 | skb = receive_small(dev, vi, rq, buf, len); |
f121159d MT |
729 | |
730 | if (unlikely(!skb)) | |
731 | return; | |
3f2c31d9 | 732 | |
9ab86bbc | 733 | hdr = skb_vnet_hdr(skb); |
3fa2a1df | 734 | |
83a27052 | 735 | u64_stats_update_begin(&stats->rx_syncp); |
3fa2a1df | 736 | stats->rx_bytes += skb->len; |
737 | stats->rx_packets++; | |
83a27052 | 738 | u64_stats_update_end(&stats->rx_syncp); |
296f96fc | 739 | |
e858fae2 | 740 | if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) |
10a8d94a | 741 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
296f96fc | 742 | |
e858fae2 MR |
743 | if (virtio_net_hdr_to_skb(skb, &hdr->hdr, |
744 | virtio_is_little_endian(vi->vdev))) { | |
745 | net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", | |
746 | dev->name, hdr->hdr.gso_type, | |
747 | hdr->hdr.gso_size); | |
748 | goto frame_err; | |
296f96fc RR |
749 | } |
750 | ||
d1dc06dc MR |
751 | skb->protocol = eth_type_trans(skb, dev); |
752 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", | |
753 | ntohs(skb->protocol), skb->len, skb->pkt_type); | |
754 | ||
0fbd050a | 755 | napi_gro_receive(&rq->napi, skb); |
296f96fc RR |
756 | return; |
757 | ||
758 | frame_err: | |
759 | dev->stats.rx_frame_errors++; | |
296f96fc RR |
760 | dev_kfree_skb(skb); |
761 | } | |
762 | ||
946fa564 MT |
763 | static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, |
764 | gfp_t gfp) | |
296f96fc RR |
765 | { |
766 | struct sk_buff *skb; | |
012873d0 | 767 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
9ab86bbc | 768 | int err; |
3f2c31d9 | 769 | |
5061de36 | 770 | skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp); |
9ab86bbc SM |
771 | if (unlikely(!skb)) |
772 | return -ENOMEM; | |
296f96fc | 773 | |
5061de36 | 774 | skb_put(skb, GOOD_PACKET_LEN); |
3f2c31d9 | 775 | |
9ab86bbc | 776 | hdr = skb_vnet_hdr(skb); |
547c890c | 777 | sg_init_table(rq->sg, 2); |
012873d0 | 778 | sg_set_buf(rq->sg, hdr, vi->hdr_len); |
e9d7417b | 779 | skb_to_sgvec(skb, rq->sg + 1, 0, skb->len); |
97402b96 | 780 | |
9dc7b9e4 | 781 | err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp); |
9ab86bbc SM |
782 | if (err < 0) |
783 | dev_kfree_skb(skb); | |
97402b96 | 784 | |
9ab86bbc SM |
785 | return err; |
786 | } | |
97402b96 | 787 | |
012873d0 MT |
788 | static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, |
789 | gfp_t gfp) | |
9ab86bbc | 790 | { |
9ab86bbc SM |
791 | struct page *first, *list = NULL; |
792 | char *p; | |
793 | int i, err, offset; | |
794 | ||
a5835440 RR |
795 | sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); |
796 | ||
e9d7417b | 797 | /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ |
9ab86bbc | 798 | for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { |
e9d7417b | 799 | first = get_a_page(rq, gfp); |
9ab86bbc SM |
800 | if (!first) { |
801 | if (list) | |
e9d7417b | 802 | give_pages(rq, list); |
9ab86bbc | 803 | return -ENOMEM; |
97402b96 | 804 | } |
e9d7417b | 805 | sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); |
97402b96 | 806 | |
9ab86bbc SM |
807 | /* chain new page in list head to match sg */ |
808 | first->private = (unsigned long)list; | |
809 | list = first; | |
810 | } | |
296f96fc | 811 | |
e9d7417b | 812 | first = get_a_page(rq, gfp); |
9ab86bbc | 813 | if (!first) { |
e9d7417b | 814 | give_pages(rq, list); |
9ab86bbc SM |
815 | return -ENOMEM; |
816 | } | |
817 | p = page_address(first); | |
818 | ||
e9d7417b | 819 | /* rq->sg[0], rq->sg[1] share the same page */ |
012873d0 MT |
820 | /* a separated rq->sg[0] for header - required in case !any_header_sg */ |
821 | sg_set_buf(&rq->sg[0], p, vi->hdr_len); | |
9ab86bbc | 822 | |
e9d7417b | 823 | /* rq->sg[1] for data packet, from offset */ |
9ab86bbc | 824 | offset = sizeof(struct padded_vnet_hdr); |
e9d7417b | 825 | sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); |
9ab86bbc SM |
826 | |
827 | /* chain first in list head */ | |
828 | first->private = (unsigned long)list; | |
9dc7b9e4 RR |
829 | err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, |
830 | first, gfp); | |
9ab86bbc | 831 | if (err < 0) |
e9d7417b | 832 | give_pages(rq, first); |
9ab86bbc SM |
833 | |
834 | return err; | |
296f96fc RR |
835 | } |
836 | ||
5377d758 | 837 | static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len) |
3f2c31d9 | 838 | { |
ab7db917 | 839 | const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); |
fbf28d78 MD |
840 | unsigned int len; |
841 | ||
5377d758 | 842 | len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), |
fbf28d78 MD |
843 | GOOD_PACKET_LEN, PAGE_SIZE - hdr_len); |
844 | return ALIGN(len, MERGEABLE_BUFFER_ALIGN); | |
845 | } | |
846 | ||
847 | static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp) | |
848 | { | |
fb51879d MD |
849 | struct page_frag *alloc_frag = &rq->alloc_frag; |
850 | char *buf; | |
ab7db917 | 851 | unsigned long ctx; |
3f2c31d9 | 852 | int err; |
fb51879d | 853 | unsigned int len, hole; |
3f2c31d9 | 854 | |
fbf28d78 | 855 | len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len); |
ab7db917 | 856 | if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) |
9ab86bbc | 857 | return -ENOMEM; |
ab7db917 | 858 | |
fb51879d | 859 | buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; |
ab7db917 | 860 | ctx = mergeable_buf_to_ctx(buf, len); |
fb51879d | 861 | get_page(alloc_frag->page); |
fb51879d MD |
862 | alloc_frag->offset += len; |
863 | hole = alloc_frag->size - alloc_frag->offset; | |
ab7db917 MD |
864 | if (hole < len) { |
865 | /* To avoid internal fragmentation, if there is very likely not | |
866 | * enough space for another buffer, add the remaining space to | |
867 | * the current buffer. This extra space is not included in | |
868 | * the truesize stored in ctx. | |
869 | */ | |
fb51879d MD |
870 | len += hole; |
871 | alloc_frag->offset += hole; | |
872 | } | |
3f2c31d9 | 873 | |
fb51879d | 874 | sg_init_one(rq->sg, buf, len); |
ab7db917 | 875 | err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp); |
9ab86bbc | 876 | if (err < 0) |
2613af0e | 877 | put_page(virt_to_head_page(buf)); |
3f2c31d9 | 878 | |
9ab86bbc SM |
879 | return err; |
880 | } | |
3f2c31d9 | 881 | |
b2baed69 RR |
882 | /* |
883 | * Returns false if we couldn't fill entirely (OOM). | |
884 | * | |
885 | * Normally run in the receive path, but can also be run from ndo_open | |
886 | * before we're receiving packets, or from refill_work which is | |
887 | * careful to disable receiving (using napi_disable). | |
888 | */ | |
946fa564 MT |
889 | static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, |
890 | gfp_t gfp) | |
9ab86bbc SM |
891 | { |
892 | int err; | |
1788f495 | 893 | bool oom; |
3f2c31d9 | 894 | |
fb51879d | 895 | gfp |= __GFP_COLD; |
9ab86bbc SM |
896 | do { |
897 | if (vi->mergeable_rx_bufs) | |
e9d7417b | 898 | err = add_recvbuf_mergeable(rq, gfp); |
9ab86bbc | 899 | else if (vi->big_packets) |
012873d0 | 900 | err = add_recvbuf_big(vi, rq, gfp); |
9ab86bbc | 901 | else |
946fa564 | 902 | err = add_recvbuf_small(vi, rq, gfp); |
3f2c31d9 | 903 | |
1788f495 | 904 | oom = err == -ENOMEM; |
9ed4cb07 | 905 | if (err) |
3f2c31d9 | 906 | break; |
b7dfde95 | 907 | } while (rq->vq->num_free); |
681daee2 | 908 | virtqueue_kick(rq->vq); |
3161e453 | 909 | return !oom; |
3f2c31d9 MM |
910 | } |
911 | ||
18445c4d | 912 | static void skb_recv_done(struct virtqueue *rvq) |
296f96fc RR |
913 | { |
914 | struct virtnet_info *vi = rvq->vdev->priv; | |
986a4f4d | 915 | struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; |
e9d7417b | 916 | |
18445c4d | 917 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
e9d7417b | 918 | if (napi_schedule_prep(&rq->napi)) { |
1915a712 | 919 | virtqueue_disable_cb(rvq); |
e9d7417b | 920 | __napi_schedule(&rq->napi); |
18445c4d | 921 | } |
296f96fc RR |
922 | } |
923 | ||
e9d7417b | 924 | static void virtnet_napi_enable(struct receive_queue *rq) |
3e9d08ec | 925 | { |
e9d7417b | 926 | napi_enable(&rq->napi); |
3e9d08ec BR |
927 | |
928 | /* If all buffers were filled by other side before we napi_enabled, we | |
929 | * won't get another interrupt, so process any outstanding packets | |
930 | * now. virtnet_poll wants re-enable the queue, so we disable here. | |
931 | * We synchronize against interrupts via NAPI_STATE_SCHED */ | |
e9d7417b JW |
932 | if (napi_schedule_prep(&rq->napi)) { |
933 | virtqueue_disable_cb(rq->vq); | |
ec13ee80 | 934 | local_bh_disable(); |
e9d7417b | 935 | __napi_schedule(&rq->napi); |
ec13ee80 | 936 | local_bh_enable(); |
3e9d08ec BR |
937 | } |
938 | } | |
939 | ||
3161e453 RR |
940 | static void refill_work(struct work_struct *work) |
941 | { | |
e9d7417b JW |
942 | struct virtnet_info *vi = |
943 | container_of(work, struct virtnet_info, refill.work); | |
3161e453 | 944 | bool still_empty; |
986a4f4d JW |
945 | int i; |
946 | ||
55257d72 | 947 | for (i = 0; i < vi->curr_queue_pairs; i++) { |
986a4f4d | 948 | struct receive_queue *rq = &vi->rq[i]; |
3161e453 | 949 | |
986a4f4d | 950 | napi_disable(&rq->napi); |
946fa564 | 951 | still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); |
986a4f4d | 952 | virtnet_napi_enable(rq); |
3161e453 | 953 | |
986a4f4d JW |
954 | /* In theory, this can happen: if we don't get any buffers in |
955 | * we will *never* try to fill again. | |
956 | */ | |
957 | if (still_empty) | |
958 | schedule_delayed_work(&vi->refill, HZ/2); | |
959 | } | |
3161e453 RR |
960 | } |
961 | ||
2ffa7598 | 962 | static int virtnet_receive(struct receive_queue *rq, int budget) |
296f96fc | 963 | { |
e9d7417b | 964 | struct virtnet_info *vi = rq->vq->vdev->priv; |
2ffa7598 | 965 | unsigned int len, received = 0; |
9ab86bbc | 966 | void *buf; |
296f96fc | 967 | |
296f96fc | 968 | while (received < budget && |
e9d7417b | 969 | (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { |
946fa564 | 970 | receive_buf(vi, rq, buf, len); |
296f96fc RR |
971 | received++; |
972 | } | |
973 | ||
be121f46 | 974 | if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { |
946fa564 | 975 | if (!try_fill_recv(vi, rq, GFP_ATOMIC)) |
3b07e9ca | 976 | schedule_delayed_work(&vi->refill, 0); |
3161e453 | 977 | } |
296f96fc | 978 | |
2ffa7598 JW |
979 | return received; |
980 | } | |
981 | ||
982 | static int virtnet_poll(struct napi_struct *napi, int budget) | |
983 | { | |
984 | struct receive_queue *rq = | |
985 | container_of(napi, struct receive_queue, napi); | |
faadb05f | 986 | unsigned int r, received; |
2ffa7598 | 987 | |
faadb05f | 988 | received = virtnet_receive(rq, budget); |
2ffa7598 | 989 | |
8329d98e RR |
990 | /* Out of packets? */ |
991 | if (received < budget) { | |
cbdadbbf | 992 | r = virtqueue_enable_cb_prepare(rq->vq); |
4d6308aa ED |
993 | if (napi_complete_done(napi, received)) { |
994 | if (unlikely(virtqueue_poll(rq->vq, r)) && | |
995 | napi_schedule_prep(napi)) { | |
996 | virtqueue_disable_cb(rq->vq); | |
997 | __napi_schedule(napi); | |
998 | } | |
4265f161 | 999 | } |
296f96fc RR |
1000 | } |
1001 | ||
1002 | return received; | |
1003 | } | |
1004 | ||
986a4f4d JW |
1005 | static int virtnet_open(struct net_device *dev) |
1006 | { | |
1007 | struct virtnet_info *vi = netdev_priv(dev); | |
1008 | int i; | |
1009 | ||
e4166625 JW |
1010 | for (i = 0; i < vi->max_queue_pairs; i++) { |
1011 | if (i < vi->curr_queue_pairs) | |
1012 | /* Make sure we have some buffers: if oom use wq. */ | |
946fa564 | 1013 | if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) |
e4166625 | 1014 | schedule_delayed_work(&vi->refill, 0); |
986a4f4d JW |
1015 | virtnet_napi_enable(&vi->rq[i]); |
1016 | } | |
1017 | ||
1018 | return 0; | |
1019 | } | |
1020 | ||
b7dfde95 | 1021 | static void free_old_xmit_skbs(struct send_queue *sq) |
296f96fc RR |
1022 | { |
1023 | struct sk_buff *skb; | |
6ee57bcc | 1024 | unsigned int len; |
e9d7417b | 1025 | struct virtnet_info *vi = sq->vq->vdev->priv; |
58472a76 | 1026 | struct virtnet_stats *stats = this_cpu_ptr(vi->stats); |
296f96fc | 1027 | |
e9d7417b | 1028 | while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { |
296f96fc | 1029 | pr_debug("Sent skb %p\n", skb); |
3fa2a1df | 1030 | |
83a27052 | 1031 | u64_stats_update_begin(&stats->tx_syncp); |
3fa2a1df | 1032 | stats->tx_bytes += skb->len; |
1033 | stats->tx_packets++; | |
83a27052 | 1034 | u64_stats_update_end(&stats->tx_syncp); |
3fa2a1df | 1035 | |
ed79bab8 | 1036 | dev_kfree_skb_any(skb); |
296f96fc RR |
1037 | } |
1038 | } | |
1039 | ||
e9d7417b | 1040 | static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) |
296f96fc | 1041 | { |
012873d0 | 1042 | struct virtio_net_hdr_mrg_rxbuf *hdr; |
296f96fc | 1043 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
e9d7417b | 1044 | struct virtnet_info *vi = sq->vq->vdev->priv; |
7bedc7dc | 1045 | unsigned num_sg; |
012873d0 | 1046 | unsigned hdr_len = vi->hdr_len; |
e7428e95 | 1047 | bool can_push; |
296f96fc | 1048 | |
e174961c | 1049 | pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); |
e7428e95 MT |
1050 | |
1051 | can_push = vi->any_header_sg && | |
1052 | !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && | |
1053 | !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; | |
1054 | /* Even if we can, don't push here yet as this would skew | |
1055 | * csum_start offset below. */ | |
1056 | if (can_push) | |
012873d0 | 1057 | hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); |
e7428e95 MT |
1058 | else |
1059 | hdr = skb_vnet_hdr(skb); | |
296f96fc | 1060 | |
e858fae2 | 1061 | if (virtio_net_hdr_from_skb(skb, &hdr->hdr, |
6391a448 | 1062 | virtio_is_little_endian(vi->vdev), false)) |
e858fae2 | 1063 | BUG(); |
296f96fc | 1064 | |
3f2c31d9 | 1065 | if (vi->mergeable_rx_bufs) |
012873d0 | 1066 | hdr->num_buffers = 0; |
3f2c31d9 | 1067 | |
547c890c | 1068 | sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); |
e7428e95 MT |
1069 | if (can_push) { |
1070 | __skb_push(skb, hdr_len); | |
1071 | num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); | |
1072 | /* Pull header back to avoid skew in tx bytes calculations. */ | |
1073 | __skb_pull(skb, hdr_len); | |
1074 | } else { | |
1075 | sg_set_buf(sq->sg, hdr, hdr_len); | |
1076 | num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; | |
1077 | } | |
9dc7b9e4 | 1078 | return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); |
11a3a154 RR |
1079 | } |
1080 | ||
424efe9c | 1081 | static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) |
99ffc696 RR |
1082 | { |
1083 | struct virtnet_info *vi = netdev_priv(dev); | |
986a4f4d JW |
1084 | int qnum = skb_get_queue_mapping(skb); |
1085 | struct send_queue *sq = &vi->sq[qnum]; | |
9ed4cb07 | 1086 | int err; |
4b7fd2e6 MT |
1087 | struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); |
1088 | bool kick = !skb->xmit_more; | |
2cb9c6ba | 1089 | |
2cb9c6ba | 1090 | /* Free up any pending old buffers before queueing new ones. */ |
e9d7417b | 1091 | free_old_xmit_skbs(sq); |
99ffc696 | 1092 | |
074c3582 JK |
1093 | /* timestamp packet in software */ |
1094 | skb_tx_timestamp(skb); | |
1095 | ||
03f191ba | 1096 | /* Try to transmit */ |
b7dfde95 | 1097 | err = xmit_skb(sq, skb); |
48925e37 | 1098 | |
9ed4cb07 | 1099 | /* This should not happen! */ |
681daee2 | 1100 | if (unlikely(err)) { |
9ed4cb07 RR |
1101 | dev->stats.tx_fifo_errors++; |
1102 | if (net_ratelimit()) | |
1103 | dev_warn(&dev->dev, | |
b7dfde95 | 1104 | "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); |
58eba97d | 1105 | dev->stats.tx_dropped++; |
85e94525 | 1106 | dev_kfree_skb_any(skb); |
58eba97d | 1107 | return NETDEV_TX_OK; |
296f96fc | 1108 | } |
03f191ba | 1109 | |
48925e37 RR |
1110 | /* Don't wait up for transmitted skbs to be freed. */ |
1111 | skb_orphan(skb); | |
1112 | nf_reset(skb); | |
1113 | ||
60302ff6 MT |
1114 | /* If running out of space, stop queue to avoid getting packets that we |
1115 | * are then unable to transmit. | |
1116 | * An alternative would be to force queuing layer to requeue the skb by | |
1117 | * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be | |
1118 | * returned in a normal path of operation: it means that driver is not | |
1119 | * maintaining the TX queue stop/start state properly, and causes | |
1120 | * the stack to do a non-trivial amount of useless work. | |
1121 | * Since most packets only take 1 or 2 ring slots, stopping the queue | |
1122 | * early means 16 slots are typically wasted. | |
d631b94e | 1123 | */ |
b7dfde95 | 1124 | if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { |
986a4f4d | 1125 | netif_stop_subqueue(dev, qnum); |
e9d7417b | 1126 | if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { |
48925e37 | 1127 | /* More just got used, free them then recheck. */ |
b7dfde95 LT |
1128 | free_old_xmit_skbs(sq); |
1129 | if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { | |
986a4f4d | 1130 | netif_start_subqueue(dev, qnum); |
e9d7417b | 1131 | virtqueue_disable_cb(sq->vq); |
48925e37 RR |
1132 | } |
1133 | } | |
99ffc696 | 1134 | } |
48925e37 | 1135 | |
4b7fd2e6 | 1136 | if (kick || netif_xmit_stopped(txq)) |
0b725a2c | 1137 | virtqueue_kick(sq->vq); |
296f96fc | 1138 | |
0b725a2c | 1139 | return NETDEV_TX_OK; |
c223a078 DM |
1140 | } |
1141 | ||
40cbfc37 AK |
1142 | /* |
1143 | * Send command via the control virtqueue and check status. Commands | |
1144 | * supported by the hypervisor, as indicated by feature bits, should | |
788a8b6d | 1145 | * never fail unless improperly formatted. |
40cbfc37 AK |
1146 | */ |
1147 | static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, | |
d24bae32 | 1148 | struct scatterlist *out) |
40cbfc37 | 1149 | { |
f7bc9594 | 1150 | struct scatterlist *sgs[4], hdr, stat; |
d24bae32 | 1151 | unsigned out_num = 0, tmp; |
40cbfc37 AK |
1152 | |
1153 | /* Caller should know better */ | |
f7bc9594 | 1154 | BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); |
40cbfc37 | 1155 | |
2ac46030 MT |
1156 | vi->ctrl_status = ~0; |
1157 | vi->ctrl_hdr.class = class; | |
1158 | vi->ctrl_hdr.cmd = cmd; | |
f7bc9594 | 1159 | /* Add header */ |
2ac46030 | 1160 | sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); |
f7bc9594 | 1161 | sgs[out_num++] = &hdr; |
40cbfc37 | 1162 | |
f7bc9594 RR |
1163 | if (out) |
1164 | sgs[out_num++] = out; | |
40cbfc37 | 1165 | |
f7bc9594 | 1166 | /* Add return status. */ |
2ac46030 | 1167 | sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); |
d24bae32 | 1168 | sgs[out_num] = &stat; |
40cbfc37 | 1169 | |
d24bae32 | 1170 | BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); |
a7c58146 | 1171 | virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); |
40cbfc37 | 1172 | |
67975901 | 1173 | if (unlikely(!virtqueue_kick(vi->cvq))) |
2ac46030 | 1174 | return vi->ctrl_status == VIRTIO_NET_OK; |
40cbfc37 AK |
1175 | |
1176 | /* Spin for a response, the kick causes an ioport write, trapping | |
1177 | * into the hypervisor, so the request should be handled immediately. | |
1178 | */ | |
047b9b94 HG |
1179 | while (!virtqueue_get_buf(vi->cvq, &tmp) && |
1180 | !virtqueue_is_broken(vi->cvq)) | |
40cbfc37 AK |
1181 | cpu_relax(); |
1182 | ||
2ac46030 | 1183 | return vi->ctrl_status == VIRTIO_NET_OK; |
40cbfc37 AK |
1184 | } |
1185 | ||
9c46f6d4 AW |
1186 | static int virtnet_set_mac_address(struct net_device *dev, void *p) |
1187 | { | |
1188 | struct virtnet_info *vi = netdev_priv(dev); | |
1189 | struct virtio_device *vdev = vi->vdev; | |
f2f2c8b4 | 1190 | int ret; |
e37e2ff3 | 1191 | struct sockaddr *addr; |
7e58d5ae | 1192 | struct scatterlist sg; |
9c46f6d4 | 1193 | |
801822d1 | 1194 | addr = kmemdup(p, sizeof(*addr), GFP_KERNEL); |
e37e2ff3 AL |
1195 | if (!addr) |
1196 | return -ENOMEM; | |
e37e2ff3 AL |
1197 | |
1198 | ret = eth_prepare_mac_addr_change(dev, addr); | |
f2f2c8b4 | 1199 | if (ret) |
e37e2ff3 | 1200 | goto out; |
9c46f6d4 | 1201 | |
7e58d5ae AK |
1202 | if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { |
1203 | sg_init_one(&sg, addr->sa_data, dev->addr_len); | |
1204 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, | |
d24bae32 | 1205 | VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { |
7e58d5ae AK |
1206 | dev_warn(&vdev->dev, |
1207 | "Failed to set mac address by vq command.\n"); | |
e37e2ff3 AL |
1208 | ret = -EINVAL; |
1209 | goto out; | |
7e58d5ae | 1210 | } |
7e93a02f MT |
1211 | } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && |
1212 | !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { | |
855e0c52 RR |
1213 | unsigned int i; |
1214 | ||
1215 | /* Naturally, this has an atomicity problem. */ | |
1216 | for (i = 0; i < dev->addr_len; i++) | |
1217 | virtio_cwrite8(vdev, | |
1218 | offsetof(struct virtio_net_config, mac) + | |
1219 | i, addr->sa_data[i]); | |
7e58d5ae AK |
1220 | } |
1221 | ||
1222 | eth_commit_mac_addr_change(dev, p); | |
e37e2ff3 | 1223 | ret = 0; |
9c46f6d4 | 1224 | |
e37e2ff3 AL |
1225 | out: |
1226 | kfree(addr); | |
1227 | return ret; | |
9c46f6d4 AW |
1228 | } |
1229 | ||
bc1f4470 | 1230 | static void virtnet_stats(struct net_device *dev, |
1231 | struct rtnl_link_stats64 *tot) | |
3fa2a1df | 1232 | { |
1233 | struct virtnet_info *vi = netdev_priv(dev); | |
1234 | int cpu; | |
1235 | unsigned int start; | |
1236 | ||
1237 | for_each_possible_cpu(cpu) { | |
58472a76 | 1238 | struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); |
3fa2a1df | 1239 | u64 tpackets, tbytes, rpackets, rbytes; |
1240 | ||
1241 | do { | |
57a7744e | 1242 | start = u64_stats_fetch_begin_irq(&stats->tx_syncp); |
3fa2a1df | 1243 | tpackets = stats->tx_packets; |
1244 | tbytes = stats->tx_bytes; | |
57a7744e | 1245 | } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); |
83a27052 ED |
1246 | |
1247 | do { | |
57a7744e | 1248 | start = u64_stats_fetch_begin_irq(&stats->rx_syncp); |
3fa2a1df | 1249 | rpackets = stats->rx_packets; |
1250 | rbytes = stats->rx_bytes; | |
57a7744e | 1251 | } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); |
3fa2a1df | 1252 | |
1253 | tot->rx_packets += rpackets; | |
1254 | tot->tx_packets += tpackets; | |
1255 | tot->rx_bytes += rbytes; | |
1256 | tot->tx_bytes += tbytes; | |
1257 | } | |
1258 | ||
1259 | tot->tx_dropped = dev->stats.tx_dropped; | |
021ac8d3 | 1260 | tot->tx_fifo_errors = dev->stats.tx_fifo_errors; |
3fa2a1df | 1261 | tot->rx_dropped = dev->stats.rx_dropped; |
1262 | tot->rx_length_errors = dev->stats.rx_length_errors; | |
1263 | tot->rx_frame_errors = dev->stats.rx_frame_errors; | |
3fa2a1df | 1264 | } |
1265 | ||
da74e89d AS |
1266 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1267 | static void virtnet_netpoll(struct net_device *dev) | |
1268 | { | |
1269 | struct virtnet_info *vi = netdev_priv(dev); | |
986a4f4d | 1270 | int i; |
da74e89d | 1271 | |
986a4f4d JW |
1272 | for (i = 0; i < vi->curr_queue_pairs; i++) |
1273 | napi_schedule(&vi->rq[i].napi); | |
da74e89d AS |
1274 | } |
1275 | #endif | |
1276 | ||
586d17c5 JW |
1277 | static void virtnet_ack_link_announce(struct virtnet_info *vi) |
1278 | { | |
1279 | rtnl_lock(); | |
1280 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, | |
d24bae32 | 1281 | VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) |
586d17c5 JW |
1282 | dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); |
1283 | rtnl_unlock(); | |
1284 | } | |
1285 | ||
47315329 | 1286 | static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) |
986a4f4d JW |
1287 | { |
1288 | struct scatterlist sg; | |
986a4f4d JW |
1289 | struct net_device *dev = vi->dev; |
1290 | ||
1291 | if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) | |
1292 | return 0; | |
1293 | ||
a725ee3e AL |
1294 | vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); |
1295 | sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq)); | |
986a4f4d JW |
1296 | |
1297 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, | |
d24bae32 | 1298 | VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { |
986a4f4d JW |
1299 | dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", |
1300 | queue_pairs); | |
1301 | return -EINVAL; | |
55257d72 | 1302 | } else { |
986a4f4d | 1303 | vi->curr_queue_pairs = queue_pairs; |
35ed159b JW |
1304 | /* virtnet_open() will refill when device is going to up. */ |
1305 | if (dev->flags & IFF_UP) | |
1306 | schedule_delayed_work(&vi->refill, 0); | |
55257d72 | 1307 | } |
986a4f4d JW |
1308 | |
1309 | return 0; | |
1310 | } | |
1311 | ||
47315329 JF |
1312 | static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) |
1313 | { | |
1314 | int err; | |
1315 | ||
1316 | rtnl_lock(); | |
1317 | err = _virtnet_set_queues(vi, queue_pairs); | |
1318 | rtnl_unlock(); | |
1319 | return err; | |
1320 | } | |
1321 | ||
296f96fc RR |
1322 | static int virtnet_close(struct net_device *dev) |
1323 | { | |
1324 | struct virtnet_info *vi = netdev_priv(dev); | |
986a4f4d | 1325 | int i; |
296f96fc | 1326 | |
b2baed69 RR |
1327 | /* Make sure refill_work doesn't re-enable napi! */ |
1328 | cancel_delayed_work_sync(&vi->refill); | |
986a4f4d JW |
1329 | |
1330 | for (i = 0; i < vi->max_queue_pairs; i++) | |
1331 | napi_disable(&vi->rq[i].napi); | |
296f96fc | 1332 | |
296f96fc RR |
1333 | return 0; |
1334 | } | |
1335 | ||
2af7698e AW |
1336 | static void virtnet_set_rx_mode(struct net_device *dev) |
1337 | { | |
1338 | struct virtnet_info *vi = netdev_priv(dev); | |
f565a7c2 | 1339 | struct scatterlist sg[2]; |
f565a7c2 | 1340 | struct virtio_net_ctrl_mac *mac_data; |
ccffad25 | 1341 | struct netdev_hw_addr *ha; |
32e7bfc4 | 1342 | int uc_count; |
4cd24eaf | 1343 | int mc_count; |
f565a7c2 AW |
1344 | void *buf; |
1345 | int i; | |
2af7698e | 1346 | |
788a8b6d | 1347 | /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ |
2af7698e AW |
1348 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) |
1349 | return; | |
1350 | ||
2ac46030 MT |
1351 | vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); |
1352 | vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); | |
2af7698e | 1353 | |
2ac46030 | 1354 | sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); |
2af7698e AW |
1355 | |
1356 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, | |
d24bae32 | 1357 | VIRTIO_NET_CTRL_RX_PROMISC, sg)) |
2af7698e | 1358 | dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", |
2ac46030 | 1359 | vi->ctrl_promisc ? "en" : "dis"); |
2af7698e | 1360 | |
2ac46030 | 1361 | sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); |
2af7698e AW |
1362 | |
1363 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, | |
d24bae32 | 1364 | VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) |
2af7698e | 1365 | dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", |
2ac46030 | 1366 | vi->ctrl_allmulti ? "en" : "dis"); |
f565a7c2 | 1367 | |
32e7bfc4 | 1368 | uc_count = netdev_uc_count(dev); |
4cd24eaf | 1369 | mc_count = netdev_mc_count(dev); |
f565a7c2 | 1370 | /* MAC filter - use one buffer for both lists */ |
4cd24eaf JP |
1371 | buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + |
1372 | (2 * sizeof(mac_data->entries)), GFP_ATOMIC); | |
1373 | mac_data = buf; | |
e68ed8f0 | 1374 | if (!buf) |
f565a7c2 | 1375 | return; |
f565a7c2 | 1376 | |
23e258e1 AW |
1377 | sg_init_table(sg, 2); |
1378 | ||
f565a7c2 | 1379 | /* Store the unicast list and count in the front of the buffer */ |
fdd819b2 | 1380 | mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); |
ccffad25 | 1381 | i = 0; |
32e7bfc4 | 1382 | netdev_for_each_uc_addr(ha, dev) |
ccffad25 | 1383 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); |
f565a7c2 AW |
1384 | |
1385 | sg_set_buf(&sg[0], mac_data, | |
32e7bfc4 | 1386 | sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); |
f565a7c2 AW |
1387 | |
1388 | /* multicast list and count fill the end */ | |
32e7bfc4 | 1389 | mac_data = (void *)&mac_data->macs[uc_count][0]; |
f565a7c2 | 1390 | |
fdd819b2 | 1391 | mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); |
567ec874 | 1392 | i = 0; |
22bedad3 JP |
1393 | netdev_for_each_mc_addr(ha, dev) |
1394 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); | |
f565a7c2 AW |
1395 | |
1396 | sg_set_buf(&sg[1], mac_data, | |
4cd24eaf | 1397 | sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); |
f565a7c2 AW |
1398 | |
1399 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, | |
d24bae32 | 1400 | VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) |
99e872ae | 1401 | dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); |
f565a7c2 AW |
1402 | |
1403 | kfree(buf); | |
2af7698e AW |
1404 | } |
1405 | ||
80d5c368 PM |
1406 | static int virtnet_vlan_rx_add_vid(struct net_device *dev, |
1407 | __be16 proto, u16 vid) | |
0bde9569 AW |
1408 | { |
1409 | struct virtnet_info *vi = netdev_priv(dev); | |
1410 | struct scatterlist sg; | |
1411 | ||
a725ee3e AL |
1412 | vi->ctrl_vid = vid; |
1413 | sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); | |
0bde9569 AW |
1414 | |
1415 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, | |
d24bae32 | 1416 | VIRTIO_NET_CTRL_VLAN_ADD, &sg)) |
0bde9569 | 1417 | dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); |
8e586137 | 1418 | return 0; |
0bde9569 AW |
1419 | } |
1420 | ||
80d5c368 PM |
1421 | static int virtnet_vlan_rx_kill_vid(struct net_device *dev, |
1422 | __be16 proto, u16 vid) | |
0bde9569 AW |
1423 | { |
1424 | struct virtnet_info *vi = netdev_priv(dev); | |
1425 | struct scatterlist sg; | |
1426 | ||
a725ee3e AL |
1427 | vi->ctrl_vid = vid; |
1428 | sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); | |
0bde9569 AW |
1429 | |
1430 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, | |
d24bae32 | 1431 | VIRTIO_NET_CTRL_VLAN_DEL, &sg)) |
0bde9569 | 1432 | dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); |
8e586137 | 1433 | return 0; |
0bde9569 AW |
1434 | } |
1435 | ||
8898c21c | 1436 | static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) |
986a4f4d JW |
1437 | { |
1438 | int i; | |
1439 | ||
8898c21c WG |
1440 | if (vi->affinity_hint_set) { |
1441 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
47be2479 WG |
1442 | virtqueue_set_affinity(vi->rq[i].vq, -1); |
1443 | virtqueue_set_affinity(vi->sq[i].vq, -1); | |
1444 | } | |
1445 | ||
8898c21c WG |
1446 | vi->affinity_hint_set = false; |
1447 | } | |
8898c21c | 1448 | } |
47be2479 | 1449 | |
8898c21c WG |
1450 | static void virtnet_set_affinity(struct virtnet_info *vi) |
1451 | { | |
1452 | int i; | |
1453 | int cpu; | |
986a4f4d JW |
1454 | |
1455 | /* In multiqueue mode, when the number of cpu is equal to the number of | |
1456 | * queue pairs, we let the queue pairs to be private to one cpu by | |
1457 | * setting the affinity hint to eliminate the contention. | |
1458 | */ | |
8898c21c WG |
1459 | if (vi->curr_queue_pairs == 1 || |
1460 | vi->max_queue_pairs != num_online_cpus()) { | |
1461 | virtnet_clean_affinity(vi, -1); | |
1462 | return; | |
986a4f4d JW |
1463 | } |
1464 | ||
8898c21c WG |
1465 | i = 0; |
1466 | for_each_online_cpu(cpu) { | |
986a4f4d JW |
1467 | virtqueue_set_affinity(vi->rq[i].vq, cpu); |
1468 | virtqueue_set_affinity(vi->sq[i].vq, cpu); | |
9bb8ca86 | 1469 | netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); |
8898c21c | 1470 | i++; |
986a4f4d JW |
1471 | } |
1472 | ||
8898c21c | 1473 | vi->affinity_hint_set = true; |
986a4f4d JW |
1474 | } |
1475 | ||
8017c279 | 1476 | static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) |
8de4b2f3 | 1477 | { |
8017c279 SAS |
1478 | struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, |
1479 | node); | |
1480 | virtnet_set_affinity(vi); | |
1481 | return 0; | |
1482 | } | |
8de4b2f3 | 1483 | |
8017c279 SAS |
1484 | static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) |
1485 | { | |
1486 | struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, | |
1487 | node_dead); | |
1488 | virtnet_set_affinity(vi); | |
1489 | return 0; | |
1490 | } | |
3ab098df | 1491 | |
8017c279 SAS |
1492 | static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) |
1493 | { | |
1494 | struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, | |
1495 | node); | |
1496 | ||
1497 | virtnet_clean_affinity(vi, cpu); | |
1498 | return 0; | |
1499 | } | |
1500 | ||
1501 | static enum cpuhp_state virtionet_online; | |
1502 | ||
1503 | static int virtnet_cpu_notif_add(struct virtnet_info *vi) | |
1504 | { | |
1505 | int ret; | |
1506 | ||
1507 | ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); | |
1508 | if (ret) | |
1509 | return ret; | |
1510 | ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, | |
1511 | &vi->node_dead); | |
1512 | if (!ret) | |
1513 | return ret; | |
1514 | cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); | |
1515 | return ret; | |
1516 | } | |
1517 | ||
1518 | static void virtnet_cpu_notif_remove(struct virtnet_info *vi) | |
1519 | { | |
1520 | cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); | |
1521 | cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, | |
1522 | &vi->node_dead); | |
986a4f4d JW |
1523 | } |
1524 | ||
8f9f4668 RJ |
1525 | static void virtnet_get_ringparam(struct net_device *dev, |
1526 | struct ethtool_ringparam *ring) | |
1527 | { | |
1528 | struct virtnet_info *vi = netdev_priv(dev); | |
1529 | ||
986a4f4d JW |
1530 | ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); |
1531 | ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); | |
8f9f4668 RJ |
1532 | ring->rx_pending = ring->rx_max_pending; |
1533 | ring->tx_pending = ring->tx_max_pending; | |
8f9f4668 RJ |
1534 | } |
1535 | ||
66846048 RJ |
1536 | |
1537 | static void virtnet_get_drvinfo(struct net_device *dev, | |
1538 | struct ethtool_drvinfo *info) | |
1539 | { | |
1540 | struct virtnet_info *vi = netdev_priv(dev); | |
1541 | struct virtio_device *vdev = vi->vdev; | |
1542 | ||
1543 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); | |
1544 | strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); | |
1545 | strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); | |
1546 | ||
1547 | } | |
1548 | ||
d73bcd2c JW |
1549 | /* TODO: Eliminate OOO packets during switching */ |
1550 | static int virtnet_set_channels(struct net_device *dev, | |
1551 | struct ethtool_channels *channels) | |
1552 | { | |
1553 | struct virtnet_info *vi = netdev_priv(dev); | |
1554 | u16 queue_pairs = channels->combined_count; | |
1555 | int err; | |
1556 | ||
1557 | /* We don't support separate rx/tx channels. | |
1558 | * We don't allow setting 'other' channels. | |
1559 | */ | |
1560 | if (channels->rx_count || channels->tx_count || channels->other_count) | |
1561 | return -EINVAL; | |
1562 | ||
c18e9cd6 | 1563 | if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) |
d73bcd2c JW |
1564 | return -EINVAL; |
1565 | ||
f600b690 JF |
1566 | /* For now we don't support modifying channels while XDP is loaded |
1567 | * also when XDP is loaded all RX queues have XDP programs so we only | |
1568 | * need to check a single RX queue. | |
1569 | */ | |
1570 | if (vi->rq[0].xdp_prog) | |
1571 | return -EINVAL; | |
1572 | ||
47be2479 | 1573 | get_online_cpus(); |
47315329 | 1574 | err = _virtnet_set_queues(vi, queue_pairs); |
d73bcd2c JW |
1575 | if (!err) { |
1576 | netif_set_real_num_tx_queues(dev, queue_pairs); | |
1577 | netif_set_real_num_rx_queues(dev, queue_pairs); | |
1578 | ||
8898c21c | 1579 | virtnet_set_affinity(vi); |
d73bcd2c | 1580 | } |
47be2479 | 1581 | put_online_cpus(); |
d73bcd2c JW |
1582 | |
1583 | return err; | |
1584 | } | |
1585 | ||
1586 | static void virtnet_get_channels(struct net_device *dev, | |
1587 | struct ethtool_channels *channels) | |
1588 | { | |
1589 | struct virtnet_info *vi = netdev_priv(dev); | |
1590 | ||
1591 | channels->combined_count = vi->curr_queue_pairs; | |
1592 | channels->max_combined = vi->max_queue_pairs; | |
1593 | channels->max_other = 0; | |
1594 | channels->rx_count = 0; | |
1595 | channels->tx_count = 0; | |
1596 | channels->other_count = 0; | |
1597 | } | |
1598 | ||
16032be5 NA |
1599 | /* Check if the user is trying to change anything besides speed/duplex */ |
1600 | static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd) | |
1601 | { | |
1602 | struct ethtool_cmd diff1 = *cmd; | |
1603 | struct ethtool_cmd diff2 = {}; | |
1604 | ||
0cf3ace9 NA |
1605 | /* cmd is always set so we need to clear it, validate the port type |
1606 | * and also without autonegotiation we can ignore advertising | |
1607 | */ | |
16032be5 | 1608 | ethtool_cmd_speed_set(&diff1, 0); |
0cf3ace9 | 1609 | diff2.port = PORT_OTHER; |
16032be5 NA |
1610 | diff1.advertising = 0; |
1611 | diff1.duplex = 0; | |
16032be5 NA |
1612 | diff1.cmd = 0; |
1613 | ||
1614 | return !memcmp(&diff1, &diff2, sizeof(diff1)); | |
1615 | } | |
1616 | ||
1617 | static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) | |
1618 | { | |
1619 | struct virtnet_info *vi = netdev_priv(dev); | |
1620 | u32 speed; | |
1621 | ||
1622 | speed = ethtool_cmd_speed(cmd); | |
1623 | /* don't allow custom speed and duplex */ | |
1624 | if (!ethtool_validate_speed(speed) || | |
1625 | !ethtool_validate_duplex(cmd->duplex) || | |
1626 | !virtnet_validate_ethtool_cmd(cmd)) | |
1627 | return -EINVAL; | |
1628 | vi->speed = speed; | |
1629 | vi->duplex = cmd->duplex; | |
1630 | ||
1631 | return 0; | |
1632 | } | |
1633 | ||
1634 | static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) | |
1635 | { | |
1636 | struct virtnet_info *vi = netdev_priv(dev); | |
1637 | ||
1638 | ethtool_cmd_speed_set(cmd, vi->speed); | |
1639 | cmd->duplex = vi->duplex; | |
1640 | cmd->port = PORT_OTHER; | |
1641 | ||
1642 | return 0; | |
1643 | } | |
1644 | ||
1645 | static void virtnet_init_settings(struct net_device *dev) | |
1646 | { | |
1647 | struct virtnet_info *vi = netdev_priv(dev); | |
1648 | ||
1649 | vi->speed = SPEED_UNKNOWN; | |
1650 | vi->duplex = DUPLEX_UNKNOWN; | |
1651 | } | |
1652 | ||
0fc0b732 | 1653 | static const struct ethtool_ops virtnet_ethtool_ops = { |
66846048 | 1654 | .get_drvinfo = virtnet_get_drvinfo, |
9f4d26d0 | 1655 | .get_link = ethtool_op_get_link, |
8f9f4668 | 1656 | .get_ringparam = virtnet_get_ringparam, |
d73bcd2c JW |
1657 | .set_channels = virtnet_set_channels, |
1658 | .get_channels = virtnet_get_channels, | |
074c3582 | 1659 | .get_ts_info = ethtool_op_get_ts_info, |
16032be5 NA |
1660 | .get_settings = virtnet_get_settings, |
1661 | .set_settings = virtnet_set_settings, | |
a9ea3fc6 HX |
1662 | }; |
1663 | ||
9fe7bfce JF |
1664 | static void virtnet_freeze_down(struct virtio_device *vdev) |
1665 | { | |
1666 | struct virtnet_info *vi = vdev->priv; | |
1667 | int i; | |
1668 | ||
1669 | /* Make sure no work handler is accessing the device */ | |
1670 | flush_work(&vi->config_work); | |
1671 | ||
1672 | netif_device_detach(vi->dev); | |
1673 | cancel_delayed_work_sync(&vi->refill); | |
1674 | ||
1675 | if (netif_running(vi->dev)) { | |
1676 | for (i = 0; i < vi->max_queue_pairs; i++) | |
1677 | napi_disable(&vi->rq[i].napi); | |
1678 | } | |
1679 | } | |
1680 | ||
1681 | static int init_vqs(struct virtnet_info *vi); | |
1682 | ||
1683 | static int virtnet_restore_up(struct virtio_device *vdev) | |
1684 | { | |
1685 | struct virtnet_info *vi = vdev->priv; | |
1686 | int err, i; | |
1687 | ||
1688 | err = init_vqs(vi); | |
1689 | if (err) | |
1690 | return err; | |
1691 | ||
1692 | virtio_device_ready(vdev); | |
1693 | ||
1694 | if (netif_running(vi->dev)) { | |
1695 | for (i = 0; i < vi->curr_queue_pairs; i++) | |
1696 | if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) | |
1697 | schedule_delayed_work(&vi->refill, 0); | |
1698 | ||
1699 | for (i = 0; i < vi->max_queue_pairs; i++) | |
1700 | virtnet_napi_enable(&vi->rq[i]); | |
1701 | } | |
1702 | ||
1703 | netif_device_attach(vi->dev); | |
1704 | return err; | |
1705 | } | |
1706 | ||
f600b690 JF |
1707 | static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog) |
1708 | { | |
1709 | unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr); | |
1710 | struct virtnet_info *vi = netdev_priv(dev); | |
1711 | struct bpf_prog *old_prog; | |
672aafd5 JF |
1712 | u16 xdp_qp = 0, curr_qp; |
1713 | int i, err; | |
f600b690 | 1714 | |
529ec6ac JK |
1715 | if (prog && prog->xdp_adjust_head) { |
1716 | netdev_warn(dev, "Does not support bpf_xdp_adjust_head()\n"); | |
1717 | return -EOPNOTSUPP; | |
1718 | } | |
1719 | ||
f600b690 | 1720 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || |
92502fe8 JW |
1721 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || |
1722 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || | |
1723 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) { | |
f600b690 JF |
1724 | netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n"); |
1725 | return -EOPNOTSUPP; | |
1726 | } | |
1727 | ||
1728 | if (vi->mergeable_rx_bufs && !vi->any_header_sg) { | |
1729 | netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n"); | |
1730 | return -EINVAL; | |
1731 | } | |
1732 | ||
1733 | if (dev->mtu > max_sz) { | |
1734 | netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz); | |
1735 | return -EINVAL; | |
1736 | } | |
1737 | ||
672aafd5 JF |
1738 | curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; |
1739 | if (prog) | |
1740 | xdp_qp = nr_cpu_ids; | |
1741 | ||
1742 | /* XDP requires extra queues for XDP_TX */ | |
1743 | if (curr_qp + xdp_qp > vi->max_queue_pairs) { | |
1744 | netdev_warn(dev, "request %i queues but max is %i\n", | |
1745 | curr_qp + xdp_qp, vi->max_queue_pairs); | |
1746 | return -ENOMEM; | |
1747 | } | |
1748 | ||
47315329 | 1749 | err = _virtnet_set_queues(vi, curr_qp + xdp_qp); |
672aafd5 JF |
1750 | if (err) { |
1751 | dev_warn(&dev->dev, "XDP Device queue allocation failure.\n"); | |
1752 | return err; | |
1753 | } | |
1754 | ||
f600b690 JF |
1755 | if (prog) { |
1756 | prog = bpf_prog_add(prog, vi->max_queue_pairs - 1); | |
672aafd5 | 1757 | if (IS_ERR(prog)) { |
47315329 | 1758 | _virtnet_set_queues(vi, curr_qp); |
f600b690 | 1759 | return PTR_ERR(prog); |
672aafd5 | 1760 | } |
f600b690 JF |
1761 | } |
1762 | ||
672aafd5 JF |
1763 | vi->xdp_queue_pairs = xdp_qp; |
1764 | netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp); | |
1765 | ||
f600b690 JF |
1766 | for (i = 0; i < vi->max_queue_pairs; i++) { |
1767 | old_prog = rtnl_dereference(vi->rq[i].xdp_prog); | |
1768 | rcu_assign_pointer(vi->rq[i].xdp_prog, prog); | |
1769 | if (old_prog) | |
1770 | bpf_prog_put(old_prog); | |
1771 | } | |
1772 | ||
1773 | return 0; | |
1774 | } | |
1775 | ||
1776 | static bool virtnet_xdp_query(struct net_device *dev) | |
1777 | { | |
1778 | struct virtnet_info *vi = netdev_priv(dev); | |
1779 | int i; | |
1780 | ||
1781 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
1782 | if (vi->rq[i].xdp_prog) | |
1783 | return true; | |
1784 | } | |
1785 | return false; | |
1786 | } | |
1787 | ||
1788 | static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp) | |
1789 | { | |
1790 | switch (xdp->command) { | |
1791 | case XDP_SETUP_PROG: | |
1792 | return virtnet_xdp_set(dev, xdp->prog); | |
1793 | case XDP_QUERY_PROG: | |
1794 | xdp->prog_attached = virtnet_xdp_query(dev); | |
1795 | return 0; | |
1796 | default: | |
1797 | return -EINVAL; | |
1798 | } | |
1799 | } | |
1800 | ||
76288b4e SH |
1801 | static const struct net_device_ops virtnet_netdev = { |
1802 | .ndo_open = virtnet_open, | |
1803 | .ndo_stop = virtnet_close, | |
1804 | .ndo_start_xmit = start_xmit, | |
1805 | .ndo_validate_addr = eth_validate_addr, | |
9c46f6d4 | 1806 | .ndo_set_mac_address = virtnet_set_mac_address, |
2af7698e | 1807 | .ndo_set_rx_mode = virtnet_set_rx_mode, |
3fa2a1df | 1808 | .ndo_get_stats64 = virtnet_stats, |
1824a989 AW |
1809 | .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, |
1810 | .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, | |
76288b4e SH |
1811 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1812 | .ndo_poll_controller = virtnet_netpoll, | |
91815639 | 1813 | #endif |
f600b690 | 1814 | .ndo_xdp = virtnet_xdp, |
76288b4e SH |
1815 | }; |
1816 | ||
586d17c5 | 1817 | static void virtnet_config_changed_work(struct work_struct *work) |
9f4d26d0 | 1818 | { |
586d17c5 JW |
1819 | struct virtnet_info *vi = |
1820 | container_of(work, struct virtnet_info, config_work); | |
9f4d26d0 MM |
1821 | u16 v; |
1822 | ||
855e0c52 RR |
1823 | if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, |
1824 | struct virtio_net_config, status, &v) < 0) | |
507613bf | 1825 | return; |
586d17c5 JW |
1826 | |
1827 | if (v & VIRTIO_NET_S_ANNOUNCE) { | |
ee89bab1 | 1828 | netdev_notify_peers(vi->dev); |
586d17c5 JW |
1829 | virtnet_ack_link_announce(vi); |
1830 | } | |
9f4d26d0 MM |
1831 | |
1832 | /* Ignore unknown (future) status bits */ | |
1833 | v &= VIRTIO_NET_S_LINK_UP; | |
1834 | ||
1835 | if (vi->status == v) | |
507613bf | 1836 | return; |
9f4d26d0 MM |
1837 | |
1838 | vi->status = v; | |
1839 | ||
1840 | if (vi->status & VIRTIO_NET_S_LINK_UP) { | |
1841 | netif_carrier_on(vi->dev); | |
986a4f4d | 1842 | netif_tx_wake_all_queues(vi->dev); |
9f4d26d0 MM |
1843 | } else { |
1844 | netif_carrier_off(vi->dev); | |
986a4f4d | 1845 | netif_tx_stop_all_queues(vi->dev); |
9f4d26d0 MM |
1846 | } |
1847 | } | |
1848 | ||
1849 | static void virtnet_config_changed(struct virtio_device *vdev) | |
1850 | { | |
1851 | struct virtnet_info *vi = vdev->priv; | |
1852 | ||
3b07e9ca | 1853 | schedule_work(&vi->config_work); |
9f4d26d0 MM |
1854 | } |
1855 | ||
986a4f4d JW |
1856 | static void virtnet_free_queues(struct virtnet_info *vi) |
1857 | { | |
d4fb84ee AV |
1858 | int i; |
1859 | ||
ab3971b1 JW |
1860 | for (i = 0; i < vi->max_queue_pairs; i++) { |
1861 | napi_hash_del(&vi->rq[i].napi); | |
d4fb84ee | 1862 | netif_napi_del(&vi->rq[i].napi); |
ab3971b1 | 1863 | } |
d4fb84ee | 1864 | |
963abe5c ED |
1865 | /* We called napi_hash_del() before netif_napi_del(), |
1866 | * we need to respect an RCU grace period before freeing vi->rq | |
1867 | */ | |
1868 | synchronize_net(); | |
1869 | ||
986a4f4d JW |
1870 | kfree(vi->rq); |
1871 | kfree(vi->sq); | |
1872 | } | |
1873 | ||
47315329 | 1874 | static void _free_receive_bufs(struct virtnet_info *vi) |
986a4f4d | 1875 | { |
f600b690 | 1876 | struct bpf_prog *old_prog; |
986a4f4d JW |
1877 | int i; |
1878 | ||
1879 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
1880 | while (vi->rq[i].pages) | |
1881 | __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); | |
f600b690 JF |
1882 | |
1883 | old_prog = rtnl_dereference(vi->rq[i].xdp_prog); | |
1884 | RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); | |
1885 | if (old_prog) | |
1886 | bpf_prog_put(old_prog); | |
986a4f4d | 1887 | } |
47315329 JF |
1888 | } |
1889 | ||
1890 | static void free_receive_bufs(struct virtnet_info *vi) | |
1891 | { | |
1892 | rtnl_lock(); | |
1893 | _free_receive_bufs(vi); | |
f600b690 | 1894 | rtnl_unlock(); |
986a4f4d JW |
1895 | } |
1896 | ||
fb51879d MD |
1897 | static void free_receive_page_frags(struct virtnet_info *vi) |
1898 | { | |
1899 | int i; | |
1900 | for (i = 0; i < vi->max_queue_pairs; i++) | |
1901 | if (vi->rq[i].alloc_frag.page) | |
1902 | put_page(vi->rq[i].alloc_frag.page); | |
1903 | } | |
1904 | ||
b68df015 | 1905 | static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) |
56434a01 | 1906 | { |
b68df015 JF |
1907 | /* For small receive mode always use kfree_skb variants */ |
1908 | if (!vi->mergeable_rx_bufs) | |
1909 | return false; | |
1910 | ||
56434a01 JF |
1911 | if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) |
1912 | return false; | |
1913 | else if (q < vi->curr_queue_pairs) | |
1914 | return true; | |
1915 | else | |
1916 | return false; | |
1917 | } | |
1918 | ||
986a4f4d JW |
1919 | static void free_unused_bufs(struct virtnet_info *vi) |
1920 | { | |
1921 | void *buf; | |
1922 | int i; | |
1923 | ||
1924 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
1925 | struct virtqueue *vq = vi->sq[i].vq; | |
56434a01 | 1926 | while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { |
b68df015 | 1927 | if (!is_xdp_raw_buffer_queue(vi, i)) |
56434a01 JF |
1928 | dev_kfree_skb(buf); |
1929 | else | |
1930 | put_page(virt_to_head_page(buf)); | |
1931 | } | |
986a4f4d JW |
1932 | } |
1933 | ||
1934 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
1935 | struct virtqueue *vq = vi->rq[i].vq; | |
1936 | ||
1937 | while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { | |
ab7db917 MD |
1938 | if (vi->mergeable_rx_bufs) { |
1939 | unsigned long ctx = (unsigned long)buf; | |
1940 | void *base = mergeable_ctx_to_buf_address(ctx); | |
1941 | put_page(virt_to_head_page(base)); | |
1942 | } else if (vi->big_packets) { | |
fa9fac17 | 1943 | give_pages(&vi->rq[i], buf); |
ab7db917 | 1944 | } else { |
986a4f4d | 1945 | dev_kfree_skb(buf); |
ab7db917 | 1946 | } |
986a4f4d | 1947 | } |
986a4f4d JW |
1948 | } |
1949 | } | |
1950 | ||
e9d7417b JW |
1951 | static void virtnet_del_vqs(struct virtnet_info *vi) |
1952 | { | |
1953 | struct virtio_device *vdev = vi->vdev; | |
1954 | ||
8898c21c | 1955 | virtnet_clean_affinity(vi, -1); |
986a4f4d | 1956 | |
e9d7417b | 1957 | vdev->config->del_vqs(vdev); |
986a4f4d JW |
1958 | |
1959 | virtnet_free_queues(vi); | |
e9d7417b JW |
1960 | } |
1961 | ||
986a4f4d | 1962 | static int virtnet_find_vqs(struct virtnet_info *vi) |
3f9c10b0 | 1963 | { |
986a4f4d JW |
1964 | vq_callback_t **callbacks; |
1965 | struct virtqueue **vqs; | |
1966 | int ret = -ENOMEM; | |
1967 | int i, total_vqs; | |
1968 | const char **names; | |
1969 | ||
1970 | /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by | |
1971 | * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by | |
1972 | * possible control vq. | |
1973 | */ | |
1974 | total_vqs = vi->max_queue_pairs * 2 + | |
1975 | virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); | |
1976 | ||
1977 | /* Allocate space for find_vqs parameters */ | |
1978 | vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); | |
1979 | if (!vqs) | |
1980 | goto err_vq; | |
1981 | callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); | |
1982 | if (!callbacks) | |
1983 | goto err_callback; | |
1984 | names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); | |
1985 | if (!names) | |
1986 | goto err_names; | |
1987 | ||
1988 | /* Parameters for control virtqueue, if any */ | |
1989 | if (vi->has_cvq) { | |
1990 | callbacks[total_vqs - 1] = NULL; | |
1991 | names[total_vqs - 1] = "control"; | |
1992 | } | |
3f9c10b0 | 1993 | |
986a4f4d JW |
1994 | /* Allocate/initialize parameters for send/receive virtqueues */ |
1995 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
1996 | callbacks[rxq2vq(i)] = skb_recv_done; | |
1997 | callbacks[txq2vq(i)] = skb_xmit_done; | |
1998 | sprintf(vi->rq[i].name, "input.%d", i); | |
1999 | sprintf(vi->sq[i].name, "output.%d", i); | |
2000 | names[rxq2vq(i)] = vi->rq[i].name; | |
2001 | names[txq2vq(i)] = vi->sq[i].name; | |
2002 | } | |
3f9c10b0 | 2003 | |
986a4f4d JW |
2004 | ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, |
2005 | names); | |
2006 | if (ret) | |
2007 | goto err_find; | |
3f9c10b0 | 2008 | |
986a4f4d JW |
2009 | if (vi->has_cvq) { |
2010 | vi->cvq = vqs[total_vqs - 1]; | |
3f9c10b0 | 2011 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) |
f646968f | 2012 | vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; |
3f9c10b0 | 2013 | } |
986a4f4d JW |
2014 | |
2015 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
2016 | vi->rq[i].vq = vqs[rxq2vq(i)]; | |
2017 | vi->sq[i].vq = vqs[txq2vq(i)]; | |
2018 | } | |
2019 | ||
2020 | kfree(names); | |
2021 | kfree(callbacks); | |
2022 | kfree(vqs); | |
2023 | ||
3f9c10b0 | 2024 | return 0; |
986a4f4d JW |
2025 | |
2026 | err_find: | |
2027 | kfree(names); | |
2028 | err_names: | |
2029 | kfree(callbacks); | |
2030 | err_callback: | |
2031 | kfree(vqs); | |
2032 | err_vq: | |
2033 | return ret; | |
2034 | } | |
2035 | ||
2036 | static int virtnet_alloc_queues(struct virtnet_info *vi) | |
2037 | { | |
2038 | int i; | |
2039 | ||
2040 | vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); | |
2041 | if (!vi->sq) | |
2042 | goto err_sq; | |
2043 | vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); | |
008d4278 | 2044 | if (!vi->rq) |
986a4f4d JW |
2045 | goto err_rq; |
2046 | ||
2047 | INIT_DELAYED_WORK(&vi->refill, refill_work); | |
2048 | for (i = 0; i < vi->max_queue_pairs; i++) { | |
2049 | vi->rq[i].pages = NULL; | |
2050 | netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, | |
2051 | napi_weight); | |
2052 | ||
2053 | sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); | |
5377d758 | 2054 | ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); |
986a4f4d JW |
2055 | sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); |
2056 | } | |
2057 | ||
2058 | return 0; | |
2059 | ||
2060 | err_rq: | |
2061 | kfree(vi->sq); | |
2062 | err_sq: | |
2063 | return -ENOMEM; | |
2064 | } | |
2065 | ||
2066 | static int init_vqs(struct virtnet_info *vi) | |
2067 | { | |
2068 | int ret; | |
2069 | ||
2070 | /* Allocate send & receive queues */ | |
2071 | ret = virtnet_alloc_queues(vi); | |
2072 | if (ret) | |
2073 | goto err; | |
2074 | ||
2075 | ret = virtnet_find_vqs(vi); | |
2076 | if (ret) | |
2077 | goto err_free; | |
2078 | ||
47be2479 | 2079 | get_online_cpus(); |
8898c21c | 2080 | virtnet_set_affinity(vi); |
47be2479 WG |
2081 | put_online_cpus(); |
2082 | ||
986a4f4d JW |
2083 | return 0; |
2084 | ||
2085 | err_free: | |
2086 | virtnet_free_queues(vi); | |
2087 | err: | |
2088 | return ret; | |
3f9c10b0 AS |
2089 | } |
2090 | ||
fbf28d78 MD |
2091 | #ifdef CONFIG_SYSFS |
2092 | static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, | |
2093 | struct rx_queue_attribute *attribute, char *buf) | |
2094 | { | |
2095 | struct virtnet_info *vi = netdev_priv(queue->dev); | |
2096 | unsigned int queue_index = get_netdev_rx_queue_index(queue); | |
5377d758 | 2097 | struct ewma_pkt_len *avg; |
fbf28d78 MD |
2098 | |
2099 | BUG_ON(queue_index >= vi->max_queue_pairs); | |
2100 | avg = &vi->rq[queue_index].mrg_avg_pkt_len; | |
2101 | return sprintf(buf, "%u\n", get_mergeable_buf_len(avg)); | |
2102 | } | |
2103 | ||
2104 | static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = | |
2105 | __ATTR_RO(mergeable_rx_buffer_size); | |
2106 | ||
2107 | static struct attribute *virtio_net_mrg_rx_attrs[] = { | |
2108 | &mergeable_rx_buffer_size_attribute.attr, | |
2109 | NULL | |
2110 | }; | |
2111 | ||
2112 | static const struct attribute_group virtio_net_mrg_rx_group = { | |
2113 | .name = "virtio_net", | |
2114 | .attrs = virtio_net_mrg_rx_attrs | |
2115 | }; | |
2116 | #endif | |
2117 | ||
892d6eb1 JW |
2118 | static bool virtnet_fail_on_feature(struct virtio_device *vdev, |
2119 | unsigned int fbit, | |
2120 | const char *fname, const char *dname) | |
2121 | { | |
2122 | if (!virtio_has_feature(vdev, fbit)) | |
2123 | return false; | |
2124 | ||
2125 | dev_err(&vdev->dev, "device advertises feature %s but not %s", | |
2126 | fname, dname); | |
2127 | ||
2128 | return true; | |
2129 | } | |
2130 | ||
2131 | #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ | |
2132 | virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) | |
2133 | ||
2134 | static bool virtnet_validate_features(struct virtio_device *vdev) | |
2135 | { | |
2136 | if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && | |
2137 | (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, | |
2138 | "VIRTIO_NET_F_CTRL_VQ") || | |
2139 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, | |
2140 | "VIRTIO_NET_F_CTRL_VQ") || | |
2141 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, | |
2142 | "VIRTIO_NET_F_CTRL_VQ") || | |
2143 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || | |
2144 | VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, | |
2145 | "VIRTIO_NET_F_CTRL_VQ"))) { | |
2146 | return false; | |
2147 | } | |
2148 | ||
2149 | return true; | |
2150 | } | |
2151 | ||
d0c2c997 JW |
2152 | #define MIN_MTU ETH_MIN_MTU |
2153 | #define MAX_MTU ETH_MAX_MTU | |
2154 | ||
296f96fc RR |
2155 | static int virtnet_probe(struct virtio_device *vdev) |
2156 | { | |
986a4f4d | 2157 | int i, err; |
296f96fc RR |
2158 | struct net_device *dev; |
2159 | struct virtnet_info *vi; | |
986a4f4d | 2160 | u16 max_queue_pairs; |
14de9d11 | 2161 | int mtu; |
986a4f4d | 2162 | |
6ba42248 MT |
2163 | if (!vdev->config->get) { |
2164 | dev_err(&vdev->dev, "%s failure: config access disabled\n", | |
2165 | __func__); | |
2166 | return -EINVAL; | |
2167 | } | |
2168 | ||
892d6eb1 JW |
2169 | if (!virtnet_validate_features(vdev)) |
2170 | return -EINVAL; | |
2171 | ||
986a4f4d | 2172 | /* Find if host supports multiqueue virtio_net device */ |
855e0c52 RR |
2173 | err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, |
2174 | struct virtio_net_config, | |
2175 | max_virtqueue_pairs, &max_queue_pairs); | |
986a4f4d JW |
2176 | |
2177 | /* We need at least 2 queue's */ | |
2178 | if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || | |
2179 | max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || | |
2180 | !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) | |
2181 | max_queue_pairs = 1; | |
296f96fc RR |
2182 | |
2183 | /* Allocate ourselves a network device with room for our info */ | |
986a4f4d | 2184 | dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); |
296f96fc RR |
2185 | if (!dev) |
2186 | return -ENOMEM; | |
2187 | ||
2188 | /* Set up network device as normal. */ | |
f2f2c8b4 | 2189 | dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; |
76288b4e | 2190 | dev->netdev_ops = &virtnet_netdev; |
296f96fc | 2191 | dev->features = NETIF_F_HIGHDMA; |
3fa2a1df | 2192 | |
7ad24ea4 | 2193 | dev->ethtool_ops = &virtnet_ethtool_ops; |
296f96fc RR |
2194 | SET_NETDEV_DEV(dev, &vdev->dev); |
2195 | ||
2196 | /* Do we support "hardware" checksums? */ | |
98e778c9 | 2197 | if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
296f96fc | 2198 | /* This opens up the world of extra features. */ |
48900cb6 | 2199 | dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; |
98e778c9 | 2200 | if (csum) |
48900cb6 | 2201 | dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; |
98e778c9 MM |
2202 | |
2203 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { | |
e3e3c423 | 2204 | dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO |
34a48579 RR |
2205 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; |
2206 | } | |
5539ae96 | 2207 | /* Individual feature bits: what can host handle? */ |
98e778c9 MM |
2208 | if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
2209 | dev->hw_features |= NETIF_F_TSO; | |
2210 | if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) | |
2211 | dev->hw_features |= NETIF_F_TSO6; | |
2212 | if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) | |
2213 | dev->hw_features |= NETIF_F_TSO_ECN; | |
e3e3c423 VY |
2214 | if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
2215 | dev->hw_features |= NETIF_F_UFO; | |
98e778c9 | 2216 | |
41f2f127 JW |
2217 | dev->features |= NETIF_F_GSO_ROBUST; |
2218 | ||
98e778c9 | 2219 | if (gso) |
e3e3c423 | 2220 | dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); |
98e778c9 | 2221 | /* (!csum && gso) case will be fixed by register_netdev() */ |
296f96fc | 2222 | } |
4f49129b TH |
2223 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) |
2224 | dev->features |= NETIF_F_RXCSUM; | |
296f96fc | 2225 | |
4fda8302 JW |
2226 | dev->vlan_features = dev->features; |
2227 | ||
d0c2c997 JW |
2228 | /* MTU range: 68 - 65535 */ |
2229 | dev->min_mtu = MIN_MTU; | |
2230 | dev->max_mtu = MAX_MTU; | |
2231 | ||
296f96fc | 2232 | /* Configuration may specify what MAC to use. Otherwise random. */ |
855e0c52 RR |
2233 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) |
2234 | virtio_cread_bytes(vdev, | |
2235 | offsetof(struct virtio_net_config, mac), | |
2236 | dev->dev_addr, dev->addr_len); | |
2237 | else | |
f2cedb63 | 2238 | eth_hw_addr_random(dev); |
296f96fc RR |
2239 | |
2240 | /* Set up our device-specific information */ | |
2241 | vi = netdev_priv(dev); | |
296f96fc RR |
2242 | vi->dev = dev; |
2243 | vi->vdev = vdev; | |
d9d5dcc8 | 2244 | vdev->priv = vi; |
3fa2a1df | 2245 | vi->stats = alloc_percpu(struct virtnet_stats); |
2246 | err = -ENOMEM; | |
2247 | if (vi->stats == NULL) | |
2248 | goto free; | |
2249 | ||
827da44c JS |
2250 | for_each_possible_cpu(i) { |
2251 | struct virtnet_stats *virtnet_stats; | |
2252 | virtnet_stats = per_cpu_ptr(vi->stats, i); | |
2253 | u64_stats_init(&virtnet_stats->tx_syncp); | |
2254 | u64_stats_init(&virtnet_stats->rx_syncp); | |
2255 | } | |
2256 | ||
586d17c5 | 2257 | INIT_WORK(&vi->config_work, virtnet_config_changed_work); |
296f96fc | 2258 | |
97402b96 | 2259 | /* If we can receive ANY GSO packets, we must allocate large ones. */ |
8e95a202 JP |
2260 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || |
2261 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || | |
e3e3c423 VY |
2262 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || |
2263 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) | |
97402b96 HX |
2264 | vi->big_packets = true; |
2265 | ||
3f2c31d9 MM |
2266 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) |
2267 | vi->mergeable_rx_bufs = true; | |
2268 | ||
d04302b3 MT |
2269 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || |
2270 | virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) | |
012873d0 MT |
2271 | vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); |
2272 | else | |
2273 | vi->hdr_len = sizeof(struct virtio_net_hdr); | |
2274 | ||
75993300 MT |
2275 | if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || |
2276 | virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) | |
e7428e95 MT |
2277 | vi->any_header_sg = true; |
2278 | ||
986a4f4d JW |
2279 | if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) |
2280 | vi->has_cvq = true; | |
2281 | ||
14de9d11 AC |
2282 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { |
2283 | mtu = virtio_cread16(vdev, | |
2284 | offsetof(struct virtio_net_config, | |
2285 | mtu)); | |
93a205ee | 2286 | if (mtu < dev->min_mtu) { |
14de9d11 | 2287 | __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); |
93a205ee | 2288 | } else { |
d0c2c997 | 2289 | dev->mtu = mtu; |
93a205ee AC |
2290 | dev->max_mtu = mtu; |
2291 | } | |
14de9d11 AC |
2292 | } |
2293 | ||
012873d0 MT |
2294 | if (vi->any_header_sg) |
2295 | dev->needed_headroom = vi->hdr_len; | |
6ebbc1a6 | 2296 | |
44900010 JW |
2297 | /* Enable multiqueue by default */ |
2298 | if (num_online_cpus() >= max_queue_pairs) | |
2299 | vi->curr_queue_pairs = max_queue_pairs; | |
2300 | else | |
2301 | vi->curr_queue_pairs = num_online_cpus(); | |
986a4f4d JW |
2302 | vi->max_queue_pairs = max_queue_pairs; |
2303 | ||
2304 | /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ | |
3f9c10b0 | 2305 | err = init_vqs(vi); |
d2a7ddda | 2306 | if (err) |
9bb8ca86 | 2307 | goto free_stats; |
296f96fc | 2308 | |
fbf28d78 MD |
2309 | #ifdef CONFIG_SYSFS |
2310 | if (vi->mergeable_rx_bufs) | |
2311 | dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; | |
2312 | #endif | |
0f13b66b ZYW |
2313 | netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); |
2314 | netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); | |
986a4f4d | 2315 | |
16032be5 NA |
2316 | virtnet_init_settings(dev); |
2317 | ||
296f96fc RR |
2318 | err = register_netdev(dev); |
2319 | if (err) { | |
2320 | pr_debug("virtio_net: registering device failed\n"); | |
d2a7ddda | 2321 | goto free_vqs; |
296f96fc | 2322 | } |
b3369c1f | 2323 | |
4baf1e33 MT |
2324 | virtio_device_ready(vdev); |
2325 | ||
8017c279 | 2326 | err = virtnet_cpu_notif_add(vi); |
8de4b2f3 WG |
2327 | if (err) { |
2328 | pr_debug("virtio_net: registering cpu notifier failed\n"); | |
f00e35e2 | 2329 | goto free_unregister_netdev; |
8de4b2f3 WG |
2330 | } |
2331 | ||
a220871b | 2332 | virtnet_set_queues(vi, vi->curr_queue_pairs); |
44900010 | 2333 | |
167c25e4 JW |
2334 | /* Assume link up if device can't report link status, |
2335 | otherwise get link status from config. */ | |
2336 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { | |
2337 | netif_carrier_off(dev); | |
3b07e9ca | 2338 | schedule_work(&vi->config_work); |
167c25e4 JW |
2339 | } else { |
2340 | vi->status = VIRTIO_NET_S_LINK_UP; | |
2341 | netif_carrier_on(dev); | |
2342 | } | |
9f4d26d0 | 2343 | |
986a4f4d JW |
2344 | pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", |
2345 | dev->name, max_queue_pairs); | |
2346 | ||
296f96fc RR |
2347 | return 0; |
2348 | ||
f00e35e2 | 2349 | free_unregister_netdev: |
02465555 MT |
2350 | vi->vdev->config->reset(vdev); |
2351 | ||
b3369c1f | 2352 | unregister_netdev(dev); |
d2a7ddda | 2353 | free_vqs: |
986a4f4d | 2354 | cancel_delayed_work_sync(&vi->refill); |
fb51879d | 2355 | free_receive_page_frags(vi); |
e9d7417b | 2356 | virtnet_del_vqs(vi); |
3fa2a1df | 2357 | free_stats: |
2358 | free_percpu(vi->stats); | |
296f96fc RR |
2359 | free: |
2360 | free_netdev(dev); | |
2361 | return err; | |
2362 | } | |
2363 | ||
04486ed0 | 2364 | static void remove_vq_common(struct virtnet_info *vi) |
296f96fc | 2365 | { |
04486ed0 | 2366 | vi->vdev->config->reset(vi->vdev); |
830a8a97 SM |
2367 | |
2368 | /* Free unused buffers in both send and recv, if any. */ | |
9ab86bbc | 2369 | free_unused_bufs(vi); |
fb6813f4 | 2370 | |
986a4f4d | 2371 | free_receive_bufs(vi); |
d2a7ddda | 2372 | |
fb51879d MD |
2373 | free_receive_page_frags(vi); |
2374 | ||
986a4f4d | 2375 | virtnet_del_vqs(vi); |
04486ed0 AS |
2376 | } |
2377 | ||
8cc085d6 | 2378 | static void virtnet_remove(struct virtio_device *vdev) |
04486ed0 AS |
2379 | { |
2380 | struct virtnet_info *vi = vdev->priv; | |
2381 | ||
8017c279 | 2382 | virtnet_cpu_notif_remove(vi); |
8de4b2f3 | 2383 | |
102a2786 MT |
2384 | /* Make sure no work handler is accessing the device. */ |
2385 | flush_work(&vi->config_work); | |
586d17c5 | 2386 | |
04486ed0 AS |
2387 | unregister_netdev(vi->dev); |
2388 | ||
2389 | remove_vq_common(vi); | |
fb6813f4 | 2390 | |
2e66f55b | 2391 | free_percpu(vi->stats); |
74b2553f | 2392 | free_netdev(vi->dev); |
296f96fc RR |
2393 | } |
2394 | ||
89107000 | 2395 | #ifdef CONFIG_PM_SLEEP |
0741bcb5 AS |
2396 | static int virtnet_freeze(struct virtio_device *vdev) |
2397 | { | |
2398 | struct virtnet_info *vi = vdev->priv; | |
2399 | ||
8017c279 | 2400 | virtnet_cpu_notif_remove(vi); |
9fe7bfce | 2401 | virtnet_freeze_down(vdev); |
0741bcb5 AS |
2402 | remove_vq_common(vi); |
2403 | ||
2404 | return 0; | |
2405 | } | |
2406 | ||
2407 | static int virtnet_restore(struct virtio_device *vdev) | |
2408 | { | |
2409 | struct virtnet_info *vi = vdev->priv; | |
9fe7bfce | 2410 | int err; |
0741bcb5 | 2411 | |
9fe7bfce | 2412 | err = virtnet_restore_up(vdev); |
0741bcb5 AS |
2413 | if (err) |
2414 | return err; | |
986a4f4d JW |
2415 | virtnet_set_queues(vi, vi->curr_queue_pairs); |
2416 | ||
8017c279 | 2417 | err = virtnet_cpu_notif_add(vi); |
ec9debbd JW |
2418 | if (err) |
2419 | return err; | |
2420 | ||
0741bcb5 AS |
2421 | return 0; |
2422 | } | |
2423 | #endif | |
2424 | ||
296f96fc RR |
2425 | static struct virtio_device_id id_table[] = { |
2426 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, | |
2427 | { 0 }, | |
2428 | }; | |
2429 | ||
f3358507 MT |
2430 | #define VIRTNET_FEATURES \ |
2431 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ | |
2432 | VIRTIO_NET_F_MAC, \ | |
2433 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ | |
2434 | VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ | |
2435 | VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ | |
2436 | VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ | |
2437 | VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ | |
2438 | VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ | |
2439 | VIRTIO_NET_F_CTRL_MAC_ADDR, \ | |
2440 | VIRTIO_NET_F_MTU | |
2441 | ||
c45a6816 | 2442 | static unsigned int features[] = { |
f3358507 MT |
2443 | VIRTNET_FEATURES, |
2444 | }; | |
2445 | ||
2446 | static unsigned int features_legacy[] = { | |
2447 | VIRTNET_FEATURES, | |
2448 | VIRTIO_NET_F_GSO, | |
e7428e95 | 2449 | VIRTIO_F_ANY_LAYOUT, |
c45a6816 RR |
2450 | }; |
2451 | ||
22402529 | 2452 | static struct virtio_driver virtio_net_driver = { |
c45a6816 RR |
2453 | .feature_table = features, |
2454 | .feature_table_size = ARRAY_SIZE(features), | |
f3358507 MT |
2455 | .feature_table_legacy = features_legacy, |
2456 | .feature_table_size_legacy = ARRAY_SIZE(features_legacy), | |
296f96fc RR |
2457 | .driver.name = KBUILD_MODNAME, |
2458 | .driver.owner = THIS_MODULE, | |
2459 | .id_table = id_table, | |
2460 | .probe = virtnet_probe, | |
8cc085d6 | 2461 | .remove = virtnet_remove, |
9f4d26d0 | 2462 | .config_changed = virtnet_config_changed, |
89107000 | 2463 | #ifdef CONFIG_PM_SLEEP |
0741bcb5 AS |
2464 | .freeze = virtnet_freeze, |
2465 | .restore = virtnet_restore, | |
2466 | #endif | |
296f96fc RR |
2467 | }; |
2468 | ||
8017c279 SAS |
2469 | static __init int virtio_net_driver_init(void) |
2470 | { | |
2471 | int ret; | |
2472 | ||
73c1b41e | 2473 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online", |
8017c279 SAS |
2474 | virtnet_cpu_online, |
2475 | virtnet_cpu_down_prep); | |
2476 | if (ret < 0) | |
2477 | goto out; | |
2478 | virtionet_online = ret; | |
73c1b41e | 2479 | ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead", |
8017c279 SAS |
2480 | NULL, virtnet_cpu_dead); |
2481 | if (ret) | |
2482 | goto err_dead; | |
2483 | ||
2484 | ret = register_virtio_driver(&virtio_net_driver); | |
2485 | if (ret) | |
2486 | goto err_virtio; | |
2487 | return 0; | |
2488 | err_virtio: | |
2489 | cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); | |
2490 | err_dead: | |
2491 | cpuhp_remove_multi_state(virtionet_online); | |
2492 | out: | |
2493 | return ret; | |
2494 | } | |
2495 | module_init(virtio_net_driver_init); | |
2496 | ||
2497 | static __exit void virtio_net_driver_exit(void) | |
2498 | { | |
2499 | cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); | |
2500 | cpuhp_remove_multi_state(virtionet_online); | |
2501 | unregister_virtio_driver(&virtio_net_driver); | |
2502 | } | |
2503 | module_exit(virtio_net_driver_exit); | |
296f96fc RR |
2504 | |
2505 | MODULE_DEVICE_TABLE(virtio, id_table); | |
2506 | MODULE_DESCRIPTION("Virtio network driver"); | |
2507 | MODULE_LICENSE("GPL"); |