netvsc_drv.c 14 KB

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