blob: 7e4e9dfdbc0b66863b191b54d375b477e822b4fa [file] [log] [blame]
Andy Grover00e0f342009-02-24 15:30:23 +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/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Andy Grover00e0f342009-02-24 15:30:23 +000036#include <net/inet_hashtables.h>
37
38#include "rds.h"
39#include "loop.h"
Andy Grover00e0f342009-02-24 15:30:23 +000040
41#define RDS_CONNECTION_HASH_BITS 12
42#define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
43#define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
44
45/* converting this to RCU is a chore for another day.. */
46static DEFINE_SPINLOCK(rds_conn_lock);
47static unsigned long rds_conn_count;
48static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
49static struct kmem_cache *rds_conn_slab;
50
51static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
52{
53 /* Pass NULL, don't need struct net for hash */
54 unsigned long hash = inet_ehashfn(NULL,
55 be32_to_cpu(laddr), 0,
56 be32_to_cpu(faddr), 0);
57 return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
58}
59
60#define rds_conn_info_set(var, test, suffix) do { \
61 if (test) \
62 var |= RDS_INFO_CONNECTION_FLAG_##suffix; \
63} while (0)
64
Andy Grover00e0f342009-02-24 15:30:23 +000065static struct rds_connection *rds_conn_lookup(struct hlist_head *head,
66 __be32 laddr, __be32 faddr,
67 struct rds_transport *trans)
68{
69 struct rds_connection *conn, *ret = NULL;
70 struct hlist_node *pos;
71
72 hlist_for_each_entry(conn, pos, head, c_hash_node) {
73 if (conn->c_faddr == faddr && conn->c_laddr == laddr &&
74 conn->c_trans == trans) {
75 ret = conn;
76 break;
77 }
78 }
79 rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret,
80 &laddr, &faddr);
81 return ret;
82}
83
84/*
85 * This is called by transports as they're bringing down a connection.
86 * It clears partial message state so that the transport can start sending
87 * and receiving over this connection again in the future. It is up to
88 * the transport to have serialized this call with its send and recv.
89 */
90void rds_conn_reset(struct rds_connection *conn)
91{
92 rdsdebug("connection %pI4 to %pI4 reset\n",
93 &conn->c_laddr, &conn->c_faddr);
94
95 rds_stats_inc(s_conn_reset);
96 rds_send_reset(conn);
97 conn->c_flags = 0;
98
99 /* Do not clear next_rx_seq here, else we cannot distinguish
100 * retransmitted packets from new packets, and will hand all
101 * of them to the application. That is not consistent with the
102 * reliability guarantees of RDS. */
103}
104
105/*
106 * There is only every one 'conn' for a given pair of addresses in the
107 * system at a time. They contain messages to be retransmitted and so
108 * span the lifetime of the actual underlying transport connections.
109 *
110 * For now they are not garbage collected once they're created. They
111 * are torn down as the module is removed, if ever.
112 */
113static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
114 struct rds_transport *trans, gfp_t gfp,
115 int is_outgoing)
116{
Andy Grovercb244052009-07-17 13:13:36 +0000117 struct rds_connection *conn, *parent = NULL;
Andy Grover00e0f342009-02-24 15:30:23 +0000118 struct hlist_head *head = rds_conn_bucket(laddr, faddr);
119 unsigned long flags;
120 int ret;
121
122 spin_lock_irqsave(&rds_conn_lock, flags);
123 conn = rds_conn_lookup(head, laddr, faddr, trans);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800124 if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport &&
125 !is_outgoing) {
Andy Grover00e0f342009-02-24 15:30:23 +0000126 /* This is a looped back IB connection, and we're
127 * called by the code handling the incoming connect.
128 * We need a second connection object into which we
129 * can stick the other QP. */
130 parent = conn;
131 conn = parent->c_passive;
132 }
133 spin_unlock_irqrestore(&rds_conn_lock, flags);
134 if (conn)
135 goto out;
136
Wei Yongjun05a178e2009-04-09 14:09:44 +0000137 conn = kmem_cache_zalloc(rds_conn_slab, gfp);
Andy Grover8690bfa2010-01-12 11:56:44 -0800138 if (!conn) {
Andy Grover00e0f342009-02-24 15:30:23 +0000139 conn = ERR_PTR(-ENOMEM);
140 goto out;
141 }
142
Andy Grover00e0f342009-02-24 15:30:23 +0000143 INIT_HLIST_NODE(&conn->c_hash_node);
Andy Grover00e0f342009-02-24 15:30:23 +0000144 conn->c_laddr = laddr;
145 conn->c_faddr = faddr;
146 spin_lock_init(&conn->c_lock);
147 conn->c_next_tx_seq = 1;
148
Andy Grover049ee3f2010-03-23 17:39:07 -0700149 spin_lock_init(&conn->c_send_lock);
Chris Mason9e29db02010-04-15 16:38:14 -0400150 atomic_set(&conn->c_send_generation, 1);
Andy Grover00e0f342009-02-24 15:30:23 +0000151 INIT_LIST_HEAD(&conn->c_send_queue);
152 INIT_LIST_HEAD(&conn->c_retrans);
153
154 ret = rds_cong_get_maps(conn);
155 if (ret) {
156 kmem_cache_free(rds_conn_slab, conn);
157 conn = ERR_PTR(ret);
158 goto out;
159 }
160
161 /*
162 * This is where a connection becomes loopback. If *any* RDS sockets
163 * can bind to the destination address then we'd rather the messages
164 * flow through loopback rather than either transport.
165 */
166 if (rds_trans_get_preferred(faddr)) {
167 conn->c_loopback = 1;
168 if (is_outgoing && trans->t_prefer_loopback) {
169 /* "outgoing" connection - and the transport
170 * says it wants the connection handled by the
171 * loopback transport. This is what TCP does.
172 */
173 trans = &rds_loop_transport;
174 }
175 }
176
177 conn->c_trans = trans;
178
179 ret = trans->conn_alloc(conn, gfp);
180 if (ret) {
181 kmem_cache_free(rds_conn_slab, conn);
182 conn = ERR_PTR(ret);
183 goto out;
184 }
185
186 atomic_set(&conn->c_state, RDS_CONN_DOWN);
187 conn->c_reconnect_jiffies = 0;
188 INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker);
189 INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker);
190 INIT_DELAYED_WORK(&conn->c_conn_w, rds_connect_worker);
191 INIT_WORK(&conn->c_down_w, rds_shutdown_worker);
192 mutex_init(&conn->c_cm_lock);
193 conn->c_flags = 0;
194
195 rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n",
196 conn, &laddr, &faddr,
197 trans->t_name ? trans->t_name : "[unknown]",
198 is_outgoing ? "(outgoing)" : "");
199
Andy Grovercb244052009-07-17 13:13:36 +0000200 /*
201 * Since we ran without holding the conn lock, someone could
202 * have created the same conn (either normal or passive) in the
203 * interim. We check while holding the lock. If we won, we complete
204 * init and return our conn. If we lost, we rollback and return the
205 * other one.
206 */
Andy Grover00e0f342009-02-24 15:30:23 +0000207 spin_lock_irqsave(&rds_conn_lock, flags);
Andy Grovercb244052009-07-17 13:13:36 +0000208 if (parent) {
209 /* Creating passive conn */
210 if (parent->c_passive) {
211 trans->conn_free(conn->c_transport_data);
212 kmem_cache_free(rds_conn_slab, conn);
213 conn = parent->c_passive;
214 } else {
Andy Grover00e0f342009-02-24 15:30:23 +0000215 parent->c_passive = conn;
Andy Grovercb244052009-07-17 13:13:36 +0000216 rds_cong_add_conn(conn);
217 rds_conn_count++;
218 }
Andy Grover00e0f342009-02-24 15:30:23 +0000219 } else {
Andy Grovercb244052009-07-17 13:13:36 +0000220 /* Creating normal conn */
221 struct rds_connection *found;
Andy Grover00e0f342009-02-24 15:30:23 +0000222
Andy Grovercb244052009-07-17 13:13:36 +0000223 found = rds_conn_lookup(head, laddr, faddr, trans);
224 if (found) {
225 trans->conn_free(conn->c_transport_data);
226 kmem_cache_free(rds_conn_slab, conn);
227 conn = found;
228 } else {
229 hlist_add_head(&conn->c_hash_node, head);
230 rds_cong_add_conn(conn);
231 rds_conn_count++;
232 }
233 }
Andy Grover00e0f342009-02-24 15:30:23 +0000234 spin_unlock_irqrestore(&rds_conn_lock, flags);
235
236out:
237 return conn;
238}
239
240struct rds_connection *rds_conn_create(__be32 laddr, __be32 faddr,
241 struct rds_transport *trans, gfp_t gfp)
242{
243 return __rds_conn_create(laddr, faddr, trans, gfp, 0);
244}
Andy Grover616b7572009-08-21 12:28:32 +0000245EXPORT_SYMBOL_GPL(rds_conn_create);
Andy Grover00e0f342009-02-24 15:30:23 +0000246
247struct rds_connection *rds_conn_create_outgoing(__be32 laddr, __be32 faddr,
248 struct rds_transport *trans, gfp_t gfp)
249{
250 return __rds_conn_create(laddr, faddr, trans, gfp, 1);
251}
Andy Grover616b7572009-08-21 12:28:32 +0000252EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
Andy Grover00e0f342009-02-24 15:30:23 +0000253
Andy Grover2dc39352010-06-11 13:49:13 -0700254void rds_conn_shutdown(struct rds_connection *conn)
255{
256 /* shut it down unless it's down already */
257 if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_DOWN)) {
258 /*
259 * Quiesce the connection mgmt handlers before we start tearing
260 * things down. We don't hold the mutex for the entire
261 * duration of the shutdown operation, else we may be
262 * deadlocking with the CM handler. Instead, the CM event
263 * handler is supposed to check for state DISCONNECTING
264 */
265 mutex_lock(&conn->c_cm_lock);
266 if (!rds_conn_transition(conn, RDS_CONN_UP, RDS_CONN_DISCONNECTING)
267 && !rds_conn_transition(conn, RDS_CONN_ERROR, RDS_CONN_DISCONNECTING)) {
268 rds_conn_error(conn, "shutdown called in state %d\n",
269 atomic_read(&conn->c_state));
270 mutex_unlock(&conn->c_cm_lock);
271 return;
272 }
273 mutex_unlock(&conn->c_cm_lock);
274
Andy Grover049ee3f2010-03-23 17:39:07 -0700275 /* verify everybody's out of rds_send_xmit() */
276 spin_lock_irq(&conn->c_send_lock);
277 spin_unlock_irq(&conn->c_send_lock);
278
Andy Grover2dc39352010-06-11 13:49:13 -0700279 conn->c_trans->conn_shutdown(conn);
280 rds_conn_reset(conn);
Andy Grover2dc39352010-06-11 13:49:13 -0700281
282 if (!rds_conn_transition(conn, RDS_CONN_DISCONNECTING, RDS_CONN_DOWN)) {
283 /* This can happen - eg when we're in the middle of tearing
284 * down the connection, and someone unloads the rds module.
285 * Quite reproduceable with loopback connections.
286 * Mostly harmless.
287 */
288 rds_conn_error(conn,
289 "%s: failed to transition to state DOWN, "
290 "current state is %d\n",
291 __func__,
292 atomic_read(&conn->c_state));
293 return;
294 }
295 }
296
297 /* Then reconnect if it's still live.
298 * The passive side of an IB loopback connection is never added
299 * to the conn hash, so we never trigger a reconnect on this
300 * conn - the reconnect is always triggered by the active peer. */
301 cancel_delayed_work_sync(&conn->c_conn_w);
302 if (!hlist_unhashed(&conn->c_hash_node))
303 rds_queue_reconnect(conn);
304}
305
306/*
307 * Stop and free a connection.
308 */
Andy Grover00e0f342009-02-24 15:30:23 +0000309void rds_conn_destroy(struct rds_connection *conn)
310{
311 struct rds_message *rm, *rtmp;
312
313 rdsdebug("freeing conn %p for %pI4 -> "
314 "%pI4\n", conn, &conn->c_laddr,
315 &conn->c_faddr);
316
317 hlist_del_init(&conn->c_hash_node);
318
319 /* wait for the rds thread to shut it down */
320 atomic_set(&conn->c_state, RDS_CONN_ERROR);
321 cancel_delayed_work(&conn->c_conn_w);
322 queue_work(rds_wq, &conn->c_down_w);
323 flush_workqueue(rds_wq);
324
325 /* tear down queued messages */
326 list_for_each_entry_safe(rm, rtmp,
327 &conn->c_send_queue,
328 m_conn_item) {
329 list_del_init(&rm->m_conn_item);
330 BUG_ON(!list_empty(&rm->m_sock_item));
331 rds_message_put(rm);
332 }
333 if (conn->c_xmit_rm)
334 rds_message_put(conn->c_xmit_rm);
335
336 conn->c_trans->conn_free(conn->c_transport_data);
337
338 /*
339 * The congestion maps aren't freed up here. They're
340 * freed by rds_cong_exit() after all the connections
341 * have been freed.
342 */
343 rds_cong_remove_conn(conn);
344
345 BUG_ON(!list_empty(&conn->c_retrans));
346 kmem_cache_free(rds_conn_slab, conn);
347
348 rds_conn_count--;
349}
Andy Grover616b7572009-08-21 12:28:32 +0000350EXPORT_SYMBOL_GPL(rds_conn_destroy);
Andy Grover00e0f342009-02-24 15:30:23 +0000351
352static void rds_conn_message_info(struct socket *sock, unsigned int len,
353 struct rds_info_iterator *iter,
354 struct rds_info_lengths *lens,
355 int want_send)
356{
357 struct hlist_head *head;
358 struct hlist_node *pos;
359 struct list_head *list;
360 struct rds_connection *conn;
361 struct rds_message *rm;
362 unsigned long flags;
363 unsigned int total = 0;
364 size_t i;
365
366 len /= sizeof(struct rds_info_message);
367
368 spin_lock_irqsave(&rds_conn_lock, flags);
369
370 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
371 i++, head++) {
372 hlist_for_each_entry(conn, pos, head, c_hash_node) {
373 if (want_send)
374 list = &conn->c_send_queue;
375 else
376 list = &conn->c_retrans;
377
378 spin_lock(&conn->c_lock);
379
380 /* XXX too lazy to maintain counts.. */
381 list_for_each_entry(rm, list, m_conn_item) {
382 total++;
383 if (total <= len)
384 rds_inc_info_copy(&rm->m_inc, iter,
385 conn->c_laddr,
386 conn->c_faddr, 0);
387 }
388
389 spin_unlock(&conn->c_lock);
390 }
391 }
392
393 spin_unlock_irqrestore(&rds_conn_lock, flags);
394
395 lens->nr = total;
396 lens->each = sizeof(struct rds_info_message);
397}
398
399static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
400 struct rds_info_iterator *iter,
401 struct rds_info_lengths *lens)
402{
403 rds_conn_message_info(sock, len, iter, lens, 1);
404}
405
406static void rds_conn_message_info_retrans(struct socket *sock,
407 unsigned int len,
408 struct rds_info_iterator *iter,
409 struct rds_info_lengths *lens)
410{
411 rds_conn_message_info(sock, len, iter, lens, 0);
412}
413
414void rds_for_each_conn_info(struct socket *sock, unsigned int len,
415 struct rds_info_iterator *iter,
416 struct rds_info_lengths *lens,
417 int (*visitor)(struct rds_connection *, void *),
418 size_t item_len)
419{
420 uint64_t buffer[(item_len + 7) / 8];
421 struct hlist_head *head;
422 struct hlist_node *pos;
423 struct hlist_node *tmp;
424 struct rds_connection *conn;
425 unsigned long flags;
426 size_t i;
427
428 spin_lock_irqsave(&rds_conn_lock, flags);
429
430 lens->nr = 0;
431 lens->each = item_len;
432
433 for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
434 i++, head++) {
435 hlist_for_each_entry_safe(conn, pos, tmp, head, c_hash_node) {
436
437 /* XXX no c_lock usage.. */
438 if (!visitor(conn, buffer))
439 continue;
440
441 /* We copy as much as we can fit in the buffer,
442 * but we count all items so that the caller
443 * can resize the buffer. */
444 if (len >= item_len) {
445 rds_info_copy(iter, buffer, item_len);
446 len -= item_len;
447 }
448 lens->nr++;
449 }
450 }
451
452 spin_unlock_irqrestore(&rds_conn_lock, flags);
453}
Andy Grover616b7572009-08-21 12:28:32 +0000454EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
Andy Grover00e0f342009-02-24 15:30:23 +0000455
456static int rds_conn_info_visitor(struct rds_connection *conn,
457 void *buffer)
458{
459 struct rds_info_connection *cinfo = buffer;
460
461 cinfo->next_tx_seq = conn->c_next_tx_seq;
462 cinfo->next_rx_seq = conn->c_next_rx_seq;
463 cinfo->laddr = conn->c_laddr;
464 cinfo->faddr = conn->c_faddr;
465 strncpy(cinfo->transport, conn->c_trans->t_name,
466 sizeof(cinfo->transport));
467 cinfo->flags = 0;
468
469 rds_conn_info_set(cinfo->flags,
Andy Grover049ee3f2010-03-23 17:39:07 -0700470 spin_is_locked(&conn->c_send_lock), SENDING);
Andy Grover00e0f342009-02-24 15:30:23 +0000471 /* XXX Future: return the state rather than these funky bits */
472 rds_conn_info_set(cinfo->flags,
473 atomic_read(&conn->c_state) == RDS_CONN_CONNECTING,
474 CONNECTING);
475 rds_conn_info_set(cinfo->flags,
476 atomic_read(&conn->c_state) == RDS_CONN_UP,
477 CONNECTED);
478 return 1;
479}
480
481static void rds_conn_info(struct socket *sock, unsigned int len,
482 struct rds_info_iterator *iter,
483 struct rds_info_lengths *lens)
484{
485 rds_for_each_conn_info(sock, len, iter, lens,
486 rds_conn_info_visitor,
487 sizeof(struct rds_info_connection));
488}
489
490int __init rds_conn_init(void)
491{
492 rds_conn_slab = kmem_cache_create("rds_connection",
493 sizeof(struct rds_connection),
494 0, 0, NULL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800495 if (!rds_conn_slab)
Andy Grover00e0f342009-02-24 15:30:23 +0000496 return -ENOMEM;
497
498 rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
499 rds_info_register_func(RDS_INFO_SEND_MESSAGES,
500 rds_conn_message_info_send);
501 rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
502 rds_conn_message_info_retrans);
503
504 return 0;
505}
506
507void rds_conn_exit(void)
508{
509 rds_loop_exit();
510
511 WARN_ON(!hlist_empty(rds_conn_hash));
512
513 kmem_cache_destroy(rds_conn_slab);
514
515 rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
516 rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
517 rds_conn_message_info_send);
518 rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
519 rds_conn_message_info_retrans);
520}
521
522/*
523 * Force a disconnect
524 */
525void rds_conn_drop(struct rds_connection *conn)
526{
527 atomic_set(&conn->c_state, RDS_CONN_ERROR);
528 queue_work(rds_wq, &conn->c_down_w);
529}
Andy Grover616b7572009-08-21 12:28:32 +0000530EXPORT_SYMBOL_GPL(rds_conn_drop);
Andy Grover00e0f342009-02-24 15:30:23 +0000531
532/*
533 * An error occurred on the connection
534 */
535void
536__rds_conn_error(struct rds_connection *conn, const char *fmt, ...)
537{
538 va_list ap;
539
540 va_start(ap, fmt);
541 vprintk(fmt, ap);
542 va_end(ap);
543
544 rds_conn_drop(conn);
545}