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