virtio_net.c 17 KB

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