netvsc_drv.c 14 KB

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