virtio_net.c 17 KB

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