backend.c 7.88 KB
Newer Older
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1998-2020 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */

#include "portable.h"

#include <ac/socket.h>
#include <ac/errno.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/unistd.h>

#include <event2/event.h>
#include <event2/dns.h>

#include "lutil.h"
#include "slap.h"

static void
upstream_name_cb( int result, struct evutil_addrinfo *res, void *arg )
{
    Backend *b = arg;
34
    ber_socket_t s = AC_SOCKET_INVALID;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
35
36
    int rc;

37
38
    ldap_pvt_thread_mutex_lock( &b->b_mutex );

Ondřej Kuzník's avatar
Ondřej Kuzník committed
39
40
41
42
    if ( result || !res ) {
        Debug( LDAP_DEBUG_ANY, "upstream_name_cb: "
                "name resolution failed for backend '%s': %s\n",
                b->b_bindconf.sb_uri.bv_val, evutil_gai_strerror( result ) );
43
        goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
44
45
    }

46
47
48
49
    /* TODO: if we get failures, try the other addrinfos */
    if ( (s = socket( res->ai_family, SOCK_STREAM, 0 )) ==
            AC_SOCKET_INVALID ) {
        goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
50
51
    }

52
53
    if ( ber_pvt_socket_set_nonblock( s, 1 ) ) {
        goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    }

    if ( res->ai_family == PF_INET ) {
        struct sockaddr_in *ai = (struct sockaddr_in *)res->ai_addr;
        ai->sin_port = htons( b->b_port );
        rc = connect( s, (struct sockaddr *)ai, res->ai_addrlen );
    } else {
        struct sockaddr_in6 *ai = (struct sockaddr_in6 *)res->ai_addr;
        ai->sin6_port = htons( b->b_port );
        rc = connect( s, (struct sockaddr *)ai, res->ai_addrlen );
    }
    if ( rc && errno != EINPROGRESS && errno != EWOULDBLOCK ) {
        Debug( LDAP_DEBUG_ANY, "upstream_name_cb: "
                "failed to connect to server '%s'\n",
                b->b_bindconf.sb_uri.bv_val );
69
        goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
70
71
    }

72
73
74
75
76
    if ( !upstream_init( s, b ) ) {
        goto fail;
    }
    b->b_opening--;
    b->b_failed = 0;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
77
    ldap_pvt_thread_mutex_unlock( &b->b_mutex );
78
    backend_retry( b );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
79
    free( res );
80
81
82
83
84
85
86
    return;

fail:
    if ( s != AC_SOCKET_INVALID ) {
        evutil_closesocket( s );
    }
    b->b_opening--;
87
    b->b_failed++;
88
89
    ldap_pvt_thread_mutex_unlock( &b->b_mutex );
    backend_retry( b );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
90
    free( res );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
91
92
93
94
95
96
97
}

