rxe_sysfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include "rxe.h"
  34. #include "rxe_net.h"
  35. /* Copy argument and remove trailing CR. Return the new length. */
  36. static int sanitize_arg(const char *val, char *intf, int intf_len)
  37. {
  38. int len;
  39. if (!val)
  40. return 0;
  41. /* Remove newline. */
  42. for (len = 0; len < intf_len - 1 && val[len] && val[len] != '\n'; len++)
  43. intf[len] = val[len];
  44. intf[len] = 0;
  45. if (len == 0 || (val[len] != 0 && val[len] != '\n'))
  46. return 0;
  47. return len;
  48. }
  49. static void rxe_set_port_state(struct net_device *ndev)
  50. {
  51. struct rxe_dev *rxe = net_to_rxe(ndev);
  52. bool is_up = netif_running(ndev) && netif_carrier_ok(ndev);
  53. if (!rxe)
  54. goto out;
  55. if (is_up)
  56. rxe_port_up(rxe);
  57. else
  58. rxe_port_down(rxe); /* down for unknown state */
  59. out:
  60. return;
  61. }
  62. static int rxe_param_set_add(const char *val, const struct kernel_param *kp)
  63. {
  64. int len;
  65. int err = 0;
  66. char intf[32];
  67. struct net_device *ndev = NULL;
  68. struct rxe_dev *rxe;
  69. len = sanitize_arg(val, intf, sizeof(intf));
  70. if (!len) {
  71. pr_err("add: invalid interface name\n");
  72. err = -EINVAL;
  73. goto err;
  74. }
  75. ndev = dev_get_by_name(&init_net, intf);
  76. if (!ndev) {
  77. pr_err("interface %s not found\n", intf);
  78. err = -EINVAL;
  79. goto err;
  80. }
  81. if (net_to_rxe(ndev)) {
  82. pr_err("already configured on %s\n", intf);
  83. err = -EINVAL;
  84. goto err;
  85. }
  86. rxe = rxe_net_add(ndev);
  87. if (!rxe) {
  88. pr_err("failed to add %s\n", intf);
  89. err = -EINVAL;
  90. goto err;
  91. }
  92. rxe_set_port_state(ndev);
  93. pr_info("added %s to %s\n", rxe->ib_dev.name, intf);
  94. err:
  95. if (ndev)
  96. dev_put(ndev);
  97. return err;
  98. }
  99. static int rxe_param_set_remove(const char *val, const struct kernel_param *kp)
  100. {
  101. int len;
  102. char intf[32];
  103. struct rxe_dev *rxe;
  104. len = sanitize_arg(val, intf, sizeof(intf));
  105. if (!len) {
  106. pr_err("add: invalid interface name\n");
  107. return -EINVAL;
  108. }
  109. if (strncmp("all", intf, len) == 0) {
  110. pr_info("rxe_sys: remove all");
  111. rxe_remove_all();
  112. return 0;
  113. }
  114. rxe = get_rxe_by_name(intf);
  115. if (!rxe) {
  116. pr_err("not configured on %s\n", intf);
  117. return -EINVAL;
  118. }
  119. list_del(&rxe->list);
  120. rxe_remove(rxe);
  121. return 0;
  122. }
  123. static const struct kernel_param_ops rxe_add_ops = {
  124. .set = rxe_param_set_add,
  125. };
  126. static const struct kernel_param_ops rxe_remove_ops = {
  127. .set = rxe_param_set_remove,
  128. };
  129. module_param_cb(add, &rxe_add_ops, NULL, 0200);
  130. MODULE_PARM_DESC(add, "Create RXE device over network interface");
  131. module_param_cb(remove, &rxe_remove_ops, NULL, 0200);
  132. MODULE_PARM_DESC(remove, "Remove RXE device over network interface");