]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/virtio_net.c
virtio net: Allow receiving SG packets
[mirror_ubuntu-bionic-kernel.git] / drivers / net / virtio_net.c
CommitLineData
296f96fc
RR
1/* 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>
a9ea3fc6 22#include <linux/ethtool.h>
296f96fc
RR
23#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
27
6c0cd7c0
DL
28static int napi_weight = 128;
29module_param(napi_weight, int, 0444);
30
34a48579
RR
31static int csum = 1, gso = 1;
32module_param(csum, bool, 0444);
33module_param(gso, bool, 0444);
34
296f96fc
RR
35/* 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
99ffc696
RR
45 /* The skb we couldn't send because buffers were full. */
46 struct sk_buff *last_xmit_skb;
47
363f1514 48 /* If we need to free in a timer, this is it. */
14c998f0
MM
49 struct timer_list xmit_free_timer;
50
296f96fc
RR
51 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
53
11a3a154
RR
54 /* For cleaning up after transmission. */
55 struct tasklet_struct tasklet;
363f1514 56 bool free_in_tasklet;
11a3a154 57
97402b96
HX
58 /* I like... big packets and I cannot lie! */
59 bool big_packets;
60
296f96fc
RR
61 /* Receive & send queues. */
62 struct sk_buff_head recv;
63 struct sk_buff_head send;
64};
65
66static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
67{
68 return (struct virtio_net_hdr *)skb->cb;
69}
70
71static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
72{
73 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
74}
75
2cb9c6ba 76static void skb_xmit_done(struct virtqueue *svq)
296f96fc 77{
2cb9c6ba 78 struct virtnet_info *vi = svq->vdev->priv;
296f96fc 79
2cb9c6ba
RR
80 /* Suppress further interrupts. */
81 svq->vq_ops->disable_cb(svq);
11a3a154 82
363f1514 83 /* We were probably waiting for more output buffers. */
296f96fc 84 netif_wake_queue(vi->dev);
11a3a154
RR
85
86 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
87 * queued, start_xmit won't be called. */
88 tasklet_schedule(&vi->tasklet);
296f96fc
RR
89}
90
91static void receive_skb(struct net_device *dev, struct sk_buff *skb,
92 unsigned len)
93{
94 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
97402b96 95 int err;
296f96fc
RR
96
97 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
98 pr_debug("%s: short packet %i\n", dev->name, len);
99 dev->stats.rx_length_errors++;
100 goto drop;
101 }
102 len -= sizeof(struct virtio_net_hdr);
23cde76d 103
97402b96
HX
104 err = pskb_trim(skb, len);
105 if (err) {
106 pr_debug("%s: pskb_trim failed %i %d\n", dev->name, len, err);
107 dev->stats.rx_dropped++;
108 goto drop;
109 }
110 skb->truesize += skb->data_len;
296f96fc
RR
111 dev->stats.rx_bytes += skb->len;
112 dev->stats.rx_packets++;
113
114 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
115 pr_debug("Needs csum!\n");
f35d9d8a 116 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
296f96fc 117 goto frame_err;
296f96fc
RR
118 }
119
23cde76d
MM
120 skb->protocol = eth_type_trans(skb, dev);
121 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
122 ntohs(skb->protocol), skb->len, skb->pkt_type);
123
296f96fc
RR
124 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
125 pr_debug("GSO!\n");
34a48579 126 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
296f96fc
RR
127 case VIRTIO_NET_HDR_GSO_TCPV4:
128 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
129 break;
296f96fc
RR
130 case VIRTIO_NET_HDR_GSO_UDP:
131 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
132 break;
133 case VIRTIO_NET_HDR_GSO_TCPV6:
134 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
135 break;
136 default:
137 if (net_ratelimit())
138 printk(KERN_WARNING "%s: bad gso type %u.\n",
139 dev->name, hdr->gso_type);
140 goto frame_err;
141 }
142
34a48579
RR
143 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
144 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
145
296f96fc
RR
146 skb_shinfo(skb)->gso_size = hdr->gso_size;
147 if (skb_shinfo(skb)->gso_size == 0) {
148 if (net_ratelimit())
149 printk(KERN_WARNING "%s: zero gso size.\n",
150 dev->name);
151 goto frame_err;
152 }
153
154 /* Header must be checked, and gso_segs computed. */
155 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
156 skb_shinfo(skb)->gso_segs = 0;
157 }
158
159 netif_receive_skb(skb);
160 return;
161
162frame_err:
163 dev->stats.rx_frame_errors++;
164drop:
165 dev_kfree_skb(skb);
166}
167
168static void try_fill_recv(struct virtnet_info *vi)
169{
170 struct sk_buff *skb;
05271685 171 struct scatterlist sg[2+MAX_SKB_FRAGS];
97402b96 172 int num, err, i;
296f96fc 173
05271685 174 sg_init_table(sg, 2+MAX_SKB_FRAGS);
296f96fc
RR
175 for (;;) {
176 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
177 if (unlikely(!skb))
178 break;
179
180 skb_put(skb, MAX_PACKET_LEN);
181 vnet_hdr_to_sg(sg, skb);
97402b96
HX
182
183 if (vi->big_packets) {
184 for (i = 0; i < MAX_SKB_FRAGS; i++) {
185 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
186 f->page = alloc_page(GFP_ATOMIC);
187 if (!f->page)
188 break;
189
190 f->page_offset = 0;
191 f->size = PAGE_SIZE;
192
193 skb->data_len += PAGE_SIZE;
194 skb->len += PAGE_SIZE;
195
196 skb_shinfo(skb)->nr_frags++;
197 }
198 }
199
296f96fc
RR
200 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
201 skb_queue_head(&vi->recv, skb);
202
203 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
204 if (err) {
205 skb_unlink(skb, &vi->recv);
206 kfree_skb(skb);
207 break;
208 }
209 vi->num++;
210 }
211 if (unlikely(vi->num > vi->max))
212 vi->max = vi->num;
213 vi->rvq->vq_ops->kick(vi->rvq);
214}
215
18445c4d 216static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
217{
218 struct virtnet_info *vi = rvq->vdev->priv;
18445c4d
RR
219 /* Schedule NAPI, Suppress further interrupts if successful. */
220 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
221 rvq->vq_ops->disable_cb(rvq);
222 __netif_rx_schedule(vi->dev, &vi->napi);
223 }
296f96fc
RR
224}
225
226static int virtnet_poll(struct napi_struct *napi, int budget)
227{
228 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
229 struct sk_buff *skb = NULL;
230 unsigned int len, received = 0;
231
232again:
233 while (received < budget &&
234 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
235 __skb_unlink(skb, &vi->recv);
236 receive_skb(vi->dev, skb, len);
237 vi->num--;
238 received++;
239 }
240
241 /* FIXME: If we oom and completely run out of inbufs, we need
242 * to start a timer trying to fill more. */
243 if (vi->num < vi->max / 2)
244 try_fill_recv(vi);
245
8329d98e
RR
246 /* Out of packets? */
247 if (received < budget) {
296f96fc 248 netif_rx_complete(vi->dev, napi);
18445c4d 249 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
4265f161
CB
250 && napi_schedule_prep(napi)) {
251 vi->rvq->vq_ops->disable_cb(vi->rvq);
252 __netif_rx_schedule(vi->dev, napi);
296f96fc 253 goto again;
4265f161 254 }
296f96fc
RR
255 }
256
257 return received;
258}
259
260static void free_old_xmit_skbs(struct virtnet_info *vi)
261{
262 struct sk_buff *skb;
263 unsigned int len;
264
265 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
266 pr_debug("Sent skb %p\n", skb);
267 __skb_unlink(skb, &vi->send);
655aa31f 268 vi->dev->stats.tx_bytes += skb->len;
296f96fc
RR
269 vi->dev->stats.tx_packets++;
270 kfree_skb(skb);
271 }
272}
273
363f1514
RR
274/* If the virtio transport doesn't always notify us when all in-flight packets
275 * are consumed, we fall back to using this function on a timer to free them. */
14c998f0
MM
276static void xmit_free(unsigned long data)
277{
278 struct virtnet_info *vi = (void *)data;
279
280 netif_tx_lock(vi->dev);
281
282 free_old_xmit_skbs(vi);
283
284 if (!skb_queue_empty(&vi->send))
285 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
286
287 netif_tx_unlock(vi->dev);
288}
289
99ffc696 290static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
296f96fc 291{
14c998f0 292 int num, err;
05271685 293 struct scatterlist sg[2+MAX_SKB_FRAGS];
296f96fc
RR
294 struct virtio_net_hdr *hdr;
295 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
296f96fc 296
05271685 297 sg_init_table(sg, 2+MAX_SKB_FRAGS);
4d125de3 298
99ffc696 299 pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb,
21f644f3
DM
300 dest[0], dest[1], dest[2],
301 dest[3], dest[4], dest[5]);
296f96fc 302
296f96fc
RR
303 /* Encode metadata header at front. */
304 hdr = skb_vnet_hdr(skb);
305 if (skb->ip_summed == CHECKSUM_PARTIAL) {
306 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
307 hdr->csum_start = skb->csum_start - skb_headroom(skb);
308 hdr->csum_offset = skb->csum_offset;
309 } else {
310 hdr->flags = 0;
311 hdr->csum_offset = hdr->csum_start = 0;
312 }
313
314 if (skb_is_gso(skb)) {
50c8ea80 315 hdr->hdr_len = skb_transport_header(skb) - skb->data;
296f96fc 316 hdr->gso_size = skb_shinfo(skb)->gso_size;
34a48579 317 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
296f96fc
RR
318 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
319 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
320 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
321 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
322 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
323 else
324 BUG();
34a48579
RR
325 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
326 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
296f96fc
RR
327 } else {
328 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
50c8ea80 329 hdr->gso_size = hdr->hdr_len = 0;
296f96fc
RR
330 }
331
332 vnet_hdr_to_sg(sg, skb);
333 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
99ffc696 334
14c998f0 335 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
363f1514 336 if (!err && !vi->free_in_tasklet)
14c998f0
MM
337 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
338
339 return err;
99ffc696
RR
340}
341
11a3a154
RR
342static void xmit_tasklet(unsigned long data)
343{
344 struct virtnet_info *vi = (void *)data;
345
346 netif_tx_lock_bh(vi->dev);
347 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
348 vi->svq->vq_ops->kick(vi->svq);
349 vi->last_xmit_skb = NULL;
350 }
363f1514
RR
351 if (vi->free_in_tasklet)
352 free_old_xmit_skbs(vi);
11a3a154
RR
353 netif_tx_unlock_bh(vi->dev);
354}
355
99ffc696
RR
356static int start_xmit(struct sk_buff *skb, struct net_device *dev)
357{
358 struct virtnet_info *vi = netdev_priv(dev);
2cb9c6ba
RR
359
360again:
361 /* Free up any pending old buffers before queueing new ones. */
362 free_old_xmit_skbs(vi);
99ffc696
RR
363
364 /* If we has a buffer left over from last time, send it now. */
9953ca6c
MM
365 if (unlikely(vi->last_xmit_skb) &&
366 xmit_skb(vi, vi->last_xmit_skb) != 0)
367 goto stop_queue;
368
369 vi->last_xmit_skb = NULL;
2cb9c6ba 370
99ffc696 371 /* Put new one in send queue and do transmit */
7eb2e251
RR
372 if (likely(skb)) {
373 __skb_queue_head(&vi->send, skb);
374 if (xmit_skb(vi, skb) != 0) {
375 vi->last_xmit_skb = skb;
376 skb = NULL;
377 goto stop_queue;
378 }
296f96fc 379 }
99ffc696 380done:
296f96fc 381 vi->svq->vq_ops->kick(vi->svq);
99ffc696
RR
382 return NETDEV_TX_OK;
383
384stop_queue:
385 pr_debug("%s: virtio not prepared to send\n", dev->name);
386 netif_stop_queue(dev);
387
388 /* Activate callback for using skbs: if this returns false it
389 * means some were used in the meantime. */
390 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
391 vi->svq->vq_ops->disable_cb(vi->svq);
392 netif_start_queue(dev);
393 goto again;
394 }
9953ca6c
MM
395 if (skb) {
396 /* Drop this skb: we only queue one. */
397 vi->dev->stats.tx_dropped++;
398 kfree_skb(skb);
399 }
99ffc696 400 goto done;
296f96fc
RR
401}
402
da74e89d
AS
403#ifdef CONFIG_NET_POLL_CONTROLLER
404static void virtnet_netpoll(struct net_device *dev)
405{
406 struct virtnet_info *vi = netdev_priv(dev);
407
408 napi_schedule(&vi->napi);
409}
410#endif
411
296f96fc
RR
412static int virtnet_open(struct net_device *dev)
413{
414 struct virtnet_info *vi = netdev_priv(dev);
415
296f96fc 416 napi_enable(&vi->napi);
a48bd8f6
RR
417
418 /* If all buffers were filled by other side before we napi_enabled, we
419 * won't get another interrupt, so process any outstanding packets
370076d9
CB
420 * now. virtnet_poll wants re-enable the queue, so we disable here.
421 * We synchronize against interrupts via NAPI_STATE_SCHED */
422 if (netif_rx_schedule_prep(dev, &vi->napi)) {
423 vi->rvq->vq_ops->disable_cb(vi->rvq);
424 __netif_rx_schedule(dev, &vi->napi);
425 }
296f96fc
RR
426 return 0;
427}
428
429static int virtnet_close(struct net_device *dev)
430{
431 struct virtnet_info *vi = netdev_priv(dev);
296f96fc
RR
432
433 napi_disable(&vi->napi);
434
296f96fc
RR
435 return 0;
436}
437
a9ea3fc6
HX
438static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
439{
440 struct virtnet_info *vi = netdev_priv(dev);
441 struct virtio_device *vdev = vi->vdev;
442
443 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
444 return -ENOSYS;
445
446 return ethtool_op_set_tx_hw_csum(dev, data);
447}
448
449static struct ethtool_ops virtnet_ethtool_ops = {
450 .set_tx_csum = virtnet_set_tx_csum,
451 .set_sg = ethtool_op_set_sg,
452};
453
296f96fc
RR
454static int virtnet_probe(struct virtio_device *vdev)
455{
456 int err;
296f96fc
RR
457 struct net_device *dev;
458 struct virtnet_info *vi;
296f96fc
RR
459
460 /* Allocate ourselves a network device with room for our info */
461 dev = alloc_etherdev(sizeof(struct virtnet_info));
462 if (!dev)
463 return -ENOMEM;
464
465 /* Set up network device as normal. */
296f96fc
RR
466 dev->open = virtnet_open;
467 dev->stop = virtnet_close;
468 dev->hard_start_xmit = start_xmit;
469 dev->features = NETIF_F_HIGHDMA;
da74e89d
AS
470#ifdef CONFIG_NET_POLL_CONTROLLER
471 dev->poll_controller = virtnet_netpoll;
472#endif
a9ea3fc6 473 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
296f96fc
RR
474 SET_NETDEV_DEV(dev, &vdev->dev);
475
476 /* Do we support "hardware" checksums? */
c45a6816 477 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc
RR
478 /* This opens up the world of extra features. */
479 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
c45a6816 480 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
34a48579
RR
481 dev->features |= NETIF_F_TSO | NETIF_F_UFO
482 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
483 }
5539ae96 484 /* Individual feature bits: what can host handle? */
c45a6816 485 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
5539ae96 486 dev->features |= NETIF_F_TSO;
c45a6816 487 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
5539ae96 488 dev->features |= NETIF_F_TSO6;
c45a6816 489 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
5539ae96 490 dev->features |= NETIF_F_TSO_ECN;
c45a6816 491 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
5539ae96 492 dev->features |= NETIF_F_UFO;
296f96fc
RR
493 }
494
495 /* Configuration may specify what MAC to use. Otherwise random. */
c45a6816 496 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
a586d4f6
RR
497 vdev->config->get(vdev,
498 offsetof(struct virtio_net_config, mac),
499 dev->dev_addr, dev->addr_len);
296f96fc
RR
500 } else
501 random_ether_addr(dev->dev_addr);
502
503 /* Set up our device-specific information */
504 vi = netdev_priv(dev);
6c0cd7c0 505 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
296f96fc
RR
506 vi->dev = dev;
507 vi->vdev = vdev;
d9d5dcc8 508 vdev->priv = vi;
296f96fc 509
363f1514
RR
510 /* If they give us a callback when all buffers are done, we don't need
511 * the timer. */
512 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
513
97402b96
HX
514 /* If we can receive ANY GSO packets, we must allocate large ones. */
515 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
516 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
517 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
518 vi->big_packets = true;
519
296f96fc 520 /* We expect two virtqueues, receive then send. */
a586d4f6 521 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
296f96fc
RR
522 if (IS_ERR(vi->rvq)) {
523 err = PTR_ERR(vi->rvq);
524 goto free;
525 }
526
a586d4f6 527 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
296f96fc
RR
528 if (IS_ERR(vi->svq)) {
529 err = PTR_ERR(vi->svq);
530 goto free_recv;
531 }
532
533 /* Initialize our empty receive and send queues. */
534 skb_queue_head_init(&vi->recv);
535 skb_queue_head_init(&vi->send);
536
11a3a154
RR
537 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
538
363f1514
RR
539 if (!vi->free_in_tasklet)
540 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
14c998f0 541
296f96fc
RR
542 err = register_netdev(dev);
543 if (err) {
544 pr_debug("virtio_net: registering device failed\n");
545 goto free_send;
546 }
b3369c1f
RR
547
548 /* Last of all, set up some receive buffers. */
549 try_fill_recv(vi);
550
551 /* If we didn't even get one input buffer, we're useless. */
552 if (vi->num == 0) {
553 err = -ENOMEM;
554 goto unregister;
555 }
556
296f96fc 557 pr_debug("virtnet: registered device %s\n", dev->name);
296f96fc
RR
558 return 0;
559
b3369c1f
RR
560unregister:
561 unregister_netdev(dev);
296f96fc
RR
562free_send:
563 vdev->config->del_vq(vi->svq);
564free_recv:
565 vdev->config->del_vq(vi->rvq);
566free:
567 free_netdev(dev);
568 return err;
569}
570
571static void virtnet_remove(struct virtio_device *vdev)
572{
74b2553f 573 struct virtnet_info *vi = vdev->priv;
b3369c1f
RR
574 struct sk_buff *skb;
575
6e5aa7ef
RR
576 /* Stop all the virtqueues. */
577 vdev->config->reset(vdev);
578
363f1514
RR
579 if (!vi->free_in_tasklet)
580 del_timer_sync(&vi->xmit_free_timer);
14c998f0 581
b3369c1f 582 /* Free our skbs in send and recv queues, if any. */
b3369c1f
RR
583 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
584 kfree_skb(skb);
585 vi->num--;
586 }
288369cc 587 __skb_queue_purge(&vi->send);
b3369c1f
RR
588
589 BUG_ON(vi->num != 0);
74b2553f
RR
590
591 vdev->config->del_vq(vi->svq);
592 vdev->config->del_vq(vi->rvq);
593 unregister_netdev(vi->dev);
594 free_netdev(vi->dev);
296f96fc
RR
595}
596
597static struct virtio_device_id id_table[] = {
598 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
599 { 0 },
600};
601
c45a6816 602static unsigned int features[] = {
5e4fe5c4
MM
603 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
604 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
c45a6816 605 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
97402b96
HX
606 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
607 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
608 VIRTIO_F_NOTIFY_ON_EMPTY,
c45a6816
RR
609};
610
296f96fc 611static struct virtio_driver virtio_net = {
c45a6816
RR
612 .feature_table = features,
613 .feature_table_size = ARRAY_SIZE(features),
296f96fc
RR
614 .driver.name = KBUILD_MODNAME,
615 .driver.owner = THIS_MODULE,
616 .id_table = id_table,
617 .probe = virtnet_probe,
618 .remove = __devexit_p(virtnet_remove),
619};
620
621static int __init init(void)
622{
623 return register_virtio_driver(&virtio_net);
624}
625
626static void __exit fini(void)
627{
628 unregister_virtio_driver(&virtio_net);
629}
630module_init(init);
631module_exit(fini);
632
633MODULE_DEVICE_TABLE(virtio, id_table);
634MODULE_DESCRIPTION("Virtio network driver");
635MODULE_LICENSE("GPL");