blob: f3afd1d60d3c7a7880993554c6375754fb4c6ff8 [file] [log] [blame]
Andy Grover0fbc78c2009-02-24 15:30:21 +00001/*
2 * Copyright (c) 2006 Oracle. 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
37#include "rds.h"
38#include "loop.h"
39
Andy Grover335776b2009-08-21 12:28:34 +000040static struct rds_transport *transports[RDS_TRANS_COUNT];
Andy Grover0fbc78c2009-02-24 15:30:21 +000041static DECLARE_RWSEM(rds_trans_sem);
42
43int rds_trans_register(struct rds_transport *trans)
44{
45 BUG_ON(strlen(trans->t_name) + 1 > TRANSNAMSIZ);
46
47 down_write(&rds_trans_sem);
48
Andy Grover335776b2009-08-21 12:28:34 +000049 if (transports[trans->t_type])
50 printk(KERN_ERR "RDS Transport type %d already registered\n",
51 trans->t_type);
52 else {
53 transports[trans->t_type] = trans;
54 printk(KERN_INFO "Registered RDS/%s transport\n", trans->t_name);
55 }
Andy Grover0fbc78c2009-02-24 15:30:21 +000056
57 up_write(&rds_trans_sem);
58
59 return 0;
60}
Andy Grover616b7572009-08-21 12:28:32 +000061EXPORT_SYMBOL_GPL(rds_trans_register);
Andy Grover0fbc78c2009-02-24 15:30:21 +000062
63void rds_trans_unregister(struct rds_transport *trans)
64{
65 down_write(&rds_trans_sem);
66
Andy Grover335776b2009-08-21 12:28:34 +000067 transports[trans->t_type] = NULL;
Andy Grover0fbc78c2009-02-24 15:30:21 +000068 printk(KERN_INFO "Unregistered RDS/%s transport\n", trans->t_name);
69
70 up_write(&rds_trans_sem);
71}
Andy Grover616b7572009-08-21 12:28:32 +000072EXPORT_SYMBOL_GPL(rds_trans_unregister);
Andy Grover0fbc78c2009-02-24 15:30:21 +000073
Zach Brown5adb5bc2010-07-23 10:32:31 -070074void rds_trans_put(struct rds_transport *trans)
75{
Markus Elfringf6e1c912015-07-02 17:58:21 +020076 if (trans)
Zach Brown5adb5bc2010-07-23 10:32:31 -070077 module_put(trans->t_owner);
78}
79
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -040080struct rds_transport *rds_trans_get_preferred(struct net *net, __be32 addr)
Andy Grover0fbc78c2009-02-24 15:30:21 +000081{
Andy Grover0fbc78c2009-02-24 15:30:21 +000082 struct rds_transport *ret = NULL;
Zach Brown5adb5bc2010-07-23 10:32:31 -070083 struct rds_transport *trans;
84 unsigned int i;
Andy Grover0fbc78c2009-02-24 15:30:21 +000085
86 if (IN_LOOPBACK(ntohl(addr)))
87 return &rds_loop_transport;
88
89 down_read(&rds_trans_sem);
Zach Brown5adb5bc2010-07-23 10:32:31 -070090 for (i = 0; i < RDS_TRANS_COUNT; i++) {
91 trans = transports[i];
92
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -040093 if (trans && (trans->laddr_check(net, addr) == 0) &&
Zach Brown5adb5bc2010-07-23 10:32:31 -070094 (!trans->t_owner || try_module_get(trans->t_owner))) {
95 ret = trans;
Andy Grover0fbc78c2009-02-24 15:30:21 +000096 break;
97 }
98 }
99 up_read(&rds_trans_sem);
100
101 return ret;
102}
103
Sowmini Varadhand97dac52015-05-29 17:28:08 -0400104struct rds_transport *rds_trans_get(int t_type)
105{
106 struct rds_transport *ret = NULL;
107 struct rds_transport *trans;
108 unsigned int i;
109
110 down_read(&rds_trans_sem);
111 for (i = 0; i < RDS_TRANS_COUNT; i++) {
112 trans = transports[i];
113
114 if (trans && trans->t_type == t_type &&
115 (!trans->t_owner || try_module_get(trans->t_owner))) {
116 ret = trans;
117 break;
118 }
119 }
120 up_read(&rds_trans_sem);
121
122 return ret;
123}
124
Andy Grover0fbc78c2009-02-24 15:30:21 +0000125/*
126 * This returns the number of stats entries in the snapshot and only
127 * copies them using the iter if there is enough space for them. The
128 * caller passes in the global stats so that we can size and copy while
129 * holding the lock.
130 */
131unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter,
132 unsigned int avail)
133
134{
135 struct rds_transport *trans;
136 unsigned int total = 0;
137 unsigned int part;
Andy Grover335776b2009-08-21 12:28:34 +0000138 int i;
Andy Grover0fbc78c2009-02-24 15:30:21 +0000139
140 rds_info_iter_unmap(iter);
141 down_read(&rds_trans_sem);
142
Andy Grover335776b2009-08-21 12:28:34 +0000143 for (i = 0; i < RDS_TRANS_COUNT; i++)
144 {
145 trans = transports[i];
146 if (!trans || !trans->stats_info_copy)
Andy Grover0fbc78c2009-02-24 15:30:21 +0000147 continue;
148
149 part = trans->stats_info_copy(iter, avail);
150 avail -= min(avail, part);
151 total += part;
152 }
153
154 up_read(&rds_trans_sem);
155
156 return total;
157}
158