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