virtio: remove unused id field from struct virtio_blk_outhdr
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
22#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
25#include <linux/scatterlist.h>
26
27/* FIXME: MTU in config. */
28#define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
29
30struct virtnet_info
31{
32 struct virtio_device *vdev;
33 struct virtqueue *rvq, *svq;
34 struct net_device *dev;
35 struct napi_struct napi;
36
37 /* Number of input buffers, and max we've ever had. */
38 unsigned int num, max;
39
40 /* Receive & send queues. */
41 struct sk_buff_head recv;
42 struct sk_buff_head send;
43};
44
45static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
46{
47 return (struct virtio_net_hdr *)skb->cb;
48}
49
50static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
51{
52 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
53}
54
18445c4d 55static void skb_xmit_done(struct virtqueue *rvq)
296f96fc
RR
56{
57 struct virtnet_info *vi = rvq->vdev->priv;
58
59 /* In case we were waiting for output buffers. */
60 netif_wake_queue(vi->dev);
296f96fc
RR
61}
62
63static void receive_skb(struct net_device *dev, struct sk_buff *skb,
64 unsigned len)
65{
66 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
67
68 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
69 pr_debug("%s: short packet %i\n", dev->name, len);
70 dev->stats.rx_length_errors++;
71 goto drop;
72 }
73 len -= sizeof(struct virtio_net_hdr);
74 BUG_ON(len > MAX_PACKET_LEN);
75
76 skb_trim(skb, len);
77 skb->protocol = eth_type_trans(skb, dev);
78 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
79 ntohs(skb->protocol), skb->len, skb->pkt_type);
80 dev->stats.rx_bytes += skb->len;
81 dev->stats.rx_packets++;
82
83 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
84 pr_debug("Needs csum!\n");
f35d9d8a 85 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
296f96fc 86 goto frame_err;
296f96fc
RR
87 }
88
89 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
90 pr_debug("GSO!\n");
91 switch (hdr->gso_type) {
92 case VIRTIO_NET_HDR_GSO_TCPV4:
93 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
94 break;
95 case VIRTIO_NET_HDR_GSO_TCPV4_ECN:
96 skb_shinfo(skb)->gso_type = SKB_GSO_TCP_ECN;
97 break;
98 case VIRTIO_NET_HDR_GSO_UDP:
99 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
100 break;
101 case VIRTIO_NET_HDR_GSO_TCPV6:
102 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
103 break;
104 default:
105 if (net_ratelimit())
106 printk(KERN_WARNING "%s: bad gso type %u.\n",
107 dev->name, hdr->gso_type);
108 goto frame_err;
109 }
110
111 skb_shinfo(skb)->gso_size = hdr->gso_size;
112 if (skb_shinfo(skb)->gso_size == 0) {
113 if (net_ratelimit())
114 printk(KERN_WARNING "%s: zero gso size.\n",
115 dev->name);
116 goto frame_err;
117 }
118
119 /* Header must be checked, and gso_segs computed. */
120 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
121 skb_shinfo(skb)->gso_segs = 0;
122 }
123
124 netif_receive_skb(skb);
125 return;
126
127frame_err:
128 dev->stats.rx_frame_errors++;
129drop:
130 dev_kfree_skb(skb);
131}
132
133static void try_fill_recv(struct virtnet_info *vi)
134{
135 struct sk_buff *skb;
136 struct scatterlist sg[1+MAX_SKB_FRAGS];
137 int num, err;
138
4d125de3 139 sg_init_table(sg, 1+MAX_SKB_FRAGS);
296f96fc
RR
140 for (;;) {
141 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
142 if (unlikely(!skb))
143 break;
144
145 skb_put(skb, MAX_PACKET_LEN);
146 vnet_hdr_to_sg(sg, skb);
147 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
148 skb_queue_head(&vi->recv, skb);
149
150 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
151 if (err) {
152 skb_unlink(skb, &vi->recv);
153 kfree_skb(skb);
154 break;
155 }
156 vi->num++;
157 }
158 if (unlikely(vi->num > vi->max))
159 vi->max = vi->num;
160 vi->rvq->vq_ops->kick(vi->rvq);
161}
162
18445c4d 163static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
164{
165 struct virtnet_info *vi = rvq->vdev->priv;
18445c4d
RR
166 /* Schedule NAPI, Suppress further interrupts if successful. */
167 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
168 rvq->vq_ops->disable_cb(rvq);
169 __netif_rx_schedule(vi->dev, &vi->napi);
170 }
296f96fc
RR
171}
172
173static int virtnet_poll(struct napi_struct *napi, int budget)
174{
175 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
176 struct sk_buff *skb = NULL;
177 unsigned int len, received = 0;
178
179again:
180 while (received < budget &&
181 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
182 __skb_unlink(skb, &vi->recv);
183 receive_skb(vi->dev, skb, len);
184 vi->num--;
185 received++;
186 }
187
188 /* FIXME: If we oom and completely run out of inbufs, we need
189 * to start a timer trying to fill more. */
190 if (vi->num < vi->max / 2)
191 try_fill_recv(vi);
192
8329d98e
RR
193 /* Out of packets? */
194 if (received < budget) {
296f96fc 195 netif_rx_complete(vi->dev, napi);
18445c4d 196 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
296f96fc
RR
197 && netif_rx_reschedule(vi->dev, napi))
198 goto again;
199 }
200
201 return received;
202}
203
204static void free_old_xmit_skbs(struct virtnet_info *vi)
205{
206 struct sk_buff *skb;
207 unsigned int len;
208
209 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
210 pr_debug("Sent skb %p\n", skb);
211 __skb_unlink(skb, &vi->send);
212 vi->dev->stats.tx_bytes += len;
213 vi->dev->stats.tx_packets++;
214 kfree_skb(skb);
215 }
216}
217
218static int start_xmit(struct sk_buff *skb, struct net_device *dev)
219{
220 struct virtnet_info *vi = netdev_priv(dev);
221 int num, err;
222 struct scatterlist sg[1+MAX_SKB_FRAGS];
223 struct virtio_net_hdr *hdr;
224 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
225 DECLARE_MAC_BUF(mac);
226
4d125de3
RR
227 sg_init_table(sg, 1+MAX_SKB_FRAGS);
228
296f96fc
RR
229 pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
230
231 free_old_xmit_skbs(vi);
232
233 /* Encode metadata header at front. */
234 hdr = skb_vnet_hdr(skb);
235 if (skb->ip_summed == CHECKSUM_PARTIAL) {
236 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
237 hdr->csum_start = skb->csum_start - skb_headroom(skb);
238 hdr->csum_offset = skb->csum_offset;
239 } else {
240 hdr->flags = 0;
241 hdr->csum_offset = hdr->csum_start = 0;
242 }
243
244 if (skb_is_gso(skb)) {
245 hdr->gso_size = skb_shinfo(skb)->gso_size;
246 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
247 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4_ECN;
248 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
249 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
250 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
251 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
252 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
253 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
254 else
255 BUG();
256 } else {
257 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
258 hdr->gso_size = 0;
259 }
260
261 vnet_hdr_to_sg(sg, skb);
262 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
263 __skb_queue_head(&vi->send, skb);
264 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
265 if (err) {
266 pr_debug("%s: virtio not prepared to send\n", dev->name);
267 skb_unlink(skb, &vi->send);
268 netif_stop_queue(dev);
269 return NETDEV_TX_BUSY;
270 }
271 vi->svq->vq_ops->kick(vi->svq);
272
273 return 0;
274}
275
276static int virtnet_open(struct net_device *dev)
277{
278 struct virtnet_info *vi = netdev_priv(dev);
279
280 try_fill_recv(vi);
281
282 /* If we didn't even get one input buffer, we're useless. */
283 if (vi->num == 0)
284 return -ENOMEM;
285
286 napi_enable(&vi->napi);
287 return 0;
288}
289
290static int virtnet_close(struct net_device *dev)
291{
292 struct virtnet_info *vi = netdev_priv(dev);
293 struct sk_buff *skb;
294
295 napi_disable(&vi->napi);
296
297 /* networking core has neutered skb_xmit_done/skb_recv_done, so don't
298 * worry about races vs. get(). */
299 vi->rvq->vq_ops->shutdown(vi->rvq);
300 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
301 kfree_skb(skb);
302 vi->num--;
303 }
304 vi->svq->vq_ops->shutdown(vi->svq);
305 while ((skb = __skb_dequeue(&vi->send)) != NULL)
306 kfree_skb(skb);
307
308 BUG_ON(vi->num != 0);
309 return 0;
310}
311
312static int virtnet_probe(struct virtio_device *vdev)
313{
314 int err;
296f96fc
RR
315 struct net_device *dev;
316 struct virtnet_info *vi;
296f96fc
RR
317
318 /* Allocate ourselves a network device with room for our info */
319 dev = alloc_etherdev(sizeof(struct virtnet_info));
320 if (!dev)
321 return -ENOMEM;
322
323 /* Set up network device as normal. */
324 ether_setup(dev);
325 dev->open = virtnet_open;
326 dev->stop = virtnet_close;
327 dev->hard_start_xmit = start_xmit;
328 dev->features = NETIF_F_HIGHDMA;
329 SET_NETDEV_DEV(dev, &vdev->dev);
330
331 /* Do we support "hardware" checksums? */
a586d4f6 332 if (vdev->config->feature(vdev, VIRTIO_NET_F_NO_CSUM)) {
296f96fc
RR
333 /* This opens up the world of extra features. */
334 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
a586d4f6 335 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO4))
296f96fc 336 dev->features |= NETIF_F_TSO;
a586d4f6 337 if (vdev->config->feature(vdev, VIRTIO_NET_F_UFO))
296f96fc 338 dev->features |= NETIF_F_UFO;
a586d4f6 339 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO4_ECN))
296f96fc 340 dev->features |= NETIF_F_TSO_ECN;
a586d4f6 341 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO6))
296f96fc
RR
342 dev->features |= NETIF_F_TSO6;
343 }
344
345 /* Configuration may specify what MAC to use. Otherwise random. */
a586d4f6
RR
346 if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) {
347 vdev->config->get(vdev,
348 offsetof(struct virtio_net_config, mac),
349 dev->dev_addr, dev->addr_len);
296f96fc
RR
350 } else
351 random_ether_addr(dev->dev_addr);
352
353 /* Set up our device-specific information */
354 vi = netdev_priv(dev);
355 netif_napi_add(dev, &vi->napi, virtnet_poll, 16);
356 vi->dev = dev;
357 vi->vdev = vdev;
358
359 /* We expect two virtqueues, receive then send. */
a586d4f6 360 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
296f96fc
RR
361 if (IS_ERR(vi->rvq)) {
362 err = PTR_ERR(vi->rvq);
363 goto free;
364 }
365
a586d4f6 366 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
296f96fc
RR
367 if (IS_ERR(vi->svq)) {
368 err = PTR_ERR(vi->svq);
369 goto free_recv;
370 }
371
372 /* Initialize our empty receive and send queues. */
373 skb_queue_head_init(&vi->recv);
374 skb_queue_head_init(&vi->send);
375
376 err = register_netdev(dev);
377 if (err) {
378 pr_debug("virtio_net: registering device failed\n");
379 goto free_send;
380 }
381 pr_debug("virtnet: registered device %s\n", dev->name);
382 vdev->priv = vi;
383 return 0;
384
385free_send:
386 vdev->config->del_vq(vi->svq);
387free_recv:
388 vdev->config->del_vq(vi->rvq);
389free:
390 free_netdev(dev);
391 return err;
392}
393
394static void virtnet_remove(struct virtio_device *vdev)
395{
74b2553f
RR
396 struct virtnet_info *vi = vdev->priv;
397
398 vdev->config->del_vq(vi->svq);
399 vdev->config->del_vq(vi->rvq);
400 unregister_netdev(vi->dev);
401 free_netdev(vi->dev);
296f96fc
RR
402}
403
404static struct virtio_device_id id_table[] = {
405 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
406 { 0 },
407};
408
409static struct virtio_driver virtio_net = {
410 .driver.name = KBUILD_MODNAME,
411 .driver.owner = THIS_MODULE,
412 .id_table = id_table,
413 .probe = virtnet_probe,
414 .remove = __devexit_p(virtnet_remove),
415};
416
417static int __init init(void)
418{
419 return register_virtio_driver(&virtio_net);
420}
421
422static void __exit fini(void)
423{
424 unregister_virtio_driver(&virtio_net);
425}
426module_init(init);
427module_exit(fini);
428
429MODULE_DEVICE_TABLE(virtio, id_table);
430MODULE_DESCRIPTION("Virtio network driver");
431MODULE_LICENSE("GPL");