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_ids.h>
26 #include <linux/virtio_net.h>
27 #include <linux/scatterlist.h>
28 #include <linux/if_vlan.h>
30 static int napi_weight = 128;
31 module_param(napi_weight, int, 0444);
33 static int csum = 1, gso = 1;
34 module_param(csum, bool, 0444);
35 module_param(gso, bool, 0444);
37 /* FIXME: MTU in config. */
38 #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39 #define GOOD_COPY_LEN 128
41 #define VIRTNET_SEND_COMMAND_SG_MAX 2
45 struct virtio_device *vdev;
46 struct virtqueue *rvq, *svq, *cvq;
47 struct net_device *dev;
48 struct napi_struct napi;
51 /* If we need to free in a timer, this is it. */
52 struct timer_list xmit_free_timer;
54 /* Number of input buffers, and max we've ever had. */
55 unsigned int num, max;
57 /* For cleaning up after transmission. */
58 struct tasklet_struct tasklet;
61 /* I like... big packets and I cannot lie! */
64 /* Host will merge rx buffers for big packets (shake it! shake it!) */
65 bool mergeable_rx_bufs;
67 /* Receive & send queues. */
68 struct sk_buff_head recv;
69 struct sk_buff_head send;
71 /* Work struct for refilling if we run low on memory. */
72 struct delayed_work refill;
74 /* Chain pages by the private ptr. */
78 static inline void *skb_vnet_hdr(struct sk_buff *skb)
80 return (struct virtio_net_hdr *)skb->cb;
83 static void give_a_page(struct virtnet_info *vi, struct page *page)
85 page->private = (unsigned long)vi->pages;
89 static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
93 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
94 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
95 skb_shinfo(skb)->nr_frags = 0;
99 static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
101 struct page *p = vi->pages;
104 vi->pages = (struct page *)p->private;
106 p = alloc_page(gfp_mask);
110 static void skb_xmit_done(struct virtqueue *svq)
112 struct virtnet_info *vi = svq->vdev->priv;
114 /* Suppress further interrupts. */
115 svq->vq_ops->disable_cb(svq);
117 /* We were probably waiting for more output buffers. */
118 netif_wake_queue(vi->dev);
120 if (vi->free_in_tasklet)
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 bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
279 struct scatterlist sg[2+MAX_SKB_FRAGS];
283 sg_init_table(sg, 2+MAX_SKB_FRAGS);
285 struct virtio_net_hdr *hdr;
287 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
288 if (unlikely(!skb)) {
293 skb_reserve(skb, NET_IP_ALIGN);
294 skb_put(skb, MAX_PACKET_LEN);
296 hdr = skb_vnet_hdr(skb);
297 sg_set_buf(sg, hdr, sizeof(*hdr));
299 if (vi->big_packets) {
300 for (i = 0; i < MAX_SKB_FRAGS; i++) {
301 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
302 f->page = get_a_page(vi, gfp);
309 skb->data_len += PAGE_SIZE;
310 skb->len += PAGE_SIZE;
312 skb_shinfo(skb)->nr_frags++;
316 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
317 skb_queue_head(&vi->recv, skb);
319 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
321 skb_unlink(skb, &vi->recv);
328 if (unlikely(vi->num > vi->max))
330 vi->rvq->vq_ops->kick(vi->rvq);
334 /* Returns false if we couldn't fill entirely (OOM). */
335 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
338 struct scatterlist sg[1];
342 if (!vi->mergeable_rx_bufs)
343 return try_fill_recv_maxbufs(vi, gfp);
348 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
349 if (unlikely(!skb)) {
354 skb_reserve(skb, NET_IP_ALIGN);
356 f = &skb_shinfo(skb)->frags[0];
357 f->page = get_a_page(vi, gfp);
367 skb_shinfo(skb)->nr_frags++;
369 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
370 skb_queue_head(&vi->recv, skb);
372 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
374 skb_unlink(skb, &vi->recv);
380 if (unlikely(vi->num > vi->max))
382 vi->rvq->vq_ops->kick(vi->rvq);
386 static void skb_recv_done(struct virtqueue *rvq)
388 struct virtnet_info *vi = rvq->vdev->priv;
389 /* Schedule NAPI, Suppress further interrupts if successful. */
390 if (napi_schedule_prep(&vi->napi)) {
391 rvq->vq_ops->disable_cb(rvq);
392 __napi_schedule(&vi->napi);
396 static void refill_work(struct work_struct *work)
398 struct virtnet_info *vi;
401 vi = container_of(work, struct virtnet_info, refill.work);
402 napi_disable(&vi->napi);
403 try_fill_recv(vi, GFP_KERNEL);
404 still_empty = (vi->num == 0);
405 napi_enable(&vi->napi);
407 /* In theory, this can happen: if we don't get any buffers in
408 * we will *never* try to fill again. */
410 schedule_delayed_work(&vi->refill, HZ/2);
413 static int virtnet_poll(struct napi_struct *napi, int budget)
415 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
416 struct sk_buff *skb = NULL;
417 unsigned int len, received = 0;
420 while (received < budget &&
421 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
422 __skb_unlink(skb, &vi->recv);
423 receive_skb(vi->dev, skb, len);
428 if (vi->num < vi->max / 2) {
429 if (!try_fill_recv(vi, GFP_ATOMIC))
430 schedule_delayed_work(&vi->refill, 0);
433 /* Out of packets? */
434 if (received < budget) {
436 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
437 && napi_schedule_prep(napi)) {
438 vi->rvq->vq_ops->disable_cb(vi->rvq);
439 __napi_schedule(napi);
447 static void free_old_xmit_skbs(struct virtnet_info *vi)
452 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
453 pr_debug("Sent skb %p\n", skb);
454 __skb_unlink(skb, &vi->send);
455 vi->dev->stats.tx_bytes += skb->len;
456 vi->dev->stats.tx_packets++;
461 /* If the virtio transport doesn't always notify us when all in-flight packets
462 * are consumed, we fall back to using this function on a timer to free them. */
463 static void xmit_free(unsigned long data)
465 struct virtnet_info *vi = (void *)data;
467 netif_tx_lock(vi->dev);
469 free_old_xmit_skbs(vi);
471 if (!skb_queue_empty(&vi->send))
472 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
474 netif_tx_unlock(vi->dev);
477 static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
480 struct scatterlist sg[2+MAX_SKB_FRAGS];
481 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
482 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
483 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
485 sg_init_table(sg, 2+MAX_SKB_FRAGS);
487 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
489 if (skb->ip_summed == CHECKSUM_PARTIAL) {
490 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
491 hdr->csum_start = skb->csum_start - skb_headroom(skb);
492 hdr->csum_offset = skb->csum_offset;
495 hdr->csum_offset = hdr->csum_start = 0;
498 if (skb_is_gso(skb)) {
499 hdr->hdr_len = skb_headlen(skb);
500 hdr->gso_size = skb_shinfo(skb)->gso_size;
501 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
502 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
503 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
504 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
505 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
506 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
509 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
510 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
512 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
513 hdr->gso_size = hdr->hdr_len = 0;
516 mhdr->num_buffers = 0;
518 /* Encode metadata header at front. */
519 if (vi->mergeable_rx_bufs)
520 sg_set_buf(sg, mhdr, sizeof(*mhdr));
522 sg_set_buf(sg, hdr, sizeof(*hdr));
524 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
526 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
527 if (err >= 0 && !vi->free_in_tasklet) {
528 /* Don't wait up for transmitted skbs to be freed. */
531 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
537 static void xmit_tasklet(unsigned long data)
539 struct virtnet_info *vi = (void *)data;
541 netif_tx_lock_bh(vi->dev);
542 free_old_xmit_skbs(vi);
543 netif_tx_unlock_bh(vi->dev);
546 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
548 struct virtnet_info *vi = netdev_priv(dev);
551 /* Free up any pending old buffers before queueing new ones. */
552 free_old_xmit_skbs(vi);
554 /* Put new one in send queue and do transmit */
555 __skb_queue_head(&vi->send, skb);
556 if (likely(xmit_skb(vi, skb) >= 0)) {
557 vi->svq->vq_ops->kick(vi->svq);
561 /* Ring too full for this packet, remove it from queue again. */
562 pr_debug("%s: virtio not prepared to send\n", dev->name);
563 __skb_unlink(skb, &vi->send);
564 netif_stop_queue(dev);
566 /* Activate callback for using skbs: if this returns false it
567 * means some were used in the meantime. */
568 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
569 vi->svq->vq_ops->disable_cb(vi->svq);
570 netif_start_queue(dev);
573 return NETDEV_TX_BUSY;
576 static int virtnet_set_mac_address(struct net_device *dev, void *p)
578 struct virtnet_info *vi = netdev_priv(dev);
579 struct virtio_device *vdev = vi->vdev;
582 ret = eth_mac_addr(dev, p);
586 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
587 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
588 dev->dev_addr, dev->addr_len);
593 #ifdef CONFIG_NET_POLL_CONTROLLER
594 static void virtnet_netpoll(struct net_device *dev)
596 struct virtnet_info *vi = netdev_priv(dev);
598 napi_schedule(&vi->napi);
602 static int virtnet_open(struct net_device *dev)
604 struct virtnet_info *vi = netdev_priv(dev);
606 napi_enable(&vi->napi);
608 /* If all buffers were filled by other side before we napi_enabled, we
609 * won't get another interrupt, so process any outstanding packets
610 * now. virtnet_poll wants re-enable the queue, so we disable here.
611 * We synchronize against interrupts via NAPI_STATE_SCHED */
612 if (napi_schedule_prep(&vi->napi)) {
613 vi->rvq->vq_ops->disable_cb(vi->rvq);
614 __napi_schedule(&vi->napi);
620 * Send command via the control virtqueue and check status. Commands
621 * supported by the hypervisor, as indicated by feature bits, should
622 * never fail unless improperly formated.
624 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
625 struct scatterlist *data, int out, int in)
627 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
628 struct virtio_net_ctrl_hdr ctrl;
629 virtio_net_ctrl_ack status = ~0;
633 /* Caller should know better */
634 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
635 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
637 out++; /* Add header */
638 in++; /* Add return status */
643 sg_init_table(sg, out + in);
645 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
646 for_each_sg(data, s, out + in - 2, i)
647 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
648 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
650 BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0);
652 vi->cvq->vq_ops->kick(vi->cvq);
655 * Spin for a response, the kick causes an ioport write, trapping
656 * into the hypervisor, so the request should be handled immediately.
658 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
661 return status == VIRTIO_NET_OK;
664 static int virtnet_close(struct net_device *dev)
666 struct virtnet_info *vi = netdev_priv(dev);
668 napi_disable(&vi->napi);
673 static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
675 struct virtnet_info *vi = netdev_priv(dev);
676 struct virtio_device *vdev = vi->vdev;
678 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
681 return ethtool_op_set_tx_hw_csum(dev, data);
684 static void virtnet_set_rx_mode(struct net_device *dev)
686 struct virtnet_info *vi = netdev_priv(dev);
687 struct scatterlist sg[2];
688 u8 promisc, allmulti;
689 struct virtio_net_ctrl_mac *mac_data;
690 struct dev_addr_list *addr;
691 struct netdev_hw_addr *ha;
695 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
696 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
699 promisc = ((dev->flags & IFF_PROMISC) != 0);
700 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
702 sg_init_one(sg, &promisc, sizeof(promisc));
704 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
705 VIRTIO_NET_CTRL_RX_PROMISC,
707 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
708 promisc ? "en" : "dis");
710 sg_init_one(sg, &allmulti, sizeof(allmulti));
712 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
713 VIRTIO_NET_CTRL_RX_ALLMULTI,
715 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
716 allmulti ? "en" : "dis");
718 /* MAC filter - use one buffer for both lists */
719 mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
720 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
722 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
726 sg_init_table(sg, 2);
728 /* Store the unicast list and count in the front of the buffer */
729 mac_data->entries = dev->uc.count;
731 list_for_each_entry(ha, &dev->uc.list, list)
732 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
734 sg_set_buf(&sg[0], mac_data,
735 sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
737 /* multicast list and count fill the end */
738 mac_data = (void *)&mac_data->macs[dev->uc.count][0];
740 mac_data->entries = dev->mc_count;
742 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
743 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
745 sg_set_buf(&sg[1], mac_data,
746 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
748 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
749 VIRTIO_NET_CTRL_MAC_TABLE_SET,
751 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
756 static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
758 struct virtnet_info *vi = netdev_priv(dev);
759 struct scatterlist sg;
761 sg_init_one(&sg, &vid, sizeof(vid));
763 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
764 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
765 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
768 static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
770 struct virtnet_info *vi = netdev_priv(dev);
771 struct scatterlist sg;
773 sg_init_one(&sg, &vid, sizeof(vid));
775 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
776 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
777 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
780 static const struct ethtool_ops virtnet_ethtool_ops = {
781 .set_tx_csum = virtnet_set_tx_csum,
782 .set_sg = ethtool_op_set_sg,
783 .set_tso = ethtool_op_set_tso,
784 .set_ufo = ethtool_op_set_ufo,
785 .get_link = ethtool_op_get_link,
789 #define MAX_MTU 65535
791 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
793 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
799 static const struct net_device_ops virtnet_netdev = {
800 .ndo_open = virtnet_open,
801 .ndo_stop = virtnet_close,
802 .ndo_start_xmit = start_xmit,
803 .ndo_validate_addr = eth_validate_addr,
804 .ndo_set_mac_address = virtnet_set_mac_address,
805 .ndo_set_rx_mode = virtnet_set_rx_mode,
806 .ndo_change_mtu = virtnet_change_mtu,
807 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
808 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
809 #ifdef CONFIG_NET_POLL_CONTROLLER
810 .ndo_poll_controller = virtnet_netpoll,
814 static void virtnet_update_status(struct virtnet_info *vi)
818 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
821 vi->vdev->config->get(vi->vdev,
822 offsetof(struct virtio_net_config, status),
825 /* Ignore unknown (future) status bits */
826 v &= VIRTIO_NET_S_LINK_UP;
833 if (vi->status & VIRTIO_NET_S_LINK_UP) {
834 netif_carrier_on(vi->dev);
835 netif_wake_queue(vi->dev);
837 netif_carrier_off(vi->dev);
838 netif_stop_queue(vi->dev);
842 static void virtnet_config_changed(struct virtio_device *vdev)
844 struct virtnet_info *vi = vdev->priv;
846 virtnet_update_status(vi);
849 static int virtnet_probe(struct virtio_device *vdev)
852 struct net_device *dev;
853 struct virtnet_info *vi;
854 struct virtqueue *vqs[3];
855 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
856 const char *names[] = { "input", "output", "control" };
859 /* Allocate ourselves a network device with room for our info */
860 dev = alloc_etherdev(sizeof(struct virtnet_info));
864 /* Set up network device as normal. */
865 dev->netdev_ops = &virtnet_netdev;
866 dev->features = NETIF_F_HIGHDMA;
867 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
868 SET_NETDEV_DEV(dev, &vdev->dev);
870 /* Do we support "hardware" checksums? */
871 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
872 /* This opens up the world of extra features. */
873 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
874 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
875 dev->features |= NETIF_F_TSO | NETIF_F_UFO
876 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
878 /* Individual feature bits: what can host handle? */
879 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
880 dev->features |= NETIF_F_TSO;
881 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
882 dev->features |= NETIF_F_TSO6;
883 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
884 dev->features |= NETIF_F_TSO_ECN;
885 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
886 dev->features |= NETIF_F_UFO;
889 /* Configuration may specify what MAC to use. Otherwise random. */
890 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
891 vdev->config->get(vdev,
892 offsetof(struct virtio_net_config, mac),
893 dev->dev_addr, dev->addr_len);
895 random_ether_addr(dev->dev_addr);
897 /* Set up our device-specific information */
898 vi = netdev_priv(dev);
899 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
904 INIT_DELAYED_WORK(&vi->refill, refill_work);
906 /* If they give us a callback when all buffers are done, we don't need
908 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
910 /* If we can receive ANY GSO packets, we must allocate large ones. */
911 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
912 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
913 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
914 vi->big_packets = true;
916 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
917 vi->mergeable_rx_bufs = true;
919 /* We expect two virtqueues, receive then send,
920 * and optionally control. */
921 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
923 err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
930 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
933 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
934 dev->features |= NETIF_F_HW_VLAN_FILTER;
937 /* Initialize our empty receive and send queues. */
938 skb_queue_head_init(&vi->recv);
939 skb_queue_head_init(&vi->send);
941 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
943 if (!vi->free_in_tasklet)
944 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
946 err = register_netdev(dev);
948 pr_debug("virtio_net: registering device failed\n");
952 /* Last of all, set up some receive buffers. */
953 try_fill_recv(vi, GFP_KERNEL);
955 /* If we didn't even get one input buffer, we're useless. */
961 vi->status = VIRTIO_NET_S_LINK_UP;
962 virtnet_update_status(vi);
963 netif_carrier_on(dev);
965 pr_debug("virtnet: registered device %s\n", dev->name);
969 unregister_netdev(dev);
970 cancel_delayed_work_sync(&vi->refill);
972 vdev->config->del_vqs(vdev);
978 static void virtnet_remove(struct virtio_device *vdev)
980 struct virtnet_info *vi = vdev->priv;
983 /* Stop all the virtqueues. */
984 vdev->config->reset(vdev);
986 if (!vi->free_in_tasklet)
987 del_timer_sync(&vi->xmit_free_timer);
989 /* Free our skbs in send and recv queues, if any. */
990 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
994 __skb_queue_purge(&vi->send);
996 BUG_ON(vi->num != 0);
998 unregister_netdev(vi->dev);
999 cancel_delayed_work_sync(&vi->refill);
1001 vdev->config->del_vqs(vi->vdev);
1004 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
1006 free_netdev(vi->dev);
1009 static struct virtio_device_id id_table[] = {
1010 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1014 static unsigned int features[] = {
1015 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1016 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1017 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
1018 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
1019 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
1020 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
1021 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1022 VIRTIO_F_NOTIFY_ON_EMPTY,
1025 static struct virtio_driver virtio_net = {
1026 .feature_table = features,
1027 .feature_table_size = ARRAY_SIZE(features),
1028 .driver.name = KBUILD_MODNAME,
1029 .driver.owner = THIS_MODULE,
1030 .id_table = id_table,
1031 .probe = virtnet_probe,
1032 .remove = __devexit_p(virtnet_remove),
1033 .config_changed = virtnet_config_changed,
1036 static int __init init(void)
1038 return register_virtio_driver(&virtio_net);
1041 static void __exit fini(void)
1043 unregister_virtio_driver(&virtio_net);
1048 MODULE_DEVICE_TABLE(virtio, id_table);
1049 MODULE_DESCRIPTION("Virtio network driver");
1050 MODULE_LICENSE("GPL");