blob: 22a8ca5d67d5e20e95003fd2eaa389a8af1e137b [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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19//#define DEBUG
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080022#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100023#include <linux/module.h>
24#include <linux/virtio.h>
Fernando Luis Vazquez Cao3ca4f5c2009-07-31 15:25:56 +090025#include <linux/virtio_ids.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100026#include <linux/virtio_net.h>
27#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100029
Dor Laor6c0cd7c2007-12-16 15:19:43 +020030static int napi_weight = 128;
31module_param(napi_weight, int, 0444);
32
Rusty Russell34a48572008-02-04 23:50:02 -050033static int csum = 1, gso = 1;
34module_param(csum, bool, 0444);
35module_param(gso, bool, 0444);
36
Rusty Russell296f96f2007-10-22 11:03:37 +100037/* FIXME: MTU in config. */
Alex Williamsone918085a2009-01-25 18:06:26 -080038#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080039#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100040
Alex Williamsonf565a7c2009-02-04 09:02:45 +000041#define VIRTNET_SEND_COMMAND_SG_MAX 2
Alex Williamson2a41f712009-02-04 09:02:34 +000042
Rusty Russell296f96f2007-10-22 11:03:37 +100043struct virtnet_info
44{
45 struct virtio_device *vdev;
Alex Williamson2a41f712009-02-04 09:02:34 +000046 struct virtqueue *rvq, *svq, *cvq;
Rusty Russell296f96f2007-10-22 11:03:37 +100047 struct net_device *dev;
48 struct napi_struct napi;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -080049 unsigned int status;
Rusty Russell296f96f2007-10-22 11:03:37 +100050
51 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
53
Herbert Xu97402b92008-04-18 11:24:27 +080054 /* I like... big packets and I cannot lie! */
55 bool big_packets;
56
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080057 /* Host will merge rx buffers for big packets (shake it! shake it!) */
58 bool mergeable_rx_bufs;
59
Rusty Russell296f96f2007-10-22 11:03:37 +100060 /* Receive & send queues. */
61 struct sk_buff_head recv;
62 struct sk_buff_head send;
Rusty Russellfb6813f2008-07-25 12:06:01 -050063
Rusty Russell3161e452009-08-26 12:22:32 -070064 /* Work struct for refilling if we run low on memory. */
65 struct delayed_work refill;
66
Rusty Russellfb6813f2008-07-25 12:06:01 -050067 /* Chain pages by the private ptr. */
68 struct page *pages;
Rusty Russell296f96f2007-10-22 11:03:37 +100069};
70
Rusty Russellb3f24692009-09-24 09:59:19 -060071struct skb_vnet_hdr {
72 union {
73 struct virtio_net_hdr hdr;
74 struct virtio_net_hdr_mrg_rxbuf mhdr;
75 };
Rusty Russell48925e32009-09-24 09:59:20 -060076 unsigned int num_sg;
Rusty Russellb3f24692009-09-24 09:59:19 -060077};
78
79static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +100080{
Rusty Russellb3f24692009-09-24 09:59:19 -060081 return (struct skb_vnet_hdr *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +100082}
83
Rusty Russellfb6813f2008-07-25 12:06:01 -050084static void give_a_page(struct virtnet_info *vi, struct page *page)
85{
86 page->private = (unsigned long)vi->pages;
87 vi->pages = page;
88}
89
Mark McLoughlin0a888fd2008-11-16 22:39:18 -080090static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
91{
92 unsigned int i;
93
94 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
95 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
96 skb_shinfo(skb)->nr_frags = 0;
97 skb->data_len = 0;
98}
99
Rusty Russellfb6813f2008-07-25 12:06:01 -0500100static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
101{
102 struct page *p = vi->pages;
103
104 if (p)
105 vi->pages = (struct page *)p->private;
106 else
107 p = alloc_page(gfp_mask);
108 return p;
109}
110
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500111static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000112{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500113 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000114
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500115 /* Suppress further interrupts. */
116 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000117
Rusty Russell363f1512008-06-08 20:51:55 +1000118 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000119 netif_wake_queue(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000120}
121
122static void receive_skb(struct net_device *dev, struct sk_buff *skb,
123 unsigned len)
124{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800125 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russellb3f24692009-09-24 09:59:19 -0600126 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800127 int err;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800128 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000129
130 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
131 pr_debug("%s: short packet %i\n", dev->name, len);
132 dev->stats.rx_length_errors++;
133 goto drop;
134 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000135
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800136 if (vi->mergeable_rx_bufs) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800137 unsigned int copy;
138 char *p = page_address(skb_shinfo(skb)->frags[0].page);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500139
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800140 if (len > PAGE_SIZE)
141 len = PAGE_SIZE;
142 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
143
Rusty Russellb3f24692009-09-24 09:59:19 -0600144 memcpy(&hdr->mhdr, p, sizeof(hdr->mhdr));
145 p += sizeof(hdr->mhdr);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800146
147 copy = len;
148 if (copy > skb_tailroom(skb))
149 copy = skb_tailroom(skb);
150
151 memcpy(skb_put(skb, copy), p, copy);
152
153 len -= copy;
154
155 if (!len) {
156 give_a_page(vi, skb_shinfo(skb)->frags[0].page);
157 skb_shinfo(skb)->nr_frags--;
158 } else {
159 skb_shinfo(skb)->frags[0].page_offset +=
Rusty Russellb3f24692009-09-24 09:59:19 -0600160 sizeof(hdr->mhdr) + copy;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800161 skb_shinfo(skb)->frags[0].size = len;
162 skb->data_len += len;
163 skb->len += len;
164 }
165
Rusty Russellb3f24692009-09-24 09:59:19 -0600166 while (--hdr->mhdr.num_buffers) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800167 struct sk_buff *nskb;
168
169 i = skb_shinfo(skb)->nr_frags;
170 if (i >= MAX_SKB_FRAGS) {
171 pr_debug("%s: packet too long %d\n", dev->name,
172 len);
173 dev->stats.rx_length_errors++;
174 goto drop;
175 }
176
177 nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
178 if (!nskb) {
179 pr_debug("%s: rx error: %d buffers missing\n",
Rusty Russellb3f24692009-09-24 09:59:19 -0600180 dev->name, hdr->mhdr.num_buffers);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800181 dev->stats.rx_length_errors++;
182 goto drop;
183 }
184
185 __skb_unlink(nskb, &vi->recv);
186 vi->num--;
187
188 skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
189 skb_shinfo(nskb)->nr_frags = 0;
190 kfree_skb(nskb);
191
192 if (len > PAGE_SIZE)
193 len = PAGE_SIZE;
194
195 skb_shinfo(skb)->frags[i].size = len;
196 skb_shinfo(skb)->nr_frags++;
197 skb->data_len += len;
198 skb->len += len;
199 }
200 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600201 len -= sizeof(hdr->hdr);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800202
203 if (len <= MAX_PACKET_LEN)
204 trim_pages(vi, skb);
205
206 err = pskb_trim(skb, len);
207 if (err) {
208 pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
209 len, err);
210 dev->stats.rx_dropped++;
211 goto drop;
212 }
Herbert Xu97402b92008-04-18 11:24:27 +0800213 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800214
Herbert Xu97402b92008-04-18 11:24:27 +0800215 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000216 dev->stats.rx_bytes += skb->len;
217 dev->stats.rx_packets++;
218
Rusty Russellb3f24692009-09-24 09:59:19 -0600219 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000220 pr_debug("Needs csum!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600221 if (!skb_partial_csum_set(skb,
222 hdr->hdr.csum_start,
223 hdr->hdr.csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000224 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000225 }
226
Mark McLoughlin23cde762008-06-08 20:49:00 +1000227 skb->protocol = eth_type_trans(skb, dev);
228 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
229 ntohs(skb->protocol), skb->len, skb->pkt_type);
230
Rusty Russellb3f24692009-09-24 09:59:19 -0600231 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000232 pr_debug("GSO!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600233 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000234 case VIRTIO_NET_HDR_GSO_TCPV4:
235 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
236 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000237 case VIRTIO_NET_HDR_GSO_UDP:
238 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
239 break;
240 case VIRTIO_NET_HDR_GSO_TCPV6:
241 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
242 break;
243 default:
244 if (net_ratelimit())
245 printk(KERN_WARNING "%s: bad gso type %u.\n",
Rusty Russellb3f24692009-09-24 09:59:19 -0600246 dev->name, hdr->hdr.gso_type);
Rusty Russell296f96f2007-10-22 11:03:37 +1000247 goto frame_err;
248 }
249
Rusty Russellb3f24692009-09-24 09:59:19 -0600250 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Rusty Russell34a48572008-02-04 23:50:02 -0500251 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
252
Rusty Russellb3f24692009-09-24 09:59:19 -0600253 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
Rusty Russell296f96f2007-10-22 11:03:37 +1000254 if (skb_shinfo(skb)->gso_size == 0) {
255 if (net_ratelimit())
256 printk(KERN_WARNING "%s: zero gso size.\n",
257 dev->name);
258 goto frame_err;
259 }
260
261 /* Header must be checked, and gso_segs computed. */
262 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
263 skb_shinfo(skb)->gso_segs = 0;
264 }
265
266 netif_receive_skb(skb);
267 return;
268
269frame_err:
270 dev->stats.rx_frame_errors++;
271drop:
272 dev_kfree_skb(skb);
273}
274
Rusty Russell3161e452009-08-26 12:22:32 -0700275static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000276{
277 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500278 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800279 int num, err, i;
Rusty Russell3161e452009-08-26 12:22:32 -0700280 bool oom = false;
Rusty Russell296f96f2007-10-22 11:03:37 +1000281
Rusty Russell05271682008-05-02 21:50:45 -0500282 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Amit Shah0aea51c2009-08-26 14:58:28 +0530283 do {
Rusty Russellb3f24692009-09-24 09:59:19 -0600284 struct skb_vnet_hdr *hdr;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800285
Eric Dumazet89d71a62009-10-13 05:34:20 +0000286 skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN);
Rusty Russell3161e452009-08-26 12:22:32 -0700287 if (unlikely(!skb)) {
288 oom = true;
Rusty Russell296f96f2007-10-22 11:03:37 +1000289 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700290 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000291
292 skb_put(skb, MAX_PACKET_LEN);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800293
294 hdr = skb_vnet_hdr(skb);
Rusty Russellb3f24692009-09-24 09:59:19 -0600295 sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800296
297 if (vi->big_packets) {
298 for (i = 0; i < MAX_SKB_FRAGS; i++) {
299 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700300 f->page = get_a_page(vi, gfp);
Herbert Xu97402b92008-04-18 11:24:27 +0800301 if (!f->page)
302 break;
303
304 f->page_offset = 0;
305 f->size = PAGE_SIZE;
306
307 skb->data_len += PAGE_SIZE;
308 skb->len += PAGE_SIZE;
309
310 skb_shinfo(skb)->nr_frags++;
311 }
312 }
313
Rusty Russell296f96f2007-10-22 11:03:37 +1000314 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
315 skb_queue_head(&vi->recv, skb);
316
317 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600318 if (err < 0) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000319 skb_unlink(skb, &vi->recv);
Mark McLoughlin0a888fd2008-11-16 22:39:18 -0800320 trim_pages(vi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000321 kfree_skb(skb);
322 break;
323 }
324 vi->num++;
Amit Shah0aea51c2009-08-26 14:58:28 +0530325 } while (err >= num);
Rusty Russell296f96f2007-10-22 11:03:37 +1000326 if (unlikely(vi->num > vi->max))
327 vi->max = vi->num;
328 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700329 return !oom;
Rusty Russell296f96f2007-10-22 11:03:37 +1000330}
331
Rusty Russell3161e452009-08-26 12:22:32 -0700332/* Returns false if we couldn't fill entirely (OOM). */
333static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800334{
335 struct sk_buff *skb;
336 struct scatterlist sg[1];
337 int err;
Rusty Russell3161e452009-08-26 12:22:32 -0700338 bool oom = false;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800339
Rusty Russell3161e452009-08-26 12:22:32 -0700340 if (!vi->mergeable_rx_bufs)
341 return try_fill_recv_maxbufs(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800342
Amit Shah0aea51c2009-08-26 14:58:28 +0530343 do {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800344 skb_frag_t *f;
345
Eric Dumazet89d71a62009-10-13 05:34:20 +0000346 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
Rusty Russell3161e452009-08-26 12:22:32 -0700347 if (unlikely(!skb)) {
348 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800349 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700350 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800351
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800352 f = &skb_shinfo(skb)->frags[0];
Rusty Russell3161e452009-08-26 12:22:32 -0700353 f->page = get_a_page(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800354 if (!f->page) {
Rusty Russell3161e452009-08-26 12:22:32 -0700355 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800356 kfree_skb(skb);
357 break;
358 }
359
360 f->page_offset = 0;
361 f->size = PAGE_SIZE;
362
363 skb_shinfo(skb)->nr_frags++;
364
365 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
366 skb_queue_head(&vi->recv, skb);
367
368 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600369 if (err < 0) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800370 skb_unlink(skb, &vi->recv);
371 kfree_skb(skb);
372 break;
373 }
374 vi->num++;
Amit Shah0aea51c2009-08-26 14:58:28 +0530375 } while (err > 0);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800376 if (unlikely(vi->num > vi->max))
377 vi->max = vi->num;
378 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700379 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800380}
381
Rusty Russell18445c42008-02-04 23:49:57 -0500382static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000383{
384 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500385 /* Schedule NAPI, Suppress further interrupts if successful. */
Ben Hutchings288379f2009-01-19 16:43:59 -0800386 if (napi_schedule_prep(&vi->napi)) {
Rusty Russell18445c42008-02-04 23:49:57 -0500387 rvq->vq_ops->disable_cb(rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800388 __napi_schedule(&vi->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500389 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000390}
391
Rusty Russell3161e452009-08-26 12:22:32 -0700392static void refill_work(struct work_struct *work)
393{
394 struct virtnet_info *vi;
395 bool still_empty;
396
397 vi = container_of(work, struct virtnet_info, refill.work);
398 napi_disable(&vi->napi);
399 try_fill_recv(vi, GFP_KERNEL);
400 still_empty = (vi->num == 0);
401 napi_enable(&vi->napi);
402
403 /* In theory, this can happen: if we don't get any buffers in
404 * we will *never* try to fill again. */
405 if (still_empty)
406 schedule_delayed_work(&vi->refill, HZ/2);
407}
408
Rusty Russell296f96f2007-10-22 11:03:37 +1000409static int virtnet_poll(struct napi_struct *napi, int budget)
410{
411 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
412 struct sk_buff *skb = NULL;
413 unsigned int len, received = 0;
414
415again:
416 while (received < budget &&
417 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
418 __skb_unlink(skb, &vi->recv);
419 receive_skb(vi->dev, skb, len);
420 vi->num--;
421 received++;
422 }
423
Rusty Russell3161e452009-08-26 12:22:32 -0700424 if (vi->num < vi->max / 2) {
425 if (!try_fill_recv(vi, GFP_ATOMIC))
426 schedule_delayed_work(&vi->refill, 0);
427 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000428
Rusty Russell8329d982007-11-19 11:20:43 -0500429 /* Out of packets? */
430 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800431 napi_complete(napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500432 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100433 && napi_schedule_prep(napi)) {
434 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800435 __napi_schedule(napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000436 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100437 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000438 }
439
440 return received;
441}
442
Rusty Russell48925e32009-09-24 09:59:20 -0600443static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +1000444{
445 struct sk_buff *skb;
Rusty Russell48925e32009-09-24 09:59:20 -0600446 unsigned int len, tot_sgs = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000447
448 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
449 pr_debug("Sent skb %p\n", skb);
450 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500451 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000452 vi->dev->stats.tx_packets++;
Rusty Russell48925e32009-09-24 09:59:20 -0600453 tot_sgs += skb_vnet_hdr(skb)->num_sg;
Eric Dumazeted79bab2009-10-14 14:36:43 +0000454 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000455 }
Rusty Russell48925e32009-09-24 09:59:20 -0600456 return tot_sgs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000457}
458
Rusty Russell99ffc692008-05-02 21:50:46 -0500459static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000460{
Rusty Russell05271682008-05-02 21:50:45 -0500461 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russellb3f24692009-09-24 09:59:19 -0600462 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000463 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000464
Rusty Russell05271682008-05-02 21:50:45 -0500465 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100466
Johannes Berge1749612008-10-27 15:59:26 -0700467 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000468
Rusty Russell296f96f2007-10-22 11:03:37 +1000469 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600470 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
471 hdr->hdr.csum_start = skb->csum_start - skb_headroom(skb);
472 hdr->hdr.csum_offset = skb->csum_offset;
Rusty Russell296f96f2007-10-22 11:03:37 +1000473 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600474 hdr->hdr.flags = 0;
475 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000476 }
477
478 if (skb_is_gso(skb)) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600479 hdr->hdr.hdr_len = skb_headlen(skb);
480 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500481 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russellb3f24692009-09-24 09:59:19 -0600482 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
Rusty Russell296f96f2007-10-22 11:03:37 +1000483 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Rusty Russellb3f24692009-09-24 09:59:19 -0600484 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Rusty Russell296f96f2007-10-22 11:03:37 +1000485 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
Rusty Russellb3f24692009-09-24 09:59:19 -0600486 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Rusty Russell296f96f2007-10-22 11:03:37 +1000487 else
488 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500489 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
Rusty Russellb3f24692009-09-24 09:59:19 -0600490 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000491 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600492 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
493 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000494 }
495
Rusty Russellb3f24692009-09-24 09:59:19 -0600496 hdr->mhdr.num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800497
498 /* Encode metadata header at front. */
499 if (vi->mergeable_rx_bufs)
Rusty Russellb3f24692009-09-24 09:59:19 -0600500 sg_set_buf(sg, &hdr->mhdr, sizeof(hdr->mhdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800501 else
Rusty Russellb3f24692009-09-24 09:59:19 -0600502 sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800503
Rusty Russell48925e32009-09-24 09:59:20 -0600504 hdr->num_sg = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
505 return vi->svq->vq_ops->add_buf(vi->svq, sg, hdr->num_sg, 0, skb);
Rusty Russell11a3a152008-05-26 17:48:13 +1000506}
507
Stephen Hemminger424efe92009-08-31 19:50:51 +0000508static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -0500509{
510 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell48925e32009-09-24 09:59:20 -0600511 int capacity;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500512
513again:
514 /* Free up any pending old buffers before queueing new ones. */
515 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500516
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700517 /* Try to transmit */
Rusty Russell48925e32009-09-24 09:59:20 -0600518 capacity = xmit_skb(vi, skb);
519
520 /* This can happen with OOM and indirect buffers. */
521 if (unlikely(capacity < 0)) {
522 netif_stop_queue(dev);
523 dev_warn(&dev->dev, "Unexpected full queue\n");
524 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
525 vi->svq->vq_ops->disable_cb(vi->svq);
526 netif_start_queue(dev);
527 goto again;
528 }
529 return NETDEV_TX_BUSY;
Rusty Russell99ffc692008-05-02 21:50:46 -0500530 }
Rusty Russell48925e32009-09-24 09:59:20 -0600531 vi->svq->vq_ops->kick(vi->svq);
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700532
533 /*
534 * Put new one in send queue. You'd expect we'd need this before
535 * xmit_skb calls add_buf(), since the callback can be triggered
536 * immediately after that. But since the callback just triggers
537 * another call back here, normal network xmit locking prevents the
538 * race.
539 */
540 __skb_queue_head(&vi->send, skb);
541
Rusty Russell48925e32009-09-24 09:59:20 -0600542 /* Don't wait up for transmitted skbs to be freed. */
543 skb_orphan(skb);
544 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -0500545
Rusty Russell48925e32009-09-24 09:59:20 -0600546 /* Apparently nice girls don't return TX_BUSY; stop the queue
547 * before it gets out of hand. Naturally, this wastes entries. */
548 if (capacity < 2+MAX_SKB_FRAGS) {
549 netif_stop_queue(dev);
550 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
551 /* More just got used, free them then recheck. */
552 capacity += free_old_xmit_skbs(vi);
553 if (capacity >= 2+MAX_SKB_FRAGS) {
554 netif_start_queue(dev);
555 vi->svq->vq_ops->disable_cb(vi->svq);
556 }
557 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500558 }
Rusty Russell48925e32009-09-24 09:59:20 -0600559
560 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +1000561}
562
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800563static int virtnet_set_mac_address(struct net_device *dev, void *p)
564{
565 struct virtnet_info *vi = netdev_priv(dev);
566 struct virtio_device *vdev = vi->vdev;
567 int ret;
568
569 ret = eth_mac_addr(dev, p);
570 if (ret)
571 return ret;
572
Alex Williamson62994b22009-04-04 16:40:19 -0700573 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
574 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
575 dev->dev_addr, dev->addr_len);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800576
577 return 0;
578}
579
Amit Shahda74e892008-02-29 16:24:50 +0530580#ifdef CONFIG_NET_POLL_CONTROLLER
581static void virtnet_netpoll(struct net_device *dev)
582{
583 struct virtnet_info *vi = netdev_priv(dev);
584
585 napi_schedule(&vi->napi);
586}
587#endif
588
Rusty Russell296f96f2007-10-22 11:03:37 +1000589static int virtnet_open(struct net_device *dev)
590{
591 struct virtnet_info *vi = netdev_priv(dev);
592
Rusty Russell296f96f2007-10-22 11:03:37 +1000593 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500594
595 /* If all buffers were filled by other side before we napi_enabled, we
596 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100597 * now. virtnet_poll wants re-enable the queue, so we disable here.
598 * We synchronize against interrupts via NAPI_STATE_SCHED */
Ben Hutchings288379f2009-01-19 16:43:59 -0800599 if (napi_schedule_prep(&vi->napi)) {
Christian Borntraeger370076d2008-02-06 08:50:11 +0100600 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800601 __napi_schedule(&vi->napi);
Christian Borntraeger370076d2008-02-06 08:50:11 +0100602 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000603 return 0;
604}
605
Alex Williamson2a41f712009-02-04 09:02:34 +0000606/*
607 * Send command via the control virtqueue and check status. Commands
608 * supported by the hypervisor, as indicated by feature bits, should
609 * never fail unless improperly formated.
610 */
611static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
612 struct scatterlist *data, int out, int in)
613{
Alex Williamson23e258e2009-05-01 17:27:56 +0000614 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
Alex Williamson2a41f712009-02-04 09:02:34 +0000615 struct virtio_net_ctrl_hdr ctrl;
616 virtio_net_ctrl_ack status = ~0;
617 unsigned int tmp;
Alex Williamson23e258e2009-05-01 17:27:56 +0000618 int i;
Alex Williamson2a41f712009-02-04 09:02:34 +0000619
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000620 /* Caller should know better */
621 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
622 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
Alex Williamson2a41f712009-02-04 09:02:34 +0000623
624 out++; /* Add header */
625 in++; /* Add return status */
626
627 ctrl.class = class;
628 ctrl.cmd = cmd;
629
630 sg_init_table(sg, out + in);
631
632 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
Alex Williamson23e258e2009-05-01 17:27:56 +0000633 for_each_sg(data, s, out + in - 2, i)
634 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
Alex Williamson2a41f712009-02-04 09:02:34 +0000635 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
636
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600637 BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0);
Alex Williamson2a41f712009-02-04 09:02:34 +0000638
639 vi->cvq->vq_ops->kick(vi->cvq);
640
641 /*
642 * Spin for a response, the kick causes an ioport write, trapping
643 * into the hypervisor, so the request should be handled immediately.
644 */
645 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
646 cpu_relax();
647
648 return status == VIRTIO_NET_OK;
649}
650
Rusty Russell296f96f2007-10-22 11:03:37 +1000651static int virtnet_close(struct net_device *dev)
652{
653 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000654
655 napi_disable(&vi->napi);
656
Rusty Russell296f96f2007-10-22 11:03:37 +1000657 return 0;
658}
659
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800660static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
661{
662 struct virtnet_info *vi = netdev_priv(dev);
663 struct virtio_device *vdev = vi->vdev;
664
665 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
666 return -ENOSYS;
667
668 return ethtool_op_set_tx_hw_csum(dev, data);
669}
670
Alex Williamson2af76982009-02-04 09:02:40 +0000671static void virtnet_set_rx_mode(struct net_device *dev)
672{
673 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000674 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +0000675 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000676 struct virtio_net_ctrl_mac *mac_data;
677 struct dev_addr_list *addr;
Jiri Pirkoccffad22009-05-22 23:22:17 +0000678 struct netdev_hw_addr *ha;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000679 void *buf;
680 int i;
Alex Williamson2af76982009-02-04 09:02:40 +0000681
682 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
683 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
684 return;
685
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000686 promisc = ((dev->flags & IFF_PROMISC) != 0);
687 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +0000688
Alex Williamson23e258e2009-05-01 17:27:56 +0000689 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +0000690
691 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
692 VIRTIO_NET_CTRL_RX_PROMISC,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000693 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000694 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
695 promisc ? "en" : "dis");
696
Alex Williamson23e258e2009-05-01 17:27:56 +0000697 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +0000698
699 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
700 VIRTIO_NET_CTRL_RX_ALLMULTI,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000701 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000702 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
703 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000704
705 /* MAC filter - use one buffer for both lists */
Jiri Pirko31278e72009-06-17 01:12:19 +0000706 mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000707 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
708 if (!buf) {
709 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
710 return;
711 }
712
Alex Williamson23e258e2009-05-01 17:27:56 +0000713 sg_init_table(sg, 2);
714
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000715 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko31278e72009-06-17 01:12:19 +0000716 mac_data->entries = dev->uc.count;
Jiri Pirkoccffad22009-05-22 23:22:17 +0000717 i = 0;
Jiri Pirko31278e72009-06-17 01:12:19 +0000718 list_for_each_entry(ha, &dev->uc.list, list)
Jiri Pirkoccffad22009-05-22 23:22:17 +0000719 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000720
721 sg_set_buf(&sg[0], mac_data,
Jiri Pirko31278e72009-06-17 01:12:19 +0000722 sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000723
724 /* multicast list and count fill the end */
Jiri Pirko31278e72009-06-17 01:12:19 +0000725 mac_data = (void *)&mac_data->macs[dev->uc.count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000726
727 mac_data->entries = dev->mc_count;
728 addr = dev->mc_list;
729 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
730 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
731
732 sg_set_buf(&sg[1], mac_data,
733 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
734
735 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
736 VIRTIO_NET_CTRL_MAC_TABLE_SET,
737 sg, 2, 0))
738 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
739
740 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +0000741}
742
Alex Williamson1824a982009-05-01 17:31:10 +0000743static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000744{
745 struct virtnet_info *vi = netdev_priv(dev);
746 struct scatterlist sg;
747
Alex Williamson23e258e2009-05-01 17:27:56 +0000748 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000749
750 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
751 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
752 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
753}
754
Alex Williamson1824a982009-05-01 17:31:10 +0000755static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000756{
757 struct virtnet_info *vi = netdev_priv(dev);
758 struct scatterlist sg;
759
Alex Williamson23e258e2009-05-01 17:27:56 +0000760 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000761
762 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
763 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
764 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
765}
766
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700767static const struct ethtool_ops virtnet_ethtool_ops = {
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800768 .set_tx_csum = virtnet_set_tx_csum,
769 .set_sg = ethtool_op_set_sg,
Mark McLoughlin0276b492008-11-16 22:40:36 -0800770 .set_tso = ethtool_op_set_tso,
Sridhar Samudrala5c516752009-07-14 14:21:02 +0000771 .set_ufo = ethtool_op_set_ufo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800772 .get_link = ethtool_op_get_link,
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800773};
774
Mark McLoughlin39da5812008-11-26 13:58:11 +0000775#define MIN_MTU 68
776#define MAX_MTU 65535
777
778static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
779{
780 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
781 return -EINVAL;
782 dev->mtu = new_mtu;
783 return 0;
784}
785
Stephen Hemminger76288b42009-01-06 10:44:22 -0800786static const struct net_device_ops virtnet_netdev = {
787 .ndo_open = virtnet_open,
788 .ndo_stop = virtnet_close,
789 .ndo_start_xmit = start_xmit,
790 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800791 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +0000792 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800793 .ndo_change_mtu = virtnet_change_mtu,
Alex Williamson1824a982009-05-01 17:31:10 +0000794 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
795 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800796#ifdef CONFIG_NET_POLL_CONTROLLER
797 .ndo_poll_controller = virtnet_netpoll,
798#endif
799};
800
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800801static void virtnet_update_status(struct virtnet_info *vi)
802{
803 u16 v;
804
805 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
806 return;
807
808 vi->vdev->config->get(vi->vdev,
809 offsetof(struct virtio_net_config, status),
810 &v, sizeof(v));
811
812 /* Ignore unknown (future) status bits */
813 v &= VIRTIO_NET_S_LINK_UP;
814
815 if (vi->status == v)
816 return;
817
818 vi->status = v;
819
820 if (vi->status & VIRTIO_NET_S_LINK_UP) {
821 netif_carrier_on(vi->dev);
822 netif_wake_queue(vi->dev);
823 } else {
824 netif_carrier_off(vi->dev);
825 netif_stop_queue(vi->dev);
826 }
827}
828
829static void virtnet_config_changed(struct virtio_device *vdev)
830{
831 struct virtnet_info *vi = vdev->priv;
832
833 virtnet_update_status(vi);
834}
835
Rusty Russell296f96f2007-10-22 11:03:37 +1000836static int virtnet_probe(struct virtio_device *vdev)
837{
838 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000839 struct net_device *dev;
840 struct virtnet_info *vi;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600841 struct virtqueue *vqs[3];
842 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
843 const char *names[] = { "input", "output", "control" };
844 int nvqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000845
846 /* Allocate ourselves a network device with room for our info */
847 dev = alloc_etherdev(sizeof(struct virtnet_info));
848 if (!dev)
849 return -ENOMEM;
850
851 /* Set up network device as normal. */
Stephen Hemminger76288b42009-01-06 10:44:22 -0800852 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +1000853 dev->features = NETIF_F_HIGHDMA;
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800854 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000855 SET_NETDEV_DEV(dev, &vdev->dev);
856
857 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500858 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000859 /* This opens up the world of extra features. */
860 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500861 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500862 dev->features |= NETIF_F_TSO | NETIF_F_UFO
863 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
864 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500865 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500866 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500867 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500868 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500869 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500870 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500871 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500872 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500873 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000874 }
875
876 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500877 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500878 vdev->config->get(vdev,
879 offsetof(struct virtio_net_config, mac),
880 dev->dev_addr, dev->addr_len);
Alex Williamson62994b22009-04-04 16:40:19 -0700881 } else
Rusty Russell296f96f2007-10-22 11:03:37 +1000882 random_ether_addr(dev->dev_addr);
883
884 /* Set up our device-specific information */
885 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200886 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000887 vi->dev = dev;
888 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100889 vdev->priv = vi;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500890 vi->pages = NULL;
Rusty Russell3161e452009-08-26 12:22:32 -0700891 INIT_DELAYED_WORK(&vi->refill, refill_work);
Rusty Russell296f96f2007-10-22 11:03:37 +1000892
Herbert Xu97402b92008-04-18 11:24:27 +0800893 /* If we can receive ANY GSO packets, we must allocate large ones. */
894 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
895 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
896 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
897 vi->big_packets = true;
898
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800899 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
900 vi->mergeable_rx_bufs = true;
901
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600902 /* We expect two virtqueues, receive then send,
903 * and optionally control. */
904 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
Rusty Russell296f96f2007-10-22 11:03:37 +1000905
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600906 err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
907 if (err)
908 goto free;
909
910 vi->rvq = vqs[0];
911 vi->svq = vqs[1];
Rusty Russell296f96f2007-10-22 11:03:37 +1000912
Alex Williamson2a41f712009-02-04 09:02:34 +0000913 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600914 vi->cvq = vqs[2];
Alex Williamson0bde95692009-02-04 09:02:50 +0000915
916 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
917 dev->features |= NETIF_F_HW_VLAN_FILTER;
Alex Williamson2a41f712009-02-04 09:02:34 +0000918 }
919
Rusty Russell296f96f2007-10-22 11:03:37 +1000920 /* Initialize our empty receive and send queues. */
921 skb_queue_head_init(&vi->recv);
922 skb_queue_head_init(&vi->send);
923
924 err = register_netdev(dev);
925 if (err) {
926 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600927 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000928 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500929
930 /* Last of all, set up some receive buffers. */
Rusty Russell3161e452009-08-26 12:22:32 -0700931 try_fill_recv(vi, GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -0500932
933 /* If we didn't even get one input buffer, we're useless. */
934 if (vi->num == 0) {
935 err = -ENOMEM;
936 goto unregister;
937 }
938
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800939 vi->status = VIRTIO_NET_S_LINK_UP;
940 virtnet_update_status(vi);
Pantelis Koukousoulas47832562009-03-18 18:40:02 -0700941 netif_carrier_on(dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800942
Rusty Russell296f96f2007-10-22 11:03:37 +1000943 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000944 return 0;
945
Rusty Russellb3369c12008-02-04 23:50:02 -0500946unregister:
947 unregister_netdev(dev);
Rusty Russell3161e452009-08-26 12:22:32 -0700948 cancel_delayed_work_sync(&vi->refill);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600949free_vqs:
950 vdev->config->del_vqs(vdev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000951free:
952 free_netdev(dev);
953 return err;
954}
955
Uwe Kleine-König3d1285b2009-09-30 22:28:34 +0000956static void __devexit virtnet_remove(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +1000957{
Rusty Russell74b25532007-11-19 11:20:42 -0500958 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500959 struct sk_buff *skb;
960
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500961 /* Stop all the virtqueues. */
962 vdev->config->reset(vdev);
963
Rusty Russellb3369c12008-02-04 23:50:02 -0500964 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500965 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
966 kfree_skb(skb);
967 vi->num--;
968 }
Wang Chen288369c2008-05-22 18:07:43 +0800969 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500970
971 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500972
Rusty Russell74b25532007-11-19 11:20:42 -0500973 unregister_netdev(vi->dev);
Rusty Russell3161e452009-08-26 12:22:32 -0700974 cancel_delayed_work_sync(&vi->refill);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500975
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600976 vdev->config->del_vqs(vi->vdev);
977
Rusty Russellfb6813f2008-07-25 12:06:01 -0500978 while (vi->pages)
979 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
980
Rusty Russell74b25532007-11-19 11:20:42 -0500981 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000982}
983
984static struct virtio_device_id id_table[] = {
985 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
986 { 0 },
987};
988
Rusty Russellc45a6812008-05-02 21:50:50 -0500989static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000990 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
991 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500992 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +0800993 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +0000994 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +0000995 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +0000996 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Rusty Russellc45a6812008-05-02 21:50:50 -0500997};
998
Uwe Kleine-König22402522009-11-05 01:32:44 -0800999static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05001000 .feature_table = features,
1001 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +10001002 .driver.name = KBUILD_MODNAME,
1003 .driver.owner = THIS_MODULE,
1004 .id_table = id_table,
1005 .probe = virtnet_probe,
1006 .remove = __devexit_p(virtnet_remove),
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001007 .config_changed = virtnet_config_changed,
Rusty Russell296f96f2007-10-22 11:03:37 +10001008};
1009
1010static int __init init(void)
1011{
Uwe Kleine-König22402522009-11-05 01:32:44 -08001012 return register_virtio_driver(&virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001013}
1014
1015static void __exit fini(void)
1016{
Uwe Kleine-König22402522009-11-05 01:32:44 -08001017 unregister_virtio_driver(&virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001018}
1019module_init(init);
1020module_exit(fini);
1021
1022MODULE_DEVICE_TABLE(virtio, id_table);
1023MODULE_DESCRIPTION("Virtio network driver");
1024MODULE_LICENSE("GPL");