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 | |
| 44 | /* Number of input buffers, and max we've ever had. */ |
| 45 | unsigned int num, max; |
| 46 | |
| 47 | /* Receive & send queues. */ |
| 48 | struct sk_buff_head recv; |
| 49 | struct sk_buff_head send; |
| 50 | }; |
| 51 | |
| 52 | static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb) |
| 53 | { |
| 54 | return (struct virtio_net_hdr *)skb->cb; |
| 55 | } |
| 56 | |
| 57 | static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb) |
| 58 | { |
| 59 | sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr)); |
| 60 | } |
| 61 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 62 | static void skb_xmit_done(struct virtqueue *svq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 63 | { |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 64 | struct virtnet_info *vi = svq->vdev->priv; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 65 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 66 | /* Suppress further interrupts. */ |
| 67 | svq->vq_ops->disable_cb(svq); |
| 68 | /* We were waiting for more output buffers. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 69 | netif_wake_queue(vi->dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, |
| 73 | unsigned len) |
| 74 | { |
| 75 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); |
| 76 | |
| 77 | if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { |
| 78 | pr_debug("%s: short packet %i\n", dev->name, len); |
| 79 | dev->stats.rx_length_errors++; |
| 80 | goto drop; |
| 81 | } |
| 82 | len -= sizeof(struct virtio_net_hdr); |
| 83 | BUG_ON(len > MAX_PACKET_LEN); |
| 84 | |
| 85 | skb_trim(skb, len); |
| 86 | skb->protocol = eth_type_trans(skb, dev); |
| 87 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", |
| 88 | ntohs(skb->protocol), skb->len, skb->pkt_type); |
| 89 | dev->stats.rx_bytes += skb->len; |
| 90 | dev->stats.rx_packets++; |
| 91 | |
| 92 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 93 | pr_debug("Needs csum!\n"); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 94 | if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 95 | goto frame_err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 99 | pr_debug("GSO!\n"); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 100 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 101 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 102 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 103 | break; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 104 | case VIRTIO_NET_HDR_GSO_UDP: |
| 105 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
| 106 | break; |
| 107 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 108 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
| 109 | break; |
| 110 | default: |
| 111 | if (net_ratelimit()) |
| 112 | printk(KERN_WARNING "%s: bad gso type %u.\n", |
| 113 | dev->name, hdr->gso_type); |
| 114 | goto frame_err; |
| 115 | } |
| 116 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 117 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
| 118 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
| 119 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 120 | skb_shinfo(skb)->gso_size = hdr->gso_size; |
| 121 | if (skb_shinfo(skb)->gso_size == 0) { |
| 122 | if (net_ratelimit()) |
| 123 | printk(KERN_WARNING "%s: zero gso size.\n", |
| 124 | dev->name); |
| 125 | goto frame_err; |
| 126 | } |
| 127 | |
| 128 | /* Header must be checked, and gso_segs computed. */ |
| 129 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 130 | skb_shinfo(skb)->gso_segs = 0; |
| 131 | } |
| 132 | |
| 133 | netif_receive_skb(skb); |
| 134 | return; |
| 135 | |
| 136 | frame_err: |
| 137 | dev->stats.rx_frame_errors++; |
| 138 | drop: |
| 139 | dev_kfree_skb(skb); |
| 140 | } |
| 141 | |
| 142 | static void try_fill_recv(struct virtnet_info *vi) |
| 143 | { |
| 144 | struct sk_buff *skb; |
| 145 | struct scatterlist sg[1+MAX_SKB_FRAGS]; |
| 146 | int num, err; |
| 147 | |
Rusty Russell | 4d125de | 2007-11-07 16:34:49 +1100 | [diff] [blame] | 148 | sg_init_table(sg, 1+MAX_SKB_FRAGS); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 149 | for (;;) { |
| 150 | skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); |
| 151 | if (unlikely(!skb)) |
| 152 | break; |
| 153 | |
| 154 | skb_put(skb, MAX_PACKET_LEN); |
| 155 | vnet_hdr_to_sg(sg, skb); |
| 156 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
| 157 | skb_queue_head(&vi->recv, skb); |
| 158 | |
| 159 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); |
| 160 | if (err) { |
| 161 | skb_unlink(skb, &vi->recv); |
| 162 | kfree_skb(skb); |
| 163 | break; |
| 164 | } |
| 165 | vi->num++; |
| 166 | } |
| 167 | if (unlikely(vi->num > vi->max)) |
| 168 | vi->max = vi->num; |
| 169 | vi->rvq->vq_ops->kick(vi->rvq); |
| 170 | } |
| 171 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 172 | static void skb_recv_done(struct virtqueue *rvq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 173 | { |
| 174 | struct virtnet_info *vi = rvq->vdev->priv; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 175 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
| 176 | if (netif_rx_schedule_prep(vi->dev, &vi->napi)) { |
| 177 | rvq->vq_ops->disable_cb(rvq); |
| 178 | __netif_rx_schedule(vi->dev, &vi->napi); |
| 179 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int virtnet_poll(struct napi_struct *napi, int budget) |
| 183 | { |
| 184 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); |
| 185 | struct sk_buff *skb = NULL; |
| 186 | unsigned int len, received = 0; |
| 187 | |
| 188 | again: |
| 189 | while (received < budget && |
| 190 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { |
| 191 | __skb_unlink(skb, &vi->recv); |
| 192 | receive_skb(vi->dev, skb, len); |
| 193 | vi->num--; |
| 194 | received++; |
| 195 | } |
| 196 | |
| 197 | /* FIXME: If we oom and completely run out of inbufs, we need |
| 198 | * to start a timer trying to fill more. */ |
| 199 | if (vi->num < vi->max / 2) |
| 200 | try_fill_recv(vi); |
| 201 | |
Rusty Russell | 8329d98 | 2007-11-19 11:20:43 -0500 | [diff] [blame] | 202 | /* Out of packets? */ |
| 203 | if (received < budget) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 204 | netif_rx_complete(vi->dev, napi); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 205 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 206 | && napi_schedule_prep(napi)) { |
| 207 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
| 208 | __netif_rx_schedule(vi->dev, napi); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 209 | goto again; |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 210 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return received; |
| 214 | } |
| 215 | |
| 216 | static void free_old_xmit_skbs(struct virtnet_info *vi) |
| 217 | { |
| 218 | struct sk_buff *skb; |
| 219 | unsigned int len; |
| 220 | |
| 221 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { |
| 222 | pr_debug("Sent skb %p\n", skb); |
| 223 | __skb_unlink(skb, &vi->send); |
| 224 | vi->dev->stats.tx_bytes += len; |
| 225 | vi->dev->stats.tx_packets++; |
| 226 | kfree_skb(skb); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static int start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 231 | { |
| 232 | struct virtnet_info *vi = netdev_priv(dev); |
| 233 | int num, err; |
| 234 | struct scatterlist sg[1+MAX_SKB_FRAGS]; |
| 235 | struct virtio_net_hdr *hdr; |
| 236 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 237 | |
Rusty Russell | 4d125de | 2007-11-07 16:34:49 +1100 | [diff] [blame] | 238 | sg_init_table(sg, 1+MAX_SKB_FRAGS); |
| 239 | |
David S. Miller | 21f644f | 2008-04-08 16:50:44 -0700 | [diff] [blame] | 240 | pr_debug("%s: xmit %p " MAC_FMT "\n", dev->name, skb, |
| 241 | dest[0], dest[1], dest[2], |
| 242 | dest[3], dest[4], dest[5]); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 243 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 244 | /* Encode metadata header at front. */ |
| 245 | hdr = skb_vnet_hdr(skb); |
| 246 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 247 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 248 | hdr->csum_start = skb->csum_start - skb_headroom(skb); |
| 249 | hdr->csum_offset = skb->csum_offset; |
| 250 | } else { |
| 251 | hdr->flags = 0; |
| 252 | hdr->csum_offset = hdr->csum_start = 0; |
| 253 | } |
| 254 | |
| 255 | if (skb_is_gso(skb)) { |
Rusty Russell | 50c8ea8 | 2008-02-04 23:50:01 -0500 | [diff] [blame] | 256 | hdr->hdr_len = skb_transport_header(skb) - skb->data; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 257 | hdr->gso_size = skb_shinfo(skb)->gso_size; |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 258 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 259 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 260 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 261 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
| 262 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 263 | hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; |
| 264 | else |
| 265 | BUG(); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 266 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
| 267 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 268 | } else { |
| 269 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
Rusty Russell | 50c8ea8 | 2008-02-04 23:50:01 -0500 | [diff] [blame] | 270 | hdr->gso_size = hdr->hdr_len = 0; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | vnet_hdr_to_sg(sg, skb); |
| 274 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
| 275 | __skb_queue_head(&vi->send, skb); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 276 | |
| 277 | again: |
| 278 | /* Free up any pending old buffers before queueing new ones. */ |
| 279 | free_old_xmit_skbs(vi); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 280 | err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); |
| 281 | if (err) { |
| 282 | pr_debug("%s: virtio not prepared to send\n", dev->name); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 283 | netif_stop_queue(dev); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 284 | |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 285 | /* Activate callback for using skbs: if this returns false it |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 286 | * means some were used in the meantime. */ |
| 287 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 288 | vi->svq->vq_ops->disable_cb(vi->svq); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 289 | netif_start_queue(dev); |
| 290 | goto again; |
| 291 | } |
| 292 | __skb_unlink(skb, &vi->send); |
| 293 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 294 | return NETDEV_TX_BUSY; |
| 295 | } |
| 296 | vi->svq->vq_ops->kick(vi->svq); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 301 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 302 | static void virtnet_netpoll(struct net_device *dev) |
| 303 | { |
| 304 | struct virtnet_info *vi = netdev_priv(dev); |
| 305 | |
| 306 | napi_schedule(&vi->napi); |
| 307 | } |
| 308 | #endif |
| 309 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 310 | static int virtnet_open(struct net_device *dev) |
| 311 | { |
| 312 | struct virtnet_info *vi = netdev_priv(dev); |
| 313 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 314 | napi_enable(&vi->napi); |
Rusty Russell | a48bd8f | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 315 | |
| 316 | /* If all buffers were filled by other side before we napi_enabled, we |
| 317 | * won't get another interrupt, so process any outstanding packets |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 318 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
| 319 | * We synchronize against interrupts via NAPI_STATE_SCHED */ |
| 320 | if (netif_rx_schedule_prep(dev, &vi->napi)) { |
| 321 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
| 322 | __netif_rx_schedule(dev, &vi->napi); |
| 323 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int virtnet_close(struct net_device *dev) |
| 328 | { |
| 329 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 330 | |
| 331 | napi_disable(&vi->napi); |
| 332 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int virtnet_probe(struct virtio_device *vdev) |
| 337 | { |
| 338 | int err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 339 | struct net_device *dev; |
| 340 | struct virtnet_info *vi; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 341 | |
| 342 | /* Allocate ourselves a network device with room for our info */ |
| 343 | dev = alloc_etherdev(sizeof(struct virtnet_info)); |
| 344 | if (!dev) |
| 345 | return -ENOMEM; |
| 346 | |
| 347 | /* Set up network device as normal. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 348 | dev->open = virtnet_open; |
| 349 | dev->stop = virtnet_close; |
| 350 | dev->hard_start_xmit = start_xmit; |
| 351 | dev->features = NETIF_F_HIGHDMA; |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 352 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 353 | dev->poll_controller = virtnet_netpoll; |
| 354 | #endif |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 355 | SET_NETDEV_DEV(dev, &vdev->dev); |
| 356 | |
| 357 | /* Do we support "hardware" checksums? */ |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 358 | if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 359 | /* This opens up the world of extra features. */ |
| 360 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 361 | if (gso && vdev->config->feature(vdev, VIRTIO_NET_F_GSO)) { |
| 362 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
| 363 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; |
| 364 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* Configuration may specify what MAC to use. Otherwise random. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 368 | if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) { |
| 369 | vdev->config->get(vdev, |
| 370 | offsetof(struct virtio_net_config, mac), |
| 371 | dev->dev_addr, dev->addr_len); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 372 | } else |
| 373 | random_ether_addr(dev->dev_addr); |
| 374 | |
| 375 | /* Set up our device-specific information */ |
| 376 | vi = netdev_priv(dev); |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 377 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 378 | vi->dev = dev; |
| 379 | vi->vdev = vdev; |
Christian Borntraeger | d9d5dcc | 2008-02-18 10:02:51 +0100 | [diff] [blame] | 380 | vdev->priv = vi; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 381 | |
| 382 | /* We expect two virtqueues, receive then send. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 383 | vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 384 | if (IS_ERR(vi->rvq)) { |
| 385 | err = PTR_ERR(vi->rvq); |
| 386 | goto free; |
| 387 | } |
| 388 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 389 | vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 390 | if (IS_ERR(vi->svq)) { |
| 391 | err = PTR_ERR(vi->svq); |
| 392 | goto free_recv; |
| 393 | } |
| 394 | |
| 395 | /* Initialize our empty receive and send queues. */ |
| 396 | skb_queue_head_init(&vi->recv); |
| 397 | skb_queue_head_init(&vi->send); |
| 398 | |
| 399 | err = register_netdev(dev); |
| 400 | if (err) { |
| 401 | pr_debug("virtio_net: registering device failed\n"); |
| 402 | goto free_send; |
| 403 | } |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 404 | |
| 405 | /* Last of all, set up some receive buffers. */ |
| 406 | try_fill_recv(vi); |
| 407 | |
| 408 | /* If we didn't even get one input buffer, we're useless. */ |
| 409 | if (vi->num == 0) { |
| 410 | err = -ENOMEM; |
| 411 | goto unregister; |
| 412 | } |
| 413 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 414 | pr_debug("virtnet: registered device %s\n", dev->name); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 415 | return 0; |
| 416 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 417 | unregister: |
| 418 | unregister_netdev(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 419 | free_send: |
| 420 | vdev->config->del_vq(vi->svq); |
| 421 | free_recv: |
| 422 | vdev->config->del_vq(vi->rvq); |
| 423 | free: |
| 424 | free_netdev(dev); |
| 425 | return err; |
| 426 | } |
| 427 | |
| 428 | static void virtnet_remove(struct virtio_device *vdev) |
| 429 | { |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 430 | struct virtnet_info *vi = vdev->priv; |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 431 | struct sk_buff *skb; |
| 432 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 433 | /* Stop all the virtqueues. */ |
| 434 | vdev->config->reset(vdev); |
| 435 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 436 | /* Free our skbs in send and recv queues, if any. */ |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 437 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
| 438 | kfree_skb(skb); |
| 439 | vi->num--; |
| 440 | } |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 441 | while ((skb = __skb_dequeue(&vi->send)) != NULL) |
| 442 | kfree_skb(skb); |
| 443 | |
| 444 | BUG_ON(vi->num != 0); |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 445 | |
| 446 | vdev->config->del_vq(vi->svq); |
| 447 | vdev->config->del_vq(vi->rvq); |
| 448 | unregister_netdev(vi->dev); |
| 449 | free_netdev(vi->dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static struct virtio_device_id id_table[] = { |
| 453 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, |
| 454 | { 0 }, |
| 455 | }; |
| 456 | |
| 457 | static struct virtio_driver virtio_net = { |
| 458 | .driver.name = KBUILD_MODNAME, |
| 459 | .driver.owner = THIS_MODULE, |
| 460 | .id_table = id_table, |
| 461 | .probe = virtnet_probe, |
| 462 | .remove = __devexit_p(virtnet_remove), |
| 463 | }; |
| 464 | |
| 465 | static int __init init(void) |
| 466 | { |
| 467 | return register_virtio_driver(&virtio_net); |
| 468 | } |
| 469 | |
| 470 | static void __exit fini(void) |
| 471 | { |
| 472 | unregister_virtio_driver(&virtio_net); |
| 473 | } |
| 474 | module_init(init); |
| 475 | module_exit(fini); |
| 476 | |
| 477 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 478 | MODULE_DESCRIPTION("Virtio network driver"); |
| 479 | MODULE_LICENSE("GPL"); |