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