netvsc_drv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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->version, HV_DRV_VERSION, sizeof(info->version));
  261. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  262. }
  263. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  264. {
  265. struct net_device_context *ndevctx = netdev_priv(ndev);
  266. struct hv_device *hdev = ndevctx->device_ctx;
  267. struct netvsc_device *nvdev = hv_get_drvdata(hdev);
  268. struct netvsc_device_info device_info;
  269. int limit = ETH_DATA_LEN;
  270. if (nvdev == NULL || nvdev->destroy)
  271. return -ENODEV;
  272. if (nvdev->nvsp_version == NVSP_PROTOCOL_VERSION_2)
  273. limit = NETVSC_MTU;
  274. if (mtu < 68 || mtu > limit)
  275. return -EINVAL;
  276. nvdev->start_remove = true;
  277. cancel_delayed_work_sync(&ndevctx->dwork);
  278. cancel_work_sync(&ndevctx->work);
  279. netif_tx_disable(ndev);
  280. rndis_filter_device_remove(hdev);
  281. ndev->mtu = mtu;
  282. ndevctx->device_ctx = hdev;
  283. hv_set_drvdata(hdev, ndev);
  284. device_info.ring_size = ring_size;
  285. rndis_filter_device_add(hdev, &device_info);
  286. netif_wake_queue(ndev);
  287. return 0;
  288. }
  289. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  290. {
  291. struct net_device_context *ndevctx = netdev_priv(ndev);
  292. struct hv_device *hdev = ndevctx->device_ctx;
  293. struct sockaddr *addr = p;
  294. char save_adr[ETH_ALEN];
  295. unsigned char save_aatype;
  296. int err;
  297. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  298. save_aatype = ndev->addr_assign_type;
  299. err = eth_mac_addr(ndev, p);
  300. if (err != 0)
  301. return err;
  302. err = rndis_filter_set_device_mac(hdev, addr->sa_data);
  303. if (err != 0) {
  304. /* roll back to saved MAC */
  305. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  306. ndev->addr_assign_type = save_aatype;
  307. }
  308. return err;
  309. }
  310. static const struct ethtool_ops ethtool_ops = {
  311. .get_drvinfo = netvsc_get_drvinfo,
  312. .get_link = ethtool_op_get_link,
  313. };
  314. static const struct net_device_ops device_ops = {
  315. .ndo_open = netvsc_open,
  316. .ndo_stop = netvsc_close,
  317. .ndo_start_xmit = netvsc_start_xmit,
  318. .ndo_set_rx_mode = netvsc_set_multicast_list,
  319. .ndo_change_mtu = netvsc_change_mtu,
  320. .ndo_validate_addr = eth_validate_addr,
  321. .ndo_set_mac_address = netvsc_set_mac_addr,
  322. };
  323. /*
  324. * Send GARP packet to network peers after migrations.
  325. * After Quick Migration, the network is not immediately operational in the
  326. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  327. * another netif_notify_peers() into a delayed work, otherwise GARP packet
  328. * will not be sent after quick migration, and cause network disconnection.
  329. */
  330. static void netvsc_send_garp(struct work_struct *w)
  331. {
  332. struct net_device_context *ndev_ctx;
  333. struct net_device *net;
  334. struct netvsc_device *net_device;
  335. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  336. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  337. net = net_device->ndev;
  338. netdev_notify_peers(net);
  339. }
  340. static int netvsc_probe(struct hv_device *dev,
  341. const struct hv_vmbus_device_id *dev_id)
  342. {
  343. struct net_device *net = NULL;
  344. struct net_device_context *net_device_ctx;
  345. struct netvsc_device_info device_info;
  346. int ret;
  347. net = alloc_etherdev(sizeof(struct net_device_context));
  348. if (!net)
  349. return -ENOMEM;
  350. /* Set initial state */
  351. netif_carrier_off(net);
  352. net_device_ctx = netdev_priv(net);
  353. net_device_ctx->device_ctx = dev;
  354. hv_set_drvdata(dev, net);
  355. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_send_garp);
  356. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  357. net->netdev_ops = &device_ops;
  358. /* TODO: Add GSO and Checksum offload */
  359. net->hw_features = 0;
  360. net->features = NETIF_F_HW_VLAN_CTAG_TX;
  361. SET_ETHTOOL_OPS(net, &ethtool_ops);
  362. SET_NETDEV_DEV(net, &dev->device);
  363. ret = register_netdev(net);
  364. if (ret != 0) {
  365. pr_err("Unable to register netdev.\n");
  366. free_netdev(net);
  367. goto out;
  368. }
  369. /* Notify the netvsc driver of the new device */
  370. device_info.ring_size = ring_size;
  371. ret = rndis_filter_device_add(dev, &device_info);
  372. if (ret != 0) {
  373. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  374. unregister_netdev(net);
  375. free_netdev(net);
  376. hv_set_drvdata(dev, NULL);
  377. return ret;
  378. }
  379. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  380. netif_carrier_on(net);
  381. out:
  382. return ret;
  383. }
  384. static int netvsc_remove(struct hv_device *dev)
  385. {
  386. struct net_device *net;
  387. struct net_device_context *ndev_ctx;
  388. struct netvsc_device *net_device;
  389. net_device = hv_get_drvdata(dev);
  390. net = net_device->ndev;
  391. if (net == NULL) {
  392. dev_err(&dev->device, "No net device to remove\n");
  393. return 0;
  394. }
  395. net_device->start_remove = true;
  396. ndev_ctx = netdev_priv(net);
  397. cancel_delayed_work_sync(&ndev_ctx->dwork);
  398. cancel_work_sync(&ndev_ctx->work);
  399. /* Stop outbound asap */
  400. netif_tx_disable(net);
  401. unregister_netdev(net);
  402. /*
  403. * Call to the vsc driver to let it know that the device is being
  404. * removed
  405. */
  406. rndis_filter_device_remove(dev);
  407. free_netdev(net);
  408. return 0;
  409. }
  410. static const struct hv_vmbus_device_id id_table[] = {
  411. /* Network guid */
  412. { HV_NIC_GUID, },
  413. { },
  414. };
  415. MODULE_DEVICE_TABLE(vmbus, id_table);
  416. /* The one and only one */
  417. static struct hv_driver netvsc_drv = {
  418. .name = KBUILD_MODNAME,
  419. .id_table = id_table,
  420. .probe = netvsc_probe,
  421. .remove = netvsc_remove,
  422. };
  423. static void __exit netvsc_drv_exit(void)
  424. {
  425. vmbus_driver_unregister(&netvsc_drv);
  426. }
  427. static int __init netvsc_drv_init(void)
  428. {
  429. if (ring_size < RING_SIZE_MIN) {
  430. ring_size = RING_SIZE_MIN;
  431. pr_info("Increased ring_size to %d (min allowed)\n",
  432. ring_size);
  433. }
  434. return vmbus_driver_register(&netvsc_drv);
  435. }
  436. MODULE_LICENSE("GPL");
  437. MODULE_VERSION(HV_DRV_VERSION);
  438. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  439. module_init(netvsc_drv_init);
  440. module_exit(netvsc_drv_exit);