virtio_net.c 11 KB

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