virtio_net.c 11 KB

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