blob: 5c24288814db775175ae28d8b37849ac9c0a760d [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 Dalton2613af02013-10-28 15:44:18 -0700428 if (vi->big_packets)
Jason Wange9d74172012-12-07 07:04:55 +0000429 give_pages(rq, buf);
Michael Dalton2613af02013-10-28 15:44:18 -0700430 else if (vi->mergeable_rx_bufs)
431 put_page(virt_to_head_page(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 Russell58eba97d2010-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,
Rusty Russellf7bc9592013-03-20 15:44:28 +1030879 struct scatterlist *out,
880 struct scatterlist *in)
Amos Kong40cbfc32013-01-21 01:17:21 +0000881{
Rusty Russellf7bc9592013-03-20 15:44:28 +1030882 struct scatterlist *sgs[4], hdr, stat;
Amos Kong40cbfc32013-01-21 01:17:21 +0000883 struct virtio_net_ctrl_hdr ctrl;
884 virtio_net_ctrl_ack status = ~0;
Rusty Russellf7bc9592013-03-20 15:44:28 +1030885 unsigned out_num = 0, in_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +0000886
887 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +1030888 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +0000889
890 ctrl.class = class;
891 ctrl.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +1030892 /* Add header */
893 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
894 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +0000895
Rusty Russellf7bc9592013-03-20 15:44:28 +1030896 if (out)
897 sgs[out_num++] = out;
898 if (in)
899 sgs[out_num + in_num++] = in;
Amos Kong40cbfc32013-01-21 01:17:21 +0000900
Rusty Russellf7bc9592013-03-20 15:44:28 +1030901 /* Add return status. */
902 sg_init_one(&stat, &status, sizeof(status));
903 sgs[out_num + in_num++] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +0000904
Rusty Russellf7bc9592013-03-20 15:44:28 +1030905 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
906 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC)
907 < 0);
Amos Kong40cbfc32013-01-21 01:17:21 +0000908
Heinz Graalfs67975902013-10-29 09:40:02 +1030909 if (unlikely(!virtqueue_kick(vi->cvq)))
910 return status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +0000911
912 /* Spin for a response, the kick causes an ioport write, trapping
913 * into the hypervisor, so the request should be handled immediately.
914 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +1030915 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
916 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +0000917 cpu_relax();
918
919 return status == VIRTIO_NET_OK;
920}
921
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800922static int virtnet_set_mac_address(struct net_device *dev, void *p)
923{
924 struct virtnet_info *vi = netdev_priv(dev);
925 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000926 int ret;
Amos Kong7e58d5a2013-01-21 01:17:23 +0000927 struct sockaddr *addr = p;
928 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800929
Amos Kong7e58d5a2013-01-21 01:17:23 +0000930 ret = eth_prepare_mac_addr_change(dev, p);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000931 if (ret)
932 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800933
Amos Kong7e58d5a2013-01-21 01:17:23 +0000934 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
935 sg_init_one(&sg, addr->sa_data, dev->addr_len);
936 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
937 VIRTIO_NET_CTRL_MAC_ADDR_SET,
Rusty Russellf7bc9592013-03-20 15:44:28 +1030938 &sg, NULL)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +0000939 dev_warn(&vdev->dev,
940 "Failed to set mac address by vq command.\n");
941 return -EINVAL;
942 }
943 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russell855e0c52013-10-14 18:11:51 +1030944 unsigned int i;
945
946 /* Naturally, this has an atomicity problem. */
947 for (i = 0; i < dev->addr_len; i++)
948 virtio_cwrite8(vdev,
949 offsetof(struct virtio_net_config, mac) +
950 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +0000951 }
952
953 eth_commit_mac_addr_change(dev, p);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800954
955 return 0;
956}
957
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000958static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
959 struct rtnl_link_stats64 *tot)
960{
961 struct virtnet_info *vi = netdev_priv(dev);
962 int cpu;
963 unsigned int start;
964
965 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +0000966 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000967 u64 tpackets, tbytes, rpackets, rbytes;
968
969 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000970 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000971 tpackets = stats->tx_packets;
972 tbytes = stats->tx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000973 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +0000974
975 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000976 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000977 rpackets = stats->rx_packets;
978 rbytes = stats->rx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000979 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000980
981 tot->rx_packets += rpackets;
982 tot->tx_packets += tpackets;
983 tot->rx_bytes += rbytes;
984 tot->tx_bytes += tbytes;
985 }
986
987 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +0000988 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000989 tot->rx_dropped = dev->stats.rx_dropped;
990 tot->rx_length_errors = dev->stats.rx_length_errors;
991 tot->rx_frame_errors = dev->stats.rx_frame_errors;
992
993 return tot;
994}
995
Amit Shahda74e892008-02-29 16:24:50 +0530996#ifdef CONFIG_NET_POLL_CONTROLLER
997static void virtnet_netpoll(struct net_device *dev)
998{
999 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001000 int i;
Amit Shahda74e892008-02-29 16:24:50 +05301001
Jason Wang986a4f42012-12-07 07:04:56 +00001002 for (i = 0; i < vi->curr_queue_pairs; i++)
1003 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +05301004}
1005#endif
1006
Jason Wang586d17c2012-04-11 20:43:52 +00001007static void virtnet_ack_link_announce(struct virtnet_info *vi)
1008{
1009 rtnl_lock();
1010 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
Rusty Russellf7bc9592013-03-20 15:44:28 +10301011 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +00001012 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1013 rtnl_unlock();
1014}
1015
Jason Wang986a4f42012-12-07 07:04:56 +00001016static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1017{
1018 struct scatterlist sg;
1019 struct virtio_net_ctrl_mq s;
1020 struct net_device *dev = vi->dev;
1021
1022 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1023 return 0;
1024
1025 s.virtqueue_pairs = queue_pairs;
1026 sg_init_one(&sg, &s, sizeof(s));
1027
1028 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
Rusty Russellf7bc9592013-03-20 15:44:28 +10301029 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001030 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1031 queue_pairs);
1032 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301033 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001034 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001035 /* virtnet_open() will refill when device is going to up. */
1036 if (dev->flags & IFF_UP)
1037 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301038 }
Jason Wang986a4f42012-12-07 07:04:56 +00001039
1040 return 0;
1041}
1042
Rusty Russell296f96f2007-10-22 11:03:37 +10001043static int virtnet_close(struct net_device *dev)
1044{
1045 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001046 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001047
Rusty Russellb2baed62011-12-29 00:42:38 +00001048 /* Make sure refill_work doesn't re-enable napi! */
1049 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001050
1051 for (i = 0; i < vi->max_queue_pairs; i++)
1052 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001053
Rusty Russell296f96f2007-10-22 11:03:37 +10001054 return 0;
1055}
1056
Alex Williamson2af76982009-02-04 09:02:40 +00001057static void virtnet_set_rx_mode(struct net_device *dev)
1058{
1059 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001060 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +00001061 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001062 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001063 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001064 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001065 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001066 void *buf;
1067 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001068
1069 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
1070 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1071 return;
1072
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001073 promisc = ((dev->flags & IFF_PROMISC) != 0);
1074 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001075
Alex Williamson23e258e2009-05-01 17:27:56 +00001076 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001077
1078 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1079 VIRTIO_NET_CTRL_RX_PROMISC,
Rusty Russellf7bc9592013-03-20 15:44:28 +10301080 sg, NULL))
Alex Williamson2af76982009-02-04 09:02:40 +00001081 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1082 promisc ? "en" : "dis");
1083
Alex Williamson23e258e2009-05-01 17:27:56 +00001084 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001085
1086 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1087 VIRTIO_NET_CTRL_RX_ALLMULTI,
Rusty Russellf7bc9592013-03-20 15:44:28 +10301088 sg, NULL))
Alex Williamson2af76982009-02-04 09:02:40 +00001089 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1090 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001091
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001092 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001093 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001094 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001095 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1096 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1097 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001098 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001099 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001100
Alex Williamson23e258e2009-05-01 17:27:56 +00001101 sg_init_table(sg, 2);
1102
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001103 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001104 mac_data->entries = uc_count;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001105 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001106 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001107 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001108
1109 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001110 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001111
1112 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001113 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001114
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001115 mac_data->entries = mc_count;
Jiri Pirko567ec872010-02-23 23:17:07 +00001116 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001117 netdev_for_each_mc_addr(ha, dev)
1118 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001119
1120 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001121 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001122
1123 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1124 VIRTIO_NET_CTRL_MAC_TABLE_SET,
Rusty Russellf7bc9592013-03-20 15:44:28 +10301125 sg, NULL))
Thomas Huth99e872a2013-11-29 10:02:19 +01001126 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001127
1128 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001129}
1130
Patrick McHardy80d5c362013-04-19 02:04:28 +00001131static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1132 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001133{
1134 struct virtnet_info *vi = netdev_priv(dev);
1135 struct scatterlist sg;
1136
Alex Williamson23e258e2009-05-01 17:27:56 +00001137 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001138
1139 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
Rusty Russellf7bc9592013-03-20 15:44:28 +10301140 VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
Alex Williamson0bde95692009-02-04 09:02:50 +00001141 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001142 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001143}
1144
Patrick McHardy80d5c362013-04-19 02:04:28 +00001145static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1146 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001147{
1148 struct virtnet_info *vi = netdev_priv(dev);
1149 struct scatterlist sg;
1150
Alex Williamson23e258e2009-05-01 17:27:56 +00001151 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001152
1153 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
Rusty Russellf7bc9592013-03-20 15:44:28 +10301154 VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
Alex Williamson0bde95692009-02-04 09:02:50 +00001155 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001156 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001157}
1158
Wanlong Gao8898c212013-01-24 23:51:30 +00001159static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001160{
1161 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001162
1163 if (vi->affinity_hint_set) {
1164 for (i = 0; i < vi->max_queue_pairs; i++) {
1165 virtqueue_set_affinity(vi->rq[i].vq, -1);
1166 virtqueue_set_affinity(vi->sq[i].vq, -1);
1167 }
1168
1169 vi->affinity_hint_set = false;
1170 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001171}
1172
1173static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001174{
1175 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001176 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001177
1178 /* In multiqueue mode, when the number of cpu is equal to the number of
1179 * queue pairs, we let the queue pairs to be private to one cpu by
1180 * setting the affinity hint to eliminate the contention.
1181 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001182 if (vi->curr_queue_pairs == 1 ||
1183 vi->max_queue_pairs != num_online_cpus()) {
1184 virtnet_clean_affinity(vi, -1);
1185 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001186 }
1187
Wanlong Gao8898c212013-01-24 23:51:30 +00001188 i = 0;
1189 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001190 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1191 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001192 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001193 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001194 }
1195
Wanlong Gao8898c212013-01-24 23:51:30 +00001196 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001197}
1198
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001199static int virtnet_cpu_callback(struct notifier_block *nfb,
1200 unsigned long action, void *hcpu)
1201{
1202 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1203
1204 switch(action & ~CPU_TASKS_FROZEN) {
1205 case CPU_ONLINE:
1206 case CPU_DOWN_FAILED:
1207 case CPU_DEAD:
1208 virtnet_set_affinity(vi);
1209 break;
1210 case CPU_DOWN_PREPARE:
1211 virtnet_clean_affinity(vi, (long)hcpu);
1212 break;
1213 default:
1214 break;
1215 }
Jason Wang3ab098d2013-10-15 11:18:58 +08001216
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001217 return NOTIFY_OK;
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001218}
1219
Rick Jones8f9f4662011-10-19 08:10:59 +00001220static void virtnet_get_ringparam(struct net_device *dev,
1221 struct ethtool_ringparam *ring)
1222{
1223 struct virtnet_info *vi = netdev_priv(dev);
1224
Jason Wang986a4f42012-12-07 07:04:56 +00001225 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1226 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001227 ring->rx_pending = ring->rx_max_pending;
1228 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001229}
1230
Rick Jones66846042011-11-14 14:17:08 +00001231
1232static void virtnet_get_drvinfo(struct net_device *dev,
1233 struct ethtool_drvinfo *info)
1234{
1235 struct virtnet_info *vi = netdev_priv(dev);
1236 struct virtio_device *vdev = vi->vdev;
1237
1238 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1239 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1240 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1241
1242}
1243
Jason Wangd73bcd22012-12-07 07:04:57 +00001244/* TODO: Eliminate OOO packets during switching */
1245static int virtnet_set_channels(struct net_device *dev,
1246 struct ethtool_channels *channels)
1247{
1248 struct virtnet_info *vi = netdev_priv(dev);
1249 u16 queue_pairs = channels->combined_count;
1250 int err;
1251
1252 /* We don't support separate rx/tx channels.
1253 * We don't allow setting 'other' channels.
1254 */
1255 if (channels->rx_count || channels->tx_count || channels->other_count)
1256 return -EINVAL;
1257
1258 if (queue_pairs > vi->max_queue_pairs)
1259 return -EINVAL;
1260
Wanlong Gao47be2472013-01-24 23:51:29 +00001261 get_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001262 err = virtnet_set_queues(vi, queue_pairs);
1263 if (!err) {
1264 netif_set_real_num_tx_queues(dev, queue_pairs);
1265 netif_set_real_num_rx_queues(dev, queue_pairs);
1266
Wanlong Gao8898c212013-01-24 23:51:30 +00001267 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001268 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001269 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001270
1271 return err;
1272}
1273
1274static void virtnet_get_channels(struct net_device *dev,
1275 struct ethtool_channels *channels)
1276{
1277 struct virtnet_info *vi = netdev_priv(dev);
1278
1279 channels->combined_count = vi->curr_queue_pairs;
1280 channels->max_combined = vi->max_queue_pairs;
1281 channels->max_other = 0;
1282 channels->rx_count = 0;
1283 channels->tx_count = 0;
1284 channels->other_count = 0;
1285}
1286
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001287static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001288 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001289 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001290 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001291 .set_channels = virtnet_set_channels,
1292 .get_channels = virtnet_get_channels,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001293};
1294
Mark McLoughlin39da5812008-11-26 13:58:11 +00001295#define MIN_MTU 68
1296#define MAX_MTU 65535
1297
1298static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1299{
1300 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1301 return -EINVAL;
1302 dev->mtu = new_mtu;
1303 return 0;
1304}
1305
Stephen Hemminger76288b42009-01-06 10:44:22 -08001306static const struct net_device_ops virtnet_netdev = {
1307 .ndo_open = virtnet_open,
1308 .ndo_stop = virtnet_close,
1309 .ndo_start_xmit = start_xmit,
1310 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001311 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001312 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001313 .ndo_change_mtu = virtnet_change_mtu,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001314 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001315 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1316 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001317#ifdef CONFIG_NET_POLL_CONTROLLER
1318 .ndo_poll_controller = virtnet_netpoll,
1319#endif
1320};
1321
Jason Wang586d17c2012-04-11 20:43:52 +00001322static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001323{
Jason Wang586d17c2012-04-11 20:43:52 +00001324 struct virtnet_info *vi =
1325 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001326 u16 v;
1327
Jason Wang586d17c2012-04-11 20:43:52 +00001328 mutex_lock(&vi->config_lock);
1329 if (!vi->config_enable)
1330 goto done;
1331
Rusty Russell855e0c52013-10-14 18:11:51 +10301332 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1333 struct virtio_net_config, status, &v) < 0)
Jason Wang586d17c2012-04-11 20:43:52 +00001334 goto done;
1335
1336 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001337 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001338 virtnet_ack_link_announce(vi);
1339 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001340
1341 /* Ignore unknown (future) status bits */
1342 v &= VIRTIO_NET_S_LINK_UP;
1343
1344 if (vi->status == v)
Jason Wang586d17c2012-04-11 20:43:52 +00001345 goto done;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001346
1347 vi->status = v;
1348
1349 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1350 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001351 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001352 } else {
1353 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001354 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001355 }
Jason Wang586d17c2012-04-11 20:43:52 +00001356done:
1357 mutex_unlock(&vi->config_lock);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001358}
1359
1360static void virtnet_config_changed(struct virtio_device *vdev)
1361{
1362 struct virtnet_info *vi = vdev->priv;
1363
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001364 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001365}
1366
Jason Wang986a4f42012-12-07 07:04:56 +00001367static void virtnet_free_queues(struct virtnet_info *vi)
1368{
1369 kfree(vi->rq);
1370 kfree(vi->sq);
1371}
1372
1373static void free_receive_bufs(struct virtnet_info *vi)
1374{
1375 int i;
1376
1377 for (i = 0; i < vi->max_queue_pairs; i++) {
1378 while (vi->rq[i].pages)
1379 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1380 }
1381}
1382
1383static void free_unused_bufs(struct virtnet_info *vi)
1384{
1385 void *buf;
1386 int i;
1387
1388 for (i = 0; i < vi->max_queue_pairs; i++) {
1389 struct virtqueue *vq = vi->sq[i].vq;
1390 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1391 dev_kfree_skb(buf);
1392 }
1393
1394 for (i = 0; i < vi->max_queue_pairs; i++) {
1395 struct virtqueue *vq = vi->rq[i].vq;
1396
1397 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Michael Dalton2613af02013-10-28 15:44:18 -07001398 if (vi->big_packets)
Jason Wang986a4f42012-12-07 07:04:56 +00001399 give_pages(&vi->rq[i], buf);
Michael Dalton2613af02013-10-28 15:44:18 -07001400 else if (vi->mergeable_rx_bufs)
1401 put_page(virt_to_head_page(buf));
Jason Wang986a4f42012-12-07 07:04:56 +00001402 else
1403 dev_kfree_skb(buf);
1404 --vi->rq[i].num;
1405 }
1406 BUG_ON(vi->rq[i].num != 0);
1407 }
1408}
1409
Jason Wange9d74172012-12-07 07:04:55 +00001410static void virtnet_del_vqs(struct virtnet_info *vi)
1411{
1412 struct virtio_device *vdev = vi->vdev;
1413
Wanlong Gao8898c212013-01-24 23:51:30 +00001414 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001415
Jason Wange9d74172012-12-07 07:04:55 +00001416 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001417
1418 virtnet_free_queues(vi);
1419}
1420
1421static int virtnet_find_vqs(struct virtnet_info *vi)
1422{
1423 vq_callback_t **callbacks;
1424 struct virtqueue **vqs;
1425 int ret = -ENOMEM;
1426 int i, total_vqs;
1427 const char **names;
1428
1429 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1430 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1431 * possible control vq.
1432 */
1433 total_vqs = vi->max_queue_pairs * 2 +
1434 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1435
1436 /* Allocate space for find_vqs parameters */
1437 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1438 if (!vqs)
1439 goto err_vq;
1440 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1441 if (!callbacks)
1442 goto err_callback;
1443 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1444 if (!names)
1445 goto err_names;
1446
1447 /* Parameters for control virtqueue, if any */
1448 if (vi->has_cvq) {
1449 callbacks[total_vqs - 1] = NULL;
1450 names[total_vqs - 1] = "control";
1451 }
1452
1453 /* Allocate/initialize parameters for send/receive virtqueues */
1454 for (i = 0; i < vi->max_queue_pairs; i++) {
1455 callbacks[rxq2vq(i)] = skb_recv_done;
1456 callbacks[txq2vq(i)] = skb_xmit_done;
1457 sprintf(vi->rq[i].name, "input.%d", i);
1458 sprintf(vi->sq[i].name, "output.%d", i);
1459 names[rxq2vq(i)] = vi->rq[i].name;
1460 names[txq2vq(i)] = vi->sq[i].name;
1461 }
1462
1463 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1464 names);
1465 if (ret)
1466 goto err_find;
1467
1468 if (vi->has_cvq) {
1469 vi->cvq = vqs[total_vqs - 1];
1470 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00001471 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00001472 }
1473
1474 for (i = 0; i < vi->max_queue_pairs; i++) {
1475 vi->rq[i].vq = vqs[rxq2vq(i)];
1476 vi->sq[i].vq = vqs[txq2vq(i)];
1477 }
1478
1479 kfree(names);
1480 kfree(callbacks);
1481 kfree(vqs);
1482
1483 return 0;
1484
1485err_find:
1486 kfree(names);
1487err_names:
1488 kfree(callbacks);
1489err_callback:
1490 kfree(vqs);
1491err_vq:
1492 return ret;
1493}
1494
1495static int virtnet_alloc_queues(struct virtnet_info *vi)
1496{
1497 int i;
1498
1499 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1500 if (!vi->sq)
1501 goto err_sq;
1502 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00001503 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00001504 goto err_rq;
1505
1506 INIT_DELAYED_WORK(&vi->refill, refill_work);
1507 for (i = 0; i < vi->max_queue_pairs; i++) {
1508 vi->rq[i].pages = NULL;
1509 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1510 napi_weight);
1511
1512 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1513 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1514 }
1515
1516 return 0;
1517
1518err_rq:
1519 kfree(vi->sq);
1520err_sq:
1521 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00001522}
1523
Amit Shah3f9c10b2011-12-22 16:58:31 +05301524static int init_vqs(struct virtnet_info *vi)
1525{
Jason Wang986a4f42012-12-07 07:04:56 +00001526 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301527
Jason Wang986a4f42012-12-07 07:04:56 +00001528 /* Allocate send & receive queues */
1529 ret = virtnet_alloc_queues(vi);
1530 if (ret)
1531 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301532
Jason Wang986a4f42012-12-07 07:04:56 +00001533 ret = virtnet_find_vqs(vi);
1534 if (ret)
1535 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301536
Wanlong Gao47be2472013-01-24 23:51:29 +00001537 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00001538 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00001539 put_online_cpus();
1540
Amit Shah3f9c10b2011-12-22 16:58:31 +05301541 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00001542
1543err_free:
1544 virtnet_free_queues(vi);
1545err:
1546 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301547}
1548
Rusty Russell296f96f2007-10-22 11:03:37 +10001549static int virtnet_probe(struct virtio_device *vdev)
1550{
Jason Wang986a4f42012-12-07 07:04:56 +00001551 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001552 struct net_device *dev;
1553 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00001554 u16 max_queue_pairs;
1555
1556 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10301557 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1558 struct virtio_net_config,
1559 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00001560
1561 /* We need at least 2 queue's */
1562 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1563 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1564 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1565 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10001566
1567 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00001568 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10001569 if (!dev)
1570 return -ENOMEM;
1571
1572 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001573 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08001574 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10001575 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001576
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001577 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +10001578 SET_NETDEV_DEV(dev, &vdev->dev);
1579
1580 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001581 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001582 /* This opens up the world of extra features. */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001583 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1584 if (csum)
1585 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1586
1587 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1588 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05001589 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1590 }
Rusty Russell5539ae962008-05-02 21:50:46 -05001591 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001592 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1593 dev->hw_features |= NETIF_F_TSO;
1594 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1595 dev->hw_features |= NETIF_F_TSO6;
1596 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1597 dev->hw_features |= NETIF_F_TSO_ECN;
1598 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1599 dev->hw_features |= NETIF_F_UFO;
1600
1601 if (gso)
1602 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1603 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10001604 }
Thomas Huth4f491292013-08-27 17:09:02 +02001605 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1606 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10001607
Jason Wang4fda8302013-04-10 23:32:21 +00001608 dev->vlan_features = dev->features;
1609
Rusty Russell296f96f2007-10-22 11:03:37 +10001610 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10301611 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1612 virtio_cread_bytes(vdev,
1613 offsetof(struct virtio_net_config, mac),
1614 dev->dev_addr, dev->addr_len);
1615 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001616 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001617
1618 /* Set up our device-specific information */
1619 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001620 vi->dev = dev;
1621 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01001622 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001623 vi->stats = alloc_percpu(struct virtnet_stats);
1624 err = -ENOMEM;
1625 if (vi->stats == NULL)
1626 goto free;
1627
John Stultz827da442013-10-07 15:51:58 -07001628 for_each_possible_cpu(i) {
1629 struct virtnet_stats *virtnet_stats;
1630 virtnet_stats = per_cpu_ptr(vi->stats, i);
1631 u64_stats_init(&virtnet_stats->tx_syncp);
1632 u64_stats_init(&virtnet_stats->rx_syncp);
1633 }
1634
Jason Wang586d17c2012-04-11 20:43:52 +00001635 mutex_init(&vi->config_lock);
1636 vi->config_enable = true;
1637 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10001638
Herbert Xu97402b92008-04-18 11:24:27 +08001639 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00001640 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1641 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1642 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
Herbert Xu97402b92008-04-18 11:24:27 +08001643 vi->big_packets = true;
1644
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001645 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1646 vi->mergeable_rx_bufs = true;
1647
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301648 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1649 vi->any_header_sg = true;
1650
Jason Wang986a4f42012-12-07 07:04:56 +00001651 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1652 vi->has_cvq = true;
1653
1654 /* Use single tx/rx queue pair as default */
1655 vi->curr_queue_pairs = 1;
1656 vi->max_queue_pairs = max_queue_pairs;
1657
1658 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05301659 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001660 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08001661 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001662
Zhi Yong Wu0f13b662013-11-18 21:19:27 +08001663 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
1664 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00001665
Rusty Russell296f96f2007-10-22 11:03:37 +10001666 err = register_netdev(dev);
1667 if (err) {
1668 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001669 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10001670 }
Rusty Russellb3369c12008-02-04 23:50:02 -05001671
1672 /* Last of all, set up some receive buffers. */
Sasha Levin55257d72013-04-29 12:00:08 +09301673 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001674 try_fill_recv(&vi->rq[i], GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -05001675
Jason Wang986a4f42012-12-07 07:04:56 +00001676 /* If we didn't even get one input buffer, we're useless. */
1677 if (vi->rq[i].num == 0) {
1678 free_unused_bufs(vi);
1679 err = -ENOMEM;
1680 goto free_recv_bufs;
1681 }
Rusty Russellb3369c12008-02-04 23:50:02 -05001682 }
1683
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001684 vi->nb.notifier_call = &virtnet_cpu_callback;
1685 err = register_hotcpu_notifier(&vi->nb);
1686 if (err) {
1687 pr_debug("virtio_net: registering cpu notifier failed\n");
1688 goto free_recv_bufs;
1689 }
1690
Jason Wang167c25e2010-11-10 14:45:41 +00001691 /* Assume link up if device can't report link status,
1692 otherwise get link status from config. */
1693 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1694 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001695 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00001696 } else {
1697 vi->status = VIRTIO_NET_S_LINK_UP;
1698 netif_carrier_on(dev);
1699 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001700
Jason Wang986a4f42012-12-07 07:04:56 +00001701 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1702 dev->name, max_queue_pairs);
1703
Rusty Russell296f96f2007-10-22 11:03:37 +10001704 return 0;
1705
Jason Wang986a4f42012-12-07 07:04:56 +00001706free_recv_bufs:
1707 free_receive_bufs(vi);
Rusty Russellb3369c12008-02-04 23:50:02 -05001708 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001709free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00001710 cancel_delayed_work_sync(&vi->refill);
Jason Wange9d74172012-12-07 07:04:55 +00001711 virtnet_del_vqs(vi);
Michael Dalton2613af02013-10-28 15:44:18 -07001712 if (vi->alloc_frag.page)
1713 put_page(vi->alloc_frag.page);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001714free_stats:
1715 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001716free:
1717 free_netdev(dev);
1718 return err;
1719}
1720
Amit Shah04486ed2011-12-22 16:58:32 +05301721static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10001722{
Amit Shah04486ed2011-12-22 16:58:32 +05301723 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00001724
1725 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001726 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001727
Jason Wang986a4f42012-12-07 07:04:56 +00001728 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001729
Jason Wang986a4f42012-12-07 07:04:56 +00001730 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05301731}
1732
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001733static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05301734{
1735 struct virtnet_info *vi = vdev->priv;
1736
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001737 unregister_hotcpu_notifier(&vi->nb);
1738
Jason Wang586d17c2012-04-11 20:43:52 +00001739 /* Prevent config work handler from accessing the device. */
1740 mutex_lock(&vi->config_lock);
1741 vi->config_enable = false;
1742 mutex_unlock(&vi->config_lock);
1743
Amit Shah04486ed2011-12-22 16:58:32 +05301744 unregister_netdev(vi->dev);
1745
1746 remove_vq_common(vi);
Michael Dalton2613af02013-10-28 15:44:18 -07001747 if (vi->alloc_frag.page)
1748 put_page(vi->alloc_frag.page);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001749
Jason Wang586d17c2012-04-11 20:43:52 +00001750 flush_work(&vi->config_work);
1751
Krishna Kumar2e66f552011-07-20 03:56:02 +00001752 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05001753 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001754}
1755
Aaron Lu89107002013-09-17 09:25:23 +09301756#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05301757static int virtnet_freeze(struct virtio_device *vdev)
1758{
1759 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001760 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05301761
Jason Wangec9debb2013-10-29 15:11:07 +08001762 unregister_hotcpu_notifier(&vi->nb);
1763
Jason Wang586d17c2012-04-11 20:43:52 +00001764 /* Prevent config work handler from accessing the device */
1765 mutex_lock(&vi->config_lock);
1766 vi->config_enable = false;
1767 mutex_unlock(&vi->config_lock);
1768
Amit Shah0741bcb2011-12-22 16:58:33 +05301769 netif_device_detach(vi->dev);
1770 cancel_delayed_work_sync(&vi->refill);
1771
1772 if (netif_running(vi->dev))
Jason Wang986a4f42012-12-07 07:04:56 +00001773 for (i = 0; i < vi->max_queue_pairs; i++) {
1774 napi_disable(&vi->rq[i].napi);
1775 netif_napi_del(&vi->rq[i].napi);
1776 }
Amit Shah0741bcb2011-12-22 16:58:33 +05301777
1778 remove_vq_common(vi);
1779
Jason Wang586d17c2012-04-11 20:43:52 +00001780 flush_work(&vi->config_work);
1781
Amit Shah0741bcb2011-12-22 16:58:33 +05301782 return 0;
1783}
1784
1785static int virtnet_restore(struct virtio_device *vdev)
1786{
1787 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001788 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05301789
1790 err = init_vqs(vi);
1791 if (err)
1792 return err;
1793
1794 if (netif_running(vi->dev))
Jason Wang986a4f42012-12-07 07:04:56 +00001795 for (i = 0; i < vi->max_queue_pairs; i++)
1796 virtnet_napi_enable(&vi->rq[i]);
Amit Shah0741bcb2011-12-22 16:58:33 +05301797
1798 netif_device_attach(vi->dev);
1799
Sasha Levin55257d72013-04-29 12:00:08 +09301800 for (i = 0; i < vi->curr_queue_pairs; i++)
Jason Wang986a4f42012-12-07 07:04:56 +00001801 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1802 schedule_delayed_work(&vi->refill, 0);
Amit Shah0741bcb2011-12-22 16:58:33 +05301803
Jason Wang586d17c2012-04-11 20:43:52 +00001804 mutex_lock(&vi->config_lock);
1805 vi->config_enable = true;
1806 mutex_unlock(&vi->config_lock);
1807
Jason Wang35ed1592013-10-15 11:18:59 +08001808 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00001809 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang35ed1592013-10-15 11:18:59 +08001810 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001811
Jason Wangec9debb2013-10-29 15:11:07 +08001812 err = register_hotcpu_notifier(&vi->nb);
1813 if (err)
1814 return err;
1815
Amit Shah0741bcb2011-12-22 16:58:33 +05301816 return 0;
1817}
1818#endif
1819
Rusty Russell296f96f2007-10-22 11:03:37 +10001820static struct virtio_device_id id_table[] = {
1821 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1822 { 0 },
1823};
1824
Rusty Russellc45a6812008-05-02 21:50:50 -05001825static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +10001826 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1827 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -05001828 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +08001829 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +00001830 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +00001831 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +00001832 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Jason Wang986a4f42012-12-07 07:04:56 +00001833 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
Amos Kong7e58d5a2013-01-21 01:17:23 +00001834 VIRTIO_NET_F_CTRL_MAC_ADDR,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301835 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05001836};
1837
Uwe Kleine-König22402522009-11-05 01:32:44 -08001838static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05001839 .feature_table = features,
1840 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +10001841 .driver.name = KBUILD_MODNAME,
1842 .driver.owner = THIS_MODULE,
1843 .id_table = id_table,
1844 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001845 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001846 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09301847#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05301848 .freeze = virtnet_freeze,
1849 .restore = virtnet_restore,
1850#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10001851};
1852
Rusty Russellb2a17022013-02-13 16:59:28 +10301853module_virtio_driver(virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001854
1855MODULE_DEVICE_TABLE(virtio, id_table);
1856MODULE_DESCRIPTION("Virtio network driver");
1857MODULE_LICENSE("GPL");