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