virtio_net.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 " MAC_FMT "\n", vi->dev->name, skb,
  271. dest[0], dest[1], dest[2],
  272. dest[3], dest[4], dest[5]);
  273. /* Encode metadata header at front. */
  274. hdr = skb_vnet_hdr(skb);
  275. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  276. hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  277. hdr->csum_start = skb->csum_start - skb_headroom(skb);
  278. hdr->csum_offset = skb->csum_offset;
  279. } else {
  280. hdr->flags = 0;
  281. hdr->csum_offset = hdr->csum_start = 0;
  282. }
  283. if (skb_is_gso(skb)) {
  284. hdr->hdr_len = skb_transport_header(skb) - skb->data;
  285. hdr->gso_size = skb_shinfo(skb)->gso_size;
  286. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  287. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  288. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  289. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  290. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  291. hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  292. else
  293. BUG();
  294. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  295. hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  296. } else {
  297. hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  298. hdr->gso_size = hdr->hdr_len = 0;
  299. }
  300. vnet_hdr_to_sg(sg, skb);
  301. num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
  302. err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
  303. if (!err && !vi->free_in_tasklet)
  304. mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
  305. return err;
  306. }
  307. static void xmit_tasklet(unsigned long data)
  308. {
  309. struct virtnet_info *vi = (void *)data;
  310. netif_tx_lock_bh(vi->dev);
  311. if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
  312. vi->svq->vq_ops->kick(vi->svq);
  313. vi->last_xmit_skb = NULL;
  314. }
  315. if (vi->free_in_tasklet)
  316. free_old_xmit_skbs(vi);
  317. netif_tx_unlock_bh(vi->dev);
  318. }
  319. static int start_xmit(struct sk_buff *skb, struct net_device *dev)
  320. {
  321. struct virtnet_info *vi = netdev_priv(dev);
  322. again:
  323. /* Free up any pending old buffers before queueing new ones. */
  324. free_old_xmit_skbs(vi);
  325. /* If we has a buffer left over from last time, send it now. */
  326. if (unlikely(vi->last_xmit_skb) &&
  327. xmit_skb(vi, vi->last_xmit_skb) != 0)
  328. goto stop_queue;
  329. vi->last_xmit_skb = NULL;
  330. /* Put new one in send queue and do transmit */
  331. if (likely(skb)) {
  332. __skb_queue_head(&vi->send, skb);
  333. if (xmit_skb(vi, skb) != 0) {
  334. vi->last_xmit_skb = skb;
  335. skb = NULL;
  336. goto stop_queue;
  337. }
  338. }
  339. done:
  340. vi->svq->vq_ops->kick(vi->svq);
  341. return NETDEV_TX_OK;
  342. stop_queue:
  343. pr_debug("%s: virtio not prepared to send\n", dev->name);
  344. netif_stop_queue(dev);
  345. /* Activate callback for using skbs: if this returns false it
  346. * means some were used in the meantime. */
  347. if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
  348. vi->svq->vq_ops->disable_cb(vi->svq);
  349. netif_start_queue(dev);
  350. goto again;
  351. }
  352. if (skb) {
  353. /* Drop this skb: we only queue one. */
  354. vi->dev->stats.tx_dropped++;
  355. kfree_skb(skb);
  356. }
  357. goto done;
  358. }
  359. #ifdef CONFIG_NET_POLL_CONTROLLER
  360. static void virtnet_netpoll(struct net_device *dev)
  361. {
  362. struct virtnet_info *vi = netdev_priv(dev);
  363. napi_schedule(&vi->napi);
  364. }
  365. #endif
  366. static int virtnet_open(struct net_device *dev)
  367. {
  368. struct virtnet_info *vi = netdev_priv(dev);
  369. napi_enable(&vi->napi);
  370. /* If all buffers were filled by other side before we napi_enabled, we
  371. * won't get another interrupt, so process any outstanding packets
  372. * now. virtnet_poll wants re-enable the queue, so we disable here.
  373. * We synchronize against interrupts via NAPI_STATE_SCHED */
  374. if (netif_rx_schedule_prep(dev, &vi->napi)) {
  375. vi->rvq->vq_ops->disable_cb(vi->rvq);
  376. __netif_rx_schedule(dev, &vi->napi);
  377. }
  378. return 0;
  379. }
  380. static int virtnet_close(struct net_device *dev)
  381. {
  382. struct virtnet_info *vi = netdev_priv(dev);
  383. napi_disable(&vi->napi);
  384. return 0;
  385. }
  386. static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
  387. {
  388. struct virtnet_info *vi = netdev_priv(dev);
  389. struct virtio_device *vdev = vi->vdev;
  390. if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
  391. return -ENOSYS;
  392. return ethtool_op_set_tx_hw_csum(dev, data);
  393. }
  394. static struct ethtool_ops virtnet_ethtool_ops = {
  395. .set_tx_csum = virtnet_set_tx_csum,
  396. .set_sg = ethtool_op_set_sg,
  397. };
  398. static int virtnet_probe(struct virtio_device *vdev)
  399. {
  400. int err;
  401. struct net_device *dev;
  402. struct virtnet_info *vi;
  403. /* Allocate ourselves a network device with room for our info */
  404. dev = alloc_etherdev(sizeof(struct virtnet_info));
  405. if (!dev)
  406. return -ENOMEM;
  407. /* Set up network device as normal. */
  408. dev->open = virtnet_open;
  409. dev->stop = virtnet_close;
  410. dev->hard_start_xmit = start_xmit;
  411. dev->features = NETIF_F_HIGHDMA;
  412. #ifdef CONFIG_NET_POLL_CONTROLLER
  413. dev->poll_controller = virtnet_netpoll;
  414. #endif
  415. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  416. SET_NETDEV_DEV(dev, &vdev->dev);
  417. /* Do we support "hardware" checksums? */
  418. if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  419. /* This opens up the world of extra features. */
  420. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  421. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  422. dev->features |= NETIF_F_TSO | NETIF_F_UFO
  423. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  424. }
  425. /* Individual feature bits: what can host handle? */
  426. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  427. dev->features |= NETIF_F_TSO;
  428. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  429. dev->features |= NETIF_F_TSO6;
  430. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  431. dev->features |= NETIF_F_TSO_ECN;
  432. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  433. dev->features |= NETIF_F_UFO;
  434. }
  435. /* Configuration may specify what MAC to use. Otherwise random. */
  436. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  437. vdev->config->get(vdev,
  438. offsetof(struct virtio_net_config, mac),
  439. dev->dev_addr, dev->addr_len);
  440. } else
  441. random_ether_addr(dev->dev_addr);
  442. /* Set up our device-specific information */
  443. vi = netdev_priv(dev);
  444. netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
  445. vi->dev = dev;
  446. vi->vdev = vdev;
  447. vdev->priv = vi;
  448. vi->pages = NULL;
  449. /* If they give us a callback when all buffers are done, we don't need
  450. * the timer. */
  451. vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
  452. /* If we can receive ANY GSO packets, we must allocate large ones. */
  453. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
  454. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
  455. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  456. vi->big_packets = true;
  457. /* We expect two virtqueues, receive then send. */
  458. vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
  459. if (IS_ERR(vi->rvq)) {
  460. err = PTR_ERR(vi->rvq);
  461. goto free;
  462. }
  463. vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
  464. if (IS_ERR(vi->svq)) {
  465. err = PTR_ERR(vi->svq);
  466. goto free_recv;
  467. }
  468. /* Initialize our empty receive and send queues. */
  469. skb_queue_head_init(&vi->recv);
  470. skb_queue_head_init(&vi->send);
  471. tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
  472. if (!vi->free_in_tasklet)
  473. setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
  474. err = register_netdev(dev);
  475. if (err) {
  476. pr_debug("virtio_net: registering device failed\n");
  477. goto free_send;
  478. }
  479. /* Last of all, set up some receive buffers. */
  480. try_fill_recv(vi);
  481. /* If we didn't even get one input buffer, we're useless. */
  482. if (vi->num == 0) {
  483. err = -ENOMEM;
  484. goto unregister;
  485. }
  486. pr_debug("virtnet: registered device %s\n", dev->name);
  487. return 0;
  488. unregister:
  489. unregister_netdev(dev);
  490. free_send:
  491. vdev->config->del_vq(vi->svq);
  492. free_recv:
  493. vdev->config->del_vq(vi->rvq);
  494. free:
  495. free_netdev(dev);
  496. return err;
  497. }
  498. static void virtnet_remove(struct virtio_device *vdev)
  499. {
  500. struct virtnet_info *vi = vdev->priv;
  501. struct sk_buff *skb;
  502. /* Stop all the virtqueues. */
  503. vdev->config->reset(vdev);
  504. if (!vi->free_in_tasklet)
  505. del_timer_sync(&vi->xmit_free_timer);
  506. /* Free our skbs in send and recv queues, if any. */
  507. while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
  508. kfree_skb(skb);
  509. vi->num--;
  510. }
  511. __skb_queue_purge(&vi->send);
  512. BUG_ON(vi->num != 0);
  513. vdev->config->del_vq(vi->svq);
  514. vdev->config->del_vq(vi->rvq);
  515. unregister_netdev(vi->dev);
  516. while (vi->pages)
  517. __free_pages(get_a_page(vi, GFP_KERNEL), 0);
  518. free_netdev(vi->dev);
  519. }
  520. static struct virtio_device_id id_table[] = {
  521. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  522. { 0 },
  523. };
  524. static unsigned int features[] = {
  525. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  526. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  527. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  528. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  529. VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
  530. VIRTIO_F_NOTIFY_ON_EMPTY,
  531. };
  532. static struct virtio_driver virtio_net = {
  533. .feature_table = features,
  534. .feature_table_size = ARRAY_SIZE(features),
  535. .driver.name = KBUILD_MODNAME,
  536. .driver.owner = THIS_MODULE,
  537. .id_table = id_table,
  538. .probe = virtnet_probe,
  539. .remove = __devexit_p(virtnet_remove),
  540. };
  541. static int __init init(void)
  542. {
  543. return register_virtio_driver(&virtio_net);
  544. }
  545. static void __exit fini(void)
  546. {
  547. unregister_virtio_driver(&virtio_net);
  548. }
  549. module_init(init);
  550. module_exit(fini);
  551. MODULE_DEVICE_TABLE(virtio, id_table);
  552. MODULE_DESCRIPTION("Virtio network driver");
  553. MODULE_LICENSE("GPL");