blob: 46f709a4b577813c9c5b784b3a766445d64ef539 [file] [log] [blame]
Andy Grover0fbc78c2009-02-24 15:30:21 +00001/*
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -07002 * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
Andy Grover0fbc78c2009-02-24 15:30:21 +00003 *
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>
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070036#include <linux/ipv6.h>
Andy Grover0fbc78c2009-02-24 15:30:21 +000037
38#include "rds.h"
39#include "loop.h"
40
Andy Grover335776b2009-08-21 12:28:34 +000041static struct rds_transport *transports[RDS_TRANS_COUNT];
Andy Grover0fbc78c2009-02-24 15:30:21 +000042static DECLARE_RWSEM(rds_trans_sem);
43
Zhu Yanjuna8d63a52017-03-03 00:44:26 -050044void rds_trans_register(struct rds_transport *trans)
Andy Grover0fbc78c2009-02-24 15:30:21 +000045{
46 BUG_ON(strlen(trans->t_name) + 1 > TRANSNAMSIZ);
47
48 down_write(&rds_trans_sem);
49
Andy Grover335776b2009-08-21 12:28:34 +000050 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 }
Andy Grover0fbc78c2009-02-24 15:30:21 +000057
58 up_write(&rds_trans_sem);
Andy Grover0fbc78c2009-02-24 15:30:21 +000059}
Andy Grover616b7572009-08-21 12:28:32 +000060EXPORT_SYMBOL_GPL(rds_trans_register);
Andy Grover0fbc78c2009-02-24 15:30:21 +000061
62void rds_trans_unregister(struct rds_transport *trans)
63{
64 down_write(&rds_trans_sem);
65
Andy Grover335776b2009-08-21 12:28:34 +000066 transports[trans->t_type] = NULL;
Andy Grover0fbc78c2009-02-24 15:30:21 +000067 printk(KERN_INFO "Unregistered RDS/%s transport\n", trans->t_name);
68
69 up_write(&rds_trans_sem);
70}
Andy Grover616b7572009-08-21 12:28:32 +000071EXPORT_SYMBOL_GPL(rds_trans_unregister);
Andy Grover0fbc78c2009-02-24 15:30:21 +000072
Zach Brown5adb5bc2010-07-23 10:32:31 -070073void rds_trans_put(struct rds_transport *trans)
74{
Markus Elfringf6e1c912015-07-02 17:58:21 +020075 if (trans)
Zach Brown5adb5bc2010-07-23 10:32:31 -070076 module_put(trans->t_owner);
77}
78
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070079struct rds_transport *rds_trans_get_preferred(struct net *net,
80 const struct in6_addr *addr,
81 __u32 scope_id)
Andy Grover0fbc78c2009-02-24 15:30:21 +000082{
Andy Grover0fbc78c2009-02-24 15:30:21 +000083 struct rds_transport *ret = NULL;
Zach Brown5adb5bc2010-07-23 10:32:31 -070084 struct rds_transport *trans;
85 unsigned int i;
Andy Grover0fbc78c2009-02-24 15:30:21 +000086
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070087 if (ipv6_addr_v4mapped(addr)) {
88 if (*(u_int8_t *)&addr->s6_addr32[3] == IN_LOOPBACKNET)
89 return &rds_loop_transport;
90 } else if (ipv6_addr_loopback(addr)) {
Andy Grover0fbc78c2009-02-24 15:30:21 +000091 return &rds_loop_transport;
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070092 }
Andy Grover0fbc78c2009-02-24 15:30:21 +000093
94 down_read(&rds_trans_sem);
Zach Brown5adb5bc2010-07-23 10:32:31 -070095 for (i = 0; i < RDS_TRANS_COUNT; i++) {
96 trans = transports[i];
97
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070098 if (trans && (trans->laddr_check(net, addr, scope_id) == 0) &&
Zach Brown5adb5bc2010-07-23 10:32:31 -070099 (!trans->t_owner || try_module_get(trans->t_owner))) {
100 ret = trans;
Andy Grover0fbc78c2009-02-24 15:30:21 +0000101 break;
102 }
103 }
104 up_read(&rds_trans_sem);
105
106 return ret;
107}
108
Sowmini Varadhand97dac52015-05-29 17:28:08 -0400109struct rds_transport *rds_trans_get(int t_type)
110{
111 struct rds_transport *ret = NULL;
112 struct rds_transport *trans;
113 unsigned int i;
114
115 down_read(&rds_trans_sem);
116 for (i = 0; i < RDS_TRANS_COUNT; i++) {
117 trans = transports[i];
118
119 if (trans && trans->t_type == t_type &&
120 (!trans->t_owner || try_module_get(trans->t_owner))) {
121 ret = trans;
122 break;
123 }
124 }
125 up_read(&rds_trans_sem);
126
127 return ret;
128}
129
Andy Grover0fbc78c2009-02-24 15:30:21 +0000130/*
131 * This returns the number of stats entries in the snapshot and only
132 * copies them using the iter if there is enough space for them. The
133 * caller passes in the global stats so that we can size and copy while
134 * holding the lock.
135 */
136unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter,
137 unsigned int avail)
138
139{
140 struct rds_transport *trans;
141 unsigned int total = 0;
142 unsigned int part;
Andy Grover335776b2009-08-21 12:28:34 +0000143 int i;
Andy Grover0fbc78c2009-02-24 15:30:21 +0000144
145 rds_info_iter_unmap(iter);
146 down_read(&rds_trans_sem);
147
Joshua Houghton5c3da572016-06-18 15:46:31 +0000148 for (i = 0; i < RDS_TRANS_COUNT; i++) {
Andy Grover335776b2009-08-21 12:28:34 +0000149 trans = transports[i];
150 if (!trans || !trans->stats_info_copy)
Andy Grover0fbc78c2009-02-24 15:30:21 +0000151 continue;
152
153 part = trans->stats_info_copy(iter, avail);
154 avail -= min(avail, part);
155 total += part;
156 }
157
158 up_read(&rds_trans_sem);
159
160 return total;
161}