blob: f5207ef21a913bf72f596683cdc3b7e557363e05 [file] [log] [blame]
Rusty Russell296f96f2007-10-22 11:03:37 +10001/* A simple network driver using virtio.
2 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
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>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
27
Dor Laor6c0cd7c2007-12-16 15:19:43 +020028static int napi_weight = 128;
29module_param(napi_weight, int, 0444);
30
Rusty Russell34a48572008-02-04 23:50:02 -050031static int csum = 1, gso = 1;
32module_param(csum, bool, 0444);
33module_param(gso, bool, 0444);
34
Rusty Russell296f96f2007-10-22 11:03:37 +100035/* FIXME: MTU in config. */
36#define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
37
38struct virtnet_info
39{
40 struct virtio_device *vdev;
41 struct virtqueue *rvq, *svq;
42 struct net_device *dev;
43 struct napi_struct napi;
44
Rusty Russell99ffc692008-05-02 21:50:46 -050045 /* The skb we couldn't send because buffers were full. */
46 struct sk_buff *last_xmit_skb;
47
Rusty Russell363f1512008-06-08 20:51:55 +100048 /* If we need to free in a timer, this is it. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +100049 struct timer_list xmit_free_timer;
50
Rusty Russell296f96f2007-10-22 11:03:37 +100051 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
53
Rusty Russell11a3a152008-05-26 17:48:13 +100054 /* For cleaning up after transmission. */
55 struct tasklet_struct tasklet;
Rusty Russell363f1512008-06-08 20:51:55 +100056 bool free_in_tasklet;
Rusty Russell11a3a152008-05-26 17:48:13 +100057
Herbert Xu97402b92008-04-18 11:24:27 +080058 /* I like... big packets and I cannot lie! */
59 bool big_packets;
60
Rusty Russell296f96f2007-10-22 11:03:37 +100061 /* Receive & send queues. */
62 struct sk_buff_head recv;
63 struct sk_buff_head send;
Rusty Russellfb6813f2008-07-25 12:06:01 -050064
65 /* Chain pages by the private ptr. */
66 struct page *pages;
Rusty Russell296f96f2007-10-22 11:03:37 +100067};
68
69static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
70{
71 return (struct virtio_net_hdr *)skb->cb;
72}
73
74static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
75{
76 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
77}
78
Rusty Russellfb6813f2008-07-25 12:06:01 -050079static void give_a_page(struct virtnet_info *vi, struct page *page)
80{
81 page->private = (unsigned long)vi->pages;
82 vi->pages = page;
83}
84
Mark McLoughlin0a888fd2008-11-16 22:39:18 -080085static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
86{
87 unsigned int i;
88
89 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
90 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
91 skb_shinfo(skb)->nr_frags = 0;
92 skb->data_len = 0;
93}
94
Rusty Russellfb6813f2008-07-25 12:06:01 -050095static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
96{
97 struct page *p = vi->pages;
98
99 if (p)
100 vi->pages = (struct page *)p->private;
101 else
102 p = alloc_page(gfp_mask);
103 return p;
104}
105
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500106static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000107{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500108 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000109
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500110 /* Suppress further interrupts. */
111 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000112
Rusty Russell363f1512008-06-08 20:51:55 +1000113 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000114 netif_wake_queue(vi->dev);
Rusty Russell11a3a152008-05-26 17:48:13 +1000115
116 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
117 * queued, start_xmit won't be called. */
118 tasklet_schedule(&vi->tasklet);
Rusty Russell296f96f2007-10-22 11:03:37 +1000119}
120
121static void receive_skb(struct net_device *dev, struct sk_buff *skb,
122 unsigned len)
123{
124 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800125 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000126
127 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
128 pr_debug("%s: short packet %i\n", dev->name, len);
129 dev->stats.rx_length_errors++;
130 goto drop;
131 }
132 len -= sizeof(struct virtio_net_hdr);
Rusty Russell296f96f2007-10-22 11:03:37 +1000133
Mark McLoughlin0a888fd2008-11-16 22:39:18 -0800134 if (len <= MAX_PACKET_LEN)
135 trim_pages(netdev_priv(dev), skb);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500136
Herbert Xu97402b92008-04-18 11:24:27 +0800137 err = pskb_trim(skb, len);
138 if (err) {
139 pr_debug("%s: pskb_trim failed %i %d\n", dev->name, len, err);
140 dev->stats.rx_dropped++;
141 goto drop;
142 }
143 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000144 dev->stats.rx_bytes += skb->len;
145 dev->stats.rx_packets++;
146
147 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
148 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500149 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000150 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000151 }
152
Mark McLoughlin23cde762008-06-08 20:49:00 +1000153 skb->protocol = eth_type_trans(skb, dev);
154 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
155 ntohs(skb->protocol), skb->len, skb->pkt_type);
156
Rusty Russell296f96f2007-10-22 11:03:37 +1000157 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
158 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500159 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000160 case VIRTIO_NET_HDR_GSO_TCPV4:
161 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
162 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000163 case VIRTIO_NET_HDR_GSO_UDP:
164 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
165 break;
166 case VIRTIO_NET_HDR_GSO_TCPV6:
167 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
168 break;
169 default:
170 if (net_ratelimit())
171 printk(KERN_WARNING "%s: bad gso type %u.\n",
172 dev->name, hdr->gso_type);
173 goto frame_err;
174 }
175
Rusty Russell34a48572008-02-04 23:50:02 -0500176 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
177 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
178
Rusty Russell296f96f2007-10-22 11:03:37 +1000179 skb_shinfo(skb)->gso_size = hdr->gso_size;
180 if (skb_shinfo(skb)->gso_size == 0) {
181 if (net_ratelimit())
182 printk(KERN_WARNING "%s: zero gso size.\n",
183 dev->name);
184 goto frame_err;
185 }
186
187 /* Header must be checked, and gso_segs computed. */
188 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
189 skb_shinfo(skb)->gso_segs = 0;
190 }
191
192 netif_receive_skb(skb);
193 return;
194
195frame_err:
196 dev->stats.rx_frame_errors++;
197drop:
198 dev_kfree_skb(skb);
199}
200
201static void try_fill_recv(struct virtnet_info *vi)
202{
203 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500204 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800205 int num, err, i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000206
Rusty Russell05271682008-05-02 21:50:45 -0500207 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000208 for (;;) {
209 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
210 if (unlikely(!skb))
211 break;
212
213 skb_put(skb, MAX_PACKET_LEN);
214 vnet_hdr_to_sg(sg, skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800215
216 if (vi->big_packets) {
217 for (i = 0; i < MAX_SKB_FRAGS; i++) {
218 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Rusty Russellfb6813f2008-07-25 12:06:01 -0500219 f->page = get_a_page(vi, GFP_ATOMIC);
Herbert Xu97402b92008-04-18 11:24:27 +0800220 if (!f->page)
221 break;
222
223 f->page_offset = 0;
224 f->size = PAGE_SIZE;
225
226 skb->data_len += PAGE_SIZE;
227 skb->len += PAGE_SIZE;
228
229 skb_shinfo(skb)->nr_frags++;
230 }
231 }
232
Rusty Russell296f96f2007-10-22 11:03:37 +1000233 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
234 skb_queue_head(&vi->recv, skb);
235
236 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
237 if (err) {
238 skb_unlink(skb, &vi->recv);
Mark McLoughlin0a888fd2008-11-16 22:39:18 -0800239 trim_pages(vi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000240 kfree_skb(skb);
241 break;
242 }
243 vi->num++;
244 }
245 if (unlikely(vi->num > vi->max))
246 vi->max = vi->num;
247 vi->rvq->vq_ops->kick(vi->rvq);
248}
249
Rusty Russell18445c42008-02-04 23:49:57 -0500250static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000251{
252 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500253 /* Schedule NAPI, Suppress further interrupts if successful. */
254 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
255 rvq->vq_ops->disable_cb(rvq);
256 __netif_rx_schedule(vi->dev, &vi->napi);
257 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000258}
259
260static int virtnet_poll(struct napi_struct *napi, int budget)
261{
262 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
263 struct sk_buff *skb = NULL;
264 unsigned int len, received = 0;
265
266again:
267 while (received < budget &&
268 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
269 __skb_unlink(skb, &vi->recv);
270 receive_skb(vi->dev, skb, len);
271 vi->num--;
272 received++;
273 }
274
275 /* FIXME: If we oom and completely run out of inbufs, we need
276 * to start a timer trying to fill more. */
277 if (vi->num < vi->max / 2)
278 try_fill_recv(vi);
279
Rusty Russell8329d982007-11-19 11:20:43 -0500280 /* Out of packets? */
281 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000282 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500283 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100284 && napi_schedule_prep(napi)) {
285 vi->rvq->vq_ops->disable_cb(vi->rvq);
286 __netif_rx_schedule(vi->dev, napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000287 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100288 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000289 }
290
291 return received;
292}
293
294static void free_old_xmit_skbs(struct virtnet_info *vi)
295{
296 struct sk_buff *skb;
297 unsigned int len;
298
299 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
300 pr_debug("Sent skb %p\n", skb);
301 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500302 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000303 vi->dev->stats.tx_packets++;
304 kfree_skb(skb);
305 }
306}
307
Rusty Russell363f1512008-06-08 20:51:55 +1000308/* If the virtio transport doesn't always notify us when all in-flight packets
309 * are consumed, we fall back to using this function on a timer to free them. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000310static void xmit_free(unsigned long data)
311{
312 struct virtnet_info *vi = (void *)data;
313
314 netif_tx_lock(vi->dev);
315
316 free_old_xmit_skbs(vi);
317
318 if (!skb_queue_empty(&vi->send))
319 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
320
321 netif_tx_unlock(vi->dev);
322}
323
Rusty Russell99ffc692008-05-02 21:50:46 -0500324static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000325{
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000326 int num, err;
Rusty Russell05271682008-05-02 21:50:45 -0500327 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russell296f96f2007-10-22 11:03:37 +1000328 struct virtio_net_hdr *hdr;
329 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000330
Rusty Russell05271682008-05-02 21:50:45 -0500331 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100332
Johannes Berge1749612008-10-27 15:59:26 -0700333 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000334
Rusty Russell296f96f2007-10-22 11:03:37 +1000335 /* Encode metadata header at front. */
336 hdr = skb_vnet_hdr(skb);
337 if (skb->ip_summed == CHECKSUM_PARTIAL) {
338 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
339 hdr->csum_start = skb->csum_start - skb_headroom(skb);
340 hdr->csum_offset = skb->csum_offset;
341 } else {
342 hdr->flags = 0;
343 hdr->csum_offset = hdr->csum_start = 0;
344 }
345
346 if (skb_is_gso(skb)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500347 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000348 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500349 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000350 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
351 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
352 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
353 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
354 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
355 else
356 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500357 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
358 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000359 } else {
360 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500361 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000362 }
363
364 vnet_hdr_to_sg(sg, skb);
365 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russell99ffc692008-05-02 21:50:46 -0500366
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000367 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
Rusty Russell363f1512008-06-08 20:51:55 +1000368 if (!err && !vi->free_in_tasklet)
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000369 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
370
371 return err;
Rusty Russell99ffc692008-05-02 21:50:46 -0500372}
373
Rusty Russell11a3a152008-05-26 17:48:13 +1000374static void xmit_tasklet(unsigned long data)
375{
376 struct virtnet_info *vi = (void *)data;
377
378 netif_tx_lock_bh(vi->dev);
379 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
380 vi->svq->vq_ops->kick(vi->svq);
381 vi->last_xmit_skb = NULL;
382 }
Rusty Russell363f1512008-06-08 20:51:55 +1000383 if (vi->free_in_tasklet)
384 free_old_xmit_skbs(vi);
Rusty Russell11a3a152008-05-26 17:48:13 +1000385 netif_tx_unlock_bh(vi->dev);
386}
387
Rusty Russell99ffc692008-05-02 21:50:46 -0500388static int start_xmit(struct sk_buff *skb, struct net_device *dev)
389{
390 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500391
392again:
393 /* Free up any pending old buffers before queueing new ones. */
394 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500395
Rusty Russell99ffc692008-05-02 21:50:46 -0500396 /* If we has a buffer left over from last time, send it now. */
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100397 if (unlikely(vi->last_xmit_skb) &&
398 xmit_skb(vi, vi->last_xmit_skb) != 0)
399 goto stop_queue;
400
401 vi->last_xmit_skb = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000402
Rusty Russell99ffc692008-05-02 21:50:46 -0500403 /* Put new one in send queue and do transmit */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000404 if (likely(skb)) {
405 __skb_queue_head(&vi->send, skb);
406 if (xmit_skb(vi, skb) != 0) {
407 vi->last_xmit_skb = skb;
408 skb = NULL;
409 goto stop_queue;
410 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500411 }
412done:
413 vi->svq->vq_ops->kick(vi->svq);
414 return NETDEV_TX_OK;
415
416stop_queue:
417 pr_debug("%s: virtio not prepared to send\n", dev->name);
418 netif_stop_queue(dev);
419
420 /* Activate callback for using skbs: if this returns false it
421 * means some were used in the meantime. */
422 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
423 vi->svq->vq_ops->disable_cb(vi->svq);
424 netif_start_queue(dev);
425 goto again;
426 }
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100427 if (skb) {
428 /* Drop this skb: we only queue one. */
429 vi->dev->stats.tx_dropped++;
430 kfree_skb(skb);
431 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500432 goto done;
Rusty Russell296f96f2007-10-22 11:03:37 +1000433}
434
Amit Shahda74e892008-02-29 16:24:50 +0530435#ifdef CONFIG_NET_POLL_CONTROLLER
436static void virtnet_netpoll(struct net_device *dev)
437{
438 struct virtnet_info *vi = netdev_priv(dev);
439
440 napi_schedule(&vi->napi);
441}
442#endif
443
Rusty Russell296f96f2007-10-22 11:03:37 +1000444static int virtnet_open(struct net_device *dev)
445{
446 struct virtnet_info *vi = netdev_priv(dev);
447
Rusty Russell296f96f2007-10-22 11:03:37 +1000448 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500449
450 /* If all buffers were filled by other side before we napi_enabled, we
451 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100452 * now. virtnet_poll wants re-enable the queue, so we disable here.
453 * We synchronize against interrupts via NAPI_STATE_SCHED */
454 if (netif_rx_schedule_prep(dev, &vi->napi)) {
455 vi->rvq->vq_ops->disable_cb(vi->rvq);
456 __netif_rx_schedule(dev, &vi->napi);
457 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000458 return 0;
459}
460
461static int virtnet_close(struct net_device *dev)
462{
463 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000464
465 napi_disable(&vi->napi);
466
Rusty Russell296f96f2007-10-22 11:03:37 +1000467 return 0;
468}
469
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800470static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
471{
472 struct virtnet_info *vi = netdev_priv(dev);
473 struct virtio_device *vdev = vi->vdev;
474
475 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
476 return -ENOSYS;
477
478 return ethtool_op_set_tx_hw_csum(dev, data);
479}
480
481static struct ethtool_ops virtnet_ethtool_ops = {
482 .set_tx_csum = virtnet_set_tx_csum,
483 .set_sg = ethtool_op_set_sg,
484};
485
Rusty Russell296f96f2007-10-22 11:03:37 +1000486static int virtnet_probe(struct virtio_device *vdev)
487{
488 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000489 struct net_device *dev;
490 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000491
492 /* Allocate ourselves a network device with room for our info */
493 dev = alloc_etherdev(sizeof(struct virtnet_info));
494 if (!dev)
495 return -ENOMEM;
496
497 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000498 dev->open = virtnet_open;
499 dev->stop = virtnet_close;
500 dev->hard_start_xmit = start_xmit;
501 dev->features = NETIF_F_HIGHDMA;
Amit Shahda74e892008-02-29 16:24:50 +0530502#ifdef CONFIG_NET_POLL_CONTROLLER
503 dev->poll_controller = virtnet_netpoll;
504#endif
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800505 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000506 SET_NETDEV_DEV(dev, &vdev->dev);
507
508 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500509 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000510 /* This opens up the world of extra features. */
511 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500512 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500513 dev->features |= NETIF_F_TSO | NETIF_F_UFO
514 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
515 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500516 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500517 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500518 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500519 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500520 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500521 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500522 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500523 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500524 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000525 }
526
527 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500528 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500529 vdev->config->get(vdev,
530 offsetof(struct virtio_net_config, mac),
531 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000532 } else
533 random_ether_addr(dev->dev_addr);
534
535 /* Set up our device-specific information */
536 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200537 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000538 vi->dev = dev;
539 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100540 vdev->priv = vi;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500541 vi->pages = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000542
Rusty Russell363f1512008-06-08 20:51:55 +1000543 /* If they give us a callback when all buffers are done, we don't need
544 * the timer. */
545 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
546
Herbert Xu97402b92008-04-18 11:24:27 +0800547 /* If we can receive ANY GSO packets, we must allocate large ones. */
548 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
549 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
550 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
551 vi->big_packets = true;
552
Rusty Russell296f96f2007-10-22 11:03:37 +1000553 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500554 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000555 if (IS_ERR(vi->rvq)) {
556 err = PTR_ERR(vi->rvq);
557 goto free;
558 }
559
Rusty Russella586d4f2008-02-04 23:49:56 -0500560 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000561 if (IS_ERR(vi->svq)) {
562 err = PTR_ERR(vi->svq);
563 goto free_recv;
564 }
565
566 /* Initialize our empty receive and send queues. */
567 skb_queue_head_init(&vi->recv);
568 skb_queue_head_init(&vi->send);
569
Rusty Russell11a3a152008-05-26 17:48:13 +1000570 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
571
Rusty Russell363f1512008-06-08 20:51:55 +1000572 if (!vi->free_in_tasklet)
573 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000574
Rusty Russell296f96f2007-10-22 11:03:37 +1000575 err = register_netdev(dev);
576 if (err) {
577 pr_debug("virtio_net: registering device failed\n");
578 goto free_send;
579 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500580
581 /* Last of all, set up some receive buffers. */
582 try_fill_recv(vi);
583
584 /* If we didn't even get one input buffer, we're useless. */
585 if (vi->num == 0) {
586 err = -ENOMEM;
587 goto unregister;
588 }
589
Rusty Russell296f96f2007-10-22 11:03:37 +1000590 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000591 return 0;
592
Rusty Russellb3369c12008-02-04 23:50:02 -0500593unregister:
594 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000595free_send:
596 vdev->config->del_vq(vi->svq);
597free_recv:
598 vdev->config->del_vq(vi->rvq);
599free:
600 free_netdev(dev);
601 return err;
602}
603
604static void virtnet_remove(struct virtio_device *vdev)
605{
Rusty Russell74b25532007-11-19 11:20:42 -0500606 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500607 struct sk_buff *skb;
608
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500609 /* Stop all the virtqueues. */
610 vdev->config->reset(vdev);
611
Rusty Russell363f1512008-06-08 20:51:55 +1000612 if (!vi->free_in_tasklet)
613 del_timer_sync(&vi->xmit_free_timer);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000614
Rusty Russellb3369c12008-02-04 23:50:02 -0500615 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500616 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
617 kfree_skb(skb);
618 vi->num--;
619 }
Wang Chen288369c2008-05-22 18:07:43 +0800620 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500621
622 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500623
624 vdev->config->del_vq(vi->svq);
625 vdev->config->del_vq(vi->rvq);
626 unregister_netdev(vi->dev);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500627
628 while (vi->pages)
629 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
630
Rusty Russell74b25532007-11-19 11:20:42 -0500631 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000632}
633
634static struct virtio_device_id id_table[] = {
635 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
636 { 0 },
637};
638
Rusty Russellc45a6812008-05-02 21:50:50 -0500639static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000640 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
641 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500642 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +0800643 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
644 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
645 VIRTIO_F_NOTIFY_ON_EMPTY,
Rusty Russellc45a6812008-05-02 21:50:50 -0500646};
647
Rusty Russell296f96f2007-10-22 11:03:37 +1000648static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500649 .feature_table = features,
650 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +1000651 .driver.name = KBUILD_MODNAME,
652 .driver.owner = THIS_MODULE,
653 .id_table = id_table,
654 .probe = virtnet_probe,
655 .remove = __devexit_p(virtnet_remove),
656};
657
658static int __init init(void)
659{
660 return register_virtio_driver(&virtio_net);
661}
662
663static void __exit fini(void)
664{
665 unregister_virtio_driver(&virtio_net);
666}
667module_init(init);
668module_exit(fini);
669
670MODULE_DEVICE_TABLE(virtio, id_table);
671MODULE_DESCRIPTION("Virtio network driver");
672MODULE_LICENSE("GPL");