virtio_net.c 14 KB

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