1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A network driver using virtio.
3 *
4 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5 */
6 //#define DEBUG
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/module.h>
11 #include <linux/virtio.h>
12 #include <linux/virtio_net.h>
13 #include <linux/bpf.h>
14 #include <linux/bpf_trace.h>
15 #include <linux/scatterlist.h>
16 #include <linux/if_vlan.h>
17 #include <linux/slab.h>
18 #include <linux/cpu.h>
19 #include <linux/average.h>
20 #include <linux/filter.h>
21 #include <linux/kernel.h>
22 #include <linux/dim.h>
23 #include <net/route.h>
24 #include <net/xdp.h>
25 #include <net/net_failover.h>
26 #include <net/netdev_rx_queue.h>
27 #include <net/netdev_queues.h>
28 #include <net/xdp_sock_drv.h>
29
30 static int napi_weight = NAPI_POLL_WEIGHT;
31 module_param(napi_weight, int, 0444);
32
33 static bool csum = true, gso = true, napi_tx = true;
34 module_param(csum, bool, 0444);
35 module_param(gso, bool, 0444);
36 module_param(napi_tx, bool, 0644);
37
38 /* FIXME: MTU in config. */
39 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
40 #define GOOD_COPY_LEN 128
41
42 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
43
44 /* Separating two types of XDP xmit */
45 #define VIRTIO_XDP_TX BIT(0)
46 #define VIRTIO_XDP_REDIR BIT(1)
47
48 /* RX packet size EWMA. The average packet size is used to determine the packet
49 * buffer size when refilling RX rings. As the entire RX ring may be refilled
50 * at once, the weight is chosen so that the EWMA will be insensitive to short-
51 * term, transient changes in packet size.
52 */
53 DECLARE_EWMA(pkt_len, 0, 64)
54
55 #define VIRTNET_DRIVER_VERSION "1.0.0"
56
57 static const unsigned long guest_offloads[] = {
58 VIRTIO_NET_F_GUEST_TSO4,
59 VIRTIO_NET_F_GUEST_TSO6,
60 VIRTIO_NET_F_GUEST_ECN,
61 VIRTIO_NET_F_GUEST_UFO,
62 VIRTIO_NET_F_GUEST_CSUM,
63 VIRTIO_NET_F_GUEST_USO4,
64 VIRTIO_NET_F_GUEST_USO6,
65 VIRTIO_NET_F_GUEST_HDRLEN
66 };
67
68 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
69 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
70 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
71 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
72 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
73 (1ULL << VIRTIO_NET_F_GUEST_USO6))
74
75 struct virtnet_stat_desc {
76 char desc[ETH_GSTRING_LEN];
77 size_t offset;
78 size_t qstat_offset;
79 };
80
81 struct virtnet_sq_free_stats {
82 u64 packets;
83 u64 bytes;
84 u64 napi_packets;
85 u64 napi_bytes;
86 u64 xsk;
87 };
88
89 struct virtnet_sq_stats {
90 struct u64_stats_sync syncp;
91 u64_stats_t packets;
92 u64_stats_t bytes;
93 u64_stats_t xdp_tx;
94 u64_stats_t xdp_tx_drops;
95 u64_stats_t kicks;
96 u64_stats_t tx_timeouts;
97 u64_stats_t stop;
98 u64_stats_t wake;
99 };
100
101 struct virtnet_rq_stats {
102 struct u64_stats_sync syncp;
103 u64_stats_t packets;
104 u64_stats_t bytes;
105 u64_stats_t drops;
106 u64_stats_t xdp_packets;
107 u64_stats_t xdp_tx;
108 u64_stats_t xdp_redirects;
109 u64_stats_t xdp_drops;
110 u64_stats_t kicks;
111 };
112
113 #define VIRTNET_SQ_STAT(name, m) {name, offsetof(struct virtnet_sq_stats, m), -1}
114 #define VIRTNET_RQ_STAT(name, m) {name, offsetof(struct virtnet_rq_stats, m), -1}
115
116 #define VIRTNET_SQ_STAT_QSTAT(name, m) \
117 { \
118 name, \
119 offsetof(struct virtnet_sq_stats, m), \
120 offsetof(struct netdev_queue_stats_tx, m), \
121 }
122
123 #define VIRTNET_RQ_STAT_QSTAT(name, m) \
124 { \
125 name, \
126 offsetof(struct virtnet_rq_stats, m), \
127 offsetof(struct netdev_queue_stats_rx, m), \
128 }
129
130 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
131 VIRTNET_SQ_STAT("xdp_tx", xdp_tx),
132 VIRTNET_SQ_STAT("xdp_tx_drops", xdp_tx_drops),
133 VIRTNET_SQ_STAT("kicks", kicks),
134 VIRTNET_SQ_STAT("tx_timeouts", tx_timeouts),
135 };
136
137 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
138 VIRTNET_RQ_STAT("drops", drops),
139 VIRTNET_RQ_STAT("xdp_packets", xdp_packets),
140 VIRTNET_RQ_STAT("xdp_tx", xdp_tx),
141 VIRTNET_RQ_STAT("xdp_redirects", xdp_redirects),
142 VIRTNET_RQ_STAT("xdp_drops", xdp_drops),
143 VIRTNET_RQ_STAT("kicks", kicks),
144 };
145
146 static const struct virtnet_stat_desc virtnet_sq_stats_desc_qstat[] = {
147 VIRTNET_SQ_STAT_QSTAT("packets", packets),
148 VIRTNET_SQ_STAT_QSTAT("bytes", bytes),
149 VIRTNET_SQ_STAT_QSTAT("stop", stop),
150 VIRTNET_SQ_STAT_QSTAT("wake", wake),
151 };
152
153 static const struct virtnet_stat_desc virtnet_rq_stats_desc_qstat[] = {
154 VIRTNET_RQ_STAT_QSTAT("packets", packets),
155 VIRTNET_RQ_STAT_QSTAT("bytes", bytes),
156 };
157
158 #define VIRTNET_STATS_DESC_CQ(name) \
159 {#name, offsetof(struct virtio_net_stats_cvq, name), -1}
160
161 #define VIRTNET_STATS_DESC_RX(class, name) \
162 {#name, offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), -1}
163
164 #define VIRTNET_STATS_DESC_TX(class, name) \
165 {#name, offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), -1}
166
167
168 static const struct virtnet_stat_desc virtnet_stats_cvq_desc[] = {
169 VIRTNET_STATS_DESC_CQ(command_num),
170 VIRTNET_STATS_DESC_CQ(ok_num),
171 };
172
173 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc[] = {
174 VIRTNET_STATS_DESC_RX(basic, packets),
175 VIRTNET_STATS_DESC_RX(basic, bytes),
176
177 VIRTNET_STATS_DESC_RX(basic, notifications),
178 VIRTNET_STATS_DESC_RX(basic, interrupts),
179 };
180
181 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc[] = {
182 VIRTNET_STATS_DESC_TX(basic, packets),
183 VIRTNET_STATS_DESC_TX(basic, bytes),
184
185 VIRTNET_STATS_DESC_TX(basic, notifications),
186 VIRTNET_STATS_DESC_TX(basic, interrupts),
187 };
188
189 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc[] = {
190 VIRTNET_STATS_DESC_RX(csum, needs_csum),
191 };
192
193 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc[] = {
194 VIRTNET_STATS_DESC_TX(gso, gso_packets_noseg),
195 VIRTNET_STATS_DESC_TX(gso, gso_bytes_noseg),
196 };
197
198 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc[] = {
199 VIRTNET_STATS_DESC_RX(speed, ratelimit_bytes),
200 };
201
202 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc[] = {
203 VIRTNET_STATS_DESC_TX(speed, ratelimit_bytes),
204 };
205
206 #define VIRTNET_STATS_DESC_RX_QSTAT(class, name, qstat_field) \
207 { \
208 #name, \
209 offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), \
210 offsetof(struct netdev_queue_stats_rx, qstat_field), \
211 }
212
213 #define VIRTNET_STATS_DESC_TX_QSTAT(class, name, qstat_field) \
214 { \
215 #name, \
216 offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), \
217 offsetof(struct netdev_queue_stats_tx, qstat_field), \
218 }
219
220 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc_qstat[] = {
221 VIRTNET_STATS_DESC_RX_QSTAT(basic, drops, hw_drops),
222 VIRTNET_STATS_DESC_RX_QSTAT(basic, drop_overruns, hw_drop_overruns),
223 };
224
225 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc_qstat[] = {
226 VIRTNET_STATS_DESC_TX_QSTAT(basic, drops, hw_drops),
227 VIRTNET_STATS_DESC_TX_QSTAT(basic, drop_malformed, hw_drop_errors),
228 };
229
230 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc_qstat[] = {
231 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_valid, csum_unnecessary),
232 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_none, csum_none),
233 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_bad, csum_bad),
234 };
235
236 static const struct virtnet_stat_desc virtnet_stats_tx_csum_desc_qstat[] = {
237 VIRTNET_STATS_DESC_TX_QSTAT(csum, csum_none, csum_none),
238 VIRTNET_STATS_DESC_TX_QSTAT(csum, needs_csum, needs_csum),
239 };
240
241 static const struct virtnet_stat_desc virtnet_stats_rx_gso_desc_qstat[] = {
242 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets, hw_gro_packets),
243 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes, hw_gro_bytes),
244 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets_coalesced, hw_gro_wire_packets),
245 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes_coalesced, hw_gro_wire_bytes),
246 };
247
248 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc_qstat[] = {
249 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_packets, hw_gso_packets),
250 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_bytes, hw_gso_bytes),
251 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments, hw_gso_wire_packets),
252 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments_bytes, hw_gso_wire_bytes),
253 };
254
255 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc_qstat[] = {
256 VIRTNET_STATS_DESC_RX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
257 };
258
259 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
260 VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
261 };
262
263 #define VIRTNET_Q_TYPE_RX 0
264 #define VIRTNET_Q_TYPE_TX 1
265 #define VIRTNET_Q_TYPE_CQ 2
266
267 struct virtnet_interrupt_coalesce {
268 u32 max_packets;
269 u32 max_usecs;
270 };
271
272 /* The dma information of pages allocated at a time. */
273 struct virtnet_rq_dma {
274 dma_addr_t addr;
275 u32 ref;
276 u16 len;
277 u16 need_sync;
278 };
279
280 /* Internal representation of a send virtqueue */
281 struct send_queue {
282 /* Virtqueue associated with this send _queue */
283 struct virtqueue *vq;
284
285 /* TX: fragments + linear part + virtio header */
286 struct scatterlist sg[MAX_SKB_FRAGS + 2];
287
288 /* Name of the send queue: output.$index */
289 char name[16];
290
291 struct virtnet_sq_stats stats;
292
293 struct virtnet_interrupt_coalesce intr_coal;
294
295 struct napi_struct napi;
296
297 /* Record whether sq is in reset state. */
298 bool reset;
299
300 struct xsk_buff_pool *xsk_pool;
301
302 dma_addr_t xsk_hdr_dma_addr;
303 };
304
305 /* Internal representation of a receive virtqueue */
306 struct receive_queue {
307 /* Virtqueue associated with this receive_queue */
308 struct virtqueue *vq;
309
310 struct napi_struct napi;
311
312 struct bpf_prog __rcu *xdp_prog;
313
314 struct virtnet_rq_stats stats;
315
316 /* The number of rx notifications */
317 u16 calls;
318
319 /* Is dynamic interrupt moderation enabled? */
320 bool dim_enabled;
321
322 /* Used to protect dim_enabled and inter_coal */
323 struct mutex dim_lock;
324
325 /* Dynamic Interrupt Moderation */
326 struct dim dim;
327
328 u32 packets_in_napi;
329
330 struct virtnet_interrupt_coalesce intr_coal;
331
332 /* Chain pages by the private ptr. */
333 struct page *pages;
334
335 /* Average packet length for mergeable receive buffers. */
336 struct ewma_pkt_len mrg_avg_pkt_len;
337
338 /* Page frag for packet buffer allocation. */
339 struct page_frag alloc_frag;
340
341 /* RX: fragments + linear part + virtio header */
342 struct scatterlist sg[MAX_SKB_FRAGS + 2];
343
344 /* Min single buffer size for mergeable buffers case. */
345 unsigned int min_buf_len;
346
347 /* Name of this receive queue: input.$index */
348 char name[16];
349
350 struct xdp_rxq_info xdp_rxq;
351
352 /* Record the last dma info to free after new pages is allocated. */
353 struct virtnet_rq_dma *last_dma;
354
355 struct xsk_buff_pool *xsk_pool;
356
357 /* xdp rxq used by xsk */
358 struct xdp_rxq_info xsk_rxq_info;
359
360 struct xdp_buff **xsk_buffs;
361 };
362
363 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
364
365 /* Control VQ buffers: protected by the rtnl lock */
366 struct control_buf {
367 struct virtio_net_ctrl_hdr hdr;
368 virtio_net_ctrl_ack status;
369 };
370
371 struct virtnet_info {
372 struct virtio_device *vdev;
373 struct virtqueue *cvq;
374 struct net_device *dev;
375 struct send_queue *sq;
376 struct receive_queue *rq;
377 unsigned int status;
378
379 /* Max # of queue pairs supported by the device */
380 u16 max_queue_pairs;
381
382 /* # of queue pairs currently used by the driver */
383 u16 curr_queue_pairs;
384
385 /* # of XDP queue pairs currently used by the driver */
386 u16 xdp_queue_pairs;
387
388 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
389 bool xdp_enabled;
390
391 /* I like... big packets and I cannot lie! */
392 bool big_packets;
393
394 /* number of sg entries allocated for big packets */
395 unsigned int big_packets_num_skbfrags;
396
397 /* Host will merge rx buffers for big packets (shake it! shake it!) */
398 bool mergeable_rx_bufs;
399
400 /* Host supports rss and/or hash report */
401 bool has_rss;
402 bool has_rss_hash_report;
403 u8 rss_key_size;
404 u16 rss_indir_table_size;
405 u32 rss_hash_types_supported;
406 u32 rss_hash_types_saved;
407 struct virtio_net_rss_config_hdr *rss_hdr;
408 struct virtio_net_rss_config_trailer rss_trailer;
409 u8 rss_hash_key_data[VIRTIO_NET_RSS_MAX_KEY_SIZE];
410
411 /* Has control virtqueue */
412 bool has_cvq;
413
414 /* Lock to protect the control VQ */
415 struct mutex cvq_lock;
416
417 /* Host can handle any s/g split between our header and packet data */
418 bool any_header_sg;
419
420 /* Packet virtio header size */
421 u8 hdr_len;
422
423 /* Work struct for delayed refilling if we run low on memory. */
424 struct delayed_work refill;
425
426 /* Is delayed refill enabled? */
427 bool refill_enabled;
428
429 /* The lock to synchronize the access to refill_enabled */
430 spinlock_t refill_lock;
431
432 /* Work struct for config space updates */
433 struct work_struct config_work;
434
435 /* Work struct for setting rx mode */
436 struct work_struct rx_mode_work;
437
438 /* OK to queue work setting RX mode? */
439 bool rx_mode_work_enabled;
440
441 /* Does the affinity hint is set for virtqueues? */
442 bool affinity_hint_set;
443
444 /* CPU hotplug instances for online & dead */
445 struct hlist_node node;
446 struct hlist_node node_dead;
447
448 struct control_buf *ctrl;
449
450 /* Ethtool settings */
451 u8 duplex;
452 u32 speed;
453
454 /* Is rx dynamic interrupt moderation enabled? */
455 bool rx_dim_enabled;
456
457 /* Interrupt coalescing settings */
458 struct virtnet_interrupt_coalesce intr_coal_tx;
459 struct virtnet_interrupt_coalesce intr_coal_rx;
460
461 unsigned long guest_offloads;
462 unsigned long guest_offloads_capable;
463
464 /* failover when STANDBY feature enabled */
465 struct failover *failover;
466
467 u64 device_stats_cap;
468 };
469
470 struct padded_vnet_hdr {
471 struct virtio_net_hdr_v1_hash hdr;
472 /*
473 * hdr is in a separate sg buffer, and data sg buffer shares same page
474 * with this header sg. This padding makes next sg 16 byte aligned
475 * after the header.
476 */
477 char padding[12];
478 };
479
480 struct virtio_net_common_hdr {
481 union {
482 struct virtio_net_hdr hdr;
483 struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
484 struct virtio_net_hdr_v1_hash hash_v1_hdr;
485 };
486 };
487
488 static struct virtio_net_common_hdr xsk_hdr;
489
490 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
491 static void virtnet_sq_free_unused_buf_done(struct virtqueue *vq);
492 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
493 struct net_device *dev,
494 unsigned int *xdp_xmit,
495 struct virtnet_rq_stats *stats);
496 static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
497 struct sk_buff *skb, u8 flags);
498 static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
499 struct sk_buff *curr_skb,
500 struct page *page, void *buf,
501 int len, int truesize);
502 static void virtnet_xsk_completed(struct send_queue *sq, int num);
503
504 enum virtnet_xmit_type {
505 VIRTNET_XMIT_TYPE_SKB,
506 VIRTNET_XMIT_TYPE_SKB_ORPHAN,
507 VIRTNET_XMIT_TYPE_XDP,
508 VIRTNET_XMIT_TYPE_XSK,
509 };
510
virtnet_rss_hdr_size(const struct virtnet_info * vi)511 static size_t virtnet_rss_hdr_size(const struct virtnet_info *vi)
512 {
513 u16 indir_table_size = vi->has_rss ? vi->rss_indir_table_size : 1;
514
515 return struct_size(vi->rss_hdr, indirection_table, indir_table_size);
516 }
517
virtnet_rss_trailer_size(const struct virtnet_info * vi)518 static size_t virtnet_rss_trailer_size(const struct virtnet_info *vi)
519 {
520 return struct_size(&vi->rss_trailer, hash_key_data, vi->rss_key_size);
521 }
522
523 /* We use the last two bits of the pointer to distinguish the xmit type. */
524 #define VIRTNET_XMIT_TYPE_MASK (BIT(0) | BIT(1))
525
526 #define VIRTIO_XSK_FLAG_OFFSET 2
527
virtnet_xmit_ptr_unpack(void ** ptr)528 static enum virtnet_xmit_type virtnet_xmit_ptr_unpack(void **ptr)
529 {
530 unsigned long p = (unsigned long)*ptr;
531
532 *ptr = (void *)(p & ~VIRTNET_XMIT_TYPE_MASK);
533
534 return p & VIRTNET_XMIT_TYPE_MASK;
535 }
536
virtnet_xmit_ptr_pack(void * ptr,enum virtnet_xmit_type type)537 static void *virtnet_xmit_ptr_pack(void *ptr, enum virtnet_xmit_type type)
538 {
539 return (void *)((unsigned long)ptr | type);
540 }
541
virtnet_add_outbuf(struct send_queue * sq,int num,void * data,enum virtnet_xmit_type type)542 static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
543 enum virtnet_xmit_type type)
544 {
545 return virtqueue_add_outbuf(sq->vq, sq->sg, num,
546 virtnet_xmit_ptr_pack(data, type),
547 GFP_ATOMIC);
548 }
549
virtnet_ptr_to_xsk_buff_len(void * ptr)550 static u32 virtnet_ptr_to_xsk_buff_len(void *ptr)
551 {
552 return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
553 }
554
sg_fill_dma(struct scatterlist * sg,dma_addr_t addr,u32 len)555 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
556 {
557 sg_dma_address(sg) = addr;
558 sg_dma_len(sg) = len;
559 }
560
__free_old_xmit(struct send_queue * sq,struct netdev_queue * txq,bool in_napi,struct virtnet_sq_free_stats * stats)561 static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
562 bool in_napi, struct virtnet_sq_free_stats *stats)
563 {
564 struct xdp_frame *frame;
565 struct sk_buff *skb;
566 unsigned int len;
567 void *ptr;
568
569 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
570 switch (virtnet_xmit_ptr_unpack(&ptr)) {
571 case VIRTNET_XMIT_TYPE_SKB:
572 skb = ptr;
573
574 pr_debug("Sent skb %p\n", skb);
575 stats->napi_packets++;
576 stats->napi_bytes += skb->len;
577 napi_consume_skb(skb, in_napi);
578 break;
579
580 case VIRTNET_XMIT_TYPE_SKB_ORPHAN:
581 skb = ptr;
582
583 stats->packets++;
584 stats->bytes += skb->len;
585 napi_consume_skb(skb, in_napi);
586 break;
587
588 case VIRTNET_XMIT_TYPE_XDP:
589 frame = ptr;
590
591 stats->packets++;
592 stats->bytes += xdp_get_frame_len(frame);
593 xdp_return_frame(frame);
594 break;
595
596 case VIRTNET_XMIT_TYPE_XSK:
597 stats->bytes += virtnet_ptr_to_xsk_buff_len(ptr);
598 stats->xsk++;
599 break;
600 }
601 }
602 netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
603 }
604
virtnet_free_old_xmit(struct send_queue * sq,struct netdev_queue * txq,bool in_napi,struct virtnet_sq_free_stats * stats)605 static void virtnet_free_old_xmit(struct send_queue *sq,
606 struct netdev_queue *txq,
607 bool in_napi,
608 struct virtnet_sq_free_stats *stats)
609 {
610 __free_old_xmit(sq, txq, in_napi, stats);
611
612 if (stats->xsk)
613 virtnet_xsk_completed(sq, stats->xsk);
614 }
615
616 /* Converting between virtqueue no. and kernel tx/rx queue no.
617 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
618 */
vq2txq(struct virtqueue * vq)619 static int vq2txq(struct virtqueue *vq)
620 {
621 return (vq->index - 1) / 2;
622 }
623
txq2vq(int txq)624 static int txq2vq(int txq)
625 {
626 return txq * 2 + 1;
627 }
628
vq2rxq(struct virtqueue * vq)629 static int vq2rxq(struct virtqueue *vq)
630 {
631 return vq->index / 2;
632 }
633
rxq2vq(int rxq)634 static int rxq2vq(int rxq)
635 {
636 return rxq * 2;
637 }
638
vq_type(struct virtnet_info * vi,int qid)639 static int vq_type(struct virtnet_info *vi, int qid)
640 {
641 if (qid == vi->max_queue_pairs * 2)
642 return VIRTNET_Q_TYPE_CQ;
643
644 if (qid % 2)
645 return VIRTNET_Q_TYPE_TX;
646
647 return VIRTNET_Q_TYPE_RX;
648 }
649
650 static inline struct virtio_net_common_hdr *
skb_vnet_common_hdr(struct sk_buff * skb)651 skb_vnet_common_hdr(struct sk_buff *skb)
652 {
653 return (struct virtio_net_common_hdr *)skb->cb;
654 }
655
656 /*
657 * private is used to chain pages for big packets, put the whole
658 * most recent used list in the beginning for reuse
659 */
give_pages(struct receive_queue * rq,struct page * page)660 static void give_pages(struct receive_queue *rq, struct page *page)
661 {
662 struct page *end;
663
664 /* Find end of list, sew whole thing into vi->rq.pages. */
665 for (end = page; end->private; end = (struct page *)end->private);
666 end->private = (unsigned long)rq->pages;
667 rq->pages = page;
668 }
669
get_a_page(struct receive_queue * rq,gfp_t gfp_mask)670 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
671 {
672 struct page *p = rq->pages;
673
674 if (p) {
675 rq->pages = (struct page *)p->private;
676 /* clear private here, it is used to chain pages */
677 p->private = 0;
678 } else
679 p = alloc_page(gfp_mask);
680 return p;
681 }
682
virtnet_rq_free_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf)683 static void virtnet_rq_free_buf(struct virtnet_info *vi,
684 struct receive_queue *rq, void *buf)
685 {
686 if (vi->mergeable_rx_bufs)
687 put_page(virt_to_head_page(buf));
688 else if (vi->big_packets)
689 give_pages(rq, buf);
690 else
691 put_page(virt_to_head_page(buf));
692 }
693
enable_delayed_refill(struct virtnet_info * vi)694 static void enable_delayed_refill(struct virtnet_info *vi)
695 {
696 spin_lock_bh(&vi->refill_lock);
697 vi->refill_enabled = true;
698 spin_unlock_bh(&vi->refill_lock);
699 }
700
disable_delayed_refill(struct virtnet_info * vi)701 static void disable_delayed_refill(struct virtnet_info *vi)
702 {
703 spin_lock_bh(&vi->refill_lock);
704 vi->refill_enabled = false;
705 spin_unlock_bh(&vi->refill_lock);
706 }
707
enable_rx_mode_work(struct virtnet_info * vi)708 static void enable_rx_mode_work(struct virtnet_info *vi)
709 {
710 rtnl_lock();
711 vi->rx_mode_work_enabled = true;
712 rtnl_unlock();
713 }
714
disable_rx_mode_work(struct virtnet_info * vi)715 static void disable_rx_mode_work(struct virtnet_info *vi)
716 {
717 rtnl_lock();
718 vi->rx_mode_work_enabled = false;
719 rtnl_unlock();
720 }
721
virtqueue_napi_schedule(struct napi_struct * napi,struct virtqueue * vq)722 static void virtqueue_napi_schedule(struct napi_struct *napi,
723 struct virtqueue *vq)
724 {
725 if (napi_schedule_prep(napi)) {
726 virtqueue_disable_cb(vq);
727 __napi_schedule(napi);
728 }
729 }
730
virtqueue_napi_complete(struct napi_struct * napi,struct virtqueue * vq,int processed)731 static bool virtqueue_napi_complete(struct napi_struct *napi,
732 struct virtqueue *vq, int processed)
733 {
734 int opaque;
735
736 opaque = virtqueue_enable_cb_prepare(vq);
737 if (napi_complete_done(napi, processed)) {
738 if (unlikely(virtqueue_poll(vq, opaque)))
739 virtqueue_napi_schedule(napi, vq);
740 else
741 return true;
742 } else {
743 virtqueue_disable_cb(vq);
744 }
745
746 return false;
747 }
748
skb_xmit_done(struct virtqueue * vq)749 static void skb_xmit_done(struct virtqueue *vq)
750 {
751 struct virtnet_info *vi = vq->vdev->priv;
752 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
753
754 /* Suppress further interrupts. */
755 virtqueue_disable_cb(vq);
756
757 if (napi->weight)
758 virtqueue_napi_schedule(napi, vq);
759 else
760 /* We were probably waiting for more output buffers. */
761 netif_wake_subqueue(vi->dev, vq2txq(vq));
762 }
763
764 #define MRG_CTX_HEADER_SHIFT 22
mergeable_len_to_ctx(unsigned int truesize,unsigned int headroom)765 static void *mergeable_len_to_ctx(unsigned int truesize,
766 unsigned int headroom)
767 {
768 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
769 }
770
mergeable_ctx_to_headroom(void * mrg_ctx)771 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
772 {
773 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
774 }
775
mergeable_ctx_to_truesize(void * mrg_ctx)776 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
777 {
778 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
779 }
780
check_mergeable_len(struct net_device * dev,void * mrg_ctx,unsigned int len)781 static int check_mergeable_len(struct net_device *dev, void *mrg_ctx,
782 unsigned int len)
783 {
784 unsigned int headroom, tailroom, room, truesize;
785
786 truesize = mergeable_ctx_to_truesize(mrg_ctx);
787 headroom = mergeable_ctx_to_headroom(mrg_ctx);
788 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
789 room = SKB_DATA_ALIGN(headroom + tailroom);
790
791 if (len > truesize - room) {
792 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
793 dev->name, len, (unsigned long)(truesize - room));
794 DEV_STATS_INC(dev, rx_length_errors);
795 return -1;
796 }
797
798 return 0;
799 }
800
virtnet_build_skb(void * buf,unsigned int buflen,unsigned int headroom,unsigned int len)801 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
802 unsigned int headroom,
803 unsigned int len)
804 {
805 struct sk_buff *skb;
806
807 skb = build_skb(buf, buflen);
808 if (unlikely(!skb))
809 return NULL;
810
811 skb_reserve(skb, headroom);
812 skb_put(skb, len);
813
814 return skb;
815 }
816
817 /* Called from bottom half context */
page_to_skb(struct virtnet_info * vi,struct receive_queue * rq,struct page * page,unsigned int offset,unsigned int len,unsigned int truesize,unsigned int headroom)818 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
819 struct receive_queue *rq,
820 struct page *page, unsigned int offset,
821 unsigned int len, unsigned int truesize,
822 unsigned int headroom)
823 {
824 struct sk_buff *skb;
825 struct virtio_net_common_hdr *hdr;
826 unsigned int copy, hdr_len, hdr_padded_len;
827 struct page *page_to_free = NULL;
828 int tailroom, shinfo_size;
829 char *p, *hdr_p, *buf;
830
831 p = page_address(page) + offset;
832 hdr_p = p;
833
834 hdr_len = vi->hdr_len;
835 if (vi->mergeable_rx_bufs)
836 hdr_padded_len = hdr_len;
837 else
838 hdr_padded_len = sizeof(struct padded_vnet_hdr);
839
840 buf = p - headroom;
841 len -= hdr_len;
842 offset += hdr_padded_len;
843 p += hdr_padded_len;
844 tailroom = truesize - headroom - hdr_padded_len - len;
845
846 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
847
848 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
849 skb = virtnet_build_skb(buf, truesize, p - buf, len);
850 if (unlikely(!skb))
851 return NULL;
852
853 page = (struct page *)page->private;
854 if (page)
855 give_pages(rq, page);
856 goto ok;
857 }
858
859 /* copy small packet so we can reuse these pages for small data */
860 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
861 if (unlikely(!skb))
862 return NULL;
863
864 /* Copy all frame if it fits skb->head, otherwise
865 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
866 */
867 if (len <= skb_tailroom(skb))
868 copy = len;
869 else
870 copy = ETH_HLEN;
871 skb_put_data(skb, p, copy);
872
873 len -= copy;
874 offset += copy;
875
876 if (vi->mergeable_rx_bufs) {
877 if (len)
878 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
879 else
880 page_to_free = page;
881 goto ok;
882 }
883
884 /*
885 * Verify that we can indeed put this data into a skb.
886 * This is here to handle cases when the device erroneously
887 * tries to receive more than is possible. This is usually
888 * the case of a broken device.
889 */
890 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
891 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
892 dev_kfree_skb(skb);
893 return NULL;
894 }
895 BUG_ON(offset >= PAGE_SIZE);
896 while (len) {
897 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
898 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
899 frag_size, truesize);
900 len -= frag_size;
901 page = (struct page *)page->private;
902 offset = 0;
903 }
904
905 if (page)
906 give_pages(rq, page);
907
908 ok:
909 hdr = skb_vnet_common_hdr(skb);
910 memcpy(hdr, hdr_p, hdr_len);
911 if (page_to_free)
912 put_page(page_to_free);
913
914 return skb;
915 }
916
virtnet_rq_unmap(struct receive_queue * rq,void * buf,u32 len)917 static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
918 {
919 struct virtnet_info *vi = rq->vq->vdev->priv;
920 struct page *page = virt_to_head_page(buf);
921 struct virtnet_rq_dma *dma;
922 void *head;
923 int offset;
924
925 BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
926
927 head = page_address(page);
928
929 dma = head;
930
931 --dma->ref;
932
933 if (dma->need_sync && len) {
934 offset = buf - (head + sizeof(*dma));
935
936 virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr,
937 offset, len,
938 DMA_FROM_DEVICE);
939 }
940
941 if (dma->ref)
942 return;
943
944 virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len,
945 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
946 put_page(page);
947 }
948
virtnet_rq_get_buf(struct receive_queue * rq,u32 * len,void ** ctx)949 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
950 {
951 struct virtnet_info *vi = rq->vq->vdev->priv;
952 void *buf;
953
954 BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
955
956 buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
957 if (buf)
958 virtnet_rq_unmap(rq, buf, *len);
959
960 return buf;
961 }
962
virtnet_rq_init_one_sg(struct receive_queue * rq,void * buf,u32 len)963 static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
964 {
965 struct virtnet_info *vi = rq->vq->vdev->priv;
966 struct virtnet_rq_dma *dma;
967 dma_addr_t addr;
968 u32 offset;
969 void *head;
970
971 BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
972
973 head = page_address(rq->alloc_frag.page);
974
975 offset = buf - head;
976
977 dma = head;
978
979 addr = dma->addr - sizeof(*dma) + offset;
980
981 sg_init_table(rq->sg, 1);
982 sg_fill_dma(rq->sg, addr, len);
983 }
984
virtnet_rq_alloc(struct receive_queue * rq,u32 size,gfp_t gfp)985 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
986 {
987 struct page_frag *alloc_frag = &rq->alloc_frag;
988 struct virtnet_info *vi = rq->vq->vdev->priv;
989 struct virtnet_rq_dma *dma;
990 void *buf, *head;
991 dma_addr_t addr;
992
993 BUG_ON(vi->big_packets && !vi->mergeable_rx_bufs);
994
995 head = page_address(alloc_frag->page);
996
997 dma = head;
998
999 /* new pages */
1000 if (!alloc_frag->offset) {
1001 if (rq->last_dma) {
1002 /* Now, the new page is allocated, the last dma
1003 * will not be used. So the dma can be unmapped
1004 * if the ref is 0.
1005 */
1006 virtnet_rq_unmap(rq, rq->last_dma, 0);
1007 rq->last_dma = NULL;
1008 }
1009
1010 dma->len = alloc_frag->size - sizeof(*dma);
1011
1012 addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1,
1013 dma->len, DMA_FROM_DEVICE, 0);
1014 if (virtqueue_dma_mapping_error(rq->vq, addr))
1015 return NULL;
1016
1017 dma->addr = addr;
1018 dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr);
1019
1020 /* Add a reference to dma to prevent the entire dma from
1021 * being released during error handling. This reference
1022 * will be freed after the pages are no longer used.
1023 */
1024 get_page(alloc_frag->page);
1025 dma->ref = 1;
1026 alloc_frag->offset = sizeof(*dma);
1027
1028 rq->last_dma = dma;
1029 }
1030
1031 ++dma->ref;
1032
1033 buf = head + alloc_frag->offset;
1034
1035 get_page(alloc_frag->page);
1036 alloc_frag->offset += size;
1037
1038 return buf;
1039 }
1040
virtnet_rq_unmap_free_buf(struct virtqueue * vq,void * buf)1041 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
1042 {
1043 struct virtnet_info *vi = vq->vdev->priv;
1044 struct receive_queue *rq;
1045 int i = vq2rxq(vq);
1046
1047 rq = &vi->rq[i];
1048
1049 if (rq->xsk_pool) {
1050 xsk_buff_free((struct xdp_buff *)buf);
1051 return;
1052 }
1053
1054 if (!vi->big_packets || vi->mergeable_rx_bufs)
1055 virtnet_rq_unmap(rq, buf, 0);
1056
1057 virtnet_rq_free_buf(vi, rq, buf);
1058 }
1059
free_old_xmit(struct send_queue * sq,struct netdev_queue * txq,bool in_napi)1060 static void free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
1061 bool in_napi)
1062 {
1063 struct virtnet_sq_free_stats stats = {0};
1064
1065 virtnet_free_old_xmit(sq, txq, in_napi, &stats);
1066
1067 /* Avoid overhead when no packets have been processed
1068 * happens when called speculatively from start_xmit.
1069 */
1070 if (!stats.packets && !stats.napi_packets)
1071 return;
1072
1073 u64_stats_update_begin(&sq->stats.syncp);
1074 u64_stats_add(&sq->stats.bytes, stats.bytes + stats.napi_bytes);
1075 u64_stats_add(&sq->stats.packets, stats.packets + stats.napi_packets);
1076 u64_stats_update_end(&sq->stats.syncp);
1077 }
1078
is_xdp_raw_buffer_queue(struct virtnet_info * vi,int q)1079 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1080 {
1081 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1082 return false;
1083 else if (q < vi->curr_queue_pairs)
1084 return true;
1085 else
1086 return false;
1087 }
1088
tx_may_stop(struct virtnet_info * vi,struct net_device * dev,struct send_queue * sq)1089 static bool tx_may_stop(struct virtnet_info *vi,
1090 struct net_device *dev,
1091 struct send_queue *sq)
1092 {
1093 int qnum;
1094
1095 qnum = sq - vi->sq;
1096
1097 /* If running out of space, stop queue to avoid getting packets that we
1098 * are then unable to transmit.
1099 * An alternative would be to force queuing layer to requeue the skb by
1100 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1101 * returned in a normal path of operation: it means that driver is not
1102 * maintaining the TX queue stop/start state properly, and causes
1103 * the stack to do a non-trivial amount of useless work.
1104 * Since most packets only take 1 or 2 ring slots, stopping the queue
1105 * early means 16 slots are typically wasted.
1106 */
1107 if (sq->vq->num_free < MAX_SKB_FRAGS + 2) {
1108 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1109
1110 netif_tx_stop_queue(txq);
1111 u64_stats_update_begin(&sq->stats.syncp);
1112 u64_stats_inc(&sq->stats.stop);
1113 u64_stats_update_end(&sq->stats.syncp);
1114
1115 return true;
1116 }
1117
1118 return false;
1119 }
1120
check_sq_full_and_disable(struct virtnet_info * vi,struct net_device * dev,struct send_queue * sq)1121 static void check_sq_full_and_disable(struct virtnet_info *vi,
1122 struct net_device *dev,
1123 struct send_queue *sq)
1124 {
1125 bool use_napi = sq->napi.weight;
1126 int qnum;
1127
1128 qnum = sq - vi->sq;
1129
1130 if (tx_may_stop(vi, dev, sq)) {
1131 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1132
1133 if (use_napi) {
1134 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
1135 virtqueue_napi_schedule(&sq->napi, sq->vq);
1136 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1137 /* More just got used, free them then recheck. */
1138 free_old_xmit(sq, txq, false);
1139 if (sq->vq->num_free >= MAX_SKB_FRAGS + 2) {
1140 netif_start_subqueue(dev, qnum);
1141 u64_stats_update_begin(&sq->stats.syncp);
1142 u64_stats_inc(&sq->stats.wake);
1143 u64_stats_update_end(&sq->stats.syncp);
1144 virtqueue_disable_cb(sq->vq);
1145 }
1146 }
1147 }
1148 }
1149
1150 /* Note that @len is the length of received data without virtio header */
buf_to_xdp(struct virtnet_info * vi,struct receive_queue * rq,void * buf,u32 len,bool first_buf)1151 static struct xdp_buff *buf_to_xdp(struct virtnet_info *vi,
1152 struct receive_queue *rq, void *buf,
1153 u32 len, bool first_buf)
1154 {
1155 struct xdp_buff *xdp;
1156 u32 bufsize;
1157
1158 xdp = (struct xdp_buff *)buf;
1159
1160 /* In virtnet_add_recvbuf_xsk, we use part of XDP_PACKET_HEADROOM for
1161 * virtio header and ask the vhost to fill data from
1162 * hard_start + XDP_PACKET_HEADROOM - vi->hdr_len
1163 * The first buffer has virtio header so the remaining region for frame
1164 * data is
1165 * xsk_pool_get_rx_frame_size()
1166 * While other buffers than the first one do not have virtio header, so
1167 * the maximum frame data's length can be
1168 * xsk_pool_get_rx_frame_size() + vi->hdr_len
1169 */
1170 bufsize = xsk_pool_get_rx_frame_size(rq->xsk_pool);
1171 if (!first_buf)
1172 bufsize += vi->hdr_len;
1173
1174 if (unlikely(len > bufsize)) {
1175 pr_debug("%s: rx error: len %u exceeds truesize %u\n",
1176 vi->dev->name, len, bufsize);
1177 DEV_STATS_INC(vi->dev, rx_length_errors);
1178 xsk_buff_free(xdp);
1179 return NULL;
1180 }
1181
1182 xsk_buff_set_size(xdp, len);
1183 xsk_buff_dma_sync_for_cpu(xdp);
1184
1185 return xdp;
1186 }
1187
xsk_construct_skb(struct receive_queue * rq,struct xdp_buff * xdp)1188 static struct sk_buff *xsk_construct_skb(struct receive_queue *rq,
1189 struct xdp_buff *xdp)
1190 {
1191 unsigned int metasize = xdp->data - xdp->data_meta;
1192 struct sk_buff *skb;
1193 unsigned int size;
1194
1195 size = xdp->data_end - xdp->data_hard_start;
1196 skb = napi_alloc_skb(&rq->napi, size);
1197 if (unlikely(!skb)) {
1198 xsk_buff_free(xdp);
1199 return NULL;
1200 }
1201
1202 skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
1203
1204 size = xdp->data_end - xdp->data_meta;
1205 memcpy(__skb_put(skb, size), xdp->data_meta, size);
1206
1207 if (metasize) {
1208 __skb_pull(skb, metasize);
1209 skb_metadata_set(skb, metasize);
1210 }
1211
1212 xsk_buff_free(xdp);
1213
1214 return skb;
1215 }
1216
virtnet_receive_xsk_small(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1217 static struct sk_buff *virtnet_receive_xsk_small(struct net_device *dev, struct virtnet_info *vi,
1218 struct receive_queue *rq, struct xdp_buff *xdp,
1219 unsigned int *xdp_xmit,
1220 struct virtnet_rq_stats *stats)
1221 {
1222 struct bpf_prog *prog;
1223 u32 ret;
1224
1225 ret = XDP_PASS;
1226 rcu_read_lock();
1227 prog = rcu_dereference(rq->xdp_prog);
1228 if (prog)
1229 ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit, stats);
1230 rcu_read_unlock();
1231
1232 switch (ret) {
1233 case XDP_PASS:
1234 return xsk_construct_skb(rq, xdp);
1235
1236 case XDP_TX:
1237 case XDP_REDIRECT:
1238 return NULL;
1239
1240 default:
1241 /* drop packet */
1242 xsk_buff_free(xdp);
1243 u64_stats_inc(&stats->drops);
1244 return NULL;
1245 }
1246 }
1247
xsk_drop_follow_bufs(struct net_device * dev,struct receive_queue * rq,u32 num_buf,struct virtnet_rq_stats * stats)1248 static void xsk_drop_follow_bufs(struct net_device *dev,
1249 struct receive_queue *rq,
1250 u32 num_buf,
1251 struct virtnet_rq_stats *stats)
1252 {
1253 struct xdp_buff *xdp;
1254 u32 len;
1255
1256 while (num_buf-- > 1) {
1257 xdp = virtqueue_get_buf(rq->vq, &len);
1258 if (unlikely(!xdp)) {
1259 pr_debug("%s: rx error: %d buffers missing\n",
1260 dev->name, num_buf);
1261 DEV_STATS_INC(dev, rx_length_errors);
1262 break;
1263 }
1264 u64_stats_add(&stats->bytes, len);
1265 xsk_buff_free(xdp);
1266 }
1267 }
1268
xsk_append_merge_buffer(struct virtnet_info * vi,struct receive_queue * rq,struct sk_buff * head_skb,u32 num_buf,struct virtio_net_hdr_mrg_rxbuf * hdr,struct virtnet_rq_stats * stats)1269 static int xsk_append_merge_buffer(struct virtnet_info *vi,
1270 struct receive_queue *rq,
1271 struct sk_buff *head_skb,
1272 u32 num_buf,
1273 struct virtio_net_hdr_mrg_rxbuf *hdr,
1274 struct virtnet_rq_stats *stats)
1275 {
1276 struct sk_buff *curr_skb;
1277 struct xdp_buff *xdp;
1278 u32 len, truesize;
1279 struct page *page;
1280 void *buf;
1281
1282 curr_skb = head_skb;
1283
1284 while (--num_buf) {
1285 buf = virtqueue_get_buf(rq->vq, &len);
1286 if (unlikely(!buf)) {
1287 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1288 vi->dev->name, num_buf,
1289 virtio16_to_cpu(vi->vdev,
1290 hdr->num_buffers));
1291 DEV_STATS_INC(vi->dev, rx_length_errors);
1292 return -EINVAL;
1293 }
1294
1295 u64_stats_add(&stats->bytes, len);
1296
1297 xdp = buf_to_xdp(vi, rq, buf, len, false);
1298 if (!xdp)
1299 goto err;
1300
1301 buf = napi_alloc_frag(len);
1302 if (!buf) {
1303 xsk_buff_free(xdp);
1304 goto err;
1305 }
1306
1307 memcpy(buf, xdp->data - vi->hdr_len, len);
1308
1309 xsk_buff_free(xdp);
1310
1311 page = virt_to_page(buf);
1312
1313 truesize = len;
1314
1315 curr_skb = virtnet_skb_append_frag(head_skb, curr_skb, page,
1316 buf, len, truesize);
1317 if (!curr_skb) {
1318 put_page(page);
1319 goto err;
1320 }
1321 }
1322
1323 return 0;
1324
1325 err:
1326 xsk_drop_follow_bufs(vi->dev, rq, num_buf, stats);
1327 return -EINVAL;
1328 }
1329
virtnet_receive_xsk_merge(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1330 static struct sk_buff *virtnet_receive_xsk_merge(struct net_device *dev, struct virtnet_info *vi,
1331 struct receive_queue *rq, struct xdp_buff *xdp,
1332 unsigned int *xdp_xmit,
1333 struct virtnet_rq_stats *stats)
1334 {
1335 struct virtio_net_hdr_mrg_rxbuf *hdr;
1336 struct bpf_prog *prog;
1337 struct sk_buff *skb;
1338 u32 ret, num_buf;
1339
1340 hdr = xdp->data - vi->hdr_len;
1341 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1342
1343 ret = XDP_PASS;
1344 rcu_read_lock();
1345 prog = rcu_dereference(rq->xdp_prog);
1346 /* TODO: support multi buffer. */
1347 if (prog && num_buf == 1)
1348 ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit, stats);
1349 rcu_read_unlock();
1350
1351 switch (ret) {
1352 case XDP_PASS:
1353 skb = xsk_construct_skb(rq, xdp);
1354 if (!skb)
1355 goto drop_bufs;
1356
1357 if (xsk_append_merge_buffer(vi, rq, skb, num_buf, hdr, stats)) {
1358 dev_kfree_skb(skb);
1359 goto drop;
1360 }
1361
1362 return skb;
1363
1364 case XDP_TX:
1365 case XDP_REDIRECT:
1366 return NULL;
1367
1368 default:
1369 /* drop packet */
1370 xsk_buff_free(xdp);
1371 }
1372
1373 drop_bufs:
1374 xsk_drop_follow_bufs(dev, rq, num_buf, stats);
1375
1376 drop:
1377 u64_stats_inc(&stats->drops);
1378 return NULL;
1379 }
1380
virtnet_receive_xsk_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf,u32 len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1381 static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queue *rq,
1382 void *buf, u32 len,
1383 unsigned int *xdp_xmit,
1384 struct virtnet_rq_stats *stats)
1385 {
1386 struct net_device *dev = vi->dev;
1387 struct sk_buff *skb = NULL;
1388 struct xdp_buff *xdp;
1389 u8 flags;
1390
1391 len -= vi->hdr_len;
1392
1393 u64_stats_add(&stats->bytes, len);
1394
1395 xdp = buf_to_xdp(vi, rq, buf, len, true);
1396 if (!xdp)
1397 return;
1398
1399 if (unlikely(len < ETH_HLEN)) {
1400 pr_debug("%s: short packet %i\n", dev->name, len);
1401 DEV_STATS_INC(dev, rx_length_errors);
1402 xsk_buff_free(xdp);
1403 return;
1404 }
1405
1406 flags = ((struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len))->hdr.flags;
1407
1408 if (!vi->mergeable_rx_bufs)
1409 skb = virtnet_receive_xsk_small(dev, vi, rq, xdp, xdp_xmit, stats);
1410 else
1411 skb = virtnet_receive_xsk_merge(dev, vi, rq, xdp, xdp_xmit, stats);
1412
1413 if (skb)
1414 virtnet_receive_done(vi, rq, skb, flags);
1415 }
1416
virtnet_add_recvbuf_xsk(struct virtnet_info * vi,struct receive_queue * rq,struct xsk_buff_pool * pool,gfp_t gfp)1417 static int virtnet_add_recvbuf_xsk(struct virtnet_info *vi, struct receive_queue *rq,
1418 struct xsk_buff_pool *pool, gfp_t gfp)
1419 {
1420 struct xdp_buff **xsk_buffs;
1421 dma_addr_t addr;
1422 int err = 0;
1423 u32 len, i;
1424 int num;
1425
1426 xsk_buffs = rq->xsk_buffs;
1427
1428 num = xsk_buff_alloc_batch(pool, xsk_buffs, rq->vq->num_free);
1429 if (!num)
1430 return -ENOMEM;
1431
1432 len = xsk_pool_get_rx_frame_size(pool) + vi->hdr_len;
1433
1434 for (i = 0; i < num; ++i) {
1435 /* Use the part of XDP_PACKET_HEADROOM as the virtnet hdr space.
1436 * We assume XDP_PACKET_HEADROOM is larger than hdr->len.
1437 * (see function virtnet_xsk_pool_enable)
1438 */
1439 addr = xsk_buff_xdp_get_dma(xsk_buffs[i]) - vi->hdr_len;
1440
1441 sg_init_table(rq->sg, 1);
1442 sg_fill_dma(rq->sg, addr, len);
1443
1444 err = virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1,
1445 xsk_buffs[i], NULL, gfp);
1446 if (err)
1447 goto err;
1448 }
1449
1450 return num;
1451
1452 err:
1453 for (; i < num; ++i)
1454 xsk_buff_free(xsk_buffs[i]);
1455
1456 return err;
1457 }
1458
virtnet_xsk_to_ptr(u32 len)1459 static void *virtnet_xsk_to_ptr(u32 len)
1460 {
1461 unsigned long p;
1462
1463 p = len << VIRTIO_XSK_FLAG_OFFSET;
1464
1465 return virtnet_xmit_ptr_pack((void *)p, VIRTNET_XMIT_TYPE_XSK);
1466 }
1467
virtnet_xsk_xmit_one(struct send_queue * sq,struct xsk_buff_pool * pool,struct xdp_desc * desc)1468 static int virtnet_xsk_xmit_one(struct send_queue *sq,
1469 struct xsk_buff_pool *pool,
1470 struct xdp_desc *desc)
1471 {
1472 struct virtnet_info *vi;
1473 dma_addr_t addr;
1474
1475 vi = sq->vq->vdev->priv;
1476
1477 addr = xsk_buff_raw_get_dma(pool, desc->addr);
1478 xsk_buff_raw_dma_sync_for_device(pool, addr, desc->len);
1479
1480 sg_init_table(sq->sg, 2);
1481 sg_fill_dma(sq->sg, sq->xsk_hdr_dma_addr, vi->hdr_len);
1482 sg_fill_dma(sq->sg + 1, addr, desc->len);
1483
1484 return virtqueue_add_outbuf_premapped(sq->vq, sq->sg, 2,
1485 virtnet_xsk_to_ptr(desc->len),
1486 GFP_ATOMIC);
1487 }
1488
virtnet_xsk_xmit_batch(struct send_queue * sq,struct xsk_buff_pool * pool,unsigned int budget,u64 * kicks)1489 static int virtnet_xsk_xmit_batch(struct send_queue *sq,
1490 struct xsk_buff_pool *pool,
1491 unsigned int budget,
1492 u64 *kicks)
1493 {
1494 struct xdp_desc *descs = pool->tx_descs;
1495 bool kick = false;
1496 u32 nb_pkts, i;
1497 int err;
1498
1499 budget = min_t(u32, budget, sq->vq->num_free);
1500
1501 nb_pkts = xsk_tx_peek_release_desc_batch(pool, budget);
1502 if (!nb_pkts)
1503 return 0;
1504
1505 for (i = 0; i < nb_pkts; i++) {
1506 err = virtnet_xsk_xmit_one(sq, pool, &descs[i]);
1507 if (unlikely(err)) {
1508 xsk_tx_completed(sq->xsk_pool, nb_pkts - i);
1509 break;
1510 }
1511
1512 kick = true;
1513 }
1514
1515 if (kick && virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
1516 (*kicks)++;
1517
1518 return i;
1519 }
1520
virtnet_xsk_xmit(struct send_queue * sq,struct xsk_buff_pool * pool,int budget)1521 static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
1522 int budget)
1523 {
1524 struct virtnet_info *vi = sq->vq->vdev->priv;
1525 struct virtnet_sq_free_stats stats = {};
1526 struct net_device *dev = vi->dev;
1527 u64 kicks = 0;
1528 int sent;
1529
1530 /* Avoid to wakeup napi meanless, so call __free_old_xmit instead of
1531 * free_old_xmit().
1532 */
1533 __free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);
1534
1535 if (stats.xsk)
1536 xsk_tx_completed(sq->xsk_pool, stats.xsk);
1537
1538 sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
1539
1540 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
1541 check_sq_full_and_disable(vi, vi->dev, sq);
1542
1543 if (sent) {
1544 struct netdev_queue *txq;
1545
1546 txq = netdev_get_tx_queue(vi->dev, sq - vi->sq);
1547 txq_trans_cond_update(txq);
1548 }
1549
1550 u64_stats_update_begin(&sq->stats.syncp);
1551 u64_stats_add(&sq->stats.packets, stats.packets);
1552 u64_stats_add(&sq->stats.bytes, stats.bytes);
1553 u64_stats_add(&sq->stats.kicks, kicks);
1554 u64_stats_add(&sq->stats.xdp_tx, sent);
1555 u64_stats_update_end(&sq->stats.syncp);
1556
1557 if (xsk_uses_need_wakeup(pool))
1558 xsk_set_tx_need_wakeup(pool);
1559
1560 return sent;
1561 }
1562
xsk_wakeup(struct send_queue * sq)1563 static void xsk_wakeup(struct send_queue *sq)
1564 {
1565 if (napi_if_scheduled_mark_missed(&sq->napi))
1566 return;
1567
1568 local_bh_disable();
1569 virtqueue_napi_schedule(&sq->napi, sq->vq);
1570 local_bh_enable();
1571 }
1572
virtnet_xsk_wakeup(struct net_device * dev,u32 qid,u32 flag)1573 static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
1574 {
1575 struct virtnet_info *vi = netdev_priv(dev);
1576 struct send_queue *sq;
1577
1578 if (!netif_running(dev))
1579 return -ENETDOWN;
1580
1581 if (qid >= vi->curr_queue_pairs)
1582 return -EINVAL;
1583
1584 sq = &vi->sq[qid];
1585
1586 xsk_wakeup(sq);
1587 return 0;
1588 }
1589
virtnet_xsk_completed(struct send_queue * sq,int num)1590 static void virtnet_xsk_completed(struct send_queue *sq, int num)
1591 {
1592 xsk_tx_completed(sq->xsk_pool, num);
1593
1594 /* If this is called by rx poll, start_xmit and xdp xmit we should
1595 * wakeup the tx napi to consume the xsk tx queue, because the tx
1596 * interrupt may not be triggered.
1597 */
1598 xsk_wakeup(sq);
1599 }
1600
__virtnet_xdp_xmit_one(struct virtnet_info * vi,struct send_queue * sq,struct xdp_frame * xdpf)1601 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
1602 struct send_queue *sq,
1603 struct xdp_frame *xdpf)
1604 {
1605 struct virtio_net_hdr_mrg_rxbuf *hdr;
1606 struct skb_shared_info *shinfo;
1607 u8 nr_frags = 0;
1608 int err, i;
1609
1610 if (unlikely(xdpf->headroom < vi->hdr_len))
1611 return -EOVERFLOW;
1612
1613 if (unlikely(xdp_frame_has_frags(xdpf))) {
1614 shinfo = xdp_get_shared_info_from_frame(xdpf);
1615 nr_frags = shinfo->nr_frags;
1616 }
1617
1618 /* In wrapping function virtnet_xdp_xmit(), we need to free
1619 * up the pending old buffers, where we need to calculate the
1620 * position of skb_shared_info in xdp_get_frame_len() and
1621 * xdp_return_frame(), which will involve to xdpf->data and
1622 * xdpf->headroom. Therefore, we need to update the value of
1623 * headroom synchronously here.
1624 */
1625 xdpf->headroom -= vi->hdr_len;
1626 xdpf->data -= vi->hdr_len;
1627 /* Zero header and leave csum up to XDP layers */
1628 hdr = xdpf->data;
1629 memset(hdr, 0, vi->hdr_len);
1630 xdpf->len += vi->hdr_len;
1631
1632 sg_init_table(sq->sg, nr_frags + 1);
1633 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
1634 for (i = 0; i < nr_frags; i++) {
1635 skb_frag_t *frag = &shinfo->frags[i];
1636
1637 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
1638 skb_frag_size(frag), skb_frag_off(frag));
1639 }
1640
1641 err = virtnet_add_outbuf(sq, nr_frags + 1, xdpf, VIRTNET_XMIT_TYPE_XDP);
1642 if (unlikely(err))
1643 return -ENOSPC; /* Caller handle free/refcnt */
1644
1645 return 0;
1646 }
1647
1648 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
1649 * the current cpu, so it does not need to be locked.
1650 *
1651 * Here we use marco instead of inline functions because we have to deal with
1652 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
1653 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
1654 * functions to perfectly solve these three problems at the same time.
1655 */
1656 #define virtnet_xdp_get_sq(vi) ({ \
1657 int cpu = smp_processor_id(); \
1658 struct netdev_queue *txq; \
1659 typeof(vi) v = (vi); \
1660 unsigned int qp; \
1661 \
1662 if (v->curr_queue_pairs > nr_cpu_ids) { \
1663 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
1664 qp += cpu; \
1665 txq = netdev_get_tx_queue(v->dev, qp); \
1666 __netif_tx_acquire(txq); \
1667 } else { \
1668 qp = cpu % v->curr_queue_pairs; \
1669 txq = netdev_get_tx_queue(v->dev, qp); \
1670 __netif_tx_lock(txq, cpu); \
1671 } \
1672 v->sq + qp; \
1673 })
1674
1675 #define virtnet_xdp_put_sq(vi, q) { \
1676 struct netdev_queue *txq; \
1677 typeof(vi) v = (vi); \
1678 \
1679 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
1680 if (v->curr_queue_pairs > nr_cpu_ids) \
1681 __netif_tx_release(txq); \
1682 else \
1683 __netif_tx_unlock(txq); \
1684 }
1685
virtnet_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)1686 static int virtnet_xdp_xmit(struct net_device *dev,
1687 int n, struct xdp_frame **frames, u32 flags)
1688 {
1689 struct virtnet_info *vi = netdev_priv(dev);
1690 struct virtnet_sq_free_stats stats = {0};
1691 struct receive_queue *rq = vi->rq;
1692 struct bpf_prog *xdp_prog;
1693 struct send_queue *sq;
1694 int nxmit = 0;
1695 int kicks = 0;
1696 int ret;
1697 int i;
1698
1699 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
1700 * indicate XDP resources have been successfully allocated.
1701 */
1702 xdp_prog = rcu_access_pointer(rq->xdp_prog);
1703 if (!xdp_prog)
1704 return -ENXIO;
1705
1706 sq = virtnet_xdp_get_sq(vi);
1707
1708 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
1709 ret = -EINVAL;
1710 goto out;
1711 }
1712
1713 /* Free up any pending old buffers before queueing new ones. */
1714 virtnet_free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq),
1715 false, &stats);
1716
1717 for (i = 0; i < n; i++) {
1718 struct xdp_frame *xdpf = frames[i];
1719
1720 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
1721 break;
1722 nxmit++;
1723 }
1724 ret = nxmit;
1725
1726 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
1727 check_sq_full_and_disable(vi, dev, sq);
1728
1729 if (flags & XDP_XMIT_FLUSH) {
1730 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
1731 kicks = 1;
1732 }
1733 out:
1734 u64_stats_update_begin(&sq->stats.syncp);
1735 u64_stats_add(&sq->stats.bytes, stats.bytes);
1736 u64_stats_add(&sq->stats.packets, stats.packets);
1737 u64_stats_add(&sq->stats.xdp_tx, n);
1738 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
1739 u64_stats_add(&sq->stats.kicks, kicks);
1740 u64_stats_update_end(&sq->stats.syncp);
1741
1742 virtnet_xdp_put_sq(vi, sq);
1743 return ret;
1744 }
1745
put_xdp_frags(struct xdp_buff * xdp)1746 static void put_xdp_frags(struct xdp_buff *xdp)
1747 {
1748 struct skb_shared_info *shinfo;
1749 struct page *xdp_page;
1750 int i;
1751
1752 if (xdp_buff_has_frags(xdp)) {
1753 shinfo = xdp_get_shared_info_from_buff(xdp);
1754 for (i = 0; i < shinfo->nr_frags; i++) {
1755 xdp_page = skb_frag_page(&shinfo->frags[i]);
1756 put_page(xdp_page);
1757 }
1758 }
1759 }
1760
virtnet_xdp_handler(struct bpf_prog * xdp_prog,struct xdp_buff * xdp,struct net_device * dev,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1761 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
1762 struct net_device *dev,
1763 unsigned int *xdp_xmit,
1764 struct virtnet_rq_stats *stats)
1765 {
1766 struct xdp_frame *xdpf;
1767 int err;
1768 u32 act;
1769
1770 act = bpf_prog_run_xdp(xdp_prog, xdp);
1771 u64_stats_inc(&stats->xdp_packets);
1772
1773 switch (act) {
1774 case XDP_PASS:
1775 return act;
1776
1777 case XDP_TX:
1778 u64_stats_inc(&stats->xdp_tx);
1779 xdpf = xdp_convert_buff_to_frame(xdp);
1780 if (unlikely(!xdpf)) {
1781 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1782 return XDP_DROP;
1783 }
1784
1785 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1786 if (unlikely(!err)) {
1787 xdp_return_frame_rx_napi(xdpf);
1788 } else if (unlikely(err < 0)) {
1789 trace_xdp_exception(dev, xdp_prog, act);
1790 return XDP_DROP;
1791 }
1792 *xdp_xmit |= VIRTIO_XDP_TX;
1793 return act;
1794
1795 case XDP_REDIRECT:
1796 u64_stats_inc(&stats->xdp_redirects);
1797 err = xdp_do_redirect(dev, xdp, xdp_prog);
1798 if (err)
1799 return XDP_DROP;
1800
1801 *xdp_xmit |= VIRTIO_XDP_REDIR;
1802 return act;
1803
1804 default:
1805 bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
1806 fallthrough;
1807 case XDP_ABORTED:
1808 trace_xdp_exception(dev, xdp_prog, act);
1809 fallthrough;
1810 case XDP_DROP:
1811 return XDP_DROP;
1812 }
1813 }
1814
virtnet_get_headroom(struct virtnet_info * vi)1815 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1816 {
1817 return vi->xdp_enabled ? XDP_PACKET_HEADROOM : 0;
1818 }
1819
1820 /* We copy the packet for XDP in the following cases:
1821 *
1822 * 1) Packet is scattered across multiple rx buffers.
1823 * 2) Headroom space is insufficient.
1824 *
1825 * This is inefficient but it's a temporary condition that
1826 * we hit right after XDP is enabled and until queue is refilled
1827 * with large buffers with sufficient headroom - so it should affect
1828 * at most queue size packets.
1829 * Afterwards, the conditions to enable
1830 * XDP should preclude the underlying device from sending packets
1831 * across multiple buffers (num_buf > 1), and we make sure buffers
1832 * have enough headroom.
1833 */
xdp_linearize_page(struct net_device * dev,struct receive_queue * rq,int * num_buf,struct page * p,int offset,int page_off,unsigned int * len)1834 static struct page *xdp_linearize_page(struct net_device *dev,
1835 struct receive_queue *rq,
1836 int *num_buf,
1837 struct page *p,
1838 int offset,
1839 int page_off,
1840 unsigned int *len)
1841 {
1842 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1843 struct page *page;
1844
1845 if (page_off + *len + tailroom > PAGE_SIZE)
1846 return NULL;
1847
1848 page = alloc_page(GFP_ATOMIC);
1849 if (!page)
1850 return NULL;
1851
1852 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
1853 page_off += *len;
1854
1855 /* Only mergeable mode can go inside this while loop. In small mode,
1856 * *num_buf == 1, so it cannot go inside.
1857 */
1858 while (--*num_buf) {
1859 unsigned int buflen;
1860 void *buf;
1861 void *ctx;
1862 int off;
1863
1864 buf = virtnet_rq_get_buf(rq, &buflen, &ctx);
1865 if (unlikely(!buf))
1866 goto err_buf;
1867
1868 p = virt_to_head_page(buf);
1869 off = buf - page_address(p);
1870
1871 if (check_mergeable_len(dev, ctx, buflen)) {
1872 put_page(p);
1873 goto err_buf;
1874 }
1875
1876 /* guard against a misconfigured or uncooperative backend that
1877 * is sending packet larger than the MTU.
1878 */
1879 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
1880 put_page(p);
1881 goto err_buf;
1882 }
1883
1884 memcpy(page_address(page) + page_off,
1885 page_address(p) + off, buflen);
1886 page_off += buflen;
1887 put_page(p);
1888 }
1889
1890 /* Headroom does not contribute to packet length */
1891 *len = page_off - XDP_PACKET_HEADROOM;
1892 return page;
1893 err_buf:
1894 __free_pages(page, 0);
1895 return NULL;
1896 }
1897
receive_small_build_skb(struct virtnet_info * vi,unsigned int xdp_headroom,void * buf,unsigned int len)1898 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
1899 unsigned int xdp_headroom,
1900 void *buf,
1901 unsigned int len)
1902 {
1903 unsigned int header_offset;
1904 unsigned int headroom;
1905 unsigned int buflen;
1906 struct sk_buff *skb;
1907
1908 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1909 headroom = vi->hdr_len + header_offset;
1910 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1911 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1912
1913 skb = virtnet_build_skb(buf, buflen, headroom, len);
1914 if (unlikely(!skb))
1915 return NULL;
1916
1917 buf += header_offset;
1918 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
1919
1920 return skb;
1921 }
1922
receive_small_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,unsigned int xdp_headroom,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1923 static struct sk_buff *receive_small_xdp(struct net_device *dev,
1924 struct virtnet_info *vi,
1925 struct receive_queue *rq,
1926 struct bpf_prog *xdp_prog,
1927 void *buf,
1928 unsigned int xdp_headroom,
1929 unsigned int len,
1930 unsigned int *xdp_xmit,
1931 struct virtnet_rq_stats *stats)
1932 {
1933 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
1934 unsigned int headroom = vi->hdr_len + header_offset;
1935 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1936 struct page *page = virt_to_head_page(buf);
1937 struct page *xdp_page;
1938 unsigned int buflen;
1939 struct xdp_buff xdp;
1940 struct sk_buff *skb;
1941 unsigned int metasize = 0;
1942 u32 act;
1943
1944 if (unlikely(hdr->hdr.gso_type))
1945 goto err_xdp;
1946
1947 /* Partially checksummed packets must be dropped. */
1948 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1949 goto err_xdp;
1950
1951 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1952 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1953
1954 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
1955 int offset = buf - page_address(page) + header_offset;
1956 unsigned int tlen = len + vi->hdr_len;
1957 int num_buf = 1;
1958
1959 xdp_headroom = virtnet_get_headroom(vi);
1960 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1961 headroom = vi->hdr_len + header_offset;
1962 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1963 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1964 xdp_page = xdp_linearize_page(dev, rq, &num_buf, page,
1965 offset, header_offset,
1966 &tlen);
1967 if (!xdp_page)
1968 goto err_xdp;
1969
1970 buf = page_address(xdp_page);
1971 put_page(page);
1972 page = xdp_page;
1973 }
1974
1975 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1976 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1977 xdp_headroom, len, true);
1978
1979 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1980
1981 switch (act) {
1982 case XDP_PASS:
1983 /* Recalculate length in case bpf program changed it */
1984 len = xdp.data_end - xdp.data;
1985 metasize = xdp.data - xdp.data_meta;
1986 break;
1987
1988 case XDP_TX:
1989 case XDP_REDIRECT:
1990 goto xdp_xmit;
1991
1992 default:
1993 goto err_xdp;
1994 }
1995
1996 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
1997 if (unlikely(!skb))
1998 goto err;
1999
2000 if (metasize)
2001 skb_metadata_set(skb, metasize);
2002
2003 return skb;
2004
2005 err_xdp:
2006 u64_stats_inc(&stats->xdp_drops);
2007 err:
2008 u64_stats_inc(&stats->drops);
2009 put_page(page);
2010 xdp_xmit:
2011 return NULL;
2012 }
2013
receive_small(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2014 static struct sk_buff *receive_small(struct net_device *dev,
2015 struct virtnet_info *vi,
2016 struct receive_queue *rq,
2017 void *buf, void *ctx,
2018 unsigned int len,
2019 unsigned int *xdp_xmit,
2020 struct virtnet_rq_stats *stats)
2021 {
2022 unsigned int xdp_headroom = (unsigned long)ctx;
2023 struct page *page = virt_to_head_page(buf);
2024 struct sk_buff *skb;
2025
2026 /* We passed the address of virtnet header to virtio-core,
2027 * so truncate the padding.
2028 */
2029 buf -= VIRTNET_RX_PAD + xdp_headroom;
2030
2031 len -= vi->hdr_len;
2032 u64_stats_add(&stats->bytes, len);
2033
2034 if (unlikely(len > GOOD_PACKET_LEN)) {
2035 pr_debug("%s: rx error: len %u exceeds max size %d\n",
2036 dev->name, len, GOOD_PACKET_LEN);
2037 DEV_STATS_INC(dev, rx_length_errors);
2038 goto err;
2039 }
2040
2041 if (unlikely(vi->xdp_enabled)) {
2042 struct bpf_prog *xdp_prog;
2043
2044 rcu_read_lock();
2045 xdp_prog = rcu_dereference(rq->xdp_prog);
2046 if (xdp_prog) {
2047 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
2048 xdp_headroom, len, xdp_xmit,
2049 stats);
2050 rcu_read_unlock();
2051 return skb;
2052 }
2053 rcu_read_unlock();
2054 }
2055
2056 skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
2057 if (likely(skb))
2058 return skb;
2059
2060 err:
2061 u64_stats_inc(&stats->drops);
2062 put_page(page);
2063 return NULL;
2064 }
2065
receive_big(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,struct virtnet_rq_stats * stats)2066 static struct sk_buff *receive_big(struct net_device *dev,
2067 struct virtnet_info *vi,
2068 struct receive_queue *rq,
2069 void *buf,
2070 unsigned int len,
2071 struct virtnet_rq_stats *stats)
2072 {
2073 struct page *page = buf;
2074 struct sk_buff *skb =
2075 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
2076
2077 u64_stats_add(&stats->bytes, len - vi->hdr_len);
2078 if (unlikely(!skb))
2079 goto err;
2080
2081 return skb;
2082
2083 err:
2084 u64_stats_inc(&stats->drops);
2085 give_pages(rq, page);
2086 return NULL;
2087 }
2088
mergeable_buf_free(struct receive_queue * rq,int num_buf,struct net_device * dev,struct virtnet_rq_stats * stats)2089 static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
2090 struct net_device *dev,
2091 struct virtnet_rq_stats *stats)
2092 {
2093 struct page *page;
2094 void *buf;
2095 int len;
2096
2097 while (num_buf-- > 1) {
2098 buf = virtnet_rq_get_buf(rq, &len, NULL);
2099 if (unlikely(!buf)) {
2100 pr_debug("%s: rx error: %d buffers missing\n",
2101 dev->name, num_buf);
2102 DEV_STATS_INC(dev, rx_length_errors);
2103 break;
2104 }
2105 u64_stats_add(&stats->bytes, len);
2106 page = virt_to_head_page(buf);
2107 put_page(page);
2108 }
2109 }
2110
2111 /* Why not use xdp_build_skb_from_frame() ?
2112 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
2113 * virtio-net there are 2 points that do not match its requirements:
2114 * 1. The size of the prefilled buffer is not fixed before xdp is set.
2115 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
2116 * like eth_type_trans() (which virtio-net does in receive_buf()).
2117 */
build_skb_from_xdp_buff(struct net_device * dev,struct virtnet_info * vi,struct xdp_buff * xdp,unsigned int xdp_frags_truesz)2118 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
2119 struct virtnet_info *vi,
2120 struct xdp_buff *xdp,
2121 unsigned int xdp_frags_truesz)
2122 {
2123 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2124 unsigned int headroom, data_len;
2125 struct sk_buff *skb;
2126 int metasize;
2127 u8 nr_frags;
2128
2129 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
2130 pr_debug("Error building skb as missing reserved tailroom for xdp");
2131 return NULL;
2132 }
2133
2134 if (unlikely(xdp_buff_has_frags(xdp)))
2135 nr_frags = sinfo->nr_frags;
2136
2137 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
2138 if (unlikely(!skb))
2139 return NULL;
2140
2141 headroom = xdp->data - xdp->data_hard_start;
2142 data_len = xdp->data_end - xdp->data;
2143 skb_reserve(skb, headroom);
2144 __skb_put(skb, data_len);
2145
2146 metasize = xdp->data - xdp->data_meta;
2147 metasize = metasize > 0 ? metasize : 0;
2148 if (metasize)
2149 skb_metadata_set(skb, metasize);
2150
2151 if (unlikely(xdp_buff_has_frags(xdp)))
2152 xdp_update_skb_shared_info(skb, nr_frags,
2153 sinfo->xdp_frags_size,
2154 xdp_frags_truesz,
2155 xdp_buff_is_frag_pfmemalloc(xdp));
2156
2157 return skb;
2158 }
2159
2160 /* TODO: build xdp in big mode */
virtnet_build_xdp_buff_mrg(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,void * buf,unsigned int len,unsigned int frame_sz,int * num_buf,unsigned int * xdp_frags_truesize,struct virtnet_rq_stats * stats)2161 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
2162 struct virtnet_info *vi,
2163 struct receive_queue *rq,
2164 struct xdp_buff *xdp,
2165 void *buf,
2166 unsigned int len,
2167 unsigned int frame_sz,
2168 int *num_buf,
2169 unsigned int *xdp_frags_truesize,
2170 struct virtnet_rq_stats *stats)
2171 {
2172 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2173 struct skb_shared_info *shinfo;
2174 unsigned int xdp_frags_truesz = 0;
2175 unsigned int truesize;
2176 struct page *page;
2177 skb_frag_t *frag;
2178 int offset;
2179 void *ctx;
2180
2181 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
2182 xdp_prepare_buff(xdp, buf - XDP_PACKET_HEADROOM,
2183 XDP_PACKET_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
2184
2185 if (!*num_buf)
2186 return 0;
2187
2188 if (*num_buf > 1) {
2189 /* If we want to build multi-buffer xdp, we need
2190 * to specify that the flags of xdp_buff have the
2191 * XDP_FLAGS_HAS_FRAG bit.
2192 */
2193 if (!xdp_buff_has_frags(xdp))
2194 xdp_buff_set_frags_flag(xdp);
2195
2196 shinfo = xdp_get_shared_info_from_buff(xdp);
2197 shinfo->nr_frags = 0;
2198 shinfo->xdp_frags_size = 0;
2199 }
2200
2201 if (*num_buf > MAX_SKB_FRAGS + 1)
2202 return -EINVAL;
2203
2204 while (--*num_buf > 0) {
2205 buf = virtnet_rq_get_buf(rq, &len, &ctx);
2206 if (unlikely(!buf)) {
2207 pr_debug("%s: rx error: %d buffers out of %d missing\n",
2208 dev->name, *num_buf,
2209 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
2210 DEV_STATS_INC(dev, rx_length_errors);
2211 goto err;
2212 }
2213
2214 u64_stats_add(&stats->bytes, len);
2215 page = virt_to_head_page(buf);
2216 offset = buf - page_address(page);
2217
2218 if (check_mergeable_len(dev, ctx, len)) {
2219 put_page(page);
2220 goto err;
2221 }
2222
2223 truesize = mergeable_ctx_to_truesize(ctx);
2224 xdp_frags_truesz += truesize;
2225
2226 frag = &shinfo->frags[shinfo->nr_frags++];
2227 skb_frag_fill_page_desc(frag, page, offset, len);
2228 if (page_is_pfmemalloc(page))
2229 xdp_buff_set_frag_pfmemalloc(xdp);
2230
2231 shinfo->xdp_frags_size += len;
2232 }
2233
2234 *xdp_frags_truesize = xdp_frags_truesz;
2235 return 0;
2236
2237 err:
2238 put_xdp_frags(xdp);
2239 return -EINVAL;
2240 }
2241
mergeable_xdp_get_buf(struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * ctx,unsigned int * frame_sz,int * num_buf,struct page ** page,int offset,unsigned int * len,struct virtio_net_hdr_mrg_rxbuf * hdr)2242 static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
2243 struct receive_queue *rq,
2244 struct bpf_prog *xdp_prog,
2245 void *ctx,
2246 unsigned int *frame_sz,
2247 int *num_buf,
2248 struct page **page,
2249 int offset,
2250 unsigned int *len,
2251 struct virtio_net_hdr_mrg_rxbuf *hdr)
2252 {
2253 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
2254 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
2255 struct page *xdp_page;
2256 unsigned int xdp_room;
2257
2258 /* Transient failure which in theory could occur if
2259 * in-flight packets from before XDP was enabled reach
2260 * the receive path after XDP is loaded.
2261 */
2262 if (unlikely(hdr->hdr.gso_type))
2263 return NULL;
2264
2265 /* Partially checksummed packets must be dropped. */
2266 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
2267 return NULL;
2268
2269 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
2270 * with headroom may add hole in truesize, which
2271 * make their length exceed PAGE_SIZE. So we disabled the
2272 * hole mechanism for xdp. See add_recvbuf_mergeable().
2273 */
2274 *frame_sz = truesize;
2275
2276 if (likely(headroom >= virtnet_get_headroom(vi) &&
2277 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
2278 return page_address(*page) + offset;
2279 }
2280
2281 /* This happens when headroom is not enough because
2282 * of the buffer was prefilled before XDP is set.
2283 * This should only happen for the first several packets.
2284 * In fact, vq reset can be used here to help us clean up
2285 * the prefilled buffers, but many existing devices do not
2286 * support it, and we don't want to bother users who are
2287 * using xdp normally.
2288 */
2289 if (!xdp_prog->aux->xdp_has_frags) {
2290 /* linearize data for XDP */
2291 xdp_page = xdp_linearize_page(vi->dev, rq, num_buf,
2292 *page, offset,
2293 XDP_PACKET_HEADROOM,
2294 len);
2295 if (!xdp_page)
2296 return NULL;
2297 } else {
2298 xdp_room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
2299 sizeof(struct skb_shared_info));
2300 if (*len + xdp_room > PAGE_SIZE)
2301 return NULL;
2302
2303 xdp_page = alloc_page(GFP_ATOMIC);
2304 if (!xdp_page)
2305 return NULL;
2306
2307 memcpy(page_address(xdp_page) + XDP_PACKET_HEADROOM,
2308 page_address(*page) + offset, *len);
2309 }
2310
2311 *frame_sz = PAGE_SIZE;
2312
2313 put_page(*page);
2314
2315 *page = xdp_page;
2316
2317 return page_address(*page) + XDP_PACKET_HEADROOM;
2318 }
2319
receive_mergeable_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2320 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
2321 struct virtnet_info *vi,
2322 struct receive_queue *rq,
2323 struct bpf_prog *xdp_prog,
2324 void *buf,
2325 void *ctx,
2326 unsigned int len,
2327 unsigned int *xdp_xmit,
2328 struct virtnet_rq_stats *stats)
2329 {
2330 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2331 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
2332 struct page *page = virt_to_head_page(buf);
2333 int offset = buf - page_address(page);
2334 unsigned int xdp_frags_truesz = 0;
2335 struct sk_buff *head_skb;
2336 unsigned int frame_sz;
2337 struct xdp_buff xdp;
2338 void *data;
2339 u32 act;
2340 int err;
2341
2342 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
2343 offset, &len, hdr);
2344 if (unlikely(!data))
2345 goto err_xdp;
2346
2347 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
2348 &num_buf, &xdp_frags_truesz, stats);
2349 if (unlikely(err))
2350 goto err_xdp;
2351
2352 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
2353
2354 switch (act) {
2355 case XDP_PASS:
2356 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
2357 if (unlikely(!head_skb))
2358 break;
2359 return head_skb;
2360
2361 case XDP_TX:
2362 case XDP_REDIRECT:
2363 return NULL;
2364
2365 default:
2366 break;
2367 }
2368
2369 put_xdp_frags(&xdp);
2370
2371 err_xdp:
2372 put_page(page);
2373 mergeable_buf_free(rq, num_buf, dev, stats);
2374
2375 u64_stats_inc(&stats->xdp_drops);
2376 u64_stats_inc(&stats->drops);
2377 return NULL;
2378 }
2379
virtnet_skb_append_frag(struct sk_buff * head_skb,struct sk_buff * curr_skb,struct page * page,void * buf,int len,int truesize)2380 static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
2381 struct sk_buff *curr_skb,
2382 struct page *page, void *buf,
2383 int len, int truesize)
2384 {
2385 int num_skb_frags;
2386 int offset;
2387
2388 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
2389 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
2390 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
2391
2392 if (unlikely(!nskb))
2393 return NULL;
2394
2395 if (curr_skb == head_skb)
2396 skb_shinfo(curr_skb)->frag_list = nskb;
2397 else
2398 curr_skb->next = nskb;
2399 curr_skb = nskb;
2400 head_skb->truesize += nskb->truesize;
2401 num_skb_frags = 0;
2402 }
2403
2404 if (curr_skb != head_skb) {
2405 head_skb->data_len += len;
2406 head_skb->len += len;
2407 head_skb->truesize += truesize;
2408 }
2409
2410 offset = buf - page_address(page);
2411 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
2412 put_page(page);
2413 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
2414 len, truesize);
2415 } else {
2416 skb_add_rx_frag(curr_skb, num_skb_frags, page,
2417 offset, len, truesize);
2418 }
2419
2420 return curr_skb;
2421 }
2422
receive_mergeable(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2423 static struct sk_buff *receive_mergeable(struct net_device *dev,
2424 struct virtnet_info *vi,
2425 struct receive_queue *rq,
2426 void *buf,
2427 void *ctx,
2428 unsigned int len,
2429 unsigned int *xdp_xmit,
2430 struct virtnet_rq_stats *stats)
2431 {
2432 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2433 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
2434 struct page *page = virt_to_head_page(buf);
2435 int offset = buf - page_address(page);
2436 struct sk_buff *head_skb, *curr_skb;
2437 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
2438 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
2439
2440 head_skb = NULL;
2441 u64_stats_add(&stats->bytes, len - vi->hdr_len);
2442
2443 if (check_mergeable_len(dev, ctx, len))
2444 goto err_skb;
2445
2446 if (unlikely(vi->xdp_enabled)) {
2447 struct bpf_prog *xdp_prog;
2448
2449 rcu_read_lock();
2450 xdp_prog = rcu_dereference(rq->xdp_prog);
2451 if (xdp_prog) {
2452 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
2453 len, xdp_xmit, stats);
2454 rcu_read_unlock();
2455 return head_skb;
2456 }
2457 rcu_read_unlock();
2458 }
2459
2460 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
2461 curr_skb = head_skb;
2462
2463 if (unlikely(!curr_skb))
2464 goto err_skb;
2465 while (--num_buf) {
2466 buf = virtnet_rq_get_buf(rq, &len, &ctx);
2467 if (unlikely(!buf)) {
2468 pr_debug("%s: rx error: %d buffers out of %d missing\n",
2469 dev->name, num_buf,
2470 virtio16_to_cpu(vi->vdev,
2471 hdr->num_buffers));
2472 DEV_STATS_INC(dev, rx_length_errors);
2473 goto err_buf;
2474 }
2475
2476 u64_stats_add(&stats->bytes, len);
2477 page = virt_to_head_page(buf);
2478
2479 if (check_mergeable_len(dev, ctx, len))
2480 goto err_skb;
2481
2482 truesize = mergeable_ctx_to_truesize(ctx);
2483 curr_skb = virtnet_skb_append_frag(head_skb, curr_skb, page,
2484 buf, len, truesize);
2485 if (!curr_skb)
2486 goto err_skb;
2487 }
2488
2489 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
2490 return head_skb;
2491
2492 err_skb:
2493 put_page(page);
2494 mergeable_buf_free(rq, num_buf, dev, stats);
2495
2496 err_buf:
2497 u64_stats_inc(&stats->drops);
2498 dev_kfree_skb(head_skb);
2499 return NULL;
2500 }
2501
virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash * hdr_hash,struct sk_buff * skb)2502 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
2503 struct sk_buff *skb)
2504 {
2505 enum pkt_hash_types rss_hash_type;
2506
2507 if (!hdr_hash || !skb)
2508 return;
2509
2510 switch (__le16_to_cpu(hdr_hash->hash_report)) {
2511 case VIRTIO_NET_HASH_REPORT_TCPv4:
2512 case VIRTIO_NET_HASH_REPORT_UDPv4:
2513 case VIRTIO_NET_HASH_REPORT_TCPv6:
2514 case VIRTIO_NET_HASH_REPORT_UDPv6:
2515 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
2516 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
2517 rss_hash_type = PKT_HASH_TYPE_L4;
2518 break;
2519 case VIRTIO_NET_HASH_REPORT_IPv4:
2520 case VIRTIO_NET_HASH_REPORT_IPv6:
2521 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
2522 rss_hash_type = PKT_HASH_TYPE_L3;
2523 break;
2524 case VIRTIO_NET_HASH_REPORT_NONE:
2525 default:
2526 rss_hash_type = PKT_HASH_TYPE_NONE;
2527 }
2528 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
2529 }
2530
virtnet_receive_done(struct virtnet_info * vi,struct receive_queue * rq,struct sk_buff * skb,u8 flags)2531 static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
2532 struct sk_buff *skb, u8 flags)
2533 {
2534 struct virtio_net_common_hdr *hdr;
2535 struct net_device *dev = vi->dev;
2536
2537 hdr = skb_vnet_common_hdr(skb);
2538 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
2539 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
2540
2541 if (flags & VIRTIO_NET_HDR_F_DATA_VALID)
2542 skb->ip_summed = CHECKSUM_UNNECESSARY;
2543
2544 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
2545 virtio_is_little_endian(vi->vdev))) {
2546 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
2547 dev->name, hdr->hdr.gso_type,
2548 hdr->hdr.gso_size);
2549 goto frame_err;
2550 }
2551
2552 skb_record_rx_queue(skb, vq2rxq(rq->vq));
2553 skb->protocol = eth_type_trans(skb, dev);
2554 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
2555 ntohs(skb->protocol), skb->len, skb->pkt_type);
2556
2557 napi_gro_receive(&rq->napi, skb);
2558 return;
2559
2560 frame_err:
2561 DEV_STATS_INC(dev, rx_frame_errors);
2562 dev_kfree_skb(skb);
2563 }
2564
receive_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,void ** ctx,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2565 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
2566 void *buf, unsigned int len, void **ctx,
2567 unsigned int *xdp_xmit,
2568 struct virtnet_rq_stats *stats)
2569 {
2570 struct net_device *dev = vi->dev;
2571 struct sk_buff *skb;
2572 u8 flags;
2573
2574 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
2575 pr_debug("%s: short packet %i\n", dev->name, len);
2576 DEV_STATS_INC(dev, rx_length_errors);
2577 virtnet_rq_free_buf(vi, rq, buf);
2578 return;
2579 }
2580
2581 /* 1. Save the flags early, as the XDP program might overwrite them.
2582 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
2583 * stay valid after XDP processing.
2584 * 2. XDP doesn't work with partially checksummed packets (refer to
2585 * virtnet_xdp_set()), so packets marked as
2586 * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
2587 */
2588 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
2589
2590 if (vi->mergeable_rx_bufs)
2591 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
2592 stats);
2593 else if (vi->big_packets)
2594 skb = receive_big(dev, vi, rq, buf, len, stats);
2595 else
2596 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
2597
2598 if (unlikely(!skb))
2599 return;
2600
2601 virtnet_receive_done(vi, rq, skb, flags);
2602 }
2603
2604 /* Unlike mergeable buffers, all buffers are allocated to the
2605 * same size, except for the headroom. For this reason we do
2606 * not need to use mergeable_len_to_ctx here - it is enough
2607 * to store the headroom as the context ignoring the truesize.
2608 */
add_recvbuf_small(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2609 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
2610 gfp_t gfp)
2611 {
2612 char *buf;
2613 unsigned int xdp_headroom = virtnet_get_headroom(vi);
2614 void *ctx = (void *)(unsigned long)xdp_headroom;
2615 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
2616 int err;
2617
2618 len = SKB_DATA_ALIGN(len) +
2619 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2620
2621 if (unlikely(!skb_page_frag_refill(len, &rq->alloc_frag, gfp)))
2622 return -ENOMEM;
2623
2624 buf = virtnet_rq_alloc(rq, len, gfp);
2625 if (unlikely(!buf))
2626 return -ENOMEM;
2627
2628 buf += VIRTNET_RX_PAD + xdp_headroom;
2629
2630 virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
2631
2632 err = virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1, buf, ctx, gfp);
2633 if (err < 0) {
2634 virtnet_rq_unmap(rq, buf, 0);
2635 put_page(virt_to_head_page(buf));
2636 }
2637
2638 return err;
2639 }
2640
add_recvbuf_big(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2641 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
2642 gfp_t gfp)
2643 {
2644 struct page *first, *list = NULL;
2645 char *p;
2646 int i, err, offset;
2647
2648 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
2649
2650 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
2651 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
2652 first = get_a_page(rq, gfp);
2653 if (!first) {
2654 if (list)
2655 give_pages(rq, list);
2656 return -ENOMEM;
2657 }
2658 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
2659
2660 /* chain new page in list head to match sg */
2661 first->private = (unsigned long)list;
2662 list = first;
2663 }
2664
2665 first = get_a_page(rq, gfp);
2666 if (!first) {
2667 give_pages(rq, list);
2668 return -ENOMEM;
2669 }
2670 p = page_address(first);
2671
2672 /* rq->sg[0], rq->sg[1] share the same page */
2673 /* a separated rq->sg[0] for header - required in case !any_header_sg */
2674 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
2675
2676 /* rq->sg[1] for data packet, from offset */
2677 offset = sizeof(struct padded_vnet_hdr);
2678 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
2679
2680 /* chain first in list head */
2681 first->private = (unsigned long)list;
2682 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
2683 first, gfp);
2684 if (err < 0)
2685 give_pages(rq, first);
2686
2687 return err;
2688 }
2689
get_mergeable_buf_len(struct receive_queue * rq,struct ewma_pkt_len * avg_pkt_len,unsigned int room)2690 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
2691 struct ewma_pkt_len *avg_pkt_len,
2692 unsigned int room)
2693 {
2694 struct virtnet_info *vi = rq->vq->vdev->priv;
2695 const size_t hdr_len = vi->hdr_len;
2696 unsigned int len;
2697
2698 if (room)
2699 return PAGE_SIZE - room;
2700
2701 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
2702 rq->min_buf_len, PAGE_SIZE - hdr_len);
2703
2704 return ALIGN(len, L1_CACHE_BYTES);
2705 }
2706
add_recvbuf_mergeable(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2707 static int add_recvbuf_mergeable(struct virtnet_info *vi,
2708 struct receive_queue *rq, gfp_t gfp)
2709 {
2710 struct page_frag *alloc_frag = &rq->alloc_frag;
2711 unsigned int headroom = virtnet_get_headroom(vi);
2712 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2713 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
2714 unsigned int len, hole;
2715 void *ctx;
2716 char *buf;
2717 int err;
2718
2719 /* Extra tailroom is needed to satisfy XDP's assumption. This
2720 * means rx frags coalescing won't work, but consider we've
2721 * disabled GSO for XDP, it won't be a big issue.
2722 */
2723 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
2724
2725 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
2726 return -ENOMEM;
2727
2728 if (!alloc_frag->offset && len + room + sizeof(struct virtnet_rq_dma) > alloc_frag->size)
2729 len -= sizeof(struct virtnet_rq_dma);
2730
2731 buf = virtnet_rq_alloc(rq, len + room, gfp);
2732 if (unlikely(!buf))
2733 return -ENOMEM;
2734
2735 buf += headroom; /* advance address leaving hole at front of pkt */
2736 hole = alloc_frag->size - alloc_frag->offset;
2737 if (hole < len + room) {
2738 /* To avoid internal fragmentation, if there is very likely not
2739 * enough space for another buffer, add the remaining space to
2740 * the current buffer.
2741 * XDP core assumes that frame_size of xdp_buff and the length
2742 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
2743 */
2744 if (!headroom)
2745 len += hole;
2746 alloc_frag->offset += hole;
2747 }
2748
2749 virtnet_rq_init_one_sg(rq, buf, len);
2750
2751 ctx = mergeable_len_to_ctx(len + room, headroom);
2752 err = virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1, buf, ctx, gfp);
2753 if (err < 0) {
2754 virtnet_rq_unmap(rq, buf, 0);
2755 put_page(virt_to_head_page(buf));
2756 }
2757
2758 return err;
2759 }
2760
2761 /*
2762 * Returns false if we couldn't fill entirely (OOM).
2763 *
2764 * Normally run in the receive path, but can also be run from ndo_open
2765 * before we're receiving packets, or from refill_work which is
2766 * careful to disable receiving (using napi_disable).
2767 */
try_fill_recv(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2768 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
2769 gfp_t gfp)
2770 {
2771 int err;
2772
2773 if (rq->xsk_pool) {
2774 err = virtnet_add_recvbuf_xsk(vi, rq, rq->xsk_pool, gfp);
2775 goto kick;
2776 }
2777
2778 do {
2779 if (vi->mergeable_rx_bufs)
2780 err = add_recvbuf_mergeable(vi, rq, gfp);
2781 else if (vi->big_packets)
2782 err = add_recvbuf_big(vi, rq, gfp);
2783 else
2784 err = add_recvbuf_small(vi, rq, gfp);
2785
2786 if (err)
2787 break;
2788 } while (rq->vq->num_free);
2789
2790 kick:
2791 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
2792 unsigned long flags;
2793
2794 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
2795 u64_stats_inc(&rq->stats.kicks);
2796 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2797 }
2798
2799 return err != -ENOMEM;
2800 }
2801
skb_recv_done(struct virtqueue * rvq)2802 static void skb_recv_done(struct virtqueue *rvq)
2803 {
2804 struct virtnet_info *vi = rvq->vdev->priv;
2805 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2806
2807 rq->calls++;
2808 virtqueue_napi_schedule(&rq->napi, rvq);
2809 }
2810
virtnet_napi_do_enable(struct virtqueue * vq,struct napi_struct * napi)2811 static void virtnet_napi_do_enable(struct virtqueue *vq,
2812 struct napi_struct *napi)
2813 {
2814 napi_enable(napi);
2815
2816 /* If all buffers were filled by other side before we napi_enabled, we
2817 * won't get another interrupt, so process any outstanding packets now.
2818 * Call local_bh_enable after to trigger softIRQ processing.
2819 */
2820 local_bh_disable();
2821 virtqueue_napi_schedule(napi, vq);
2822 local_bh_enable();
2823 }
2824
virtnet_napi_enable(struct receive_queue * rq)2825 static void virtnet_napi_enable(struct receive_queue *rq)
2826 {
2827 struct virtnet_info *vi = rq->vq->vdev->priv;
2828 int qidx = vq2rxq(rq->vq);
2829
2830 virtnet_napi_do_enable(rq->vq, &rq->napi);
2831 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_RX, &rq->napi);
2832 }
2833
virtnet_napi_tx_enable(struct send_queue * sq)2834 static void virtnet_napi_tx_enable(struct send_queue *sq)
2835 {
2836 struct virtnet_info *vi = sq->vq->vdev->priv;
2837 struct napi_struct *napi = &sq->napi;
2838 int qidx = vq2txq(sq->vq);
2839
2840 if (!napi->weight)
2841 return;
2842
2843 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2844 * enable the feature if this is likely affine with the transmit path.
2845 */
2846 if (!vi->affinity_hint_set) {
2847 napi->weight = 0;
2848 return;
2849 }
2850
2851 virtnet_napi_do_enable(sq->vq, napi);
2852 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_TX, napi);
2853 }
2854
virtnet_napi_tx_disable(struct send_queue * sq)2855 static void virtnet_napi_tx_disable(struct send_queue *sq)
2856 {
2857 struct virtnet_info *vi = sq->vq->vdev->priv;
2858 struct napi_struct *napi = &sq->napi;
2859 int qidx = vq2txq(sq->vq);
2860
2861 if (napi->weight) {
2862 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_TX, NULL);
2863 napi_disable(napi);
2864 }
2865 }
2866
virtnet_napi_disable(struct receive_queue * rq)2867 static void virtnet_napi_disable(struct receive_queue *rq)
2868 {
2869 struct virtnet_info *vi = rq->vq->vdev->priv;
2870 struct napi_struct *napi = &rq->napi;
2871 int qidx = vq2rxq(rq->vq);
2872
2873 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_RX, NULL);
2874 napi_disable(napi);
2875 }
2876
refill_work(struct work_struct * work)2877 static void refill_work(struct work_struct *work)
2878 {
2879 struct virtnet_info *vi =
2880 container_of(work, struct virtnet_info, refill.work);
2881 bool still_empty;
2882 int i;
2883
2884 for (i = 0; i < vi->curr_queue_pairs; i++) {
2885 struct receive_queue *rq = &vi->rq[i];
2886
2887 /*
2888 * When queue API support is added in the future and the call
2889 * below becomes napi_disable_locked, this driver will need to
2890 * be refactored.
2891 *
2892 * One possible solution would be to:
2893 * - cancel refill_work with cancel_delayed_work (note:
2894 * non-sync)
2895 * - cancel refill_work with cancel_delayed_work_sync in
2896 * virtnet_remove after the netdev is unregistered
2897 * - wrap all of the work in a lock (perhaps the netdev
2898 * instance lock)
2899 * - check netif_running() and return early to avoid a race
2900 */
2901 napi_disable(&rq->napi);
2902 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
2903 virtnet_napi_do_enable(rq->vq, &rq->napi);
2904
2905 /* In theory, this can happen: if we don't get any buffers in
2906 * we will *never* try to fill again.
2907 */
2908 if (still_empty)
2909 schedule_delayed_work(&vi->refill, HZ/2);
2910 }
2911 }
2912
virtnet_receive_xsk_bufs(struct virtnet_info * vi,struct receive_queue * rq,int budget,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2913 static int virtnet_receive_xsk_bufs(struct virtnet_info *vi,
2914 struct receive_queue *rq,
2915 int budget,
2916 unsigned int *xdp_xmit,
2917 struct virtnet_rq_stats *stats)
2918 {
2919 unsigned int len;
2920 int packets = 0;
2921 void *buf;
2922
2923 while (packets < budget) {
2924 buf = virtqueue_get_buf(rq->vq, &len);
2925 if (!buf)
2926 break;
2927
2928 virtnet_receive_xsk_buf(vi, rq, buf, len, xdp_xmit, stats);
2929 packets++;
2930 }
2931
2932 return packets;
2933 }
2934
virtnet_receive_packets(struct virtnet_info * vi,struct receive_queue * rq,int budget,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2935 static int virtnet_receive_packets(struct virtnet_info *vi,
2936 struct receive_queue *rq,
2937 int budget,
2938 unsigned int *xdp_xmit,
2939 struct virtnet_rq_stats *stats)
2940 {
2941 unsigned int len;
2942 int packets = 0;
2943 void *buf;
2944
2945 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2946 void *ctx;
2947 while (packets < budget &&
2948 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2949 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, stats);
2950 packets++;
2951 }
2952 } else {
2953 while (packets < budget &&
2954 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
2955 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, stats);
2956 packets++;
2957 }
2958 }
2959
2960 return packets;
2961 }
2962
virtnet_receive(struct receive_queue * rq,int budget,unsigned int * xdp_xmit)2963 static int virtnet_receive(struct receive_queue *rq, int budget,
2964 unsigned int *xdp_xmit)
2965 {
2966 struct virtnet_info *vi = rq->vq->vdev->priv;
2967 struct virtnet_rq_stats stats = {};
2968 int i, packets;
2969
2970 if (rq->xsk_pool)
2971 packets = virtnet_receive_xsk_bufs(vi, rq, budget, xdp_xmit, &stats);
2972 else
2973 packets = virtnet_receive_packets(vi, rq, budget, xdp_xmit, &stats);
2974
2975 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
2976 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
2977 spin_lock(&vi->refill_lock);
2978 if (vi->refill_enabled)
2979 schedule_delayed_work(&vi->refill, 0);
2980 spin_unlock(&vi->refill_lock);
2981 }
2982 }
2983
2984 u64_stats_set(&stats.packets, packets);
2985 u64_stats_update_begin(&rq->stats.syncp);
2986 for (i = 0; i < ARRAY_SIZE(virtnet_rq_stats_desc); i++) {
2987 size_t offset = virtnet_rq_stats_desc[i].offset;
2988 u64_stats_t *item, *src;
2989
2990 item = (u64_stats_t *)((u8 *)&rq->stats + offset);
2991 src = (u64_stats_t *)((u8 *)&stats + offset);
2992 u64_stats_add(item, u64_stats_read(src));
2993 }
2994
2995 u64_stats_add(&rq->stats.packets, u64_stats_read(&stats.packets));
2996 u64_stats_add(&rq->stats.bytes, u64_stats_read(&stats.bytes));
2997
2998 u64_stats_update_end(&rq->stats.syncp);
2999
3000 return packets;
3001 }
3002
virtnet_poll_cleantx(struct receive_queue * rq,int budget)3003 static void virtnet_poll_cleantx(struct receive_queue *rq, int budget)
3004 {
3005 struct virtnet_info *vi = rq->vq->vdev->priv;
3006 unsigned int index = vq2rxq(rq->vq);
3007 struct send_queue *sq = &vi->sq[index];
3008 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
3009
3010 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
3011 return;
3012
3013 if (__netif_tx_trylock(txq)) {
3014 if (sq->reset) {
3015 __netif_tx_unlock(txq);
3016 return;
3017 }
3018
3019 do {
3020 virtqueue_disable_cb(sq->vq);
3021 free_old_xmit(sq, txq, !!budget);
3022 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
3023
3024 if (sq->vq->num_free >= MAX_SKB_FRAGS + 2) {
3025 if (netif_tx_queue_stopped(txq)) {
3026 u64_stats_update_begin(&sq->stats.syncp);
3027 u64_stats_inc(&sq->stats.wake);
3028 u64_stats_update_end(&sq->stats.syncp);
3029 }
3030 netif_tx_wake_queue(txq);
3031 }
3032
3033 __netif_tx_unlock(txq);
3034 }
3035 }
3036
virtnet_rx_dim_update(struct virtnet_info * vi,struct receive_queue * rq)3037 static void virtnet_rx_dim_update(struct virtnet_info *vi, struct receive_queue *rq)
3038 {
3039 struct dim_sample cur_sample = {};
3040
3041 if (!rq->packets_in_napi)
3042 return;
3043
3044 /* Don't need protection when fetching stats, since fetcher and
3045 * updater of the stats are in same context
3046 */
3047 dim_update_sample(rq->calls,
3048 u64_stats_read(&rq->stats.packets),
3049 u64_stats_read(&rq->stats.bytes),
3050 &cur_sample);
3051
3052 net_dim(&rq->dim, &cur_sample);
3053 rq->packets_in_napi = 0;
3054 }
3055
virtnet_poll(struct napi_struct * napi,int budget)3056 static int virtnet_poll(struct napi_struct *napi, int budget)
3057 {
3058 struct receive_queue *rq =
3059 container_of(napi, struct receive_queue, napi);
3060 struct virtnet_info *vi = rq->vq->vdev->priv;
3061 struct send_queue *sq;
3062 unsigned int received;
3063 unsigned int xdp_xmit = 0;
3064 bool napi_complete;
3065
3066 virtnet_poll_cleantx(rq, budget);
3067
3068 received = virtnet_receive(rq, budget, &xdp_xmit);
3069 rq->packets_in_napi += received;
3070
3071 if (xdp_xmit & VIRTIO_XDP_REDIR)
3072 xdp_do_flush();
3073
3074 /* Out of packets? */
3075 if (received < budget) {
3076 napi_complete = virtqueue_napi_complete(napi, rq->vq, received);
3077 /* Intentionally not taking dim_lock here. This may result in a
3078 * spurious net_dim call. But if that happens virtnet_rx_dim_work
3079 * will not act on the scheduled work.
3080 */
3081 if (napi_complete && rq->dim_enabled)
3082 virtnet_rx_dim_update(vi, rq);
3083 }
3084
3085 if (xdp_xmit & VIRTIO_XDP_TX) {
3086 sq = virtnet_xdp_get_sq(vi);
3087 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
3088 u64_stats_update_begin(&sq->stats.syncp);
3089 u64_stats_inc(&sq->stats.kicks);
3090 u64_stats_update_end(&sq->stats.syncp);
3091 }
3092 virtnet_xdp_put_sq(vi, sq);
3093 }
3094
3095 return received;
3096 }
3097
virtnet_disable_queue_pair(struct virtnet_info * vi,int qp_index)3098 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
3099 {
3100 virtnet_napi_tx_disable(&vi->sq[qp_index]);
3101 virtnet_napi_disable(&vi->rq[qp_index]);
3102 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
3103 }
3104
virtnet_enable_queue_pair(struct virtnet_info * vi,int qp_index)3105 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
3106 {
3107 struct net_device *dev = vi->dev;
3108 int err;
3109
3110 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
3111 vi->rq[qp_index].napi.napi_id);
3112 if (err < 0)
3113 return err;
3114
3115 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
3116 MEM_TYPE_PAGE_SHARED, NULL);
3117 if (err < 0)
3118 goto err_xdp_reg_mem_model;
3119
3120 virtnet_napi_enable(&vi->rq[qp_index]);
3121 virtnet_napi_tx_enable(&vi->sq[qp_index]);
3122
3123 return 0;
3124
3125 err_xdp_reg_mem_model:
3126 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
3127 return err;
3128 }
3129
virtnet_cancel_dim(struct virtnet_info * vi,struct dim * dim)3130 static void virtnet_cancel_dim(struct virtnet_info *vi, struct dim *dim)
3131 {
3132 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3133 return;
3134 net_dim_work_cancel(dim);
3135 }
3136
virtnet_update_settings(struct virtnet_info * vi)3137 static void virtnet_update_settings(struct virtnet_info *vi)
3138 {
3139 u32 speed;
3140 u8 duplex;
3141
3142 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3143 return;
3144
3145 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
3146
3147 if (ethtool_validate_speed(speed))
3148 vi->speed = speed;
3149
3150 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
3151
3152 if (ethtool_validate_duplex(duplex))
3153 vi->duplex = duplex;
3154 }
3155
virtnet_open(struct net_device * dev)3156 static int virtnet_open(struct net_device *dev)
3157 {
3158 struct virtnet_info *vi = netdev_priv(dev);
3159 int i, err;
3160
3161 enable_delayed_refill(vi);
3162
3163 for (i = 0; i < vi->max_queue_pairs; i++) {
3164 if (i < vi->curr_queue_pairs)
3165 /* Make sure we have some buffers: if oom use wq. */
3166 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
3167 schedule_delayed_work(&vi->refill, 0);
3168
3169 err = virtnet_enable_queue_pair(vi, i);
3170 if (err < 0)
3171 goto err_enable_qp;
3172 }
3173
3174 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3175 if (vi->status & VIRTIO_NET_S_LINK_UP)
3176 netif_carrier_on(vi->dev);
3177 virtio_config_driver_enable(vi->vdev);
3178 } else {
3179 vi->status = VIRTIO_NET_S_LINK_UP;
3180 netif_carrier_on(dev);
3181 }
3182
3183 return 0;
3184
3185 err_enable_qp:
3186 disable_delayed_refill(vi);
3187 cancel_delayed_work_sync(&vi->refill);
3188
3189 for (i--; i >= 0; i--) {
3190 virtnet_disable_queue_pair(vi, i);
3191 virtnet_cancel_dim(vi, &vi->rq[i].dim);
3192 }
3193
3194 return err;
3195 }
3196
virtnet_poll_tx(struct napi_struct * napi,int budget)3197 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
3198 {
3199 struct send_queue *sq = container_of(napi, struct send_queue, napi);
3200 struct virtnet_info *vi = sq->vq->vdev->priv;
3201 unsigned int index = vq2txq(sq->vq);
3202 struct netdev_queue *txq;
3203 int opaque, xsk_done = 0;
3204 bool done;
3205
3206 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
3207 /* We don't need to enable cb for XDP */
3208 napi_complete_done(napi, 0);
3209 return 0;
3210 }
3211
3212 txq = netdev_get_tx_queue(vi->dev, index);
3213 __netif_tx_lock(txq, raw_smp_processor_id());
3214 virtqueue_disable_cb(sq->vq);
3215
3216 if (sq->xsk_pool)
3217 xsk_done = virtnet_xsk_xmit(sq, sq->xsk_pool, budget);
3218 else
3219 free_old_xmit(sq, txq, !!budget);
3220
3221 if (sq->vq->num_free >= MAX_SKB_FRAGS + 2) {
3222 if (netif_tx_queue_stopped(txq)) {
3223 u64_stats_update_begin(&sq->stats.syncp);
3224 u64_stats_inc(&sq->stats.wake);
3225 u64_stats_update_end(&sq->stats.syncp);
3226 }
3227 netif_tx_wake_queue(txq);
3228 }
3229
3230 if (xsk_done >= budget) {
3231 __netif_tx_unlock(txq);
3232 return budget;
3233 }
3234
3235 opaque = virtqueue_enable_cb_prepare(sq->vq);
3236
3237 done = napi_complete_done(napi, 0);
3238
3239 if (!done)
3240 virtqueue_disable_cb(sq->vq);
3241
3242 __netif_tx_unlock(txq);
3243
3244 if (done) {
3245 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
3246 if (napi_schedule_prep(napi)) {
3247 __netif_tx_lock(txq, raw_smp_processor_id());
3248 virtqueue_disable_cb(sq->vq);
3249 __netif_tx_unlock(txq);
3250 __napi_schedule(napi);
3251 }
3252 }
3253 }
3254
3255 return 0;
3256 }
3257
xmit_skb(struct send_queue * sq,struct sk_buff * skb,bool orphan)3258 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
3259 {
3260 struct virtio_net_hdr_mrg_rxbuf *hdr;
3261 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
3262 struct virtnet_info *vi = sq->vq->vdev->priv;
3263 int num_sg;
3264 unsigned hdr_len = vi->hdr_len;
3265 bool can_push;
3266
3267 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
3268
3269 can_push = vi->any_header_sg &&
3270 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
3271 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
3272 /* Even if we can, don't push here yet as this would skew
3273 * csum_start offset below. */
3274 if (can_push)
3275 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
3276 else
3277 hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
3278
3279 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
3280 virtio_is_little_endian(vi->vdev), false,
3281 0))
3282 return -EPROTO;
3283
3284 if (vi->mergeable_rx_bufs)
3285 hdr->num_buffers = 0;
3286
3287 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
3288 if (can_push) {
3289 __skb_push(skb, hdr_len);
3290 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
3291 if (unlikely(num_sg < 0))
3292 return num_sg;
3293 /* Pull header back to avoid skew in tx bytes calculations. */
3294 __skb_pull(skb, hdr_len);
3295 } else {
3296 sg_set_buf(sq->sg, hdr, hdr_len);
3297 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
3298 if (unlikely(num_sg < 0))
3299 return num_sg;
3300 num_sg++;
3301 }
3302
3303 return virtnet_add_outbuf(sq, num_sg, skb,
3304 orphan ? VIRTNET_XMIT_TYPE_SKB_ORPHAN : VIRTNET_XMIT_TYPE_SKB);
3305 }
3306
start_xmit(struct sk_buff * skb,struct net_device * dev)3307 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
3308 {
3309 struct virtnet_info *vi = netdev_priv(dev);
3310 int qnum = skb_get_queue_mapping(skb);
3311 struct send_queue *sq = &vi->sq[qnum];
3312 int err;
3313 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
3314 bool xmit_more = netdev_xmit_more();
3315 bool use_napi = sq->napi.weight;
3316 bool kick;
3317
3318 if (!use_napi)
3319 free_old_xmit(sq, txq, false);
3320 else
3321 virtqueue_disable_cb(sq->vq);
3322
3323 /* timestamp packet in software */
3324 skb_tx_timestamp(skb);
3325
3326 /* Try to transmit */
3327 err = xmit_skb(sq, skb, !use_napi);
3328
3329 /* This should not happen! */
3330 if (unlikely(err)) {
3331 DEV_STATS_INC(dev, tx_fifo_errors);
3332 if (net_ratelimit())
3333 dev_warn(&dev->dev,
3334 "Unexpected TXQ (%d) queue failure: %d\n",
3335 qnum, err);
3336 DEV_STATS_INC(dev, tx_dropped);
3337 dev_kfree_skb_any(skb);
3338 return NETDEV_TX_OK;
3339 }
3340
3341 /* Don't wait up for transmitted skbs to be freed. */
3342 if (!use_napi) {
3343 skb_orphan(skb);
3344 nf_reset_ct(skb);
3345 }
3346
3347 if (use_napi)
3348 tx_may_stop(vi, dev, sq);
3349 else
3350 check_sq_full_and_disable(vi, dev,sq);
3351
3352 kick = use_napi ? __netdev_tx_sent_queue(txq, skb->len, xmit_more) :
3353 !xmit_more || netif_xmit_stopped(txq);
3354 if (kick) {
3355 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
3356 u64_stats_update_begin(&sq->stats.syncp);
3357 u64_stats_inc(&sq->stats.kicks);
3358 u64_stats_update_end(&sq->stats.syncp);
3359 }
3360 }
3361
3362 if (use_napi && kick && unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
3363 virtqueue_napi_schedule(&sq->napi, sq->vq);
3364
3365 return NETDEV_TX_OK;
3366 }
3367
__virtnet_rx_pause(struct virtnet_info * vi,struct receive_queue * rq)3368 static void __virtnet_rx_pause(struct virtnet_info *vi,
3369 struct receive_queue *rq)
3370 {
3371 bool running = netif_running(vi->dev);
3372
3373 if (running) {
3374 virtnet_napi_disable(rq);
3375 virtnet_cancel_dim(vi, &rq->dim);
3376 }
3377 }
3378
virtnet_rx_pause_all(struct virtnet_info * vi)3379 static void virtnet_rx_pause_all(struct virtnet_info *vi)
3380 {
3381 int i;
3382
3383 /*
3384 * Make sure refill_work does not run concurrently to
3385 * avoid napi_disable race which leads to deadlock.
3386 */
3387 disable_delayed_refill(vi);
3388 cancel_delayed_work_sync(&vi->refill);
3389 for (i = 0; i < vi->max_queue_pairs; i++)
3390 __virtnet_rx_pause(vi, &vi->rq[i]);
3391 }
3392
virtnet_rx_pause(struct virtnet_info * vi,struct receive_queue * rq)3393 static void virtnet_rx_pause(struct virtnet_info *vi, struct receive_queue *rq)
3394 {
3395 /*
3396 * Make sure refill_work does not run concurrently to
3397 * avoid napi_disable race which leads to deadlock.
3398 */
3399 disable_delayed_refill(vi);
3400 cancel_delayed_work_sync(&vi->refill);
3401 __virtnet_rx_pause(vi, rq);
3402 }
3403
__virtnet_rx_resume(struct virtnet_info * vi,struct receive_queue * rq,bool refill)3404 static void __virtnet_rx_resume(struct virtnet_info *vi,
3405 struct receive_queue *rq,
3406 bool refill)
3407 {
3408 bool running = netif_running(vi->dev);
3409 bool schedule_refill = false;
3410
3411 if (refill && !try_fill_recv(vi, rq, GFP_KERNEL))
3412 schedule_refill = true;
3413 if (running)
3414 virtnet_napi_enable(rq);
3415
3416 if (schedule_refill)
3417 schedule_delayed_work(&vi->refill, 0);
3418 }
3419
virtnet_rx_resume_all(struct virtnet_info * vi)3420 static void virtnet_rx_resume_all(struct virtnet_info *vi)
3421 {
3422 int i;
3423
3424 enable_delayed_refill(vi);
3425 for (i = 0; i < vi->max_queue_pairs; i++) {
3426 if (i < vi->curr_queue_pairs)
3427 __virtnet_rx_resume(vi, &vi->rq[i], true);
3428 else
3429 __virtnet_rx_resume(vi, &vi->rq[i], false);
3430 }
3431 }
3432
virtnet_rx_resume(struct virtnet_info * vi,struct receive_queue * rq)3433 static void virtnet_rx_resume(struct virtnet_info *vi, struct receive_queue *rq)
3434 {
3435 enable_delayed_refill(vi);
3436 __virtnet_rx_resume(vi, rq, true);
3437 }
3438
virtnet_rx_resize(struct virtnet_info * vi,struct receive_queue * rq,u32 ring_num)3439 static int virtnet_rx_resize(struct virtnet_info *vi,
3440 struct receive_queue *rq, u32 ring_num)
3441 {
3442 int err, qindex;
3443
3444 qindex = rq - vi->rq;
3445
3446 virtnet_rx_pause(vi, rq);
3447
3448 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf, NULL);
3449 if (err)
3450 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
3451
3452 virtnet_rx_resume(vi, rq);
3453 return err;
3454 }
3455
virtnet_tx_pause(struct virtnet_info * vi,struct send_queue * sq)3456 static void virtnet_tx_pause(struct virtnet_info *vi, struct send_queue *sq)
3457 {
3458 bool running = netif_running(vi->dev);
3459 struct netdev_queue *txq;
3460 int qindex;
3461
3462 qindex = sq - vi->sq;
3463
3464 if (running)
3465 virtnet_napi_tx_disable(sq);
3466
3467 txq = netdev_get_tx_queue(vi->dev, qindex);
3468
3469 /* 1. wait all ximt complete
3470 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
3471 */
3472 __netif_tx_lock_bh(txq);
3473
3474 /* Prevent rx poll from accessing sq. */
3475 sq->reset = true;
3476
3477 /* Prevent the upper layer from trying to send packets. */
3478 netif_stop_subqueue(vi->dev, qindex);
3479
3480 __netif_tx_unlock_bh(txq);
3481 }
3482
virtnet_tx_resume(struct virtnet_info * vi,struct send_queue * sq)3483 static void virtnet_tx_resume(struct virtnet_info *vi, struct send_queue *sq)
3484 {
3485 bool running = netif_running(vi->dev);
3486 struct netdev_queue *txq;
3487 int qindex;
3488
3489 qindex = sq - vi->sq;
3490
3491 txq = netdev_get_tx_queue(vi->dev, qindex);
3492
3493 __netif_tx_lock_bh(txq);
3494 sq->reset = false;
3495 netif_tx_wake_queue(txq);
3496 __netif_tx_unlock_bh(txq);
3497
3498 if (running)
3499 virtnet_napi_tx_enable(sq);
3500 }
3501
virtnet_tx_resize(struct virtnet_info * vi,struct send_queue * sq,u32 ring_num)3502 static int virtnet_tx_resize(struct virtnet_info *vi, struct send_queue *sq,
3503 u32 ring_num)
3504 {
3505 int qindex, err;
3506
3507 if (ring_num <= MAX_SKB_FRAGS + 2) {
3508 netdev_err(vi->dev, "tx size (%d) cannot be smaller than %d\n",
3509 ring_num, MAX_SKB_FRAGS + 2);
3510 return -EINVAL;
3511 }
3512
3513 qindex = sq - vi->sq;
3514
3515 virtnet_tx_pause(vi, sq);
3516
3517 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf,
3518 virtnet_sq_free_unused_buf_done);
3519 if (err)
3520 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
3521
3522 virtnet_tx_resume(vi, sq);
3523
3524 return err;
3525 }
3526
3527 /*
3528 * Send command via the control virtqueue and check status. Commands
3529 * supported by the hypervisor, as indicated by feature bits, should
3530 * never fail unless improperly formatted.
3531 */
virtnet_send_command_reply(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out,struct scatterlist * in)3532 static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd,
3533 struct scatterlist *out,
3534 struct scatterlist *in)
3535 {
3536 struct scatterlist *sgs[5], hdr, stat;
3537 u32 out_num = 0, tmp, in_num = 0;
3538 bool ok;
3539 int ret;
3540
3541 /* Caller should know better */
3542 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
3543
3544 mutex_lock(&vi->cvq_lock);
3545 vi->ctrl->status = ~0;
3546 vi->ctrl->hdr.class = class;
3547 vi->ctrl->hdr.cmd = cmd;
3548 /* Add header */
3549 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
3550 sgs[out_num++] = &hdr;
3551
3552 if (out)
3553 sgs[out_num++] = out;
3554
3555 /* Add return status. */
3556 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
3557 sgs[out_num + in_num++] = &stat;
3558
3559 if (in)
3560 sgs[out_num + in_num++] = in;
3561
3562 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
3563 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
3564 if (ret < 0) {
3565 dev_warn(&vi->vdev->dev,
3566 "Failed to add sgs for command vq: %d\n.", ret);
3567 mutex_unlock(&vi->cvq_lock);
3568 return false;
3569 }
3570
3571 if (unlikely(!virtqueue_kick(vi->cvq)))
3572 goto unlock;
3573
3574 /* Spin for a response, the kick causes an ioport write, trapping
3575 * into the hypervisor, so the request should be handled immediately.
3576 */
3577 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
3578 !virtqueue_is_broken(vi->cvq)) {
3579 cond_resched();
3580 cpu_relax();
3581 }
3582
3583 unlock:
3584 ok = vi->ctrl->status == VIRTIO_NET_OK;
3585 mutex_unlock(&vi->cvq_lock);
3586 return ok;
3587 }
3588
virtnet_send_command(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out)3589 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
3590 struct scatterlist *out)
3591 {
3592 return virtnet_send_command_reply(vi, class, cmd, out, NULL);
3593 }
3594
virtnet_set_mac_address(struct net_device * dev,void * p)3595 static int virtnet_set_mac_address(struct net_device *dev, void *p)
3596 {
3597 struct virtnet_info *vi = netdev_priv(dev);
3598 struct virtio_device *vdev = vi->vdev;
3599 int ret;
3600 struct sockaddr *addr;
3601 struct scatterlist sg;
3602
3603 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3604 return -EOPNOTSUPP;
3605
3606 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
3607 if (!addr)
3608 return -ENOMEM;
3609
3610 ret = eth_prepare_mac_addr_change(dev, addr);
3611 if (ret)
3612 goto out;
3613
3614 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
3615 sg_init_one(&sg, addr->sa_data, dev->addr_len);
3616 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
3617 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
3618 dev_warn(&vdev->dev,
3619 "Failed to set mac address by vq command.\n");
3620 ret = -EINVAL;
3621 goto out;
3622 }
3623 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
3624 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3625 unsigned int i;
3626
3627 /* Naturally, this has an atomicity problem. */
3628 for (i = 0; i < dev->addr_len; i++)
3629 virtio_cwrite8(vdev,
3630 offsetof(struct virtio_net_config, mac) +
3631 i, addr->sa_data[i]);
3632 }
3633
3634 eth_commit_mac_addr_change(dev, p);
3635 ret = 0;
3636
3637 out:
3638 kfree(addr);
3639 return ret;
3640 }
3641
virtnet_stats(struct net_device * dev,struct rtnl_link_stats64 * tot)3642 static void virtnet_stats(struct net_device *dev,
3643 struct rtnl_link_stats64 *tot)
3644 {
3645 struct virtnet_info *vi = netdev_priv(dev);
3646 unsigned int start;
3647 int i;
3648
3649 for (i = 0; i < vi->max_queue_pairs; i++) {
3650 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
3651 struct receive_queue *rq = &vi->rq[i];
3652 struct send_queue *sq = &vi->sq[i];
3653
3654 do {
3655 start = u64_stats_fetch_begin(&sq->stats.syncp);
3656 tpackets = u64_stats_read(&sq->stats.packets);
3657 tbytes = u64_stats_read(&sq->stats.bytes);
3658 terrors = u64_stats_read(&sq->stats.tx_timeouts);
3659 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
3660
3661 do {
3662 start = u64_stats_fetch_begin(&rq->stats.syncp);
3663 rpackets = u64_stats_read(&rq->stats.packets);
3664 rbytes = u64_stats_read(&rq->stats.bytes);
3665 rdrops = u64_stats_read(&rq->stats.drops);
3666 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
3667
3668 tot->rx_packets += rpackets;
3669 tot->tx_packets += tpackets;
3670 tot->rx_bytes += rbytes;
3671 tot->tx_bytes += tbytes;
3672 tot->rx_dropped += rdrops;
3673 tot->tx_errors += terrors;
3674 }
3675
3676 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
3677 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
3678 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
3679 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
3680 }
3681
virtnet_ack_link_announce(struct virtnet_info * vi)3682 static void virtnet_ack_link_announce(struct virtnet_info *vi)
3683 {
3684 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
3685 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
3686 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
3687 }
3688
3689 static bool virtnet_commit_rss_command(struct virtnet_info *vi);
3690
virtnet_rss_update_by_qpairs(struct virtnet_info * vi,u16 queue_pairs)3691 static void virtnet_rss_update_by_qpairs(struct virtnet_info *vi, u16 queue_pairs)
3692 {
3693 u32 indir_val = 0;
3694 int i = 0;
3695
3696 for (; i < vi->rss_indir_table_size; ++i) {
3697 indir_val = ethtool_rxfh_indir_default(i, queue_pairs);
3698 vi->rss_hdr->indirection_table[i] = cpu_to_le16(indir_val);
3699 }
3700 vi->rss_trailer.max_tx_vq = cpu_to_le16(queue_pairs);
3701 }
3702
virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)3703 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
3704 {
3705 struct virtio_net_ctrl_mq *mq __free(kfree) = NULL;
3706 struct virtio_net_rss_config_hdr *old_rss_hdr;
3707 struct virtio_net_rss_config_trailer old_rss_trailer;
3708 struct net_device *dev = vi->dev;
3709 struct scatterlist sg;
3710
3711 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
3712 return 0;
3713
3714 /* Firstly check if we need update rss. Do updating if both (1) rss enabled and
3715 * (2) no user configuration.
3716 *
3717 * During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is,
3718 * the device updates queue_pairs together with rss, so we can skip the sperate queue_pairs
3719 * update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.
3720 */
3721 if (vi->has_rss && !netif_is_rxfh_configured(dev)) {
3722 old_rss_hdr = vi->rss_hdr;
3723 old_rss_trailer = vi->rss_trailer;
3724 vi->rss_hdr = devm_kzalloc(&dev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
3725 if (!vi->rss_hdr) {
3726 vi->rss_hdr = old_rss_hdr;
3727 return -ENOMEM;
3728 }
3729
3730 *vi->rss_hdr = *old_rss_hdr;
3731 virtnet_rss_update_by_qpairs(vi, queue_pairs);
3732
3733 if (!virtnet_commit_rss_command(vi)) {
3734 /* restore ctrl_rss if commit_rss_command failed */
3735 devm_kfree(&dev->dev, vi->rss_hdr);
3736 vi->rss_hdr = old_rss_hdr;
3737 vi->rss_trailer = old_rss_trailer;
3738
3739 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d, because committing RSS failed\n",
3740 queue_pairs);
3741 return -EINVAL;
3742 }
3743 devm_kfree(&dev->dev, old_rss_hdr);
3744 goto succ;
3745 }
3746
3747 mq = kzalloc(sizeof(*mq), GFP_KERNEL);
3748 if (!mq)
3749 return -ENOMEM;
3750
3751 mq->virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
3752 sg_init_one(&sg, mq, sizeof(*mq));
3753
3754 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
3755 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
3756 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
3757 queue_pairs);
3758 return -EINVAL;
3759 }
3760 succ:
3761 vi->curr_queue_pairs = queue_pairs;
3762 /* virtnet_open() will refill when device is going to up. */
3763 spin_lock_bh(&vi->refill_lock);
3764 if (dev->flags & IFF_UP && vi->refill_enabled)
3765 schedule_delayed_work(&vi->refill, 0);
3766 spin_unlock_bh(&vi->refill_lock);
3767
3768 return 0;
3769 }
3770
virtnet_close(struct net_device * dev)3771 static int virtnet_close(struct net_device *dev)
3772 {
3773 struct virtnet_info *vi = netdev_priv(dev);
3774 int i;
3775
3776 /* Make sure NAPI doesn't schedule refill work */
3777 disable_delayed_refill(vi);
3778 /* Make sure refill_work doesn't re-enable napi! */
3779 cancel_delayed_work_sync(&vi->refill);
3780 /* Prevent the config change callback from changing carrier
3781 * after close
3782 */
3783 virtio_config_driver_disable(vi->vdev);
3784 /* Stop getting status/speed updates: we don't care until next
3785 * open
3786 */
3787 cancel_work_sync(&vi->config_work);
3788
3789 for (i = 0; i < vi->max_queue_pairs; i++) {
3790 virtnet_disable_queue_pair(vi, i);
3791 virtnet_cancel_dim(vi, &vi->rq[i].dim);
3792 }
3793
3794 netif_carrier_off(dev);
3795
3796 return 0;
3797 }
3798
virtnet_rx_mode_work(struct work_struct * work)3799 static void virtnet_rx_mode_work(struct work_struct *work)
3800 {
3801 struct virtnet_info *vi =
3802 container_of(work, struct virtnet_info, rx_mode_work);
3803 u8 *promisc_allmulti __free(kfree) = NULL;
3804 struct net_device *dev = vi->dev;
3805 struct scatterlist sg[2];
3806 struct virtio_net_ctrl_mac *mac_data;
3807 struct netdev_hw_addr *ha;
3808 int uc_count;
3809 int mc_count;
3810 void *buf;
3811 int i;
3812
3813 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
3814 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
3815 return;
3816
3817 promisc_allmulti = kzalloc(sizeof(*promisc_allmulti), GFP_KERNEL);
3818 if (!promisc_allmulti) {
3819 dev_warn(&dev->dev, "Failed to set RX mode, no memory.\n");
3820 return;
3821 }
3822
3823 rtnl_lock();
3824
3825 *promisc_allmulti = !!(dev->flags & IFF_PROMISC);
3826 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
3827
3828 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
3829 VIRTIO_NET_CTRL_RX_PROMISC, sg))
3830 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
3831 *promisc_allmulti ? "en" : "dis");
3832
3833 *promisc_allmulti = !!(dev->flags & IFF_ALLMULTI);
3834 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
3835
3836 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
3837 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
3838 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
3839 *promisc_allmulti ? "en" : "dis");
3840
3841 netif_addr_lock_bh(dev);
3842
3843 uc_count = netdev_uc_count(dev);
3844 mc_count = netdev_mc_count(dev);
3845 /* MAC filter - use one buffer for both lists */
3846 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
3847 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
3848 mac_data = buf;
3849 if (!buf) {
3850 netif_addr_unlock_bh(dev);
3851 rtnl_unlock();
3852 return;
3853 }
3854
3855 sg_init_table(sg, 2);
3856
3857 /* Store the unicast list and count in the front of the buffer */
3858 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
3859 i = 0;
3860 netdev_for_each_uc_addr(ha, dev)
3861 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
3862
3863 sg_set_buf(&sg[0], mac_data,
3864 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
3865
3866 /* multicast list and count fill the end */
3867 mac_data = (void *)&mac_data->macs[uc_count][0];
3868
3869 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
3870 i = 0;
3871 netdev_for_each_mc_addr(ha, dev)
3872 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
3873
3874 netif_addr_unlock_bh(dev);
3875
3876 sg_set_buf(&sg[1], mac_data,
3877 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
3878
3879 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
3880 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
3881 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
3882
3883 rtnl_unlock();
3884
3885 kfree(buf);
3886 }
3887
virtnet_set_rx_mode(struct net_device * dev)3888 static void virtnet_set_rx_mode(struct net_device *dev)
3889 {
3890 struct virtnet_info *vi = netdev_priv(dev);
3891
3892 if (vi->rx_mode_work_enabled)
3893 schedule_work(&vi->rx_mode_work);
3894 }
3895
virtnet_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)3896 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
3897 __be16 proto, u16 vid)
3898 {
3899 struct virtnet_info *vi = netdev_priv(dev);
3900 __virtio16 *_vid __free(kfree) = NULL;
3901 struct scatterlist sg;
3902
3903 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL);
3904 if (!_vid)
3905 return -ENOMEM;
3906
3907 *_vid = cpu_to_virtio16(vi->vdev, vid);
3908 sg_init_one(&sg, _vid, sizeof(*_vid));
3909
3910 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
3911 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
3912 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
3913 return 0;
3914 }
3915
virtnet_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)3916 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
3917 __be16 proto, u16 vid)
3918 {
3919 struct virtnet_info *vi = netdev_priv(dev);
3920 __virtio16 *_vid __free(kfree) = NULL;
3921 struct scatterlist sg;
3922
3923 _vid = kzalloc(sizeof(*_vid), GFP_KERNEL);
3924 if (!_vid)
3925 return -ENOMEM;
3926
3927 *_vid = cpu_to_virtio16(vi->vdev, vid);
3928 sg_init_one(&sg, _vid, sizeof(*_vid));
3929
3930 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
3931 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
3932 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
3933 return 0;
3934 }
3935
virtnet_clean_affinity(struct virtnet_info * vi)3936 static void virtnet_clean_affinity(struct virtnet_info *vi)
3937 {
3938 int i;
3939
3940 if (vi->affinity_hint_set) {
3941 for (i = 0; i < vi->max_queue_pairs; i++) {
3942 virtqueue_set_affinity(vi->rq[i].vq, NULL);
3943 virtqueue_set_affinity(vi->sq[i].vq, NULL);
3944 }
3945
3946 vi->affinity_hint_set = false;
3947 }
3948 }
3949
virtnet_set_affinity(struct virtnet_info * vi)3950 static void virtnet_set_affinity(struct virtnet_info *vi)
3951 {
3952 cpumask_var_t mask;
3953 int stragglers;
3954 int group_size;
3955 int i, start = 0, cpu;
3956 int num_cpu;
3957 int stride;
3958
3959 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
3960 virtnet_clean_affinity(vi);
3961 return;
3962 }
3963
3964 num_cpu = num_online_cpus();
3965 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
3966 stragglers = num_cpu >= vi->curr_queue_pairs ?
3967 num_cpu % vi->curr_queue_pairs :
3968 0;
3969
3970 for (i = 0; i < vi->curr_queue_pairs; i++) {
3971 group_size = stride + (i < stragglers ? 1 : 0);
3972
3973 for_each_online_cpu_wrap(cpu, start) {
3974 if (!group_size--) {
3975 start = cpu;
3976 break;
3977 }
3978 cpumask_set_cpu(cpu, mask);
3979 }
3980
3981 virtqueue_set_affinity(vi->rq[i].vq, mask);
3982 virtqueue_set_affinity(vi->sq[i].vq, mask);
3983 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
3984 cpumask_clear(mask);
3985 }
3986
3987 vi->affinity_hint_set = true;
3988 free_cpumask_var(mask);
3989 }
3990
virtnet_cpu_online(unsigned int cpu,struct hlist_node * node)3991 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
3992 {
3993 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3994 node);
3995 virtnet_set_affinity(vi);
3996 return 0;
3997 }
3998
virtnet_cpu_dead(unsigned int cpu,struct hlist_node * node)3999 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
4000 {
4001 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
4002 node_dead);
4003 virtnet_set_affinity(vi);
4004 return 0;
4005 }
4006
virtnet_cpu_down_prep(unsigned int cpu,struct hlist_node * node)4007 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
4008 {
4009 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
4010 node);
4011
4012 virtnet_clean_affinity(vi);
4013 return 0;
4014 }
4015
4016 static enum cpuhp_state virtionet_online;
4017
virtnet_cpu_notif_add(struct virtnet_info * vi)4018 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
4019 {
4020 int ret;
4021
4022 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
4023 if (ret)
4024 return ret;
4025 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
4026 &vi->node_dead);
4027 if (!ret)
4028 return ret;
4029 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
4030 return ret;
4031 }
4032
virtnet_cpu_notif_remove(struct virtnet_info * vi)4033 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
4034 {
4035 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
4036 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
4037 &vi->node_dead);
4038 }
4039
virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 vqn,u32 max_usecs,u32 max_packets)4040 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
4041 u16 vqn, u32 max_usecs, u32 max_packets)
4042 {
4043 struct virtio_net_ctrl_coal_vq *coal_vq __free(kfree) = NULL;
4044 struct scatterlist sgs;
4045
4046 coal_vq = kzalloc(sizeof(*coal_vq), GFP_KERNEL);
4047 if (!coal_vq)
4048 return -ENOMEM;
4049
4050 coal_vq->vqn = cpu_to_le16(vqn);
4051 coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
4052 coal_vq->coal.max_packets = cpu_to_le32(max_packets);
4053 sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
4054
4055 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
4056 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
4057 &sgs))
4058 return -EINVAL;
4059
4060 return 0;
4061 }
4062
virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 queue,u32 max_usecs,u32 max_packets)4063 static int virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
4064 u16 queue, u32 max_usecs,
4065 u32 max_packets)
4066 {
4067 int err;
4068
4069 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
4070 return -EOPNOTSUPP;
4071
4072 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
4073 max_usecs, max_packets);
4074 if (err)
4075 return err;
4076
4077 vi->rq[queue].intr_coal.max_usecs = max_usecs;
4078 vi->rq[queue].intr_coal.max_packets = max_packets;
4079
4080 return 0;
4081 }
4082
virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 queue,u32 max_usecs,u32 max_packets)4083 static int virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
4084 u16 queue, u32 max_usecs,
4085 u32 max_packets)
4086 {
4087 int err;
4088
4089 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
4090 return -EOPNOTSUPP;
4091
4092 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
4093 max_usecs, max_packets);
4094 if (err)
4095 return err;
4096
4097 vi->sq[queue].intr_coal.max_usecs = max_usecs;
4098 vi->sq[queue].intr_coal.max_packets = max_packets;
4099
4100 return 0;
4101 }
4102
virtnet_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)4103 static void virtnet_get_ringparam(struct net_device *dev,
4104 struct ethtool_ringparam *ring,
4105 struct kernel_ethtool_ringparam *kernel_ring,
4106 struct netlink_ext_ack *extack)
4107 {
4108 struct virtnet_info *vi = netdev_priv(dev);
4109
4110 ring->rx_max_pending = vi->rq[0].vq->num_max;
4111 ring->tx_max_pending = vi->sq[0].vq->num_max;
4112 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
4113 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
4114 }
4115
virtnet_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)4116 static int virtnet_set_ringparam(struct net_device *dev,
4117 struct ethtool_ringparam *ring,
4118 struct kernel_ethtool_ringparam *kernel_ring,
4119 struct netlink_ext_ack *extack)
4120 {
4121 struct virtnet_info *vi = netdev_priv(dev);
4122 u32 rx_pending, tx_pending;
4123 struct receive_queue *rq;
4124 struct send_queue *sq;
4125 int i, err;
4126
4127 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
4128 return -EINVAL;
4129
4130 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
4131 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
4132
4133 if (ring->rx_pending == rx_pending &&
4134 ring->tx_pending == tx_pending)
4135 return 0;
4136
4137 if (ring->rx_pending > vi->rq[0].vq->num_max)
4138 return -EINVAL;
4139
4140 if (ring->tx_pending > vi->sq[0].vq->num_max)
4141 return -EINVAL;
4142
4143 for (i = 0; i < vi->max_queue_pairs; i++) {
4144 rq = vi->rq + i;
4145 sq = vi->sq + i;
4146
4147 if (ring->tx_pending != tx_pending) {
4148 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
4149 if (err)
4150 return err;
4151
4152 /* Upon disabling and re-enabling a transmit virtqueue, the device must
4153 * set the coalescing parameters of the virtqueue to those configured
4154 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
4155 * did not set any TX coalescing parameters, to 0.
4156 */
4157 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, i,
4158 vi->intr_coal_tx.max_usecs,
4159 vi->intr_coal_tx.max_packets);
4160
4161 /* Don't break the tx resize action if the vq coalescing is not
4162 * supported. The same is true for rx resize below.
4163 */
4164 if (err && err != -EOPNOTSUPP)
4165 return err;
4166 }
4167
4168 if (ring->rx_pending != rx_pending) {
4169 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
4170 if (err)
4171 return err;
4172
4173 /* The reason is same as the transmit virtqueue reset */
4174 mutex_lock(&vi->rq[i].dim_lock);
4175 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, i,
4176 vi->intr_coal_rx.max_usecs,
4177 vi->intr_coal_rx.max_packets);
4178 mutex_unlock(&vi->rq[i].dim_lock);
4179 if (err && err != -EOPNOTSUPP)
4180 return err;
4181 }
4182 }
4183
4184 return 0;
4185 }
4186
virtnet_commit_rss_command(struct virtnet_info * vi)4187 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
4188 {
4189 struct net_device *dev = vi->dev;
4190 struct scatterlist sgs[2];
4191
4192 /* prepare sgs */
4193 sg_init_table(sgs, 2);
4194 sg_set_buf(&sgs[0], vi->rss_hdr, virtnet_rss_hdr_size(vi));
4195 sg_set_buf(&sgs[1], &vi->rss_trailer, virtnet_rss_trailer_size(vi));
4196
4197 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
4198 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
4199 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs))
4200 goto err;
4201
4202 return true;
4203
4204 err:
4205 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
4206 return false;
4207
4208 }
4209
virtnet_init_default_rss(struct virtnet_info * vi)4210 static void virtnet_init_default_rss(struct virtnet_info *vi)
4211 {
4212 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_supported);
4213 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
4214 vi->rss_hdr->indirection_table_mask = vi->rss_indir_table_size
4215 ? cpu_to_le16(vi->rss_indir_table_size - 1) : 0;
4216 vi->rss_hdr->unclassified_queue = 0;
4217
4218 virtnet_rss_update_by_qpairs(vi, vi->curr_queue_pairs);
4219
4220 vi->rss_trailer.hash_key_length = vi->rss_key_size;
4221
4222 netdev_rss_key_fill(vi->rss_hash_key_data, vi->rss_key_size);
4223 }
4224
virtnet_get_hashflow(const struct virtnet_info * vi,struct ethtool_rxnfc * info)4225 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
4226 {
4227 info->data = 0;
4228 switch (info->flow_type) {
4229 case TCP_V4_FLOW:
4230 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
4231 info->data = RXH_IP_SRC | RXH_IP_DST |
4232 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4233 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
4234 info->data = RXH_IP_SRC | RXH_IP_DST;
4235 }
4236 break;
4237 case TCP_V6_FLOW:
4238 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
4239 info->data = RXH_IP_SRC | RXH_IP_DST |
4240 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4241 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
4242 info->data = RXH_IP_SRC | RXH_IP_DST;
4243 }
4244 break;
4245 case UDP_V4_FLOW:
4246 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
4247 info->data = RXH_IP_SRC | RXH_IP_DST |
4248 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4249 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
4250 info->data = RXH_IP_SRC | RXH_IP_DST;
4251 }
4252 break;
4253 case UDP_V6_FLOW:
4254 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
4255 info->data = RXH_IP_SRC | RXH_IP_DST |
4256 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4257 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
4258 info->data = RXH_IP_SRC | RXH_IP_DST;
4259 }
4260 break;
4261 case IPV4_FLOW:
4262 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
4263 info->data = RXH_IP_SRC | RXH_IP_DST;
4264
4265 break;
4266 case IPV6_FLOW:
4267 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
4268 info->data = RXH_IP_SRC | RXH_IP_DST;
4269
4270 break;
4271 default:
4272 info->data = 0;
4273 break;
4274 }
4275 }
4276
virtnet_set_hashflow(struct virtnet_info * vi,struct ethtool_rxnfc * info)4277 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
4278 {
4279 u32 new_hashtypes = vi->rss_hash_types_saved;
4280 bool is_disable = info->data & RXH_DISCARD;
4281 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
4282
4283 /* supports only 'sd', 'sdfn' and 'r' */
4284 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
4285 return false;
4286
4287 switch (info->flow_type) {
4288 case TCP_V4_FLOW:
4289 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
4290 if (!is_disable)
4291 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
4292 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
4293 break;
4294 case UDP_V4_FLOW:
4295 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
4296 if (!is_disable)
4297 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
4298 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
4299 break;
4300 case IPV4_FLOW:
4301 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
4302 if (!is_disable)
4303 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
4304 break;
4305 case TCP_V6_FLOW:
4306 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
4307 if (!is_disable)
4308 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
4309 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
4310 break;
4311 case UDP_V6_FLOW:
4312 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
4313 if (!is_disable)
4314 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
4315 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
4316 break;
4317 case IPV6_FLOW:
4318 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
4319 if (!is_disable)
4320 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
4321 break;
4322 default:
4323 /* unsupported flow */
4324 return false;
4325 }
4326
4327 /* if unsupported hashtype was set */
4328 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
4329 return false;
4330
4331 if (new_hashtypes != vi->rss_hash_types_saved) {
4332 vi->rss_hash_types_saved = new_hashtypes;
4333 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
4334 if (vi->dev->features & NETIF_F_RXHASH)
4335 return virtnet_commit_rss_command(vi);
4336 }
4337
4338 return true;
4339 }
4340
virtnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)4341 static void virtnet_get_drvinfo(struct net_device *dev,
4342 struct ethtool_drvinfo *info)
4343 {
4344 struct virtnet_info *vi = netdev_priv(dev);
4345 struct virtio_device *vdev = vi->vdev;
4346
4347 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
4348 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
4349 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
4350
4351 }
4352
4353 /* TODO: Eliminate OOO packets during switching */
virtnet_set_channels(struct net_device * dev,struct ethtool_channels * channels)4354 static int virtnet_set_channels(struct net_device *dev,
4355 struct ethtool_channels *channels)
4356 {
4357 struct virtnet_info *vi = netdev_priv(dev);
4358 u16 queue_pairs = channels->combined_count;
4359 int err;
4360
4361 /* We don't support separate rx/tx channels.
4362 * We don't allow setting 'other' channels.
4363 */
4364 if (channels->rx_count || channels->tx_count || channels->other_count)
4365 return -EINVAL;
4366
4367 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
4368 return -EINVAL;
4369
4370 /* For now we don't support modifying channels while XDP is loaded
4371 * also when XDP is loaded all RX queues have XDP programs so we only
4372 * need to check a single RX queue.
4373 */
4374 if (vi->rq[0].xdp_prog)
4375 return -EINVAL;
4376
4377 cpus_read_lock();
4378 err = virtnet_set_queues(vi, queue_pairs);
4379 if (err) {
4380 cpus_read_unlock();
4381 goto err;
4382 }
4383 virtnet_set_affinity(vi);
4384 cpus_read_unlock();
4385
4386 netif_set_real_num_tx_queues(dev, queue_pairs);
4387 netif_set_real_num_rx_queues(dev, queue_pairs);
4388 err:
4389 return err;
4390 }
4391
virtnet_stats_sprintf(u8 ** p,const char * fmt,const char * noq_fmt,int num,int qid,const struct virtnet_stat_desc * desc)4392 static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt,
4393 int num, int qid, const struct virtnet_stat_desc *desc)
4394 {
4395 int i;
4396
4397 if (qid < 0) {
4398 for (i = 0; i < num; ++i)
4399 ethtool_sprintf(p, noq_fmt, desc[i].desc);
4400 } else {
4401 for (i = 0; i < num; ++i)
4402 ethtool_sprintf(p, fmt, qid, desc[i].desc);
4403 }
4404 }
4405
4406 /* qid == -1: for rx/tx queue total field */
virtnet_get_stats_string(struct virtnet_info * vi,int type,int qid,u8 ** data)4407 static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data)
4408 {
4409 const struct virtnet_stat_desc *desc;
4410 const char *fmt, *noq_fmt;
4411 u8 *p = *data;
4412 u32 num;
4413
4414 if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) {
4415 noq_fmt = "cq_hw_%s";
4416
4417 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
4418 desc = &virtnet_stats_cvq_desc[0];
4419 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
4420
4421 virtnet_stats_sprintf(&p, NULL, noq_fmt, num, -1, desc);
4422 }
4423 }
4424
4425 if (type == VIRTNET_Q_TYPE_RX) {
4426 fmt = "rx%u_%s";
4427 noq_fmt = "rx_%s";
4428
4429 desc = &virtnet_rq_stats_desc[0];
4430 num = ARRAY_SIZE(virtnet_rq_stats_desc);
4431
4432 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4433
4434 fmt = "rx%u_hw_%s";
4435 noq_fmt = "rx_hw_%s";
4436
4437 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4438 desc = &virtnet_stats_rx_basic_desc[0];
4439 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4440
4441 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4442 }
4443
4444 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4445 desc = &virtnet_stats_rx_csum_desc[0];
4446 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4447
4448 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4449 }
4450
4451 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4452 desc = &virtnet_stats_rx_speed_desc[0];
4453 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4454
4455 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4456 }
4457 }
4458
4459 if (type == VIRTNET_Q_TYPE_TX) {
4460 fmt = "tx%u_%s";
4461 noq_fmt = "tx_%s";
4462
4463 desc = &virtnet_sq_stats_desc[0];
4464 num = ARRAY_SIZE(virtnet_sq_stats_desc);
4465
4466 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4467
4468 fmt = "tx%u_hw_%s";
4469 noq_fmt = "tx_hw_%s";
4470
4471 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4472 desc = &virtnet_stats_tx_basic_desc[0];
4473 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4474
4475 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4476 }
4477
4478 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4479 desc = &virtnet_stats_tx_gso_desc[0];
4480 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4481
4482 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4483 }
4484
4485 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4486 desc = &virtnet_stats_tx_speed_desc[0];
4487 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4488
4489 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4490 }
4491 }
4492
4493 *data = p;
4494 }
4495
4496 struct virtnet_stats_ctx {
4497 /* The stats are write to qstats or ethtool -S */
4498 bool to_qstat;
4499
4500 /* Used to calculate the offset inside the output buffer. */
4501 u32 desc_num[3];
4502
4503 /* The actual supported stat types. */
4504 u64 bitmap[3];
4505
4506 /* Used to calculate the reply buffer size. */
4507 u32 size[3];
4508
4509 /* Record the output buffer. */
4510 u64 *data;
4511 };
4512
virtnet_stats_ctx_init(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,u64 * data,bool to_qstat)4513 static void virtnet_stats_ctx_init(struct virtnet_info *vi,
4514 struct virtnet_stats_ctx *ctx,
4515 u64 *data, bool to_qstat)
4516 {
4517 u32 queue_type;
4518
4519 ctx->data = data;
4520 ctx->to_qstat = to_qstat;
4521
4522 if (to_qstat) {
4523 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
4524 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
4525
4526 queue_type = VIRTNET_Q_TYPE_RX;
4527
4528 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4529 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
4530 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
4531 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
4532 }
4533
4534 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4535 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
4536 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
4537 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
4538 }
4539
4540 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
4541 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_GSO;
4542 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
4543 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_gso);
4544 }
4545
4546 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4547 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
4548 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
4549 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
4550 }
4551
4552 queue_type = VIRTNET_Q_TYPE_TX;
4553
4554 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4555 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
4556 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
4557 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
4558 }
4559
4560 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
4561 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_CSUM;
4562 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
4563 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_csum);
4564 }
4565
4566 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4567 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
4568 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
4569 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
4570 }
4571
4572 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4573 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
4574 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
4575 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
4576 }
4577
4578 return;
4579 }
4580
4581 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc);
4582 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc);
4583
4584 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
4585 queue_type = VIRTNET_Q_TYPE_CQ;
4586
4587 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_CVQ;
4588 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_cvq_desc);
4589 ctx->size[queue_type] += sizeof(struct virtio_net_stats_cvq);
4590 }
4591
4592 queue_type = VIRTNET_Q_TYPE_RX;
4593
4594 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4595 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
4596 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4597 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
4598 }
4599
4600 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4601 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
4602 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4603 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
4604 }
4605
4606 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4607 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
4608 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4609 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
4610 }
4611
4612 queue_type = VIRTNET_Q_TYPE_TX;
4613
4614 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4615 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
4616 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4617 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
4618 }
4619
4620 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4621 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
4622 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4623 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
4624 }
4625
4626 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4627 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
4628 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4629 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
4630 }
4631 }
4632
4633 /* stats_sum_queue - Calculate the sum of the same fields in sq or rq.
4634 * @sum: the position to store the sum values
4635 * @num: field num
4636 * @q_value: the first queue fields
4637 * @q_num: number of the queues
4638 */
stats_sum_queue(u64 * sum,u32 num,u64 * q_value,u32 q_num)4639 static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num)
4640 {
4641 u32 step = num;
4642 int i, j;
4643 u64 *p;
4644
4645 for (i = 0; i < num; ++i) {
4646 p = sum + i;
4647 *p = 0;
4648
4649 for (j = 0; j < q_num; ++j)
4650 *p += *(q_value + i + j * step);
4651 }
4652 }
4653
virtnet_fill_total_fields(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx)4654 static void virtnet_fill_total_fields(struct virtnet_info *vi,
4655 struct virtnet_stats_ctx *ctx)
4656 {
4657 u64 *data, *first_rx_q, *first_tx_q;
4658 u32 num_cq, num_rx, num_tx;
4659
4660 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
4661 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
4662 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
4663
4664 first_rx_q = ctx->data + num_rx + num_tx + num_cq;
4665 first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx;
4666
4667 data = ctx->data;
4668
4669 stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs);
4670
4671 data = ctx->data + num_rx;
4672
4673 stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs);
4674 }
4675
virtnet_fill_stats_qstat(struct virtnet_info * vi,u32 qid,struct virtnet_stats_ctx * ctx,const u8 * base,bool drv_stats,u8 reply_type)4676 static void virtnet_fill_stats_qstat(struct virtnet_info *vi, u32 qid,
4677 struct virtnet_stats_ctx *ctx,
4678 const u8 *base, bool drv_stats, u8 reply_type)
4679 {
4680 const struct virtnet_stat_desc *desc;
4681 const u64_stats_t *v_stat;
4682 u64 offset, bitmap;
4683 const __le64 *v;
4684 u32 queue_type;
4685 int i, num;
4686
4687 queue_type = vq_type(vi, qid);
4688 bitmap = ctx->bitmap[queue_type];
4689
4690 if (drv_stats) {
4691 if (queue_type == VIRTNET_Q_TYPE_RX) {
4692 desc = &virtnet_rq_stats_desc_qstat[0];
4693 num = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
4694 } else {
4695 desc = &virtnet_sq_stats_desc_qstat[0];
4696 num = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
4697 }
4698
4699 for (i = 0; i < num; ++i) {
4700 offset = desc[i].qstat_offset / sizeof(*ctx->data);
4701 v_stat = (const u64_stats_t *)(base + desc[i].offset);
4702 ctx->data[offset] = u64_stats_read(v_stat);
4703 }
4704 return;
4705 }
4706
4707 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4708 desc = &virtnet_stats_rx_basic_desc_qstat[0];
4709 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
4710 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
4711 goto found;
4712 }
4713
4714 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4715 desc = &virtnet_stats_rx_csum_desc_qstat[0];
4716 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
4717 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
4718 goto found;
4719 }
4720
4721 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
4722 desc = &virtnet_stats_rx_gso_desc_qstat[0];
4723 num = ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
4724 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO)
4725 goto found;
4726 }
4727
4728 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4729 desc = &virtnet_stats_rx_speed_desc_qstat[0];
4730 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
4731 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
4732 goto found;
4733 }
4734
4735 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4736 desc = &virtnet_stats_tx_basic_desc_qstat[0];
4737 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
4738 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
4739 goto found;
4740 }
4741
4742 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
4743 desc = &virtnet_stats_tx_csum_desc_qstat[0];
4744 num = ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
4745 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM)
4746 goto found;
4747 }
4748
4749 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4750 desc = &virtnet_stats_tx_gso_desc_qstat[0];
4751 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
4752 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
4753 goto found;
4754 }
4755
4756 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4757 desc = &virtnet_stats_tx_speed_desc_qstat[0];
4758 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
4759 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
4760 goto found;
4761 }
4762
4763 return;
4764
4765 found:
4766 for (i = 0; i < num; ++i) {
4767 offset = desc[i].qstat_offset / sizeof(*ctx->data);
4768 v = (const __le64 *)(base + desc[i].offset);
4769 ctx->data[offset] = le64_to_cpu(*v);
4770 }
4771 }
4772
4773 /* virtnet_fill_stats - copy the stats to qstats or ethtool -S
4774 * The stats source is the device or the driver.
4775 *
4776 * @vi: virtio net info
4777 * @qid: the vq id
4778 * @ctx: stats ctx (initiated by virtnet_stats_ctx_init())
4779 * @base: pointer to the device reply or the driver stats structure.
4780 * @drv_stats: designate the base type (device reply, driver stats)
4781 * @type: the type of the device reply (if drv_stats is true, this must be zero)
4782 */
virtnet_fill_stats(struct virtnet_info * vi,u32 qid,struct virtnet_stats_ctx * ctx,const u8 * base,bool drv_stats,u8 reply_type)4783 static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
4784 struct virtnet_stats_ctx *ctx,
4785 const u8 *base, bool drv_stats, u8 reply_type)
4786 {
4787 u32 queue_type, num_rx, num_tx, num_cq;
4788 const struct virtnet_stat_desc *desc;
4789 const u64_stats_t *v_stat;
4790 u64 offset, bitmap;
4791 const __le64 *v;
4792 int i, num;
4793
4794 if (ctx->to_qstat)
4795 return virtnet_fill_stats_qstat(vi, qid, ctx, base, drv_stats, reply_type);
4796
4797 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
4798 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
4799 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
4800
4801 queue_type = vq_type(vi, qid);
4802 bitmap = ctx->bitmap[queue_type];
4803
4804 /* skip the total fields of pairs */
4805 offset = num_rx + num_tx;
4806
4807 if (queue_type == VIRTNET_Q_TYPE_TX) {
4808 offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2);
4809
4810 num = ARRAY_SIZE(virtnet_sq_stats_desc);
4811 if (drv_stats) {
4812 desc = &virtnet_sq_stats_desc[0];
4813 goto drv_stats;
4814 }
4815
4816 offset += num;
4817
4818 } else if (queue_type == VIRTNET_Q_TYPE_RX) {
4819 offset += num_cq + num_rx * (qid / 2);
4820
4821 num = ARRAY_SIZE(virtnet_rq_stats_desc);
4822 if (drv_stats) {
4823 desc = &virtnet_rq_stats_desc[0];
4824 goto drv_stats;
4825 }
4826
4827 offset += num;
4828 }
4829
4830 if (bitmap & VIRTIO_NET_STATS_TYPE_CVQ) {
4831 desc = &virtnet_stats_cvq_desc[0];
4832 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
4833 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_CVQ)
4834 goto found;
4835
4836 offset += num;
4837 }
4838
4839 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4840 desc = &virtnet_stats_rx_basic_desc[0];
4841 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4842 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
4843 goto found;
4844
4845 offset += num;
4846 }
4847
4848 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4849 desc = &virtnet_stats_rx_csum_desc[0];
4850 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4851 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
4852 goto found;
4853
4854 offset += num;
4855 }
4856
4857 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4858 desc = &virtnet_stats_rx_speed_desc[0];
4859 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4860 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
4861 goto found;
4862
4863 offset += num;
4864 }
4865
4866 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4867 desc = &virtnet_stats_tx_basic_desc[0];
4868 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4869 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
4870 goto found;
4871
4872 offset += num;
4873 }
4874
4875 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4876 desc = &virtnet_stats_tx_gso_desc[0];
4877 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4878 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
4879 goto found;
4880
4881 offset += num;
4882 }
4883
4884 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4885 desc = &virtnet_stats_tx_speed_desc[0];
4886 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4887 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
4888 goto found;
4889
4890 offset += num;
4891 }
4892
4893 return;
4894
4895 found:
4896 for (i = 0; i < num; ++i) {
4897 v = (const __le64 *)(base + desc[i].offset);
4898 ctx->data[offset + i] = le64_to_cpu(*v);
4899 }
4900
4901 return;
4902
4903 drv_stats:
4904 for (i = 0; i < num; ++i) {
4905 v_stat = (const u64_stats_t *)(base + desc[i].offset);
4906 ctx->data[offset + i] = u64_stats_read(v_stat);
4907 }
4908 }
4909
__virtnet_get_hw_stats(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,struct virtio_net_ctrl_queue_stats * req,int req_size,void * reply,int res_size)4910 static int __virtnet_get_hw_stats(struct virtnet_info *vi,
4911 struct virtnet_stats_ctx *ctx,
4912 struct virtio_net_ctrl_queue_stats *req,
4913 int req_size, void *reply, int res_size)
4914 {
4915 struct virtio_net_stats_reply_hdr *hdr;
4916 struct scatterlist sgs_in, sgs_out;
4917 void *p;
4918 u32 qid;
4919 int ok;
4920
4921 sg_init_one(&sgs_out, req, req_size);
4922 sg_init_one(&sgs_in, reply, res_size);
4923
4924 ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
4925 VIRTIO_NET_CTRL_STATS_GET,
4926 &sgs_out, &sgs_in);
4927
4928 if (!ok)
4929 return ok;
4930
4931 for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
4932 hdr = p;
4933 qid = le16_to_cpu(hdr->vq_index);
4934 virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
4935 }
4936
4937 return 0;
4938 }
4939
virtnet_make_stat_req(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,struct virtio_net_ctrl_queue_stats * req,int qid,int * idx)4940 static void virtnet_make_stat_req(struct virtnet_info *vi,
4941 struct virtnet_stats_ctx *ctx,
4942 struct virtio_net_ctrl_queue_stats *req,
4943 int qid, int *idx)
4944 {
4945 int qtype = vq_type(vi, qid);
4946 u64 bitmap = ctx->bitmap[qtype];
4947
4948 if (!bitmap)
4949 return;
4950
4951 req->stats[*idx].vq_index = cpu_to_le16(qid);
4952 req->stats[*idx].types_bitmap[0] = cpu_to_le64(bitmap);
4953 *idx += 1;
4954 }
4955
4956 /* qid: -1: get stats of all vq.
4957 * > 0: get the stats for the special vq. This must not be cvq.
4958 */
virtnet_get_hw_stats(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,int qid)4959 static int virtnet_get_hw_stats(struct virtnet_info *vi,
4960 struct virtnet_stats_ctx *ctx, int qid)
4961 {
4962 int qnum, i, j, res_size, qtype, last_vq, first_vq;
4963 struct virtio_net_ctrl_queue_stats *req;
4964 bool enable_cvq;
4965 void *reply;
4966 int ok;
4967
4968 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS))
4969 return 0;
4970
4971 if (qid == -1) {
4972 last_vq = vi->curr_queue_pairs * 2 - 1;
4973 first_vq = 0;
4974 enable_cvq = true;
4975 } else {
4976 last_vq = qid;
4977 first_vq = qid;
4978 enable_cvq = false;
4979 }
4980
4981 qnum = 0;
4982 res_size = 0;
4983 for (i = first_vq; i <= last_vq ; ++i) {
4984 qtype = vq_type(vi, i);
4985 if (ctx->bitmap[qtype]) {
4986 ++qnum;
4987 res_size += ctx->size[qtype];
4988 }
4989 }
4990
4991 if (enable_cvq && ctx->bitmap[VIRTNET_Q_TYPE_CQ]) {
4992 res_size += ctx->size[VIRTNET_Q_TYPE_CQ];
4993 qnum += 1;
4994 }
4995
4996 req = kcalloc(qnum, sizeof(*req), GFP_KERNEL);
4997 if (!req)
4998 return -ENOMEM;
4999
5000 reply = kmalloc(res_size, GFP_KERNEL);
5001 if (!reply) {
5002 kfree(req);
5003 return -ENOMEM;
5004 }
5005
5006 j = 0;
5007 for (i = first_vq; i <= last_vq ; ++i)
5008 virtnet_make_stat_req(vi, ctx, req, i, &j);
5009
5010 if (enable_cvq)
5011 virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j);
5012
5013 ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size);
5014
5015 kfree(req);
5016 kfree(reply);
5017
5018 return ok;
5019 }
5020
virtnet_get_strings(struct net_device * dev,u32 stringset,u8 * data)5021 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
5022 {
5023 struct virtnet_info *vi = netdev_priv(dev);
5024 unsigned int i;
5025 u8 *p = data;
5026
5027 switch (stringset) {
5028 case ETH_SS_STATS:
5029 /* Generate the total field names. */
5030 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p);
5031 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p);
5032
5033 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p);
5034
5035 for (i = 0; i < vi->curr_queue_pairs; ++i)
5036 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, i, &p);
5037
5038 for (i = 0; i < vi->curr_queue_pairs; ++i)
5039 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, i, &p);
5040 break;
5041 }
5042 }
5043
virtnet_get_sset_count(struct net_device * dev,int sset)5044 static int virtnet_get_sset_count(struct net_device *dev, int sset)
5045 {
5046 struct virtnet_info *vi = netdev_priv(dev);
5047 struct virtnet_stats_ctx ctx = {0};
5048 u32 pair_count;
5049
5050 switch (sset) {
5051 case ETH_SS_STATS:
5052 virtnet_stats_ctx_init(vi, &ctx, NULL, false);
5053
5054 pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX];
5055
5056 return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] +
5057 vi->curr_queue_pairs * pair_count;
5058 default:
5059 return -EOPNOTSUPP;
5060 }
5061 }
5062
virtnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)5063 static void virtnet_get_ethtool_stats(struct net_device *dev,
5064 struct ethtool_stats *stats, u64 *data)
5065 {
5066 struct virtnet_info *vi = netdev_priv(dev);
5067 struct virtnet_stats_ctx ctx = {0};
5068 unsigned int start, i;
5069 const u8 *stats_base;
5070
5071 virtnet_stats_ctx_init(vi, &ctx, data, false);
5072 if (virtnet_get_hw_stats(vi, &ctx, -1))
5073 dev_warn(&vi->dev->dev, "Failed to get hw stats.\n");
5074
5075 for (i = 0; i < vi->curr_queue_pairs; i++) {
5076 struct receive_queue *rq = &vi->rq[i];
5077 struct send_queue *sq = &vi->sq[i];
5078
5079 stats_base = (const u8 *)&rq->stats;
5080 do {
5081 start = u64_stats_fetch_begin(&rq->stats.syncp);
5082 virtnet_fill_stats(vi, i * 2, &ctx, stats_base, true, 0);
5083 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
5084
5085 stats_base = (const u8 *)&sq->stats;
5086 do {
5087 start = u64_stats_fetch_begin(&sq->stats.syncp);
5088 virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0);
5089 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
5090 }
5091
5092 virtnet_fill_total_fields(vi, &ctx);
5093 }
5094
virtnet_get_channels(struct net_device * dev,struct ethtool_channels * channels)5095 static void virtnet_get_channels(struct net_device *dev,
5096 struct ethtool_channels *channels)
5097 {
5098 struct virtnet_info *vi = netdev_priv(dev);
5099
5100 channels->combined_count = vi->curr_queue_pairs;
5101 channels->max_combined = vi->max_queue_pairs;
5102 channels->max_other = 0;
5103 channels->rx_count = 0;
5104 channels->tx_count = 0;
5105 channels->other_count = 0;
5106 }
5107
virtnet_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)5108 static int virtnet_set_link_ksettings(struct net_device *dev,
5109 const struct ethtool_link_ksettings *cmd)
5110 {
5111 struct virtnet_info *vi = netdev_priv(dev);
5112
5113 return ethtool_virtdev_set_link_ksettings(dev, cmd,
5114 &vi->speed, &vi->duplex);
5115 }
5116
virtnet_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)5117 static int virtnet_get_link_ksettings(struct net_device *dev,
5118 struct ethtool_link_ksettings *cmd)
5119 {
5120 struct virtnet_info *vi = netdev_priv(dev);
5121
5122 cmd->base.speed = vi->speed;
5123 cmd->base.duplex = vi->duplex;
5124 cmd->base.port = PORT_OTHER;
5125
5126 return 0;
5127 }
5128
virtnet_send_tx_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)5129 static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi,
5130 struct ethtool_coalesce *ec)
5131 {
5132 struct virtio_net_ctrl_coal_tx *coal_tx __free(kfree) = NULL;
5133 struct scatterlist sgs_tx;
5134 int i;
5135
5136 coal_tx = kzalloc(sizeof(*coal_tx), GFP_KERNEL);
5137 if (!coal_tx)
5138 return -ENOMEM;
5139
5140 coal_tx->tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
5141 coal_tx->tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
5142 sg_init_one(&sgs_tx, coal_tx, sizeof(*coal_tx));
5143
5144 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
5145 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
5146 &sgs_tx))
5147 return -EINVAL;
5148
5149 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
5150 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
5151 for (i = 0; i < vi->max_queue_pairs; i++) {
5152 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
5153 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
5154 }
5155
5156 return 0;
5157 }
5158
virtnet_send_rx_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)5159 static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi,
5160 struct ethtool_coalesce *ec)
5161 {
5162 struct virtio_net_ctrl_coal_rx *coal_rx __free(kfree) = NULL;
5163 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
5164 struct scatterlist sgs_rx;
5165 int i;
5166
5167 if (rx_ctrl_dim_on && !virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
5168 return -EOPNOTSUPP;
5169
5170 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs ||
5171 ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets))
5172 return -EINVAL;
5173
5174 if (rx_ctrl_dim_on && !vi->rx_dim_enabled) {
5175 vi->rx_dim_enabled = true;
5176 for (i = 0; i < vi->max_queue_pairs; i++) {
5177 mutex_lock(&vi->rq[i].dim_lock);
5178 vi->rq[i].dim_enabled = true;
5179 mutex_unlock(&vi->rq[i].dim_lock);
5180 }
5181 return 0;
5182 }
5183
5184 coal_rx = kzalloc(sizeof(*coal_rx), GFP_KERNEL);
5185 if (!coal_rx)
5186 return -ENOMEM;
5187
5188 if (!rx_ctrl_dim_on && vi->rx_dim_enabled) {
5189 vi->rx_dim_enabled = false;
5190 for (i = 0; i < vi->max_queue_pairs; i++) {
5191 mutex_lock(&vi->rq[i].dim_lock);
5192 vi->rq[i].dim_enabled = false;
5193 mutex_unlock(&vi->rq[i].dim_lock);
5194 }
5195 }
5196
5197 /* Since the per-queue coalescing params can be set,
5198 * we need apply the global new params even if they
5199 * are not updated.
5200 */
5201 coal_rx->rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
5202 coal_rx->rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
5203 sg_init_one(&sgs_rx, coal_rx, sizeof(*coal_rx));
5204
5205 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
5206 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
5207 &sgs_rx))
5208 return -EINVAL;
5209
5210 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
5211 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
5212 for (i = 0; i < vi->max_queue_pairs; i++) {
5213 mutex_lock(&vi->rq[i].dim_lock);
5214 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
5215 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
5216 mutex_unlock(&vi->rq[i].dim_lock);
5217 }
5218
5219 return 0;
5220 }
5221
virtnet_send_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)5222 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
5223 struct ethtool_coalesce *ec)
5224 {
5225 int err;
5226
5227 err = virtnet_send_tx_notf_coal_cmds(vi, ec);
5228 if (err)
5229 return err;
5230
5231 err = virtnet_send_rx_notf_coal_cmds(vi, ec);
5232 if (err)
5233 return err;
5234
5235 return 0;
5236 }
5237
virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec,u16 queue)5238 static int virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info *vi,
5239 struct ethtool_coalesce *ec,
5240 u16 queue)
5241 {
5242 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
5243 u32 max_usecs, max_packets;
5244 bool cur_rx_dim;
5245 int err;
5246
5247 mutex_lock(&vi->rq[queue].dim_lock);
5248 cur_rx_dim = vi->rq[queue].dim_enabled;
5249 max_usecs = vi->rq[queue].intr_coal.max_usecs;
5250 max_packets = vi->rq[queue].intr_coal.max_packets;
5251
5252 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != max_usecs ||
5253 ec->rx_max_coalesced_frames != max_packets)) {
5254 mutex_unlock(&vi->rq[queue].dim_lock);
5255 return -EINVAL;
5256 }
5257
5258 if (rx_ctrl_dim_on && !cur_rx_dim) {
5259 vi->rq[queue].dim_enabled = true;
5260 mutex_unlock(&vi->rq[queue].dim_lock);
5261 return 0;
5262 }
5263
5264 if (!rx_ctrl_dim_on && cur_rx_dim)
5265 vi->rq[queue].dim_enabled = false;
5266
5267 /* If no params are updated, userspace ethtool will
5268 * reject the modification.
5269 */
5270 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, queue,
5271 ec->rx_coalesce_usecs,
5272 ec->rx_max_coalesced_frames);
5273 mutex_unlock(&vi->rq[queue].dim_lock);
5274 return err;
5275 }
5276
virtnet_send_notf_coal_vq_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec,u16 queue)5277 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
5278 struct ethtool_coalesce *ec,
5279 u16 queue)
5280 {
5281 int err;
5282
5283 err = virtnet_send_rx_notf_coal_vq_cmds(vi, ec, queue);
5284 if (err)
5285 return err;
5286
5287 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, queue,
5288 ec->tx_coalesce_usecs,
5289 ec->tx_max_coalesced_frames);
5290 if (err)
5291 return err;
5292
5293 return 0;
5294 }
5295
virtnet_rx_dim_work(struct work_struct * work)5296 static void virtnet_rx_dim_work(struct work_struct *work)
5297 {
5298 struct dim *dim = container_of(work, struct dim, work);
5299 struct receive_queue *rq = container_of(dim,
5300 struct receive_queue, dim);
5301 struct virtnet_info *vi = rq->vq->vdev->priv;
5302 struct net_device *dev = vi->dev;
5303 struct dim_cq_moder update_moder;
5304 int qnum, err;
5305
5306 qnum = rq - vi->rq;
5307
5308 mutex_lock(&rq->dim_lock);
5309 if (!rq->dim_enabled)
5310 goto out;
5311
5312 update_moder = net_dim_get_rx_irq_moder(dev, dim);
5313 if (update_moder.usec != rq->intr_coal.max_usecs ||
5314 update_moder.pkts != rq->intr_coal.max_packets) {
5315 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum,
5316 update_moder.usec,
5317 update_moder.pkts);
5318 if (err)
5319 pr_debug("%s: Failed to send dim parameters on rxq%d\n",
5320 dev->name, qnum);
5321 }
5322 out:
5323 dim->state = DIM_START_MEASURE;
5324 mutex_unlock(&rq->dim_lock);
5325 }
5326
virtnet_coal_params_supported(struct ethtool_coalesce * ec)5327 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
5328 {
5329 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
5330 * or VIRTIO_NET_F_VQ_NOTF_COAL feature is negotiated.
5331 */
5332 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
5333 return -EOPNOTSUPP;
5334
5335 if (ec->tx_max_coalesced_frames > 1 ||
5336 ec->rx_max_coalesced_frames != 1)
5337 return -EINVAL;
5338
5339 return 0;
5340 }
5341
virtnet_should_update_vq_weight(int dev_flags,int weight,int vq_weight,bool * should_update)5342 static int virtnet_should_update_vq_weight(int dev_flags, int weight,
5343 int vq_weight, bool *should_update)
5344 {
5345 if (weight ^ vq_weight) {
5346 if (dev_flags & IFF_UP)
5347 return -EBUSY;
5348 *should_update = true;
5349 }
5350
5351 return 0;
5352 }
5353
virtnet_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)5354 static int virtnet_set_coalesce(struct net_device *dev,
5355 struct ethtool_coalesce *ec,
5356 struct kernel_ethtool_coalesce *kernel_coal,
5357 struct netlink_ext_ack *extack)
5358 {
5359 struct virtnet_info *vi = netdev_priv(dev);
5360 int ret, queue_number, napi_weight, i;
5361 bool update_napi = false;
5362
5363 /* Can't change NAPI weight if the link is up */
5364 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
5365 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
5366 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
5367 vi->sq[queue_number].napi.weight,
5368 &update_napi);
5369 if (ret)
5370 return ret;
5371
5372 if (update_napi) {
5373 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
5374 * updated for the sake of simplicity, which might not be necessary
5375 */
5376 break;
5377 }
5378 }
5379
5380 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
5381 ret = virtnet_send_notf_coal_cmds(vi, ec);
5382 else
5383 ret = virtnet_coal_params_supported(ec);
5384
5385 if (ret)
5386 return ret;
5387
5388 if (update_napi) {
5389 /* xsk xmit depends on the tx napi. So if xsk is active,
5390 * prevent modifications to tx napi.
5391 */
5392 for (i = queue_number; i < vi->max_queue_pairs; i++) {
5393 if (vi->sq[i].xsk_pool)
5394 return -EBUSY;
5395 }
5396
5397 for (; queue_number < vi->max_queue_pairs; queue_number++)
5398 vi->sq[queue_number].napi.weight = napi_weight;
5399 }
5400
5401 return ret;
5402 }
5403
virtnet_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)5404 static int virtnet_get_coalesce(struct net_device *dev,
5405 struct ethtool_coalesce *ec,
5406 struct kernel_ethtool_coalesce *kernel_coal,
5407 struct netlink_ext_ack *extack)
5408 {
5409 struct virtnet_info *vi = netdev_priv(dev);
5410
5411 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
5412 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
5413 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
5414 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
5415 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
5416 ec->use_adaptive_rx_coalesce = vi->rx_dim_enabled;
5417 } else {
5418 ec->rx_max_coalesced_frames = 1;
5419
5420 if (vi->sq[0].napi.weight)
5421 ec->tx_max_coalesced_frames = 1;
5422 }
5423
5424 return 0;
5425 }
5426
virtnet_set_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)5427 static int virtnet_set_per_queue_coalesce(struct net_device *dev,
5428 u32 queue,
5429 struct ethtool_coalesce *ec)
5430 {
5431 struct virtnet_info *vi = netdev_priv(dev);
5432 int ret, napi_weight;
5433 bool update_napi = false;
5434
5435 if (queue >= vi->max_queue_pairs)
5436 return -EINVAL;
5437
5438 /* Can't change NAPI weight if the link is up */
5439 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
5440 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
5441 vi->sq[queue].napi.weight,
5442 &update_napi);
5443 if (ret)
5444 return ret;
5445
5446 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
5447 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
5448 else
5449 ret = virtnet_coal_params_supported(ec);
5450
5451 if (ret)
5452 return ret;
5453
5454 if (update_napi)
5455 vi->sq[queue].napi.weight = napi_weight;
5456
5457 return 0;
5458 }
5459
virtnet_get_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)5460 static int virtnet_get_per_queue_coalesce(struct net_device *dev,
5461 u32 queue,
5462 struct ethtool_coalesce *ec)
5463 {
5464 struct virtnet_info *vi = netdev_priv(dev);
5465
5466 if (queue >= vi->max_queue_pairs)
5467 return -EINVAL;
5468
5469 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
5470 mutex_lock(&vi->rq[queue].dim_lock);
5471 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
5472 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
5473 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
5474 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
5475 ec->use_adaptive_rx_coalesce = vi->rq[queue].dim_enabled;
5476 mutex_unlock(&vi->rq[queue].dim_lock);
5477 } else {
5478 ec->rx_max_coalesced_frames = 1;
5479
5480 if (vi->sq[queue].napi.weight)
5481 ec->tx_max_coalesced_frames = 1;
5482 }
5483
5484 return 0;
5485 }
5486
virtnet_init_settings(struct net_device * dev)5487 static void virtnet_init_settings(struct net_device *dev)
5488 {
5489 struct virtnet_info *vi = netdev_priv(dev);
5490
5491 vi->speed = SPEED_UNKNOWN;
5492 vi->duplex = DUPLEX_UNKNOWN;
5493 }
5494
virtnet_get_rxfh_key_size(struct net_device * dev)5495 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
5496 {
5497 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
5498 }
5499
virtnet_get_rxfh_indir_size(struct net_device * dev)5500 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
5501 {
5502 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
5503 }
5504
virtnet_get_rxfh(struct net_device * dev,struct ethtool_rxfh_param * rxfh)5505 static int virtnet_get_rxfh(struct net_device *dev,
5506 struct ethtool_rxfh_param *rxfh)
5507 {
5508 struct virtnet_info *vi = netdev_priv(dev);
5509 int i;
5510
5511 if (rxfh->indir) {
5512 for (i = 0; i < vi->rss_indir_table_size; ++i)
5513 rxfh->indir[i] = le16_to_cpu(vi->rss_hdr->indirection_table[i]);
5514 }
5515
5516 if (rxfh->key)
5517 memcpy(rxfh->key, vi->rss_hash_key_data, vi->rss_key_size);
5518
5519 rxfh->hfunc = ETH_RSS_HASH_TOP;
5520
5521 return 0;
5522 }
5523
virtnet_set_rxfh(struct net_device * dev,struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)5524 static int virtnet_set_rxfh(struct net_device *dev,
5525 struct ethtool_rxfh_param *rxfh,
5526 struct netlink_ext_ack *extack)
5527 {
5528 struct virtnet_info *vi = netdev_priv(dev);
5529 bool update = false;
5530 int i;
5531
5532 if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
5533 rxfh->hfunc != ETH_RSS_HASH_TOP)
5534 return -EOPNOTSUPP;
5535
5536 if (rxfh->indir) {
5537 if (!vi->has_rss)
5538 return -EOPNOTSUPP;
5539
5540 for (i = 0; i < vi->rss_indir_table_size; ++i)
5541 vi->rss_hdr->indirection_table[i] = cpu_to_le16(rxfh->indir[i]);
5542 update = true;
5543 }
5544
5545 if (rxfh->key) {
5546 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the
5547 * device provides hash calculation capabilities, that is,
5548 * hash_key is configured.
5549 */
5550 if (!vi->has_rss && !vi->has_rss_hash_report)
5551 return -EOPNOTSUPP;
5552
5553 memcpy(vi->rss_hash_key_data, rxfh->key, vi->rss_key_size);
5554 update = true;
5555 }
5556
5557 if (update)
5558 virtnet_commit_rss_command(vi);
5559
5560 return 0;
5561 }
5562
virtnet_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rule_locs)5563 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
5564 {
5565 struct virtnet_info *vi = netdev_priv(dev);
5566 int rc = 0;
5567
5568 switch (info->cmd) {
5569 case ETHTOOL_GRXRINGS:
5570 info->data = vi->curr_queue_pairs;
5571 break;
5572 case ETHTOOL_GRXFH:
5573 virtnet_get_hashflow(vi, info);
5574 break;
5575 default:
5576 rc = -EOPNOTSUPP;
5577 }
5578
5579 return rc;
5580 }
5581
virtnet_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)5582 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
5583 {
5584 struct virtnet_info *vi = netdev_priv(dev);
5585 int rc = 0;
5586
5587 switch (info->cmd) {
5588 case ETHTOOL_SRXFH:
5589 if (!virtnet_set_hashflow(vi, info))
5590 rc = -EINVAL;
5591
5592 break;
5593 default:
5594 rc = -EOPNOTSUPP;
5595 }
5596
5597 return rc;
5598 }
5599
5600 static const struct ethtool_ops virtnet_ethtool_ops = {
5601 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
5602 ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
5603 .get_drvinfo = virtnet_get_drvinfo,
5604 .get_link = ethtool_op_get_link,
5605 .get_ringparam = virtnet_get_ringparam,
5606 .set_ringparam = virtnet_set_ringparam,
5607 .get_strings = virtnet_get_strings,
5608 .get_sset_count = virtnet_get_sset_count,
5609 .get_ethtool_stats = virtnet_get_ethtool_stats,
5610 .set_channels = virtnet_set_channels,
5611 .get_channels = virtnet_get_channels,
5612 .get_ts_info = ethtool_op_get_ts_info,
5613 .get_link_ksettings = virtnet_get_link_ksettings,
5614 .set_link_ksettings = virtnet_set_link_ksettings,
5615 .set_coalesce = virtnet_set_coalesce,
5616 .get_coalesce = virtnet_get_coalesce,
5617 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
5618 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
5619 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
5620 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
5621 .get_rxfh = virtnet_get_rxfh,
5622 .set_rxfh = virtnet_set_rxfh,
5623 .get_rxnfc = virtnet_get_rxnfc,
5624 .set_rxnfc = virtnet_set_rxnfc,
5625 };
5626
virtnet_get_queue_stats_rx(struct net_device * dev,int i,struct netdev_queue_stats_rx * stats)5627 static void virtnet_get_queue_stats_rx(struct net_device *dev, int i,
5628 struct netdev_queue_stats_rx *stats)
5629 {
5630 struct virtnet_info *vi = netdev_priv(dev);
5631 struct receive_queue *rq = &vi->rq[i];
5632 struct virtnet_stats_ctx ctx = {0};
5633
5634 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
5635
5636 virtnet_get_hw_stats(vi, &ctx, i * 2);
5637 virtnet_fill_stats(vi, i * 2, &ctx, (void *)&rq->stats, true, 0);
5638 }
5639
virtnet_get_queue_stats_tx(struct net_device * dev,int i,struct netdev_queue_stats_tx * stats)5640 static void virtnet_get_queue_stats_tx(struct net_device *dev, int i,
5641 struct netdev_queue_stats_tx *stats)
5642 {
5643 struct virtnet_info *vi = netdev_priv(dev);
5644 struct send_queue *sq = &vi->sq[i];
5645 struct virtnet_stats_ctx ctx = {0};
5646
5647 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
5648
5649 virtnet_get_hw_stats(vi, &ctx, i * 2 + 1);
5650 virtnet_fill_stats(vi, i * 2 + 1, &ctx, (void *)&sq->stats, true, 0);
5651 }
5652
virtnet_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)5653 static void virtnet_get_base_stats(struct net_device *dev,
5654 struct netdev_queue_stats_rx *rx,
5655 struct netdev_queue_stats_tx *tx)
5656 {
5657 struct virtnet_info *vi = netdev_priv(dev);
5658
5659 /* The queue stats of the virtio-net will not be reset. So here we
5660 * return 0.
5661 */
5662 rx->bytes = 0;
5663 rx->packets = 0;
5664
5665 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
5666 rx->hw_drops = 0;
5667 rx->hw_drop_overruns = 0;
5668 }
5669
5670 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
5671 rx->csum_unnecessary = 0;
5672 rx->csum_none = 0;
5673 rx->csum_bad = 0;
5674 }
5675
5676 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
5677 rx->hw_gro_packets = 0;
5678 rx->hw_gro_bytes = 0;
5679 rx->hw_gro_wire_packets = 0;
5680 rx->hw_gro_wire_bytes = 0;
5681 }
5682
5683 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED)
5684 rx->hw_drop_ratelimits = 0;
5685
5686 tx->bytes = 0;
5687 tx->packets = 0;
5688 tx->stop = 0;
5689 tx->wake = 0;
5690
5691 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
5692 tx->hw_drops = 0;
5693 tx->hw_drop_errors = 0;
5694 }
5695
5696 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
5697 tx->csum_none = 0;
5698 tx->needs_csum = 0;
5699 }
5700
5701 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
5702 tx->hw_gso_packets = 0;
5703 tx->hw_gso_bytes = 0;
5704 tx->hw_gso_wire_packets = 0;
5705 tx->hw_gso_wire_bytes = 0;
5706 }
5707
5708 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED)
5709 tx->hw_drop_ratelimits = 0;
5710
5711 netdev_stat_queue_sum(dev,
5712 dev->real_num_rx_queues, vi->max_queue_pairs, rx,
5713 dev->real_num_tx_queues, vi->max_queue_pairs, tx);
5714 }
5715
5716 static const struct netdev_stat_ops virtnet_stat_ops = {
5717 .get_queue_stats_rx = virtnet_get_queue_stats_rx,
5718 .get_queue_stats_tx = virtnet_get_queue_stats_tx,
5719 .get_base_stats = virtnet_get_base_stats,
5720 };
5721
virtnet_freeze_down(struct virtio_device * vdev)5722 static void virtnet_freeze_down(struct virtio_device *vdev)
5723 {
5724 struct virtnet_info *vi = vdev->priv;
5725
5726 /* Make sure no work handler is accessing the device */
5727 flush_work(&vi->config_work);
5728 disable_rx_mode_work(vi);
5729 flush_work(&vi->rx_mode_work);
5730
5731 netif_tx_lock_bh(vi->dev);
5732 netif_device_detach(vi->dev);
5733 netif_tx_unlock_bh(vi->dev);
5734 if (netif_running(vi->dev)) {
5735 rtnl_lock();
5736 virtnet_close(vi->dev);
5737 rtnl_unlock();
5738 }
5739 }
5740
5741 static int init_vqs(struct virtnet_info *vi);
5742
virtnet_restore_up(struct virtio_device * vdev)5743 static int virtnet_restore_up(struct virtio_device *vdev)
5744 {
5745 struct virtnet_info *vi = vdev->priv;
5746 int err;
5747
5748 err = init_vqs(vi);
5749 if (err)
5750 return err;
5751
5752 virtio_device_ready(vdev);
5753
5754 enable_delayed_refill(vi);
5755 enable_rx_mode_work(vi);
5756
5757 if (netif_running(vi->dev)) {
5758 rtnl_lock();
5759 err = virtnet_open(vi->dev);
5760 rtnl_unlock();
5761 if (err)
5762 return err;
5763 }
5764
5765 netif_tx_lock_bh(vi->dev);
5766 netif_device_attach(vi->dev);
5767 netif_tx_unlock_bh(vi->dev);
5768 return err;
5769 }
5770
virtnet_set_guest_offloads(struct virtnet_info * vi,u64 offloads)5771 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
5772 {
5773 __virtio64 *_offloads __free(kfree) = NULL;
5774 struct scatterlist sg;
5775
5776 _offloads = kzalloc(sizeof(*_offloads), GFP_KERNEL);
5777 if (!_offloads)
5778 return -ENOMEM;
5779
5780 *_offloads = cpu_to_virtio64(vi->vdev, offloads);
5781
5782 sg_init_one(&sg, _offloads, sizeof(*_offloads));
5783
5784 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
5785 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
5786 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
5787 return -EINVAL;
5788 }
5789
5790 return 0;
5791 }
5792
virtnet_clear_guest_offloads(struct virtnet_info * vi)5793 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
5794 {
5795 u64 offloads = 0;
5796
5797 if (!vi->guest_offloads)
5798 return 0;
5799
5800 return virtnet_set_guest_offloads(vi, offloads);
5801 }
5802
virtnet_restore_guest_offloads(struct virtnet_info * vi)5803 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
5804 {
5805 u64 offloads = vi->guest_offloads;
5806
5807 if (!vi->guest_offloads)
5808 return 0;
5809
5810 return virtnet_set_guest_offloads(vi, offloads);
5811 }
5812
virtnet_rq_bind_xsk_pool(struct virtnet_info * vi,struct receive_queue * rq,struct xsk_buff_pool * pool)5813 static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queue *rq,
5814 struct xsk_buff_pool *pool)
5815 {
5816 int err, qindex;
5817
5818 qindex = rq - vi->rq;
5819
5820 if (pool) {
5821 err = xdp_rxq_info_reg(&rq->xsk_rxq_info, vi->dev, qindex, rq->napi.napi_id);
5822 if (err < 0)
5823 return err;
5824
5825 err = xdp_rxq_info_reg_mem_model(&rq->xsk_rxq_info,
5826 MEM_TYPE_XSK_BUFF_POOL, NULL);
5827 if (err < 0)
5828 goto unreg;
5829
5830 xsk_pool_set_rxq_info(pool, &rq->xsk_rxq_info);
5831 }
5832
5833 virtnet_rx_pause(vi, rq);
5834
5835 err = virtqueue_reset(rq->vq, virtnet_rq_unmap_free_buf, NULL);
5836 if (err) {
5837 netdev_err(vi->dev, "reset rx fail: rx queue index: %d err: %d\n", qindex, err);
5838
5839 pool = NULL;
5840 }
5841
5842 rq->xsk_pool = pool;
5843
5844 virtnet_rx_resume(vi, rq);
5845
5846 if (pool)
5847 return 0;
5848
5849 unreg:
5850 xdp_rxq_info_unreg(&rq->xsk_rxq_info);
5851 return err;
5852 }
5853
virtnet_sq_bind_xsk_pool(struct virtnet_info * vi,struct send_queue * sq,struct xsk_buff_pool * pool)5854 static int virtnet_sq_bind_xsk_pool(struct virtnet_info *vi,
5855 struct send_queue *sq,
5856 struct xsk_buff_pool *pool)
5857 {
5858 int err, qindex;
5859
5860 qindex = sq - vi->sq;
5861
5862 virtnet_tx_pause(vi, sq);
5863
5864 err = virtqueue_reset(sq->vq, virtnet_sq_free_unused_buf,
5865 virtnet_sq_free_unused_buf_done);
5866 if (err) {
5867 netdev_err(vi->dev, "reset tx fail: tx queue index: %d err: %d\n", qindex, err);
5868 pool = NULL;
5869 }
5870
5871 sq->xsk_pool = pool;
5872
5873 virtnet_tx_resume(vi, sq);
5874
5875 return err;
5876 }
5877
virtnet_xsk_pool_enable(struct net_device * dev,struct xsk_buff_pool * pool,u16 qid)5878 static int virtnet_xsk_pool_enable(struct net_device *dev,
5879 struct xsk_buff_pool *pool,
5880 u16 qid)
5881 {
5882 struct virtnet_info *vi = netdev_priv(dev);
5883 struct receive_queue *rq;
5884 struct device *dma_dev;
5885 struct send_queue *sq;
5886 dma_addr_t hdr_dma;
5887 int err, size;
5888
5889 if (vi->hdr_len > xsk_pool_get_headroom(pool))
5890 return -EINVAL;
5891
5892 /* In big_packets mode, xdp cannot work, so there is no need to
5893 * initialize xsk of rq.
5894 */
5895 if (vi->big_packets && !vi->mergeable_rx_bufs)
5896 return -ENOENT;
5897
5898 if (qid >= vi->curr_queue_pairs)
5899 return -EINVAL;
5900
5901 sq = &vi->sq[qid];
5902 rq = &vi->rq[qid];
5903
5904 /* xsk assumes that tx and rx must have the same dma device. The af-xdp
5905 * may use one buffer to receive from the rx and reuse this buffer to
5906 * send by the tx. So the dma dev of sq and rq must be the same one.
5907 *
5908 * But vq->dma_dev allows every vq has the respective dma dev. So I
5909 * check the dma dev of vq and sq is the same dev.
5910 */
5911 if (virtqueue_dma_dev(rq->vq) != virtqueue_dma_dev(sq->vq))
5912 return -EINVAL;
5913
5914 dma_dev = virtqueue_dma_dev(rq->vq);
5915 if (!dma_dev)
5916 return -EINVAL;
5917
5918 size = virtqueue_get_vring_size(rq->vq);
5919
5920 rq->xsk_buffs = kvcalloc(size, sizeof(*rq->xsk_buffs), GFP_KERNEL);
5921 if (!rq->xsk_buffs)
5922 return -ENOMEM;
5923
5924 hdr_dma = virtqueue_dma_map_single_attrs(sq->vq, &xsk_hdr, vi->hdr_len,
5925 DMA_TO_DEVICE, 0);
5926 if (virtqueue_dma_mapping_error(sq->vq, hdr_dma)) {
5927 err = -ENOMEM;
5928 goto err_free_buffs;
5929 }
5930
5931 err = xsk_pool_dma_map(pool, dma_dev, 0);
5932 if (err)
5933 goto err_xsk_map;
5934
5935 err = virtnet_rq_bind_xsk_pool(vi, rq, pool);
5936 if (err)
5937 goto err_rq;
5938
5939 err = virtnet_sq_bind_xsk_pool(vi, sq, pool);
5940 if (err)
5941 goto err_sq;
5942
5943 /* Now, we do not support tx offload(such as tx csum), so all the tx
5944 * virtnet hdr is zero. So all the tx packets can share a single hdr.
5945 */
5946 sq->xsk_hdr_dma_addr = hdr_dma;
5947
5948 return 0;
5949
5950 err_sq:
5951 virtnet_rq_bind_xsk_pool(vi, rq, NULL);
5952 err_rq:
5953 xsk_pool_dma_unmap(pool, 0);
5954 err_xsk_map:
5955 virtqueue_dma_unmap_single_attrs(rq->vq, hdr_dma, vi->hdr_len,
5956 DMA_TO_DEVICE, 0);
5957 err_free_buffs:
5958 kvfree(rq->xsk_buffs);
5959 return err;
5960 }
5961
virtnet_xsk_pool_disable(struct net_device * dev,u16 qid)5962 static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
5963 {
5964 struct virtnet_info *vi = netdev_priv(dev);
5965 struct xsk_buff_pool *pool;
5966 struct receive_queue *rq;
5967 struct send_queue *sq;
5968 int err;
5969
5970 if (qid >= vi->curr_queue_pairs)
5971 return -EINVAL;
5972
5973 sq = &vi->sq[qid];
5974 rq = &vi->rq[qid];
5975
5976 pool = rq->xsk_pool;
5977
5978 err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
5979 err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL);
5980
5981 xsk_pool_dma_unmap(pool, 0);
5982
5983 virtqueue_dma_unmap_single_attrs(sq->vq, sq->xsk_hdr_dma_addr,
5984 vi->hdr_len, DMA_TO_DEVICE, 0);
5985 kvfree(rq->xsk_buffs);
5986
5987 return err;
5988 }
5989
virtnet_xsk_pool_setup(struct net_device * dev,struct netdev_bpf * xdp)5990 static int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp)
5991 {
5992 if (xdp->xsk.pool)
5993 return virtnet_xsk_pool_enable(dev, xdp->xsk.pool,
5994 xdp->xsk.queue_id);
5995 else
5996 return virtnet_xsk_pool_disable(dev, xdp->xsk.queue_id);
5997 }
5998
virtnet_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)5999 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
6000 struct netlink_ext_ack *extack)
6001 {
6002 unsigned int room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
6003 sizeof(struct skb_shared_info));
6004 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
6005 struct virtnet_info *vi = netdev_priv(dev);
6006 struct bpf_prog *old_prog;
6007 u16 xdp_qp = 0, curr_qp;
6008 int i, err;
6009
6010 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
6011 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6012 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
6013 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
6014 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
6015 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
6016 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
6017 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
6018 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
6019 return -EOPNOTSUPP;
6020 }
6021
6022 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
6023 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
6024 return -EINVAL;
6025 }
6026
6027 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
6028 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
6029 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
6030 return -EINVAL;
6031 }
6032
6033 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
6034 if (prog)
6035 xdp_qp = nr_cpu_ids;
6036
6037 /* XDP requires extra queues for XDP_TX */
6038 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
6039 netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
6040 curr_qp + xdp_qp, vi->max_queue_pairs);
6041 xdp_qp = 0;
6042 }
6043
6044 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
6045 if (!prog && !old_prog)
6046 return 0;
6047
6048 if (prog)
6049 bpf_prog_add(prog, vi->max_queue_pairs - 1);
6050
6051 virtnet_rx_pause_all(vi);
6052
6053 /* Make sure NAPI is not using any XDP TX queues for RX. */
6054 if (netif_running(dev)) {
6055 for (i = 0; i < vi->max_queue_pairs; i++)
6056 virtnet_napi_tx_disable(&vi->sq[i]);
6057 }
6058
6059 if (!prog) {
6060 for (i = 0; i < vi->max_queue_pairs; i++) {
6061 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
6062 if (i == 0)
6063 virtnet_restore_guest_offloads(vi);
6064 }
6065 synchronize_net();
6066 }
6067
6068 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
6069 if (err)
6070 goto err;
6071 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
6072 vi->xdp_queue_pairs = xdp_qp;
6073
6074 if (prog) {
6075 vi->xdp_enabled = true;
6076 for (i = 0; i < vi->max_queue_pairs; i++) {
6077 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
6078 if (i == 0 && !old_prog)
6079 virtnet_clear_guest_offloads(vi);
6080 }
6081 if (!old_prog)
6082 xdp_features_set_redirect_target(dev, true);
6083 } else {
6084 xdp_features_clear_redirect_target(dev);
6085 vi->xdp_enabled = false;
6086 }
6087
6088 virtnet_rx_resume_all(vi);
6089 for (i = 0; i < vi->max_queue_pairs; i++) {
6090 if (old_prog)
6091 bpf_prog_put(old_prog);
6092 if (netif_running(dev))
6093 virtnet_napi_tx_enable(&vi->sq[i]);
6094 }
6095
6096 return 0;
6097
6098 err:
6099 if (!prog) {
6100 virtnet_clear_guest_offloads(vi);
6101 for (i = 0; i < vi->max_queue_pairs; i++)
6102 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
6103 }
6104
6105 virtnet_rx_resume_all(vi);
6106 if (netif_running(dev)) {
6107 for (i = 0; i < vi->max_queue_pairs; i++)
6108 virtnet_napi_tx_enable(&vi->sq[i]);
6109 }
6110 if (prog)
6111 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
6112 return err;
6113 }
6114
virtnet_xdp(struct net_device * dev,struct netdev_bpf * xdp)6115 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
6116 {
6117 switch (xdp->command) {
6118 case XDP_SETUP_PROG:
6119 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
6120 case XDP_SETUP_XSK_POOL:
6121 return virtnet_xsk_pool_setup(dev, xdp);
6122 default:
6123 return -EINVAL;
6124 }
6125 }
6126
virtnet_get_phys_port_name(struct net_device * dev,char * buf,size_t len)6127 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
6128 size_t len)
6129 {
6130 struct virtnet_info *vi = netdev_priv(dev);
6131 int ret;
6132
6133 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
6134 return -EOPNOTSUPP;
6135
6136 ret = snprintf(buf, len, "sby");
6137 if (ret >= len)
6138 return -EOPNOTSUPP;
6139
6140 return 0;
6141 }
6142
virtnet_set_features(struct net_device * dev,netdev_features_t features)6143 static int virtnet_set_features(struct net_device *dev,
6144 netdev_features_t features)
6145 {
6146 struct virtnet_info *vi = netdev_priv(dev);
6147 u64 offloads;
6148 int err;
6149
6150 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
6151 if (vi->xdp_enabled)
6152 return -EBUSY;
6153
6154 if (features & NETIF_F_GRO_HW)
6155 offloads = vi->guest_offloads_capable;
6156 else
6157 offloads = vi->guest_offloads_capable &
6158 ~GUEST_OFFLOAD_GRO_HW_MASK;
6159
6160 err = virtnet_set_guest_offloads(vi, offloads);
6161 if (err)
6162 return err;
6163 vi->guest_offloads = offloads;
6164 }
6165
6166 if ((dev->features ^ features) & NETIF_F_RXHASH) {
6167 if (features & NETIF_F_RXHASH)
6168 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
6169 else
6170 vi->rss_hdr->hash_types = cpu_to_le32(VIRTIO_NET_HASH_REPORT_NONE);
6171
6172 if (!virtnet_commit_rss_command(vi))
6173 return -EINVAL;
6174 }
6175
6176 return 0;
6177 }
6178
virtnet_tx_timeout(struct net_device * dev,unsigned int txqueue)6179 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
6180 {
6181 struct virtnet_info *priv = netdev_priv(dev);
6182 struct send_queue *sq = &priv->sq[txqueue];
6183 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
6184
6185 u64_stats_update_begin(&sq->stats.syncp);
6186 u64_stats_inc(&sq->stats.tx_timeouts);
6187 u64_stats_update_end(&sq->stats.syncp);
6188
6189 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
6190 txqueue, sq->name, sq->vq->index, sq->vq->name,
6191 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
6192 }
6193
virtnet_init_irq_moder(struct virtnet_info * vi)6194 static int virtnet_init_irq_moder(struct virtnet_info *vi)
6195 {
6196 u8 profile_flags = 0, coal_flags = 0;
6197 int ret, i;
6198
6199 profile_flags |= DIM_PROFILE_RX;
6200 coal_flags |= DIM_COALESCE_USEC | DIM_COALESCE_PKTS;
6201 ret = net_dim_init_irq_moder(vi->dev, profile_flags, coal_flags,
6202 DIM_CQ_PERIOD_MODE_START_FROM_EQE,
6203 0, virtnet_rx_dim_work, NULL);
6204
6205 if (ret)
6206 return ret;
6207
6208 for (i = 0; i < vi->max_queue_pairs; i++)
6209 net_dim_setting(vi->dev, &vi->rq[i].dim, false);
6210
6211 return 0;
6212 }
6213
virtnet_free_irq_moder(struct virtnet_info * vi)6214 static void virtnet_free_irq_moder(struct virtnet_info *vi)
6215 {
6216 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
6217 return;
6218
6219 rtnl_lock();
6220 net_dim_free_irq_moder(vi->dev);
6221 rtnl_unlock();
6222 }
6223
6224 static const struct net_device_ops virtnet_netdev = {
6225 .ndo_open = virtnet_open,
6226 .ndo_stop = virtnet_close,
6227 .ndo_start_xmit = start_xmit,
6228 .ndo_validate_addr = eth_validate_addr,
6229 .ndo_set_mac_address = virtnet_set_mac_address,
6230 .ndo_set_rx_mode = virtnet_set_rx_mode,
6231 .ndo_get_stats64 = virtnet_stats,
6232 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
6233 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
6234 .ndo_bpf = virtnet_xdp,
6235 .ndo_xdp_xmit = virtnet_xdp_xmit,
6236 .ndo_xsk_wakeup = virtnet_xsk_wakeup,
6237 .ndo_features_check = passthru_features_check,
6238 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
6239 .ndo_set_features = virtnet_set_features,
6240 .ndo_tx_timeout = virtnet_tx_timeout,
6241 };
6242
virtnet_config_changed_work(struct work_struct * work)6243 static void virtnet_config_changed_work(struct work_struct *work)
6244 {
6245 struct virtnet_info *vi =
6246 container_of(work, struct virtnet_info, config_work);
6247 u16 v;
6248
6249 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
6250 struct virtio_net_config, status, &v) < 0)
6251 return;
6252
6253 if (v & VIRTIO_NET_S_ANNOUNCE) {
6254 netdev_notify_peers(vi->dev);
6255 virtnet_ack_link_announce(vi);
6256 }
6257
6258 /* Ignore unknown (future) status bits */
6259 v &= VIRTIO_NET_S_LINK_UP;
6260
6261 if (vi->status == v)
6262 return;
6263
6264 vi->status = v;
6265
6266 if (vi->status & VIRTIO_NET_S_LINK_UP) {
6267 virtnet_update_settings(vi);
6268 netif_carrier_on(vi->dev);
6269 netif_tx_wake_all_queues(vi->dev);
6270 } else {
6271 netif_carrier_off(vi->dev);
6272 netif_tx_stop_all_queues(vi->dev);
6273 }
6274 }
6275
virtnet_config_changed(struct virtio_device * vdev)6276 static void virtnet_config_changed(struct virtio_device *vdev)
6277 {
6278 struct virtnet_info *vi = vdev->priv;
6279
6280 schedule_work(&vi->config_work);
6281 }
6282
virtnet_free_queues(struct virtnet_info * vi)6283 static void virtnet_free_queues(struct virtnet_info *vi)
6284 {
6285 int i;
6286
6287 for (i = 0; i < vi->max_queue_pairs; i++) {
6288 __netif_napi_del(&vi->rq[i].napi);
6289 __netif_napi_del(&vi->sq[i].napi);
6290 }
6291
6292 /* We called __netif_napi_del(),
6293 * we need to respect an RCU grace period before freeing vi->rq
6294 */
6295 synchronize_net();
6296
6297 kfree(vi->rq);
6298 kfree(vi->sq);
6299 kfree(vi->ctrl);
6300 }
6301
_free_receive_bufs(struct virtnet_info * vi)6302 static void _free_receive_bufs(struct virtnet_info *vi)
6303 {
6304 struct bpf_prog *old_prog;
6305 int i;
6306
6307 for (i = 0; i < vi->max_queue_pairs; i++) {
6308 while (vi->rq[i].pages)
6309 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
6310
6311 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
6312 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
6313 if (old_prog)
6314 bpf_prog_put(old_prog);
6315 }
6316 }
6317
free_receive_bufs(struct virtnet_info * vi)6318 static void free_receive_bufs(struct virtnet_info *vi)
6319 {
6320 rtnl_lock();
6321 _free_receive_bufs(vi);
6322 rtnl_unlock();
6323 }
6324
free_receive_page_frags(struct virtnet_info * vi)6325 static void free_receive_page_frags(struct virtnet_info *vi)
6326 {
6327 int i;
6328 for (i = 0; i < vi->max_queue_pairs; i++)
6329 if (vi->rq[i].alloc_frag.page) {
6330 if (vi->rq[i].last_dma)
6331 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
6332 put_page(vi->rq[i].alloc_frag.page);
6333 }
6334 }
6335
virtnet_sq_free_unused_buf(struct virtqueue * vq,void * buf)6336 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
6337 {
6338 struct virtnet_info *vi = vq->vdev->priv;
6339 struct send_queue *sq;
6340 int i = vq2txq(vq);
6341
6342 sq = &vi->sq[i];
6343
6344 switch (virtnet_xmit_ptr_unpack(&buf)) {
6345 case VIRTNET_XMIT_TYPE_SKB:
6346 case VIRTNET_XMIT_TYPE_SKB_ORPHAN:
6347 dev_kfree_skb(buf);
6348 break;
6349
6350 case VIRTNET_XMIT_TYPE_XDP:
6351 xdp_return_frame(buf);
6352 break;
6353
6354 case VIRTNET_XMIT_TYPE_XSK:
6355 xsk_tx_completed(sq->xsk_pool, 1);
6356 break;
6357 }
6358 }
6359
virtnet_sq_free_unused_buf_done(struct virtqueue * vq)6360 static void virtnet_sq_free_unused_buf_done(struct virtqueue *vq)
6361 {
6362 struct virtnet_info *vi = vq->vdev->priv;
6363 int i = vq2txq(vq);
6364
6365 netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i));
6366 }
6367
free_unused_bufs(struct virtnet_info * vi)6368 static void free_unused_bufs(struct virtnet_info *vi)
6369 {
6370 void *buf;
6371 int i;
6372
6373 for (i = 0; i < vi->max_queue_pairs; i++) {
6374 struct virtqueue *vq = vi->sq[i].vq;
6375 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
6376 virtnet_sq_free_unused_buf(vq, buf);
6377 cond_resched();
6378 }
6379
6380 for (i = 0; i < vi->max_queue_pairs; i++) {
6381 struct virtqueue *vq = vi->rq[i].vq;
6382
6383 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
6384 virtnet_rq_unmap_free_buf(vq, buf);
6385 cond_resched();
6386 }
6387 }
6388
virtnet_del_vqs(struct virtnet_info * vi)6389 static void virtnet_del_vqs(struct virtnet_info *vi)
6390 {
6391 struct virtio_device *vdev = vi->vdev;
6392
6393 virtnet_clean_affinity(vi);
6394
6395 vdev->config->del_vqs(vdev);
6396
6397 virtnet_free_queues(vi);
6398 }
6399
6400 /* How large should a single buffer be so a queue full of these can fit at
6401 * least one full packet?
6402 * Logic below assumes the mergeable buffer header is used.
6403 */
mergeable_min_buf_len(struct virtnet_info * vi,struct virtqueue * vq)6404 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
6405 {
6406 const unsigned int hdr_len = vi->hdr_len;
6407 unsigned int rq_size = virtqueue_get_vring_size(vq);
6408 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
6409 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
6410 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
6411
6412 return max(max(min_buf_len, hdr_len) - hdr_len,
6413 (unsigned int)GOOD_PACKET_LEN);
6414 }
6415
virtnet_find_vqs(struct virtnet_info * vi)6416 static int virtnet_find_vqs(struct virtnet_info *vi)
6417 {
6418 struct virtqueue_info *vqs_info;
6419 struct virtqueue **vqs;
6420 int ret = -ENOMEM;
6421 int total_vqs;
6422 bool *ctx;
6423 u16 i;
6424
6425 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
6426 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
6427 * possible control vq.
6428 */
6429 total_vqs = vi->max_queue_pairs * 2 +
6430 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
6431
6432 /* Allocate space for find_vqs parameters */
6433 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
6434 if (!vqs)
6435 goto err_vq;
6436 vqs_info = kcalloc(total_vqs, sizeof(*vqs_info), GFP_KERNEL);
6437 if (!vqs_info)
6438 goto err_vqs_info;
6439 if (!vi->big_packets || vi->mergeable_rx_bufs) {
6440 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
6441 if (!ctx)
6442 goto err_ctx;
6443 } else {
6444 ctx = NULL;
6445 }
6446
6447 /* Parameters for control virtqueue, if any */
6448 if (vi->has_cvq) {
6449 vqs_info[total_vqs - 1].name = "control";
6450 }
6451
6452 /* Allocate/initialize parameters for send/receive virtqueues */
6453 for (i = 0; i < vi->max_queue_pairs; i++) {
6454 vqs_info[rxq2vq(i)].callback = skb_recv_done;
6455 vqs_info[txq2vq(i)].callback = skb_xmit_done;
6456 sprintf(vi->rq[i].name, "input.%u", i);
6457 sprintf(vi->sq[i].name, "output.%u", i);
6458 vqs_info[rxq2vq(i)].name = vi->rq[i].name;
6459 vqs_info[txq2vq(i)].name = vi->sq[i].name;
6460 if (ctx)
6461 vqs_info[rxq2vq(i)].ctx = true;
6462 }
6463
6464 ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, vqs_info, NULL);
6465 if (ret)
6466 goto err_find;
6467
6468 if (vi->has_cvq) {
6469 vi->cvq = vqs[total_vqs - 1];
6470 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
6471 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6472 }
6473
6474 for (i = 0; i < vi->max_queue_pairs; i++) {
6475 vi->rq[i].vq = vqs[rxq2vq(i)];
6476 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
6477 vi->sq[i].vq = vqs[txq2vq(i)];
6478 }
6479
6480 /* run here: ret == 0. */
6481
6482
6483 err_find:
6484 kfree(ctx);
6485 err_ctx:
6486 kfree(vqs_info);
6487 err_vqs_info:
6488 kfree(vqs);
6489 err_vq:
6490 return ret;
6491 }
6492
virtnet_alloc_queues(struct virtnet_info * vi)6493 static int virtnet_alloc_queues(struct virtnet_info *vi)
6494 {
6495 int i;
6496
6497 if (vi->has_cvq) {
6498 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
6499 if (!vi->ctrl)
6500 goto err_ctrl;
6501 } else {
6502 vi->ctrl = NULL;
6503 }
6504 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
6505 if (!vi->sq)
6506 goto err_sq;
6507 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
6508 if (!vi->rq)
6509 goto err_rq;
6510
6511 INIT_DELAYED_WORK(&vi->refill, refill_work);
6512 for (i = 0; i < vi->max_queue_pairs; i++) {
6513 vi->rq[i].pages = NULL;
6514 netif_napi_add_config(vi->dev, &vi->rq[i].napi, virtnet_poll,
6515 i);
6516 vi->rq[i].napi.weight = napi_weight;
6517 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
6518 virtnet_poll_tx,
6519 napi_tx ? napi_weight : 0);
6520
6521 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
6522 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
6523 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
6524
6525 u64_stats_init(&vi->rq[i].stats.syncp);
6526 u64_stats_init(&vi->sq[i].stats.syncp);
6527 mutex_init(&vi->rq[i].dim_lock);
6528 }
6529
6530 return 0;
6531
6532 err_rq:
6533 kfree(vi->sq);
6534 err_sq:
6535 kfree(vi->ctrl);
6536 err_ctrl:
6537 return -ENOMEM;
6538 }
6539
init_vqs(struct virtnet_info * vi)6540 static int init_vqs(struct virtnet_info *vi)
6541 {
6542 int ret;
6543
6544 /* Allocate send & receive queues */
6545 ret = virtnet_alloc_queues(vi);
6546 if (ret)
6547 goto err;
6548
6549 ret = virtnet_find_vqs(vi);
6550 if (ret)
6551 goto err_free;
6552
6553 cpus_read_lock();
6554 virtnet_set_affinity(vi);
6555 cpus_read_unlock();
6556
6557 return 0;
6558
6559 err_free:
6560 virtnet_free_queues(vi);
6561 err:
6562 return ret;
6563 }
6564
6565 #ifdef CONFIG_SYSFS
mergeable_rx_buffer_size_show(struct netdev_rx_queue * queue,char * buf)6566 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
6567 char *buf)
6568 {
6569 struct virtnet_info *vi = netdev_priv(queue->dev);
6570 unsigned int queue_index = get_netdev_rx_queue_index(queue);
6571 unsigned int headroom = virtnet_get_headroom(vi);
6572 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
6573 struct ewma_pkt_len *avg;
6574
6575 BUG_ON(queue_index >= vi->max_queue_pairs);
6576 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
6577 return sprintf(buf, "%u\n",
6578 get_mergeable_buf_len(&vi->rq[queue_index], avg,
6579 SKB_DATA_ALIGN(headroom + tailroom)));
6580 }
6581
6582 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
6583 __ATTR_RO(mergeable_rx_buffer_size);
6584
6585 static struct attribute *virtio_net_mrg_rx_attrs[] = {
6586 &mergeable_rx_buffer_size_attribute.attr,
6587 NULL
6588 };
6589
6590 static const struct attribute_group virtio_net_mrg_rx_group = {
6591 .name = "virtio_net",
6592 .attrs = virtio_net_mrg_rx_attrs
6593 };
6594 #endif
6595
virtnet_fail_on_feature(struct virtio_device * vdev,unsigned int fbit,const char * fname,const char * dname)6596 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
6597 unsigned int fbit,
6598 const char *fname, const char *dname)
6599 {
6600 if (!virtio_has_feature(vdev, fbit))
6601 return false;
6602
6603 dev_err(&vdev->dev, "device advertises feature %s but not %s",
6604 fname, dname);
6605
6606 return true;
6607 }
6608
6609 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
6610 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
6611
virtnet_validate_features(struct virtio_device * vdev)6612 static bool virtnet_validate_features(struct virtio_device *vdev)
6613 {
6614 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
6615 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
6616 "VIRTIO_NET_F_CTRL_VQ") ||
6617 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
6618 "VIRTIO_NET_F_CTRL_VQ") ||
6619 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
6620 "VIRTIO_NET_F_CTRL_VQ") ||
6621 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
6622 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
6623 "VIRTIO_NET_F_CTRL_VQ") ||
6624 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
6625 "VIRTIO_NET_F_CTRL_VQ") ||
6626 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
6627 "VIRTIO_NET_F_CTRL_VQ") ||
6628 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
6629 "VIRTIO_NET_F_CTRL_VQ") ||
6630 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
6631 "VIRTIO_NET_F_CTRL_VQ"))) {
6632 return false;
6633 }
6634
6635 return true;
6636 }
6637
6638 #define MIN_MTU ETH_MIN_MTU
6639 #define MAX_MTU ETH_MAX_MTU
6640
virtnet_validate(struct virtio_device * vdev)6641 static int virtnet_validate(struct virtio_device *vdev)
6642 {
6643 if (!vdev->config->get) {
6644 dev_err(&vdev->dev, "%s failure: config access disabled\n",
6645 __func__);
6646 return -EINVAL;
6647 }
6648
6649 if (!virtnet_validate_features(vdev))
6650 return -EINVAL;
6651
6652 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
6653 int mtu = virtio_cread16(vdev,
6654 offsetof(struct virtio_net_config,
6655 mtu));
6656 if (mtu < MIN_MTU)
6657 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
6658 }
6659
6660 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
6661 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
6662 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
6663 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
6664 }
6665
6666 return 0;
6667 }
6668
virtnet_check_guest_gso(const struct virtnet_info * vi)6669 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
6670 {
6671 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6672 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
6673 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
6674 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
6675 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
6676 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
6677 }
6678
virtnet_set_big_packets(struct virtnet_info * vi,const int mtu)6679 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
6680 {
6681 bool guest_gso = virtnet_check_guest_gso(vi);
6682
6683 /* If device can receive ANY guest GSO packets, regardless of mtu,
6684 * allocate packets of maximum size, otherwise limit it to only
6685 * mtu size worth only.
6686 */
6687 if (mtu > ETH_DATA_LEN || guest_gso) {
6688 vi->big_packets = true;
6689 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
6690 }
6691 }
6692
6693 #define VIRTIO_NET_HASH_REPORT_MAX_TABLE 10
6694 static enum xdp_rss_hash_type
6695 virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = {
6696 [VIRTIO_NET_HASH_REPORT_NONE] = XDP_RSS_TYPE_NONE,
6697 [VIRTIO_NET_HASH_REPORT_IPv4] = XDP_RSS_TYPE_L3_IPV4,
6698 [VIRTIO_NET_HASH_REPORT_TCPv4] = XDP_RSS_TYPE_L4_IPV4_TCP,
6699 [VIRTIO_NET_HASH_REPORT_UDPv4] = XDP_RSS_TYPE_L4_IPV4_UDP,
6700 [VIRTIO_NET_HASH_REPORT_IPv6] = XDP_RSS_TYPE_L3_IPV6,
6701 [VIRTIO_NET_HASH_REPORT_TCPv6] = XDP_RSS_TYPE_L4_IPV6_TCP,
6702 [VIRTIO_NET_HASH_REPORT_UDPv6] = XDP_RSS_TYPE_L4_IPV6_UDP,
6703 [VIRTIO_NET_HASH_REPORT_IPv6_EX] = XDP_RSS_TYPE_L3_IPV6_EX,
6704 [VIRTIO_NET_HASH_REPORT_TCPv6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX,
6705 [VIRTIO_NET_HASH_REPORT_UDPv6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX
6706 };
6707
virtnet_xdp_rx_hash(const struct xdp_md * _ctx,u32 * hash,enum xdp_rss_hash_type * rss_type)6708 static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
6709 enum xdp_rss_hash_type *rss_type)
6710 {
6711 const struct xdp_buff *xdp = (void *)_ctx;
6712 struct virtio_net_hdr_v1_hash *hdr_hash;
6713 struct virtnet_info *vi;
6714 u16 hash_report;
6715
6716 if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
6717 return -ENODATA;
6718
6719 vi = netdev_priv(xdp->rxq->dev);
6720 hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
6721 hash_report = __le16_to_cpu(hdr_hash->hash_report);
6722
6723 if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE)
6724 hash_report = VIRTIO_NET_HASH_REPORT_NONE;
6725
6726 *rss_type = virtnet_xdp_rss_type[hash_report];
6727 *hash = __le32_to_cpu(hdr_hash->hash_value);
6728 return 0;
6729 }
6730
6731 static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
6732 .xmo_rx_hash = virtnet_xdp_rx_hash,
6733 };
6734
virtnet_probe(struct virtio_device * vdev)6735 static int virtnet_probe(struct virtio_device *vdev)
6736 {
6737 int i, err = -ENOMEM;
6738 struct net_device *dev;
6739 struct virtnet_info *vi;
6740 u16 max_queue_pairs;
6741 int mtu = 0;
6742
6743 /* Find if host supports multiqueue/rss virtio_net device */
6744 max_queue_pairs = 1;
6745 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
6746 max_queue_pairs =
6747 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
6748
6749 /* We need at least 2 queue's */
6750 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
6751 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
6752 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
6753 max_queue_pairs = 1;
6754
6755 /* Allocate ourselves a network device with room for our info */
6756 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
6757 if (!dev)
6758 return -ENOMEM;
6759
6760 /* Set up network device as normal. */
6761 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
6762 IFF_TX_SKB_NO_LINEAR;
6763 dev->netdev_ops = &virtnet_netdev;
6764 dev->stat_ops = &virtnet_stat_ops;
6765 dev->features = NETIF_F_HIGHDMA;
6766
6767 dev->ethtool_ops = &virtnet_ethtool_ops;
6768 SET_NETDEV_DEV(dev, &vdev->dev);
6769
6770 /* Do we support "hardware" checksums? */
6771 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
6772 /* This opens up the world of extra features. */
6773 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
6774 if (csum)
6775 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
6776
6777 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
6778 dev->hw_features |= NETIF_F_TSO
6779 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
6780 }
6781 /* Individual feature bits: what can host handle? */
6782 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
6783 dev->hw_features |= NETIF_F_TSO;
6784 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
6785 dev->hw_features |= NETIF_F_TSO6;
6786 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
6787 dev->hw_features |= NETIF_F_TSO_ECN;
6788 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
6789 dev->hw_features |= NETIF_F_GSO_UDP_L4;
6790
6791 dev->features |= NETIF_F_GSO_ROBUST;
6792
6793 if (gso)
6794 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
6795 /* (!csum && gso) case will be fixed by register_netdev() */
6796 }
6797
6798 /* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't
6799 * need to calculate checksums for partially checksummed packets,
6800 * as they're considered valid by the upper layer.
6801 * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only
6802 * receives fully checksummed packets. The device may assist in
6803 * validating these packets' checksums, so the driver won't have to.
6804 */
6805 dev->features |= NETIF_F_RXCSUM;
6806
6807 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6808 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
6809 dev->features |= NETIF_F_GRO_HW;
6810 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
6811 dev->hw_features |= NETIF_F_GRO_HW;
6812
6813 dev->vlan_features = dev->features;
6814 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
6815 NETDEV_XDP_ACT_XSK_ZEROCOPY;
6816
6817 /* MTU range: 68 - 65535 */
6818 dev->min_mtu = MIN_MTU;
6819 dev->max_mtu = MAX_MTU;
6820
6821 /* Configuration may specify what MAC to use. Otherwise random. */
6822 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
6823 u8 addr[ETH_ALEN];
6824
6825 virtio_cread_bytes(vdev,
6826 offsetof(struct virtio_net_config, mac),
6827 addr, ETH_ALEN);
6828 eth_hw_addr_set(dev, addr);
6829 } else {
6830 eth_hw_addr_random(dev);
6831 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
6832 dev->dev_addr);
6833 }
6834
6835 /* Set up our device-specific information */
6836 vi = netdev_priv(dev);
6837 vi->dev = dev;
6838 vi->vdev = vdev;
6839 vdev->priv = vi;
6840
6841 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
6842 INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work);
6843 spin_lock_init(&vi->refill_lock);
6844
6845 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
6846 vi->mergeable_rx_bufs = true;
6847 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
6848 }
6849
6850 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
6851 vi->has_rss_hash_report = true;
6852
6853 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
6854 vi->has_rss = true;
6855
6856 vi->rss_indir_table_size =
6857 virtio_cread16(vdev, offsetof(struct virtio_net_config,
6858 rss_max_indirection_table_length));
6859 }
6860 vi->rss_hdr = devm_kzalloc(&vdev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
6861 if (!vi->rss_hdr) {
6862 err = -ENOMEM;
6863 goto free;
6864 }
6865
6866 if (vi->has_rss || vi->has_rss_hash_report) {
6867 vi->rss_key_size =
6868 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
6869 if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
6870 dev_err(&vdev->dev, "rss_max_key_size=%u exceeds the limit %u.\n",
6871 vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE);
6872 err = -EINVAL;
6873 goto free;
6874 }
6875
6876 vi->rss_hash_types_supported =
6877 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
6878 vi->rss_hash_types_supported &=
6879 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
6880 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
6881 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
6882
6883 dev->hw_features |= NETIF_F_RXHASH;
6884 dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
6885 }
6886
6887 if (vi->has_rss_hash_report)
6888 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
6889 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
6890 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
6891 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
6892 else
6893 vi->hdr_len = sizeof(struct virtio_net_hdr);
6894
6895 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
6896 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
6897 vi->any_header_sg = true;
6898
6899 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
6900 vi->has_cvq = true;
6901
6902 mutex_init(&vi->cvq_lock);
6903
6904 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
6905 mtu = virtio_cread16(vdev,
6906 offsetof(struct virtio_net_config,
6907 mtu));
6908 if (mtu < dev->min_mtu) {
6909 /* Should never trigger: MTU was previously validated
6910 * in virtnet_validate.
6911 */
6912 dev_err(&vdev->dev,
6913 "device MTU appears to have changed it is now %d < %d",
6914 mtu, dev->min_mtu);
6915 err = -EINVAL;
6916 goto free;
6917 }
6918
6919 dev->mtu = mtu;
6920 dev->max_mtu = mtu;
6921 }
6922
6923 virtnet_set_big_packets(vi, mtu);
6924
6925 if (vi->any_header_sg)
6926 dev->needed_headroom = vi->hdr_len;
6927
6928 /* Enable multiqueue by default */
6929 if (num_online_cpus() >= max_queue_pairs)
6930 vi->curr_queue_pairs = max_queue_pairs;
6931 else
6932 vi->curr_queue_pairs = num_online_cpus();
6933 vi->max_queue_pairs = max_queue_pairs;
6934
6935 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
6936 err = init_vqs(vi);
6937 if (err)
6938 goto free;
6939
6940 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
6941 vi->intr_coal_rx.max_usecs = 0;
6942 vi->intr_coal_tx.max_usecs = 0;
6943 vi->intr_coal_rx.max_packets = 0;
6944
6945 /* Keep the default values of the coalescing parameters
6946 * aligned with the default napi_tx state.
6947 */
6948 if (vi->sq[0].napi.weight)
6949 vi->intr_coal_tx.max_packets = 1;
6950 else
6951 vi->intr_coal_tx.max_packets = 0;
6952 }
6953
6954 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
6955 /* The reason is the same as VIRTIO_NET_F_NOTF_COAL. */
6956 for (i = 0; i < vi->max_queue_pairs; i++)
6957 if (vi->sq[i].napi.weight)
6958 vi->sq[i].intr_coal.max_packets = 1;
6959
6960 err = virtnet_init_irq_moder(vi);
6961 if (err)
6962 goto free;
6963 }
6964
6965 #ifdef CONFIG_SYSFS
6966 if (vi->mergeable_rx_bufs)
6967 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
6968 #endif
6969 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
6970 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
6971
6972 virtnet_init_settings(dev);
6973
6974 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
6975 vi->failover = net_failover_create(vi->dev);
6976 if (IS_ERR(vi->failover)) {
6977 err = PTR_ERR(vi->failover);
6978 goto free_vqs;
6979 }
6980 }
6981
6982 if (vi->has_rss || vi->has_rss_hash_report)
6983 virtnet_init_default_rss(vi);
6984
6985 enable_rx_mode_work(vi);
6986
6987 /* serialize netdev register + virtio_device_ready() with ndo_open() */
6988 rtnl_lock();
6989
6990 err = register_netdevice(dev);
6991 if (err) {
6992 pr_debug("virtio_net: registering device failed\n");
6993 rtnl_unlock();
6994 goto free_failover;
6995 }
6996
6997 /* Disable config change notification until ndo_open. */
6998 virtio_config_driver_disable(vi->vdev);
6999
7000 virtio_device_ready(vdev);
7001
7002 if (vi->has_rss || vi->has_rss_hash_report) {
7003 if (!virtnet_commit_rss_command(vi)) {
7004 dev_warn(&vdev->dev, "RSS disabled because committing failed.\n");
7005 dev->hw_features &= ~NETIF_F_RXHASH;
7006 vi->has_rss_hash_report = false;
7007 vi->has_rss = false;
7008 }
7009 }
7010
7011 virtnet_set_queues(vi, vi->curr_queue_pairs);
7012
7013 /* a random MAC address has been assigned, notify the device.
7014 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
7015 * because many devices work fine without getting MAC explicitly
7016 */
7017 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
7018 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
7019 struct scatterlist sg;
7020
7021 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
7022 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
7023 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
7024 pr_debug("virtio_net: setting MAC address failed\n");
7025 rtnl_unlock();
7026 err = -EINVAL;
7027 goto free_unregister_netdev;
7028 }
7029 }
7030
7031 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) {
7032 struct virtio_net_stats_capabilities *stats_cap __free(kfree) = NULL;
7033 struct scatterlist sg;
7034 __le64 v;
7035
7036 stats_cap = kzalloc(sizeof(*stats_cap), GFP_KERNEL);
7037 if (!stats_cap) {
7038 rtnl_unlock();
7039 err = -ENOMEM;
7040 goto free_unregister_netdev;
7041 }
7042
7043 sg_init_one(&sg, stats_cap, sizeof(*stats_cap));
7044
7045 if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
7046 VIRTIO_NET_CTRL_STATS_QUERY,
7047 NULL, &sg)) {
7048 pr_debug("virtio_net: fail to get stats capability\n");
7049 rtnl_unlock();
7050 err = -EINVAL;
7051 goto free_unregister_netdev;
7052 }
7053
7054 v = stats_cap->supported_stats_types[0];
7055 vi->device_stats_cap = le64_to_cpu(v);
7056 }
7057
7058 /* Assume link up if device can't report link status,
7059 otherwise get link status from config. */
7060 netif_carrier_off(dev);
7061 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
7062 virtnet_config_changed_work(&vi->config_work);
7063 } else {
7064 vi->status = VIRTIO_NET_S_LINK_UP;
7065 virtnet_update_settings(vi);
7066 netif_carrier_on(dev);
7067 }
7068
7069 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
7070 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
7071 set_bit(guest_offloads[i], &vi->guest_offloads);
7072 vi->guest_offloads_capable = vi->guest_offloads;
7073
7074 rtnl_unlock();
7075
7076 err = virtnet_cpu_notif_add(vi);
7077 if (err) {
7078 pr_debug("virtio_net: registering cpu notifier failed\n");
7079 goto free_unregister_netdev;
7080 }
7081
7082 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
7083 dev->name, max_queue_pairs);
7084
7085 return 0;
7086
7087 free_unregister_netdev:
7088 unregister_netdev(dev);
7089 free_failover:
7090 net_failover_destroy(vi->failover);
7091 free_vqs:
7092 virtio_reset_device(vdev);
7093 cancel_delayed_work_sync(&vi->refill);
7094 free_receive_page_frags(vi);
7095 virtnet_del_vqs(vi);
7096 free:
7097 free_netdev(dev);
7098 return err;
7099 }
7100
remove_vq_common(struct virtnet_info * vi)7101 static void remove_vq_common(struct virtnet_info *vi)
7102 {
7103 int i;
7104
7105 virtio_reset_device(vi->vdev);
7106
7107 /* Free unused buffers in both send and recv, if any. */
7108 free_unused_bufs(vi);
7109
7110 /*
7111 * Rule of thumb is netdev_tx_reset_queue() should follow any
7112 * skb freeing not followed by netdev_tx_completed_queue()
7113 */
7114 for (i = 0; i < vi->max_queue_pairs; i++)
7115 netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i));
7116
7117 free_receive_bufs(vi);
7118
7119 free_receive_page_frags(vi);
7120
7121 virtnet_del_vqs(vi);
7122 }
7123
virtnet_remove(struct virtio_device * vdev)7124 static void virtnet_remove(struct virtio_device *vdev)
7125 {
7126 struct virtnet_info *vi = vdev->priv;
7127
7128 virtnet_cpu_notif_remove(vi);
7129
7130 /* Make sure no work handler is accessing the device. */
7131 flush_work(&vi->config_work);
7132 disable_rx_mode_work(vi);
7133 flush_work(&vi->rx_mode_work);
7134
7135 virtnet_free_irq_moder(vi);
7136
7137 unregister_netdev(vi->dev);
7138
7139 net_failover_destroy(vi->failover);
7140
7141 remove_vq_common(vi);
7142
7143 free_netdev(vi->dev);
7144 }
7145
virtnet_freeze(struct virtio_device * vdev)7146 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
7147 {
7148 struct virtnet_info *vi = vdev->priv;
7149
7150 virtnet_cpu_notif_remove(vi);
7151 virtnet_freeze_down(vdev);
7152 remove_vq_common(vi);
7153
7154 return 0;
7155 }
7156
virtnet_restore(struct virtio_device * vdev)7157 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
7158 {
7159 struct virtnet_info *vi = vdev->priv;
7160 int err;
7161
7162 err = virtnet_restore_up(vdev);
7163 if (err)
7164 return err;
7165 virtnet_set_queues(vi, vi->curr_queue_pairs);
7166
7167 err = virtnet_cpu_notif_add(vi);
7168 if (err) {
7169 virtnet_freeze_down(vdev);
7170 remove_vq_common(vi);
7171 return err;
7172 }
7173
7174 return 0;
7175 }
7176
7177 static struct virtio_device_id id_table[] = {
7178 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
7179 { 0 },
7180 };
7181
7182 #define VIRTNET_FEATURES \
7183 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
7184 VIRTIO_NET_F_MAC, \
7185 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
7186 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
7187 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
7188 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
7189 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
7190 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
7191 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
7192 VIRTIO_NET_F_CTRL_MAC_ADDR, \
7193 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
7194 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
7195 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
7196 VIRTIO_NET_F_VQ_NOTF_COAL, \
7197 VIRTIO_NET_F_GUEST_HDRLEN, VIRTIO_NET_F_DEVICE_STATS
7198
7199 static unsigned int features[] = {
7200 VIRTNET_FEATURES,
7201 };
7202
7203 static unsigned int features_legacy[] = {
7204 VIRTNET_FEATURES,
7205 VIRTIO_NET_F_GSO,
7206 VIRTIO_F_ANY_LAYOUT,
7207 };
7208
7209 static struct virtio_driver virtio_net_driver = {
7210 .feature_table = features,
7211 .feature_table_size = ARRAY_SIZE(features),
7212 .feature_table_legacy = features_legacy,
7213 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
7214 .driver.name = KBUILD_MODNAME,
7215 .id_table = id_table,
7216 .validate = virtnet_validate,
7217 .probe = virtnet_probe,
7218 .remove = virtnet_remove,
7219 .config_changed = virtnet_config_changed,
7220 #ifdef CONFIG_PM_SLEEP
7221 .freeze = virtnet_freeze,
7222 .restore = virtnet_restore,
7223 #endif
7224 };
7225
virtio_net_driver_init(void)7226 static __init int virtio_net_driver_init(void)
7227 {
7228 int ret;
7229
7230 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
7231 virtnet_cpu_online,
7232 virtnet_cpu_down_prep);
7233 if (ret < 0)
7234 goto out;
7235 virtionet_online = ret;
7236 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
7237 NULL, virtnet_cpu_dead);
7238 if (ret)
7239 goto err_dead;
7240 ret = register_virtio_driver(&virtio_net_driver);
7241 if (ret)
7242 goto err_virtio;
7243 return 0;
7244 err_virtio:
7245 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
7246 err_dead:
7247 cpuhp_remove_multi_state(virtionet_online);
7248 out:
7249 return ret;
7250 }
7251 module_init(virtio_net_driver_init);
7252
virtio_net_driver_exit(void)7253 static __exit void virtio_net_driver_exit(void)
7254 {
7255 unregister_virtio_driver(&virtio_net_driver);
7256 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
7257 cpuhp_remove_multi_state(virtionet_online);
7258 }
7259 module_exit(virtio_net_driver_exit);
7260
7261 MODULE_DEVICE_TABLE(virtio, id_table);
7262 MODULE_DESCRIPTION("Virtio network driver");
7263 MODULE_LICENSE("GPL");
7264