blob: 9bd70aa87bf739f53b629b9523bc7210897f0740 [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
Jason Wange9d74172012-12-07 07:04:55 +000075 /* Chain pages by the private ptr. */
76 struct page *pages;
77
78 /* RX: fragments + linear part + virtio header */
79 struct scatterlist sg[MAX_SKB_FRAGS + 2];
Jason Wang986a4f42012-12-07 07:04:56 +000080
81 /* Name of this receive queue: input.$index */
82 char name[40];
Jason Wange9d74172012-12-07 07:04:55 +000083};
84
85struct virtnet_info {
86 struct virtio_device *vdev;
87 struct virtqueue *cvq;
88 struct net_device *dev;
Jason Wang986a4f42012-12-07 07:04:56 +000089 struct send_queue *sq;
90 struct receive_queue *rq;
Jason Wange9d74172012-12-07 07:04:55 +000091 unsigned int status;
92
Jason Wang986a4f42012-12-07 07:04:56 +000093 /* Max # of queue pairs supported by the device */
94 u16 max_queue_pairs;
95
96 /* # of queue pairs currently used by the driver */
97 u16 curr_queue_pairs;
98
Herbert Xu97402b92008-04-18 11:24:27 +080099 /* I like... big packets and I cannot lie! */
100 bool big_packets;
101
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800102 /* Host will merge rx buffers for big packets (shake it! shake it!) */
103 bool mergeable_rx_bufs;
104
Jason Wang986a4f42012-12-07 07:04:56 +0000105 /* Has control virtqueue */
106 bool has_cvq;
107
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930108 /* Host can handle any s/g split between our header and packet data */
109 bool any_header_sg;
110
Jason Wang586d17c2012-04-11 20:43:52 +0000111 /* enable config space updates */
112 bool config_enable;
113
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000114 /* Active statistics */
115 struct virtnet_stats __percpu *stats;
116
Rusty Russell3161e452009-08-26 12:22:32 -0700117 /* Work struct for refilling if we run low on memory. */
118 struct delayed_work refill;
119
Jason Wang586d17c2012-04-11 20:43:52 +0000120 /* Work struct for config space updates */
121 struct work_struct config_work;
122
123 /* Lock for config space updates */
124 struct mutex config_lock;
Jason Wang986a4f42012-12-07 07:04:56 +0000125
Michael Dalton2613af02013-10-28 15:44:18 -0700126 /* Page_frag for GFP_KERNEL packet buffer allocation when we run
127 * low on memory.
128 */
129 struct page_frag alloc_frag;
130
Jason Wang986a4f42012-12-07 07:04:56 +0000131 /* Does the affinity hint is set for virtqueues? */
132 bool affinity_hint_set;
Wanlong Gao47be2472013-01-24 23:51:29 +0000133
Wanlong Gao8de4b2f2013-01-24 23:51:31 +0000134 /* CPU hot plug notifier */
135 struct notifier_block nb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000136};
137
Rusty Russellb3f24692009-09-24 09:59:19 -0600138struct skb_vnet_hdr {
139 union {
140 struct virtio_net_hdr hdr;
141 struct virtio_net_hdr_mrg_rxbuf mhdr;
142 };
143};
144
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000145struct padded_vnet_hdr {
146 struct virtio_net_hdr hdr;
147 /*
148 * virtio_net_hdr should be in a separated sg buffer because of a
149 * QEMU bug, and data sg buffer shares same page with this header sg.
150 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
151 */
152 char padding[6];
153};
154
Jason Wang986a4f42012-12-07 07:04:56 +0000155/* Converting between virtqueue no. and kernel tx/rx queue no.
156 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
157 */
158static int vq2txq(struct virtqueue *vq)
159{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000160 return (vq->index - 1) / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000161}
162
163static int txq2vq(int txq)
164{
165 return txq * 2 + 1;
166}
167
168static int vq2rxq(struct virtqueue *vq)
169{
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000170 return vq->index / 2;
Jason Wang986a4f42012-12-07 07:04:56 +0000171}
172
173static int rxq2vq(int rxq)
174{
175 return rxq * 2;
176}
177
Rusty Russellb3f24692009-09-24 09:59:19 -0600178static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000179{
Rusty Russellb3f24692009-09-24 09:59:19 -0600180 return (struct skb_vnet_hdr *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000181}
182
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000183/*
184 * private is used to chain pages for big packets, put the whole
185 * most recent used list in the beginning for reuse
186 */
Jason Wange9d74172012-12-07 07:04:55 +0000187static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500188{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000189 struct page *end;
190
Jason Wange9d74172012-12-07 07:04:55 +0000191 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000192 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000193 end->private = (unsigned long)rq->pages;
194 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500195}
196
Jason Wange9d74172012-12-07 07:04:55 +0000197static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500198{
Jason Wange9d74172012-12-07 07:04:55 +0000199 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500200
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000201 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000202 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000203 /* clear private here, it is used to chain pages */
204 p->private = 0;
205 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500206 p = alloc_page(gfp_mask);
207 return p;
208}
209
Jason Wange9d74172012-12-07 07:04:55 +0000210static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000211{
Jason Wange9d74172012-12-07 07:04:55 +0000212 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000213
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500214 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000215 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000216
Rusty Russell363f1512008-06-08 20:51:55 +1000217 /* We were probably waiting for more output buffers. */
Jason Wang986a4f42012-12-07 07:04:56 +0000218 netif_wake_subqueue(vi->dev, vq2txq(vq));
Rusty Russell296f96f2007-10-22 11:03:37 +1000219}
220
Mike Waychison34646452012-01-04 12:52:32 +0000221/* Called from bottom half context */
Jason Wange9d74172012-12-07 07:04:55 +0000222static struct sk_buff *page_to_skb(struct receive_queue *rq,
Michael Dalton2613af02013-10-28 15:44:18 -0700223 struct page *page, unsigned int offset,
224 unsigned int len, unsigned int truesize)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000225{
Jason Wange9d74172012-12-07 07:04:55 +0000226 struct virtnet_info *vi = rq->vq->vdev->priv;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000227 struct sk_buff *skb;
228 struct skb_vnet_hdr *hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700229 unsigned int copy, hdr_len, hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000230 char *p;
231
Michael Dalton2613af02013-10-28 15:44:18 -0700232 p = page_address(page) + offset;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000233
234 /* copy small packet so we can reuse these pages for small data */
235 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
236 if (unlikely(!skb))
237 return NULL;
238
239 hdr = skb_vnet_hdr(skb);
240
241 if (vi->mergeable_rx_bufs) {
242 hdr_len = sizeof hdr->mhdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700243 hdr_padded_len = sizeof hdr->mhdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000244 } else {
245 hdr_len = sizeof hdr->hdr;
Michael Dalton2613af02013-10-28 15:44:18 -0700246 hdr_padded_len = sizeof(struct padded_vnet_hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000247 }
248
249 memcpy(hdr, p, hdr_len);
250
251 len -= hdr_len;
Michael Dalton2613af02013-10-28 15:44:18 -0700252 offset += hdr_padded_len;
253 p += hdr_padded_len;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000254
255 copy = len;
256 if (copy > skb_tailroom(skb))
257 copy = skb_tailroom(skb);
258 memcpy(skb_put(skb, copy), p, copy);
259
260 len -= copy;
261 offset += copy;
262
Michael Dalton2613af02013-10-28 15:44:18 -0700263 if (vi->mergeable_rx_bufs) {
264 if (len)
265 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
266 else
267 put_page(page);
268 return skb;
269 }
270
Sasha Levine878d782011-09-28 04:40:54 +0000271 /*
272 * Verify that we can indeed put this data into a skb.
273 * This is here to handle cases when the device erroneously
274 * tries to receive more than is possible. This is usually
275 * the case of a broken device.
276 */
277 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000278 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000279 dev_kfree_skb(skb);
280 return NULL;
281 }
Michael Dalton2613af02013-10-28 15:44:18 -0700282 BUG_ON(offset >= PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000283 while (len) {
Michael Dalton2613af02013-10-28 15:44:18 -0700284 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
285 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
286 frag_size, truesize);
287 len -= frag_size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000288 page = (struct page *)page->private;
289 offset = 0;
290 }
291
292 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000293 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000294
295 return skb;
296}
297
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200298static struct sk_buff *receive_small(void *buf, unsigned int len)
299{
300 struct sk_buff * skb = buf;
301
302 len -= sizeof(struct virtio_net_hdr);
303 skb_trim(skb, len);
304
305 return skb;
306}
307
308static struct sk_buff *receive_big(struct net_device *dev,
309 struct receive_queue *rq,
310 void *buf,
311 unsigned int len)
312{
313 struct page *page = buf;
314 struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
315
316 if (unlikely(!skb))
317 goto err;
318
319 return skb;
320
321err:
322 dev->stats.rx_dropped++;
323 give_pages(rq, page);
324 return NULL;
325}
326
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200327static struct sk_buff *receive_mergeable(struct net_device *dev,
328 struct receive_queue *rq,
329 void *buf,
330 unsigned int len)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000331{
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200332 struct skb_vnet_hdr *hdr = buf;
333 int num_buf = hdr->mhdr.num_buffers;
334 struct page *page = virt_to_head_page(buf);
335 int offset = buf - page_address(page);
336 struct sk_buff *head_skb = page_to_skb(rq, page, offset, len,
337 MERGE_BUFFER_LEN);
Michael Dalton2613af02013-10-28 15:44:18 -0700338 struct sk_buff *curr_skb = head_skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000339
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200340 if (unlikely(!curr_skb))
341 goto err_skb;
342
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000343 while (--num_buf) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200344 int num_skb_frags;
345
Michael Dalton2613af02013-10-28 15:44:18 -0700346 buf = virtqueue_get_buf(rq->vq, &len);
347 if (unlikely(!buf)) {
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200348 pr_debug("%s: rx error: %d buffers out of %d missing\n",
349 dev->name, num_buf, hdr->mhdr.num_buffers);
350 dev->stats.rx_length_errors++;
351 goto err_buf;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000352 }
Michael Dalton5061de32013-11-14 10:41:04 -0800353 if (unlikely(len > MERGE_BUFFER_LEN)) {
Michael Dalton2613af02013-10-28 15:44:18 -0700354 pr_debug("%s: rx error: merge buffer too long\n",
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200355 dev->name);
Michael Dalton5061de32013-11-14 10:41:04 -0800356 len = MERGE_BUFFER_LEN;
Michael Dalton2613af02013-10-28 15:44:18 -0700357 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200358
359 page = virt_to_head_page(buf);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200360
361 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
Michael Dalton2613af02013-10-28 15:44:18 -0700362 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
363 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200364
365 if (unlikely(!nskb))
366 goto err_skb;
Michael Dalton2613af02013-10-28 15:44:18 -0700367 if (curr_skb == head_skb)
368 skb_shinfo(curr_skb)->frag_list = nskb;
369 else
370 curr_skb->next = nskb;
371 curr_skb = nskb;
372 head_skb->truesize += nskb->truesize;
373 num_skb_frags = 0;
374 }
375 if (curr_skb != head_skb) {
376 head_skb->data_len += len;
377 head_skb->len += len;
Michael Dalton5061de32013-11-14 10:41:04 -0800378 head_skb->truesize += MERGE_BUFFER_LEN;
Michael Dalton2613af02013-10-28 15:44:18 -0700379 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200380 offset = buf - page_address(page);
Jason Wangba275242013-11-01 14:07:48 +0800381 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
382 put_page(page);
383 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
Michael Dalton5061de32013-11-14 10:41:04 -0800384 len, MERGE_BUFFER_LEN);
Jason Wangba275242013-11-01 14:07:48 +0800385 } else {
386 skb_add_rx_frag(curr_skb, num_skb_frags, page,
Michael Dalton5061de32013-11-14 10:41:04 -0800387 offset, len, MERGE_BUFFER_LEN);
Jason Wangba275242013-11-01 14:07:48 +0800388 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200389 }
390
391 return head_skb;
392
393err_skb:
394 put_page(page);
395 while (--num_buf) {
396 buf = virtqueue_get_buf(rq->vq, &len);
397 if (unlikely(!buf)) {
398 pr_debug("%s: rx error: %d buffers missing\n",
399 dev->name, num_buf);
400 dev->stats.rx_length_errors++;
401 break;
402 }
403 page = virt_to_head_page(buf);
404 put_page(page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000405 }
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200406err_buf:
407 dev->stats.rx_dropped++;
408 dev_kfree_skb(head_skb);
409 return NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000410}
411
Jason Wange9d74172012-12-07 07:04:55 +0000412static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000413{
Jason Wange9d74172012-12-07 07:04:55 +0000414 struct virtnet_info *vi = rq->vq->vdev->priv;
415 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000416 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000417 struct sk_buff *skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000418 struct skb_vnet_hdr *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000419
420 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
421 pr_debug("%s: short packet %i\n", dev->name, len);
422 dev->stats.rx_length_errors++;
Michael Dalton98bfd232013-12-05 13:14:05 -0800423 if (vi->mergeable_rx_bufs)
Michael Dalton2613af02013-10-28 15:44:18 -0700424 put_page(virt_to_head_page(buf));
Michael Dalton98bfd232013-12-05 13:14:05 -0800425 else if (vi->big_packets)
426 give_pages(rq, buf);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000427 else
428 dev_kfree_skb(buf);
429 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000430 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000431
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200432 if (vi->mergeable_rx_bufs)
Michael S. Tsirkin8fc3b9e2013-11-28 13:30:55 +0200433 skb = receive_mergeable(dev, rq, buf, len);
Michael S. Tsirkinf1211592013-11-28 13:30:59 +0200434 else if (vi->big_packets)
435 skb = receive_big(dev, rq, buf, len);
436 else
437 skb = receive_small(buf, len);
438
439 if (unlikely(!skb))
440 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800441
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000442 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000443
Eric Dumazet83a27052012-06-05 22:35:24 +0000444 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000445 stats->rx_bytes += skb->len;
446 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000447 u64_stats_update_end(&stats->rx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +1000448
Rusty Russellb3f24692009-09-24 09:59:19 -0600449 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000450 pr_debug("Needs csum!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600451 if (!skb_partial_csum_set(skb,
452 hdr->hdr.csum_start,
453 hdr->hdr.csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000454 goto frame_err;
Jason Wang10a8d942011-06-10 00:56:17 +0000455 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
456 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000457 }
458
Mark McLoughlin23cde762008-06-08 20:49:00 +1000459 skb->protocol = eth_type_trans(skb, dev);
460 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
461 ntohs(skb->protocol), skb->len, skb->pkt_type);
462
Rusty Russellb3f24692009-09-24 09:59:19 -0600463 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000464 pr_debug("GSO!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600465 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000466 case VIRTIO_NET_HDR_GSO_TCPV4:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000467 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
Rusty Russell296f96f2007-10-22 11:03:37 +1000468 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000469 case VIRTIO_NET_HDR_GSO_UDP:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000470 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Rusty Russell296f96f2007-10-22 11:03:37 +1000471 break;
472 case VIRTIO_NET_HDR_GSO_TCPV6:
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000473 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
Rusty Russell296f96f2007-10-22 11:03:37 +1000474 break;
475 default:
Amerigo Wangbe443892012-11-08 17:47:28 +0000476 net_warn_ratelimited("%s: bad gso type %u.\n",
477 dev->name, hdr->hdr.gso_type);
Rusty Russell296f96f2007-10-22 11:03:37 +1000478 goto frame_err;
479 }
480
Rusty Russellb3f24692009-09-24 09:59:19 -0600481 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000482 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
Rusty Russell34a48572008-02-04 23:50:02 -0500483
Rusty Russellb3f24692009-09-24 09:59:19 -0600484 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
Rusty Russell296f96f2007-10-22 11:03:37 +1000485 if (skb_shinfo(skb)->gso_size == 0) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000486 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000487 goto frame_err;
488 }
489
490 /* Header must be checked, and gso_segs computed. */
491 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
492 skb_shinfo(skb)->gso_segs = 0;
493 }
494
495 netif_receive_skb(skb);
496 return;
497
498frame_err:
499 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000500 dev_kfree_skb(skb);
501}
502
Jason Wange9d74172012-12-07 07:04:55 +0000503static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000504{
Jason Wange9d74172012-12-07 07:04:55 +0000505 struct virtnet_info *vi = rq->vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000506 struct sk_buff *skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000507 struct skb_vnet_hdr *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000508 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000509
Michael Dalton5061de32013-11-14 10:41:04 -0800510 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000511 if (unlikely(!skb))
512 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800513
Michael Dalton5061de32013-11-14 10:41:04 -0800514 skb_put(skb, GOOD_PACKET_LEN);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000515
516 hdr = skb_vnet_hdr(skb);
Jason Wange9d74172012-12-07 07:04:55 +0000517 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000518
Jason Wange9d74172012-12-07 07:04:55 +0000519 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000520
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030521 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000522 if (err < 0)
523 dev_kfree_skb(skb);
524
525 return err;
526}
527
Jason Wange9d74172012-12-07 07:04:55 +0000528static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000529{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000530 struct page *first, *list = NULL;
531 char *p;
532 int i, err, offset;
533
Jason Wange9d74172012-12-07 07:04:55 +0000534 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000535 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000536 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000537 if (!first) {
538 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000539 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000540 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700541 }
Jason Wange9d74172012-12-07 07:04:55 +0000542 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000543
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000544 /* chain new page in list head to match sg */
545 first->private = (unsigned long)list;
546 list = first;
547 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800548
Jason Wange9d74172012-12-07 07:04:55 +0000549 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000550 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000551 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000552 return -ENOMEM;
553 }
554 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800555
Jason Wange9d74172012-12-07 07:04:55 +0000556 /* rq->sg[0], rq->sg[1] share the same page */
557 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
558 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800559
Jason Wange9d74172012-12-07 07:04:55 +0000560 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000561 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000562 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800563
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000564 /* chain first in list head */
565 first->private = (unsigned long)list;
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030566 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
567 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000568 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000569 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800570
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000571 return err;
572}
Herbert Xu97402b92008-04-18 11:24:27 +0800573
Jason Wange9d74172012-12-07 07:04:55 +0000574static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000575{
Michael Dalton2613af02013-10-28 15:44:18 -0700576 struct virtnet_info *vi = rq->vq->vdev->priv;
577 char *buf = NULL;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000578 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000579
Michael Dalton2613af02013-10-28 15:44:18 -0700580 if (gfp & __GFP_WAIT) {
Michael Dalton5061de32013-11-14 10:41:04 -0800581 if (skb_page_frag_refill(MERGE_BUFFER_LEN, &vi->alloc_frag,
Michael Dalton2613af02013-10-28 15:44:18 -0700582 gfp)) {
583 buf = (char *)page_address(vi->alloc_frag.page) +
584 vi->alloc_frag.offset;
585 get_page(vi->alloc_frag.page);
Michael Dalton5061de32013-11-14 10:41:04 -0800586 vi->alloc_frag.offset += MERGE_BUFFER_LEN;
Michael Dalton2613af02013-10-28 15:44:18 -0700587 }
588 } else {
Michael Dalton5061de32013-11-14 10:41:04 -0800589 buf = netdev_alloc_frag(MERGE_BUFFER_LEN);
Michael Dalton2613af02013-10-28 15:44:18 -0700590 }
591 if (!buf)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000592 return -ENOMEM;
593
Michael Dalton5061de32013-11-14 10:41:04 -0800594 sg_init_one(rq->sg, buf, MERGE_BUFFER_LEN);
Michael Dalton2613af02013-10-28 15:44:18 -0700595 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000596 if (err < 0)
Michael Dalton2613af02013-10-28 15:44:18 -0700597 put_page(virt_to_head_page(buf));
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000598
599 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000600}
601
Rusty Russellb2baed62011-12-29 00:42:38 +0000602/*
603 * Returns false if we couldn't fill entirely (OOM).
604 *
605 * Normally run in the receive path, but can also be run from ndo_open
606 * before we're receiving packets, or from refill_work which is
607 * careful to disable receiving (using napi_disable).
608 */
Jason Wange9d74172012-12-07 07:04:55 +0000609static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800610{
Jason Wange9d74172012-12-07 07:04:55 +0000611 struct virtnet_info *vi = rq->vq->vdev->priv;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800612 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000613 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800614
Amit Shah0aea51c2009-08-26 14:58:28 +0530615 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000616 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000617 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000618 else if (vi->big_packets)
Jason Wange9d74172012-12-07 07:04:55 +0000619 err = add_recvbuf_big(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000620 else
Jason Wange9d74172012-12-07 07:04:55 +0000621 err = add_recvbuf_small(rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800622
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000623 oom = err == -ENOMEM;
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030624 if (err)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800625 break;
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800626 } while (rq->vq->num_free);
Heinz Graalfs67975902013-10-29 09:40:02 +1030627 if (unlikely(!virtqueue_kick(rq->vq)))
628 return false;
Rusty Russell3161e452009-08-26 12:22:32 -0700629 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800630}
631
Rusty Russell18445c42008-02-04 23:49:57 -0500632static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000633{
634 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +0000635 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
Jason Wange9d74172012-12-07 07:04:55 +0000636
Rusty Russell18445c42008-02-04 23:49:57 -0500637 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000638 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300639 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000640 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500641 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000642}
643
Jason Wange9d74172012-12-07 07:04:55 +0000644static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800645{
Jason Wange9d74172012-12-07 07:04:55 +0000646 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800647
648 /* If all buffers were filled by other side before we napi_enabled, we
649 * won't get another interrupt, so process any outstanding packets
650 * now. virtnet_poll wants re-enable the queue, so we disable here.
651 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000652 if (napi_schedule_prep(&rq->napi)) {
653 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300654 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000655 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300656 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800657 }
658}
659
Rusty Russell3161e452009-08-26 12:22:32 -0700660static void refill_work(struct work_struct *work)
661{
Jason Wange9d74172012-12-07 07:04:55 +0000662 struct virtnet_info *vi =
663 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700664 bool still_empty;
Jason Wang986a4f42012-12-07 07:04:56 +0000665 int i;
Rusty Russell3161e452009-08-26 12:22:32 -0700666
Sasha Levin55257d72013-04-29 12:00:08 +0930667 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +0000668 struct receive_queue *rq = &vi->rq[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700669
Jason Wang986a4f42012-12-07 07:04:56 +0000670 napi_disable(&rq->napi);
671 still_empty = !try_fill_recv(rq, GFP_KERNEL);
672 virtnet_napi_enable(rq);
673
674 /* In theory, this can happen: if we don't get any buffers in
675 * we will *never* try to fill again.
676 */
677 if (still_empty)
678 schedule_delayed_work(&vi->refill, HZ/2);
679 }
Rusty Russell3161e452009-08-26 12:22:32 -0700680}
681
Rusty Russell296f96f2007-10-22 11:03:37 +1000682static int virtnet_poll(struct napi_struct *napi, int budget)
683{
Jason Wange9d74172012-12-07 07:04:55 +0000684 struct receive_queue *rq =
685 container_of(napi, struct receive_queue, napi);
686 struct virtnet_info *vi = rq->vq->vdev->priv;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000687 void *buf;
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +0300688 unsigned int r, len, received = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000689
690again:
691 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000692 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
693 receive_buf(rq, buf, len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000694 received++;
695 }
696
Jason Wangbe121f42014-01-16 14:45:24 +0800697 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
Jason Wange9d74172012-12-07 07:04:55 +0000698 if (!try_fill_recv(rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700699 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700700 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000701
Rusty Russell8329d982007-11-19 11:20:43 -0500702 /* Out of packets? */
703 if (received < budget) {
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +0300704 r = virtqueue_enable_cb_prepare(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800705 napi_complete(napi);
Michael S. Tsirkincbdadbb2013-07-09 08:13:04 +0300706 if (unlikely(virtqueue_poll(rq->vq, r)) &&
Joe Perches8e95a202009-12-03 07:58:21 +0000707 napi_schedule_prep(napi)) {
Jason Wange9d74172012-12-07 07:04:55 +0000708 virtqueue_disable_cb(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800709 __napi_schedule(napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000710 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100711 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000712 }
713
714 return received;
715}
716
Jason Wang986a4f42012-12-07 07:04:56 +0000717static int virtnet_open(struct net_device *dev)
718{
719 struct virtnet_info *vi = netdev_priv(dev);
720 int i;
721
Jason Wange4166622013-05-21 20:03:58 +0000722 for (i = 0; i < vi->max_queue_pairs; i++) {
723 if (i < vi->curr_queue_pairs)
724 /* Make sure we have some buffers: if oom use wq. */
725 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
726 schedule_delayed_work(&vi->refill, 0);
Jason Wang986a4f42012-12-07 07:04:56 +0000727 virtnet_napi_enable(&vi->rq[i]);
728 }
729
730 return 0;
731}
732
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800733static void free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000734{
735 struct sk_buff *skb;
Michael S. Tsirkin6ee57bc2012-10-16 23:56:14 +1030736 unsigned int len;
Jason Wange9d74172012-12-07 07:04:55 +0000737 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +0000738 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +1000739
Jason Wange9d74172012-12-07 07:04:55 +0000740 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000741 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000742
Eric Dumazet83a27052012-06-05 22:35:24 +0000743 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000744 stats->tx_bytes += skb->len;
745 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000746 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000747
Eric Dumazeted79bab2009-10-14 14:36:43 +0000748 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000749 }
750}
751
Jason Wange9d74172012-12-07 07:04:55 +0000752static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000753{
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930754 struct skb_vnet_hdr *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000755 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +0000756 struct virtnet_info *vi = sq->vq->vdev->priv;
Michael S. Tsirkin7bedc7d2012-10-16 23:56:14 +1030757 unsigned num_sg;
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930758 unsigned hdr_len;
759 bool can_push;
Rusty Russell296f96f2007-10-22 11:03:37 +1000760
Johannes Berge1749612008-10-27 15:59:26 -0700761 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930762 if (vi->mergeable_rx_bufs)
763 hdr_len = sizeof hdr->mhdr;
764 else
765 hdr_len = sizeof hdr->hdr;
766
767 can_push = vi->any_header_sg &&
768 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
769 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
770 /* Even if we can, don't push here yet as this would skew
771 * csum_start offset below. */
772 if (can_push)
773 hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
774 else
775 hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000776
Rusty Russell296f96f2007-10-22 11:03:37 +1000777 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600778 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +0000779 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
Rusty Russellb3f24692009-09-24 09:59:19 -0600780 hdr->hdr.csum_offset = skb->csum_offset;
Rusty Russell296f96f2007-10-22 11:03:37 +1000781 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600782 hdr->hdr.flags = 0;
783 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000784 }
785
786 if (skb_is_gso(skb)) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600787 hdr->hdr.hdr_len = skb_headlen(skb);
788 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500789 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russellb3f24692009-09-24 09:59:19 -0600790 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
Rusty Russell296f96f2007-10-22 11:03:37 +1000791 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Rusty Russellb3f24692009-09-24 09:59:19 -0600792 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Rusty Russell296f96f2007-10-22 11:03:37 +1000793 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
Rusty Russellb3f24692009-09-24 09:59:19 -0600794 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Rusty Russell296f96f2007-10-22 11:03:37 +1000795 else
796 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500797 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
Rusty Russellb3f24692009-09-24 09:59:19 -0600798 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000799 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600800 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
801 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000802 }
803
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800804 if (vi->mergeable_rx_bufs)
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930805 hdr->mhdr.num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800806
Michael S. Tsirkine7428e92013-07-25 10:20:23 +0930807 if (can_push) {
808 __skb_push(skb, hdr_len);
809 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
810 /* Pull header back to avoid skew in tx bytes calculations. */
811 __skb_pull(skb, hdr_len);
812 } else {
813 sg_set_buf(sq->sg, hdr, hdr_len);
814 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
815 }
Rusty Russell9dc7b9e2013-03-20 15:44:28 +1030816 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +1000817}
818
Stephen Hemminger424efe92009-08-31 19:50:51 +0000819static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -0500820{
821 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +0000822 int qnum = skb_get_queue_mapping(skb);
823 struct send_queue *sq = &vi->sq[qnum];
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030824 int err;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500825
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500826 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +0000827 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500828
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700829 /* Try to transmit */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800830 err = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -0600831
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030832 /* This should not happen! */
Heinz Graalfs67975902013-10-29 09:40:02 +1030833 if (unlikely(err) || unlikely(!virtqueue_kick(sq->vq))) {
Rusty Russell9ed4cb02012-10-16 23:56:14 +1030834 dev->stats.tx_fifo_errors++;
835 if (net_ratelimit())
836 dev_warn(&dev->dev,
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800837 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
Rusty Russell58eba97d2010-07-02 16:34:01 +0000838 dev->stats.tx_dropped++;
839 kfree_skb(skb);
840 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -0500841 }
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700842
Rusty Russell48925e32009-09-24 09:59:20 -0600843 /* Don't wait up for transmitted skbs to be freed. */
844 skb_orphan(skb);
845 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -0500846
Rusty Russell48925e32009-09-24 09:59:20 -0600847 /* Apparently nice girls don't return TX_BUSY; stop the queue
848 * before it gets out of hand. Naturally, this wastes entries. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800849 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +0000850 netif_stop_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +0000851 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -0600852 /* More just got used, free them then recheck. */
Linus Torvaldsb7dfde92012-12-20 08:37:04 -0800853 free_old_xmit_skbs(sq);
854 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
Jason Wang986a4f42012-12-07 07:04:56 +0000855 netif_start_subqueue(dev, qnum);
Jason Wange9d74172012-12-07 07:04:55 +0000856 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -0600857 }
858 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500859 }
Rusty Russell48925e32009-09-24 09:59:20 -0600860
861 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +1000862}
863
Amos Kong40cbfc32013-01-21 01:17:21 +0000864/*
865 * Send command via the control virtqueue and check status. Commands
866 * supported by the hypervisor, as indicated by feature bits, should
stephen hemminger788a8b62013-12-09 16:18:45 -0800867 * never fail unless improperly formatted.
Amos Kong40cbfc32013-01-21 01:17:21 +0000868 */
869static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
stephen hemmingerd24bae32013-12-09 16:17:40 -0800870 struct scatterlist *out)
Amos Kong40cbfc32013-01-21 01:17:21 +0000871{
Rusty Russellf7bc9592013-03-20 15:44:28 +1030872 struct scatterlist *sgs[4], hdr, stat;
Amos Kong40cbfc32013-01-21 01:17:21 +0000873 struct virtio_net_ctrl_hdr ctrl;
874 virtio_net_ctrl_ack status = ~0;
stephen hemmingerd24bae32013-12-09 16:17:40 -0800875 unsigned out_num = 0, tmp;
Amos Kong40cbfc32013-01-21 01:17:21 +0000876
877 /* Caller should know better */
Rusty Russellf7bc9592013-03-20 15:44:28 +1030878 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
Amos Kong40cbfc32013-01-21 01:17:21 +0000879
880 ctrl.class = class;
881 ctrl.cmd = cmd;
Rusty Russellf7bc9592013-03-20 15:44:28 +1030882 /* Add header */
883 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
884 sgs[out_num++] = &hdr;
Amos Kong40cbfc32013-01-21 01:17:21 +0000885
Rusty Russellf7bc9592013-03-20 15:44:28 +1030886 if (out)
887 sgs[out_num++] = out;
Amos Kong40cbfc32013-01-21 01:17:21 +0000888
Rusty Russellf7bc9592013-03-20 15:44:28 +1030889 /* Add return status. */
890 sg_init_one(&stat, &status, sizeof(status));
stephen hemmingerd24bae32013-12-09 16:17:40 -0800891 sgs[out_num] = &stat;
Amos Kong40cbfc32013-01-21 01:17:21 +0000892
stephen hemmingerd24bae32013-12-09 16:17:40 -0800893 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
894 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC) < 0);
Amos Kong40cbfc32013-01-21 01:17:21 +0000895
Heinz Graalfs67975902013-10-29 09:40:02 +1030896 if (unlikely(!virtqueue_kick(vi->cvq)))
897 return status == VIRTIO_NET_OK;
Amos Kong40cbfc32013-01-21 01:17:21 +0000898
899 /* Spin for a response, the kick causes an ioport write, trapping
900 * into the hypervisor, so the request should be handled immediately.
901 */
Heinz Graalfs047b9b92013-10-29 09:40:47 +1030902 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
903 !virtqueue_is_broken(vi->cvq))
Amos Kong40cbfc32013-01-21 01:17:21 +0000904 cpu_relax();
905
906 return status == VIRTIO_NET_OK;
907}
908
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800909static int virtnet_set_mac_address(struct net_device *dev, void *p)
910{
911 struct virtnet_info *vi = netdev_priv(dev);
912 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000913 int ret;
Amos Kong7e58d5a2013-01-21 01:17:23 +0000914 struct sockaddr *addr = p;
915 struct scatterlist sg;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800916
Amos Kong7e58d5a2013-01-21 01:17:23 +0000917 ret = eth_prepare_mac_addr_change(dev, p);
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000918 if (ret)
919 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800920
Amos Kong7e58d5a2013-01-21 01:17:23 +0000921 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
922 sg_init_one(&sg, addr->sa_data, dev->addr_len);
923 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -0800924 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
Amos Kong7e58d5a2013-01-21 01:17:23 +0000925 dev_warn(&vdev->dev,
926 "Failed to set mac address by vq command.\n");
927 return -EINVAL;
928 }
929 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russell855e0c52013-10-14 18:11:51 +1030930 unsigned int i;
931
932 /* Naturally, this has an atomicity problem. */
933 for (i = 0; i < dev->addr_len; i++)
934 virtio_cwrite8(vdev,
935 offsetof(struct virtio_net_config, mac) +
936 i, addr->sa_data[i]);
Amos Kong7e58d5a2013-01-21 01:17:23 +0000937 }
938
939 eth_commit_mac_addr_change(dev, p);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800940
941 return 0;
942}
943
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000944static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
945 struct rtnl_link_stats64 *tot)
946{
947 struct virtnet_info *vi = netdev_priv(dev);
948 int cpu;
949 unsigned int start;
950
951 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +0000952 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000953 u64 tpackets, tbytes, rpackets, rbytes;
954
955 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000956 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000957 tpackets = stats->tx_packets;
958 tbytes = stats->tx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000959 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +0000960
961 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000962 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000963 rpackets = stats->rx_packets;
964 rbytes = stats->rx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000965 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000966
967 tot->rx_packets += rpackets;
968 tot->tx_packets += tpackets;
969 tot->rx_bytes += rbytes;
970 tot->tx_bytes += tbytes;
971 }
972
973 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +0000974 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000975 tot->rx_dropped = dev->stats.rx_dropped;
976 tot->rx_length_errors = dev->stats.rx_length_errors;
977 tot->rx_frame_errors = dev->stats.rx_frame_errors;
978
979 return tot;
980}
981
Amit Shahda74e892008-02-29 16:24:50 +0530982#ifdef CONFIG_NET_POLL_CONTROLLER
983static void virtnet_netpoll(struct net_device *dev)
984{
985 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +0000986 int i;
Amit Shahda74e892008-02-29 16:24:50 +0530987
Jason Wang986a4f42012-12-07 07:04:56 +0000988 for (i = 0; i < vi->curr_queue_pairs; i++)
989 napi_schedule(&vi->rq[i].napi);
Amit Shahda74e892008-02-29 16:24:50 +0530990}
991#endif
992
Jason Wang586d17c2012-04-11 20:43:52 +0000993static void virtnet_ack_link_announce(struct virtnet_info *vi)
994{
995 rtnl_lock();
996 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
stephen hemmingerd24bae32013-12-09 16:17:40 -0800997 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
Jason Wang586d17c2012-04-11 20:43:52 +0000998 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
999 rtnl_unlock();
1000}
1001
Jason Wang986a4f42012-12-07 07:04:56 +00001002static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1003{
1004 struct scatterlist sg;
1005 struct virtio_net_ctrl_mq s;
1006 struct net_device *dev = vi->dev;
1007
1008 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1009 return 0;
1010
1011 s.virtqueue_pairs = queue_pairs;
1012 sg_init_one(&sg, &s, sizeof(s));
1013
1014 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001015 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001016 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1017 queue_pairs);
1018 return -EINVAL;
Sasha Levin55257d72013-04-29 12:00:08 +09301019 } else {
Jason Wang986a4f42012-12-07 07:04:56 +00001020 vi->curr_queue_pairs = queue_pairs;
Jason Wang35ed1592013-10-15 11:18:59 +08001021 /* virtnet_open() will refill when device is going to up. */
1022 if (dev->flags & IFF_UP)
1023 schedule_delayed_work(&vi->refill, 0);
Sasha Levin55257d72013-04-29 12:00:08 +09301024 }
Jason Wang986a4f42012-12-07 07:04:56 +00001025
1026 return 0;
1027}
1028
Rusty Russell296f96f2007-10-22 11:03:37 +10001029static int virtnet_close(struct net_device *dev)
1030{
1031 struct virtnet_info *vi = netdev_priv(dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001032 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +10001033
Rusty Russellb2baed62011-12-29 00:42:38 +00001034 /* Make sure refill_work doesn't re-enable napi! */
1035 cancel_delayed_work_sync(&vi->refill);
Jason Wang986a4f42012-12-07 07:04:56 +00001036
1037 for (i = 0; i < vi->max_queue_pairs; i++)
1038 napi_disable(&vi->rq[i].napi);
Rusty Russell296f96f2007-10-22 11:03:37 +10001039
Rusty Russell296f96f2007-10-22 11:03:37 +10001040 return 0;
1041}
1042
Alex Williamson2af76982009-02-04 09:02:40 +00001043static void virtnet_set_rx_mode(struct net_device *dev)
1044{
1045 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001046 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +00001047 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001048 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001049 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001050 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001051 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001052 void *buf;
1053 int i;
Alex Williamson2af76982009-02-04 09:02:40 +00001054
stephen hemminger788a8b62013-12-09 16:18:45 -08001055 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
Alex Williamson2af76982009-02-04 09:02:40 +00001056 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1057 return;
1058
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001059 promisc = ((dev->flags & IFF_PROMISC) != 0);
1060 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +00001061
Alex Williamson23e258e2009-05-01 17:27:56 +00001062 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +00001063
1064 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001065 VIRTIO_NET_CTRL_RX_PROMISC, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001066 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1067 promisc ? "en" : "dis");
1068
Alex Williamson23e258e2009-05-01 17:27:56 +00001069 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +00001070
1071 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001072 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
Alex Williamson2af76982009-02-04 09:02:40 +00001073 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1074 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001075
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001076 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001077 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001078 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001079 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1080 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1081 mac_data = buf;
Joe Perchese68ed8f2013-02-03 17:28:15 +00001082 if (!buf)
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001083 return;
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001084
Alex Williamson23e258e2009-05-01 17:27:56 +00001085 sg_init_table(sg, 2);
1086
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001087 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001088 mac_data->entries = uc_count;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001089 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001090 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +00001091 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001092
1093 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001094 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001095
1096 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001097 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001098
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001099 mac_data->entries = mc_count;
Jiri Pirko567ec872010-02-23 23:17:07 +00001100 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001101 netdev_for_each_mc_addr(ha, dev)
1102 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001103
1104 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001105 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001106
1107 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001108 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
Thomas Huth99e872a2013-11-29 10:02:19 +01001109 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
Alex Williamsonf565a7c2009-02-04 09:02:45 +00001110
1111 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +00001112}
1113
Patrick McHardy80d5c362013-04-19 02:04:28 +00001114static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1115 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001116{
1117 struct virtnet_info *vi = netdev_priv(dev);
1118 struct scatterlist sg;
1119
Alex Williamson23e258e2009-05-01 17:27:56 +00001120 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001121
1122 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001123 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001124 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001125 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001126}
1127
Patrick McHardy80d5c362013-04-19 02:04:28 +00001128static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1129 __be16 proto, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +00001130{
1131 struct virtnet_info *vi = netdev_priv(dev);
1132 struct scatterlist sg;
1133
Alex Williamson23e258e2009-05-01 17:27:56 +00001134 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +00001135
1136 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
stephen hemmingerd24bae32013-12-09 16:17:40 -08001137 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
Alex Williamson0bde95692009-02-04 09:02:50 +00001138 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -05001139 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +00001140}
1141
Wanlong Gao8898c212013-01-24 23:51:30 +00001142static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
Jason Wang986a4f42012-12-07 07:04:56 +00001143{
1144 int i;
Wanlong Gao8898c212013-01-24 23:51:30 +00001145
1146 if (vi->affinity_hint_set) {
1147 for (i = 0; i < vi->max_queue_pairs; i++) {
1148 virtqueue_set_affinity(vi->rq[i].vq, -1);
1149 virtqueue_set_affinity(vi->sq[i].vq, -1);
1150 }
1151
1152 vi->affinity_hint_set = false;
1153 }
Wanlong Gao8898c212013-01-24 23:51:30 +00001154}
1155
1156static void virtnet_set_affinity(struct virtnet_info *vi)
Jason Wang986a4f42012-12-07 07:04:56 +00001157{
1158 int i;
Wanlong Gao47be2472013-01-24 23:51:29 +00001159 int cpu;
Jason Wang986a4f42012-12-07 07:04:56 +00001160
1161 /* In multiqueue mode, when the number of cpu is equal to the number of
1162 * queue pairs, we let the queue pairs to be private to one cpu by
1163 * setting the affinity hint to eliminate the contention.
1164 */
Wanlong Gao8898c212013-01-24 23:51:30 +00001165 if (vi->curr_queue_pairs == 1 ||
1166 vi->max_queue_pairs != num_online_cpus()) {
1167 virtnet_clean_affinity(vi, -1);
1168 return;
Jason Wang986a4f42012-12-07 07:04:56 +00001169 }
1170
Wanlong Gao8898c212013-01-24 23:51:30 +00001171 i = 0;
1172 for_each_online_cpu(cpu) {
Jason Wang986a4f42012-12-07 07:04:56 +00001173 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1174 virtqueue_set_affinity(vi->sq[i].vq, cpu);
Jason Wang9bb8ca82013-11-05 18:19:45 +08001175 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
Wanlong Gao8898c212013-01-24 23:51:30 +00001176 i++;
Jason Wang986a4f42012-12-07 07:04:56 +00001177 }
1178
Wanlong Gao8898c212013-01-24 23:51:30 +00001179 vi->affinity_hint_set = true;
Jason Wang986a4f42012-12-07 07:04:56 +00001180}
1181
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001182static int virtnet_cpu_callback(struct notifier_block *nfb,
1183 unsigned long action, void *hcpu)
1184{
1185 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1186
1187 switch(action & ~CPU_TASKS_FROZEN) {
1188 case CPU_ONLINE:
1189 case CPU_DOWN_FAILED:
1190 case CPU_DEAD:
1191 virtnet_set_affinity(vi);
1192 break;
1193 case CPU_DOWN_PREPARE:
1194 virtnet_clean_affinity(vi, (long)hcpu);
1195 break;
1196 default:
1197 break;
1198 }
Jason Wang3ab098d2013-10-15 11:18:58 +08001199
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001200 return NOTIFY_OK;
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001201}
1202
Rick Jones8f9f4662011-10-19 08:10:59 +00001203static void virtnet_get_ringparam(struct net_device *dev,
1204 struct ethtool_ringparam *ring)
1205{
1206 struct virtnet_info *vi = netdev_priv(dev);
1207
Jason Wang986a4f42012-12-07 07:04:56 +00001208 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1209 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
Rick Jones8f9f4662011-10-19 08:10:59 +00001210 ring->rx_pending = ring->rx_max_pending;
1211 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +00001212}
1213
Rick Jones66846042011-11-14 14:17:08 +00001214
1215static void virtnet_get_drvinfo(struct net_device *dev,
1216 struct ethtool_drvinfo *info)
1217{
1218 struct virtnet_info *vi = netdev_priv(dev);
1219 struct virtio_device *vdev = vi->vdev;
1220
1221 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1222 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1223 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1224
1225}
1226
Jason Wangd73bcd22012-12-07 07:04:57 +00001227/* TODO: Eliminate OOO packets during switching */
1228static int virtnet_set_channels(struct net_device *dev,
1229 struct ethtool_channels *channels)
1230{
1231 struct virtnet_info *vi = netdev_priv(dev);
1232 u16 queue_pairs = channels->combined_count;
1233 int err;
1234
1235 /* We don't support separate rx/tx channels.
1236 * We don't allow setting 'other' channels.
1237 */
1238 if (channels->rx_count || channels->tx_count || channels->other_count)
1239 return -EINVAL;
1240
1241 if (queue_pairs > vi->max_queue_pairs)
1242 return -EINVAL;
1243
Wanlong Gao47be2472013-01-24 23:51:29 +00001244 get_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001245 err = virtnet_set_queues(vi, queue_pairs);
1246 if (!err) {
1247 netif_set_real_num_tx_queues(dev, queue_pairs);
1248 netif_set_real_num_rx_queues(dev, queue_pairs);
1249
Wanlong Gao8898c212013-01-24 23:51:30 +00001250 virtnet_set_affinity(vi);
Jason Wangd73bcd22012-12-07 07:04:57 +00001251 }
Wanlong Gao47be2472013-01-24 23:51:29 +00001252 put_online_cpus();
Jason Wangd73bcd22012-12-07 07:04:57 +00001253
1254 return err;
1255}
1256
1257static void virtnet_get_channels(struct net_device *dev,
1258 struct ethtool_channels *channels)
1259{
1260 struct virtnet_info *vi = netdev_priv(dev);
1261
1262 channels->combined_count = vi->curr_queue_pairs;
1263 channels->max_combined = vi->max_queue_pairs;
1264 channels->max_other = 0;
1265 channels->rx_count = 0;
1266 channels->tx_count = 0;
1267 channels->other_count = 0;
1268}
1269
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001270static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +00001271 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001272 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +00001273 .get_ringparam = virtnet_get_ringparam,
Jason Wangd73bcd22012-12-07 07:04:57 +00001274 .set_channels = virtnet_set_channels,
1275 .get_channels = virtnet_get_channels,
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001276};
1277
Mark McLoughlin39da5812008-11-26 13:58:11 +00001278#define MIN_MTU 68
1279#define MAX_MTU 65535
1280
1281static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1282{
1283 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1284 return -EINVAL;
1285 dev->mtu = new_mtu;
1286 return 0;
1287}
1288
Stephen Hemminger76288b42009-01-06 10:44:22 -08001289static const struct net_device_ops virtnet_netdev = {
1290 .ndo_open = virtnet_open,
1291 .ndo_stop = virtnet_close,
1292 .ndo_start_xmit = start_xmit,
1293 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -08001294 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +00001295 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001296 .ndo_change_mtu = virtnet_change_mtu,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001297 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +00001298 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1299 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -08001300#ifdef CONFIG_NET_POLL_CONTROLLER
1301 .ndo_poll_controller = virtnet_netpoll,
1302#endif
1303};
1304
Jason Wang586d17c2012-04-11 20:43:52 +00001305static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001306{
Jason Wang586d17c2012-04-11 20:43:52 +00001307 struct virtnet_info *vi =
1308 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001309 u16 v;
1310
Jason Wang586d17c2012-04-11 20:43:52 +00001311 mutex_lock(&vi->config_lock);
1312 if (!vi->config_enable)
1313 goto done;
1314
Rusty Russell855e0c52013-10-14 18:11:51 +10301315 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1316 struct virtio_net_config, status, &v) < 0)
Jason Wang586d17c2012-04-11 20:43:52 +00001317 goto done;
1318
1319 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001320 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001321 virtnet_ack_link_announce(vi);
1322 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001323
1324 /* Ignore unknown (future) status bits */
1325 v &= VIRTIO_NET_S_LINK_UP;
1326
1327 if (vi->status == v)
Jason Wang586d17c2012-04-11 20:43:52 +00001328 goto done;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001329
1330 vi->status = v;
1331
1332 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1333 netif_carrier_on(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001334 netif_tx_wake_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001335 } else {
1336 netif_carrier_off(vi->dev);
Jason Wang986a4f42012-12-07 07:04:56 +00001337 netif_tx_stop_all_queues(vi->dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001338 }
Jason Wang586d17c2012-04-11 20:43:52 +00001339done:
1340 mutex_unlock(&vi->config_lock);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001341}
1342
1343static void virtnet_config_changed(struct virtio_device *vdev)
1344{
1345 struct virtnet_info *vi = vdev->priv;
1346
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001347 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001348}
1349
Jason Wang986a4f42012-12-07 07:04:56 +00001350static void virtnet_free_queues(struct virtnet_info *vi)
1351{
Andrey Vagind4fb84e2013-12-05 18:36:21 +04001352 int i;
1353
1354 for (i = 0; i < vi->max_queue_pairs; i++)
1355 netif_napi_del(&vi->rq[i].napi);
1356
Jason Wang986a4f42012-12-07 07:04:56 +00001357 kfree(vi->rq);
1358 kfree(vi->sq);
1359}
1360
1361static void free_receive_bufs(struct virtnet_info *vi)
1362{
1363 int i;
1364
1365 for (i = 0; i < vi->max_queue_pairs; i++) {
1366 while (vi->rq[i].pages)
1367 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1368 }
1369}
1370
1371static void free_unused_bufs(struct virtnet_info *vi)
1372{
1373 void *buf;
1374 int i;
1375
1376 for (i = 0; i < vi->max_queue_pairs; i++) {
1377 struct virtqueue *vq = vi->sq[i].vq;
1378 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1379 dev_kfree_skb(buf);
1380 }
1381
1382 for (i = 0; i < vi->max_queue_pairs; i++) {
1383 struct virtqueue *vq = vi->rq[i].vq;
1384
1385 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001386 if (vi->mergeable_rx_bufs)
Michael Dalton2613af02013-10-28 15:44:18 -07001387 put_page(virt_to_head_page(buf));
Andrey Vaginfa9fac12013-12-05 18:36:20 +04001388 else if (vi->big_packets)
1389 give_pages(&vi->rq[i], buf);
Jason Wang986a4f42012-12-07 07:04:56 +00001390 else
1391 dev_kfree_skb(buf);
Jason Wang986a4f42012-12-07 07:04:56 +00001392 }
Jason Wang986a4f42012-12-07 07:04:56 +00001393 }
1394}
1395
Jason Wange9d74172012-12-07 07:04:55 +00001396static void virtnet_del_vqs(struct virtnet_info *vi)
1397{
1398 struct virtio_device *vdev = vi->vdev;
1399
Wanlong Gao8898c212013-01-24 23:51:30 +00001400 virtnet_clean_affinity(vi, -1);
Jason Wang986a4f42012-12-07 07:04:56 +00001401
Jason Wange9d74172012-12-07 07:04:55 +00001402 vdev->config->del_vqs(vdev);
Jason Wang986a4f42012-12-07 07:04:56 +00001403
1404 virtnet_free_queues(vi);
1405}
1406
1407static int virtnet_find_vqs(struct virtnet_info *vi)
1408{
1409 vq_callback_t **callbacks;
1410 struct virtqueue **vqs;
1411 int ret = -ENOMEM;
1412 int i, total_vqs;
1413 const char **names;
1414
1415 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1416 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1417 * possible control vq.
1418 */
1419 total_vqs = vi->max_queue_pairs * 2 +
1420 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1421
1422 /* Allocate space for find_vqs parameters */
1423 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1424 if (!vqs)
1425 goto err_vq;
1426 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1427 if (!callbacks)
1428 goto err_callback;
1429 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1430 if (!names)
1431 goto err_names;
1432
1433 /* Parameters for control virtqueue, if any */
1434 if (vi->has_cvq) {
1435 callbacks[total_vqs - 1] = NULL;
1436 names[total_vqs - 1] = "control";
1437 }
1438
1439 /* Allocate/initialize parameters for send/receive virtqueues */
1440 for (i = 0; i < vi->max_queue_pairs; i++) {
1441 callbacks[rxq2vq(i)] = skb_recv_done;
1442 callbacks[txq2vq(i)] = skb_xmit_done;
1443 sprintf(vi->rq[i].name, "input.%d", i);
1444 sprintf(vi->sq[i].name, "output.%d", i);
1445 names[rxq2vq(i)] = vi->rq[i].name;
1446 names[txq2vq(i)] = vi->sq[i].name;
1447 }
1448
1449 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1450 names);
1451 if (ret)
1452 goto err_find;
1453
1454 if (vi->has_cvq) {
1455 vi->cvq = vqs[total_vqs - 1];
1456 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
Patrick McHardyf6469682013-04-19 02:04:27 +00001457 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Jason Wang986a4f42012-12-07 07:04:56 +00001458 }
1459
1460 for (i = 0; i < vi->max_queue_pairs; i++) {
1461 vi->rq[i].vq = vqs[rxq2vq(i)];
1462 vi->sq[i].vq = vqs[txq2vq(i)];
1463 }
1464
1465 kfree(names);
1466 kfree(callbacks);
1467 kfree(vqs);
1468
1469 return 0;
1470
1471err_find:
1472 kfree(names);
1473err_names:
1474 kfree(callbacks);
1475err_callback:
1476 kfree(vqs);
1477err_vq:
1478 return ret;
1479}
1480
1481static int virtnet_alloc_queues(struct virtnet_info *vi)
1482{
1483 int i;
1484
1485 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1486 if (!vi->sq)
1487 goto err_sq;
1488 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
Amerigo Wang008d4272012-12-10 02:24:08 +00001489 if (!vi->rq)
Jason Wang986a4f42012-12-07 07:04:56 +00001490 goto err_rq;
1491
1492 INIT_DELAYED_WORK(&vi->refill, refill_work);
1493 for (i = 0; i < vi->max_queue_pairs; i++) {
1494 vi->rq[i].pages = NULL;
1495 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1496 napi_weight);
1497
1498 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1499 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1500 }
1501
1502 return 0;
1503
1504err_rq:
1505 kfree(vi->sq);
1506err_sq:
1507 return -ENOMEM;
Jason Wange9d74172012-12-07 07:04:55 +00001508}
1509
Amit Shah3f9c10b2011-12-22 16:58:31 +05301510static int init_vqs(struct virtnet_info *vi)
1511{
Jason Wang986a4f42012-12-07 07:04:56 +00001512 int ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301513
Jason Wang986a4f42012-12-07 07:04:56 +00001514 /* Allocate send & receive queues */
1515 ret = virtnet_alloc_queues(vi);
1516 if (ret)
1517 goto err;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301518
Jason Wang986a4f42012-12-07 07:04:56 +00001519 ret = virtnet_find_vqs(vi);
1520 if (ret)
1521 goto err_free;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301522
Wanlong Gao47be2472013-01-24 23:51:29 +00001523 get_online_cpus();
Wanlong Gao8898c212013-01-24 23:51:30 +00001524 virtnet_set_affinity(vi);
Wanlong Gao47be2472013-01-24 23:51:29 +00001525 put_online_cpus();
1526
Amit Shah3f9c10b2011-12-22 16:58:31 +05301527 return 0;
Jason Wang986a4f42012-12-07 07:04:56 +00001528
1529err_free:
1530 virtnet_free_queues(vi);
1531err:
1532 return ret;
Amit Shah3f9c10b2011-12-22 16:58:31 +05301533}
1534
Rusty Russell296f96f2007-10-22 11:03:37 +10001535static int virtnet_probe(struct virtio_device *vdev)
1536{
Jason Wang986a4f42012-12-07 07:04:56 +00001537 int i, err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001538 struct net_device *dev;
1539 struct virtnet_info *vi;
Jason Wang986a4f42012-12-07 07:04:56 +00001540 u16 max_queue_pairs;
1541
1542 /* Find if host supports multiqueue virtio_net device */
Rusty Russell855e0c52013-10-14 18:11:51 +10301543 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1544 struct virtio_net_config,
1545 max_virtqueue_pairs, &max_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00001546
1547 /* We need at least 2 queue's */
1548 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1549 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1550 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1551 max_queue_pairs = 1;
Rusty Russell296f96f2007-10-22 11:03:37 +10001552
1553 /* Allocate ourselves a network device with room for our info */
Jason Wang986a4f42012-12-07 07:04:56 +00001554 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
Rusty Russell296f96f2007-10-22 11:03:37 +10001555 if (!dev)
1556 return -ENOMEM;
1557
1558 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001559 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08001560 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10001561 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001562
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001563 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +10001564 SET_NETDEV_DEV(dev, &vdev->dev);
1565
1566 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001567 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001568 /* This opens up the world of extra features. */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001569 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1570 if (csum)
1571 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1572
1573 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1574 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05001575 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1576 }
Rusty Russell5539ae962008-05-02 21:50:46 -05001577 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001578 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1579 dev->hw_features |= NETIF_F_TSO;
1580 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1581 dev->hw_features |= NETIF_F_TSO6;
1582 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1583 dev->hw_features |= NETIF_F_TSO_ECN;
1584 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1585 dev->hw_features |= NETIF_F_UFO;
1586
1587 if (gso)
1588 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1589 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10001590 }
Thomas Huth4f491292013-08-27 17:09:02 +02001591 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1592 dev->features |= NETIF_F_RXCSUM;
Rusty Russell296f96f2007-10-22 11:03:37 +10001593
Jason Wang4fda8302013-04-10 23:32:21 +00001594 dev->vlan_features = dev->features;
1595
Rusty Russell296f96f2007-10-22 11:03:37 +10001596 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russell855e0c52013-10-14 18:11:51 +10301597 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1598 virtio_cread_bytes(vdev,
1599 offsetof(struct virtio_net_config, mac),
1600 dev->dev_addr, dev->addr_len);
1601 else
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001602 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001603
1604 /* Set up our device-specific information */
1605 vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001606 vi->dev = dev;
1607 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01001608 vdev->priv = vi;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001609 vi->stats = alloc_percpu(struct virtnet_stats);
1610 err = -ENOMEM;
1611 if (vi->stats == NULL)
1612 goto free;
1613
John Stultz827da442013-10-07 15:51:58 -07001614 for_each_possible_cpu(i) {
1615 struct virtnet_stats *virtnet_stats;
1616 virtnet_stats = per_cpu_ptr(vi->stats, i);
1617 u64_stats_init(&virtnet_stats->tx_syncp);
1618 u64_stats_init(&virtnet_stats->rx_syncp);
1619 }
1620
Jason Wang586d17c2012-04-11 20:43:52 +00001621 mutex_init(&vi->config_lock);
1622 vi->config_enable = true;
1623 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Rusty Russell296f96f2007-10-22 11:03:37 +10001624
Herbert Xu97402b92008-04-18 11:24:27 +08001625 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00001626 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1627 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1628 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
Herbert Xu97402b92008-04-18 11:24:27 +08001629 vi->big_packets = true;
1630
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001631 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1632 vi->mergeable_rx_bufs = true;
1633
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301634 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1635 vi->any_header_sg = true;
1636
Jason Wang986a4f42012-12-07 07:04:56 +00001637 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1638 vi->has_cvq = true;
1639
1640 /* Use single tx/rx queue pair as default */
1641 vi->curr_queue_pairs = 1;
1642 vi->max_queue_pairs = max_queue_pairs;
1643
1644 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
Amit Shah3f9c10b2011-12-22 16:58:31 +05301645 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001646 if (err)
Jason Wang9bb8ca82013-11-05 18:19:45 +08001647 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001648
Zhi Yong Wu0f13b66b2013-11-18 21:19:27 +08001649 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
1650 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
Jason Wang986a4f42012-12-07 07:04:56 +00001651
Rusty Russell296f96f2007-10-22 11:03:37 +10001652 err = register_netdev(dev);
1653 if (err) {
1654 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001655 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10001656 }
Rusty Russellb3369c12008-02-04 23:50:02 -05001657
1658 /* Last of all, set up some receive buffers. */
Sasha Levin55257d72013-04-29 12:00:08 +09301659 for (i = 0; i < vi->curr_queue_pairs; i++) {
Jason Wang986a4f42012-12-07 07:04:56 +00001660 try_fill_recv(&vi->rq[i], GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -05001661
Jason Wang986a4f42012-12-07 07:04:56 +00001662 /* If we didn't even get one input buffer, we're useless. */
Jason Wangbe121f42014-01-16 14:45:24 +08001663 if (vi->rq[i].vq->num_free ==
1664 virtqueue_get_vring_size(vi->rq[i].vq)) {
Jason Wang986a4f42012-12-07 07:04:56 +00001665 free_unused_bufs(vi);
1666 err = -ENOMEM;
1667 goto free_recv_bufs;
1668 }
Rusty Russellb3369c12008-02-04 23:50:02 -05001669 }
1670
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001671 vi->nb.notifier_call = &virtnet_cpu_callback;
1672 err = register_hotcpu_notifier(&vi->nb);
1673 if (err) {
1674 pr_debug("virtio_net: registering cpu notifier failed\n");
1675 goto free_recv_bufs;
1676 }
1677
Jason Wang167c25e2010-11-10 14:45:41 +00001678 /* Assume link up if device can't report link status,
1679 otherwise get link status from config. */
1680 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1681 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001682 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00001683 } else {
1684 vi->status = VIRTIO_NET_S_LINK_UP;
1685 netif_carrier_on(dev);
1686 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001687
Jason Wang986a4f42012-12-07 07:04:56 +00001688 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1689 dev->name, max_queue_pairs);
1690
Rusty Russell296f96f2007-10-22 11:03:37 +10001691 return 0;
1692
Jason Wang986a4f42012-12-07 07:04:56 +00001693free_recv_bufs:
1694 free_receive_bufs(vi);
Rusty Russellb3369c12008-02-04 23:50:02 -05001695 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001696free_vqs:
Jason Wang986a4f42012-12-07 07:04:56 +00001697 cancel_delayed_work_sync(&vi->refill);
Jason Wange9d74172012-12-07 07:04:55 +00001698 virtnet_del_vqs(vi);
Michael Dalton2613af02013-10-28 15:44:18 -07001699 if (vi->alloc_frag.page)
1700 put_page(vi->alloc_frag.page);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001701free_stats:
1702 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001703free:
1704 free_netdev(dev);
1705 return err;
1706}
1707
Amit Shah04486ed2011-12-22 16:58:32 +05301708static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10001709{
Amit Shah04486ed2011-12-22 16:58:32 +05301710 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00001711
1712 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001713 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001714
Jason Wang986a4f42012-12-07 07:04:56 +00001715 free_receive_bufs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001716
Jason Wang986a4f42012-12-07 07:04:56 +00001717 virtnet_del_vqs(vi);
Amit Shah04486ed2011-12-22 16:58:32 +05301718}
1719
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001720static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05301721{
1722 struct virtnet_info *vi = vdev->priv;
1723
Wanlong Gao8de4b2f2013-01-24 23:51:31 +00001724 unregister_hotcpu_notifier(&vi->nb);
1725
Jason Wang586d17c2012-04-11 20:43:52 +00001726 /* Prevent config work handler from accessing the device. */
1727 mutex_lock(&vi->config_lock);
1728 vi->config_enable = false;
1729 mutex_unlock(&vi->config_lock);
1730
Amit Shah04486ed2011-12-22 16:58:32 +05301731 unregister_netdev(vi->dev);
1732
1733 remove_vq_common(vi);
Michael Dalton2613af02013-10-28 15:44:18 -07001734 if (vi->alloc_frag.page)
1735 put_page(vi->alloc_frag.page);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001736
Jason Wang586d17c2012-04-11 20:43:52 +00001737 flush_work(&vi->config_work);
1738
Krishna Kumar2e66f552011-07-20 03:56:02 +00001739 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05001740 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001741}
1742
Aaron Lu89107002013-09-17 09:25:23 +09301743#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05301744static int virtnet_freeze(struct virtio_device *vdev)
1745{
1746 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001747 int i;
Amit Shah0741bcb2011-12-22 16:58:33 +05301748
Jason Wangec9debb2013-10-29 15:11:07 +08001749 unregister_hotcpu_notifier(&vi->nb);
1750
Jason Wang586d17c2012-04-11 20:43:52 +00001751 /* Prevent config work handler from accessing the device */
1752 mutex_lock(&vi->config_lock);
1753 vi->config_enable = false;
1754 mutex_unlock(&vi->config_lock);
1755
Amit Shah0741bcb2011-12-22 16:58:33 +05301756 netif_device_detach(vi->dev);
1757 cancel_delayed_work_sync(&vi->refill);
1758
1759 if (netif_running(vi->dev))
Jason Wang986a4f42012-12-07 07:04:56 +00001760 for (i = 0; i < vi->max_queue_pairs; i++) {
1761 napi_disable(&vi->rq[i].napi);
1762 netif_napi_del(&vi->rq[i].napi);
1763 }
Amit Shah0741bcb2011-12-22 16:58:33 +05301764
1765 remove_vq_common(vi);
1766
Jason Wang586d17c2012-04-11 20:43:52 +00001767 flush_work(&vi->config_work);
1768
Amit Shah0741bcb2011-12-22 16:58:33 +05301769 return 0;
1770}
1771
1772static int virtnet_restore(struct virtio_device *vdev)
1773{
1774 struct virtnet_info *vi = vdev->priv;
Jason Wang986a4f42012-12-07 07:04:56 +00001775 int err, i;
Amit Shah0741bcb2011-12-22 16:58:33 +05301776
1777 err = init_vqs(vi);
1778 if (err)
1779 return err;
1780
Jason Wang6cd4ce02013-12-30 11:34:40 +08001781 if (netif_running(vi->dev)) {
1782 for (i = 0; i < vi->curr_queue_pairs; i++)
1783 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1784 schedule_delayed_work(&vi->refill, 0);
1785
Jason Wang986a4f42012-12-07 07:04:56 +00001786 for (i = 0; i < vi->max_queue_pairs; i++)
1787 virtnet_napi_enable(&vi->rq[i]);
Jason Wang6cd4ce02013-12-30 11:34:40 +08001788 }
Amit Shah0741bcb2011-12-22 16:58:33 +05301789
1790 netif_device_attach(vi->dev);
1791
Jason Wang586d17c2012-04-11 20:43:52 +00001792 mutex_lock(&vi->config_lock);
1793 vi->config_enable = true;
1794 mutex_unlock(&vi->config_lock);
1795
Jason Wang35ed1592013-10-15 11:18:59 +08001796 rtnl_lock();
Jason Wang986a4f42012-12-07 07:04:56 +00001797 virtnet_set_queues(vi, vi->curr_queue_pairs);
Jason Wang35ed1592013-10-15 11:18:59 +08001798 rtnl_unlock();
Jason Wang986a4f42012-12-07 07:04:56 +00001799
Jason Wangec9debb2013-10-29 15:11:07 +08001800 err = register_hotcpu_notifier(&vi->nb);
1801 if (err)
1802 return err;
1803
Amit Shah0741bcb2011-12-22 16:58:33 +05301804 return 0;
1805}
1806#endif
1807
Rusty Russell296f96f2007-10-22 11:03:37 +10001808static struct virtio_device_id id_table[] = {
1809 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1810 { 0 },
1811};
1812
Rusty Russellc45a6812008-05-02 21:50:50 -05001813static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +10001814 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1815 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -05001816 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +08001817 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +00001818 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +00001819 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +00001820 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Jason Wang986a4f42012-12-07 07:04:56 +00001821 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
Amos Kong7e58d5a2013-01-21 01:17:23 +00001822 VIRTIO_NET_F_CTRL_MAC_ADDR,
Michael S. Tsirkine7428e92013-07-25 10:20:23 +09301823 VIRTIO_F_ANY_LAYOUT,
Rusty Russellc45a6812008-05-02 21:50:50 -05001824};
1825
Uwe Kleine-König22402522009-11-05 01:32:44 -08001826static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05001827 .feature_table = features,
1828 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +10001829 .driver.name = KBUILD_MODNAME,
1830 .driver.owner = THIS_MODULE,
1831 .id_table = id_table,
1832 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001833 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001834 .config_changed = virtnet_config_changed,
Aaron Lu89107002013-09-17 09:25:23 +09301835#ifdef CONFIG_PM_SLEEP
Amit Shah0741bcb2011-12-22 16:58:33 +05301836 .freeze = virtnet_freeze,
1837 .restore = virtnet_restore,
1838#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10001839};
1840
Rusty Russellb2a17022013-02-13 16:59:28 +10301841module_virtio_driver(virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001842
1843MODULE_DEVICE_TABLE(virtio, id_table);
1844MODULE_DESCRIPTION("Virtio network driver");
1845MODULE_LICENSE("GPL");