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