]>
Commit | Line | Data |
---|---|---|
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 | ||
34a48579 RR |
27 | static int csum = 1, gso = 1; |
28 | module_param(csum, bool, 0444); | |
29 | module_param(gso, bool, 0444); | |
30 | ||
296f96fc RR |
31 | /* FIXME: MTU in config. */ |
32 | #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN) | |
33 | ||
34 | struct virtnet_info | |
35 | { | |
36 | struct virtio_device *vdev; | |
37 | struct virtqueue *rvq, *svq; | |
38 | struct net_device *dev; | |
39 | struct napi_struct napi; | |
40 | ||
41 | /* Number of input buffers, and max we've ever had. */ | |
42 | unsigned int num, max; | |
43 | ||
44 | /* Receive & send queues. */ | |
45 | struct sk_buff_head recv; | |
46 | struct sk_buff_head send; | |
47 | }; | |
48 | ||
49 | static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb) | |
50 | { | |
51 | return (struct virtio_net_hdr *)skb->cb; | |
52 | } | |
53 | ||
54 | static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb) | |
55 | { | |
56 | sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr)); | |
57 | } | |
58 | ||
18445c4d | 59 | static void skb_xmit_done(struct virtqueue *rvq) |
296f96fc RR |
60 | { |
61 | struct virtnet_info *vi = rvq->vdev->priv; | |
62 | ||
63 | /* In case we were waiting for output buffers. */ | |
64 | netif_wake_queue(vi->dev); | |
296f96fc RR |
65 | } |
66 | ||
67 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, | |
68 | unsigned len) | |
69 | { | |
70 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); | |
71 | ||
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++; | |
75 | goto drop; | |
76 | } | |
77 | len -= sizeof(struct virtio_net_hdr); | |
78 | BUG_ON(len > MAX_PACKET_LEN); | |
79 | ||
80 | skb_trim(skb, 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++; | |
86 | ||
87 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { | |
88 | pr_debug("Needs csum!\n"); | |
f35d9d8a | 89 | if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) |
296f96fc | 90 | goto frame_err; |
296f96fc RR |
91 | } |
92 | ||
93 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { | |
94 | pr_debug("GSO!\n"); | |
34a48579 | 95 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
296f96fc RR |
96 | case VIRTIO_NET_HDR_GSO_TCPV4: |
97 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; | |
98 | break; | |
296f96fc RR |
99 | case VIRTIO_NET_HDR_GSO_UDP: |
100 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; | |
101 | break; | |
102 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
103 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; | |
104 | break; | |
105 | default: | |
106 | if (net_ratelimit()) | |
107 | printk(KERN_WARNING "%s: bad gso type %u.\n", | |
108 | dev->name, hdr->gso_type); | |
109 | goto frame_err; | |
110 | } | |
111 | ||
34a48579 RR |
112 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
113 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; | |
114 | ||
296f96fc RR |
115 | skb_shinfo(skb)->gso_size = hdr->gso_size; |
116 | if (skb_shinfo(skb)->gso_size == 0) { | |
117 | if (net_ratelimit()) | |
118 | printk(KERN_WARNING "%s: zero gso size.\n", | |
119 | dev->name); | |
120 | goto frame_err; | |
121 | } | |
122 | ||
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; | |
126 | } | |
127 | ||
128 | netif_receive_skb(skb); | |
129 | return; | |
130 | ||
131 | frame_err: | |
132 | dev->stats.rx_frame_errors++; | |
133 | drop: | |
134 | dev_kfree_skb(skb); | |
135 | } | |
136 | ||
137 | static void try_fill_recv(struct virtnet_info *vi) | |
138 | { | |
139 | struct sk_buff *skb; | |
140 | struct scatterlist sg[1+MAX_SKB_FRAGS]; | |
141 | int num, err; | |
142 | ||
4d125de3 | 143 | sg_init_table(sg, 1+MAX_SKB_FRAGS); |
296f96fc RR |
144 | for (;;) { |
145 | skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); | |
146 | if (unlikely(!skb)) | |
147 | break; | |
148 | ||
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); | |
153 | ||
154 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); | |
155 | if (err) { | |
156 | skb_unlink(skb, &vi->recv); | |
157 | kfree_skb(skb); | |
158 | break; | |
159 | } | |
160 | vi->num++; | |
161 | } | |
162 | if (unlikely(vi->num > vi->max)) | |
163 | vi->max = vi->num; | |
164 | vi->rvq->vq_ops->kick(vi->rvq); | |
165 | } | |
166 | ||
18445c4d | 167 | static void skb_recv_done(struct virtqueue *rvq) |
296f96fc RR |
168 | { |
169 | struct virtnet_info *vi = rvq->vdev->priv; | |
18445c4d RR |
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); | |
174 | } | |
296f96fc RR |
175 | } |
176 | ||
177 | static int virtnet_poll(struct napi_struct *napi, int budget) | |
178 | { | |
179 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); | |
180 | struct sk_buff *skb = NULL; | |
181 | unsigned int len, received = 0; | |
182 | ||
183 | again: | |
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); | |
188 | vi->num--; | |
189 | received++; | |
190 | } | |
191 | ||
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) | |
195 | try_fill_recv(vi); | |
196 | ||
8329d98e RR |
197 | /* Out of packets? */ |
198 | if (received < budget) { | |
296f96fc | 199 | netif_rx_complete(vi->dev, napi); |
18445c4d | 200 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) |
296f96fc RR |
201 | && netif_rx_reschedule(vi->dev, napi)) |
202 | goto again; | |
203 | } | |
204 | ||
205 | return received; | |
206 | } | |
207 | ||
208 | static void free_old_xmit_skbs(struct virtnet_info *vi) | |
209 | { | |
210 | struct sk_buff *skb; | |
211 | unsigned int len; | |
212 | ||
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++; | |
218 | kfree_skb(skb); | |
219 | } | |
220 | } | |
221 | ||
222 | static int start_xmit(struct sk_buff *skb, struct net_device *dev) | |
223 | { | |
224 | struct virtnet_info *vi = netdev_priv(dev); | |
225 | int num, err; | |
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); | |
230 | ||
4d125de3 RR |
231 | sg_init_table(sg, 1+MAX_SKB_FRAGS); |
232 | ||
296f96fc RR |
233 | pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest)); |
234 | ||
235 | free_old_xmit_skbs(vi); | |
236 | ||
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; | |
243 | } else { | |
244 | hdr->flags = 0; | |
245 | hdr->csum_offset = hdr->csum_start = 0; | |
246 | } | |
247 | ||
248 | if (skb_is_gso(skb)) { | |
50c8ea80 | 249 | hdr->hdr_len = skb_transport_header(skb) - skb->data; |
296f96fc | 250 | hdr->gso_size = skb_shinfo(skb)->gso_size; |
34a48579 | 251 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
296f96fc RR |
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; | |
257 | else | |
258 | BUG(); | |
34a48579 RR |
259 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
260 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; | |
296f96fc RR |
261 | } else { |
262 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
50c8ea80 | 263 | hdr->gso_size = hdr->hdr_len = 0; |
296f96fc RR |
264 | } |
265 | ||
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); | |
270 | if (err) { | |
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; | |
275 | } | |
276 | vi->svq->vq_ops->kick(vi->svq); | |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
281 | static int virtnet_open(struct net_device *dev) | |
282 | { | |
283 | struct virtnet_info *vi = netdev_priv(dev); | |
284 | ||
296f96fc RR |
285 | napi_enable(&vi->napi); |
286 | return 0; | |
287 | } | |
288 | ||
289 | static int virtnet_close(struct net_device *dev) | |
290 | { | |
291 | struct virtnet_info *vi = netdev_priv(dev); | |
296f96fc RR |
292 | |
293 | napi_disable(&vi->napi); | |
294 | ||
296f96fc RR |
295 | return 0; |
296 | } | |
297 | ||
298 | static int virtnet_probe(struct virtio_device *vdev) | |
299 | { | |
300 | int err; | |
296f96fc RR |
301 | struct net_device *dev; |
302 | struct virtnet_info *vi; | |
296f96fc RR |
303 | |
304 | /* Allocate ourselves a network device with room for our info */ | |
305 | dev = alloc_etherdev(sizeof(struct virtnet_info)); | |
306 | if (!dev) | |
307 | return -ENOMEM; | |
308 | ||
309 | /* Set up network device as normal. */ | |
296f96fc RR |
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); | |
315 | ||
316 | /* Do we support "hardware" checksums? */ | |
34a48579 | 317 | if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) { |
296f96fc RR |
318 | /* This opens up the world of extra features. */ |
319 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; | |
34a48579 RR |
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; | |
323 | } | |
296f96fc RR |
324 | } |
325 | ||
326 | /* Configuration may specify what MAC to use. Otherwise random. */ | |
a586d4f6 RR |
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); | |
296f96fc RR |
331 | } else |
332 | random_ether_addr(dev->dev_addr); | |
333 | ||
334 | /* Set up our device-specific information */ | |
335 | vi = netdev_priv(dev); | |
336 | netif_napi_add(dev, &vi->napi, virtnet_poll, 16); | |
337 | vi->dev = dev; | |
338 | vi->vdev = vdev; | |
339 | ||
340 | /* We expect two virtqueues, receive then send. */ | |
a586d4f6 | 341 | vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); |
296f96fc RR |
342 | if (IS_ERR(vi->rvq)) { |
343 | err = PTR_ERR(vi->rvq); | |
344 | goto free; | |
345 | } | |
346 | ||
a586d4f6 | 347 | vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); |
296f96fc RR |
348 | if (IS_ERR(vi->svq)) { |
349 | err = PTR_ERR(vi->svq); | |
350 | goto free_recv; | |
351 | } | |
352 | ||
353 | /* Initialize our empty receive and send queues. */ | |
354 | skb_queue_head_init(&vi->recv); | |
355 | skb_queue_head_init(&vi->send); | |
356 | ||
357 | err = register_netdev(dev); | |
358 | if (err) { | |
359 | pr_debug("virtio_net: registering device failed\n"); | |
360 | goto free_send; | |
361 | } | |
b3369c1f RR |
362 | |
363 | /* Last of all, set up some receive buffers. */ | |
364 | try_fill_recv(vi); | |
365 | ||
366 | /* If we didn't even get one input buffer, we're useless. */ | |
367 | if (vi->num == 0) { | |
368 | err = -ENOMEM; | |
369 | goto unregister; | |
370 | } | |
371 | ||
296f96fc RR |
372 | pr_debug("virtnet: registered device %s\n", dev->name); |
373 | vdev->priv = vi; | |
374 | return 0; | |
375 | ||
b3369c1f RR |
376 | unregister: |
377 | unregister_netdev(dev); | |
296f96fc RR |
378 | free_send: |
379 | vdev->config->del_vq(vi->svq); | |
380 | free_recv: | |
381 | vdev->config->del_vq(vi->rvq); | |
382 | free: | |
383 | free_netdev(dev); | |
384 | return err; | |
385 | } | |
386 | ||
387 | static void virtnet_remove(struct virtio_device *vdev) | |
388 | { | |
74b2553f | 389 | struct virtnet_info *vi = vdev->priv; |
b3369c1f RR |
390 | struct sk_buff *skb; |
391 | ||
6e5aa7ef RR |
392 | /* Stop all the virtqueues. */ |
393 | vdev->config->reset(vdev); | |
394 | ||
b3369c1f | 395 | /* Free our skbs in send and recv queues, if any. */ |
b3369c1f RR |
396 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
397 | kfree_skb(skb); | |
398 | vi->num--; | |
399 | } | |
b3369c1f RR |
400 | while ((skb = __skb_dequeue(&vi->send)) != NULL) |
401 | kfree_skb(skb); | |
402 | ||
403 | BUG_ON(vi->num != 0); | |
74b2553f RR |
404 | |
405 | vdev->config->del_vq(vi->svq); | |
406 | vdev->config->del_vq(vi->rvq); | |
407 | unregister_netdev(vi->dev); | |
408 | free_netdev(vi->dev); | |
296f96fc RR |
409 | } |
410 | ||
411 | static struct virtio_device_id id_table[] = { | |
412 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, | |
413 | { 0 }, | |
414 | }; | |
415 | ||
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), | |
422 | }; | |
423 | ||
424 | static int __init init(void) | |
425 | { | |
426 | return register_virtio_driver(&virtio_net); | |
427 | } | |
428 | ||
429 | static void __exit fini(void) | |
430 | { | |
431 | unregister_virtio_driver(&virtio_net); | |
432 | } | |
433 | module_init(init); | |
434 | module_exit(fini); | |
435 | ||
436 | MODULE_DEVICE_TABLE(virtio, id_table); | |
437 | MODULE_DESCRIPTION("Virtio network driver"); | |
438 | MODULE_LICENSE("GPL"); |