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