virtio_net.c 11 KB

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