Skip to content

Commit 4ddf03c

Browse files
Roytakmichalvasko
authored andcommitted
session server UPDATE non-blocking accept
Allow accepting new sessions from multiple threads. Made server sockets non-blocking.
1 parent e1df68c commit 4ddf03c

File tree

4 files changed

+337
-165
lines changed

4 files changed

+337
-165
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
@@ -1846,7 +1846,6 @@ nc_client_ch_add_bind_listen(const char *address, uint16_t port, const char *hos
18461846
client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
18471847
client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
18481848
client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
1849-
client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
18501849

18511850
return 0;
18521851
}
@@ -1909,7 +1908,7 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
19091908
{
19101909
int ret, sock;
19111910
char *host = NULL;
1912-
uint16_t port, idx;
1911+
uint16_t port, bind_idx = 0;
19131912

19141913
NC_CHECK_ARG_RET(NULL, session, -1);
19151914

@@ -1918,8 +1917,8 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
19181917
return -1;
19191918
}
19201919

1921-
ret = nc_sock_accept_binds(NULL, client_opts.ch_binds, client_opts.ch_bind_count, &client_opts.ch_bind_lock, timeout,
1922-
&host, &port, &idx, &sock);
1920+
ret = nc_server_ch_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout,
1921+
&host, &port, &bind_idx, &sock);
19231922
if (ret < 1) {
19241923
free(host);
19251924
return ret;
@@ -1932,11 +1931,11 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
19321931
return -1;
19331932
}
19341933

1935-
if (client_opts.ch_binds_aux[idx].ti == NC_TI_SSH) {
1934+
if (client_opts.ch_binds_aux[bind_idx].ti == NC_TI_SSH) {
19361935
*session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
1937-
} else if (client_opts.ch_binds_aux[idx].ti == NC_TI_TLS) {
1936+
} else if (client_opts.ch_binds_aux[bind_idx].ti == NC_TI_TLS) {
19381937
*session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT,
1939-
client_opts.ch_binds_aux[idx].hostname);
1938+
client_opts.ch_binds_aux[bind_idx].hostname);
19401939
} else {
19411940
close(sock);
19421941
*session = NULL;
@@ -1994,13 +1993,6 @@ nc_session_ntf_thread_running(const struct nc_session *session)
19941993
API int
19951994
nc_client_init(void)
19961995
{
1997-
int r;
1998-
1999-
if ((r = pthread_mutex_init(&client_opts.ch_bind_lock, NULL))) {
2000-
ERR(NULL, "%s: failed to init bind lock(%s).", __func__, strerror(r));
2001-
return -1;
2002-
}
2003-
20041996
#ifdef NC_ENABLED_SSH_TLS
20051997
if (nc_tls_backend_init_wrap()) {
20061998
ERR(NULL, "%s: failed to init the SSL library backend.", __func__);
@@ -2018,7 +2010,6 @@ nc_client_init(void)
20182010
API void
20192011
nc_client_destroy(void)
20202012
{
2021-
pthread_mutex_destroy(&client_opts.ch_bind_lock);
20222013
nc_client_set_schema_searchpath(NULL);
20232014
nc_client_unix_set_username(NULL);
20242015
#ifdef NC_ENABLED_SSH_TLS

src/session_p.h

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

505504
struct nc_client_unix_opts {
@@ -583,7 +582,6 @@ struct nc_client_opts {
583582
uint32_t capabilities_count; /**< Count of capabilities. */
584583

585584
struct nc_bind *ch_binds;
586-
pthread_mutex_t ch_bind_lock; /**< To avoid concurrent calls of poll and accept on the bound sockets **/
587585

588586
struct {
589587
NC_TRANSPORT_IMPL ti;
@@ -702,9 +700,7 @@ struct nc_server_config {
702700
struct nc_endpt {
703701
char *name; /**< Identifier of the endpoint. */
704702

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

709705
struct nc_keepalives ka; /**< TCP keepalives configuration. */
710706

@@ -1302,23 +1298,19 @@ int nc_sock_accept(int sock, int timeout, char **peer_host, uint16_t *peer_port)
13021298
int nc_sock_listen_inet(const char *address, uint16_t port);
13031299

13041300
/**
1305-
* @brief Accept a new connection on a listening socket.
1301+
* @brief Accept a new connection on any of the given Call Home binds.
13061302
*
1307-
* @param[in] endpt Optional endpoint the binds belong to (only for logging purposes).
1308-
* @param[in] binds Structure with the listening sockets.
1303+
* @param[in] binds Call Home binds to accept on.
13091304
* @param[in] bind_count Number of @p binds.
1310-
* @param[in] bind_lock Lock for avoiding concurrent poll/accept on a single bind.
13111305
* @param[in] timeout Timeout for accepting.
13121306
* @param[out] host Host of the remote peer. Can be NULL.
13131307
* @param[out] port Port of the new connection. Can be NULL.
1314-
* @param[out] idx Index of the bind that was accepted. Can be NULL.
1308+
* @param[out] bind_idx Index of the bind that was accepted. Can be NULL.
13151309
* @param[out] sock Accepted socket, if any.
1316-
* @return -1 on error.
1317-
* @return 0 on timeout.
1318-
* @return 1 if a socket was accepted.
1310+
* @return -1 on error, 0 on timeout, 1 if a socket was accepted.
13191311
*/
1320-
int nc_sock_accept_binds(struct nc_endpt *endpt, struct nc_bind *binds, uint16_t bind_count,
1321-
pthread_mutex_t *bind_lock, int timeout, char **host, uint16_t *port, uint16_t *idx, int *sock);
1312+
int nc_server_ch_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host,
1313+
uint16_t *port, uint16_t *bind_idx, int *sock);
13221314

13231315
/**
13241316
* @brief Establish a UNIX transport session.

0 commit comments

Comments
 (0)