Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1 | /* A simple network driver using virtio. |
| 2 | * |
| 3 | * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | //#define DEBUG |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/etherdevice.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/virtio.h> |
| 24 | #include <linux/virtio_net.h> |
| 25 | #include <linux/scatterlist.h> |
| 26 | |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 27 | static int napi_weight = 128; |
| 28 | module_param(napi_weight, int, 0444); |
| 29 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 30 | static int csum = 1, gso = 1; |
| 31 | module_param(csum, bool, 0444); |
| 32 | module_param(gso, bool, 0444); |
| 33 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 34 | /* FIXME: MTU in config. */ |
| 35 | #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN) |
| 36 | |
| 37 | struct virtnet_info |
| 38 | { |
| 39 | struct virtio_device *vdev; |
| 40 | struct virtqueue *rvq, *svq; |
| 41 | struct net_device *dev; |
| 42 | struct napi_struct napi; |
| 43 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 44 | /* The skb we couldn't send because buffers were full. */ |
| 45 | struct sk_buff *last_xmit_skb; |
| 46 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 47 | /* Number of input buffers, and max we've ever had. */ |
| 48 | unsigned int num, max; |
| 49 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 50 | /* For cleaning up after transmission. */ |
| 51 | struct tasklet_struct tasklet; |
| 52 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 53 | /* Receive & send queues. */ |
| 54 | struct sk_buff_head recv; |
| 55 | struct sk_buff_head send; |
| 56 | }; |
| 57 | |
| 58 | static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb) |
| 59 | { |
| 60 | return (struct virtio_net_hdr *)skb->cb; |
| 61 | } |
| 62 | |
| 63 | static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb) |
| 64 | { |
| 65 | sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr)); |
| 66 | } |
| 67 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 68 | static void skb_xmit_done(struct virtqueue *svq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 69 | { |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 70 | struct virtnet_info *vi = svq->vdev->priv; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 71 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 72 | /* Suppress further interrupts. */ |
| 73 | svq->vq_ops->disable_cb(svq); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 74 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 75 | /* We were waiting for more output buffers. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 76 | netif_wake_queue(vi->dev); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 77 | |
| 78 | /* Make sure we re-xmit last_xmit_skb: if there are no more packets |
| 79 | * queued, start_xmit won't be called. */ |
| 80 | tasklet_schedule(&vi->tasklet); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, |
| 84 | unsigned len) |
| 85 | { |
| 86 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); |
| 87 | |
| 88 | if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { |
| 89 | pr_debug("%s: short packet %i\n", dev->name, len); |
| 90 | dev->stats.rx_length_errors++; |
| 91 | goto drop; |
| 92 | } |
| 93 | len -= sizeof(struct virtio_net_hdr); |
| 94 | BUG_ON(len > MAX_PACKET_LEN); |
| 95 | |
| 96 | skb_trim(skb, len); |
| 97 | skb->protocol = eth_type_trans(skb, dev); |
| 98 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", |
| 99 | ntohs(skb->protocol), skb->len, skb->pkt_type); |
| 100 | dev->stats.rx_bytes += skb->len; |
| 101 | dev->stats.rx_packets++; |
| 102 | |
| 103 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 104 | pr_debug("Needs csum!\n"); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 105 | if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 106 | goto frame_err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 110 | pr_debug("GSO!\n"); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 111 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 112 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 113 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 114 | break; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 115 | case VIRTIO_NET_HDR_GSO_UDP: |
| 116 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
| 117 | break; |
| 118 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 119 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
| 120 | break; |
| 121 | default: |
| 122 | if (net_ratelimit()) |
| 123 | printk(KERN_WARNING "%s: bad gso type %u.\n", |
| 124 | dev->name, hdr->gso_type); |
| 125 | goto frame_err; |
| 126 | } |
| 127 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 128 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
| 129 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
| 130 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 131 | skb_shinfo(skb)->gso_size = hdr->gso_size; |
| 132 | if (skb_shinfo(skb)->gso_size == 0) { |
| 133 | if (net_ratelimit()) |
| 134 | printk(KERN_WARNING "%s: zero gso size.\n", |
| 135 | dev->name); |
| 136 | goto frame_err; |
| 137 | } |
| 138 | |
| 139 | /* Header must be checked, and gso_segs computed. */ |
| 140 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 141 | skb_shinfo(skb)->gso_segs = 0; |
| 142 | } |
| 143 | |
| 144 | netif_receive_skb(skb); |
| 145 | return; |
| 146 | |
| 147 | frame_err: |
| 148 | dev->stats.rx_frame_errors++; |
| 149 | drop: |
| 150 | dev_kfree_skb(skb); |
| 151 | } |
| 152 | |
| 153 | static void try_fill_recv(struct virtnet_info *vi) |
| 154 | { |
| 155 | struct sk_buff *skb; |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 156 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 157 | int num, err; |
| 158 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 159 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 160 | for (;;) { |
| 161 | skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); |
| 162 | if (unlikely(!skb)) |
| 163 | break; |
| 164 | |
| 165 | skb_put(skb, MAX_PACKET_LEN); |
| 166 | vnet_hdr_to_sg(sg, skb); |
| 167 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
| 168 | skb_queue_head(&vi->recv, skb); |
| 169 | |
| 170 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); |
| 171 | if (err) { |
| 172 | skb_unlink(skb, &vi->recv); |
| 173 | kfree_skb(skb); |
| 174 | break; |
| 175 | } |
| 176 | vi->num++; |
| 177 | } |
| 178 | if (unlikely(vi->num > vi->max)) |
| 179 | vi->max = vi->num; |
| 180 | vi->rvq->vq_ops->kick(vi->rvq); |
| 181 | } |
| 182 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 183 | static void skb_recv_done(struct virtqueue *rvq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 184 | { |
| 185 | struct virtnet_info *vi = rvq->vdev->priv; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 186 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
| 187 | if (netif_rx_schedule_prep(vi->dev, &vi->napi)) { |
| 188 | rvq->vq_ops->disable_cb(rvq); |
| 189 | __netif_rx_schedule(vi->dev, &vi->napi); |
| 190 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static int virtnet_poll(struct napi_struct *napi, int budget) |
| 194 | { |
| 195 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); |
| 196 | struct sk_buff *skb = NULL; |
| 197 | unsigned int len, received = 0; |
| 198 | |
| 199 | again: |
| 200 | while (received < budget && |
| 201 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { |
| 202 | __skb_unlink(skb, &vi->recv); |
| 203 | receive_skb(vi->dev, skb, len); |
| 204 | vi->num--; |
| 205 | received++; |
| 206 | } |
| 207 | |
| 208 | /* FIXME: If we oom and completely run out of inbufs, we need |
| 209 | * to start a timer trying to fill more. */ |
| 210 | if (vi->num < vi->max / 2) |
| 211 | try_fill_recv(vi); |
| 212 | |
Rusty Russell | 8329d98 | 2007-11-19 11:20:43 -0500 | [diff] [blame] | 213 | /* Out of packets? */ |
| 214 | if (received < budget) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 215 | netif_rx_complete(vi->dev, napi); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 216 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 217 | && napi_schedule_prep(napi)) { |
| 218 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
| 219 | __netif_rx_schedule(vi->dev, napi); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 220 | goto again; |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 221 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return received; |
| 225 | } |
| 226 | |
| 227 | static void free_old_xmit_skbs(struct virtnet_info *vi) |
| 228 | { |
| 229 | struct sk_buff *skb; |
| 230 | unsigned int len; |
| 231 | |
| 232 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { |
| 233 | pr_debug("Sent skb %p\n", skb); |
| 234 | __skb_unlink(skb, &vi->send); |
Rusty Russell | 655aa31 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 235 | vi->dev->stats.tx_bytes += skb->len; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 236 | vi->dev->stats.tx_packets++; |
| 237 | kfree_skb(skb); |
| 238 | } |
| 239 | } |
| 240 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 241 | static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 242 | { |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 243 | int num; |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 244 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 245 | struct virtio_net_hdr *hdr; |
| 246 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 247 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 248 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Rusty Russell | 4d125de | 2007-11-07 16:34:49 +1100 | [diff] [blame] | 249 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 250 | pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb, |
David S. Miller | 21f644f | 2008-04-08 16:50:44 -0700 | [diff] [blame] | 251 | dest[0], dest[1], dest[2], |
| 252 | dest[3], dest[4], dest[5]); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 253 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 254 | /* Encode metadata header at front. */ |
| 255 | hdr = skb_vnet_hdr(skb); |
| 256 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 257 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 258 | hdr->csum_start = skb->csum_start - skb_headroom(skb); |
| 259 | hdr->csum_offset = skb->csum_offset; |
| 260 | } else { |
| 261 | hdr->flags = 0; |
| 262 | hdr->csum_offset = hdr->csum_start = 0; |
| 263 | } |
| 264 | |
| 265 | if (skb_is_gso(skb)) { |
Rusty Russell | 50c8ea8 | 2008-02-04 23:50:01 -0500 | [diff] [blame] | 266 | hdr->hdr_len = skb_transport_header(skb) - skb->data; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 267 | hdr->gso_size = skb_shinfo(skb)->gso_size; |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 268 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 269 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 270 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 271 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
| 272 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 273 | hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; |
| 274 | else |
| 275 | BUG(); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 276 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
| 277 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 278 | } else { |
| 279 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
Rusty Russell | 50c8ea8 | 2008-02-04 23:50:01 -0500 | [diff] [blame] | 280 | hdr->gso_size = hdr->hdr_len = 0; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | vnet_hdr_to_sg(sg, skb); |
| 284 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 285 | |
| 286 | return vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); |
| 287 | } |
| 288 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 289 | static void xmit_tasklet(unsigned long data) |
| 290 | { |
| 291 | struct virtnet_info *vi = (void *)data; |
| 292 | |
| 293 | netif_tx_lock_bh(vi->dev); |
| 294 | if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { |
| 295 | vi->svq->vq_ops->kick(vi->svq); |
| 296 | vi->last_xmit_skb = NULL; |
| 297 | } |
| 298 | netif_tx_unlock_bh(vi->dev); |
| 299 | } |
| 300 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 301 | static int start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 302 | { |
| 303 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 304 | |
| 305 | again: |
| 306 | /* Free up any pending old buffers before queueing new ones. */ |
| 307 | free_old_xmit_skbs(vi); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 308 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 309 | /* If we has a buffer left over from last time, send it now. */ |
Rusty Russell | 7eb2e25 | 2008-05-26 17:42:42 +1000 | [diff] [blame] | 310 | if (unlikely(vi->last_xmit_skb)) { |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 311 | if (xmit_skb(vi, vi->last_xmit_skb) != 0) { |
| 312 | /* Drop this skb: we only queue one. */ |
| 313 | vi->dev->stats.tx_dropped++; |
| 314 | kfree_skb(skb); |
Rusty Russell | 7eb2e25 | 2008-05-26 17:42:42 +1000 | [diff] [blame] | 315 | skb = NULL; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 316 | goto stop_queue; |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 317 | } |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 318 | vi->last_xmit_skb = NULL; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 319 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 320 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 321 | /* Put new one in send queue and do transmit */ |
Rusty Russell | 7eb2e25 | 2008-05-26 17:42:42 +1000 | [diff] [blame] | 322 | if (likely(skb)) { |
| 323 | __skb_queue_head(&vi->send, skb); |
| 324 | if (xmit_skb(vi, skb) != 0) { |
| 325 | vi->last_xmit_skb = skb; |
| 326 | skb = NULL; |
| 327 | goto stop_queue; |
| 328 | } |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 329 | } |
| 330 | done: |
| 331 | vi->svq->vq_ops->kick(vi->svq); |
| 332 | return NETDEV_TX_OK; |
| 333 | |
| 334 | stop_queue: |
| 335 | pr_debug("%s: virtio not prepared to send\n", dev->name); |
| 336 | netif_stop_queue(dev); |
| 337 | |
| 338 | /* Activate callback for using skbs: if this returns false it |
| 339 | * means some were used in the meantime. */ |
| 340 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { |
| 341 | vi->svq->vq_ops->disable_cb(vi->svq); |
| 342 | netif_start_queue(dev); |
| 343 | goto again; |
| 344 | } |
| 345 | goto done; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 346 | } |
| 347 | |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 348 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 349 | static void virtnet_netpoll(struct net_device *dev) |
| 350 | { |
| 351 | struct virtnet_info *vi = netdev_priv(dev); |
| 352 | |
| 353 | napi_schedule(&vi->napi); |
| 354 | } |
| 355 | #endif |
| 356 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 357 | static int virtnet_open(struct net_device *dev) |
| 358 | { |
| 359 | struct virtnet_info *vi = netdev_priv(dev); |
| 360 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 361 | napi_enable(&vi->napi); |
Rusty Russell | a48bd8f | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 362 | |
| 363 | /* If all buffers were filled by other side before we napi_enabled, we |
| 364 | * won't get another interrupt, so process any outstanding packets |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 365 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
| 366 | * We synchronize against interrupts via NAPI_STATE_SCHED */ |
| 367 | if (netif_rx_schedule_prep(dev, &vi->napi)) { |
| 368 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
| 369 | __netif_rx_schedule(dev, &vi->napi); |
| 370 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int virtnet_close(struct net_device *dev) |
| 375 | { |
| 376 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 377 | |
| 378 | napi_disable(&vi->napi); |
| 379 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int virtnet_probe(struct virtio_device *vdev) |
| 384 | { |
| 385 | int err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 386 | struct net_device *dev; |
| 387 | struct virtnet_info *vi; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 388 | |
| 389 | /* Allocate ourselves a network device with room for our info */ |
| 390 | dev = alloc_etherdev(sizeof(struct virtnet_info)); |
| 391 | if (!dev) |
| 392 | return -ENOMEM; |
| 393 | |
| 394 | /* Set up network device as normal. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 395 | dev->open = virtnet_open; |
| 396 | dev->stop = virtnet_close; |
| 397 | dev->hard_start_xmit = start_xmit; |
| 398 | dev->features = NETIF_F_HIGHDMA; |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 399 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 400 | dev->poll_controller = virtnet_netpoll; |
| 401 | #endif |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 402 | SET_NETDEV_DEV(dev, &vdev->dev); |
| 403 | |
| 404 | /* Do we support "hardware" checksums? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 405 | if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 406 | /* This opens up the world of extra features. */ |
| 407 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 408 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 409 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
| 410 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; |
| 411 | } |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 412 | /* Individual feature bits: what can host handle? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 413 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 414 | dev->features |= NETIF_F_TSO; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 415 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 416 | dev->features |= NETIF_F_TSO6; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 417 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 418 | dev->features |= NETIF_F_TSO_ECN; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 419 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 420 | dev->features |= NETIF_F_UFO; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* Configuration may specify what MAC to use. Otherwise random. */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 424 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 425 | vdev->config->get(vdev, |
| 426 | offsetof(struct virtio_net_config, mac), |
| 427 | dev->dev_addr, dev->addr_len); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 428 | } else |
| 429 | random_ether_addr(dev->dev_addr); |
| 430 | |
| 431 | /* Set up our device-specific information */ |
| 432 | vi = netdev_priv(dev); |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 433 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 434 | vi->dev = dev; |
| 435 | vi->vdev = vdev; |
Christian Borntraeger | d9d5dcc | 2008-02-18 10:02:51 +0100 | [diff] [blame] | 436 | vdev->priv = vi; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 437 | |
| 438 | /* We expect two virtqueues, receive then send. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 439 | vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 440 | if (IS_ERR(vi->rvq)) { |
| 441 | err = PTR_ERR(vi->rvq); |
| 442 | goto free; |
| 443 | } |
| 444 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 445 | vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 446 | if (IS_ERR(vi->svq)) { |
| 447 | err = PTR_ERR(vi->svq); |
| 448 | goto free_recv; |
| 449 | } |
| 450 | |
| 451 | /* Initialize our empty receive and send queues. */ |
| 452 | skb_queue_head_init(&vi->recv); |
| 453 | skb_queue_head_init(&vi->send); |
| 454 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 455 | tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); |
| 456 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 457 | err = register_netdev(dev); |
| 458 | if (err) { |
| 459 | pr_debug("virtio_net: registering device failed\n"); |
| 460 | goto free_send; |
| 461 | } |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 462 | |
| 463 | /* Last of all, set up some receive buffers. */ |
| 464 | try_fill_recv(vi); |
| 465 | |
| 466 | /* If we didn't even get one input buffer, we're useless. */ |
| 467 | if (vi->num == 0) { |
| 468 | err = -ENOMEM; |
| 469 | goto unregister; |
| 470 | } |
| 471 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 472 | pr_debug("virtnet: registered device %s\n", dev->name); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 473 | return 0; |
| 474 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 475 | unregister: |
| 476 | unregister_netdev(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 477 | free_send: |
| 478 | vdev->config->del_vq(vi->svq); |
| 479 | free_recv: |
| 480 | vdev->config->del_vq(vi->rvq); |
| 481 | free: |
| 482 | free_netdev(dev); |
| 483 | return err; |
| 484 | } |
| 485 | |
| 486 | static void virtnet_remove(struct virtio_device *vdev) |
| 487 | { |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 488 | struct virtnet_info *vi = vdev->priv; |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 489 | struct sk_buff *skb; |
| 490 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 491 | /* Stop all the virtqueues. */ |
| 492 | vdev->config->reset(vdev); |
| 493 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 494 | /* Free our skbs in send and recv queues, if any. */ |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 495 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
| 496 | kfree_skb(skb); |
| 497 | vi->num--; |
| 498 | } |
Wang Chen | 288369c | 2008-05-22 18:07:43 +0800 | [diff] [blame] | 499 | __skb_queue_purge(&vi->send); |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 500 | |
| 501 | BUG_ON(vi->num != 0); |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 502 | |
| 503 | vdev->config->del_vq(vi->svq); |
| 504 | vdev->config->del_vq(vi->rvq); |
| 505 | unregister_netdev(vi->dev); |
| 506 | free_netdev(vi->dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static struct virtio_device_id id_table[] = { |
| 510 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, |
| 511 | { 0 }, |
| 512 | }; |
| 513 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 514 | static unsigned int features[] = { |
| 515 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, |
| 516 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, |
| 517 | VIRTIO_NET_F_HOST_ECN, |
| 518 | }; |
| 519 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 520 | static struct virtio_driver virtio_net = { |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 521 | .feature_table = features, |
| 522 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 523 | .driver.name = KBUILD_MODNAME, |
| 524 | .driver.owner = THIS_MODULE, |
| 525 | .id_table = id_table, |
| 526 | .probe = virtnet_probe, |
| 527 | .remove = __devexit_p(virtnet_remove), |
| 528 | }; |
| 529 | |
| 530 | static int __init init(void) |
| 531 | { |
| 532 | return register_virtio_driver(&virtio_net); |
| 533 | } |
| 534 | |
| 535 | static void __exit fini(void) |
| 536 | { |
| 537 | unregister_virtio_driver(&virtio_net); |
| 538 | } |
| 539 | module_init(init); |
| 540 | module_exit(fini); |
| 541 | |
| 542 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 543 | MODULE_DESCRIPTION("Virtio network driver"); |
| 544 | MODULE_LICENSE("GPL"); |