1 /* A simple network driver using virtio.
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
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.
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.
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
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/module.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_net.h>
25 #include <linux/scatterlist.h>
27 static int csum
= 1, gso
= 1;
28 module_param(csum
, bool, 0444);
29 module_param(gso
, bool, 0444);
31 /* FIXME: MTU in config. */
32 #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
36 struct virtio_device
*vdev
;
37 struct virtqueue
*rvq
, *svq
;
38 struct net_device
*dev
;
39 struct napi_struct napi
;
41 /* Number of input buffers, and max we've ever had. */
42 unsigned int num
, max
;
44 /* Receive & send queues. */
45 struct sk_buff_head recv
;
46 struct sk_buff_head send
;
49 static inline struct virtio_net_hdr
*skb_vnet_hdr(struct sk_buff
*skb
)
51 return (struct virtio_net_hdr
*)skb
->cb
;
54 static inline void vnet_hdr_to_sg(struct scatterlist
*sg
, struct sk_buff
*skb
)
56 sg_init_one(sg
, skb_vnet_hdr(skb
), sizeof(struct virtio_net_hdr
));
59 static void skb_xmit_done(struct virtqueue
*rvq
)
61 struct virtnet_info
*vi
= rvq
->vdev
->priv
;
63 /* In case we were waiting for output buffers. */
64 netif_wake_queue(vi
->dev
);
67 static void receive_skb(struct net_device
*dev
, struct sk_buff
*skb
,
70 struct virtio_net_hdr
*hdr
= skb_vnet_hdr(skb
);
72 if (unlikely(len
< sizeof(struct virtio_net_hdr
) + ETH_HLEN
)) {
73 pr_debug("%s: short packet %i\n", dev
->name
, len
);
74 dev
->stats
.rx_length_errors
++;
77 len
-= sizeof(struct virtio_net_hdr
);
78 BUG_ON(len
> MAX_PACKET_LEN
);
81 skb
->protocol
= eth_type_trans(skb
, dev
);
82 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
83 ntohs(skb
->protocol
), skb
->len
, skb
->pkt_type
);
84 dev
->stats
.rx_bytes
+= skb
->len
;
85 dev
->stats
.rx_packets
++;
87 if (hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
88 pr_debug("Needs csum!\n");
89 if (!skb_partial_csum_set(skb
,hdr
->csum_start
,hdr
->csum_offset
))
93 if (hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
95 switch (hdr
->gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
96 case VIRTIO_NET_HDR_GSO_TCPV4
:
97 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
99 case VIRTIO_NET_HDR_GSO_UDP
:
100 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
102 case VIRTIO_NET_HDR_GSO_TCPV6
:
103 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
107 printk(KERN_WARNING
"%s: bad gso type %u.\n",
108 dev
->name
, hdr
->gso_type
);
112 if (hdr
->gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
113 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
115 skb_shinfo(skb
)->gso_size
= hdr
->gso_size
;
116 if (skb_shinfo(skb
)->gso_size
== 0) {
118 printk(KERN_WARNING
"%s: zero gso size.\n",
123 /* Header must be checked, and gso_segs computed. */
124 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
125 skb_shinfo(skb
)->gso_segs
= 0;
128 netif_receive_skb(skb
);
132 dev
->stats
.rx_frame_errors
++;
137 static void try_fill_recv(struct virtnet_info
*vi
)
140 struct scatterlist sg
[1+MAX_SKB_FRAGS
];
143 sg_init_table(sg
, 1+MAX_SKB_FRAGS
);
145 skb
= netdev_alloc_skb(vi
->dev
, MAX_PACKET_LEN
);
149 skb_put(skb
, MAX_PACKET_LEN
);
150 vnet_hdr_to_sg(sg
, skb
);
151 num
= skb_to_sgvec(skb
, sg
+1, 0, skb
->len
) + 1;
152 skb_queue_head(&vi
->recv
, skb
);
154 err
= vi
->rvq
->vq_ops
->add_buf(vi
->rvq
, sg
, 0, num
, skb
);
156 skb_unlink(skb
, &vi
->recv
);
162 if (unlikely(vi
->num
> vi
->max
))
164 vi
->rvq
->vq_ops
->kick(vi
->rvq
);
167 static void skb_recv_done(struct virtqueue
*rvq
)
169 struct virtnet_info
*vi
= rvq
->vdev
->priv
;
170 /* Schedule NAPI, Suppress further interrupts if successful. */
171 if (netif_rx_schedule_prep(vi
->dev
, &vi
->napi
)) {
172 rvq
->vq_ops
->disable_cb(rvq
);
173 __netif_rx_schedule(vi
->dev
, &vi
->napi
);
177 static int virtnet_poll(struct napi_struct
*napi
, int budget
)
179 struct virtnet_info
*vi
= container_of(napi
, struct virtnet_info
, napi
);
180 struct sk_buff
*skb
= NULL
;
181 unsigned int len
, received
= 0;
184 while (received
< budget
&&
185 (skb
= vi
->rvq
->vq_ops
->get_buf(vi
->rvq
, &len
)) != NULL
) {
186 __skb_unlink(skb
, &vi
->recv
);
187 receive_skb(vi
->dev
, skb
, len
);
192 /* FIXME: If we oom and completely run out of inbufs, we need
193 * to start a timer trying to fill more. */
194 if (vi
->num
< vi
->max
/ 2)
197 /* Out of packets? */
198 if (received
< budget
) {
199 netif_rx_complete(vi
->dev
, napi
);
200 if (unlikely(!vi
->rvq
->vq_ops
->enable_cb(vi
->rvq
))
201 && netif_rx_reschedule(vi
->dev
, napi
))
208 static void free_old_xmit_skbs(struct virtnet_info
*vi
)
213 while ((skb
= vi
->svq
->vq_ops
->get_buf(vi
->svq
, &len
)) != NULL
) {
214 pr_debug("Sent skb %p\n", skb
);
215 __skb_unlink(skb
, &vi
->send
);
216 vi
->dev
->stats
.tx_bytes
+= len
;
217 vi
->dev
->stats
.tx_packets
++;
222 static int start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
224 struct virtnet_info
*vi
= netdev_priv(dev
);
226 struct scatterlist sg
[1+MAX_SKB_FRAGS
];
227 struct virtio_net_hdr
*hdr
;
228 const unsigned char *dest
= ((struct ethhdr
*)skb
->data
)->h_dest
;
229 DECLARE_MAC_BUF(mac
);
231 sg_init_table(sg
, 1+MAX_SKB_FRAGS
);
233 pr_debug("%s: xmit %p %s\n", dev
->name
, skb
, print_mac(mac
, dest
));
235 free_old_xmit_skbs(vi
);
237 /* Encode metadata header at front. */
238 hdr
= skb_vnet_hdr(skb
);
239 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
240 hdr
->flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
241 hdr
->csum_start
= skb
->csum_start
- skb_headroom(skb
);
242 hdr
->csum_offset
= skb
->csum_offset
;
245 hdr
->csum_offset
= hdr
->csum_start
= 0;
248 if (skb_is_gso(skb
)) {
249 hdr
->hdr_len
= skb_transport_header(skb
) - skb
->data
;
250 hdr
->gso_size
= skb_shinfo(skb
)->gso_size
;
251 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV4
)
252 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
253 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
)
254 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
255 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
256 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
259 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCP_ECN
)
260 hdr
->gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
262 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
263 hdr
->gso_size
= hdr
->hdr_len
= 0;
266 vnet_hdr_to_sg(sg
, skb
);
267 num
= skb_to_sgvec(skb
, sg
+1, 0, skb
->len
) + 1;
268 __skb_queue_head(&vi
->send
, skb
);
269 err
= vi
->svq
->vq_ops
->add_buf(vi
->svq
, sg
, num
, 0, skb
);
271 pr_debug("%s: virtio not prepared to send\n", dev
->name
);
272 skb_unlink(skb
, &vi
->send
);
273 netif_stop_queue(dev
);
274 return NETDEV_TX_BUSY
;
276 vi
->svq
->vq_ops
->kick(vi
->svq
);
281 static int virtnet_open(struct net_device
*dev
)
283 struct virtnet_info
*vi
= netdev_priv(dev
);
285 napi_enable(&vi
->napi
);
289 static int virtnet_close(struct net_device
*dev
)
291 struct virtnet_info
*vi
= netdev_priv(dev
);
293 napi_disable(&vi
->napi
);
298 static int virtnet_probe(struct virtio_device
*vdev
)
301 struct net_device
*dev
;
302 struct virtnet_info
*vi
;
304 /* Allocate ourselves a network device with room for our info */
305 dev
= alloc_etherdev(sizeof(struct virtnet_info
));
309 /* Set up network device as normal. */
310 dev
->open
= virtnet_open
;
311 dev
->stop
= virtnet_close
;
312 dev
->hard_start_xmit
= start_xmit
;
313 dev
->features
= NETIF_F_HIGHDMA
;
314 SET_NETDEV_DEV(dev
, &vdev
->dev
);
316 /* Do we support "hardware" checksums? */
317 if (csum
&& vdev
->config
->feature(vdev
, VIRTIO_NET_F_CSUM
)) {
318 /* This opens up the world of extra features. */
319 dev
->features
|= NETIF_F_HW_CSUM
|NETIF_F_SG
|NETIF_F_FRAGLIST
;
320 if (gso
&& vdev
->config
->feature(vdev
, VIRTIO_NET_F_GSO
)) {
321 dev
->features
|= NETIF_F_TSO
| NETIF_F_UFO
322 | NETIF_F_TSO_ECN
| NETIF_F_TSO6
;
326 /* Configuration may specify what MAC to use. Otherwise random. */
327 if (vdev
->config
->feature(vdev
, VIRTIO_NET_F_MAC
)) {
328 vdev
->config
->get(vdev
,
329 offsetof(struct virtio_net_config
, mac
),
330 dev
->dev_addr
, dev
->addr_len
);
332 random_ether_addr(dev
->dev_addr
);
334 /* Set up our device-specific information */
335 vi
= netdev_priv(dev
);
336 netif_napi_add(dev
, &vi
->napi
, virtnet_poll
, 16);
340 /* We expect two virtqueues, receive then send. */
341 vi
->rvq
= vdev
->config
->find_vq(vdev
, 0, skb_recv_done
);
342 if (IS_ERR(vi
->rvq
)) {
343 err
= PTR_ERR(vi
->rvq
);
347 vi
->svq
= vdev
->config
->find_vq(vdev
, 1, skb_xmit_done
);
348 if (IS_ERR(vi
->svq
)) {
349 err
= PTR_ERR(vi
->svq
);
353 /* Initialize our empty receive and send queues. */
354 skb_queue_head_init(&vi
->recv
);
355 skb_queue_head_init(&vi
->send
);
357 err
= register_netdev(dev
);
359 pr_debug("virtio_net: registering device failed\n");
363 /* Last of all, set up some receive buffers. */
366 /* If we didn't even get one input buffer, we're useless. */
372 pr_debug("virtnet: registered device %s\n", dev
->name
);
377 unregister_netdev(dev
);
379 vdev
->config
->del_vq(vi
->svq
);
381 vdev
->config
->del_vq(vi
->rvq
);
387 static void virtnet_remove(struct virtio_device
*vdev
)
389 struct virtnet_info
*vi
= vdev
->priv
;
392 /* Stop all the virtqueues. */
393 vdev
->config
->reset(vdev
);
395 /* Free our skbs in send and recv queues, if any. */
396 while ((skb
= __skb_dequeue(&vi
->recv
)) != NULL
) {
400 while ((skb
= __skb_dequeue(&vi
->send
)) != NULL
)
403 BUG_ON(vi
->num
!= 0);
405 vdev
->config
->del_vq(vi
->svq
);
406 vdev
->config
->del_vq(vi
->rvq
);
407 unregister_netdev(vi
->dev
);
408 free_netdev(vi
->dev
);
411 static struct virtio_device_id id_table
[] = {
412 { VIRTIO_ID_NET
, VIRTIO_DEV_ANY_ID
},
416 static struct virtio_driver virtio_net
= {
417 .driver
.name
= KBUILD_MODNAME
,
418 .driver
.owner
= THIS_MODULE
,
419 .id_table
= id_table
,
420 .probe
= virtnet_probe
,
421 .remove
= __devexit_p(virtnet_remove
),
424 static int __init
init(void)
426 return register_virtio_driver(&virtio_net
);
429 static void __exit
fini(void)
431 unregister_virtio_driver(&virtio_net
);
436 MODULE_DEVICE_TABLE(virtio
, id_table
);
437 MODULE_DESCRIPTION("Virtio network driver");
438 MODULE_LICENSE("GPL");