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> | |
a9ea3fc6 | 22 | #include <linux/ethtool.h> |
296f96fc RR |
23 | #include <linux/module.h> |
24 | #include <linux/virtio.h> | |
25 | #include <linux/virtio_net.h> | |
26 | #include <linux/scatterlist.h> | |
27 | ||
6c0cd7c0 DL |
28 | static int napi_weight = 128; |
29 | module_param(napi_weight, int, 0444); | |
30 | ||
34a48579 RR |
31 | static int csum = 1, gso = 1; |
32 | module_param(csum, bool, 0444); | |
33 | module_param(gso, bool, 0444); | |
34 | ||
296f96fc RR |
35 | /* FIXME: MTU in config. */ |
36 | #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN) | |
3f2c31d9 | 37 | #define GOOD_COPY_LEN 128 |
296f96fc RR |
38 | |
39 | struct virtnet_info | |
40 | { | |
41 | struct virtio_device *vdev; | |
42 | struct virtqueue *rvq, *svq; | |
43 | struct net_device *dev; | |
44 | struct napi_struct napi; | |
45 | ||
99ffc696 RR |
46 | /* The skb we couldn't send because buffers were full. */ |
47 | struct sk_buff *last_xmit_skb; | |
48 | ||
363f1514 | 49 | /* If we need to free in a timer, this is it. */ |
14c998f0 MM |
50 | struct timer_list xmit_free_timer; |
51 | ||
296f96fc RR |
52 | /* Number of input buffers, and max we've ever had. */ |
53 | unsigned int num, max; | |
54 | ||
11a3a154 RR |
55 | /* For cleaning up after transmission. */ |
56 | struct tasklet_struct tasklet; | |
363f1514 | 57 | bool free_in_tasklet; |
11a3a154 | 58 | |
97402b96 HX |
59 | /* I like... big packets and I cannot lie! */ |
60 | bool big_packets; | |
61 | ||
3f2c31d9 MM |
62 | /* Host will merge rx buffers for big packets (shake it! shake it!) */ |
63 | bool mergeable_rx_bufs; | |
64 | ||
296f96fc RR |
65 | /* Receive & send queues. */ |
66 | struct sk_buff_head recv; | |
67 | struct sk_buff_head send; | |
fb6813f4 RR |
68 | |
69 | /* Chain pages by the private ptr. */ | |
70 | struct page *pages; | |
296f96fc RR |
71 | }; |
72 | ||
3f2c31d9 | 73 | static inline void *skb_vnet_hdr(struct sk_buff *skb) |
296f96fc RR |
74 | { |
75 | return (struct virtio_net_hdr *)skb->cb; | |
76 | } | |
77 | ||
fb6813f4 RR |
78 | static void give_a_page(struct virtnet_info *vi, struct page *page) |
79 | { | |
80 | page->private = (unsigned long)vi->pages; | |
81 | vi->pages = page; | |
82 | } | |
83 | ||
0a888fd1 MM |
84 | static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb) |
85 | { | |
86 | unsigned int i; | |
87 | ||
88 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
89 | give_a_page(vi, skb_shinfo(skb)->frags[i].page); | |
90 | skb_shinfo(skb)->nr_frags = 0; | |
91 | skb->data_len = 0; | |
92 | } | |
93 | ||
fb6813f4 RR |
94 | static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask) |
95 | { | |
96 | struct page *p = vi->pages; | |
97 | ||
98 | if (p) | |
99 | vi->pages = (struct page *)p->private; | |
100 | else | |
101 | p = alloc_page(gfp_mask); | |
102 | return p; | |
103 | } | |
104 | ||
2cb9c6ba | 105 | static void skb_xmit_done(struct virtqueue *svq) |
296f96fc | 106 | { |
2cb9c6ba | 107 | struct virtnet_info *vi = svq->vdev->priv; |
296f96fc | 108 | |
2cb9c6ba RR |
109 | /* Suppress further interrupts. */ |
110 | svq->vq_ops->disable_cb(svq); | |
11a3a154 | 111 | |
363f1514 | 112 | /* We were probably waiting for more output buffers. */ |
296f96fc | 113 | netif_wake_queue(vi->dev); |
11a3a154 RR |
114 | |
115 | /* Make sure we re-xmit last_xmit_skb: if there are no more packets | |
116 | * queued, start_xmit won't be called. */ | |
117 | tasklet_schedule(&vi->tasklet); | |
296f96fc RR |
118 | } |
119 | ||
120 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, | |
121 | unsigned len) | |
122 | { | |
3f2c31d9 | 123 | struct virtnet_info *vi = netdev_priv(dev); |
296f96fc | 124 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); |
97402b96 | 125 | int err; |
3f2c31d9 | 126 | int i; |
296f96fc RR |
127 | |
128 | if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { | |
129 | pr_debug("%s: short packet %i\n", dev->name, len); | |
130 | dev->stats.rx_length_errors++; | |
131 | goto drop; | |
132 | } | |
23cde76d | 133 | |
3f2c31d9 MM |
134 | if (vi->mergeable_rx_bufs) { |
135 | struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); | |
136 | unsigned int copy; | |
137 | char *p = page_address(skb_shinfo(skb)->frags[0].page); | |
fb6813f4 | 138 | |
3f2c31d9 MM |
139 | if (len > PAGE_SIZE) |
140 | len = PAGE_SIZE; | |
141 | len -= sizeof(struct virtio_net_hdr_mrg_rxbuf); | |
142 | ||
143 | memcpy(hdr, p, sizeof(*mhdr)); | |
144 | p += sizeof(*mhdr); | |
145 | ||
146 | copy = len; | |
147 | if (copy > skb_tailroom(skb)) | |
148 | copy = skb_tailroom(skb); | |
149 | ||
150 | memcpy(skb_put(skb, copy), p, copy); | |
151 | ||
152 | len -= copy; | |
153 | ||
154 | if (!len) { | |
155 | give_a_page(vi, skb_shinfo(skb)->frags[0].page); | |
156 | skb_shinfo(skb)->nr_frags--; | |
157 | } else { | |
158 | skb_shinfo(skb)->frags[0].page_offset += | |
159 | sizeof(*mhdr) + copy; | |
160 | skb_shinfo(skb)->frags[0].size = len; | |
161 | skb->data_len += len; | |
162 | skb->len += len; | |
163 | } | |
164 | ||
165 | while (--mhdr->num_buffers) { | |
166 | struct sk_buff *nskb; | |
167 | ||
168 | i = skb_shinfo(skb)->nr_frags; | |
169 | if (i >= MAX_SKB_FRAGS) { | |
170 | pr_debug("%s: packet too long %d\n", dev->name, | |
171 | len); | |
172 | dev->stats.rx_length_errors++; | |
173 | goto drop; | |
174 | } | |
175 | ||
176 | nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len); | |
177 | if (!nskb) { | |
178 | pr_debug("%s: rx error: %d buffers missing\n", | |
179 | dev->name, mhdr->num_buffers); | |
180 | dev->stats.rx_length_errors++; | |
181 | goto drop; | |
182 | } | |
183 | ||
184 | __skb_unlink(nskb, &vi->recv); | |
185 | vi->num--; | |
186 | ||
187 | skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0]; | |
188 | skb_shinfo(nskb)->nr_frags = 0; | |
189 | kfree_skb(nskb); | |
190 | ||
191 | if (len > PAGE_SIZE) | |
192 | len = PAGE_SIZE; | |
193 | ||
194 | skb_shinfo(skb)->frags[i].size = len; | |
195 | skb_shinfo(skb)->nr_frags++; | |
196 | skb->data_len += len; | |
197 | skb->len += len; | |
198 | } | |
199 | } else { | |
200 | len -= sizeof(struct virtio_net_hdr); | |
201 | ||
202 | if (len <= MAX_PACKET_LEN) | |
203 | trim_pages(vi, skb); | |
204 | ||
205 | err = pskb_trim(skb, len); | |
206 | if (err) { | |
207 | pr_debug("%s: pskb_trim failed %i %d\n", dev->name, | |
208 | len, err); | |
209 | dev->stats.rx_dropped++; | |
210 | goto drop; | |
211 | } | |
97402b96 | 212 | } |
3f2c31d9 | 213 | |
97402b96 | 214 | skb->truesize += skb->data_len; |
296f96fc RR |
215 | dev->stats.rx_bytes += skb->len; |
216 | dev->stats.rx_packets++; | |
217 | ||
218 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { | |
219 | pr_debug("Needs csum!\n"); | |
f35d9d8a | 220 | if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) |
296f96fc | 221 | goto frame_err; |
296f96fc RR |
222 | } |
223 | ||
23cde76d MM |
224 | skb->protocol = eth_type_trans(skb, dev); |
225 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", | |
226 | ntohs(skb->protocol), skb->len, skb->pkt_type); | |
227 | ||
296f96fc RR |
228 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
229 | pr_debug("GSO!\n"); | |
34a48579 | 230 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
296f96fc RR |
231 | case VIRTIO_NET_HDR_GSO_TCPV4: |
232 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; | |
233 | break; | |
296f96fc RR |
234 | case VIRTIO_NET_HDR_GSO_UDP: |
235 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; | |
236 | break; | |
237 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
238 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; | |
239 | break; | |
240 | default: | |
241 | if (net_ratelimit()) | |
242 | printk(KERN_WARNING "%s: bad gso type %u.\n", | |
243 | dev->name, hdr->gso_type); | |
244 | goto frame_err; | |
245 | } | |
246 | ||
34a48579 RR |
247 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
248 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; | |
249 | ||
296f96fc RR |
250 | skb_shinfo(skb)->gso_size = hdr->gso_size; |
251 | if (skb_shinfo(skb)->gso_size == 0) { | |
252 | if (net_ratelimit()) | |
253 | printk(KERN_WARNING "%s: zero gso size.\n", | |
254 | dev->name); | |
255 | goto frame_err; | |
256 | } | |
257 | ||
258 | /* Header must be checked, and gso_segs computed. */ | |
259 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
260 | skb_shinfo(skb)->gso_segs = 0; | |
261 | } | |
262 | ||
263 | netif_receive_skb(skb); | |
264 | return; | |
265 | ||
266 | frame_err: | |
267 | dev->stats.rx_frame_errors++; | |
268 | drop: | |
269 | dev_kfree_skb(skb); | |
270 | } | |
271 | ||
3f2c31d9 | 272 | static void try_fill_recv_maxbufs(struct virtnet_info *vi) |
296f96fc RR |
273 | { |
274 | struct sk_buff *skb; | |
05271685 | 275 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
97402b96 | 276 | int num, err, i; |
296f96fc | 277 | |
05271685 | 278 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
296f96fc | 279 | for (;;) { |
3f2c31d9 MM |
280 | struct virtio_net_hdr *hdr; |
281 | ||
296f96fc RR |
282 | skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); |
283 | if (unlikely(!skb)) | |
284 | break; | |
285 | ||
286 | skb_put(skb, MAX_PACKET_LEN); | |
3f2c31d9 MM |
287 | |
288 | hdr = skb_vnet_hdr(skb); | |
289 | sg_init_one(sg, hdr, sizeof(*hdr)); | |
97402b96 HX |
290 | |
291 | if (vi->big_packets) { | |
292 | for (i = 0; i < MAX_SKB_FRAGS; i++) { | |
293 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; | |
fb6813f4 | 294 | f->page = get_a_page(vi, GFP_ATOMIC); |
97402b96 HX |
295 | if (!f->page) |
296 | break; | |
297 | ||
298 | f->page_offset = 0; | |
299 | f->size = PAGE_SIZE; | |
300 | ||
301 | skb->data_len += PAGE_SIZE; | |
302 | skb->len += PAGE_SIZE; | |
303 | ||
304 | skb_shinfo(skb)->nr_frags++; | |
305 | } | |
306 | } | |
307 | ||
296f96fc RR |
308 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
309 | skb_queue_head(&vi->recv, skb); | |
310 | ||
311 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); | |
312 | if (err) { | |
313 | skb_unlink(skb, &vi->recv); | |
0a888fd1 | 314 | trim_pages(vi, skb); |
296f96fc RR |
315 | kfree_skb(skb); |
316 | break; | |
317 | } | |
318 | vi->num++; | |
319 | } | |
320 | if (unlikely(vi->num > vi->max)) | |
321 | vi->max = vi->num; | |
322 | vi->rvq->vq_ops->kick(vi->rvq); | |
323 | } | |
324 | ||
3f2c31d9 MM |
325 | static void try_fill_recv(struct virtnet_info *vi) |
326 | { | |
327 | struct sk_buff *skb; | |
328 | struct scatterlist sg[1]; | |
329 | int err; | |
330 | ||
331 | if (!vi->mergeable_rx_bufs) { | |
332 | try_fill_recv_maxbufs(vi); | |
333 | return; | |
334 | } | |
335 | ||
336 | for (;;) { | |
337 | skb_frag_t *f; | |
338 | ||
339 | skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN); | |
340 | if (unlikely(!skb)) | |
341 | break; | |
342 | ||
343 | skb_reserve(skb, NET_IP_ALIGN); | |
344 | ||
345 | f = &skb_shinfo(skb)->frags[0]; | |
346 | f->page = get_a_page(vi, GFP_ATOMIC); | |
347 | if (!f->page) { | |
348 | kfree_skb(skb); | |
349 | break; | |
350 | } | |
351 | ||
352 | f->page_offset = 0; | |
353 | f->size = PAGE_SIZE; | |
354 | ||
355 | skb_shinfo(skb)->nr_frags++; | |
356 | ||
357 | sg_init_one(sg, page_address(f->page), PAGE_SIZE); | |
358 | skb_queue_head(&vi->recv, skb); | |
359 | ||
360 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); | |
361 | if (err) { | |
362 | skb_unlink(skb, &vi->recv); | |
363 | kfree_skb(skb); | |
364 | break; | |
365 | } | |
366 | vi->num++; | |
367 | } | |
368 | if (unlikely(vi->num > vi->max)) | |
369 | vi->max = vi->num; | |
370 | vi->rvq->vq_ops->kick(vi->rvq); | |
371 | } | |
372 | ||
18445c4d | 373 | static void skb_recv_done(struct virtqueue *rvq) |
296f96fc RR |
374 | { |
375 | struct virtnet_info *vi = rvq->vdev->priv; | |
18445c4d | 376 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
908a7a16 | 377 | if (netif_rx_schedule_prep(&vi->napi)) { |
18445c4d | 378 | rvq->vq_ops->disable_cb(rvq); |
908a7a16 | 379 | __netif_rx_schedule(&vi->napi); |
18445c4d | 380 | } |
296f96fc RR |
381 | } |
382 | ||
383 | static int virtnet_poll(struct napi_struct *napi, int budget) | |
384 | { | |
385 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); | |
386 | struct sk_buff *skb = NULL; | |
387 | unsigned int len, received = 0; | |
388 | ||
389 | again: | |
390 | while (received < budget && | |
391 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { | |
392 | __skb_unlink(skb, &vi->recv); | |
393 | receive_skb(vi->dev, skb, len); | |
394 | vi->num--; | |
395 | received++; | |
396 | } | |
397 | ||
398 | /* FIXME: If we oom and completely run out of inbufs, we need | |
399 | * to start a timer trying to fill more. */ | |
400 | if (vi->num < vi->max / 2) | |
401 | try_fill_recv(vi); | |
402 | ||
8329d98e RR |
403 | /* Out of packets? */ |
404 | if (received < budget) { | |
908a7a16 | 405 | netif_rx_complete(napi); |
18445c4d | 406 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) |
4265f161 CB |
407 | && napi_schedule_prep(napi)) { |
408 | vi->rvq->vq_ops->disable_cb(vi->rvq); | |
908a7a16 | 409 | __netif_rx_schedule(napi); |
296f96fc | 410 | goto again; |
4265f161 | 411 | } |
296f96fc RR |
412 | } |
413 | ||
414 | return received; | |
415 | } | |
416 | ||
417 | static void free_old_xmit_skbs(struct virtnet_info *vi) | |
418 | { | |
419 | struct sk_buff *skb; | |
420 | unsigned int len; | |
421 | ||
422 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { | |
423 | pr_debug("Sent skb %p\n", skb); | |
424 | __skb_unlink(skb, &vi->send); | |
655aa31f | 425 | vi->dev->stats.tx_bytes += skb->len; |
296f96fc RR |
426 | vi->dev->stats.tx_packets++; |
427 | kfree_skb(skb); | |
428 | } | |
429 | } | |
430 | ||
363f1514 RR |
431 | /* If the virtio transport doesn't always notify us when all in-flight packets |
432 | * are consumed, we fall back to using this function on a timer to free them. */ | |
14c998f0 MM |
433 | static void xmit_free(unsigned long data) |
434 | { | |
435 | struct virtnet_info *vi = (void *)data; | |
436 | ||
437 | netif_tx_lock(vi->dev); | |
438 | ||
439 | free_old_xmit_skbs(vi); | |
440 | ||
441 | if (!skb_queue_empty(&vi->send)) | |
442 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); | |
443 | ||
444 | netif_tx_unlock(vi->dev); | |
445 | } | |
446 | ||
99ffc696 | 447 | static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) |
296f96fc | 448 | { |
14c998f0 | 449 | int num, err; |
05271685 | 450 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
3f2c31d9 MM |
451 | struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); |
452 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); | |
296f96fc | 453 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
296f96fc | 454 | |
05271685 | 455 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
4d125de3 | 456 | |
e174961c | 457 | pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); |
296f96fc | 458 | |
296f96fc RR |
459 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
460 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
461 | hdr->csum_start = skb->csum_start - skb_headroom(skb); | |
462 | hdr->csum_offset = skb->csum_offset; | |
463 | } else { | |
464 | hdr->flags = 0; | |
465 | hdr->csum_offset = hdr->csum_start = 0; | |
466 | } | |
467 | ||
468 | if (skb_is_gso(skb)) { | |
50c8ea80 | 469 | hdr->hdr_len = skb_transport_header(skb) - skb->data; |
296f96fc | 470 | hdr->gso_size = skb_shinfo(skb)->gso_size; |
34a48579 | 471 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
296f96fc RR |
472 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
473 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) | |
474 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; | |
475 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
476 | hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; | |
477 | else | |
478 | BUG(); | |
34a48579 RR |
479 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
480 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; | |
296f96fc RR |
481 | } else { |
482 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
50c8ea80 | 483 | hdr->gso_size = hdr->hdr_len = 0; |
296f96fc RR |
484 | } |
485 | ||
3f2c31d9 MM |
486 | mhdr->num_buffers = 0; |
487 | ||
488 | /* Encode metadata header at front. */ | |
489 | if (vi->mergeable_rx_bufs) | |
490 | sg_init_one(sg, mhdr, sizeof(*mhdr)); | |
491 | else | |
492 | sg_init_one(sg, hdr, sizeof(*hdr)); | |
493 | ||
296f96fc | 494 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
99ffc696 | 495 | |
14c998f0 | 496 | err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); |
363f1514 | 497 | if (!err && !vi->free_in_tasklet) |
14c998f0 MM |
498 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); |
499 | ||
500 | return err; | |
99ffc696 RR |
501 | } |
502 | ||
11a3a154 RR |
503 | static void xmit_tasklet(unsigned long data) |
504 | { | |
505 | struct virtnet_info *vi = (void *)data; | |
506 | ||
507 | netif_tx_lock_bh(vi->dev); | |
508 | if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { | |
509 | vi->svq->vq_ops->kick(vi->svq); | |
510 | vi->last_xmit_skb = NULL; | |
511 | } | |
363f1514 RR |
512 | if (vi->free_in_tasklet) |
513 | free_old_xmit_skbs(vi); | |
11a3a154 RR |
514 | netif_tx_unlock_bh(vi->dev); |
515 | } | |
516 | ||
99ffc696 RR |
517 | static int start_xmit(struct sk_buff *skb, struct net_device *dev) |
518 | { | |
519 | struct virtnet_info *vi = netdev_priv(dev); | |
2cb9c6ba RR |
520 | |
521 | again: | |
522 | /* Free up any pending old buffers before queueing new ones. */ | |
523 | free_old_xmit_skbs(vi); | |
99ffc696 RR |
524 | |
525 | /* If we has a buffer left over from last time, send it now. */ | |
9953ca6c MM |
526 | if (unlikely(vi->last_xmit_skb) && |
527 | xmit_skb(vi, vi->last_xmit_skb) != 0) | |
528 | goto stop_queue; | |
529 | ||
530 | vi->last_xmit_skb = NULL; | |
2cb9c6ba | 531 | |
99ffc696 | 532 | /* Put new one in send queue and do transmit */ |
7eb2e251 RR |
533 | if (likely(skb)) { |
534 | __skb_queue_head(&vi->send, skb); | |
535 | if (xmit_skb(vi, skb) != 0) { | |
536 | vi->last_xmit_skb = skb; | |
537 | skb = NULL; | |
538 | goto stop_queue; | |
539 | } | |
296f96fc | 540 | } |
99ffc696 | 541 | done: |
296f96fc | 542 | vi->svq->vq_ops->kick(vi->svq); |
99ffc696 RR |
543 | return NETDEV_TX_OK; |
544 | ||
545 | stop_queue: | |
546 | pr_debug("%s: virtio not prepared to send\n", dev->name); | |
547 | netif_stop_queue(dev); | |
548 | ||
549 | /* Activate callback for using skbs: if this returns false it | |
550 | * means some were used in the meantime. */ | |
551 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { | |
552 | vi->svq->vq_ops->disable_cb(vi->svq); | |
553 | netif_start_queue(dev); | |
554 | goto again; | |
555 | } | |
9953ca6c MM |
556 | if (skb) { |
557 | /* Drop this skb: we only queue one. */ | |
558 | vi->dev->stats.tx_dropped++; | |
559 | kfree_skb(skb); | |
560 | } | |
99ffc696 | 561 | goto done; |
296f96fc RR |
562 | } |
563 | ||
da74e89d AS |
564 | #ifdef CONFIG_NET_POLL_CONTROLLER |
565 | static void virtnet_netpoll(struct net_device *dev) | |
566 | { | |
567 | struct virtnet_info *vi = netdev_priv(dev); | |
568 | ||
569 | napi_schedule(&vi->napi); | |
570 | } | |
571 | #endif | |
572 | ||
296f96fc RR |
573 | static int virtnet_open(struct net_device *dev) |
574 | { | |
575 | struct virtnet_info *vi = netdev_priv(dev); | |
576 | ||
296f96fc | 577 | napi_enable(&vi->napi); |
a48bd8f6 RR |
578 | |
579 | /* If all buffers were filled by other side before we napi_enabled, we | |
580 | * won't get another interrupt, so process any outstanding packets | |
370076d9 CB |
581 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
582 | * We synchronize against interrupts via NAPI_STATE_SCHED */ | |
908a7a16 | 583 | if (netif_rx_schedule_prep(&vi->napi)) { |
370076d9 | 584 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
908a7a16 | 585 | __netif_rx_schedule(&vi->napi); |
370076d9 | 586 | } |
296f96fc RR |
587 | return 0; |
588 | } | |
589 | ||
590 | static int virtnet_close(struct net_device *dev) | |
591 | { | |
592 | struct virtnet_info *vi = netdev_priv(dev); | |
296f96fc RR |
593 | |
594 | napi_disable(&vi->napi); | |
595 | ||
296f96fc RR |
596 | return 0; |
597 | } | |
598 | ||
a9ea3fc6 HX |
599 | static int virtnet_set_tx_csum(struct net_device *dev, u32 data) |
600 | { | |
601 | struct virtnet_info *vi = netdev_priv(dev); | |
602 | struct virtio_device *vdev = vi->vdev; | |
603 | ||
604 | if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) | |
605 | return -ENOSYS; | |
606 | ||
607 | return ethtool_op_set_tx_hw_csum(dev, data); | |
608 | } | |
609 | ||
610 | static struct ethtool_ops virtnet_ethtool_ops = { | |
611 | .set_tx_csum = virtnet_set_tx_csum, | |
612 | .set_sg = ethtool_op_set_sg, | |
0276b497 | 613 | .set_tso = ethtool_op_set_tso, |
a9ea3fc6 HX |
614 | }; |
615 | ||
39da5814 MM |
616 | #define MIN_MTU 68 |
617 | #define MAX_MTU 65535 | |
618 | ||
619 | static int virtnet_change_mtu(struct net_device *dev, int new_mtu) | |
620 | { | |
621 | if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) | |
622 | return -EINVAL; | |
623 | dev->mtu = new_mtu; | |
624 | return 0; | |
625 | } | |
626 | ||
76288b4e SH |
627 | static const struct net_device_ops virtnet_netdev = { |
628 | .ndo_open = virtnet_open, | |
629 | .ndo_stop = virtnet_close, | |
630 | .ndo_start_xmit = start_xmit, | |
631 | .ndo_validate_addr = eth_validate_addr, | |
632 | .ndo_set_mac_address = eth_mac_addr, | |
633 | .ndo_change_mtu = virtnet_change_mtu, | |
634 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
635 | .ndo_poll_controller = virtnet_netpoll, | |
636 | #endif | |
637 | }; | |
638 | ||
296f96fc RR |
639 | static int virtnet_probe(struct virtio_device *vdev) |
640 | { | |
641 | int err; | |
296f96fc RR |
642 | struct net_device *dev; |
643 | struct virtnet_info *vi; | |
296f96fc RR |
644 | |
645 | /* Allocate ourselves a network device with room for our info */ | |
646 | dev = alloc_etherdev(sizeof(struct virtnet_info)); | |
647 | if (!dev) | |
648 | return -ENOMEM; | |
649 | ||
650 | /* Set up network device as normal. */ | |
76288b4e | 651 | dev->netdev_ops = &virtnet_netdev; |
296f96fc | 652 | dev->features = NETIF_F_HIGHDMA; |
a9ea3fc6 | 653 | SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); |
296f96fc RR |
654 | SET_NETDEV_DEV(dev, &vdev->dev); |
655 | ||
656 | /* Do we support "hardware" checksums? */ | |
c45a6816 | 657 | if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
296f96fc RR |
658 | /* This opens up the world of extra features. */ |
659 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; | |
c45a6816 | 660 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
34a48579 RR |
661 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
662 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; | |
663 | } | |
5539ae96 | 664 | /* Individual feature bits: what can host handle? */ |
c45a6816 | 665 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
5539ae96 | 666 | dev->features |= NETIF_F_TSO; |
c45a6816 | 667 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
5539ae96 | 668 | dev->features |= NETIF_F_TSO6; |
c45a6816 | 669 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
5539ae96 | 670 | dev->features |= NETIF_F_TSO_ECN; |
c45a6816 | 671 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
5539ae96 | 672 | dev->features |= NETIF_F_UFO; |
296f96fc RR |
673 | } |
674 | ||
675 | /* Configuration may specify what MAC to use. Otherwise random. */ | |
c45a6816 | 676 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { |
a586d4f6 RR |
677 | vdev->config->get(vdev, |
678 | offsetof(struct virtio_net_config, mac), | |
679 | dev->dev_addr, dev->addr_len); | |
296f96fc RR |
680 | } else |
681 | random_ether_addr(dev->dev_addr); | |
682 | ||
683 | /* Set up our device-specific information */ | |
684 | vi = netdev_priv(dev); | |
6c0cd7c0 | 685 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
296f96fc RR |
686 | vi->dev = dev; |
687 | vi->vdev = vdev; | |
d9d5dcc8 | 688 | vdev->priv = vi; |
fb6813f4 | 689 | vi->pages = NULL; |
296f96fc | 690 | |
363f1514 RR |
691 | /* If they give us a callback when all buffers are done, we don't need |
692 | * the timer. */ | |
693 | vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); | |
694 | ||
97402b96 HX |
695 | /* If we can receive ANY GSO packets, we must allocate large ones. */ |
696 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) | |
697 | || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) | |
698 | || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) | |
699 | vi->big_packets = true; | |
700 | ||
3f2c31d9 MM |
701 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) |
702 | vi->mergeable_rx_bufs = true; | |
703 | ||
296f96fc | 704 | /* We expect two virtqueues, receive then send. */ |
a586d4f6 | 705 | vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); |
296f96fc RR |
706 | if (IS_ERR(vi->rvq)) { |
707 | err = PTR_ERR(vi->rvq); | |
708 | goto free; | |
709 | } | |
710 | ||
a586d4f6 | 711 | vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); |
296f96fc RR |
712 | if (IS_ERR(vi->svq)) { |
713 | err = PTR_ERR(vi->svq); | |
714 | goto free_recv; | |
715 | } | |
716 | ||
717 | /* Initialize our empty receive and send queues. */ | |
718 | skb_queue_head_init(&vi->recv); | |
719 | skb_queue_head_init(&vi->send); | |
720 | ||
11a3a154 RR |
721 | tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); |
722 | ||
363f1514 RR |
723 | if (!vi->free_in_tasklet) |
724 | setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); | |
14c998f0 | 725 | |
296f96fc RR |
726 | err = register_netdev(dev); |
727 | if (err) { | |
728 | pr_debug("virtio_net: registering device failed\n"); | |
729 | goto free_send; | |
730 | } | |
b3369c1f RR |
731 | |
732 | /* Last of all, set up some receive buffers. */ | |
733 | try_fill_recv(vi); | |
734 | ||
735 | /* If we didn't even get one input buffer, we're useless. */ | |
736 | if (vi->num == 0) { | |
737 | err = -ENOMEM; | |
738 | goto unregister; | |
739 | } | |
740 | ||
296f96fc | 741 | pr_debug("virtnet: registered device %s\n", dev->name); |
296f96fc RR |
742 | return 0; |
743 | ||
b3369c1f RR |
744 | unregister: |
745 | unregister_netdev(dev); | |
296f96fc RR |
746 | free_send: |
747 | vdev->config->del_vq(vi->svq); | |
748 | free_recv: | |
749 | vdev->config->del_vq(vi->rvq); | |
750 | free: | |
751 | free_netdev(dev); | |
752 | return err; | |
753 | } | |
754 | ||
755 | static void virtnet_remove(struct virtio_device *vdev) | |
756 | { | |
74b2553f | 757 | struct virtnet_info *vi = vdev->priv; |
b3369c1f RR |
758 | struct sk_buff *skb; |
759 | ||
6e5aa7ef RR |
760 | /* Stop all the virtqueues. */ |
761 | vdev->config->reset(vdev); | |
762 | ||
363f1514 RR |
763 | if (!vi->free_in_tasklet) |
764 | del_timer_sync(&vi->xmit_free_timer); | |
14c998f0 | 765 | |
b3369c1f | 766 | /* Free our skbs in send and recv queues, if any. */ |
b3369c1f RR |
767 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
768 | kfree_skb(skb); | |
769 | vi->num--; | |
770 | } | |
288369cc | 771 | __skb_queue_purge(&vi->send); |
b3369c1f RR |
772 | |
773 | BUG_ON(vi->num != 0); | |
74b2553f RR |
774 | |
775 | vdev->config->del_vq(vi->svq); | |
776 | vdev->config->del_vq(vi->rvq); | |
777 | unregister_netdev(vi->dev); | |
fb6813f4 RR |
778 | |
779 | while (vi->pages) | |
780 | __free_pages(get_a_page(vi, GFP_KERNEL), 0); | |
781 | ||
74b2553f | 782 | free_netdev(vi->dev); |
296f96fc RR |
783 | } |
784 | ||
785 | static struct virtio_device_id id_table[] = { | |
786 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, | |
787 | { 0 }, | |
788 | }; | |
789 | ||
c45a6816 | 790 | static unsigned int features[] = { |
5e4fe5c4 MM |
791 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, |
792 | VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, | |
c45a6816 | 793 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, |
97402b96 HX |
794 | VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, |
795 | VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */ | |
3f2c31d9 | 796 | VIRTIO_NET_F_MRG_RXBUF, |
97402b96 | 797 | VIRTIO_F_NOTIFY_ON_EMPTY, |
c45a6816 RR |
798 | }; |
799 | ||
296f96fc | 800 | static struct virtio_driver virtio_net = { |
c45a6816 RR |
801 | .feature_table = features, |
802 | .feature_table_size = ARRAY_SIZE(features), | |
296f96fc RR |
803 | .driver.name = KBUILD_MODNAME, |
804 | .driver.owner = THIS_MODULE, | |
805 | .id_table = id_table, | |
806 | .probe = virtnet_probe, | |
807 | .remove = __devexit_p(virtnet_remove), | |
808 | }; | |
809 | ||
810 | static int __init init(void) | |
811 | { | |
812 | return register_virtio_driver(&virtio_net); | |
813 | } | |
814 | ||
815 | static void __exit fini(void) | |
816 | { | |
817 | unregister_virtio_driver(&virtio_net); | |
818 | } | |
819 | module_init(init); | |
820 | module_exit(fini); | |
821 | ||
822 | MODULE_DEVICE_TABLE(virtio, id_table); | |
823 | MODULE_DESCRIPTION("Virtio network driver"); | |
824 | MODULE_LICENSE("GPL"); |