PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/hyperv/netvsc_drv.c

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