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 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
54 /* I like... big packets and I cannot lie! */
57 /* Host will merge rx buffers for big packets (shake it! shake it!) */
58 bool mergeable_rx_bufs;
60 /* Receive & send queues. */
61 struct sk_buff_head recv;
62 struct sk_buff_head send;
64 /* Work struct for refilling if we run low on memory. */
65 struct delayed_work refill;
67 /* Chain pages by the private ptr. */
71 static inline void *skb_vnet_hdr(struct sk_buff *skb)
73 return (struct virtio_net_hdr *)skb->cb;
76 static void give_a_page(struct virtnet_info *vi, struct page *page)
78 page->private = (unsigned long)vi->pages;
82 static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
86 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
87 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
88 skb_shinfo(skb)->nr_frags = 0;
92 static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
94 struct page *p = vi->pages;
97 vi->pages = (struct page *)p->private;
99 p = alloc_page(gfp_mask);
103 static void skb_xmit_done(struct virtqueue *svq)
105 struct virtnet_info *vi = svq->vdev->priv;
107 /* Suppress further interrupts. */
108 svq->vq_ops->disable_cb(svq);
110 /* We were probably waiting for more output buffers. */
111 netif_wake_queue(vi->dev);
114 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
117 struct virtnet_info *vi = netdev_priv(dev);
118 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
122 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
123 pr_debug("%s: short packet %i\n", dev->name, len);
124 dev->stats.rx_length_errors++;
128 if (vi->mergeable_rx_bufs) {
129 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
131 char *p = page_address(skb_shinfo(skb)->frags[0].page);
135 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
137 memcpy(hdr, p, sizeof(*mhdr));
141 if (copy > skb_tailroom(skb))
142 copy = skb_tailroom(skb);
144 memcpy(skb_put(skb, copy), p, copy);
149 give_a_page(vi, skb_shinfo(skb)->frags[0].page);
150 skb_shinfo(skb)->nr_frags--;
152 skb_shinfo(skb)->frags[0].page_offset +=
153 sizeof(*mhdr) + copy;
154 skb_shinfo(skb)->frags[0].size = len;
155 skb->data_len += len;
159 while (--mhdr->num_buffers) {
160 struct sk_buff *nskb;
162 i = skb_shinfo(skb)->nr_frags;
163 if (i >= MAX_SKB_FRAGS) {
164 pr_debug("%s: packet too long %d\n", dev->name,
166 dev->stats.rx_length_errors++;
170 nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
172 pr_debug("%s: rx error: %d buffers missing\n",
173 dev->name, mhdr->num_buffers);
174 dev->stats.rx_length_errors++;
178 __skb_unlink(nskb, &vi->recv);
181 skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
182 skb_shinfo(nskb)->nr_frags = 0;
188 skb_shinfo(skb)->frags[i].size = len;
189 skb_shinfo(skb)->nr_frags++;
190 skb->data_len += len;
194 len -= sizeof(struct virtio_net_hdr);
196 if (len <= MAX_PACKET_LEN)
199 err = pskb_trim(skb, len);
201 pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
203 dev->stats.rx_dropped++;
208 skb->truesize += skb->data_len;
209 dev->stats.rx_bytes += skb->len;
210 dev->stats.rx_packets++;
212 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
213 pr_debug("Needs csum!\n");
214 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
218 skb->protocol = eth_type_trans(skb, dev);
219 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
220 ntohs(skb->protocol), skb->len, skb->pkt_type);
222 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
224 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
225 case VIRTIO_NET_HDR_GSO_TCPV4:
226 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
228 case VIRTIO_NET_HDR_GSO_UDP:
229 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
231 case VIRTIO_NET_HDR_GSO_TCPV6:
232 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
236 printk(KERN_WARNING "%s: bad gso type %u.\n",
237 dev->name, hdr->gso_type);
241 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
242 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
244 skb_shinfo(skb)->gso_size = hdr->gso_size;
245 if (skb_shinfo(skb)->gso_size == 0) {
247 printk(KERN_WARNING "%s: zero gso size.\n",
252 /* Header must be checked, and gso_segs computed. */
253 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
254 skb_shinfo(skb)->gso_segs = 0;
257 netif_receive_skb(skb);
261 dev->stats.rx_frame_errors++;
266 static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
269 struct scatterlist sg[2+MAX_SKB_FRAGS];
273 sg_init_table(sg, 2+MAX_SKB_FRAGS);
275 struct virtio_net_hdr *hdr;
277 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
278 if (unlikely(!skb)) {
283 skb_reserve(skb, NET_IP_ALIGN);
284 skb_put(skb, MAX_PACKET_LEN);
286 hdr = skb_vnet_hdr(skb);
287 sg_set_buf(sg, hdr, sizeof(*hdr));
289 if (vi->big_packets) {
290 for (i = 0; i < MAX_SKB_FRAGS; i++) {
291 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
292 f->page = get_a_page(vi, gfp);
299 skb->data_len += PAGE_SIZE;
300 skb->len += PAGE_SIZE;
302 skb_shinfo(skb)->nr_frags++;
306 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
307 skb_queue_head(&vi->recv, skb);
309 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
311 skb_unlink(skb, &vi->recv);
318 if (unlikely(vi->num > vi->max))
320 vi->rvq->vq_ops->kick(vi->rvq);
324 /* Returns false if we couldn't fill entirely (OOM). */
325 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
328 struct scatterlist sg[1];
332 if (!vi->mergeable_rx_bufs)
333 return try_fill_recv_maxbufs(vi, gfp);
338 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
339 if (unlikely(!skb)) {
344 skb_reserve(skb, NET_IP_ALIGN);
346 f = &skb_shinfo(skb)->frags[0];
347 f->page = get_a_page(vi, gfp);
357 skb_shinfo(skb)->nr_frags++;
359 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
360 skb_queue_head(&vi->recv, skb);
362 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
364 skb_unlink(skb, &vi->recv);
370 if (unlikely(vi->num > vi->max))
372 vi->rvq->vq_ops->kick(vi->rvq);
376 static void skb_recv_done(struct virtqueue *rvq)
378 struct virtnet_info *vi = rvq->vdev->priv;
379 /* Schedule NAPI, Suppress further interrupts if successful. */
380 if (napi_schedule_prep(&vi->napi)) {
381 rvq->vq_ops->disable_cb(rvq);
382 __napi_schedule(&vi->napi);
386 static void refill_work(struct work_struct *work)
388 struct virtnet_info *vi;
391 vi = container_of(work, struct virtnet_info, refill.work);
392 napi_disable(&vi->napi);
393 try_fill_recv(vi, GFP_KERNEL);
394 still_empty = (vi->num == 0);
395 napi_enable(&vi->napi);
397 /* In theory, this can happen: if we don't get any buffers in
398 * we will *never* try to fill again. */
400 schedule_delayed_work(&vi->refill, HZ/2);
403 static int virtnet_poll(struct napi_struct *napi, int budget)
405 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
406 struct sk_buff *skb = NULL;
407 unsigned int len, received = 0;
410 while (received < budget &&
411 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
412 __skb_unlink(skb, &vi->recv);
413 receive_skb(vi->dev, skb, len);
418 if (vi->num < vi->max / 2) {
419 if (!try_fill_recv(vi, GFP_ATOMIC))
420 schedule_delayed_work(&vi->refill, 0);
423 /* Out of packets? */
424 if (received < budget) {
426 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
427 && napi_schedule_prep(napi)) {
428 vi->rvq->vq_ops->disable_cb(vi->rvq);
429 __napi_schedule(napi);
437 static void free_old_xmit_skbs(struct virtnet_info *vi)
442 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
443 pr_debug("Sent skb %p\n", skb);
444 __skb_unlink(skb, &vi->send);
445 vi->dev->stats.tx_bytes += skb->len;
446 vi->dev->stats.tx_packets++;
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_headlen(skb);
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;
499 return vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
502 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
504 struct virtnet_info *vi = netdev_priv(dev);
507 /* Free up any pending old buffers before queueing new ones. */
508 free_old_xmit_skbs(vi);
510 /* Put new one in send queue and do transmit */
511 __skb_queue_head(&vi->send, skb);
512 if (likely(xmit_skb(vi, skb) >= 0)) {
513 vi->svq->vq_ops->kick(vi->svq);
514 /* Don't wait up for transmitted skbs to be freed. */
520 /* Ring too full for this packet, remove it from queue again. */
521 pr_debug("%s: virtio not prepared to send\n", dev->name);
522 __skb_unlink(skb, &vi->send);
523 netif_stop_queue(dev);
525 /* Activate callback for using skbs: if this returns false it
526 * means some were used in the meantime. */
527 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
528 vi->svq->vq_ops->disable_cb(vi->svq);
529 netif_start_queue(dev);
532 return NETDEV_TX_BUSY;
535 static int virtnet_set_mac_address(struct net_device *dev, void *p)
537 struct virtnet_info *vi = netdev_priv(dev);
538 struct virtio_device *vdev = vi->vdev;
541 ret = eth_mac_addr(dev, p);
545 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
546 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
547 dev->dev_addr, dev->addr_len);
552 #ifdef CONFIG_NET_POLL_CONTROLLER
553 static void virtnet_netpoll(struct net_device *dev)
555 struct virtnet_info *vi = netdev_priv(dev);
557 napi_schedule(&vi->napi);
561 static int virtnet_open(struct net_device *dev)
563 struct virtnet_info *vi = netdev_priv(dev);
565 napi_enable(&vi->napi);
567 /* If all buffers were filled by other side before we napi_enabled, we
568 * won't get another interrupt, so process any outstanding packets
569 * now. virtnet_poll wants re-enable the queue, so we disable here.
570 * We synchronize against interrupts via NAPI_STATE_SCHED */
571 if (napi_schedule_prep(&vi->napi)) {
572 vi->rvq->vq_ops->disable_cb(vi->rvq);
573 __napi_schedule(&vi->napi);
579 * Send command via the control virtqueue and check status. Commands
580 * supported by the hypervisor, as indicated by feature bits, should
581 * never fail unless improperly formated.
583 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
584 struct scatterlist *data, int out, int in)
586 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
587 struct virtio_net_ctrl_hdr ctrl;
588 virtio_net_ctrl_ack status = ~0;
592 /* Caller should know better */
593 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
594 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
596 out++; /* Add header */
597 in++; /* Add return status */
602 sg_init_table(sg, out + in);
604 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
605 for_each_sg(data, s, out + in - 2, i)
606 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
607 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
609 BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0);
611 vi->cvq->vq_ops->kick(vi->cvq);
614 * Spin for a response, the kick causes an ioport write, trapping
615 * into the hypervisor, so the request should be handled immediately.
617 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
620 return status == VIRTIO_NET_OK;
623 static int virtnet_close(struct net_device *dev)
625 struct virtnet_info *vi = netdev_priv(dev);
627 napi_disable(&vi->napi);
632 static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
634 struct virtnet_info *vi = netdev_priv(dev);
635 struct virtio_device *vdev = vi->vdev;
637 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
640 return ethtool_op_set_tx_hw_csum(dev, data);
643 static void virtnet_set_rx_mode(struct net_device *dev)
645 struct virtnet_info *vi = netdev_priv(dev);
646 struct scatterlist sg[2];
647 u8 promisc, allmulti;
648 struct virtio_net_ctrl_mac *mac_data;
649 struct dev_addr_list *addr;
650 struct netdev_hw_addr *ha;
654 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
655 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
658 promisc = ((dev->flags & IFF_PROMISC) != 0);
659 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
661 sg_init_one(sg, &promisc, sizeof(promisc));
663 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
664 VIRTIO_NET_CTRL_RX_PROMISC,
666 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
667 promisc ? "en" : "dis");
669 sg_init_one(sg, &allmulti, sizeof(allmulti));
671 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
672 VIRTIO_NET_CTRL_RX_ALLMULTI,
674 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
675 allmulti ? "en" : "dis");
677 /* MAC filter - use one buffer for both lists */
678 mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
679 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
681 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
685 sg_init_table(sg, 2);
687 /* Store the unicast list and count in the front of the buffer */
688 mac_data->entries = dev->uc.count;
690 list_for_each_entry(ha, &dev->uc.list, list)
691 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
693 sg_set_buf(&sg[0], mac_data,
694 sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
696 /* multicast list and count fill the end */
697 mac_data = (void *)&mac_data->macs[dev->uc.count][0];
699 mac_data->entries = dev->mc_count;
701 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
702 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
704 sg_set_buf(&sg[1], mac_data,
705 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
707 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
708 VIRTIO_NET_CTRL_MAC_TABLE_SET,
710 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
715 static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
717 struct virtnet_info *vi = netdev_priv(dev);
718 struct scatterlist sg;
720 sg_init_one(&sg, &vid, sizeof(vid));
722 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
723 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
724 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
727 static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
729 struct virtnet_info *vi = netdev_priv(dev);
730 struct scatterlist sg;
732 sg_init_one(&sg, &vid, sizeof(vid));
734 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
735 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
736 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
739 static const struct ethtool_ops virtnet_ethtool_ops = {
740 .set_tx_csum = virtnet_set_tx_csum,
741 .set_sg = ethtool_op_set_sg,
742 .set_tso = ethtool_op_set_tso,
743 .set_ufo = ethtool_op_set_ufo,
744 .get_link = ethtool_op_get_link,
748 #define MAX_MTU 65535
750 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
752 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
758 static const struct net_device_ops virtnet_netdev = {
759 .ndo_open = virtnet_open,
760 .ndo_stop = virtnet_close,
761 .ndo_start_xmit = start_xmit,
762 .ndo_validate_addr = eth_validate_addr,
763 .ndo_set_mac_address = virtnet_set_mac_address,
764 .ndo_set_rx_mode = virtnet_set_rx_mode,
765 .ndo_change_mtu = virtnet_change_mtu,
766 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
767 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
768 #ifdef CONFIG_NET_POLL_CONTROLLER
769 .ndo_poll_controller = virtnet_netpoll,
773 static void virtnet_update_status(struct virtnet_info *vi)
777 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
780 vi->vdev->config->get(vi->vdev,
781 offsetof(struct virtio_net_config, status),
784 /* Ignore unknown (future) status bits */
785 v &= VIRTIO_NET_S_LINK_UP;
792 if (vi->status & VIRTIO_NET_S_LINK_UP) {
793 netif_carrier_on(vi->dev);
794 netif_wake_queue(vi->dev);
796 netif_carrier_off(vi->dev);
797 netif_stop_queue(vi->dev);
801 static void virtnet_config_changed(struct virtio_device *vdev)
803 struct virtnet_info *vi = vdev->priv;
805 virtnet_update_status(vi);
808 static int virtnet_probe(struct virtio_device *vdev)
811 struct net_device *dev;
812 struct virtnet_info *vi;
813 struct virtqueue *vqs[3];
814 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
815 const char *names[] = { "input", "output", "control" };
818 /* Allocate ourselves a network device with room for our info */
819 dev = alloc_etherdev(sizeof(struct virtnet_info));
823 /* Set up network device as normal. */
824 dev->netdev_ops = &virtnet_netdev;
825 dev->features = NETIF_F_HIGHDMA;
826 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
827 SET_NETDEV_DEV(dev, &vdev->dev);
829 /* Do we support "hardware" checksums? */
830 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
831 /* This opens up the world of extra features. */
832 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
833 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
834 dev->features |= NETIF_F_TSO | NETIF_F_UFO
835 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
837 /* Individual feature bits: what can host handle? */
838 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
839 dev->features |= NETIF_F_TSO;
840 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
841 dev->features |= NETIF_F_TSO6;
842 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
843 dev->features |= NETIF_F_TSO_ECN;
844 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
845 dev->features |= NETIF_F_UFO;
848 /* Configuration may specify what MAC to use. Otherwise random. */
849 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
850 vdev->config->get(vdev,
851 offsetof(struct virtio_net_config, mac),
852 dev->dev_addr, dev->addr_len);
854 random_ether_addr(dev->dev_addr);
856 /* Set up our device-specific information */
857 vi = netdev_priv(dev);
858 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
863 INIT_DELAYED_WORK(&vi->refill, refill_work);
865 /* If we can receive ANY GSO packets, we must allocate large ones. */
866 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
867 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
868 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
869 vi->big_packets = true;
871 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
872 vi->mergeable_rx_bufs = true;
874 /* We expect two virtqueues, receive then send,
875 * and optionally control. */
876 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
878 err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
885 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
888 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
889 dev->features |= NETIF_F_HW_VLAN_FILTER;
892 /* Initialize our empty receive and send queues. */
893 skb_queue_head_init(&vi->recv);
894 skb_queue_head_init(&vi->send);
896 err = register_netdev(dev);
898 pr_debug("virtio_net: registering device failed\n");
902 /* Last of all, set up some receive buffers. */
903 try_fill_recv(vi, GFP_KERNEL);
905 /* If we didn't even get one input buffer, we're useless. */
911 vi->status = VIRTIO_NET_S_LINK_UP;
912 virtnet_update_status(vi);
913 netif_carrier_on(dev);
915 pr_debug("virtnet: registered device %s\n", dev->name);
919 unregister_netdev(dev);
920 cancel_delayed_work_sync(&vi->refill);
922 vdev->config->del_vqs(vdev);
928 static void virtnet_remove(struct virtio_device *vdev)
930 struct virtnet_info *vi = vdev->priv;
933 /* Stop all the virtqueues. */
934 vdev->config->reset(vdev);
936 /* Free our skbs in send and recv queues, if any. */
937 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
941 __skb_queue_purge(&vi->send);
943 BUG_ON(vi->num != 0);
945 unregister_netdev(vi->dev);
946 cancel_delayed_work_sync(&vi->refill);
948 vdev->config->del_vqs(vi->vdev);
951 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
953 free_netdev(vi->dev);
956 static struct virtio_device_id id_table[] = {
957 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
961 static unsigned int features[] = {
962 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
963 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
964 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
965 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
966 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
967 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
968 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
971 static struct virtio_driver virtio_net = {
972 .feature_table = features,
973 .feature_table_size = ARRAY_SIZE(features),
974 .driver.name = KBUILD_MODNAME,
975 .driver.owner = THIS_MODULE,
976 .id_table = id_table,
977 .probe = virtnet_probe,
978 .remove = __devexit_p(virtnet_remove),
979 .config_changed = virtnet_config_changed,
982 static int __init init(void)
984 return register_virtio_driver(&virtio_net);
987 static void __exit fini(void)
989 unregister_virtio_driver(&virtio_net);
994 MODULE_DEVICE_TABLE(virtio, id_table);
995 MODULE_DESCRIPTION("Virtio network driver");
996 MODULE_LICENSE("GPL");