netvsc_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/atomic.h>
  24. #include <linux/module.h>
  25. #include <linux/highmem.h>
  26. #include <linux/device.h>
  27. #include <linux/io.h>
  28. #include <linux/delay.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/inetdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/in.h>
  34. #include <linux/slab.h>
  35. #include <net/arp.h>
  36. #include <net/route.h>
  37. #include <net/sock.h>
  38. #include <net/pkt_sched.h>
  39. #include "hyperv_net.h"
  40. struct net_device_context {
  41. /* point back to our device context */
  42. struct hv_device *device_ctx;
  43. struct delayed_work dwork;
  44. struct work_struct work;
  45. };
  46. static int ring_size = 128;
  47. module_param(ring_size, int, S_IRUGO);
  48. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  49. static void do_set_multicast(struct work_struct *w)
  50. {
  51. struct net_device_context *ndevctx =
  52. container_of(w, struct net_device_context, work);
  53. struct netvsc_device *nvdev;
  54. struct rndis_device *rdev;
  55. nvdev = hv_get_drvdata(ndevctx->device_ctx);
  56. if (nvdev == NULL || nvdev->ndev == NULL)
  57. return;
  58. rdev = nvdev->extension;
  59. if (rdev == NULL)
  60. return;
  61. if (nvdev->ndev->flags & IFF_PROMISC)
  62. rndis_filter_set_packet_filter(rdev,
  63. NDIS_PACKET_TYPE_PROMISCUOUS);
  64. else
  65. rndis_filter_set_packet_filter(rdev,
  66. NDIS_PACKET_TYPE_BROADCAST |
  67. NDIS_PACKET_TYPE_ALL_MULTICAST |
  68. NDIS_PACKET_TYPE_DIRECTED);
  69. }
  70. static void netvsc_set_multicast_list(struct net_device *net)
  71. {
  72. struct net_device_context *net_device_ctx = netdev_priv(net);
  73. schedule_work(&net_device_ctx->work);
  74. }
  75. static int netvsc_open(struct net_device *net)
  76. {
  77. struct net_device_context *net_device_ctx = netdev_priv(net);
  78. struct hv_device *device_obj = net_device_ctx->device_ctx;
  79. int ret = 0;
  80. /* Open up the device */
  81. ret = rndis_filter_open(device_obj);
  82. if (ret != 0) {
  83. netdev_err(net, "unable to open device (ret %d).\n", ret);
  84. return ret;
  85. }
  86. netif_start_queue(net);
  87. return ret;
  88. }
  89. static int netvsc_close(struct net_device *net)
  90. {
  91. struct net_device_context *net_device_ctx = netdev_priv(net);
  92. struct hv_device *device_obj = net_device_ctx->device_ctx;
  93. int ret;
  94. netif_tx_disable(net);
  95. /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
  96. cancel_work_sync(&net_device_ctx->work);
  97. ret = rndis_filter_close(device_obj);
  98. if (ret != 0)
  99. netdev_err(net, "unable to close device (ret %d).\n", ret);
  100. return ret;
  101. }
  102. static void netvsc_xmit_completion(void *context)
  103. {
  104. struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
  105. struct sk_buff *skb = (struct sk_buff *)
  106. (unsigned long)packet->completion.send.send_completion_tid;
  107. kfree(packet);
  108. if (skb)
  109. dev_kfree_skb_any(skb);
  110. }
  111. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  112. {
  113. struct net_device_context *net_device_ctx = netdev_priv(net);
  114. struct hv_netvsc_packet *packet;
  115. int ret;
  116. unsigned int i, num_pages, npg_data;
  117. /* Add multipages for skb->data and additional 2 for RNDIS */
  118. npg_data = (((unsigned long)skb->data + skb_headlen(skb) - 1)
  119. >> PAGE_SHIFT) - ((unsigned long)skb->data >> PAGE_SHIFT) + 1;
  120. num_pages = skb_shinfo(skb)->nr_frags + npg_data + 2;
  121. /* Allocate a netvsc packet based on # of frags. */
  122. packet = kzalloc(sizeof(struct hv_netvsc_packet) +
  123. (num_pages * sizeof(struct hv_page_buffer)) +
  124. sizeof(struct rndis_filter_packet) +
  125. NDIS_VLAN_PPI_SIZE, GFP_ATOMIC);
  126. if (!packet) {
  127. /* out of memory, drop packet */
  128. netdev_err(net, "unable to allocate hv_netvsc_packet\n");
  129. dev_kfree_skb(skb);
  130. net->stats.tx_dropped++;
  131. return NETDEV_TX_OK;
  132. }
  133. packet->vlan_tci = skb->vlan_tci;
  134. packet->extension = (void *)(unsigned long)packet +
  135. sizeof(struct hv_netvsc_packet) +
  136. (num_pages * sizeof(struct hv_page_buffer));
  137. /* If the rndis msg goes beyond 1 page, we will add 1 later */
  138. packet->page_buf_cnt = num_pages - 1;
  139. /* Initialize it from the skb */
  140. packet->total_data_buflen = skb->len;
  141. /* Start filling in the page buffers starting after RNDIS buffer. */
  142. packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
  143. packet->page_buf[1].offset
  144. = (unsigned long)skb->data & (PAGE_SIZE - 1);
  145. if (npg_data == 1)
  146. packet->page_buf[1].len = skb_headlen(skb);
  147. else
  148. packet->page_buf[1].len = PAGE_SIZE
  149. - packet->page_buf[1].offset;
  150. for (i = 2; i <= npg_data; i++) {
  151. packet->page_buf[i].pfn = virt_to_phys(skb->data
  152. + PAGE_SIZE * (i-1)) >> PAGE_SHIFT;
  153. packet->page_buf[i].offset = 0;
  154. packet->page_buf[i].len = PAGE_SIZE;
  155. }
  156. if (npg_data > 1)
  157. packet->page_buf[npg_data].len = (((unsigned long)skb->data
  158. + skb_headlen(skb) - 1) & (PAGE_SIZE - 1)) + 1;
  159. /* Additional fragments are after SKB data */
  160. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  161. const skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  162. packet->page_buf[i+npg_data+1].pfn =
  163. page_to_pfn(skb_frag_page(f));
  164. packet->page_buf[i+npg_data+1].offset = f->page_offset;
  165. packet->page_buf[i+npg_data+1].len = skb_frag_size(f);
  166. }
  167. /* Set the completion routine */
  168. packet->completion.send.send_completion = netvsc_xmit_completion;
  169. packet->completion.send.send_completion_ctx = packet;
  170. packet->completion.send.send_completion_tid = (unsigned long)skb;
  171. ret = rndis_filter_send(net_device_ctx->device_ctx,
  172. packet);
  173. if (ret == 0) {
  174. net->stats.tx_bytes += skb->len;
  175. net->stats.tx_packets++;
  176. } else {
  177. kfree(packet);
  178. if (ret != -EAGAIN) {
  179. dev_kfree_skb_any(skb);
  180. net->stats.tx_dropped++;
  181. }
  182. }
  183. return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  184. }
  185. /*
  186. * netvsc_linkstatus_callback - Link up/down notification
  187. */
  188. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  189. unsigned int status)
  190. {
  191. struct net_device *net;
  192. struct net_device_context *ndev_ctx;
  193. struct netvsc_device *net_device;
  194. net_device = hv_get_drvdata(device_obj);
  195. net = net_device->ndev;
  196. if (!net) {
  197. netdev_err(net, "got link status but net device "
  198. "not initialized yet\n");
  199. return;
  200. }
  201. if (status == 1) {
  202. netif_carrier_on(net);
  203. netif_wake_queue(net);
  204. ndev_ctx = netdev_priv(net);
  205. schedule_delayed_work(&ndev_ctx->dwork, 0);
  206. schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
  207. } else {
  208. netif_carrier_off(net);
  209. netif_tx_disable(net);
  210. }
  211. }
  212. /*
  213. * netvsc_recv_callback - Callback when we receive a packet from the
  214. * "wire" on the specified device.
  215. */
  216. int netvsc_recv_callback(struct hv_device *device_obj,
  217. struct hv_netvsc_packet *packet)
  218. {
  219. struct net_device *net;
  220. struct sk_buff *skb;
  221. net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
  222. if (!net) {
  223. netdev_err(net, "got receive callback but net device"
  224. " not initialized yet\n");
  225. return 0;
  226. }
  227. /* Allocate a skb - TODO direct I/O to pages? */
  228. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  229. if (unlikely(!skb)) {
  230. ++net->stats.rx_dropped;
  231. return 0;
  232. }
  233. /*
  234. * Copy to skb. This copy is needed here since the memory pointed by
  235. * hv_netvsc_packet cannot be deallocated
  236. */
  237. memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
  238. packet->total_data_buflen);
  239. skb->protocol = eth_type_trans(skb, net);
  240. skb->ip_summed = CHECKSUM_NONE;
  241. skb->vlan_tci = packet->vlan_tci;
  242. net->stats.rx_packets++;
  243. net->stats.rx_bytes += packet->total_data_buflen;
  244. /*
  245. * Pass the skb back up. Network stack will deallocate the skb when it
  246. * is done.
  247. * TODO - use NAPI?
  248. */
  249. netif_rx(skb);
  250. return 0;
  251. }
  252. static void netvsc_get_drvinfo(struct net_device *net,
  253. struct ethtool_drvinfo *info)
  254. {
  255. strcpy(info->driver, KBUILD_MODNAME);
  256. strcpy(info->version, HV_DRV_VERSION);
  257. strcpy(info->fw_version, "N/A");
  258. }
  259. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  260. {
  261. struct net_device_context *ndevctx = netdev_priv(ndev);
  262. struct hv_device *hdev = ndevctx->device_ctx;
  263. struct netvsc_device *nvdev = hv_get_drvdata(hdev);
  264. struct netvsc_device_info device_info;
  265. int limit = ETH_DATA_LEN;
  266. if (nvdev == NULL || nvdev->destroy)
  267. return -ENODEV;
  268. if (nvdev->nvsp_version == NVSP_PROTOCOL_VERSION_2)
  269. limit = NETVSC_MTU;
  270. if (mtu < 68 || mtu > limit)
  271. return -EINVAL;
  272. nvdev->start_remove = true;
  273. cancel_delayed_work_sync(&ndevctx->dwork);
  274. cancel_work_sync(&ndevctx->work);
  275. netif_tx_disable(ndev);
  276. rndis_filter_device_remove(hdev);
  277. ndev->mtu = mtu;
  278. ndevctx->device_ctx = hdev;
  279. hv_set_drvdata(hdev, ndev);
  280. device_info.ring_size = ring_size;
  281. rndis_filter_device_add(hdev, &device_info);
  282. netif_wake_queue(ndev);
  283. return 0;
  284. }
  285. static const struct ethtool_ops ethtool_ops = {
  286. .get_drvinfo = netvsc_get_drvinfo,
  287. .get_link = ethtool_op_get_link,
  288. };
  289. static const struct net_device_ops device_ops = {
  290. .ndo_open = netvsc_open,
  291. .ndo_stop = netvsc_close,
  292. .ndo_start_xmit = netvsc_start_xmit,
  293. .ndo_set_rx_mode = netvsc_set_multicast_list,
  294. .ndo_change_mtu = netvsc_change_mtu,
  295. .ndo_validate_addr = eth_validate_addr,
  296. .ndo_set_mac_address = eth_mac_addr,
  297. };
  298. /*
  299. * Send GARP packet to network peers after migrations.
  300. * After Quick Migration, the network is not immediately operational in the
  301. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  302. * another netif_notify_peers() into a delayed work, otherwise GARP packet
  303. * will not be sent after quick migration, and cause network disconnection.
  304. */
  305. static void netvsc_send_garp(struct work_struct *w)
  306. {
  307. struct net_device_context *ndev_ctx;
  308. struct net_device *net;
  309. struct netvsc_device *net_device;
  310. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  311. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  312. net = net_device->ndev;
  313. netif_notify_peers(net);
  314. }
  315. static int netvsc_probe(struct hv_device *dev,
  316. const struct hv_vmbus_device_id *dev_id)
  317. {
  318. struct net_device *net = NULL;
  319. struct net_device_context *net_device_ctx;
  320. struct netvsc_device_info device_info;
  321. int ret;
  322. net = alloc_etherdev(sizeof(struct net_device_context));
  323. if (!net)
  324. return -ENOMEM;
  325. /* Set initial state */
  326. netif_carrier_off(net);
  327. net_device_ctx = netdev_priv(net);
  328. net_device_ctx->device_ctx = dev;
  329. hv_set_drvdata(dev, net);
  330. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_send_garp);
  331. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  332. net->netdev_ops = &device_ops;
  333. /* TODO: Add GSO and Checksum offload */
  334. net->hw_features = NETIF_F_SG;
  335. net->features = NETIF_F_SG | NETIF_F_HW_VLAN_TX;
  336. SET_ETHTOOL_OPS(net, &ethtool_ops);
  337. SET_NETDEV_DEV(net, &dev->device);
  338. ret = register_netdev(net);
  339. if (ret != 0) {
  340. pr_err("Unable to register netdev.\n");
  341. free_netdev(net);
  342. goto out;
  343. }
  344. /* Notify the netvsc driver of the new device */
  345. device_info.ring_size = ring_size;
  346. ret = rndis_filter_device_add(dev, &device_info);
  347. if (ret != 0) {
  348. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  349. unregister_netdev(net);
  350. free_netdev(net);
  351. hv_set_drvdata(dev, NULL);
  352. return ret;
  353. }
  354. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  355. netif_carrier_on(net);
  356. out:
  357. return ret;
  358. }
  359. static int netvsc_remove(struct hv_device *dev)
  360. {
  361. struct net_device *net;
  362. struct net_device_context *ndev_ctx;
  363. struct netvsc_device *net_device;
  364. net_device = hv_get_drvdata(dev);
  365. net = net_device->ndev;
  366. if (net == NULL) {
  367. dev_err(&dev->device, "No net device to remove\n");
  368. return 0;
  369. }
  370. net_device->start_remove = true;
  371. ndev_ctx = netdev_priv(net);
  372. cancel_delayed_work_sync(&ndev_ctx->dwork);
  373. cancel_work_sync(&ndev_ctx->work);
  374. /* Stop outbound asap */
  375. netif_tx_disable(net);
  376. unregister_netdev(net);
  377. /*
  378. * Call to the vsc driver to let it know that the device is being
  379. * removed
  380. */
  381. rndis_filter_device_remove(dev);
  382. free_netdev(net);
  383. return 0;
  384. }
  385. static const struct hv_vmbus_device_id id_table[] = {
  386. /* Network guid */
  387. { VMBUS_DEVICE(0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
  388. 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E) },
  389. { },
  390. };
  391. MODULE_DEVICE_TABLE(vmbus, id_table);
  392. /* The one and only one */
  393. static struct hv_driver netvsc_drv = {
  394. .name = KBUILD_MODNAME,
  395. .id_table = id_table,
  396. .probe = netvsc_probe,
  397. .remove = netvsc_remove,
  398. };
  399. static void __exit netvsc_drv_exit(void)
  400. {
  401. vmbus_driver_unregister(&netvsc_drv);
  402. }
  403. static int __init netvsc_drv_init(void)
  404. {
  405. return vmbus_driver_register(&netvsc_drv);
  406. }
  407. MODULE_LICENSE("GPL");
  408. MODULE_VERSION(HV_DRV_VERSION);
  409. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  410. module_init(netvsc_drv_init);
  411. module_exit(netvsc_drv_exit);