Skip to content

Commit c6527ef

Browse files
committed
session server UPDATE non-blocking accept
Allow accepting new sessions from multiple threads. Made server sockets non-blocking.
1 parent 2332e58 commit c6527ef

File tree

4 files changed

+324
-164
lines changed

4 files changed

+324
-164
lines changed

src/server_config.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ nc_server_config_free(struct nc_server_config *config)
407407
}
408408
}
409409
free(endpt->binds[j].address);
410-
pthread_mutex_destroy(&endpt->bind_lock);
411410
}
412411
LY_ARRAY_FREE(endpt->binds);
413412

@@ -3325,7 +3324,6 @@ config_endpoint(const struct lyd_node *node, enum nc_operation parent_op,
33253324
struct nc_endpt *endpt = NULL;
33263325
const char *name;
33273326
LY_ARRAY_COUNT_TYPE i = 0;
3328-
int r;
33293327

33303328
NC_NODE_GET_OP(node, parent_op, &op);
33313329

@@ -3346,12 +3344,6 @@ config_endpoint(const struct lyd_node *node, enum nc_operation parent_op,
33463344
} else if (op == NC_OP_CREATE) {
33473345
/* create a new endpoint */
33483346
LY_ARRAY_NEW_RET(LYD_CTX(node), config->endpts, endpt, 1);
3349-
3350-
/* init the new endpoint */
3351-
if ((r = pthread_mutex_init(&endpt->bind_lock, NULL))) {
3352-
ERR(NULL, "Mutex init failed (%s).", strerror(r));
3353-
return 1;
3354-
}
33553347
}
33563348

33573349
/* config name */
@@ -3376,7 +3368,6 @@ config_endpoint(const struct lyd_node *node, enum nc_operation parent_op,
33763368

33773369
/* all children processed, we can now delete the endpoint */
33783370
if (op == NC_OP_DELETE) {
3379-
pthread_mutex_destroy(&endpt->bind_lock);
33803371
if (i < LY_ARRAY_COUNT(config->endpts) - 1) {
33813372
config->endpts[i] = config->endpts[LY_ARRAY_COUNT(config->endpts) - 1];
33823373
}
@@ -6007,7 +5998,6 @@ nc_server_config_dup(const struct nc_server_config *src, struct nc_server_config
60075998
dst_endpt->binds[j].sock = -1;
60085999
LY_ARRAY_INCREMENT(dst_endpt->binds);
60096000
}
6010-
pthread_mutex_init(&dst_endpt->bind_lock, NULL);
60116001

60126002
dst_endpt->ka = src_endpt->ka;
60136003

src/session_client.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,6 @@ nc_client_ch_add_bind_listen(const char *address, uint16_t port, const char *hos
18231823
client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
18241824
client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
18251825
client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
1826-
client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
18271826

18281827
return 0;
18291828
}
@@ -1886,7 +1885,7 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
18861885
{
18871886
int ret, sock;
18881887
char *host = NULL;
1889-
uint16_t port, idx;
1888+
uint16_t port, bind_idx = 0;
18901889

18911890
NC_CHECK_ARG_RET(NULL, session, -1);
18921891

@@ -1895,8 +1894,8 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
18951894
return -1;
18961895
}
18971896

1898-
ret = nc_sock_accept_binds(NULL, client_opts.ch_binds, client_opts.ch_bind_count, &client_opts.ch_bind_lock, timeout,
1899-
&host, &port, &idx, &sock);
1897+
ret = nc_server_ch_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout,
1898+
&host, &port, &bind_idx, &sock);
19001899
if (ret < 1) {
19011900
free(host);
19021901
return ret;
@@ -1909,11 +1908,11 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
19091908
return -1;
19101909
}
19111910

1912-
if (client_opts.ch_binds_aux[idx].ti == NC_TI_SSH) {
1911+
if (client_opts.ch_binds_aux[bind_idx].ti == NC_TI_SSH) {
19131912
*session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
1914-
} else if (client_opts.ch_binds_aux[idx].ti == NC_TI_TLS) {
1913+
} else if (client_opts.ch_binds_aux[bind_idx].ti == NC_TI_TLS) {
19151914
*session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT,
1916-
client_opts.ch_binds_aux[idx].hostname);
1915+
client_opts.ch_binds_aux[bind_idx].hostname);
19171916
} else {
19181917
close(sock);
19191918
*session = NULL;
@@ -1971,13 +1970,6 @@ nc_session_ntf_thread_running(const struct nc_session *session)
19711970
API int
19721971
nc_client_init(void)
19731972
{
1974-
int r;
1975-
1976-
if ((r = pthread_mutex_init(&client_opts.ch_bind_lock, NULL))) {
1977-
ERR(NULL, "%s: failed to init bind lock(%s).", __func__, strerror(r));
1978-
return -1;
1979-
}
1980-
19811973
#ifdef NC_ENABLED_SSH_TLS
19821974
if (nc_tls_backend_init_wrap()) {
19831975
ERR(NULL, "%s: failed to init the SSL library backend.", __func__);
@@ -1995,7 +1987,6 @@ nc_client_init(void)
19951987
API void
19961988
nc_client_destroy(void)
19971989
{
1998-
pthread_mutex_destroy(&client_opts.ch_bind_lock);
19991990
nc_client_set_schema_searchpath(NULL);
20001991
nc_client_unix_set_username(NULL);
20011992
#ifdef NC_ENABLED_SSH_TLS

src/session_p.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ struct nc_bind {
492492
char *address; /**< Either IPv4/IPv6 address or path to UNIX socket. */
493493
uint16_t port; /**< Either port number or 0 for UNIX socket. */
494494
int sock; /**< Socket file descriptor, -1 if not created yet. */
495-
int pollin; /**< Specifies, which sockets to poll on. */
496495
};
497496

498497
struct nc_client_unix_opts {
@@ -573,7 +572,6 @@ struct nc_client_opts {
573572
struct nc_keepalives ka;
574573

575574
struct nc_bind *ch_binds;
576-
pthread_mutex_t ch_bind_lock; /**< To avoid concurrent calls of poll and accept on the bound sockets **/
577575

578576
struct {
579577
NC_TRANSPORT_IMPL ti;
@@ -692,9 +690,7 @@ struct nc_server_config {
692690
struct nc_endpt {
693691
char *name; /**< Identifier of the endpoint. */
694692

695-
/* ACCESS locked - bind_lock */
696693
struct nc_bind *binds; /**< Listening binds of the endpoint (sized-array, see libyang docs). */
697-
pthread_mutex_t bind_lock; /**< To avoid concurrent calls of poll and accept on the bound sockets. **/
698694

699695
struct nc_keepalives ka; /**< TCP keepalives configuration. */
700696

@@ -1285,23 +1281,19 @@ int nc_sock_accept(int sock, int timeout, char **peer_host, uint16_t *peer_port)
12851281
int nc_sock_listen_inet(const char *address, uint16_t port);
12861282

12871283
/**
1288-
* @brief Accept a new connection on a listening socket.
1284+
* @brief Accept a new connection on any of the given Call Home binds.
12891285
*
1290-
* @param[in] endpt Optional endpoint the binds belong to (only for logging purposes).
1291-
* @param[in] binds Structure with the listening sockets.
1286+
* @param[in] binds Call Home binds to accept on.
12921287
* @param[in] bind_count Number of @p binds.
1293-
* @param[in] bind_lock Lock for avoiding concurrent poll/accept on a single bind.
12941288
* @param[in] timeout Timeout for accepting.
12951289
* @param[out] host Host of the remote peer. Can be NULL.
12961290
* @param[out] port Port of the new connection. Can be NULL.
1297-
* @param[out] idx Index of the bind that was accepted. Can be NULL.
1291+
* @param[out] bind_idx Index of the bind that was accepted. Can be NULL.
12981292
* @param[out] sock Accepted socket, if any.
1299-
* @return -1 on error.
1300-
* @return 0 on timeout.
1301-
* @return 1 if a socket was accepted.
1293+
* @return -1 on error, 0 on timeout, 1 if a socket was accepted.
13021294
*/
1303-
int nc_sock_accept_binds(struct nc_endpt *endpt, struct nc_bind *binds, uint16_t bind_count,
1304-
pthread_mutex_t *bind_lock, int timeout, char **host, uint16_t *port, uint16_t *idx, int *sock);
1295+
int nc_server_ch_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host,
1296+
uint16_t *port, uint16_t *bind_idx, int *sock);
13051297

13061298
/**
13071299
* @brief Establish a UNIX transport session.

0 commit comments

Comments
 (0)