virtio_net.c 13 KB

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