netvsc_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. }
  179. return ret ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  180. }
  181. /*
  182. * netvsc_linkstatus_callback - Link up/down notification
  183. */
  184. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  185. unsigned int status)
  186. {
  187. struct net_device *net;
  188. struct net_device_context *ndev_ctx;
  189. struct netvsc_device *net_device;
  190. net_device = hv_get_drvdata(device_obj);
  191. net = net_device->ndev;
  192. if (!net) {
  193. netdev_err(net, "got link status but net device "
  194. "not initialized yet\n");
  195. return;
  196. }
  197. if (status == 1) {
  198. netif_carrier_on(net);
  199. netif_wake_queue(net);
  200. ndev_ctx = netdev_priv(net);
  201. schedule_delayed_work(&ndev_ctx->dwork, 0);
  202. schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
  203. } else {
  204. netif_carrier_off(net);
  205. netif_tx_disable(net);
  206. }
  207. }
  208. /*
  209. * netvsc_recv_callback - Callback when we receive a packet from the
  210. * "wire" on the specified device.
  211. */
  212. int netvsc_recv_callback(struct hv_device *device_obj,
  213. struct hv_netvsc_packet *packet)
  214. {
  215. struct net_device *net;
  216. struct sk_buff *skb;
  217. net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
  218. if (!net) {
  219. netdev_err(net, "got receive callback but net device"
  220. " not initialized yet\n");
  221. return 0;
  222. }
  223. /* Allocate a skb - TODO direct I/O to pages? */
  224. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  225. if (unlikely(!skb)) {
  226. ++net->stats.rx_dropped;
  227. return 0;
  228. }
  229. /*
  230. * Copy to skb. This copy is needed here since the memory pointed by
  231. * hv_netvsc_packet cannot be deallocated
  232. */
  233. memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
  234. packet->total_data_buflen);
  235. skb->protocol = eth_type_trans(skb, net);
  236. skb->ip_summed = CHECKSUM_NONE;
  237. skb->vlan_tci = packet->vlan_tci;
  238. net->stats.rx_packets++;
  239. net->stats.rx_bytes += packet->total_data_buflen;
  240. /*
  241. * Pass the skb back up. Network stack will deallocate the skb when it
  242. * is done.
  243. * TODO - use NAPI?
  244. */
  245. netif_rx(skb);
  246. return 0;
  247. }
  248. static void netvsc_get_drvinfo(struct net_device *net,
  249. struct ethtool_drvinfo *info)
  250. {
  251. strcpy(info->driver, KBUILD_MODNAME);
  252. strcpy(info->version, HV_DRV_VERSION);
  253. strcpy(info->fw_version, "N/A");
  254. }
  255. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  256. {
  257. struct net_device_context *ndevctx = netdev_priv(ndev);
  258. struct hv_device *hdev = ndevctx->device_ctx;
  259. struct netvsc_device *nvdev = hv_get_drvdata(hdev);
  260. struct netvsc_device_info device_info;
  261. int limit = ETH_DATA_LEN;
  262. if (nvdev == NULL || nvdev->destroy)
  263. return -ENODEV;
  264. if (nvdev->nvsp_version == NVSP_PROTOCOL_VERSION_2)
  265. limit = NETVSC_MTU;
  266. if (mtu < 68 || mtu > limit)
  267. return -EINVAL;
  268. nvdev->start_remove = true;
  269. cancel_work_sync(&ndevctx->work);
  270. netif_tx_disable(ndev);
  271. rndis_filter_device_remove(hdev);
  272. ndev->mtu = mtu;
  273. ndevctx->device_ctx = hdev;
  274. hv_set_drvdata(hdev, ndev);
  275. device_info.ring_size = ring_size;
  276. rndis_filter_device_add(hdev, &device_info);
  277. netif_wake_queue(ndev);
  278. return 0;
  279. }
  280. static const struct ethtool_ops ethtool_ops = {
  281. .get_drvinfo = netvsc_get_drvinfo,
  282. .get_link = ethtool_op_get_link,
  283. };
  284. static const struct net_device_ops device_ops = {
  285. .ndo_open = netvsc_open,
  286. .ndo_stop = netvsc_close,
  287. .ndo_start_xmit = netvsc_start_xmit,
  288. .ndo_set_rx_mode = netvsc_set_multicast_list,
  289. .ndo_change_mtu = netvsc_change_mtu,
  290. .ndo_validate_addr = eth_validate_addr,
  291. .ndo_set_mac_address = eth_mac_addr,
  292. };
  293. /*
  294. * Send GARP packet to network peers after migrations.
  295. * After Quick Migration, the network is not immediately operational in the
  296. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  297. * another netif_notify_peers() into a delayed work, otherwise GARP packet
  298. * will not be sent after quick migration, and cause network disconnection.
  299. */
  300. static void netvsc_send_garp(struct work_struct *w)
  301. {
  302. struct net_device_context *ndev_ctx;
  303. struct net_device *net;
  304. struct netvsc_device *net_device;
  305. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  306. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  307. net = net_device->ndev;
  308. netif_notify_peers(net);
  309. }
  310. static int netvsc_probe(struct hv_device *dev,
  311. const struct hv_vmbus_device_id *dev_id)
  312. {
  313. struct net_device *net = NULL;
  314. struct net_device_context *net_device_ctx;
  315. struct netvsc_device_info device_info;
  316. int ret;
  317. net = alloc_etherdev(sizeof(struct net_device_context));
  318. if (!net)
  319. return -ENOMEM;
  320. /* Set initial state */
  321. netif_carrier_off(net);
  322. net_device_ctx = netdev_priv(net);
  323. net_device_ctx->device_ctx = dev;
  324. hv_set_drvdata(dev, net);
  325. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_send_garp);
  326. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  327. net->netdev_ops = &device_ops;
  328. /* TODO: Add GSO and Checksum offload */
  329. net->hw_features = NETIF_F_SG;
  330. net->features = NETIF_F_SG | NETIF_F_HW_VLAN_TX;
  331. SET_ETHTOOL_OPS(net, &ethtool_ops);
  332. SET_NETDEV_DEV(net, &dev->device);
  333. ret = register_netdev(net);
  334. if (ret != 0) {
  335. pr_err("Unable to register netdev.\n");
  336. free_netdev(net);
  337. goto out;
  338. }
  339. /* Notify the netvsc driver of the new device */
  340. device_info.ring_size = ring_size;
  341. ret = rndis_filter_device_add(dev, &device_info);
  342. if (ret != 0) {
  343. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  344. unregister_netdev(net);
  345. free_netdev(net);
  346. hv_set_drvdata(dev, NULL);
  347. return ret;
  348. }
  349. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  350. netif_carrier_on(net);
  351. out:
  352. return ret;
  353. }
  354. static int netvsc_remove(struct hv_device *dev)
  355. {
  356. struct net_device *net;
  357. struct net_device_context *ndev_ctx;
  358. struct netvsc_device *net_device;
  359. net_device = hv_get_drvdata(dev);
  360. net = net_device->ndev;
  361. if (net == NULL) {
  362. dev_err(&dev->device, "No net device to remove\n");
  363. return 0;
  364. }
  365. net_device->start_remove = true;
  366. ndev_ctx = netdev_priv(net);
  367. cancel_delayed_work_sync(&ndev_ctx->dwork);
  368. cancel_work_sync(&ndev_ctx->work);
  369. /* Stop outbound asap */
  370. netif_tx_disable(net);
  371. unregister_netdev(net);
  372. /*
  373. * Call to the vsc driver to let it know that the device is being
  374. * removed
  375. */
  376. rndis_filter_device_remove(dev);
  377. free_netdev(net);
  378. return 0;
  379. }
  380. static const struct hv_vmbus_device_id id_table[] = {
  381. /* Network guid */
  382. { VMBUS_DEVICE(0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
  383. 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E) },
  384. { },
  385. };
  386. MODULE_DEVICE_TABLE(vmbus, id_table);
  387. /* The one and only one */
  388. static struct hv_driver netvsc_drv = {
  389. .name = KBUILD_MODNAME,
  390. .id_table = id_table,
  391. .probe = netvsc_probe,
  392. .remove = netvsc_remove,
  393. };
  394. static void __exit netvsc_drv_exit(void)
  395. {
  396. vmbus_driver_unregister(&netvsc_drv);
  397. }
  398. static int __init netvsc_drv_init(void)
  399. {
  400. return vmbus_driver_register(&netvsc_drv);
  401. }
  402. MODULE_LICENSE("GPL");
  403. MODULE_VERSION(HV_DRV_VERSION);
  404. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  405. module_init(netvsc_drv_init);
  406. module_exit(netvsc_drv_exit);