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