Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 1 | /* |
| 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 35 | #include <linux/in.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 36 | #include <linux/module.h> |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 37 | #include <net/tcp.h> |
| 38 | |
| 39 | #include "rds.h" |
| 40 | #include "tcp.h" |
| 41 | |
| 42 | /* only for info exporting */ |
| 43 | static DEFINE_SPINLOCK(rds_tcp_tc_list_lock); |
| 44 | static LIST_HEAD(rds_tcp_tc_list); |
stephen hemminger | ff51bf8 | 2010-10-19 08:08:33 +0000 | [diff] [blame] | 45 | static unsigned int rds_tcp_tc_count; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 46 | |
| 47 | /* Track rds_tcp_connection structs so they can be cleaned up */ |
| 48 | static DEFINE_SPINLOCK(rds_tcp_conn_lock); |
| 49 | static LIST_HEAD(rds_tcp_conn_list); |
| 50 | |
| 51 | static struct kmem_cache *rds_tcp_conn_slab; |
| 52 | |
| 53 | #define RDS_TCP_DEFAULT_BUFSIZE (128 * 1024) |
| 54 | |
| 55 | /* doing it this way avoids calling tcp_sk() */ |
| 56 | void rds_tcp_nonagle(struct socket *sock) |
| 57 | { |
| 58 | mm_segment_t oldfs = get_fs(); |
| 59 | int val = 1; |
| 60 | |
| 61 | set_fs(KERNEL_DS); |
| 62 | sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY, (char __user *)&val, |
| 63 | sizeof(val)); |
| 64 | set_fs(oldfs); |
| 65 | } |
| 66 | |
| 67 | void rds_tcp_tune(struct socket *sock) |
| 68 | { |
| 69 | struct sock *sk = sock->sk; |
| 70 | |
| 71 | rds_tcp_nonagle(sock); |
| 72 | |
| 73 | /* |
| 74 | * We're trying to saturate gigabit with the default, |
| 75 | * see svc_sock_setbufsize(). |
| 76 | */ |
| 77 | lock_sock(sk); |
| 78 | sk->sk_sndbuf = RDS_TCP_DEFAULT_BUFSIZE; |
| 79 | sk->sk_rcvbuf = RDS_TCP_DEFAULT_BUFSIZE; |
| 80 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK; |
| 81 | release_sock(sk); |
| 82 | } |
| 83 | |
| 84 | u32 rds_tcp_snd_nxt(struct rds_tcp_connection *tc) |
| 85 | { |
| 86 | return tcp_sk(tc->t_sock->sk)->snd_nxt; |
| 87 | } |
| 88 | |
| 89 | u32 rds_tcp_snd_una(struct rds_tcp_connection *tc) |
| 90 | { |
| 91 | return tcp_sk(tc->t_sock->sk)->snd_una; |
| 92 | } |
| 93 | |
| 94 | void rds_tcp_restore_callbacks(struct socket *sock, |
| 95 | struct rds_tcp_connection *tc) |
| 96 | { |
| 97 | rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc); |
| 98 | write_lock_bh(&sock->sk->sk_callback_lock); |
| 99 | |
| 100 | /* done under the callback_lock to serialize with write_space */ |
| 101 | spin_lock(&rds_tcp_tc_list_lock); |
| 102 | list_del_init(&tc->t_list_item); |
| 103 | rds_tcp_tc_count--; |
| 104 | spin_unlock(&rds_tcp_tc_list_lock); |
| 105 | |
| 106 | tc->t_sock = NULL; |
| 107 | |
| 108 | sock->sk->sk_write_space = tc->t_orig_write_space; |
| 109 | sock->sk->sk_data_ready = tc->t_orig_data_ready; |
| 110 | sock->sk->sk_state_change = tc->t_orig_state_change; |
| 111 | sock->sk->sk_user_data = NULL; |
| 112 | |
| 113 | write_unlock_bh(&sock->sk->sk_callback_lock); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * This is the only path that sets tc->t_sock. Send and receive trust that |
| 118 | * it is set. The RDS_CONN_CONNECTED bit protects those paths from being |
| 119 | * called while it isn't set. |
| 120 | */ |
| 121 | void rds_tcp_set_callbacks(struct socket *sock, struct rds_connection *conn) |
| 122 | { |
| 123 | struct rds_tcp_connection *tc = conn->c_transport_data; |
| 124 | |
| 125 | rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc); |
| 126 | write_lock_bh(&sock->sk->sk_callback_lock); |
| 127 | |
| 128 | /* done under the callback_lock to serialize with write_space */ |
| 129 | spin_lock(&rds_tcp_tc_list_lock); |
| 130 | list_add_tail(&tc->t_list_item, &rds_tcp_tc_list); |
| 131 | rds_tcp_tc_count++; |
| 132 | spin_unlock(&rds_tcp_tc_list_lock); |
| 133 | |
| 134 | /* accepted sockets need our listen data ready undone */ |
| 135 | if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready) |
| 136 | sock->sk->sk_data_ready = sock->sk->sk_user_data; |
| 137 | |
| 138 | tc->t_sock = sock; |
| 139 | tc->conn = conn; |
| 140 | tc->t_orig_data_ready = sock->sk->sk_data_ready; |
| 141 | tc->t_orig_write_space = sock->sk->sk_write_space; |
| 142 | tc->t_orig_state_change = sock->sk->sk_state_change; |
| 143 | |
| 144 | sock->sk->sk_user_data = conn; |
| 145 | sock->sk->sk_data_ready = rds_tcp_data_ready; |
| 146 | sock->sk->sk_write_space = rds_tcp_write_space; |
| 147 | sock->sk->sk_state_change = rds_tcp_state_change; |
| 148 | |
| 149 | write_unlock_bh(&sock->sk->sk_callback_lock); |
| 150 | } |
| 151 | |
| 152 | static void rds_tcp_tc_info(struct socket *sock, unsigned int len, |
| 153 | struct rds_info_iterator *iter, |
| 154 | struct rds_info_lengths *lens) |
| 155 | { |
| 156 | struct rds_info_tcp_socket tsinfo; |
| 157 | struct rds_tcp_connection *tc; |
| 158 | unsigned long flags; |
| 159 | struct sockaddr_in sin; |
| 160 | int sinlen; |
| 161 | |
| 162 | spin_lock_irqsave(&rds_tcp_tc_list_lock, flags); |
| 163 | |
| 164 | if (len / sizeof(tsinfo) < rds_tcp_tc_count) |
| 165 | goto out; |
| 166 | |
| 167 | list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) { |
| 168 | |
| 169 | sock->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 0); |
| 170 | tsinfo.local_addr = sin.sin_addr.s_addr; |
| 171 | tsinfo.local_port = sin.sin_port; |
| 172 | sock->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 1); |
| 173 | tsinfo.peer_addr = sin.sin_addr.s_addr; |
| 174 | tsinfo.peer_port = sin.sin_port; |
| 175 | |
| 176 | tsinfo.hdr_rem = tc->t_tinc_hdr_rem; |
| 177 | tsinfo.data_rem = tc->t_tinc_data_rem; |
| 178 | tsinfo.last_sent_nxt = tc->t_last_sent_nxt; |
| 179 | tsinfo.last_expected_una = tc->t_last_expected_una; |
| 180 | tsinfo.last_seen_una = tc->t_last_seen_una; |
| 181 | |
| 182 | rds_info_copy(iter, &tsinfo, sizeof(tsinfo)); |
| 183 | } |
| 184 | |
| 185 | out: |
| 186 | lens->nr = rds_tcp_tc_count; |
| 187 | lens->each = sizeof(tsinfo); |
| 188 | |
| 189 | spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags); |
| 190 | } |
| 191 | |
Sowmini Varadhan | d5a8ac2 | 2015-08-05 01:43:25 -0400 | [diff] [blame^] | 192 | static int rds_tcp_laddr_check(struct net *net, __be32 addr) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 193 | { |
Sowmini Varadhan | d5a8ac2 | 2015-08-05 01:43:25 -0400 | [diff] [blame^] | 194 | if (inet_addr_type(net, addr) == RTN_LOCAL) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | return -EADDRNOTAVAIL; |
| 197 | } |
| 198 | |
| 199 | static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp) |
| 200 | { |
| 201 | struct rds_tcp_connection *tc; |
| 202 | |
| 203 | tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 204 | if (!tc) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 205 | return -ENOMEM; |
| 206 | |
| 207 | tc->t_sock = NULL; |
| 208 | tc->t_tinc = NULL; |
| 209 | tc->t_tinc_hdr_rem = sizeof(struct rds_header); |
| 210 | tc->t_tinc_data_rem = 0; |
| 211 | |
| 212 | conn->c_transport_data = tc; |
| 213 | |
| 214 | spin_lock_irq(&rds_tcp_conn_lock); |
| 215 | list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list); |
| 216 | spin_unlock_irq(&rds_tcp_conn_lock); |
| 217 | |
| 218 | rdsdebug("alloced tc %p\n", conn->c_transport_data); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void rds_tcp_conn_free(void *arg) |
| 223 | { |
| 224 | struct rds_tcp_connection *tc = arg; |
Pavel Emelyanov | 8200a59 | 2010-11-02 01:54:01 +0000 | [diff] [blame] | 225 | unsigned long flags; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 226 | rdsdebug("freeing tc %p\n", tc); |
Pavel Emelyanov | 8200a59 | 2010-11-02 01:54:01 +0000 | [diff] [blame] | 227 | |
| 228 | spin_lock_irqsave(&rds_tcp_conn_lock, flags); |
| 229 | list_del(&tc->t_tcp_node); |
| 230 | spin_unlock_irqrestore(&rds_tcp_conn_lock, flags); |
| 231 | |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 232 | kmem_cache_free(rds_tcp_conn_slab, tc); |
| 233 | } |
| 234 | |
| 235 | static void rds_tcp_destroy_conns(void) |
| 236 | { |
| 237 | struct rds_tcp_connection *tc, *_tc; |
| 238 | LIST_HEAD(tmp_list); |
| 239 | |
| 240 | /* avoid calling conn_destroy with irqs off */ |
| 241 | spin_lock_irq(&rds_tcp_conn_lock); |
| 242 | list_splice(&rds_tcp_conn_list, &tmp_list); |
| 243 | INIT_LIST_HEAD(&rds_tcp_conn_list); |
| 244 | spin_unlock_irq(&rds_tcp_conn_lock); |
| 245 | |
| 246 | list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) { |
| 247 | if (tc->conn->c_passive) |
| 248 | rds_conn_destroy(tc->conn->c_passive); |
| 249 | rds_conn_destroy(tc->conn); |
| 250 | } |
| 251 | } |
| 252 | |
stephen hemminger | ff51bf8 | 2010-10-19 08:08:33 +0000 | [diff] [blame] | 253 | static void rds_tcp_exit(void) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 254 | { |
| 255 | rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info); |
| 256 | rds_tcp_listen_stop(); |
| 257 | rds_tcp_destroy_conns(); |
| 258 | rds_trans_unregister(&rds_tcp_transport); |
| 259 | rds_tcp_recv_exit(); |
| 260 | kmem_cache_destroy(rds_tcp_conn_slab); |
| 261 | } |
| 262 | module_exit(rds_tcp_exit); |
| 263 | |
| 264 | struct rds_transport rds_tcp_transport = { |
| 265 | .laddr_check = rds_tcp_laddr_check, |
| 266 | .xmit_prepare = rds_tcp_xmit_prepare, |
| 267 | .xmit_complete = rds_tcp_xmit_complete, |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 268 | .xmit = rds_tcp_xmit, |
| 269 | .recv = rds_tcp_recv, |
| 270 | .conn_alloc = rds_tcp_conn_alloc, |
| 271 | .conn_free = rds_tcp_conn_free, |
| 272 | .conn_connect = rds_tcp_conn_connect, |
| 273 | .conn_shutdown = rds_tcp_conn_shutdown, |
| 274 | .inc_copy_to_user = rds_tcp_inc_copy_to_user, |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 275 | .inc_free = rds_tcp_inc_free, |
| 276 | .stats_info_copy = rds_tcp_stats_info_copy, |
| 277 | .exit = rds_tcp_exit, |
| 278 | .t_owner = THIS_MODULE, |
| 279 | .t_name = "tcp", |
Andy Grover | 335776bd | 2009-08-21 12:28:34 +0000 | [diff] [blame] | 280 | .t_type = RDS_TRANS_TCP, |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 281 | .t_prefer_loopback = 1, |
| 282 | }; |
| 283 | |
stephen hemminger | ff51bf8 | 2010-10-19 08:08:33 +0000 | [diff] [blame] | 284 | static int rds_tcp_init(void) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 285 | { |
| 286 | int ret; |
| 287 | |
| 288 | rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection", |
| 289 | sizeof(struct rds_tcp_connection), |
| 290 | 0, 0, NULL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 291 | if (!rds_tcp_conn_slab) { |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 292 | ret = -ENOMEM; |
| 293 | goto out; |
| 294 | } |
| 295 | |
| 296 | ret = rds_tcp_recv_init(); |
| 297 | if (ret) |
| 298 | goto out_slab; |
| 299 | |
| 300 | ret = rds_trans_register(&rds_tcp_transport); |
| 301 | if (ret) |
| 302 | goto out_recv; |
| 303 | |
| 304 | ret = rds_tcp_listen_init(); |
| 305 | if (ret) |
| 306 | goto out_register; |
| 307 | |
| 308 | rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info); |
| 309 | |
| 310 | goto out; |
| 311 | |
| 312 | out_register: |
| 313 | rds_trans_unregister(&rds_tcp_transport); |
| 314 | out_recv: |
| 315 | rds_tcp_recv_exit(); |
| 316 | out_slab: |
| 317 | kmem_cache_destroy(rds_tcp_conn_slab); |
| 318 | out: |
| 319 | return ret; |
| 320 | } |
| 321 | module_init(rds_tcp_init); |
| 322 | |
| 323 | MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>"); |
| 324 | MODULE_DESCRIPTION("RDS: TCP transport"); |
| 325 | MODULE_LICENSE("Dual BSD/GPL"); |
| 326 | |