Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 1 | /* |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved. |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 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> |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 36 | #include <linux/ipv6.h> |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 37 | |
| 38 | #include "rds.h" |
| 39 | #include "loop.h" |
| 40 | |
Rao Shoaib | 4c342f77 | 2020-06-25 13:46:00 -0700 | [diff] [blame] | 41 | static char * const rds_trans_modules[] = { |
| 42 | [RDS_TRANS_IB] = "rds_rdma", |
| 43 | [RDS_TRANS_GAP] = NULL, |
| 44 | [RDS_TRANS_TCP] = "rds_tcp", |
| 45 | }; |
| 46 | |
Andy Grover | 335776b | 2009-08-21 12:28:34 +0000 | [diff] [blame] | 47 | static struct rds_transport *transports[RDS_TRANS_COUNT]; |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 48 | static DECLARE_RWSEM(rds_trans_sem); |
| 49 | |
Zhu Yanjun | a8d63a5 | 2017-03-03 00:44:26 -0500 | [diff] [blame] | 50 | void rds_trans_register(struct rds_transport *trans) |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 51 | { |
| 52 | BUG_ON(strlen(trans->t_name) + 1 > TRANSNAMSIZ); |
| 53 | |
| 54 | down_write(&rds_trans_sem); |
| 55 | |
Andy Grover | 335776b | 2009-08-21 12:28:34 +0000 | [diff] [blame] | 56 | if (transports[trans->t_type]) |
| 57 | printk(KERN_ERR "RDS Transport type %d already registered\n", |
| 58 | trans->t_type); |
| 59 | else { |
| 60 | transports[trans->t_type] = trans; |
| 61 | printk(KERN_INFO "Registered RDS/%s transport\n", trans->t_name); |
| 62 | } |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 63 | |
| 64 | up_write(&rds_trans_sem); |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 65 | } |
Andy Grover | 616b757 | 2009-08-21 12:28:32 +0000 | [diff] [blame] | 66 | EXPORT_SYMBOL_GPL(rds_trans_register); |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 67 | |
| 68 | void rds_trans_unregister(struct rds_transport *trans) |
| 69 | { |
| 70 | down_write(&rds_trans_sem); |
| 71 | |
Andy Grover | 335776b | 2009-08-21 12:28:34 +0000 | [diff] [blame] | 72 | transports[trans->t_type] = NULL; |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 73 | printk(KERN_INFO "Unregistered RDS/%s transport\n", trans->t_name); |
| 74 | |
| 75 | up_write(&rds_trans_sem); |
| 76 | } |
Andy Grover | 616b757 | 2009-08-21 12:28:32 +0000 | [diff] [blame] | 77 | EXPORT_SYMBOL_GPL(rds_trans_unregister); |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 78 | |
Zach Brown | 5adb5bc | 2010-07-23 10:32:31 -0700 | [diff] [blame] | 79 | void rds_trans_put(struct rds_transport *trans) |
| 80 | { |
Markus Elfring | f6e1c91 | 2015-07-02 17:58:21 +0200 | [diff] [blame] | 81 | if (trans) |
Zach Brown | 5adb5bc | 2010-07-23 10:32:31 -0700 | [diff] [blame] | 82 | module_put(trans->t_owner); |
| 83 | } |
| 84 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 85 | struct rds_transport *rds_trans_get_preferred(struct net *net, |
| 86 | const struct in6_addr *addr, |
| 87 | __u32 scope_id) |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 88 | { |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 89 | struct rds_transport *ret = NULL; |
Zach Brown | 5adb5bc | 2010-07-23 10:32:31 -0700 | [diff] [blame] | 90 | struct rds_transport *trans; |
| 91 | unsigned int i; |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 92 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 93 | if (ipv6_addr_v4mapped(addr)) { |
| 94 | if (*(u_int8_t *)&addr->s6_addr32[3] == IN_LOOPBACKNET) |
| 95 | return &rds_loop_transport; |
| 96 | } else if (ipv6_addr_loopback(addr)) { |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 97 | return &rds_loop_transport; |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 98 | } |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 99 | |
| 100 | down_read(&rds_trans_sem); |
Zach Brown | 5adb5bc | 2010-07-23 10:32:31 -0700 | [diff] [blame] | 101 | for (i = 0; i < RDS_TRANS_COUNT; i++) { |
| 102 | trans = transports[i]; |
| 103 | |
Ka-Cheong Poon | eee2fa6 | 2018-07-23 20:51:21 -0700 | [diff] [blame] | 104 | if (trans && (trans->laddr_check(net, addr, scope_id) == 0) && |
Zach Brown | 5adb5bc | 2010-07-23 10:32:31 -0700 | [diff] [blame] | 105 | (!trans->t_owner || try_module_get(trans->t_owner))) { |
| 106 | ret = trans; |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | } |
| 110 | up_read(&rds_trans_sem); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Sowmini Varadhan | d97dac5 | 2015-05-29 17:28:08 -0400 | [diff] [blame] | 115 | struct rds_transport *rds_trans_get(int t_type) |
| 116 | { |
| 117 | struct rds_transport *ret = NULL; |
| 118 | struct rds_transport *trans; |
Sowmini Varadhan | d97dac5 | 2015-05-29 17:28:08 -0400 | [diff] [blame] | 119 | |
| 120 | down_read(&rds_trans_sem); |
Rao Shoaib | 4c342f77 | 2020-06-25 13:46:00 -0700 | [diff] [blame] | 121 | trans = transports[t_type]; |
| 122 | if (!trans) { |
| 123 | up_read(&rds_trans_sem); |
| 124 | if (rds_trans_modules[t_type]) |
| 125 | request_module(rds_trans_modules[t_type]); |
| 126 | down_read(&rds_trans_sem); |
| 127 | trans = transports[t_type]; |
Sowmini Varadhan | d97dac5 | 2015-05-29 17:28:08 -0400 | [diff] [blame] | 128 | } |
Rao Shoaib | 4c342f77 | 2020-06-25 13:46:00 -0700 | [diff] [blame] | 129 | if (trans && trans->t_type == t_type && |
| 130 | (!trans->t_owner || try_module_get(trans->t_owner))) |
| 131 | ret = trans; |
| 132 | |
Sowmini Varadhan | d97dac5 | 2015-05-29 17:28:08 -0400 | [diff] [blame] | 133 | up_read(&rds_trans_sem); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 138 | /* |
| 139 | * This returns the number of stats entries in the snapshot and only |
| 140 | * copies them using the iter if there is enough space for them. The |
| 141 | * caller passes in the global stats so that we can size and copy while |
| 142 | * holding the lock. |
| 143 | */ |
| 144 | unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter, |
| 145 | unsigned int avail) |
| 146 | |
| 147 | { |
| 148 | struct rds_transport *trans; |
| 149 | unsigned int total = 0; |
| 150 | unsigned int part; |
Andy Grover | 335776b | 2009-08-21 12:28:34 +0000 | [diff] [blame] | 151 | int i; |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 152 | |
| 153 | rds_info_iter_unmap(iter); |
| 154 | down_read(&rds_trans_sem); |
| 155 | |
Joshua Houghton | 5c3da57 | 2016-06-18 15:46:31 +0000 | [diff] [blame] | 156 | for (i = 0; i < RDS_TRANS_COUNT; i++) { |
Andy Grover | 335776b | 2009-08-21 12:28:34 +0000 | [diff] [blame] | 157 | trans = transports[i]; |
| 158 | if (!trans || !trans->stats_info_copy) |
Andy Grover | 0fbc78c | 2009-02-24 15:30:21 +0000 | [diff] [blame] | 159 | continue; |
| 160 | |
| 161 | part = trans->stats_info_copy(iter, avail); |
| 162 | avail -= min(avail, part); |
| 163 | total += part; |
| 164 | } |
| 165 | |
| 166 | up_read(&rds_trans_sem); |
| 167 | |
| 168 | return total; |
| 169 | } |