1 /* A simple network driver using virtio.
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
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.
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.
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
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/ethtool.h>
23 #include <linux/module.h>
24 #include <linux/virtio.h>
25 #include <linux/virtio_net.h>
26 #include <linux/scatterlist.h>
27 #include <linux/if_vlan.h>
29 static int napi_weight = 128;
30 module_param(napi_weight, int, 0444);
32 static int csum = 1, gso = 1;
33 module_param(csum, bool, 0444);
34 module_param(gso, bool, 0444);
36 /* FIXME: MTU in config. */
37 #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
38 #define GOOD_COPY_LEN 128
40 #define VIRTNET_SEND_COMMAND_SG_MAX 2
44 struct virtio_device *vdev;
45 struct virtqueue *rvq, *svq, *cvq;
46 struct net_device *dev;
47 struct napi_struct napi;
50 /* The skb we couldn't send because buffers were full. */
51 struct sk_buff *last_xmit_skb;
53 /* If we need to free in a timer, this is it. */
54 struct timer_list xmit_free_timer;
56 /* Number of input buffers, and max we've ever had. */
57 unsigned int num, max;
59 /* For cleaning up after transmission. */
60 struct tasklet_struct tasklet;
63 /* I like... big packets and I cannot lie! */
66 /* Host will merge rx buffers for big packets (shake it! shake it!) */
67 bool mergeable_rx_bufs;
69 /* Receive & send queues. */
70 struct sk_buff_head recv;
71 struct sk_buff_head send;
73 /* Chain pages by the private ptr. */
77 static inline void *skb_vnet_hdr(struct sk_buff *skb)
79 return (struct virtio_net_hdr *)skb->cb;
82 static void give_a_page(struct virtnet_info *vi, struct page *page)
84 page->private = (unsigned long)vi->pages;
88 static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
92 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
93 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
94 skb_shinfo(skb)->nr_frags = 0;
98 static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
100 struct page *p = vi->pages;
103 vi->pages = (struct page *)p->private;
105 p = alloc_page(gfp_mask);
109 static void skb_xmit_done(struct virtqueue *svq)
111 struct virtnet_info *vi = svq->vdev->priv;
113 /* Suppress further interrupts. */
114 svq->vq_ops->disable_cb(svq);
116 /* We were probably waiting for more output buffers. */
117 netif_wake_queue(vi->dev);
119 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
120 * queued, start_xmit won't be called. */
121 tasklet_schedule(&vi->tasklet);
124 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
127 struct virtnet_info *vi = netdev_priv(dev);
128 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
132 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
133 pr_debug("%s: short packet %i\n", dev->name, len);
134 dev->stats.rx_length_errors++;
138 if (vi->mergeable_rx_bufs) {
139 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
141 char *p = page_address(skb_shinfo(skb)->frags[0].page);
145 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
147 memcpy(hdr, p, sizeof(*mhdr));
151 if (copy > skb_tailroom(skb))
152 copy = skb_tailroom(skb);
154 memcpy(skb_put(skb, copy), p, copy);
159 give_a_page(vi, skb_shinfo(skb)->frags[0].page);
160 skb_shinfo(skb)->nr_frags--;
162 skb_shinfo(skb)->frags[0].page_offset +=
163 sizeof(*mhdr) + copy;
164 skb_shinfo(skb)->frags[0].size = len;
165 skb->data_len += len;
169 while (--mhdr->num_buffers) {
170 struct sk_buff *nskb;
172 i = skb_shinfo(skb)->nr_frags;
173 if (i >= MAX_SKB_FRAGS) {
174 pr_debug("%s: packet too long %d\n", dev->name,
176 dev->stats.rx_length_errors++;
180 nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
182 pr_debug("%s: rx error: %d buffers missing\n",
183 dev->name, mhdr->num_buffers);
184 dev->stats.rx_length_errors++;
188 __skb_unlink(nskb, &vi->recv);
191 skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
192 skb_shinfo(nskb)->nr_frags = 0;
198 skb_shinfo(skb)->frags[i].size = len;
199 skb_shinfo(skb)->nr_frags++;
200 skb->data_len += len;
204 len -= sizeof(struct virtio_net_hdr);
206 if (len <= MAX_PACKET_LEN)
209 err = pskb_trim(skb, len);
211 pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
213 dev->stats.rx_dropped++;
218 skb->truesize += skb->data_len;
219 dev->stats.rx_bytes += skb->len;
220 dev->stats.rx_packets++;
222 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
223 pr_debug("Needs csum!\n");
224 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
228 skb->protocol = eth_type_trans(skb, dev);
229 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
230 ntohs(skb->protocol), skb->len, skb->pkt_type);
232 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
234 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
235 case VIRTIO_NET_HDR_GSO_TCPV4:
236 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
238 case VIRTIO_NET_HDR_GSO_UDP:
239 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
241 case VIRTIO_NET_HDR_GSO_TCPV6:
242 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
246 printk(KERN_WARNING "%s: bad gso type %u.\n",
247 dev->name, hdr->gso_type);
251 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
252 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
254 skb_shinfo(skb)->gso_size = hdr->gso_size;
255 if (skb_shinfo(skb)->gso_size == 0) {
257 printk(KERN_WARNING "%s: zero gso size.\n",
262 /* Header must be checked, and gso_segs computed. */
263 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
264 skb_shinfo(skb)->gso_segs = 0;
267 netif_receive_skb(skb);
271 dev->stats.rx_frame_errors++;
276 static void try_fill_recv_maxbufs(struct virtnet_info *vi)
279 struct scatterlist sg[2+MAX_SKB_FRAGS];
282 sg_init_table(sg, 2+MAX_SKB_FRAGS);
284 struct virtio_net_hdr *hdr;
286 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
290 skb_put(skb, MAX_PACKET_LEN);
292 hdr = skb_vnet_hdr(skb);
293 sg_set_buf(sg, hdr, sizeof(*hdr));
295 if (vi->big_packets) {
296 for (i = 0; i < MAX_SKB_FRAGS; i++) {
297 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
298 f->page = get_a_page(vi, GFP_ATOMIC);
305 skb->data_len += PAGE_SIZE;
306 skb->len += PAGE_SIZE;
308 skb_shinfo(skb)->nr_frags++;
312 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
313 skb_queue_head(&vi->recv, skb);
315 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
317 skb_unlink(skb, &vi->recv);
324 if (unlikely(vi->num > vi->max))
326 vi->rvq->vq_ops->kick(vi->rvq);
329 static void try_fill_recv(struct virtnet_info *vi)
332 struct scatterlist sg[1];
335 if (!vi->mergeable_rx_bufs) {
336 try_fill_recv_maxbufs(vi);
343 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
347 skb_reserve(skb, NET_IP_ALIGN);
349 f = &skb_shinfo(skb)->frags[0];
350 f->page = get_a_page(vi, GFP_ATOMIC);
359 skb_shinfo(skb)->nr_frags++;
361 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
362 skb_queue_head(&vi->recv, skb);
364 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
366 skb_unlink(skb, &vi->recv);
372 if (unlikely(vi->num > vi->max))
374 vi->rvq->vq_ops->kick(vi->rvq);
377 static void skb_recv_done(struct virtqueue *rvq)
379 struct virtnet_info *vi = rvq->vdev->priv;
380 /* Schedule NAPI, Suppress further interrupts if successful. */
381 if (napi_schedule_prep(&vi->napi)) {
382 rvq->vq_ops->disable_cb(rvq);
383 __napi_schedule(&vi->napi);
387 static int virtnet_poll(struct napi_struct *napi, int budget)
389 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
390 struct sk_buff *skb = NULL;
391 unsigned int len, received = 0;
394 while (received < budget &&
395 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
396 __skb_unlink(skb, &vi->recv);
397 receive_skb(vi->dev, skb, len);
402 /* FIXME: If we oom and completely run out of inbufs, we need
403 * to start a timer trying to fill more. */
404 if (vi->num < vi->max / 2)
407 /* Out of packets? */
408 if (received < budget) {
410 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
411 && napi_schedule_prep(napi)) {
412 vi->rvq->vq_ops->disable_cb(vi->rvq);
413 __napi_schedule(napi);
421 static void free_old_xmit_skbs(struct virtnet_info *vi)
426 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
427 pr_debug("Sent skb %p\n", skb);
428 __skb_unlink(skb, &vi->send);
429 vi->dev->stats.tx_bytes += skb->len;
430 vi->dev->stats.tx_packets++;
435 /* If the virtio transport doesn't always notify us when all in-flight packets
436 * are consumed, we fall back to using this function on a timer to free them. */
437 static void xmit_free(unsigned long data)
439 struct virtnet_info *vi = (void *)data;
441 netif_tx_lock(vi->dev);
443 free_old_xmit_skbs(vi);
445 if (!skb_queue_empty(&vi->send))
446 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
448 netif_tx_unlock(vi->dev);
451 static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
454 struct scatterlist sg[2+MAX_SKB_FRAGS];
455 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
456 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
457 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
459 sg_init_table(sg, 2+MAX_SKB_FRAGS);
461 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
463 if (skb->ip_summed == CHECKSUM_PARTIAL) {
464 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
465 hdr->csum_start = skb->csum_start - skb_headroom(skb);
466 hdr->csum_offset = skb->csum_offset;
469 hdr->csum_offset = hdr->csum_start = 0;
472 if (skb_is_gso(skb)) {
473 hdr->hdr_len = skb_transport_header(skb) - skb->data;
474 hdr->gso_size = skb_shinfo(skb)->gso_size;
475 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
476 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
477 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
478 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
479 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
480 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
483 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
484 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
486 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
487 hdr->gso_size = hdr->hdr_len = 0;
490 mhdr->num_buffers = 0;
492 /* Encode metadata header at front. */
493 if (vi->mergeable_rx_bufs)
494 sg_set_buf(sg, mhdr, sizeof(*mhdr));
496 sg_set_buf(sg, hdr, sizeof(*hdr));
498 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
500 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
501 if (!err && !vi->free_in_tasklet)
502 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
507 static void xmit_tasklet(unsigned long data)
509 struct virtnet_info *vi = (void *)data;
511 netif_tx_lock_bh(vi->dev);
512 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
513 vi->svq->vq_ops->kick(vi->svq);
514 vi->last_xmit_skb = NULL;
516 if (vi->free_in_tasklet)
517 free_old_xmit_skbs(vi);
518 netif_tx_unlock_bh(vi->dev);
521 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
523 struct virtnet_info *vi = netdev_priv(dev);
526 /* Free up any pending old buffers before queueing new ones. */
527 free_old_xmit_skbs(vi);
529 /* If we has a buffer left over from last time, send it now. */
530 if (unlikely(vi->last_xmit_skb) &&
531 xmit_skb(vi, vi->last_xmit_skb) != 0)
534 vi->last_xmit_skb = NULL;
536 /* Put new one in send queue and do transmit */
538 __skb_queue_head(&vi->send, skb);
539 if (xmit_skb(vi, skb) != 0) {
540 vi->last_xmit_skb = skb;
546 vi->svq->vq_ops->kick(vi->svq);
550 pr_debug("%s: virtio not prepared to send\n", dev->name);
551 netif_stop_queue(dev);
553 /* Activate callback for using skbs: if this returns false it
554 * means some were used in the meantime. */
555 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
556 vi->svq->vq_ops->disable_cb(vi->svq);
557 netif_start_queue(dev);
561 /* Drop this skb: we only queue one. */
562 vi->dev->stats.tx_dropped++;
568 static int virtnet_set_mac_address(struct net_device *dev, void *p)
570 struct virtnet_info *vi = netdev_priv(dev);
571 struct virtio_device *vdev = vi->vdev;
574 ret = eth_mac_addr(dev, p);
578 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
579 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
580 dev->dev_addr, dev->addr_len);
585 #ifdef CONFIG_NET_POLL_CONTROLLER
586 static void virtnet_netpoll(struct net_device *dev)
588 struct virtnet_info *vi = netdev_priv(dev);
590 napi_schedule(&vi->napi);
594 static int virtnet_open(struct net_device *dev)
596 struct virtnet_info *vi = netdev_priv(dev);
598 napi_enable(&vi->napi);
600 /* If all buffers were filled by other side before we napi_enabled, we
601 * won't get another interrupt, so process any outstanding packets
602 * now. virtnet_poll wants re-enable the queue, so we disable here.
603 * We synchronize against interrupts via NAPI_STATE_SCHED */
604 if (napi_schedule_prep(&vi->napi)) {
605 vi->rvq->vq_ops->disable_cb(vi->rvq);
606 __napi_schedule(&vi->napi);
612 * Send command via the control virtqueue and check status. Commands
613 * supported by the hypervisor, as indicated by feature bits, should
614 * never fail unless improperly formated.
616 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
617 struct scatterlist *data, int out, int in)
619 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
620 struct virtio_net_ctrl_hdr ctrl;
621 virtio_net_ctrl_ack status = ~0;
625 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
626 BUG(); /* Caller should know better */
630 BUG_ON(out + in > VIRTNET_SEND_COMMAND_SG_MAX);
632 out++; /* Add header */
633 in++; /* Add return status */
638 sg_init_table(sg, out + in);
640 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
641 for_each_sg(data, s, out + in - 2, i)
642 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
643 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
645 if (vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) != 0)
648 vi->cvq->vq_ops->kick(vi->cvq);
651 * Spin for a response, the kick causes an ioport write, trapping
652 * into the hypervisor, so the request should be handled immediately.
654 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
657 return status == VIRTIO_NET_OK;
660 static int virtnet_close(struct net_device *dev)
662 struct virtnet_info *vi = netdev_priv(dev);
664 napi_disable(&vi->napi);
669 static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
671 struct virtnet_info *vi = netdev_priv(dev);
672 struct virtio_device *vdev = vi->vdev;
674 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
677 return ethtool_op_set_tx_hw_csum(dev, data);
680 static void virtnet_set_rx_mode(struct net_device *dev)
682 struct virtnet_info *vi = netdev_priv(dev);
683 struct scatterlist sg[2];
684 u8 promisc, allmulti;
685 struct virtio_net_ctrl_mac *mac_data;
686 struct dev_addr_list *addr;
690 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
691 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
694 promisc = ((dev->flags & IFF_PROMISC) != 0);
695 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
697 sg_init_one(sg, &promisc, sizeof(promisc));
699 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
700 VIRTIO_NET_CTRL_RX_PROMISC,
702 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
703 promisc ? "en" : "dis");
705 sg_init_one(sg, &allmulti, sizeof(allmulti));
707 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
708 VIRTIO_NET_CTRL_RX_ALLMULTI,
710 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
711 allmulti ? "en" : "dis");
713 /* MAC filter - use one buffer for both lists */
714 mac_data = buf = kzalloc(((dev->uc_count + dev->mc_count) * ETH_ALEN) +
715 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
717 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
721 sg_init_table(sg, 2);
723 /* Store the unicast list and count in the front of the buffer */
724 mac_data->entries = dev->uc_count;
726 for (i = 0; i < dev->uc_count; i++, addr = addr->next)
727 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
729 sg_set_buf(&sg[0], mac_data,
730 sizeof(mac_data->entries) + (dev->uc_count * ETH_ALEN));
732 /* multicast list and count fill the end */
733 mac_data = (void *)&mac_data->macs[dev->uc_count][0];
735 mac_data->entries = dev->mc_count;
737 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
738 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
740 sg_set_buf(&sg[1], mac_data,
741 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
743 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
744 VIRTIO_NET_CTRL_MAC_TABLE_SET,
746 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
751 static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
753 struct virtnet_info *vi = netdev_priv(dev);
754 struct scatterlist sg;
756 sg_init_one(&sg, &vid, sizeof(vid));
758 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
759 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
760 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
763 static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
765 struct virtnet_info *vi = netdev_priv(dev);
766 struct scatterlist sg;
768 sg_init_one(&sg, &vid, sizeof(vid));
770 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
771 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
772 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
775 static struct ethtool_ops virtnet_ethtool_ops = {
776 .set_tx_csum = virtnet_set_tx_csum,
777 .set_sg = ethtool_op_set_sg,
778 .set_tso = ethtool_op_set_tso,
779 .get_link = ethtool_op_get_link,
783 #define MAX_MTU 65535
785 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
787 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
793 static const struct net_device_ops virtnet_netdev = {
794 .ndo_open = virtnet_open,
795 .ndo_stop = virtnet_close,
796 .ndo_start_xmit = start_xmit,
797 .ndo_validate_addr = eth_validate_addr,
798 .ndo_set_mac_address = virtnet_set_mac_address,
799 .ndo_set_rx_mode = virtnet_set_rx_mode,
800 .ndo_change_mtu = virtnet_change_mtu,
801 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
802 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
803 #ifdef CONFIG_NET_POLL_CONTROLLER
804 .ndo_poll_controller = virtnet_netpoll,
808 static void virtnet_update_status(struct virtnet_info *vi)
812 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
815 vi->vdev->config->get(vi->vdev,
816 offsetof(struct virtio_net_config, status),
819 /* Ignore unknown (future) status bits */
820 v &= VIRTIO_NET_S_LINK_UP;
827 if (vi->status & VIRTIO_NET_S_LINK_UP) {
828 netif_carrier_on(vi->dev);
829 netif_wake_queue(vi->dev);
831 netif_carrier_off(vi->dev);
832 netif_stop_queue(vi->dev);
836 static void virtnet_config_changed(struct virtio_device *vdev)
838 struct virtnet_info *vi = vdev->priv;
840 virtnet_update_status(vi);
843 static int virtnet_probe(struct virtio_device *vdev)
846 struct net_device *dev;
847 struct virtnet_info *vi;
848 struct virtqueue *vqs[3];
849 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
850 const char *names[] = { "input", "output", "control" };
853 /* Allocate ourselves a network device with room for our info */
854 dev = alloc_etherdev(sizeof(struct virtnet_info));
858 /* Set up network device as normal. */
859 dev->netdev_ops = &virtnet_netdev;
860 dev->features = NETIF_F_HIGHDMA;
861 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
862 SET_NETDEV_DEV(dev, &vdev->dev);
864 /* Do we support "hardware" checksums? */
865 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
866 /* This opens up the world of extra features. */
867 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
868 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
869 dev->features |= NETIF_F_TSO | NETIF_F_UFO
870 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
872 /* Individual feature bits: what can host handle? */
873 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
874 dev->features |= NETIF_F_TSO;
875 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
876 dev->features |= NETIF_F_TSO6;
877 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
878 dev->features |= NETIF_F_TSO_ECN;
879 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
880 dev->features |= NETIF_F_UFO;
883 /* Configuration may specify what MAC to use. Otherwise random. */
884 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
885 vdev->config->get(vdev,
886 offsetof(struct virtio_net_config, mac),
887 dev->dev_addr, dev->addr_len);
889 random_ether_addr(dev->dev_addr);
891 /* Set up our device-specific information */
892 vi = netdev_priv(dev);
893 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
899 /* If they give us a callback when all buffers are done, we don't need
901 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
903 /* If we can receive ANY GSO packets, we must allocate large ones. */
904 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
905 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
906 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
907 vi->big_packets = true;
909 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
910 vi->mergeable_rx_bufs = true;
912 /* We expect two virtqueues, receive then send,
913 * and optionally control. */
914 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
916 err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
923 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
926 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
927 dev->features |= NETIF_F_HW_VLAN_FILTER;
930 /* Initialize our empty receive and send queues. */
931 skb_queue_head_init(&vi->recv);
932 skb_queue_head_init(&vi->send);
934 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
936 if (!vi->free_in_tasklet)
937 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
939 err = register_netdev(dev);
941 pr_debug("virtio_net: registering device failed\n");
945 /* Last of all, set up some receive buffers. */
948 /* If we didn't even get one input buffer, we're useless. */
954 vi->status = VIRTIO_NET_S_LINK_UP;
955 virtnet_update_status(vi);
956 netif_carrier_on(dev);
958 pr_debug("virtnet: registered device %s\n", dev->name);
962 unregister_netdev(dev);
964 vdev->config->del_vqs(vdev);
970 static void virtnet_remove(struct virtio_device *vdev)
972 struct virtnet_info *vi = vdev->priv;
975 /* Stop all the virtqueues. */
976 vdev->config->reset(vdev);
978 if (!vi->free_in_tasklet)
979 del_timer_sync(&vi->xmit_free_timer);
981 /* Free our skbs in send and recv queues, if any. */
982 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
986 __skb_queue_purge(&vi->send);
988 BUG_ON(vi->num != 0);
990 unregister_netdev(vi->dev);
992 vdev->config->del_vqs(vi->vdev);
995 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
997 free_netdev(vi->dev);
1000 static struct virtio_device_id id_table[] = {
1001 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1005 static unsigned int features[] = {
1006 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1007 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1008 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
1009 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
1010 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
1011 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
1012 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1013 VIRTIO_F_NOTIFY_ON_EMPTY,
1016 static struct virtio_driver virtio_net = {
1017 .feature_table = features,
1018 .feature_table_size = ARRAY_SIZE(features),
1019 .driver.name = KBUILD_MODNAME,
1020 .driver.owner = THIS_MODULE,
1021 .id_table = id_table,
1022 .probe = virtnet_probe,
1023 .remove = __devexit_p(virtnet_remove),
1024 .config_changed = virtnet_config_changed,
1027 static int __init init(void)
1029 return register_virtio_driver(&virtio_net);
1032 static void __exit fini(void)
1034 unregister_virtio_driver(&virtio_net);
1039 MODULE_DEVICE_TABLE(virtio, id_table);
1040 MODULE_DESCRIPTION("Virtio network driver");
1041 MODULE_LICENSE("GPL");