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