netvsc_drv.c 13 KB

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