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