Connection *
backend_select( Operation *op )
{
    Backend *b;

98
99
    /* TODO: Two runs, one with trylock, then one actually locked if we don't
     * find anything? */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
100
    LDAP_STAILQ_FOREACH ( b, &backend, b_next ) {
101
        struct ConnSt *head;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
102
103
        Connection *c;

Ondřej Kuzník's avatar
Ondřej Kuzník committed
104
        ldap_pvt_thread_mutex_lock( &b->b_mutex );
105
106
107
108
109
110
111
112
113

        if ( b->b_max_pending && b->b_n_ops_executing >= b->b_max_pending ) {
            Debug( LDAP_DEBUG_CONNS, "backend_select: "
                    "backend %s too busy\n",
                    b->b_bindconf.sb_uri.bv_val );
            ldap_pvt_thread_mutex_unlock( &b->b_mutex );
            continue;
        }

114
115
116
117
118
119
120
121
122
123
124
125
        if ( op->o_tag == LDAP_REQ_BIND &&
                !(lload_features & LLOAD_FEATURE_VC) ) {
            head = &b->b_bindconns;
        } else {
            head = &b->b_conns;
        }

        /* TODO: Use CIRCLEQ so that we can do a natural round robin over the
         * backend's connections? */
        LDAP_LIST_FOREACH( c, head, c_next )
        {
            ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
126
            CONNECTION_LOCK(c);
127
128
129
            if ( c->c_state == SLAP_C_READY && !c->c_pendingber &&
                    ( b->b_max_conn_pending == 0 ||
                            c->c_n_ops_executing < b->b_max_conn_pending ) ) {
130
131
                Debug( LDAP_DEBUG_CONNS, "backend_select: "
                        "selected connection %lu for client %lu msgid=%d\n",
132
133
134
135
                        c->c_connid, op->o_client_connid, op->o_client_msgid );

                b->b_n_ops_executing++;
                c->c_n_ops_executing++;
136
                CONNECTION_UNLOCK_INCREF(c);
137
138
139
                ldap_pvt_thread_mutex_unlock( &b->b_mutex );
                return c;
            }
140
            CONNECTION_UNLOCK(c);
141
            ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
142
        }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
143
        ldap_pvt_thread_mutex_unlock( &b->b_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
144
145
146
147
148
    }

    return NULL;
}

149
150
151
152
153
154
155
156
157
158
159
160
void
backend_retry( Backend *b )
{
    int rc, requested;

    ldap_pvt_thread_mutex_lock( &b->b_mutex );

    requested = b->b_numconns;
    if ( !(lload_features & LLOAD_FEATURE_VC) ) {
        requested += b->b_numbindconns;
    }
    if ( b->b_active + b->b_bindavail + b->b_opening < requested ) {
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
        if ( b->b_opening > 0 || b->b_failed > 0 ) {
            if ( !event_pending( b->b_retry_event, EV_TIMEOUT, NULL ) ) {
                Debug( LDAP_DEBUG_CONNS, "backend_retry: "
                        "scheduling a retry in %d ms\n",
                        b->b_retry_timeout );
                b->b_opening++;
                event_add( b->b_retry_event, &b->b_retry_tv );
                ldap_pvt_thread_mutex_unlock( &b->b_mutex );
                return;
            } else {
                Debug( LDAP_DEBUG_CONNS, "backend_retry: "
                        "retry already scheduled\n" );
            }
        } else {
            Debug( LDAP_DEBUG_CONNS, "backend_retry: "
                    "scheduling re-connection straight away\n" );
            b->b_opening++;
            rc = ldap_pvt_thread_pool_submit(
                    &connection_pool, backend_connect_task, b );
            /* TODO check we're not shutting down */
            if ( rc ) {
                ldap_pvt_thread_mutex_unlock( &b->b_mutex );
                backend_connect( -1, 0, b );
                return;
            }
186
        }
187
188
189
    } else {
        Debug( LDAP_DEBUG_CONNS, "backend_retry: "
                "no more connections needed for this backend\n" );
190
191
192
193
    }
    ldap_pvt_thread_mutex_unlock( &b->b_mutex );
}

194
195
void
backend_connect( evutil_socket_t s, short what, void *arg )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
196
197
{
    struct evutil_addrinfo hints = {};
198
    Backend *b = arg;
199
    char *hostname;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
200

201
    ldap_pvt_thread_mutex_lock( &b->b_mutex );
202
203
204
205
    Debug( LDAP_DEBUG_CONNS, "backend_connect: "
            "attempting connection to %s\n",
            b->b_host );

Ondřej Kuzník's avatar
Ondřej Kuzník committed
206
207
208
209
210
211
212
#ifdef LDAP_PF_LOCAL
    if ( b->b_proto == LDAP_PROTO_IPC ) {
        struct sockaddr_un addr;
        ber_socket_t s = socket( PF_LOCAL, SOCK_STREAM, 0 );
        int rc;

        if ( s == AC_SOCKET_INVALID ) {
213
            goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
214
215
216
217
218
        }

        rc = ber_pvt_socket_set_nonblock( s, 1 );
        if ( rc ) {
            evutil_closesocket( s );
219
            goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
220
221
222
223
        }

        if ( strlen( b->b_host ) > ( sizeof(addr.sun_path) - 1 ) ) {
            evutil_closesocket( s );
224
            goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
225
226
227
228
229
230
231
232
233
        }
        memset( &addr, '\0', sizeof(addr) );
        addr.sun_family = AF_LOCAL;
        strcpy( addr.sun_path, b->b_host );

        rc = connect(
                s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un) );
        if ( rc && errno != EINPROGRESS && errno != EWOULDBLOCK ) {
            evutil_closesocket( s );
234
            goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
235
236
        }

237
238
239
240
241
        if ( !upstream_init( s, b ) ) {
            goto fail;
        }
        b->b_opening--;
        b->b_failed = 0;
242
243
        ldap_pvt_thread_mutex_unlock( &b->b_mutex );
        backend_retry( b );
244
        return;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
245
246
247
248
249
250
251
252
    }
#endif /* LDAP_PF_LOCAL */

    hints.ai_family = AF_UNSPEC;
    hints.ai_flags = EVUTIL_AI_CANONNAME;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

253
254
255
256
    hostname = b->b_host;
    ldap_pvt_thread_mutex_unlock( &b->b_mutex );

    evdns_getaddrinfo( dnsbase, hostname, NULL, &hints, upstream_name_cb, b );
257
    return;
258
259
260

fail:
    b->b_opening--;
261
    b->b_failed++;
262
263
    ldap_pvt_thread_mutex_unlock( &b->b_mutex );
    backend_retry( b );
264
265
266
267
268
269
270
}

void *
backend_connect_task( void *ctx, void *arg )
{
    backend_connect( -1, 0, arg );
    return NULL;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
271
}