blob: 71ca29cc184d84e4c4fdd0cbbda8656c3da6801a [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)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080037#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100038
39struct virtnet_info
40{
41 struct virtio_device *vdev;
42 struct virtqueue *rvq, *svq;
43 struct net_device *dev;
44 struct napi_struct napi;
45
Rusty Russell99ffc692008-05-02 21:50:46 -050046 /* The skb we couldn't send because buffers were full. */
47 struct sk_buff *last_xmit_skb;
48
Rusty Russell363f1512008-06-08 20:51:55 +100049 /* If we need to free in a timer, this is it. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +100050 struct timer_list xmit_free_timer;
51
Rusty Russell296f96f2007-10-22 11:03:37 +100052 /* Number of input buffers, and max we've ever had. */
53 unsigned int num, max;
54
Rusty Russell11a3a152008-05-26 17:48:13 +100055 /* For cleaning up after transmission. */
56 struct tasklet_struct tasklet;
Rusty Russell363f1512008-06-08 20:51:55 +100057 bool free_in_tasklet;
Rusty Russell11a3a152008-05-26 17:48:13 +100058
Herbert Xu97402b92008-04-18 11:24:27 +080059 /* I like... big packets and I cannot lie! */
60 bool big_packets;
61
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080062 /* Host will merge rx buffers for big packets (shake it! shake it!) */
63 bool mergeable_rx_bufs;
64
Rusty Russell296f96f2007-10-22 11:03:37 +100065 /* Receive & send queues. */
66 struct sk_buff_head recv;
67 struct sk_buff_head send;
Rusty Russellfb6813f2008-07-25 12:06:01 -050068
69 /* Chain pages by the private ptr. */
70 struct page *pages;
Rusty Russell296f96f2007-10-22 11:03:37 +100071};
72
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080073static inline void *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +100074{
75 return (struct virtio_net_hdr *)skb->cb;
76}
77
Rusty Russellfb6813f2008-07-25 12:06:01 -050078static void give_a_page(struct virtnet_info *vi, struct page *page)
79{
80 page->private = (unsigned long)vi->pages;
81 vi->pages = page;
82}
83
Mark McLoughlin0a888fd2008-11-16 22:39:18 -080084static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
85{
86 unsigned int i;
87
88 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
89 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
90 skb_shinfo(skb)->nr_frags = 0;
91 skb->data_len = 0;
92}
93
Rusty Russellfb6813f2008-07-25 12:06:01 -050094static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
95{
96 struct page *p = vi->pages;
97
98 if (p)
99 vi->pages = (struct page *)p->private;
100 else
101 p = alloc_page(gfp_mask);
102 return p;
103}
104
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500105static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000106{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500107 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000108
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500109 /* Suppress further interrupts. */
110 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000111
Rusty Russell363f1512008-06-08 20:51:55 +1000112 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000113 netif_wake_queue(vi->dev);
Rusty Russell11a3a152008-05-26 17:48:13 +1000114
115 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
116 * queued, start_xmit won't be called. */
117 tasklet_schedule(&vi->tasklet);
Rusty Russell296f96f2007-10-22 11:03:37 +1000118}
119
120static void receive_skb(struct net_device *dev, struct sk_buff *skb,
121 unsigned len)
122{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800123 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000124 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800125 int err;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800126 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000127
128 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
129 pr_debug("%s: short packet %i\n", dev->name, len);
130 dev->stats.rx_length_errors++;
131 goto drop;
132 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000133
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800134 if (vi->mergeable_rx_bufs) {
135 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
136 unsigned int copy;
137 char *p = page_address(skb_shinfo(skb)->frags[0].page);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500138
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800139 if (len > PAGE_SIZE)
140 len = PAGE_SIZE;
141 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
142
143 memcpy(hdr, p, sizeof(*mhdr));
144 p += sizeof(*mhdr);
145
146 copy = len;
147 if (copy > skb_tailroom(skb))
148 copy = skb_tailroom(skb);
149
150 memcpy(skb_put(skb, copy), p, copy);
151
152 len -= copy;
153
154 if (!len) {
155 give_a_page(vi, skb_shinfo(skb)->frags[0].page);
156 skb_shinfo(skb)->nr_frags--;
157 } else {
158 skb_shinfo(skb)->frags[0].page_offset +=
159 sizeof(*mhdr) + copy;
160 skb_shinfo(skb)->frags[0].size = len;
161 skb->data_len += len;
162 skb->len += len;
163 }
164
165 while (--mhdr->num_buffers) {
166 struct sk_buff *nskb;
167
168 i = skb_shinfo(skb)->nr_frags;
169 if (i >= MAX_SKB_FRAGS) {
170 pr_debug("%s: packet too long %d\n", dev->name,
171 len);
172 dev->stats.rx_length_errors++;
173 goto drop;
174 }
175
176 nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
177 if (!nskb) {
178 pr_debug("%s: rx error: %d buffers missing\n",
179 dev->name, mhdr->num_buffers);
180 dev->stats.rx_length_errors++;
181 goto drop;
182 }
183
184 __skb_unlink(nskb, &vi->recv);
185 vi->num--;
186
187 skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
188 skb_shinfo(nskb)->nr_frags = 0;
189 kfree_skb(nskb);
190
191 if (len > PAGE_SIZE)
192 len = PAGE_SIZE;
193
194 skb_shinfo(skb)->frags[i].size = len;
195 skb_shinfo(skb)->nr_frags++;
196 skb->data_len += len;
197 skb->len += len;
198 }
199 } else {
200 len -= sizeof(struct virtio_net_hdr);
201
202 if (len <= MAX_PACKET_LEN)
203 trim_pages(vi, skb);
204
205 err = pskb_trim(skb, len);
206 if (err) {
207 pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
208 len, err);
209 dev->stats.rx_dropped++;
210 goto drop;
211 }
Herbert Xu97402b92008-04-18 11:24:27 +0800212 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800213
Herbert Xu97402b92008-04-18 11:24:27 +0800214 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000215 dev->stats.rx_bytes += skb->len;
216 dev->stats.rx_packets++;
217
218 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
219 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500220 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000221 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000222 }
223
Mark McLoughlin23cde762008-06-08 20:49:00 +1000224 skb->protocol = eth_type_trans(skb, dev);
225 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
226 ntohs(skb->protocol), skb->len, skb->pkt_type);
227
Rusty Russell296f96f2007-10-22 11:03:37 +1000228 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
229 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500230 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000231 case VIRTIO_NET_HDR_GSO_TCPV4:
232 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
233 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000234 case VIRTIO_NET_HDR_GSO_UDP:
235 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
236 break;
237 case VIRTIO_NET_HDR_GSO_TCPV6:
238 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
239 break;
240 default:
241 if (net_ratelimit())
242 printk(KERN_WARNING "%s: bad gso type %u.\n",
243 dev->name, hdr->gso_type);
244 goto frame_err;
245 }
246
Rusty Russell34a48572008-02-04 23:50:02 -0500247 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
248 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
249
Rusty Russell296f96f2007-10-22 11:03:37 +1000250 skb_shinfo(skb)->gso_size = hdr->gso_size;
251 if (skb_shinfo(skb)->gso_size == 0) {
252 if (net_ratelimit())
253 printk(KERN_WARNING "%s: zero gso size.\n",
254 dev->name);
255 goto frame_err;
256 }
257
258 /* Header must be checked, and gso_segs computed. */
259 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
260 skb_shinfo(skb)->gso_segs = 0;
261 }
262
263 netif_receive_skb(skb);
264 return;
265
266frame_err:
267 dev->stats.rx_frame_errors++;
268drop:
269 dev_kfree_skb(skb);
270}
271
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800272static void try_fill_recv_maxbufs(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +1000273{
274 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500275 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800276 int num, err, i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000277
Rusty Russell05271682008-05-02 21:50:45 -0500278 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000279 for (;;) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800280 struct virtio_net_hdr *hdr;
281
Rusty Russell296f96f2007-10-22 11:03:37 +1000282 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
283 if (unlikely(!skb))
284 break;
285
286 skb_put(skb, MAX_PACKET_LEN);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800287
288 hdr = skb_vnet_hdr(skb);
289 sg_init_one(sg, hdr, sizeof(*hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800290
291 if (vi->big_packets) {
292 for (i = 0; i < MAX_SKB_FRAGS; i++) {
293 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Rusty Russellfb6813f2008-07-25 12:06:01 -0500294 f->page = get_a_page(vi, GFP_ATOMIC);
Herbert Xu97402b92008-04-18 11:24:27 +0800295 if (!f->page)
296 break;
297
298 f->page_offset = 0;
299 f->size = PAGE_SIZE;
300
301 skb->data_len += PAGE_SIZE;
302 skb->len += PAGE_SIZE;
303
304 skb_shinfo(skb)->nr_frags++;
305 }
306 }
307
Rusty Russell296f96f2007-10-22 11:03:37 +1000308 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
309 skb_queue_head(&vi->recv, skb);
310
311 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
312 if (err) {
313 skb_unlink(skb, &vi->recv);
Mark McLoughlin0a888fd2008-11-16 22:39:18 -0800314 trim_pages(vi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000315 kfree_skb(skb);
316 break;
317 }
318 vi->num++;
319 }
320 if (unlikely(vi->num > vi->max))
321 vi->max = vi->num;
322 vi->rvq->vq_ops->kick(vi->rvq);
323}
324
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800325static void try_fill_recv(struct virtnet_info *vi)
326{
327 struct sk_buff *skb;
328 struct scatterlist sg[1];
329 int err;
330
331 if (!vi->mergeable_rx_bufs) {
332 try_fill_recv_maxbufs(vi);
333 return;
334 }
335
336 for (;;) {
337 skb_frag_t *f;
338
339 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
340 if (unlikely(!skb))
341 break;
342
343 skb_reserve(skb, NET_IP_ALIGN);
344
345 f = &skb_shinfo(skb)->frags[0];
346 f->page = get_a_page(vi, GFP_ATOMIC);
347 if (!f->page) {
348 kfree_skb(skb);
349 break;
350 }
351
352 f->page_offset = 0;
353 f->size = PAGE_SIZE;
354
355 skb_shinfo(skb)->nr_frags++;
356
357 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
358 skb_queue_head(&vi->recv, skb);
359
360 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
361 if (err) {
362 skb_unlink(skb, &vi->recv);
363 kfree_skb(skb);
364 break;
365 }
366 vi->num++;
367 }
368 if (unlikely(vi->num > vi->max))
369 vi->max = vi->num;
370 vi->rvq->vq_ops->kick(vi->rvq);
371}
372
Rusty Russell18445c42008-02-04 23:49:57 -0500373static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000374{
375 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500376 /* Schedule NAPI, Suppress further interrupts if successful. */
377 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
378 rvq->vq_ops->disable_cb(rvq);
379 __netif_rx_schedule(vi->dev, &vi->napi);
380 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000381}
382
383static int virtnet_poll(struct napi_struct *napi, int budget)
384{
385 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
386 struct sk_buff *skb = NULL;
387 unsigned int len, received = 0;
388
389again:
390 while (received < budget &&
391 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
392 __skb_unlink(skb, &vi->recv);
393 receive_skb(vi->dev, skb, len);
394 vi->num--;
395 received++;
396 }
397
398 /* FIXME: If we oom and completely run out of inbufs, we need
399 * to start a timer trying to fill more. */
400 if (vi->num < vi->max / 2)
401 try_fill_recv(vi);
402
Rusty Russell8329d982007-11-19 11:20:43 -0500403 /* Out of packets? */
404 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000405 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500406 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100407 && napi_schedule_prep(napi)) {
408 vi->rvq->vq_ops->disable_cb(vi->rvq);
409 __netif_rx_schedule(vi->dev, napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000410 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100411 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000412 }
413
414 return received;
415}
416
417static void free_old_xmit_skbs(struct virtnet_info *vi)
418{
419 struct sk_buff *skb;
420 unsigned int len;
421
422 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
423 pr_debug("Sent skb %p\n", skb);
424 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500425 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000426 vi->dev->stats.tx_packets++;
427 kfree_skb(skb);
428 }
429}
430
Rusty Russell363f1512008-06-08 20:51:55 +1000431/* If the virtio transport doesn't always notify us when all in-flight packets
432 * are consumed, we fall back to using this function on a timer to free them. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000433static void xmit_free(unsigned long data)
434{
435 struct virtnet_info *vi = (void *)data;
436
437 netif_tx_lock(vi->dev);
438
439 free_old_xmit_skbs(vi);
440
441 if (!skb_queue_empty(&vi->send))
442 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
443
444 netif_tx_unlock(vi->dev);
445}
446
Rusty Russell99ffc692008-05-02 21:50:46 -0500447static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000448{
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000449 int num, err;
Rusty Russell05271682008-05-02 21:50:45 -0500450 struct scatterlist sg[2+MAX_SKB_FRAGS];
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800451 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
452 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000453 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000454
Rusty Russell05271682008-05-02 21:50:45 -0500455 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100456
Johannes Berge1749612008-10-27 15:59:26 -0700457 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000458
Rusty Russell296f96f2007-10-22 11:03:37 +1000459 if (skb->ip_summed == CHECKSUM_PARTIAL) {
460 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
461 hdr->csum_start = skb->csum_start - skb_headroom(skb);
462 hdr->csum_offset = skb->csum_offset;
463 } else {
464 hdr->flags = 0;
465 hdr->csum_offset = hdr->csum_start = 0;
466 }
467
468 if (skb_is_gso(skb)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500469 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000470 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500471 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000472 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
473 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
474 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
475 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
476 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
477 else
478 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500479 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
480 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000481 } else {
482 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500483 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000484 }
485
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800486 mhdr->num_buffers = 0;
487
488 /* Encode metadata header at front. */
489 if (vi->mergeable_rx_bufs)
490 sg_init_one(sg, mhdr, sizeof(*mhdr));
491 else
492 sg_init_one(sg, hdr, sizeof(*hdr));
493
Rusty Russell296f96f2007-10-22 11:03:37 +1000494 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russell99ffc692008-05-02 21:50:46 -0500495
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000496 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
Rusty Russell363f1512008-06-08 20:51:55 +1000497 if (!err && !vi->free_in_tasklet)
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000498 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
499
500 return err;
Rusty Russell99ffc692008-05-02 21:50:46 -0500501}
502
Rusty Russell11a3a152008-05-26 17:48:13 +1000503static void xmit_tasklet(unsigned long data)
504{
505 struct virtnet_info *vi = (void *)data;
506
507 netif_tx_lock_bh(vi->dev);
508 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
509 vi->svq->vq_ops->kick(vi->svq);
510 vi->last_xmit_skb = NULL;
511 }
Rusty Russell363f1512008-06-08 20:51:55 +1000512 if (vi->free_in_tasklet)
513 free_old_xmit_skbs(vi);
Rusty Russell11a3a152008-05-26 17:48:13 +1000514 netif_tx_unlock_bh(vi->dev);
515}
516
Rusty Russell99ffc692008-05-02 21:50:46 -0500517static int start_xmit(struct sk_buff *skb, struct net_device *dev)
518{
519 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500520
521again:
522 /* Free up any pending old buffers before queueing new ones. */
523 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500524
Rusty Russell99ffc692008-05-02 21:50:46 -0500525 /* If we has a buffer left over from last time, send it now. */
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100526 if (unlikely(vi->last_xmit_skb) &&
527 xmit_skb(vi, vi->last_xmit_skb) != 0)
528 goto stop_queue;
529
530 vi->last_xmit_skb = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000531
Rusty Russell99ffc692008-05-02 21:50:46 -0500532 /* Put new one in send queue and do transmit */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000533 if (likely(skb)) {
534 __skb_queue_head(&vi->send, skb);
535 if (xmit_skb(vi, skb) != 0) {
536 vi->last_xmit_skb = skb;
537 skb = NULL;
538 goto stop_queue;
539 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500540 }
541done:
542 vi->svq->vq_ops->kick(vi->svq);
543 return NETDEV_TX_OK;
544
545stop_queue:
546 pr_debug("%s: virtio not prepared to send\n", dev->name);
547 netif_stop_queue(dev);
548
549 /* Activate callback for using skbs: if this returns false it
550 * means some were used in the meantime. */
551 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
552 vi->svq->vq_ops->disable_cb(vi->svq);
553 netif_start_queue(dev);
554 goto again;
555 }
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100556 if (skb) {
557 /* Drop this skb: we only queue one. */
558 vi->dev->stats.tx_dropped++;
559 kfree_skb(skb);
560 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500561 goto done;
Rusty Russell296f96f2007-10-22 11:03:37 +1000562}
563
Amit Shahda74e892008-02-29 16:24:50 +0530564#ifdef CONFIG_NET_POLL_CONTROLLER
565static void virtnet_netpoll(struct net_device *dev)
566{
567 struct virtnet_info *vi = netdev_priv(dev);
568
569 napi_schedule(&vi->napi);
570}
571#endif
572
Rusty Russell296f96f2007-10-22 11:03:37 +1000573static int virtnet_open(struct net_device *dev)
574{
575 struct virtnet_info *vi = netdev_priv(dev);
576
Rusty Russell296f96f2007-10-22 11:03:37 +1000577 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500578
579 /* If all buffers were filled by other side before we napi_enabled, we
580 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100581 * now. virtnet_poll wants re-enable the queue, so we disable here.
582 * We synchronize against interrupts via NAPI_STATE_SCHED */
583 if (netif_rx_schedule_prep(dev, &vi->napi)) {
584 vi->rvq->vq_ops->disable_cb(vi->rvq);
585 __netif_rx_schedule(dev, &vi->napi);
586 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000587 return 0;
588}
589
590static int virtnet_close(struct net_device *dev)
591{
592 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000593
594 napi_disable(&vi->napi);
595
Rusty Russell296f96f2007-10-22 11:03:37 +1000596 return 0;
597}
598
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800599static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
600{
601 struct virtnet_info *vi = netdev_priv(dev);
602 struct virtio_device *vdev = vi->vdev;
603
604 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
605 return -ENOSYS;
606
607 return ethtool_op_set_tx_hw_csum(dev, data);
608}
609
610static struct ethtool_ops virtnet_ethtool_ops = {
611 .set_tx_csum = virtnet_set_tx_csum,
612 .set_sg = ethtool_op_set_sg,
Mark McLoughlin0276b492008-11-16 22:40:36 -0800613 .set_tso = ethtool_op_set_tso,
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800614};
615
Mark McLoughlin39da5812008-11-26 13:58:11 +0000616#define MIN_MTU 68
617#define MAX_MTU 65535
618
619static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
620{
621 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
622 return -EINVAL;
623 dev->mtu = new_mtu;
624 return 0;
625}
626
Rusty Russell296f96f2007-10-22 11:03:37 +1000627static int virtnet_probe(struct virtio_device *vdev)
628{
629 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000630 struct net_device *dev;
631 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000632
633 /* Allocate ourselves a network device with room for our info */
634 dev = alloc_etherdev(sizeof(struct virtnet_info));
635 if (!dev)
636 return -ENOMEM;
637
638 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000639 dev->open = virtnet_open;
640 dev->stop = virtnet_close;
641 dev->hard_start_xmit = start_xmit;
Mark McLoughlin39da5812008-11-26 13:58:11 +0000642 dev->change_mtu = virtnet_change_mtu;
Rusty Russell296f96f2007-10-22 11:03:37 +1000643 dev->features = NETIF_F_HIGHDMA;
Amit Shahda74e892008-02-29 16:24:50 +0530644#ifdef CONFIG_NET_POLL_CONTROLLER
645 dev->poll_controller = virtnet_netpoll;
646#endif
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800647 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000648 SET_NETDEV_DEV(dev, &vdev->dev);
649
650 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500651 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000652 /* This opens up the world of extra features. */
653 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500654 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500655 dev->features |= NETIF_F_TSO | NETIF_F_UFO
656 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
657 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500658 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500659 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500660 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500661 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500662 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500663 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500664 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500665 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500666 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000667 }
668
669 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500670 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500671 vdev->config->get(vdev,
672 offsetof(struct virtio_net_config, mac),
673 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000674 } else
675 random_ether_addr(dev->dev_addr);
676
677 /* Set up our device-specific information */
678 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200679 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000680 vi->dev = dev;
681 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100682 vdev->priv = vi;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500683 vi->pages = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000684
Rusty Russell363f1512008-06-08 20:51:55 +1000685 /* If they give us a callback when all buffers are done, we don't need
686 * the timer. */
687 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
688
Herbert Xu97402b92008-04-18 11:24:27 +0800689 /* If we can receive ANY GSO packets, we must allocate large ones. */
690 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
691 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
692 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
693 vi->big_packets = true;
694
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800695 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
696 vi->mergeable_rx_bufs = true;
697
Rusty Russell296f96f2007-10-22 11:03:37 +1000698 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500699 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000700 if (IS_ERR(vi->rvq)) {
701 err = PTR_ERR(vi->rvq);
702 goto free;
703 }
704
Rusty Russella586d4f2008-02-04 23:49:56 -0500705 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000706 if (IS_ERR(vi->svq)) {
707 err = PTR_ERR(vi->svq);
708 goto free_recv;
709 }
710
711 /* Initialize our empty receive and send queues. */
712 skb_queue_head_init(&vi->recv);
713 skb_queue_head_init(&vi->send);
714
Rusty Russell11a3a152008-05-26 17:48:13 +1000715 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
716
Rusty Russell363f1512008-06-08 20:51:55 +1000717 if (!vi->free_in_tasklet)
718 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000719
Rusty Russell296f96f2007-10-22 11:03:37 +1000720 err = register_netdev(dev);
721 if (err) {
722 pr_debug("virtio_net: registering device failed\n");
723 goto free_send;
724 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500725
726 /* Last of all, set up some receive buffers. */
727 try_fill_recv(vi);
728
729 /* If we didn't even get one input buffer, we're useless. */
730 if (vi->num == 0) {
731 err = -ENOMEM;
732 goto unregister;
733 }
734
Rusty Russell296f96f2007-10-22 11:03:37 +1000735 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000736 return 0;
737
Rusty Russellb3369c12008-02-04 23:50:02 -0500738unregister:
739 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000740free_send:
741 vdev->config->del_vq(vi->svq);
742free_recv:
743 vdev->config->del_vq(vi->rvq);
744free:
745 free_netdev(dev);
746 return err;
747}
748
749static void virtnet_remove(struct virtio_device *vdev)
750{
Rusty Russell74b25532007-11-19 11:20:42 -0500751 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500752 struct sk_buff *skb;
753
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500754 /* Stop all the virtqueues. */
755 vdev->config->reset(vdev);
756
Rusty Russell363f1512008-06-08 20:51:55 +1000757 if (!vi->free_in_tasklet)
758 del_timer_sync(&vi->xmit_free_timer);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000759
Rusty Russellb3369c12008-02-04 23:50:02 -0500760 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500761 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
762 kfree_skb(skb);
763 vi->num--;
764 }
Wang Chen288369c2008-05-22 18:07:43 +0800765 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500766
767 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500768
769 vdev->config->del_vq(vi->svq);
770 vdev->config->del_vq(vi->rvq);
771 unregister_netdev(vi->dev);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500772
773 while (vi->pages)
774 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
775
Rusty Russell74b25532007-11-19 11:20:42 -0500776 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000777}
778
779static struct virtio_device_id id_table[] = {
780 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
781 { 0 },
782};
783
Rusty Russellc45a6812008-05-02 21:50:50 -0500784static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000785 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
786 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500787 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +0800788 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
789 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800790 VIRTIO_NET_F_MRG_RXBUF,
Herbert Xu97402b92008-04-18 11:24:27 +0800791 VIRTIO_F_NOTIFY_ON_EMPTY,
Rusty Russellc45a6812008-05-02 21:50:50 -0500792};
793
Rusty Russell296f96f2007-10-22 11:03:37 +1000794static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500795 .feature_table = features,
796 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +1000797 .driver.name = KBUILD_MODNAME,
798 .driver.owner = THIS_MODULE,
799 .id_table = id_table,
800 .probe = virtnet_probe,
801 .remove = __devexit_p(virtnet_remove),
802};
803
804static int __init init(void)
805{
806 return register_virtio_driver(&virtio_net);
807}
808
809static void __exit fini(void)
810{
811 unregister_virtio_driver(&virtio_net);
812}
813module_init(init);
814module_exit(fini);
815
816MODULE_DEVICE_TABLE(virtio, id_table);
817MODULE_DESCRIPTION("Virtio network driver");
818MODULE_LICENSE("GPL");