From ccc91df0c7ae5419a62b72a4129299d08d78dfd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Fri, 30 Nov 2018 12:56:18 +0200 Subject: [PATCH] MXS-2196: Pass Listener into dcb_accept Modified the functions to use a listener instead of a DCB in the accepting process. This removes some of the dependenices that the listeners have on the DCB system. --- include/maxscale/dcb.h | 2 +- include/maxscale/listener.hh | 2 + server/core/dcb.cc | 37 +++++++++---------- server/modules/protocol/CDC/cdc.cc | 2 +- server/modules/protocol/HTTPD/httpd.cc | 2 +- .../MySQL/mariadbclient/mysql_client.cc | 2 +- .../modules/protocol/maxscaled/maxscaled.cc | 2 +- server/modules/protocol/telnetd/telnetd.cc | 2 +- 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/include/maxscale/dcb.h b/include/maxscale/dcb.h index 6518b793f..9ecca0d5c 100644 --- a/include/maxscale/dcb.h +++ b/include/maxscale/dcb.h @@ -260,7 +260,7 @@ typedef enum void dcb_global_init(); int dcb_write(DCB*, GWBUF*); -DCB* dcb_accept(DCB* listener); +DCB* dcb_accept(Listener* listener); DCB* dcb_alloc(dcb_role_t, Listener*, SERVICE* service); DCB* dcb_connect(struct server*, struct session*, const char*); int dcb_read(DCB*, GWBUF**, int); diff --git a/include/maxscale/listener.hh b/include/maxscale/listener.hh index 44fbb7abb..2426873ad 100644 --- a/include/maxscale/listener.hh +++ b/include/maxscale/listener.hh @@ -209,6 +209,8 @@ private: Listener(SERVICE* service, const std::string& name, const std::string& address, uint16_t port, const std::string& protocol, const std::string& authenticator, const std::string& auth_opts, void* auth_instance, SSL_LISTENER* ssl); + + friend DCB* dcb_accept(Listener* listener); }; /** diff --git a/server/core/dcb.cc b/server/core/dcb.cc index 083a06a8a..5892ed74a 100644 --- a/server/core/dcb.cc +++ b/server/core/dcb.cc @@ -105,7 +105,7 @@ static void dcb_log_write_failure(DCB* dcb, GWBUF* queue, int eno); static int gw_write(DCB* dcb, GWBUF* writeq, bool* stop_writing); static int gw_write_SSL(DCB* dcb, GWBUF* writeq, bool* stop_writing); static int dcb_log_errors_SSL(DCB* dcb, int ret); -static int dcb_accept_one_connection(DCB* dcb, struct sockaddr* client_conn); +static int dcb_accept_one_connection(int fd, struct sockaddr* client_conn); static int dcb_listen_create_socket_inet(const char* host, uint16_t port); static int dcb_listen_create_socket_unix(const char* path); static int dcb_set_socket_option(int sockfd, int level, int optname, void* optval, socklen_t optlen); @@ -2384,22 +2384,21 @@ int dcb_connect_SSL(DCB* dcb) * are set before returning the new DCB to the caller, or returning NULL if * no new connection could be achieved. * - * @param dcb Listener DCB that has detected new connection request + * @param listener Listener that has a new connection request + * * @return DCB - The new client DCB for the new connection, or NULL if failed */ -DCB* dcb_accept(DCB* dcb) +DCB* dcb_accept(Listener* listener) { DCB* client_dcb = NULL; int c_sock; struct sockaddr_storage client_conn; - if ((c_sock = dcb_accept_one_connection(dcb, (struct sockaddr*)&client_conn)) >= 0) + if ((c_sock = dcb_accept_one_connection(listener->m_listener->fd, (struct sockaddr*)&client_conn)) >= 0) { - dcb->stats.n_accepts++; - configure_network_socket(c_sock, client_conn.ss_family); - client_dcb = dcb_alloc(DCB_ROLE_CLIENT_HANDLER, dcb->listener, dcb->listener->service()); + client_dcb = dcb_alloc(DCB_ROLE_CLIENT_HANDLER, listener, listener->service()); if (client_dcb == NULL) { @@ -2408,7 +2407,7 @@ DCB* dcb_accept(DCB* dcb) } else { - client_dcb->service = dcb->listener->service(); + client_dcb->service = listener->service(); client_dcb->session = session_set_dummy(client_dcb); client_dcb->fd = c_sock; @@ -2418,7 +2417,7 @@ DCB* dcb_accept(DCB* dcb) // client address client_dcb->ip.ss_family = AF_UNIX; client_dcb->remote = MXS_STRDUP_A("localhost"); - client_dcb->path = MXS_STRDUP_A(dcb->path); + client_dcb->path = MXS_STRDUP_A(listener->address()); } else { @@ -2446,8 +2445,8 @@ DCB* dcb_accept(DCB* dcb) } } - client_dcb->func = dcb->listener->protocol_func(); - client_dcb->authfunc = dcb->listener->auth_func(); + client_dcb->func = listener->protocol_func(); + client_dcb->authfunc = listener->auth_func(); /** Allocate DCB specific authentication data */ if (client_dcb->authfunc.create @@ -2460,7 +2459,7 @@ DCB* dcb_accept(DCB* dcb) } /* Register downstream throttling callbacks */ - if (DCB_THROTTLING_ENABLED(dcb)) + if (DCB_THROTTLING_ENABLED(client_dcb)) { dcb_add_callback(client_dcb, DCB_REASON_HIGH_WATER, downstream_throttle_callback, NULL); dcb_add_callback(client_dcb, DCB_REASON_LOW_WATER, downstream_throttle_callback, NULL); @@ -2475,7 +2474,7 @@ DCB* dcb_accept(DCB* dcb) { client_dcb->func.connlimit(client_dcb, client_dcb->service->max_connections); } - dcb->session->close_reason = SESSION_CLOSE_TOO_MANY_CONNECTIONS; + client_dcb->session->close_reason = SESSION_CLOSE_TOO_MANY_CONNECTIONS; dcb_close(client_dcb); client_dcb = NULL; } @@ -2493,14 +2492,16 @@ DCB* dcb_accept(DCB* dcb) /** * @brief Accept a new client connection, given listener, return file descriptor * - * Up to 10 retries will be attempted in case of non-permanent errors. Calls + * Up to 10 retries will be attempted in case of non-permanent errors. Calls * the accept function and analyses the return, logging any errors and making * an appropriate return. * - * @param dcb Listener DCB that has detected new connection request + * @param fd File descriptor to accept from + * @param client_conn Output where connection information is stored + * * @return -1 for failure, or a file descriptor for the new connection */ -static int dcb_accept_one_connection(DCB* dcb, struct sockaddr* client_conn) +static int dcb_accept_one_connection(int fd, struct sockaddr* client_conn) { int c_sock; @@ -2511,9 +2512,7 @@ static int dcb_accept_one_connection(DCB* dcb, struct sockaddr* client_conn) int eno = 0; /* new connection from client */ - c_sock = accept(dcb->fd, - client_conn, - &client_len); + c_sock = accept(fd, client_conn, &client_len); eno = errno; errno = 0; diff --git a/server/modules/protocol/CDC/cdc.cc b/server/modules/protocol/CDC/cdc.cc index fe03336b1..2b57b2227 100644 --- a/server/modules/protocol/CDC/cdc.cc +++ b/server/modules/protocol/CDC/cdc.cc @@ -285,7 +285,7 @@ static int cdc_accept(DCB* listener) int n_connect = 0; DCB* client_dcb; - while ((client_dcb = dcb_accept(listener)) != NULL) + while ((client_dcb = dcb_accept(listener->listener)) != NULL) { CDC_session* client_data = NULL; CDC_protocol* protocol = NULL; diff --git a/server/modules/protocol/HTTPD/httpd.cc b/server/modules/protocol/HTTPD/httpd.cc index 108007300..ce548dd8c 100644 --- a/server/modules/protocol/HTTPD/httpd.cc +++ b/server/modules/protocol/HTTPD/httpd.cc @@ -363,7 +363,7 @@ static int httpd_accept(DCB* listener) int n_connect = 0; DCB* client_dcb; - while ((client_dcb = dcb_accept(listener)) != NULL) + while ((client_dcb = dcb_accept(listener->listener)) != NULL) { HTTPD_session* client_data = NULL; diff --git a/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc b/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc index 8390405db..07c24faba 100644 --- a/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc +++ b/server/modules/protocol/MySQL/mariadbclient/mysql_client.cc @@ -1397,7 +1397,7 @@ int gw_MySQLAccept(DCB* listener) { DCB* client_dcb; - while ((client_dcb = dcb_accept(listener)) != NULL) + while ((client_dcb = dcb_accept(listener->listener)) != NULL) { gw_process_one_new_client(client_dcb); } /**< while client_dcb != NULL */ diff --git a/server/modules/protocol/maxscaled/maxscaled.cc b/server/modules/protocol/maxscaled/maxscaled.cc index a2bf73d5e..f75e13ad5 100644 --- a/server/modules/protocol/maxscaled/maxscaled.cc +++ b/server/modules/protocol/maxscaled/maxscaled.cc @@ -352,7 +352,7 @@ static int maxscaled_accept(DCB* listener) socklen_t len = sizeof(struct ucred); struct ucred ucred; - while ((client_dcb = dcb_accept(listener)) != NULL) + while ((client_dcb = dcb_accept(listener->listener)) != NULL) { MAXSCALED* maxscaled_protocol = (MAXSCALED*)calloc(1, sizeof(MAXSCALED)); diff --git a/server/modules/protocol/telnetd/telnetd.cc b/server/modules/protocol/telnetd/telnetd.cc index 6b4f73a1f..e4b47a83d 100644 --- a/server/modules/protocol/telnetd/telnetd.cc +++ b/server/modules/protocol/telnetd/telnetd.cc @@ -281,7 +281,7 @@ static int telnetd_accept(DCB* listener) int n_connect = 0; DCB* client_dcb; - while ((client_dcb = dcb_accept(listener)) != NULL) + while ((client_dcb = dcb_accept(listener->listener)) != NULL) { TELNETD* telnetd_protocol = NULL;