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