blob: d26f695ee64db1c1f11e122d679f0ff8432a0f07 [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
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 Kirsheradf8d3f2013-12-06 06:28:47 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Rusty Russell296f96f2007-10-22 11:03:37 +100017 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080021#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100022#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
25#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080026#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Wanlong Gao8de4b2f2013-01-24 23:51:31 +000028#include <linux/cpu.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100029
Amerigo Wangd34710e2013-05-09 19:50:51 +000030static int napi_weight = NAPI_POLL_WEIGHT;
Dor Laor6c0cd7c2007-12-16 15:19:43 +020031module_param(napi_weight, int, 0444);
32
Rusty Russelleb939922011-12-19 14:08:01 +000033static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050034module_param(csum, bool, 0444);
35module_param(gso, bool, 0444);
36
Rusty Russell296f96f2007-10-22 11:03:37 +100037/* FIXME: MTU in config. */
Michael Dalton5061de32013-11-14 10:41:04 -080038#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39#define MERGE_BUFFER_LEN (ALIGN(GOOD_PACKET_LEN + \
40 sizeof(struct virtio_net_hdr_mrg_rxbuf), \
41 L1_CACHE_BYTES))
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080042#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100043
Rick Jones66846042011-11-14 14:17:08 +000044#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000045
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000046struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000047 struct u64_stats_sync tx_syncp;
48 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000049 u64 tx_bytes;
50 u64 tx_packets;
51
52 u64 rx_bytes;
53 u64 rx_packets;
54};
55
Jason Wange9d74172012-12-07 07:04:55 +000056/* Internal representation of a send virtqueue */
57struct send_queue {
58 /* Virtqueue associated with this send _queue */
59 struct virtqueue *vq;
60
61 /* TX: fragments + linear part + virtio header */
62 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000063
64 /* Name of the send queue: output.$index */
65 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000066};
67
68/* Internal representation of a receive virtqueue */
69struct receive_queue {
70 /* Virtqueue associated with this receive_queue */
71 struct virtqueue *vq;
72
Rusty Russell296f96f2007-10-22 11:03:37 +100073 struct napi_struct napi;
74
75 /* Number of input buffers, and max we've ever had. */
76 unsigned int num, max;
77
Jason Wange9d74172012-12-07 07:04:55 +000078 /* Chain pages by the private ptr. */
79 struct page *pages;
80
81 /* RX: fragments + linear part + virtio header */
82 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000083
84 /* Name of this receive queue: input.$index */
85 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000086};
87
88struct virtnet_info {
89 struct virtio_device *vdev;
90 struct virtqueue *cvq;
91 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +000092 struct send_queue *sq;
93 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +000094 unsigned int status;
95
Jason Wang986a4f42012-12-07 07:04:56 +000096 /* Max # of queue pairs supported by the device */
97 u16 max_queue_pairs;
98
99 /* # of queue pairs currently used by the driver */
100 u16 curr_queue_pairs;
101
Herbert Xu97402b92008-04-18 11:24:27 +0800102 /* I like... big packets and I cannot lie! */
103 bool big_packets;
104
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800105 /* Host will merge rx buffers for big packets (shake it! shake it!) */
106 bool mergeable_rx_bufs;
107
Jason Wang986a4f42012-12-07 07:04:56 +0000108 /* Has control virtqueue */
109 bool has_cvq;
110
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930111 /* Host can handle any s/g split between our header and packet data */
112 bool any_header_sg;
113
Jason Wang586d17c2012-04-11 20:43:52 +0000114 /* enable config space updates */
115 bool config_enable;
116
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000117 /* Active statistics */
118 struct virtnet_stats __percpu *stats;
119
Rusty Russell3161e452009-08-26 12:22:32 -0700120 /* Work struct for refilling if we run low on memory. */
121 struct delayed_work refill;
122
Jason Wang586d17c2012-04-11 20:43:52 +0000123 /* Work struct for config space updates */
124 struct work_struct config_work;
125
126 /* Lock for config space updates */
127 struct mutex config_lock;
Jason Wang986a4f42012-12-07 07:04:56 +0000128
Michael Dalton2613af02013-10-28 15:44:18 -0700129 /* Page_frag for GFP_KERNEL packet buffer allocation when we run
130 * low on memory.
131 */
132 struct page_frag alloc_frag;
133
Jason Wang986a4f42012-12-07 07:04:56 +0000134 /* Does the affinity hint is set for virtqueues? */
135 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000136
Wanlong Gao8de4b2f2013-01-24 23:51:31 +0000137 /* CPU hot plug notifier */
138 struct notifier_block nb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000139};
140
Rusty Russellb3f24692009-09-24 09:59:19 -0600141struct skb_vnet_hdr {
142 union {
143 struct virtio_net_hdr hdr;
144 struct virtio_net_hdr_mrg_rxbuf mhdr;
145 };
146};
147
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000148struct padded_vnet_hdr {
149 struct virtio_net_hdr hdr;
150 /*
151 * virtio_net_hdr should be in a separated sg buffer because of a
152 * QEMU bug, and data sg buffer shares same page with this header sg.
153 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
154 */
155 char padding[6];
156};
157
Jason Wang986a4f42012-12-07 07:04:56 +0000158/* Converting between virtqueue no. and kernel tx/rx queue no.
159 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
160 */
161static int vq2txq(struct virtqueue *vq)
162{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000163 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000164}
165
166static int txq2vq(int txq)
167{
168 return txq * 2 + 1;
169}
170
171static int vq2rxq(struct virtqueue *vq)
172{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000173 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000174}
175
176static int rxq2vq(int rxq)
177{
178 return rxq * 2;
179}
180
Rusty Russellb3f24692009-09-24 09:59:19 -0600181static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000182{
Rusty Russellb3f24692009-09-24 09:59:19 -0600183 return (struct skb_vnet_hdr *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000184}
185
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000186/*
187 * private is used to chain pages for big packets, put the whole
188 * most recent used list in the beginning for reuse
189 */
Jason Wange9d74172012-12-07 07:04:55 +0000190static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500191{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000192 struct page *end;
193
Jason Wange9d74172012-12-07 07:04:55 +0000194 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000195 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000196 end->private = (unsigned long)rq->pages;
197 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500198}
199
Jason Wange9d74172012-12-07 07:04:55 +0000200static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500201{
Jason Wange9d74172012-12-07 07:04:55 +0000202 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500203
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000204 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000205 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000206 /* clear private here, it is used to chain pages */
207 p->private = 0;
208 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500209 p = alloc_page(gfp_mask);
210 return p;
211}
212
Jason Wange9d74172012-12-07 07:04:55 +0000213static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000214{
Jason Wange9d74172012-12-07 07:04:55 +0000215 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000216
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500217 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000218 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000219
Rusty Russell363f1512008-06-08 20:51:55 +1000220 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000221 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000222}
223
Mike Waychison34646452012-01-04 12:52:32 +0000224/* Called from bottom half context */
Jason Wange9d74172012-12-07 07:04:55 +0000225static struct sk_buff *page_to_skb(struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700226 struct page *page, unsigned int offset,
227 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000228{
Jason Wange9d74172012-12-07 07:04:55 +0000229 struct virtnet_info *vi = rq->vq->vdev->priv;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000230 struct sk_buff *skb;
231 struct skb_vnet_hdr *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700232 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000233 char *p;
234
Michael Dalton2613af02013-10-28 15:44:18 -0700235 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000236
237 /* copy small packet so we can reuse these pages for small data */
238 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
239 if (unlikely(!skb))
240 return NULL;
241
242 hdr = skb_vnet_hdr(skb);
243
244 if (vi->mergeable_rx_bufs) {
245 hdr_len = sizeof hdr->mhdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700246 hdr_padded_len = sizeof hdr->mhdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000247 } else {
248 hdr_len = sizeof hdr->hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700249 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000250 }
251
252 memcpy(hdr, p, hdr_len);
253
254 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700255 offset += hdr_padded_len;
256 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000257
258 copy = len;
259 if (copy > skb_tailroom(skb))
260 copy = skb_tailroom(skb);
261 memcpy(skb_put(skb, copy), p, copy);
262
263 len -= copy;
264 offset += copy;
265
Michael Dalton2613af02013-10-28 15:44:18 -0700266 if (vi->mergeable_rx_bufs) {
267 if (len)
268 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
269 else
270 put_page(page);
271 return skb;
272 }
273
Sasha Levine878d782011-09-28 04:40:54 +0000274 /*
275 * Verify that we can indeed put this data into a skb.
276 * This is here to handle cases when the device erroneously
277 * tries to receive more than is possible. This is usually
278 * the case of a broken device.
279 */
280 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000281 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000282 dev_kfree_skb(skb);
283 return NULL;
284 }
Michael Dalton2613af02013-10-28 15:44:18 -0700285 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000286 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700287 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
288 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
289 frag_size, truesize);
290 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000291 page = (struct page *)page->private;
292 offset = 0;
293 }
294
295 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000296 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000297
298 return skb;
299}
300
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200301static struct sk_buff *receive_small(void *buf, unsigned int len)
302{
303 struct sk_buff * skb = buf;
304
305 len -= sizeof(struct virtio_net_hdr);
306 skb_trim(skb, len);
307
308 return skb;
309}
310
311static struct sk_buff *receive_big(struct net_device *dev,
312 struct receive_queue *rq,
313 void *buf,
314 unsigned int len)
315{
316 struct page *page = buf;
317 struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
318
319 if (unlikely(!skb))
320 goto err;
321
322 return skb;
323
324err:
325 dev->stats.rx_dropped++;
326 give_pages(rq, page);
327 return NULL;
328}
329
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200330static struct sk_buff *receive_mergeable(struct net_device *dev,
331 struct receive_queue *rq,
332 void *buf,
333 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000334{
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200335 struct skb_vnet_hdr *hdr = buf;
336 int num_buf = hdr->mhdr.num_buffers;
337 struct page *page = virt_to_head_page(buf);
338 int offset = buf - page_address(page);
339 struct sk_buff *head_skb = page_to_skb(rq, page, offset, len,
340 MERGE_BUFFER_LEN);
Michael Dalton2613af02013-10-28 15:44:18 -0700341 struct sk_buff *curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000342
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200343 if (unlikely(!curr_skb))
344 goto err_skb;
345
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000346 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200347 int num_skb_frags;
348
Michael Dalton2613af02013-10-28 15:44:18 -0700349 buf = virtqueue_get_buf(rq->vq, &len);
350 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200351 pr_debug("%s: rx error: %d buffers out of %d missing\n",
352 dev->name, num_buf, hdr->mhdr.num_buffers);
353 dev->stats.rx_length_errors++;
354 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000355 }
Michael Dalton5061de32013-11-14 10:41:04 -0800356 if (unlikely(len > MERGE_BUFFER_LEN)) {
Michael Dalton2613af02013-10-28 15:44:18 -0700357 pr_debug("%s: rx error: merge buffer too long\n",
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200358 dev->name);
Michael Dalton5061de32013-11-14 10:41:04 -0800359 len = MERGE_BUFFER_LEN;
Michael Dalton2613af02013-10-28 15:44:18 -0700360 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200361
362 page = virt_to_head_page(buf);
363 --rq->num;
364
365 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700366 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
367 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200368
369 if (unlikely(!nskb))
370 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700371 if (curr_skb == head_skb)
372 skb_shinfo(curr_skb)->frag_list = nskb;
373 else
374 curr_skb->next = nskb;
375 curr_skb = nskb;
376 head_skb->truesize += nskb->truesize;
377 num_skb_frags = 0;
378 }
379 if (curr_skb != head_skb) {
380 head_skb->data_len += len;
381 head_skb->len += len;
Michael Dalton5061de32013-11-14 10:41:04 -0800382 head_skb->truesize += MERGE_BUFFER_LEN;
Michael Dalton2613af02013-10-28 15:44:18 -0700383 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200384 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800385 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
386 put_page(page);
387 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Dalton5061de32013-11-14 10:41:04 -0800388 len, MERGE_BUFFER_LEN);
Jason Wangba275242013-11-01 14:07:48 +0800389 } else {
390 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Dalton5061de32013-11-14 10:41:04 -0800391 offset, len, MERGE_BUFFER_LEN);
Jason Wangba275242013-11-01 14:07:48 +0800392 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200393 }
394
395 return head_skb;
396
397err_skb:
398 put_page(page);
399 while (--num_buf) {
400 buf = virtqueue_get_buf(rq->vq, &len);
401 if (unlikely(!buf)) {
402 pr_debug("%s: rx error: %d buffers missing\n",
403 dev->name, num_buf);
404 dev->stats.rx_length_errors++;
405 break;
406 }
407 page = virt_to_head_page(buf);
408 put_page(page);
Jason Wange9d74172012-12-07 07:04:55 +0000409 --rq->num;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000410 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200411err_buf:
412 dev->stats.rx_dropped++;
413 dev_kfree_skb(head_skb);
414 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000415}
416
Jason Wange9d74172012-12-07 07:04:55 +0000417static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000418{
Jason Wange9d74172012-12-07 07:04:55 +0000419 struct virtnet_info *vi = rq->vq->vdev->priv;
420 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000421 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000422 struct sk_buff *skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000423 struct skb_vnet_hdr *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000424
425 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
426 pr_debug("%s: short packet %i\n", dev->name, len);
427 dev->stats.rx_length_errors++;
Michael Dalton98bfd232013-12-05 13:14:05 -0800428 if (vi->mergeable_rx_bufs)
Michael Dalton2613af02013-10-28 15:44:18 -0700429 put_page(virt_to_head_page(buf));
Michael Dalton98bfd232013-12-05 13:14:05 -0800430 else if (vi->big_packets)
431 give_pages(rq, buf);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000432 else
433 dev_kfree_skb(buf);
434 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000435 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000436
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200437 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200438 skb = receive_mergeable(dev, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200439 else if (vi->big_packets)
440 skb = receive_big(dev, rq, buf, len);
441 else
442 skb = receive_small(buf, len);
443
444 if (unlikely(!skb))
445 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800446
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000447 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000448
Eric Dumazet83a27052012-06-05 22:35:24 +0000449 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000450 stats->rx_bytes += skb->len;
451 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000452 u64_stats_update_end(&stats->rx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +1000453
Rusty Russellb3f24692009-09-24 09:59:19 -0600454 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000455 pr_debug("Needs csum!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600456 if (!skb_partial_csum_set(skb,
457 hdr->hdr.csum_start,
458 hdr->hdr.csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000459 goto frame_err;
Jason Wang10a8d942011-06-10 00:56:17 +0000460 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
461 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000462 }
463
Mark McLoughlin23cde762008-06-08 20:49:00 +1000464 skb->protocol = eth_type_trans(skb, dev);
465 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
466 ntohs(skb->protocol), skb->len, skb->pkt_type);
467
Rusty Russellb3f24692009-09-24 09:59:19 -0600468 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000469 pr_debug("GSO!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600470 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000471 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000472 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russell296f96f2007-10-22 11:03:37 +1000473 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000474 case VIRTIO_NET_HDR_GSO_UDP:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000475 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Rusty Russell296f96f2007-10-22 11:03:37 +1000476 break;
477 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000478 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russell296f96f2007-10-22 11:03:37 +1000479 break;
480 default:
Amerigo Wangbe443892012-11-08 17:47:28 +0000481 net_warn_ratelimited("%s: bad gso type %u.\n",
482 dev->name, hdr->hdr.gso_type);
Rusty Russell296f96f2007-10-22 11:03:37 +1000483 goto frame_err;
484 }
485
Rusty Russellb3f24692009-09-24 09:59:19 -0600486 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000487 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russell34a48572008-02-04 23:50:02 -0500488
Rusty Russellb3f24692009-09-24 09:59:19 -0600489 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
Rusty Russell296f96f2007-10-22 11:03:37 +1000490 if (skb_shinfo(skb)->gso_size == 0) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000491 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000492 goto frame_err;
493 }
494
495 /* Header must be checked, and gso_segs computed. */
496 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
497 skb_shinfo(skb)->gso_segs = 0;
498 }
499
500 netif_receive_skb(skb);
501 return;
502
503frame_err:
504 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000505 dev_kfree_skb(skb);
506}
507
Jason Wange9d74172012-12-07 07:04:55 +0000508static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000509{
Jason Wange9d74172012-12-07 07:04:55 +0000510 struct virtnet_info *vi = rq->vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000511 struct sk_buff *skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000512 struct skb_vnet_hdr *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000513 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000514
Michael Dalton5061de32013-11-14 10:41:04 -0800515 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000516 if (unlikely(!skb))
517 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800518
Michael Dalton5061de32013-11-14 10:41:04 -0800519 skb_put(skb, GOOD_PACKET_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000520
521 hdr = skb_vnet_hdr(skb);
Jason Wange9d74172012-12-07 07:04:55 +0000522 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000523
Jason Wange9d74172012-12-07 07:04:55 +0000524 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000525
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030526 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000527 if (err < 0)
528 dev_kfree_skb(skb);
529
530 return err;
531}
532
Jason Wange9d74172012-12-07 07:04:55 +0000533static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000534{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000535 struct page *first, *list = NULL;
536 char *p;
537 int i, err, offset;
538
Jason Wange9d74172012-12-07 07:04:55 +0000539 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000540 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000541 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000542 if (!first) {
543 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000544 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000545 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700546 }
Jason Wange9d74172012-12-07 07:04:55 +0000547 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000548
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000549 /* chain new page in list head to match sg */
550 first->private = (unsigned long)list;
551 list = first;
552 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800553
Jason Wange9d74172012-12-07 07:04:55 +0000554 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000555 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000556 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000557 return -ENOMEM;
558 }
559 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800560
Jason Wange9d74172012-12-07 07:04:55 +0000561 /* rq->sg[0], rq->sg[1] share the same page */
562 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
563 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800564
Jason Wange9d74172012-12-07 07:04:55 +0000565 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000566 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000567 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800568
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000569 /* chain first in list head */
570 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030571 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
572 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000573 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000574 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800575
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000576 return err;
577}
Herbert Xu97402b92008-04-18 11:24:27 +0800578
Jason Wange9d74172012-12-07 07:04:55 +0000579static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000580{
Michael Dalton2613af02013-10-28 15:44:18 -0700581 struct virtnet_info *vi = rq->vq->vdev->priv;
582 char *buf = NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000583 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000584
Michael Dalton2613af02013-10-28 15:44:18 -0700585 if (gfp & __GFP_WAIT) {
Michael Dalton5061de32013-11-14 10:41:04 -0800586 if (skb_page_frag_refill(MERGE_BUFFER_LEN, &vi->alloc_frag,
Michael Dalton2613af02013-10-28 15:44:18 -0700587 gfp)) {
588 buf = (char *)page_address(vi->alloc_frag.page) +
589 vi->alloc_frag.offset;
590 get_page(vi->alloc_frag.page);
Michael Dalton5061de32013-11-14 10:41:04 -0800591 vi->alloc_frag.offset += MERGE_BUFFER_LEN;
Michael Dalton2613af02013-10-28 15:44:18 -0700592 }
593 } else {
Michael Dalton5061de32013-11-14 10:41:04 -0800594 buf = netdev_alloc_frag(MERGE_BUFFER_LEN);
Michael Dalton2613af02013-10-28 15:44:18 -0700595 }
596 if (!buf)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000597 return -ENOMEM;
598
Michael Dalton5061de32013-11-14 10:41:04 -0800599 sg_init_one(rq->sg, buf, MERGE_BUFFER_LEN);
Michael Dalton2613af02013-10-28 15:44:18 -0700600 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000601 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700602 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000603
604 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000605}
606
Rusty Russellb2baed62011-12-29 00:42:38 +0000607/*
608 * Returns false if we couldn't fill entirely (OOM).
609 *
610 * Normally run in the receive path, but can also be run from ndo_open
611 * before we're receiving packets, or from refill_work which is
612 * careful to disable receiving (using napi_disable).
613 */
Jason Wange9d74172012-12-07 07:04:55 +0000614static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800615{
Jason Wange9d74172012-12-07 07:04:55 +0000616 struct virtnet_info *vi = rq->vq->vdev->priv;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800617 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000618 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800619
Amit Shah0aea51c2009-08-26 14:58:28 +0530620 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000621 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000622 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000623 else if (vi->big_packets)
Jason Wange9d74172012-12-07 07:04:55 +0000624 err = add_recvbuf_big(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000625 else
Jason Wange9d74172012-12-07 07:04:55 +0000626 err = add_recvbuf_small(rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800627
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000628 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030629 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800630 break;
Jason Wange9d74172012-12-07 07:04:55 +0000631 ++rq->num;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800632 } while (rq->vq->num_free);
Jason Wange9d74172012-12-07 07:04:55 +0000633 if (unlikely(rq->num > rq->max))
634 rq->max = rq->num;
Heinz Graalfs67975902013-10-29 09:40:02 +1030635 if (unlikely(!virtqueue_kick(rq->vq)))
636 return false;
Rusty Russell3161e452009-08-26 12:22:32 -0700637 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800638}
639
Rusty Russell18445c42008-02-04 23:49:57 -0500640static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000641{
642 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000643 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000644
Rusty Russell18445c42008-02-04 23:49:57 -0500645 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000646 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300647 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000648 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500649 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000650}
651
Jason Wange9d74172012-12-07 07:04:55 +0000652static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800653{
Jason Wange9d74172012-12-07 07:04:55 +0000654 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800655
656 /* If all buffers were filled by other side before we napi_enabled, we
657 * won't get another interrupt, so process any outstanding packets
658 * now. virtnet_poll wants re-enable the queue, so we disable here.
659 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000660 if (napi_schedule_prep(&rq->napi)) {
661 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300662 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000663 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300664 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800665 }
666}
667
Rusty Russell3161e452009-08-26 12:22:32 -0700668static void refill_work(struct work_struct *work)
669{
Jason Wange9d74172012-12-07 07:04:55 +0000670 struct virtnet_info *vi =
671 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700672 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000673 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700674
Sasha Levin55257d72013-04-29 12:00:08 +0930675 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000676 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700677
Jason Wang986a4f42012-12-07 07:04:56 +0000678 napi_disable(&rq->napi);
679 still_empty = !try_fill_recv(rq, GFP_KERNEL);
680 virtnet_napi_enable(rq);
681
682 /* In theory, this can happen: if we don't get any buffers in
683 * we will *never* try to fill again.
684 */
685 if (still_empty)
686 schedule_delayed_work(&vi->refill, HZ/2);
687 }
Rusty Russell3161e452009-08-26 12:22:32 -0700688}
689
Rusty Russell296f96f2007-10-22 11:03:37 +1000690static int virtnet_poll(struct napi_struct *napi, int budget)
691{
Jason Wange9d74172012-12-07 07:04:55 +0000692 struct receive_queue *rq =
693 container_of(napi, struct receive_queue, napi);
694 struct virtnet_info *vi = rq->vq->vdev->priv;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000695 void *buf;
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +0300696 unsigned int r, len, received = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000697
698again:
699 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000700 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
701 receive_buf(rq, buf, len);
702 --rq->num;
Rusty Russell296f96f2007-10-22 11:03:37 +1000703 received++;
704 }
705
Jason Wange9d74172012-12-07 07:04:55 +0000706 if (rq->num < rq->max / 2) {
707 if (!try_fill_recv(rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700708 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700709 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000710
Rusty Russell8329d982007-11-19 11:20:43 -0500711 /* Out of packets? */
712 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +0300713 r = virtqueue_enable_cb_prepare(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800714 napi_complete(napi);
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +0300715 if (unlikely(virtqueue_poll(rq->vq, r)) &&
Joe Perches8e95a202009-12-03 07:58:21 +0000716 napi_schedule_prep(napi)) {
Jason Wange9d74172012-12-07 07:04:55 +0000717 virtqueue_disable_cb(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800718 __napi_schedule(napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000719 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100720 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000721 }
722
723 return received;
724}
725
Jason Wang986a4f42012-12-07 07:04:56 +0000726static int virtnet_open(struct net_device *dev)
727{
728 struct virtnet_info *vi = netdev_priv(dev);
729 int i;
730
Jason Wange4166622013-05-21 20:03:58 +0000731 for (i = 0; i < vi->max_queue_pairs; i++) {
732 if (i < vi->curr_queue_pairs)
733 /* Make sure we have some buffers: if oom use wq. */
734 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
735 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +0000736 virtnet_napi_enable(&vi->rq[i]);
737 }
738
739 return 0;
740}
741
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800742static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000743{
744 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +1030745 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +0000746 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +0000747 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +1000748
Jason Wange9d74172012-12-07 07:04:55 +0000749 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000750 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000751
Eric Dumazet83a27052012-06-05 22:35:24 +0000752 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000753 stats->tx_bytes += skb->len;
754 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000755 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000756
Eric Dumazeted79bab2009-10-14 14:36:43 +0000757 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000758 }
759}
760
Jason Wange9d74172012-12-07 07:04:55 +0000761static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000762{
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930763 struct skb_vnet_hdr *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000764 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +0000765 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +1030766 unsigned num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930767 unsigned hdr_len;
768 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +1000769
Johannes Berge1749612008-10-27 15:59:26 -0700770 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930771 if (vi->mergeable_rx_bufs)
772 hdr_len = sizeof hdr->mhdr;
773 else
774 hdr_len = sizeof hdr->hdr;
775
776 can_push = vi->any_header_sg &&
777 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
778 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
779 /* Even if we can, don't push here yet as this would skew
780 * csum_start offset below. */
781 if (can_push)
782 hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
783 else
784 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000785
Rusty Russell296f96f2007-10-22 11:03:37 +1000786 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600787 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +0000788 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
Rusty Russellb3f24692009-09-24 09:59:19 -0600789 hdr->hdr.csum_offset = skb->csum_offset;
Rusty Russell296f96f2007-10-22 11:03:37 +1000790 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600791 hdr->hdr.flags = 0;
792 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000793 }
794
795 if (skb_is_gso(skb)) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600796 hdr->hdr.hdr_len = skb_headlen(skb);
797 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500798 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russellb3f24692009-09-24 09:59:19 -0600799 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
Rusty Russell296f96f2007-10-22 11:03:37 +1000800 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Rusty Russellb3f24692009-09-24 09:59:19 -0600801 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Rusty Russell296f96f2007-10-22 11:03:37 +1000802 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
Rusty Russellb3f24692009-09-24 09:59:19 -0600803 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Rusty Russell296f96f2007-10-22 11:03:37 +1000804 else
805 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500806 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
Rusty Russellb3f24692009-09-24 09:59:19 -0600807 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000808 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600809 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
810 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000811 }
812
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800813 if (vi->mergeable_rx_bufs)
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930814 hdr->mhdr.num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800815
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930816 if (can_push) {
817 __skb_push(skb, hdr_len);
818 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
819 /* Pull header back to avoid skew in tx bytes calculations. */
820 __skb_pull(skb, hdr_len);
821 } else {
822 sg_set_buf(sq->sg, hdr, hdr_len);
823 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
824 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030825 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +1000826}
827
Stephen Hemminger424efe92009-08-31 19:50:51 +0000828static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -0500829{
830 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +0000831 int qnum = skb_get_queue_mapping(skb);
832 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030833 int err;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500834
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500835 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +0000836 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500837
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700838 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800839 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -0600840
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030841 /* This should not happen! */
Heinz Graalfs67975902013-10-29 09:40:02 +1030842 if (unlikely(err) || unlikely(!virtqueue_kick(sq->vq))) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030843 dev->stats.tx_fifo_errors++;
844 if (net_ratelimit())
845 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800846 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba972010-07-02 16:34:01 +0000847 dev->stats.tx_dropped++;
848 kfree_skb(skb);
849 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -0500850 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700851
Rusty Russell48925e32009-09-24 09:59:20 -0600852 /* Don't wait up for transmitted skbs to be freed. */
853 skb_orphan(skb);
854 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -0500855
Rusty Russell48925e32009-09-24 09:59:20 -0600856 /* Apparently nice girls don't return TX_BUSY; stop the queue
857 * before it gets out of hand. Naturally, this wastes entries. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800858 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +0000859 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +0000860 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -0600861 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800862 free_old_xmit_skbs(sq);
863 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +0000864 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +0000865 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -0600866 }
867 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500868 }
Rusty Russell48925e32009-09-24 09:59:20 -0600869
870 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +1000871}
872
Amos Kong40cbfc32013-01-21 01:17:21 +0000873/*
874 * Send command via the control virtqueue and check status. Commands
875 * supported by the hypervisor, as indicated by feature bits, should
876 * never fail unless improperly formated.
877 */
878static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -0800879 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +0000880{
Rusty Russellf7bc9592013-03-20 15:44:28 +1030881 struct scatterlist *sgs[4], hdr, stat;
Amos Kong40cbfc32013-01-21 01:17:21 +0000882 struct virtio_net_ctrl_hdr ctrl;
883 virtio_net_ctrl_ack status = ~0;
stephen hemmingerd24bae32013-12-09 16:17:40 -0800884 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +0000885
886 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +1030887 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +0000888
889 ctrl.class = class;
890 ctrl.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +1030891 /* Add header */
892 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
893 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +0000894
Rusty Russellf7bc9592013-03-20 15:44:28 +1030895 if (out)
896 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +0000897
Rusty Russellf7bc9592013-03-20 15:44:28 +1030898 /* Add return status. */
899 sg_init_one(&stat, &status, sizeof(status));
stephen hemmingerd24bae32013-12-09 16:17:40 -0800900 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +0000901
stephen hemmingerd24bae32013-12-09 16:17:40 -0800902 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
903 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC) < 0);
Amos Kong40cbfc32013-01-21 01:17:21 +0000904
Heinz Graalfs67975902013-10-29 09:40:02 +1030905 if (unlikely(!virtqueue_kick(vi->cvq)))
906 return status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +0000907
908 /* Spin for a response, the kick causes an ioport write, trapping
909 * into the hypervisor, so the request should be handled immediately.
910 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +1030911 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
912 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +0000913 cpu_relax();
914
915 return status == VIRTIO_NET_OK;
916}
917
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800918static int virtnet_set_mac_address(struct net_device *dev, void *p)
919{
920 struct virtnet_info *vi = netdev_priv(dev);
921 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000922 int ret;
Amos Kong7e58d5a2013-01-21 01:17:23 +0000923 struct sockaddr *addr = p;
924 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800925
Amos Kong7e58d5a2013-01-21 01:17:23 +0000926 ret = eth_prepare_mac_addr_change(dev, p);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000927 if (ret)
928 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800929
Amos Kong7e58d5a2013-01-21 01:17:23 +0000930 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
931 sg_init_one(&sg, addr->sa_data, dev->addr_len);
932 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -0800933 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +0000934 dev_warn(&vdev->dev,
935 "Failed to set mac address by vq command.\n");
936 return -EINVAL;
937 }
938 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russell855e0c52013-10-14 18:11:51 +1030939 unsigned int i;
940
941 /* Naturally, this has an atomicity problem. */
942 for (i = 0; i < dev->addr_len; i++)
943 virtio_cwrite8(vdev,
944 offsetof(struct virtio_net_config, mac) +
945 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +0000946 }
947
948 eth_commit_mac_addr_change(dev, p);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800949
950 return 0;
951}
952
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000953static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
954 struct rtnl_link_stats64 *tot)
955{
956 struct virtnet_info *vi = netdev_priv(dev);
957 int cpu;
958 unsigned int start;
959
960 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +0000961 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000962 u64 tpackets, tbytes, rpackets, rbytes;
963
964 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000965 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000966 tpackets = stats->tx_packets;
967 tbytes = stats->tx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000968 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +0000969
970 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000971 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000972 rpackets = stats->rx_packets;
973 rbytes = stats->rx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000974 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000975
976 tot->rx_packets += rpackets;
977 tot->tx_packets += tpackets;
978 tot->rx_bytes += rbytes;
979 tot->tx_bytes += tbytes;
980 }
981
982 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +0000983 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000984 tot->rx_dropped = dev->stats.rx_dropped;
985 tot->rx_length_errors = dev->stats.rx_length_errors;
986 tot->rx_frame_errors = dev->stats.rx_frame_errors;
987
988 return tot;
989}
990
Amit Shahda74e892008-02-29 16:24:50 +0530991#ifdef CONFIG_NET_POLL_CONTROLLER
992static void virtnet_netpoll(struct net_device *dev)
993{
994 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +0000995 int i;
Amit Shahda74e892008-02-29 16:24:50 +0530996
Jason Wang986a4f42012-12-07 07:04:56 +0000997 for (i = 0; i < vi->curr_queue_pairs; i++)
998 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +0530999}
1000#endif
1001
Jason Wang586d17c2012-04-11 20:43:52 +00001002static void virtnet_ack_link_announce(struct virtnet_info *vi)
1003{
1004 rtnl_lock();
1005 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001006 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001007 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1008 rtnl_unlock();
1009}
1010
Jason Wang986a4f42012-12-07 07:04:56 +00001011static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1012{
1013 struct scatterlist sg;
1014 struct virtio_net_ctrl_mq s;
1015 struct net_device *dev = vi->dev;
1016
1017 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1018 return 0;
1019
1020 s.virtqueue_pairs = queue_pairs;
1021 sg_init_one(&sg, &s, sizeof(s));
1022
1023 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001024 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001025 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1026 queue_pairs);
1027 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301028 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001029 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001030 /* virtnet_open() will refill when device is going to up. */
1031 if (dev->flags & IFF_UP)
1032 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301033 }
Jason Wang986a4f42012-12-07 07:04:56 +00001034
1035 return 0;
1036}
1037
Rusty Russell296f96f2007-10-22 11:03:37 +10001038static int virtnet_close(struct net_device *dev)
1039{
1040 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001041 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001042
Rusty Russellb2baed62011-12-29 00:42:38 +00001043 /* Make sure refill_work doesn't re-enable napi! */
1044 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001045
1046 for (i = 0; i < vi->max_queue_pairs; i++)
1047 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001048
Rusty Russell296f96f2007-10-22 11:03:37 +10001049 return 0;
1050}
1051
Alex Williamson2af76982009-02-04 09:02:40 +00001052static void virtnet_set_rx_mode(struct net_device *dev)
1053{
1054 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001055 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +00001056 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001057 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001058 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001059 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001060 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001061 void *buf;
1062 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001063
1064 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
1065 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1066 return;
1067
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001068 promisc = ((dev->flags & IFF_PROMISC) != 0);
1069 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001070
Alex Williamson23e258e2009-05-01 17:27:56 +00001071 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001072
1073 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001074 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001075 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1076 promisc ? "en" : "dis");
1077
Alex Williamson23e258e2009-05-01 17:27:56 +00001078 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001079
1080 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001081 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001082 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1083 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001084
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001085 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001086 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001087 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001088 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1089 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1090 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001091 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001092 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001093
Alex Williamson23e258e2009-05-01 17:27:56 +00001094 sg_init_table(sg, 2);
1095
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001096 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001097 mac_data->entries = uc_count;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001098 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001099 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001100 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001101
1102 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001103 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001104
1105 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001106 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001107
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001108 mac_data->entries = mc_count;
Jiri Pirko567ec872010-02-23 23:17:07 +00001109 i = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001110 netdev_for_each_mc_addr(ha, dev)
1111 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001112
1113 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001114 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001115
1116 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001117 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001118 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001119
1120 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001121}
1122
Patrick McHardy80d5c362013-04-19 02:04:28 +00001123static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1124 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001125{
1126 struct virtnet_info *vi = netdev_priv(dev);
1127 struct scatterlist sg;
1128
Alex Williamson23e258e2009-05-01 17:27:56 +00001129 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001130
1131 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001132 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001133 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001134 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001135}
1136
Patrick McHardy80d5c362013-04-19 02:04:28 +00001137static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1138 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001139{
1140 struct virtnet_info *vi = netdev_priv(dev);
1141 struct scatterlist sg;
1142
Alex Williamson23e258e2009-05-01 17:27:56 +00001143 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001144
1145 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001146 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001147 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001148 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001149}
1150
Wanlong Gao8898c212013-01-24 23:51:30 +00001151static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001152{
1153 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001154
1155 if (vi->affinity_hint_set) {
1156 for (i = 0; i < vi->max_queue_pairs; i++) {
1157 virtqueue_set_affinity(vi->rq[i].vq, -1);
1158 virtqueue_set_affinity(vi->sq[i].vq, -1);
1159 }
1160
1161 vi->affinity_hint_set = false;
1162 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001163}
1164
1165static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001166{
1167 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001168 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001169
1170 /* In multiqueue mode, when the number of cpu is equal to the number of
1171 * queue pairs, we let the queue pairs to be private to one cpu by
1172 * setting the affinity hint to eliminate the contention.
1173 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001174 if (vi->curr_queue_pairs == 1 ||
1175 vi->max_queue_pairs != num_online_cpus()) {
1176 virtnet_clean_affinity(vi, -1);
1177 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001178 }
1179
Wanlong Gao8898c212013-01-24 23:51:30 +00001180 i = 0;
1181 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001182 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1183 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001184 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001185 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001186 }
1187
Wanlong Gao8898c212013-01-24 23:51:30 +00001188 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001189}
1190
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001191static int virtnet_cpu_callback(struct notifier_block *nfb,
1192 unsigned long action, void *hcpu)
1193{
1194 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1195
1196 switch(action & ~CPU_TASKS_FROZEN) {
1197 case CPU_ONLINE:
1198 case CPU_DOWN_FAILED:
1199 case CPU_DEAD:
1200 virtnet_set_affinity(vi);
1201 break;
1202 case CPU_DOWN_PREPARE:
1203 virtnet_clean_affinity(vi, (long)hcpu);
1204 break;
1205 default:
1206 break;
1207 }
Jason Wang3ab098d2013-10-15 11:18:58 +08001208
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001209 return NOTIFY_OK;
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001210}
1211
Rick Jones8f9f4662011-10-19 08:10:59 +00001212static void virtnet_get_ringparam(struct net_device *dev,
1213 struct ethtool_ringparam *ring)
1214{
1215 struct virtnet_info *vi = netdev_priv(dev);
1216
Jason Wang986a4f42012-12-07 07:04:56 +00001217 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1218 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001219 ring->rx_pending = ring->rx_max_pending;
1220 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001221}
1222
Rick Jones66846042011-11-14 14:17:08 +00001223
1224static void virtnet_get_drvinfo(struct net_device *dev,
1225 struct ethtool_drvinfo *info)
1226{
1227 struct virtnet_info *vi = netdev_priv(dev);
1228 struct virtio_device *vdev = vi->vdev;
1229
1230 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1231 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1232 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1233
1234}
1235
Jason Wangd73bcd22012-12-07 07:04:57 +00001236/* TODO: Eliminate OOO packets during switching */
1237static int virtnet_set_channels(struct net_device *dev,
1238 struct ethtool_channels *channels)
1239{
1240 struct virtnet_info *vi = netdev_priv(dev);
1241 u16 queue_pairs = channels->combined_count;
1242 int err;
1243
1244 /* We don't support separate rx/tx channels.
1245 * We don't allow setting 'other' channels.
1246 */
1247 if (channels->rx_count || channels->tx_count || channels->other_count)
1248 return -EINVAL;
1249
1250 if (queue_pairs > vi->max_queue_pairs)
1251 return -EINVAL;
1252
Wanlong Gao47be2472013-01-24 23:51:29 +00001253 get_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001254 err = virtnet_set_queues(vi, queue_pairs);
1255 if (!err) {
1256 netif_set_real_num_tx_queues(dev, queue_pairs);
1257 netif_set_real_num_rx_queues(dev, queue_pairs);
1258
Wanlong Gao8898c212013-01-24 23:51:30 +00001259 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001260 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001261 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001262
1263 return err;
1264}
1265
1266static void virtnet_get_channels(struct net_device *dev,
1267 struct ethtool_channels *channels)
1268{
1269 struct virtnet_info *vi = netdev_priv(dev);
1270
1271 channels->combined_count = vi->curr_queue_pairs;
1272 channels->max_combined = vi->max_queue_pairs;
1273 channels->max_other = 0;
1274 channels->rx_count = 0;
1275 channels->tx_count = 0;
1276 channels->other_count = 0;
1277}
1278
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001279static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001280 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001281 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001282 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001283 .set_channels = virtnet_set_channels,
1284 .get_channels = virtnet_get_channels,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001285};
1286
Mark McLoughlin39da5812008-11-26 13:58:11 +00001287#define MIN_MTU 68
1288#define MAX_MTU 65535
1289
1290static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1291{
1292 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1293 return -EINVAL;
1294 dev->mtu = new_mtu;
1295 return 0;
1296}
1297
Stephen Hemminger76288b42009-01-06 10:44:22 -08001298static const struct net_device_ops virtnet_netdev = {
1299 .ndo_open = virtnet_open,
1300 .ndo_stop = virtnet_close,
1301 .ndo_start_xmit = start_xmit,
1302 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001303 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001304 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001305 .ndo_change_mtu = virtnet_change_mtu,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001306 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001307 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1308 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001309#ifdef CONFIG_NET_POLL_CONTROLLER
1310 .ndo_poll_controller = virtnet_netpoll,
1311#endif
1312};
1313
Jason Wang586d17c2012-04-11 20:43:52 +00001314static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001315{
Jason Wang586d17c2012-04-11 20:43:52 +00001316 struct virtnet_info *vi =
1317 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001318 u16 v;
1319
Jason Wang586d17c2012-04-11 20:43:52 +00001320 mutex_lock(&vi->config_lock);
1321 if (!vi->config_enable)
1322 goto done;
1323
Rusty Russell855e0c52013-10-14 18:11:51 +10301324 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1325 struct virtio_net_config, status, &v) < 0)
Jason Wang586d17c2012-04-11 20:43:52 +00001326 goto done;
1327
1328 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001329 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001330 virtnet_ack_link_announce(vi);
1331 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001332
1333 /* Ignore unknown (future) status bits */
1334 v &= VIRTIO_NET_S_LINK_UP;
1335
1336 if (vi->status == v)
Jason Wang586d17c2012-04-11 20:43:52 +00001337 goto done;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001338
1339 vi->status = v;
1340
1341 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1342 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001343 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001344 } else {
1345 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001346 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001347 }
Jason Wang586d17c2012-04-11 20:43:52 +00001348done:
1349 mutex_unlock(&vi->config_lock);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001350}
1351
1352static void virtnet_config_changed(struct virtio_device *vdev)
1353{
1354 struct virtnet_info *vi = vdev->priv;
1355
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001356 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001357}
1358
Jason Wang986a4f42012-12-07 07:04:56 +00001359static void virtnet_free_queues(struct virtnet_info *vi)
1360{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001361 int i;
1362
1363 for (i = 0; i < vi->max_queue_pairs; i++)
1364 netif_napi_del(&vi->rq[i].napi);
1365
Jason Wang986a4f42012-12-07 07:04:56 +00001366 kfree(vi->rq);
1367 kfree(vi->sq);
1368}
1369
1370static void free_receive_bufs(struct virtnet_info *vi)
1371{
1372 int i;
1373
1374 for (i = 0; i < vi->max_queue_pairs; i++) {
1375 while (vi->rq[i].pages)
1376 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1377 }
1378}
1379
1380static void free_unused_bufs(struct virtnet_info *vi)
1381{
1382 void *buf;
1383 int i;
1384
1385 for (i = 0; i < vi->max_queue_pairs; i++) {
1386 struct virtqueue *vq = vi->sq[i].vq;
1387 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1388 dev_kfree_skb(buf);
1389 }
1390
1391 for (i = 0; i < vi->max_queue_pairs; i++) {
1392 struct virtqueue *vq = vi->rq[i].vq;
1393
1394 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001395 if (vi->mergeable_rx_bufs)
Michael Dalton2613af02013-10-28 15:44:18 -07001396 put_page(virt_to_head_page(buf));
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001397 else if (vi->big_packets)
1398 give_pages(&vi->rq[i], buf);
Jason Wang986a4f42012-12-07 07:04:56 +00001399 else
1400 dev_kfree_skb(buf);
1401 --vi->rq[i].num;
1402 }
1403 BUG_ON(vi->rq[i].num != 0);
1404 }
1405}
1406
Jason Wange9d74172012-12-07 07:04:55 +00001407static void virtnet_del_vqs(struct virtnet_info *vi)
1408{
1409 struct virtio_device *vdev = vi->vdev;
1410
Wanlong Gao8898c212013-01-24 23:51:30 +00001411 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001412
Jason Wange9d74172012-12-07 07:04:55 +00001413 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001414
1415 virtnet_free_queues(vi);
1416}
1417
1418static int virtnet_find_vqs(struct virtnet_info *vi)
1419{
1420 vq_callback_t **callbacks;
1421 struct virtqueue **vqs;
1422 int ret = -ENOMEM;
1423 int i, total_vqs;
1424 const char **names;
1425
1426 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1427 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1428 * possible control vq.
1429 */
1430 total_vqs = vi->max_queue_pairs * 2 +
1431 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1432
1433 /* Allocate space for find_vqs parameters */
1434 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1435 if (!vqs)
1436 goto err_vq;
1437 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1438 if (!callbacks)
1439 goto err_callback;
1440 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1441 if (!names)
1442 goto err_names;
1443
1444 /* Parameters for control virtqueue, if any */
1445 if (vi->has_cvq) {
1446 callbacks[total_vqs - 1] = NULL;
1447 names[total_vqs - 1] = "control";
1448 }
1449
1450 /* Allocate/initialize parameters for send/receive virtqueues */
1451 for (i = 0; i < vi->max_queue_pairs; i++) {
1452 callbacks[rxq2vq(i)] = skb_recv_done;
1453 callbacks[txq2vq(i)] = skb_xmit_done;
1454 sprintf(vi->rq[i].name, "input.%d", i);
1455 sprintf(vi->sq[i].name, "output.%d", i);
1456 names[rxq2vq(i)] = vi->rq[i].name;
1457 names[txq2vq(i)] = vi->sq[i].name;
1458 }
1459
1460 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1461 names);
1462 if (ret)
1463 goto err_find;
1464
1465 if (vi->has_cvq) {
1466 vi->cvq = vqs[total_vqs - 1];
1467 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00001468 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00001469 }
1470
1471 for (i = 0; i < vi->max_queue_pairs; i++) {
1472 vi->rq[i].vq = vqs[rxq2vq(i)];
1473 vi->sq[i].vq = vqs[txq2vq(i)];
1474 }
1475
1476 kfree(names);
1477 kfree(callbacks);
1478 kfree(vqs);
1479
1480 return 0;
1481
1482err_find:
1483 kfree(names);
1484err_names:
1485 kfree(callbacks);
1486err_callback:
1487 kfree(vqs);
1488err_vq:
1489 return ret;
1490}
1491
1492static int virtnet_alloc_queues(struct virtnet_info *vi)
1493{
1494 int i;
1495
1496 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1497 if (!vi->sq)
1498 goto err_sq;
1499 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00001500 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00001501 goto err_rq;
1502
1503 INIT_DELAYED_WORK(&vi->refill, refill_work);
1504 for (i = 0; i < vi->max_queue_pairs; i++) {
1505 vi->rq[i].pages = NULL;
1506 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1507 napi_weight);
1508
1509 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1510 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1511 }
1512
1513 return 0;
1514
1515err_rq:
1516 kfree(vi->sq);
1517err_sq:
1518 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00001519}
1520
Amit Shah3f9c10b2011-12-22 16:58:31 +05301521static int init_vqs(struct virtnet_info *vi)
1522{
Jason Wang986a4f42012-12-07 07:04:56 +00001523 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301524
Jason Wang986a4f42012-12-07 07:04:56 +00001525 /* Allocate send & receive queues */
1526 ret = virtnet_alloc_queues(vi);
1527 if (ret)
1528 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301529
Jason Wang986a4f42012-12-07 07:04:56 +00001530 ret = virtnet_find_vqs(vi);
1531 if (ret)
1532 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301533
Wanlong Gao47be2472013-01-24 23:51:29 +00001534 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00001535 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00001536 put_online_cpus();
1537
Amit Shah3f9c10b2011-12-22 16:58:31 +05301538 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00001539
1540err_free:
1541 virtnet_free_queues(vi);
1542err:
1543 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301544}
1545
Rusty Russell296f96f2007-10-22 11:03:37 +10001546static int virtnet_probe(struct virtio_device *vdev)
1547{
Jason Wang986a4f42012-12-07 07:04:56 +00001548 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001549 struct net_device *dev;
1550 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00001551 u16 max_queue_pairs;
1552
1553 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10301554 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1555 struct virtio_net_config,
1556 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00001557
1558 /* We need at least 2 queue's */
1559 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1560 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1561 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1562 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10001563
1564 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00001565 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10001566 if (!dev)
1567 return -ENOMEM;
1568
1569 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001570 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08001571 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10001572 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001573
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001574 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +10001575 SET_NETDEV_DEV(dev, &vdev->dev);
1576
1577 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001578 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001579 /* This opens up the world of extra features. */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001580 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1581 if (csum)
1582 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1583
1584 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1585 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05001586 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1587 }
Rusty Russell5539ae92008-05-02 21:50:46 -05001588 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001589 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1590 dev->hw_features |= NETIF_F_TSO;
1591 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1592 dev->hw_features |= NETIF_F_TSO6;
1593 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1594 dev->hw_features |= NETIF_F_TSO_ECN;
1595 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1596 dev->hw_features |= NETIF_F_UFO;
1597
1598 if (gso)
1599 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1600 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10001601 }
Thomas Huth4f491292013-08-27 17:09:02 +02001602 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1603 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10001604
Jason Wang4fda8302013-04-10 23:32:21 +00001605 dev->vlan_features = dev->features;
1606
Rusty Russell296f96f2007-10-22 11:03:37 +10001607 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10301608 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1609 virtio_cread_bytes(vdev,
1610 offsetof(struct virtio_net_config, mac),
1611 dev->dev_addr, dev->addr_len);
1612 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001613 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001614
1615 /* Set up our device-specific information */
1616 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001617 vi->dev = dev;
1618 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01001619 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001620 vi->stats = alloc_percpu(struct virtnet_stats);
1621 err = -ENOMEM;
1622 if (vi->stats == NULL)
1623 goto free;
1624
John Stultz827da442013-10-07 15:51:58 -07001625 for_each_possible_cpu(i) {
1626 struct virtnet_stats *virtnet_stats;
1627 virtnet_stats = per_cpu_ptr(vi->stats, i);
1628 u64_stats_init(&virtnet_stats->tx_syncp);
1629 u64_stats_init(&virtnet_stats->rx_syncp);
1630 }
1631
Jason Wang586d17c2012-04-11 20:43:52 +00001632 mutex_init(&vi->config_lock);
1633 vi->config_enable = true;
1634 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10001635
Herbert Xu97402b92008-04-18 11:24:27 +08001636 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00001637 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1638 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1639 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
Herbert Xu97402b92008-04-18 11:24:27 +08001640 vi->big_packets = true;
1641
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001642 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1643 vi->mergeable_rx_bufs = true;
1644
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301645 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1646 vi->any_header_sg = true;
1647
Jason Wang986a4f42012-12-07 07:04:56 +00001648 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1649 vi->has_cvq = true;
1650
1651 /* Use single tx/rx queue pair as default */
1652 vi->curr_queue_pairs = 1;
1653 vi->max_queue_pairs = max_queue_pairs;
1654
1655 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05301656 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001657 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08001658 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001659
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08001660 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
1661 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00001662
Rusty Russell296f96f2007-10-22 11:03:37 +10001663 err = register_netdev(dev);
1664 if (err) {
1665 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001666 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10001667 }
Rusty Russellb3369c12008-02-04 23:50:02 -05001668
1669 /* Last of all, set up some receive buffers. */
Sasha Levin55257d72013-04-29 12:00:08 +09301670 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001671 try_fill_recv(&vi->rq[i], GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -05001672
Jason Wang986a4f42012-12-07 07:04:56 +00001673 /* If we didn't even get one input buffer, we're useless. */
1674 if (vi->rq[i].num == 0) {
1675 free_unused_bufs(vi);
1676 err = -ENOMEM;
1677 goto free_recv_bufs;
1678 }
Rusty Russellb3369c12008-02-04 23:50:02 -05001679 }
1680
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001681 vi->nb.notifier_call = &virtnet_cpu_callback;
1682 err = register_hotcpu_notifier(&vi->nb);
1683 if (err) {
1684 pr_debug("virtio_net: registering cpu notifier failed\n");
1685 goto free_recv_bufs;
1686 }
1687
Jason Wang167c25e2010-11-10 14:45:41 +00001688 /* Assume link up if device can't report link status,
1689 otherwise get link status from config. */
1690 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1691 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001692 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00001693 } else {
1694 vi->status = VIRTIO_NET_S_LINK_UP;
1695 netif_carrier_on(dev);
1696 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001697
Jason Wang986a4f42012-12-07 07:04:56 +00001698 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1699 dev->name, max_queue_pairs);
1700
Rusty Russell296f96f2007-10-22 11:03:37 +10001701 return 0;
1702
Jason Wang986a4f42012-12-07 07:04:56 +00001703free_recv_bufs:
1704 free_receive_bufs(vi);
Rusty Russellb3369c12008-02-04 23:50:02 -05001705 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001706free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00001707 cancel_delayed_work_sync(&vi->refill);
Jason Wange9d74172012-12-07 07:04:55 +00001708 virtnet_del_vqs(vi);
Michael Dalton2613af02013-10-28 15:44:18 -07001709 if (vi->alloc_frag.page)
1710 put_page(vi->alloc_frag.page);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001711free_stats:
1712 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001713free:
1714 free_netdev(dev);
1715 return err;
1716}
1717
Amit Shah04486ed2011-12-22 16:58:32 +05301718static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10001719{
Amit Shah04486ed2011-12-22 16:58:32 +05301720 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00001721
1722 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001723 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001724
Jason Wang986a4f42012-12-07 07:04:56 +00001725 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001726
Jason Wang986a4f42012-12-07 07:04:56 +00001727 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05301728}
1729
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001730static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05301731{
1732 struct virtnet_info *vi = vdev->priv;
1733
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001734 unregister_hotcpu_notifier(&vi->nb);
1735
Jason Wang586d17c2012-04-11 20:43:52 +00001736 /* Prevent config work handler from accessing the device. */
1737 mutex_lock(&vi->config_lock);
1738 vi->config_enable = false;
1739 mutex_unlock(&vi->config_lock);
1740
Amit Shah04486ed2011-12-22 16:58:32 +05301741 unregister_netdev(vi->dev);
1742
1743 remove_vq_common(vi);
Michael Dalton2613af02013-10-28 15:44:18 -07001744 if (vi->alloc_frag.page)
1745 put_page(vi->alloc_frag.page);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001746
Jason Wang586d17c2012-04-11 20:43:52 +00001747 flush_work(&vi->config_work);
1748
Krishna Kumar2e66f552011-07-20 03:56:02 +00001749 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05001750 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001751}
1752
Aaron Lu89107002013-09-17 09:25:23 +09301753#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05301754static int virtnet_freeze(struct virtio_device *vdev)
1755{
1756 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001757 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05301758
Jason Wangec9debb2013-10-29 15:11:07 +08001759 unregister_hotcpu_notifier(&vi->nb);
1760
Jason Wang586d17c2012-04-11 20:43:52 +00001761 /* Prevent config work handler from accessing the device */
1762 mutex_lock(&vi->config_lock);
1763 vi->config_enable = false;
1764 mutex_unlock(&vi->config_lock);
1765
Amit Shah0741bcb2011-12-22 16:58:33 +05301766 netif_device_detach(vi->dev);
1767 cancel_delayed_work_sync(&vi->refill);
1768
1769 if (netif_running(vi->dev))
Jason Wang986a4f42012-12-07 07:04:56 +00001770 for (i = 0; i < vi->max_queue_pairs; i++) {
1771 napi_disable(&vi->rq[i].napi);
1772 netif_napi_del(&vi->rq[i].napi);
1773 }
Amit Shah0741bcb2011-12-22 16:58:33 +05301774
1775 remove_vq_common(vi);
1776
Jason Wang586d17c2012-04-11 20:43:52 +00001777 flush_work(&vi->config_work);
1778
Amit Shah0741bcb2011-12-22 16:58:33 +05301779 return 0;
1780}
1781
1782static int virtnet_restore(struct virtio_device *vdev)
1783{
1784 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001785 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05301786
1787 err = init_vqs(vi);
1788 if (err)
1789 return err;
1790
1791 if (netif_running(vi->dev))
Jason Wang986a4f42012-12-07 07:04:56 +00001792 for (i = 0; i < vi->max_queue_pairs; i++)
1793 virtnet_napi_enable(&vi->rq[i]);
Amit Shah0741bcb2011-12-22 16:58:33 +05301794
1795 netif_device_attach(vi->dev);
1796
Sasha Levin55257d72013-04-29 12:00:08 +09301797 for (i = 0; i < vi->curr_queue_pairs; i++)
Jason Wang986a4f42012-12-07 07:04:56 +00001798 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1799 schedule_delayed_work(&vi->refill, 0);
Amit Shah0741bcb2011-12-22 16:58:33 +05301800
Jason Wang586d17c2012-04-11 20:43:52 +00001801 mutex_lock(&vi->config_lock);
1802 vi->config_enable = true;
1803 mutex_unlock(&vi->config_lock);
1804
Jason Wang35ed1592013-10-15 11:18:59 +08001805 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00001806 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang35ed1592013-10-15 11:18:59 +08001807 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001808
Jason Wangec9debb2013-10-29 15:11:07 +08001809 err = register_hotcpu_notifier(&vi->nb);
1810 if (err)
1811 return err;
1812
Amit Shah0741bcb2011-12-22 16:58:33 +05301813 return 0;
1814}
1815#endif
1816
Rusty Russell296f96f2007-10-22 11:03:37 +10001817static struct virtio_device_id id_table[] = {
1818 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1819 { 0 },
1820};
1821
Rusty Russellc45a6812008-05-02 21:50:50 -05001822static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +10001823 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1824 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -05001825 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +08001826 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +00001827 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +00001828 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +00001829 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Jason Wang986a4f42012-12-07 07:04:56 +00001830 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
Amos Kong7e58d5a2013-01-21 01:17:23 +00001831 VIRTIO_NET_F_CTRL_MAC_ADDR,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301832 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05001833};
1834
Uwe Kleine-König22402522009-11-05 01:32:44 -08001835static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05001836 .feature_table = features,
1837 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +10001838 .driver.name = KBUILD_MODNAME,
1839 .driver.owner = THIS_MODULE,
1840 .id_table = id_table,
1841 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001842 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001843 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09301844#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05301845 .freeze = virtnet_freeze,
1846 .restore = virtnet_restore,
1847#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10001848};
1849
Rusty Russellb2a17022013-02-13 16:59:28 +10301850module_virtio_driver(virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001851
1852MODULE_DEVICE_TABLE(virtio, id_table);
1853MODULE_DESCRIPTION("Virtio network driver");
1854MODULE_LICENSE("GPL");