virtio_net.c 17 KB

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