virtio_net.c 12 KB

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