/net/rds/transport.c

http://github.com/mirrors/linux · C · 161 lines · 97 code · 26 blank · 38 comment · 21 complexity · 37c1efa6d944a65cdbd9cd49d15da582 MD5 · raw file

  1. /*
  2. * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/in.h>
  36. #include <linux/ipv6.h>
  37. #include "rds.h"
  38. #include "loop.h"
  39. static struct rds_transport *transports[RDS_TRANS_COUNT];
  40. static DECLARE_RWSEM(rds_trans_sem);
  41. void rds_trans_register(struct rds_transport *trans)
  42. {
  43. BUG_ON(strlen(trans->t_name) + 1 > TRANSNAMSIZ);
  44. down_write(&rds_trans_sem);
  45. if (transports[trans->t_type])
  46. printk(KERN_ERR "RDS Transport type %d already registered\n",
  47. trans->t_type);
  48. else {
  49. transports[trans->t_type] = trans;
  50. printk(KERN_INFO "Registered RDS/%s transport\n", trans->t_name);
  51. }
  52. up_write(&rds_trans_sem);
  53. }
  54. EXPORT_SYMBOL_GPL(rds_trans_register);
  55. void rds_trans_unregister(struct rds_transport *trans)
  56. {
  57. down_write(&rds_trans_sem);
  58. transports[trans->t_type] = NULL;
  59. printk(KERN_INFO "Unregistered RDS/%s transport\n", trans->t_name);
  60. up_write(&rds_trans_sem);
  61. }
  62. EXPORT_SYMBOL_GPL(rds_trans_unregister);
  63. void rds_trans_put(struct rds_transport *trans)
  64. {
  65. if (trans)
  66. module_put(trans->t_owner);
  67. }
  68. struct rds_transport *rds_trans_get_preferred(struct net *net,
  69. const struct in6_addr *addr,
  70. __u32 scope_id)
  71. {
  72. struct rds_transport *ret = NULL;
  73. struct rds_transport *trans;
  74. unsigned int i;
  75. if (ipv6_addr_v4mapped(addr)) {
  76. if (*(u_int8_t *)&addr->s6_addr32[3] == IN_LOOPBACKNET)
  77. return &rds_loop_transport;
  78. } else if (ipv6_addr_loopback(addr)) {
  79. return &rds_loop_transport;
  80. }
  81. down_read(&rds_trans_sem);
  82. for (i = 0; i < RDS_TRANS_COUNT; i++) {
  83. trans = transports[i];
  84. if (trans && (trans->laddr_check(net, addr, scope_id) == 0) &&
  85. (!trans->t_owner || try_module_get(trans->t_owner))) {
  86. ret = trans;
  87. break;
  88. }
  89. }
  90. up_read(&rds_trans_sem);
  91. return ret;
  92. }
  93. struct rds_transport *rds_trans_get(int t_type)
  94. {
  95. struct rds_transport *ret = NULL;
  96. struct rds_transport *trans;
  97. unsigned int i;
  98. down_read(&rds_trans_sem);
  99. for (i = 0; i < RDS_TRANS_COUNT; i++) {
  100. trans = transports[i];
  101. if (trans && trans->t_type == t_type &&
  102. (!trans->t_owner || try_module_get(trans->t_owner))) {
  103. ret = trans;
  104. break;
  105. }
  106. }
  107. up_read(&rds_trans_sem);
  108. return ret;
  109. }
  110. /*
  111. * This returns the number of stats entries in the snapshot and only
  112. * copies them using the iter if there is enough space for them. The
  113. * caller passes in the global stats so that we can size and copy while
  114. * holding the lock.
  115. */
  116. unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter,
  117. unsigned int avail)
  118. {
  119. struct rds_transport *trans;
  120. unsigned int total = 0;
  121. unsigned int part;
  122. int i;
  123. rds_info_iter_unmap(iter);
  124. down_read(&rds_trans_sem);
  125. for (i = 0; i < RDS_TRANS_COUNT; i++) {
  126. trans = transports[i];
  127. if (!trans || !trans->stats_info_copy)
  128. continue;
  129. part = trans->stats_info_copy(iter, avail);
  130. avail -= min(avail, part);
  131. total += part;
  132. }
  133. up_read(&rds_trans_sem);
  134. return total;
  135. }