openldap/servers/lloadd/backend.c

488 lines
15 KiB
C
Raw Normal View History

2017-03-16 20:11:45 +08:00
/* $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"
2017-06-26 22:48:32 +08:00
static void
upstream_connect_cb( evutil_socket_t s, short what, void *arg )
{
PendingConnection *conn = arg;
Backend *b = conn->backend;
2017-09-25 17:45:07 +08:00
int error = 0, rc = -1;
2017-06-26 22:48:32 +08:00
ldap_pvt_thread_mutex_lock( &b->b_mutex );
Debug( LDAP_DEBUG_CONNS, "upstream_connect_cb: "
"fd=%d connection callback for backend uri='%s'\n",
s, b->b_uri.bv_val );
if ( what == EV_WRITE ) {
socklen_t optlen = sizeof(error);
if ( getsockopt( conn->fd, SOL_SOCKET, SO_ERROR, (void *)&error,
&optlen ) < 0 ) {
goto done;
}
if ( error == EINTR || error == EINPROGRESS || error == EWOULDBLOCK ) {
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
return;
} else if ( error ) {
goto done;
} else if ( !upstream_init( s, conn->backend ) ) {
goto done;
}
rc = LDAP_SUCCESS;
}
done:
if ( rc ) {
2017-09-25 17:45:07 +08:00
char ebuf[128];
2017-06-26 22:48:32 +08:00
evutil_closesocket( conn->fd );
2017-09-25 17:45:07 +08:00
b->b_opening--;
2017-06-26 22:48:32 +08:00
b->b_failed++;
Debug( LDAP_DEBUG_ANY, "upstream_connect_cb: "
2017-09-25 17:45:07 +08:00
"fd=%d connection set up failed%s%s\n",
s, error ? ": " : "",
error ? sock_errstr( error, ebuf, sizeof(ebuf) ) : "" );
2017-06-26 22:48:32 +08:00
} else {
b->b_failed = 0;
}
2017-09-25 17:45:07 +08:00
LDAP_LIST_REMOVE( conn, next );
2017-06-26 22:48:32 +08:00
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
event_free( conn->event );
ch_free( conn );
2017-09-25 17:45:07 +08:00
if ( rc ) {
backend_retry( b );
}
2017-06-26 22:48:32 +08:00
}
2017-03-16 20:11:45 +08:00
static void
upstream_name_cb( int result, struct evutil_addrinfo *res, void *arg )
{
Backend *b = arg;
ber_socket_t s = AC_SOCKET_INVALID;
2017-03-16 20:11:45 +08:00
int rc;
ldap_pvt_thread_mutex_lock( &b->b_mutex );
2017-03-16 20:11:45 +08:00
if ( result || !res ) {
Debug( LDAP_DEBUG_ANY, "upstream_name_cb: "
"name resolution failed for backend '%s': %s\n",
2017-06-14 02:46:31 +08:00
b->b_uri.bv_val, evutil_gai_strerror( result ) );
goto fail;
2017-03-16 20:11:45 +08:00
}
/* TODO: if we get failures, try the other addrinfos */
if ( (s = socket( res->ai_family, SOCK_STREAM, 0 )) ==
AC_SOCKET_INVALID ) {
goto fail;
2017-03-16 20:11:45 +08:00
}
if ( ber_pvt_socket_set_nonblock( s, 1 ) ) {
goto fail;
2017-03-16 20:11:45 +08:00
}
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 );
}
2017-06-26 22:48:32 +08:00
/* Asynchronous connect */
if ( rc ) {
struct timeval tv = { slap_write_timeout / 1000,
1000 * ( slap_write_timeout % 1000 ) };
PendingConnection *conn;
if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
Debug( LDAP_DEBUG_ANY, "upstream_name_cb: "
"failed to connect to server '%s'\n",
b->b_uri.bv_val );
evutil_closesocket( s );
goto fail;
}
conn = ch_calloc( 1, sizeof(PendingConnection) );
LDAP_LIST_ENTRY_INIT( conn, next );
conn->backend = b;
conn->fd = s;
conn->event = event_new( slap_get_base( s ), s, EV_WRITE|EV_PERSIST,
upstream_connect_cb, conn );
if ( !conn->event ) {
Debug( LDAP_DEBUG_ANY, "upstream_name_cb: "
"failed to acquire an event to finish upstream "
"connection setup.\n" );
ch_free( conn );
evutil_closesocket( s );
goto fail;
}
event_add( conn->event, &tv );
LDAP_LIST_INSERT_HEAD( &b->b_connecting, conn, next );
Debug( LDAP_DEBUG_CONNS, "upstream_name_cb: "
"connection to backend uri=%s in progress\n",
2017-06-14 02:46:31 +08:00
b->b_uri.bv_val );
2017-09-25 17:45:07 +08:00
} else if ( !upstream_init( s, b ) ) {
2017-04-12 23:00:52 +08:00
goto fail;
}
2017-09-25 17:45:07 +08:00
2017-03-18 01:01:36 +08:00
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
2017-09-25 17:45:07 +08:00
evutil_freeaddrinfo( res );
return;
fail:
if ( s != AC_SOCKET_INVALID ) {
evutil_closesocket( s );
}
b->b_opening--;
2017-04-12 23:00:52 +08:00
b->b_failed++;
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
backend_retry( b );
2017-09-25 17:45:07 +08:00
if ( res ) {
evutil_freeaddrinfo( res );
}
2017-03-16 20:11:45 +08:00
}
Connection *
backend_select( Operation *op )
{
Backend *b, *first, *next;
ldap_pvt_thread_mutex_lock( &backend_mutex );
first = b = current_backend;
ldap_pvt_thread_mutex_unlock( &backend_mutex );
if ( !first ) {
return NULL;
}
2017-03-16 20:11:45 +08:00
/* TODO: Two runs, one with trylock, then one actually locked if we don't
* find anything? */
do {
struct ConnSt *head;
2017-03-16 20:11:45 +08:00
Connection *c;
2017-03-18 01:01:36 +08:00
ldap_pvt_thread_mutex_lock( &b->b_mutex );
next = LDAP_CIRCLEQ_LOOP_NEXT( &backend, b, b_next );
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",
2017-06-14 02:46:31 +08:00
b->b_uri.bv_val );
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
b = next;
continue;
}
if ( op->o_tag == LDAP_REQ_BIND
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
&& !(lload_features & LLOAD_FEATURE_VC)
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
) {
head = &b->b_bindconns;
} else {
head = &b->b_conns;
}
LDAP_CIRCLEQ_FOREACH ( c, head, c_next ) {
ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
2017-04-21 18:07:43 +08:00
CONNECTION_LOCK(c);
2017-09-28 17:13:24 +08:00
if ( c->c_state == LLOAD_C_READY && !c->c_pendingber &&
( b->b_max_conn_pending == 0 ||
c->c_n_ops_executing < b->b_max_conn_pending ) ) {
Debug( LDAP_DEBUG_CONNS, "backend_select: "
2017-06-20 20:00:31 +08:00
"selected connection connid=%lu for client "
"connid=%lu msgid=%d\n",
c->c_connid, op->o_client_connid, op->o_client_msgid );
2017-05-10 23:06:19 +08:00
/*
* Round-robin step:
* Rotate the queue to put this connection at the end, same for
* the backend.
2017-05-10 23:06:19 +08:00
*/
LDAP_CIRCLEQ_MAKE_TAIL( head, c, c_next );
ldap_pvt_thread_mutex_lock( &backend_mutex );
current_backend = next;
ldap_pvt_thread_mutex_unlock( &backend_mutex );
b->b_n_ops_executing++;
c->c_n_ops_executing++;
2017-04-21 18:07:43 +08:00
CONNECTION_UNLOCK_INCREF(c);
2017-05-10 23:06:19 +08:00
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
return c;
}
2017-04-21 18:07:43 +08:00
CONNECTION_UNLOCK(c);
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
2017-03-16 20:11:45 +08:00
}
2017-03-18 01:01:36 +08:00
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
b = next;
} while ( b != first );
2017-03-16 20:11:45 +08:00
return NULL;
}
void
backend_retry( Backend *b )
{
int rc, requested;
2017-05-03 18:07:35 +08:00
if ( slapd_shutdown ) {
Debug( LDAP_DEBUG_CONNS, "backend_retry: "
"shutting down\n" );
return;
}
ldap_pvt_thread_mutex_lock( &b->b_mutex );
requested = b->b_numconns;
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
if ( !(lload_features & LLOAD_FEATURE_VC) )
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
{
requested += b->b_numbindconns;
}
if ( b->b_active + b->b_bindavail + b->b_opening < requested ) {
2017-04-12 23:02:35 +08:00
if ( b->b_opening > 0 || b->b_failed > 0 ) {
2017-07-10 17:21:35 +08:00
if ( b->b_failed > 0 &&
!event_pending( b->b_retry_event, EV_TIMEOUT, NULL ) ) {
2017-04-12 23:02:35 +08:00
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: "
2017-07-10 17:21:35 +08:00
"retry in progress already\n" );
2017-04-12 23:02:35 +08:00
}
} 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 );
if ( rc ) {
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
backend_connect( -1, 0, b );
return;
}
}
2017-04-12 23:02:35 +08:00
} else {
Debug( LDAP_DEBUG_CONNS, "backend_retry: "
"no more connections needed for this backend\n" );
}
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
}
2017-04-12 23:02:35 +08:00
void
backend_connect( evutil_socket_t s, short what, void *arg )
2017-03-16 20:11:45 +08:00
{
struct evutil_addrinfo hints = {};
Backend *b = arg;
char *hostname;
2017-03-16 20:11:45 +08:00
2017-05-03 18:07:35 +08:00
if ( slapd_shutdown ) {
Debug( LDAP_DEBUG_CONNS, "backend_connect: "
"doing nothing, shutdown in progress\n" );
return;
}
ldap_pvt_thread_mutex_lock( &b->b_mutex );
2017-04-12 23:02:35 +08:00
Debug( LDAP_DEBUG_CONNS, "backend_connect: "
2017-07-10 17:21:35 +08:00
"%sattempting connection to %s\n",
(what & EV_TIMEOUT) ? "retry timeout finished, " : "",
2017-04-12 23:02:35 +08:00
b->b_host );
2017-03-16 20:11:45 +08:00
#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 ) {
goto fail;
2017-03-16 20:11:45 +08:00
}
rc = ber_pvt_socket_set_nonblock( s, 1 );
if ( rc ) {
evutil_closesocket( s );
goto fail;
2017-03-16 20:11:45 +08:00
}
if ( strlen( b->b_host ) > ( sizeof(addr.sun_path) - 1 ) ) {
evutil_closesocket( s );
goto fail;
2017-03-16 20:11:45 +08:00
}
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) );
2017-06-26 22:48:32 +08:00
/* Asynchronous connect */
if ( rc ) {
struct timeval tv = { slap_write_timeout / 1000,
1000 * ( slap_write_timeout % 1000 ) };
PendingConnection *conn;
if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
evutil_closesocket( s );
goto fail;
}
conn = ch_calloc( 1, sizeof(PendingConnection) );
LDAP_LIST_ENTRY_INIT( conn, next );
conn->backend = b;
conn->fd = s;
conn->event = event_new( slap_get_base( s ), s,
EV_WRITE|EV_PERSIST, upstream_connect_cb, conn );
if ( !conn->event ) {
Debug( LDAP_DEBUG_ANY, "backend_connect: "
"failed to acquire an event to finish upstream "
"connection setup.\n" );
ch_free( conn );
evutil_closesocket( s );
goto fail;
}
event_add( conn->event, &tv );
LDAP_LIST_INSERT_HEAD( &b->b_connecting, conn, next );
Debug( LDAP_DEBUG_CONNS, "backend_connect: "
"connection to backend uri=%s in progress\n",
b->b_uri.bv_val );
} else if ( !upstream_init( s, b ) ) {
2017-04-12 23:00:52 +08:00
goto fail;
}
2017-06-26 22:48:32 +08:00
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
2017-04-12 23:02:35 +08:00
return;
2017-03-16 20:11:45 +08:00
}
#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;
hostname = b->b_host;
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
evdns_getaddrinfo( dnsbase, hostname, NULL, &hints, upstream_name_cb, b );
2017-04-12 23:02:35 +08:00
return;
fail:
b->b_opening--;
2017-04-12 23:00:52 +08:00
b->b_failed++;
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
backend_retry( b );
2017-04-12 23:02:35 +08:00
}
void *
backend_connect_task( void *ctx, void *arg )
{
backend_connect( -1, 0, arg );
return NULL;
2017-03-16 20:11:45 +08:00
}
2017-05-03 18:07:35 +08:00
void
backends_destroy( void )
{
while ( !LDAP_CIRCLEQ_EMPTY( &backend ) ) {
Backend *b = LDAP_CIRCLEQ_FIRST( &backend );
2017-05-03 18:07:35 +08:00
Debug( LDAP_DEBUG_CONNS, "backends_destroy: "
"destroying backend uri='%s', numconns=%d, numbindconns=%d\n",
2017-06-14 02:46:31 +08:00
b->b_uri.bv_val, b->b_numconns, b->b_numbindconns );
2017-05-03 18:07:35 +08:00
2017-06-26 22:48:32 +08:00
while ( !LDAP_LIST_EMPTY( &b->b_connecting ) ) {
PendingConnection *pending = LDAP_LIST_FIRST( &b->b_connecting );
Debug( LDAP_DEBUG_CONNS, "backends_destroy: "
"destroying socket pending connect() fd=%d\n",
pending->fd );
event_free( pending->event );
evutil_closesocket( pending->fd );
LDAP_LIST_REMOVE( pending, next );
ch_free( pending );
}
2017-09-25 17:45:07 +08:00
while ( !LDAP_CIRCLEQ_EMPTY( &b->b_preparing ) ) {
Connection *c = LDAP_CIRCLEQ_FIRST( &b->b_preparing );
CONNECTION_LOCK(c);
Debug( LDAP_DEBUG_CONNS, "backends_destroy: "
"destroying connection being set up connid=%lu\n",
c->c_connid );
assert( c->c_live );
CONNECTION_DESTROY(c);
}
while ( !LDAP_CIRCLEQ_EMPTY( &b->b_bindconns ) ) {
Connection *c = LDAP_CIRCLEQ_FIRST( &b->b_bindconns );
2017-05-03 18:07:35 +08:00
CONNECTION_LOCK(c);
Debug( LDAP_DEBUG_CONNS, "backends_destroy: "
"destroying bind connection connid=%lu, pending ops=%ld\n",
c->c_connid, c->c_n_ops_executing );
assert( c->c_live );
2017-09-22 17:24:52 +08:00
CONNECTION_DESTROY(c);
2017-05-03 18:07:35 +08:00
}
while ( !LDAP_CIRCLEQ_EMPTY( &b->b_conns ) ) {
Connection *c = LDAP_CIRCLEQ_FIRST( &b->b_conns );
2017-05-03 18:07:35 +08:00
CONNECTION_LOCK(c);
Debug( LDAP_DEBUG_CONNS, "backends_destroy: "
"destroying regular connection connid=%lu, pending "
"ops=%ld\n",
c->c_connid, c->c_n_ops_executing );
assert( c->c_live );
2017-09-22 17:24:52 +08:00
CONNECTION_DESTROY(c);
2017-05-03 18:07:35 +08:00
}
LDAP_CIRCLEQ_REMOVE( &backend, b, b_next );
2017-05-03 18:07:35 +08:00
ldap_pvt_thread_mutex_destroy( &b->b_mutex );
event_del( b->b_retry_event );
event_free( b->b_retry_event );
ch_free( b->b_host );
2017-06-14 02:46:31 +08:00
ch_free( b->b_uri.bv_val );
2017-05-03 18:07:35 +08:00
ch_free( b );
}
}