Root/net/rds/connection.c

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>
34#include <linux/list.h>
35#include <linux/slab.h>
36#include <net/inet_hashtables.h>
37
38#include "rds.h"
39#include "loop.h"
40#include "rdma.h"
41
42#define RDS_CONNECTION_HASH_BITS 12
43#define RDS_CONNECTION_HASH_ENTRIES (1 << RDS_CONNECTION_HASH_BITS)
44#define RDS_CONNECTION_HASH_MASK (RDS_CONNECTION_HASH_ENTRIES - 1)
45
46/* converting this to RCU is a chore for another day.. */
47static DEFINE_SPINLOCK(rds_conn_lock);
48static unsigned long rds_conn_count;
49static struct hlist_head rds_conn_hash[RDS_CONNECTION_HASH_ENTRIES];
50static struct kmem_cache *rds_conn_slab;
51
52static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
53{
54    /* Pass NULL, don't need struct net for hash */
55    unsigned long hash = inet_ehashfn(NULL,
56                      be32_to_cpu(laddr), 0,
57                      be32_to_cpu(faddr), 0);
58    return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
59}
60
61#define rds_conn_info_set(var, test, suffix) do { \
62    if (test) \
63        var |= RDS_INFO_CONNECTION_FLAG_##suffix; \
64} while (0)
65
66static inline int rds_conn_is_sending(struct rds_connection *conn)
67{
68    int ret = 0;
69
70    if (!mutex_trylock(&conn->c_send_lock))
71        ret = 1;
72    else
73        mutex_unlock(&conn->c_send_lock);
74
75    return ret;
76}
77
78static struct rds_connection *rds_conn_lookup(struct hlist_head *head,
79                          __be32 laddr, __be32 faddr,
80                          struct rds_transport *trans)
81{
82    struct rds_connection *conn, *ret = NULL;
83    struct hlist_node *pos;
84
85    hlist_for_each_entry(conn, pos, head, c_hash_node) {
86        if (conn->c_faddr == faddr && conn->c_laddr == laddr &&
87                conn->c_trans == trans) {
88            ret = conn;
89            break;
90        }
91    }
92    rdsdebug("returning conn %p for %pI4 -> %pI4\n", ret,
93         &laddr, &faddr);
94    return ret;
95}
96
97/*
98 * This is called by transports as they're bringing down a connection.
99 * It clears partial message state so that the transport can start sending
100 * and receiving over this connection again in the future. It is up to
101 * the transport to have serialized this call with its send and recv.
102 */
103void rds_conn_reset(struct rds_connection *conn)
104{
105    rdsdebug("connection %pI4 to %pI4 reset\n",
106      &conn->c_laddr, &conn->c_faddr);
107
108    rds_stats_inc(s_conn_reset);
109    rds_send_reset(conn);
110    conn->c_flags = 0;
111
112    /* Do not clear next_rx_seq here, else we cannot distinguish
113     * retransmitted packets from new packets, and will hand all
114     * of them to the application. That is not consistent with the
115     * reliability guarantees of RDS. */
116}
117
118/*
119 * There is only every one 'conn' for a given pair of addresses in the
120 * system at a time. They contain messages to be retransmitted and so
121 * span the lifetime of the actual underlying transport connections.
122 *
123 * For now they are not garbage collected once they're created. They
124 * are torn down as the module is removed, if ever.
125 */
126static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
127                       struct rds_transport *trans, gfp_t gfp,
128                       int is_outgoing)
129{
130    struct rds_connection *conn, *parent = NULL;
131    struct hlist_head *head = rds_conn_bucket(laddr, faddr);
132    unsigned long flags;
133    int ret;
134
135    spin_lock_irqsave(&rds_conn_lock, flags);
136    conn = rds_conn_lookup(head, laddr, faddr, trans);
137    if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport &&
138        !is_outgoing) {
139        /* This is a looped back IB connection, and we're
140         * called by the code handling the incoming connect.
141         * We need a second connection object into which we
142         * can stick the other QP. */
143        parent = conn;
144        conn = parent->c_passive;
145    }
146    spin_unlock_irqrestore(&rds_conn_lock, flags);
147    if (conn)
148        goto out;
149
150    conn = kmem_cache_zalloc(rds_conn_slab, gfp);
151    if (conn == NULL) {
152        conn = ERR_PTR(-ENOMEM);
153        goto out;
154    }
155
156    INIT_HLIST_NODE(&conn->c_hash_node);
157    conn->c_laddr = laddr;
158    conn->c_faddr = faddr;
159    spin_lock_init(&conn->c_lock);
160    conn->c_next_tx_seq = 1;
161
162    mutex_init(&conn->c_send_lock);
163    INIT_LIST_HEAD(&conn->c_send_queue);
164    INIT_LIST_HEAD(&conn->c_retrans);
165
166    ret = rds_cong_get_maps(conn);
167    if (ret) {
168        kmem_cache_free(rds_conn_slab, conn);
169        conn = ERR_PTR(ret);
170        goto out;
171    }
172
173    /*
174     * This is where a connection becomes loopback. If *any* RDS sockets
175     * can bind to the destination address then we'd rather the messages
176     * flow through loopback rather than either transport.
177     */
178    if (rds_trans_get_preferred(faddr)) {
179        conn->c_loopback = 1;
180        if (is_outgoing && trans->t_prefer_loopback) {
181            /* "outgoing" connection - and the transport
182             * says it wants the connection handled by the
183             * loopback transport. This is what TCP does.
184             */
185            trans = &rds_loop_transport;
186        }
187    }
188
189    conn->c_trans = trans;
190
191    ret = trans->conn_alloc(conn, gfp);
192    if (ret) {
193        kmem_cache_free(rds_conn_slab, conn);
194        conn = ERR_PTR(ret);
195        goto out;
196    }
197
198    atomic_set(&conn->c_state, RDS_CONN_DOWN);
199    conn->c_reconnect_jiffies = 0;
200    INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker);
201    INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker);
202    INIT_DELAYED_WORK(&conn->c_conn_w, rds_connect_worker);
203    INIT_WORK(&conn->c_down_w, rds_shutdown_worker);
204    mutex_init(&conn->c_cm_lock);
205    conn->c_flags = 0;
206
207    rdsdebug("allocated conn %p for %pI4 -> %pI4 over %s %s\n",
208      conn, &laddr, &faddr,
209      trans->t_name ? trans->t_name : "[unknown]",
210      is_outgoing ? "(outgoing)" : "");
211
212    /*
213     * Since we ran without holding the conn lock, someone could
214     * have created the same conn (either normal or passive) in the
215     * interim. We check while holding the lock. If we won, we complete
216     * init and return our conn. If we lost, we rollback and return the
217     * other one.
218     */
219    spin_lock_irqsave(&rds_conn_lock, flags);
220    if (parent) {
221        /* Creating passive conn */
222        if (parent->c_passive) {
223            trans->conn_free(conn->c_transport_data);
224            kmem_cache_free(rds_conn_slab, conn);
225            conn = parent->c_passive;
226        } else {
227            parent->c_passive = conn;
228            rds_cong_add_conn(conn);
229            rds_conn_count++;
230        }
231    } else {
232        /* Creating normal conn */
233        struct rds_connection *found;
234
235        found = rds_conn_lookup(head, laddr, faddr, trans);
236        if (found) {
237            trans->conn_free(conn->c_transport_data);
238            kmem_cache_free(rds_conn_slab, conn);
239            conn = found;
240        } else {
241            hlist_add_head(&conn->c_hash_node, head);
242            rds_cong_add_conn(conn);
243            rds_conn_count++;
244        }
245    }
246    spin_unlock_irqrestore(&rds_conn_lock, flags);
247
248out:
249    return conn;
250}
251
252struct rds_connection *rds_conn_create(__be32 laddr, __be32 faddr,
253                       struct rds_transport *trans, gfp_t gfp)
254{
255    return __rds_conn_create(laddr, faddr, trans, gfp, 0);
256}
257EXPORT_SYMBOL_GPL(rds_conn_create);
258
259struct rds_connection *rds_conn_create_outgoing(__be32 laddr, __be32 faddr,
260                       struct rds_transport *trans, gfp_t gfp)
261{
262    return __rds_conn_create(laddr, faddr, trans, gfp, 1);
263}
264EXPORT_SYMBOL_GPL(rds_conn_create_outgoing);
265
266void rds_conn_destroy(struct rds_connection *conn)
267{
268    struct rds_message *rm, *rtmp;
269
270    rdsdebug("freeing conn %p for %pI4 -> "
271         "%pI4\n", conn, &conn->c_laddr,
272         &conn->c_faddr);
273
274    hlist_del_init(&conn->c_hash_node);
275
276    /* wait for the rds thread to shut it down */
277    atomic_set(&conn->c_state, RDS_CONN_ERROR);
278    cancel_delayed_work(&conn->c_conn_w);
279    queue_work(rds_wq, &conn->c_down_w);
280    flush_workqueue(rds_wq);
281
282    /* tear down queued messages */
283    list_for_each_entry_safe(rm, rtmp,
284                 &conn->c_send_queue,
285                 m_conn_item) {
286        list_del_init(&rm->m_conn_item);
287        BUG_ON(!list_empty(&rm->m_sock_item));
288        rds_message_put(rm);
289    }
290    if (conn->c_xmit_rm)
291        rds_message_put(conn->c_xmit_rm);
292
293    conn->c_trans->conn_free(conn->c_transport_data);
294
295    /*
296     * The congestion maps aren't freed up here. They're
297     * freed by rds_cong_exit() after all the connections
298     * have been freed.
299     */
300    rds_cong_remove_conn(conn);
301
302    BUG_ON(!list_empty(&conn->c_retrans));
303    kmem_cache_free(rds_conn_slab, conn);
304
305    rds_conn_count--;
306}
307EXPORT_SYMBOL_GPL(rds_conn_destroy);
308
309static void rds_conn_message_info(struct socket *sock, unsigned int len,
310                  struct rds_info_iterator *iter,
311                  struct rds_info_lengths *lens,
312                  int want_send)
313{
314    struct hlist_head *head;
315    struct hlist_node *pos;
316    struct list_head *list;
317    struct rds_connection *conn;
318    struct rds_message *rm;
319    unsigned long flags;
320    unsigned int total = 0;
321    size_t i;
322
323    len /= sizeof(struct rds_info_message);
324
325    spin_lock_irqsave(&rds_conn_lock, flags);
326
327    for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
328         i++, head++) {
329        hlist_for_each_entry(conn, pos, head, c_hash_node) {
330            if (want_send)
331                list = &conn->c_send_queue;
332            else
333                list = &conn->c_retrans;
334
335            spin_lock(&conn->c_lock);
336
337            /* XXX too lazy to maintain counts.. */
338            list_for_each_entry(rm, list, m_conn_item) {
339                total++;
340                if (total <= len)
341                    rds_inc_info_copy(&rm->m_inc, iter,
342                              conn->c_laddr,
343                              conn->c_faddr, 0);
344            }
345
346            spin_unlock(&conn->c_lock);
347        }
348    }
349
350    spin_unlock_irqrestore(&rds_conn_lock, flags);
351
352    lens->nr = total;
353    lens->each = sizeof(struct rds_info_message);
354}
355
356static void rds_conn_message_info_send(struct socket *sock, unsigned int len,
357                       struct rds_info_iterator *iter,
358                       struct rds_info_lengths *lens)
359{
360    rds_conn_message_info(sock, len, iter, lens, 1);
361}
362
363static void rds_conn_message_info_retrans(struct socket *sock,
364                      unsigned int len,
365                      struct rds_info_iterator *iter,
366                      struct rds_info_lengths *lens)
367{
368    rds_conn_message_info(sock, len, iter, lens, 0);
369}
370
371void rds_for_each_conn_info(struct socket *sock, unsigned int len,
372              struct rds_info_iterator *iter,
373              struct rds_info_lengths *lens,
374              int (*visitor)(struct rds_connection *, void *),
375              size_t item_len)
376{
377    uint64_t buffer[(item_len + 7) / 8];
378    struct hlist_head *head;
379    struct hlist_node *pos;
380    struct hlist_node *tmp;
381    struct rds_connection *conn;
382    unsigned long flags;
383    size_t i;
384
385    spin_lock_irqsave(&rds_conn_lock, flags);
386
387    lens->nr = 0;
388    lens->each = item_len;
389
390    for (i = 0, head = rds_conn_hash; i < ARRAY_SIZE(rds_conn_hash);
391         i++, head++) {
392        hlist_for_each_entry_safe(conn, pos, tmp, head, c_hash_node) {
393
394            /* XXX no c_lock usage.. */
395            if (!visitor(conn, buffer))
396                continue;
397
398            /* We copy as much as we can fit in the buffer,
399             * but we count all items so that the caller
400             * can resize the buffer. */
401            if (len >= item_len) {
402                rds_info_copy(iter, buffer, item_len);
403                len -= item_len;
404            }
405            lens->nr++;
406        }
407    }
408
409    spin_unlock_irqrestore(&rds_conn_lock, flags);
410}
411EXPORT_SYMBOL_GPL(rds_for_each_conn_info);
412
413static int rds_conn_info_visitor(struct rds_connection *conn,
414                  void *buffer)
415{
416    struct rds_info_connection *cinfo = buffer;
417
418    cinfo->next_tx_seq = conn->c_next_tx_seq;
419    cinfo->next_rx_seq = conn->c_next_rx_seq;
420    cinfo->laddr = conn->c_laddr;
421    cinfo->faddr = conn->c_faddr;
422    strncpy(cinfo->transport, conn->c_trans->t_name,
423        sizeof(cinfo->transport));
424    cinfo->flags = 0;
425
426    rds_conn_info_set(cinfo->flags,
427              rds_conn_is_sending(conn), SENDING);
428    /* XXX Future: return the state rather than these funky bits */
429    rds_conn_info_set(cinfo->flags,
430              atomic_read(&conn->c_state) == RDS_CONN_CONNECTING,
431              CONNECTING);
432    rds_conn_info_set(cinfo->flags,
433              atomic_read(&conn->c_state) == RDS_CONN_UP,
434              CONNECTED);
435    return 1;
436}
437
438static void rds_conn_info(struct socket *sock, unsigned int len,
439              struct rds_info_iterator *iter,
440              struct rds_info_lengths *lens)
441{
442    rds_for_each_conn_info(sock, len, iter, lens,
443                rds_conn_info_visitor,
444                sizeof(struct rds_info_connection));
445}
446
447int __init rds_conn_init(void)
448{
449    rds_conn_slab = kmem_cache_create("rds_connection",
450                      sizeof(struct rds_connection),
451                      0, 0, NULL);
452    if (rds_conn_slab == NULL)
453        return -ENOMEM;
454
455    rds_info_register_func(RDS_INFO_CONNECTIONS, rds_conn_info);
456    rds_info_register_func(RDS_INFO_SEND_MESSAGES,
457                   rds_conn_message_info_send);
458    rds_info_register_func(RDS_INFO_RETRANS_MESSAGES,
459                   rds_conn_message_info_retrans);
460
461    return 0;
462}
463
464void rds_conn_exit(void)
465{
466    rds_loop_exit();
467
468    WARN_ON(!hlist_empty(rds_conn_hash));
469
470    kmem_cache_destroy(rds_conn_slab);
471
472    rds_info_deregister_func(RDS_INFO_CONNECTIONS, rds_conn_info);
473    rds_info_deregister_func(RDS_INFO_SEND_MESSAGES,
474                 rds_conn_message_info_send);
475    rds_info_deregister_func(RDS_INFO_RETRANS_MESSAGES,
476                 rds_conn_message_info_retrans);
477}
478
479/*
480 * Force a disconnect
481 */
482void rds_conn_drop(struct rds_connection *conn)
483{
484    atomic_set(&conn->c_state, RDS_CONN_ERROR);
485    queue_work(rds_wq, &conn->c_down_w);
486}
487EXPORT_SYMBOL_GPL(rds_conn_drop);
488
489/*
490 * An error occurred on the connection
491 */
492void
493__rds_conn_error(struct rds_connection *conn, const char *fmt, ...)
494{
495    va_list ap;
496
497    va_start(ap, fmt);
498    vprintk(fmt, ap);
499    va_end(ap);
500
501    rds_conn_drop(conn);
502}
503

Archive Download this file



interactive