transport.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 char * const rds_trans_modules[] = {
  40. [RDS_TRANS_IB] = "rds_rdma",
  41. [RDS_TRANS_GAP] = NULL,
  42. [RDS_TRANS_TCP] = "rds_tcp",
  43. };
  44. static struct rds_transport *transports[RDS_TRANS_COUNT];
  45. static DECLARE_RWSEM(rds_trans_sem);
  46. void rds_trans_register(struct rds_transport *trans)
  47. {
  48. BUG_ON(strlen(trans->t_name) + 1 > TRANSNAMSIZ);
  49. down_write(&rds_trans_sem);
  50. if (transports[trans->t_type])
  51. printk(KERN_ERR "RDS Transport type %d already registered\n",
  52. trans->t_type);
  53. else {
  54. transports[trans->t_type] = trans;
  55. printk(KERN_INFO "Registered RDS/%s transport\n", trans->t_name);
  56. }
  57. up_write(&rds_trans_sem);
  58. }
  59. EXPORT_SYMBOL_GPL(rds_trans_register);
  60. void rds_trans_unregister(struct rds_transport *trans)
  61. {
  62. down_write(&rds_trans_sem);
  63. transports[trans->t_type] = NULL;
  64. printk(KERN_INFO "Unregistered RDS/%s transport\n", trans->t_name);
  65. up_write(&rds_trans_sem);
  66. }
  67. EXPORT_SYMBOL_GPL(rds_trans_unregister);
  68. void rds_trans_put(struct rds_transport *trans)
  69. {
  70. if (trans)
  71. module_put(trans->t_owner);
  72. }
  73. struct rds_transport *rds_trans_get_preferred(struct net *net,
  74. const struct in6_addr *addr,
  75. __u32 scope_id)
  76. {
  77. struct rds_transport *ret = NULL;
  78. struct rds_transport *trans;
  79. unsigned int i;
  80. if (ipv6_addr_v4mapped(addr)) {
  81. if (*(u_int8_t *)&addr->s6_addr32[3] == IN_LOOPBACKNET)
  82. return &rds_loop_transport;
  83. } else if (ipv6_addr_loopback(addr)) {
  84. return &rds_loop_transport;
  85. }
  86. down_read(&rds_trans_sem);
  87. for (i = 0; i < RDS_TRANS_COUNT; i++) {
  88. trans = transports[i];
  89. if (trans && (trans->laddr_check(net, addr, scope_id) == 0) &&
  90. (!trans->t_owner || try_module_get(trans->t_owner))) {
  91. ret = trans;
  92. break;
  93. }
  94. }
  95. up_read(&rds_trans_sem);
  96. return ret;
  97. }
  98. struct rds_transport *rds_trans_get(int t_type)
  99. {
  100. struct rds_transport *ret = NULL;
  101. struct rds_transport *trans;
  102. down_read(&rds_trans_sem);
  103. trans = transports[t_type];
  104. if (!trans) {
  105. up_read(&rds_trans_sem);
  106. if (rds_trans_modules[t_type])
  107. request_module(rds_trans_modules[t_type]);
  108. down_read(&rds_trans_sem);
  109. trans = transports[t_type];
  110. }
  111. if (trans && trans->t_type == t_type &&
  112. (!trans->t_owner || try_module_get(trans->t_owner)))
  113. ret = trans;
  114. up_read(&rds_trans_sem);
  115. return ret;
  116. }
  117. /*
  118. * This returns the number of stats entries in the snapshot and only
  119. * copies them using the iter if there is enough space for them. The
  120. * caller passes in the global stats so that we can size and copy while
  121. * holding the lock.
  122. */
  123. unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter,
  124. unsigned int avail)
  125. {
  126. struct rds_transport *trans;
  127. unsigned int total = 0;
  128. unsigned int part;
  129. int i;
  130. rds_info_iter_unmap(iter);
  131. down_read(&rds_trans_sem);
  132. for (i = 0; i < RDS_TRANS_COUNT; i++) {
  133. trans = transports[i];
  134. if (!trans || !trans->stats_info_copy)
  135. continue;
  136. part = trans->stats_info_copy(iter, avail);
  137. avail -= min(avail, part);
  138. total += part;
  139. }
  140. up_read(&rds_trans_sem);
  141. return total;
  142. }