From 19feee9e0eeca02a0b2b0b7cd0b47356b803fdd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 16 Jul 2018 19:32:34 +0300 Subject: [PATCH] Remove old session command implementation The old implementation was largely. Also removed some unused macros from the header. --- include/maxscale/protocol/mysql.h | 30 -- .../MySQL/mariadbbackend/mysql_backend.cc | 12 +- server/modules/protocol/MySQL/mysql_common.cc | 296 ------------------ 3 files changed, 1 insertion(+), 337 deletions(-) diff --git a/include/maxscale/protocol/mysql.h b/include/maxscale/protocol/mysql.h index 8a8ba82e0..62ced3401 100644 --- a/include/maxscale/protocol/mysql.h +++ b/include/maxscale/protocol/mysql.h @@ -44,9 +44,6 @@ MXS_BEGIN_DECLS #define GW_MYSQL_VERSION "5.5.5-10.2.12 " MAXSCALE_VERSION "-maxscale" -#define GW_MYSQL_LOOP_TIMEOUT 300000000 -#define GW_MYSQL_READ 0 -#define GW_MYSQL_WRITE 1 #define MYSQL_HEADER_LEN 4 #define MYSQL_CHECKSUM_LEN 4 @@ -112,9 +109,6 @@ MXS_BEGIN_DECLS #define MYSQL_TABLE_MAXLEN 64 #define GW_NOINTR_CALL(A) do { errno = 0; A; } while (errno == EINTR) -#define SMALL_CHUNK 1024 -#define MAX_CHUNK SMALL_CHUNK * 8 * 4 -#define ToHex(Y) (Y>='0'&&Y<='9'?Y-'0':Y-'A'+10) #define COM_QUIT_PACKET_SIZE (4+1) struct dcb; @@ -320,19 +314,6 @@ static const mxs_mysql_cmd_t MXS_COM_UNDEFINED = (mxs_mysql_cmd_t) - 1; */ static const char* const MXS_LAST_GTID = "last_gtid"; -/** - * List of server commands, and number of response packets are stored here. - * server_command_t is used in MySQLProtocol structure, so for each DCB there is - * one MySQLProtocol and one server command list. - */ -typedef struct server_command_st -{ - mxs_mysql_cmd_t scom_cmd; - int scom_nresponse_packets; /*< packets in response */ - size_t scom_nbytes_to_read; /*< bytes left to read in current packet */ - struct server_command_st* scom_next; -} server_command_t; - /** * MySQL Protocol specific state data. * @@ -347,8 +328,6 @@ typedef struct int fd; /*< The socket descriptor */ struct dcb* owner_dcb; /*< The DCB of the socket we are running on */ mxs_mysql_cmd_t current_command; /*< Current command being executed */ - server_command_t protocol_command; /*< session command list */ - server_command_t* protocol_cmd_history; /*< session command history */ mxs_auth_state_t protocol_auth_state; /*< Authentication status */ mysql_protocol_state_t protocol_state; /*< Protocol struct status */ uint8_t scramble[MYSQL_SCRAMBLE_LEN]; /*< server scramble, created or received */ @@ -486,15 +465,6 @@ int mysql_send_custom_error(DCB *dcb, int sequence, int affected_rows, const cha int mysql_send_standard_error(DCB *dcb, int sequence, int errnum, const char *msg); int mysql_send_auth_error(DCB *dcb, int sequence, int affected_rows, const char* msg); -void protocol_add_srv_command(MySQLProtocol* p, mxs_mysql_cmd_t cmd); -void protocol_remove_srv_command(MySQLProtocol* p); -bool protocol_waits_response(MySQLProtocol* p); -mxs_mysql_cmd_t protocol_get_srv_command(MySQLProtocol* p, bool removep); -int get_stmt_nresponse_packets(GWBUF* buf, mxs_mysql_cmd_t cmd); -bool protocol_get_response_status(MySQLProtocol* p, int* npackets, size_t* nbytes); -void protocol_set_response_status(MySQLProtocol* p, int npackets, size_t nbytes); -void protocol_archive_srv_command(MySQLProtocol* p); - char* create_auth_fail_str(char *username, char *hostaddr, bool password, char *db, int); void init_response_status(GWBUF* buf, uint8_t cmd, int* npackets, size_t* nbytes); diff --git a/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc b/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc index a95f7c084..fef393f07 100644 --- a/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc +++ b/server/modules/protocol/MySQL/mariadbbackend/mysql_backend.cc @@ -432,11 +432,6 @@ static inline void prepare_for_write(DCB *dcb, GWBUF *buffer) } } - if (GWBUF_IS_TYPE_SESCMD(buffer)) - { - mxs_mysql_cmd_t cmd = static_cast(mxs_mysql_get_command(buffer)); - protocol_add_srv_command(proto, cmd); - } if (GWBUF_SHOULD_COLLECT_RESULT(buffer)) { proto->collect_result = true; @@ -1642,12 +1637,7 @@ static int gw_change_user(DCB *backend, rv = 0; goto retblock; } - /** - * Add command to backend's protocol, create artificial reply - * packet and add it to client's read buffer. - */ - protocol_add_srv_command((MySQLProtocol*)backend->protocol, - MXS_COM_CHANGE_USER); + modutil_reply_auth_error(backend, message, 0); rv = 1; } diff --git a/server/modules/protocol/MySQL/mysql_common.cc b/server/modules/protocol/MySQL/mysql_common.cc index 52f915788..7d4727d83 100644 --- a/server/modules/protocol/MySQL/mysql_common.cc +++ b/server/modules/protocol/MySQL/mysql_common.cc @@ -34,8 +34,6 @@ uint8_t null_client_sha1[MYSQL_SCRAMBLE_LEN] = ""; -static server_command_t* server_command_init(server_command_t* srvcmd, mxs_mysql_cmd_t cmd); - MYSQL_session* mysql_session_alloc() { MYSQL_session* ses = (MYSQL_session*)MXS_CALLOC(1, sizeof(MYSQL_session)); @@ -65,9 +63,6 @@ MySQLProtocol* mysql_protocol_init(DCB* dcb, int fd) p->protocol_state = MYSQL_PROTOCOL_ALLOC; p->protocol_auth_state = MXS_AUTH_STATE_INIT; p->current_command = MXS_COM_UNDEFINED; - p->protocol_command.scom_cmd = MXS_COM_UNDEFINED; - p->protocol_command.scom_nresponse_packets = 0; - p->protocol_command.scom_nbytes_to_read = 0; p->stored_query = NULL; p->extra_capabilities = 0; p->ignore_replies = 0; @@ -95,17 +90,7 @@ bool mysql_protocol_done(DCB* dcb) if (p->protocol_state == MYSQL_PROTOCOL_ACTIVE) { - server_command_t* scmd = p->protocol_cmd_history; - - while (scmd) - { - server_command_t* temp = scmd; - scmd = scmd->scom_next; - MXS_FREE(temp); - } - gwbuf_free(p->stored_query); - p->protocol_state = MYSQL_PROTOCOL_DONE; rval = true; } @@ -475,287 +460,6 @@ int mysql_send_auth_error(DCB *dcb, return sizeof(mysql_packet_header) + mysql_payload_size; } -static server_command_t* server_command_init(server_command_t* srvcmd, - mxs_mysql_cmd_t cmd) -{ - server_command_t* c; - - if (srvcmd != NULL) - { - c = srvcmd; - } - else - { - c = (server_command_t *)MXS_MALLOC(sizeof(server_command_t)); - } - if (c != NULL) - { - c->scom_cmd = cmd; - c->scom_nresponse_packets = -1; - c->scom_nbytes_to_read = 0; - c->scom_next = NULL; - } - - return c; -} - -static server_command_t* server_command_copy(server_command_t* srvcmd) -{ - server_command_t* c = (server_command_t *)MXS_MALLOC(sizeof(server_command_t)); - if (c) - { - *c = *srvcmd; - } - - return c; -} - -#define MAX_CMD_HISTORY 10 - -void protocol_archive_srv_command(MySQLProtocol* p) -{ - server_command_t* s1; - server_command_t* h1; - int len = 0; - - CHK_PROTOCOL(p); - - if (p->protocol_state != MYSQL_PROTOCOL_ACTIVE) - { - goto retblock; - } - - s1 = &p->protocol_command; -#if defined(EXTRA_SS_DEBUG) - MXS_INFO("Move command %s from fd %d to command history.", - STRPACKETTYPE(s1->scom_cmd), - p->owner_dcb->fd); -#endif - /** Copy to history list */ - if ((h1 = p->protocol_cmd_history) == NULL) - { - p->protocol_cmd_history = server_command_copy(s1); - } - else /*< scan and count history commands */ - { - len = 1; - - while (h1->scom_next != NULL) - { - h1 = h1->scom_next; - len += 1; - } - h1->scom_next = server_command_copy(s1); - } - - /** Keep history limits, remove oldest */ - if (len > MAX_CMD_HISTORY) - { - server_command_t* c = p->protocol_cmd_history; - p->protocol_cmd_history = p->protocol_cmd_history->scom_next; - MXS_FREE(c); - } - - /** Remove from command list */ - if (s1->scom_next == NULL) - { - p->protocol_command.scom_cmd = MXS_COM_UNDEFINED; - } - else - { - p->protocol_command = *(s1->scom_next); - MXS_FREE(s1->scom_next); - } - -retblock: - CHK_PROTOCOL(p); -} - - -/** - * If router expects to get separate, complete statements, add MySQL command - * to MySQLProtocol structure. It is removed when response has arrived. - */ -void protocol_add_srv_command(MySQLProtocol* p, - mxs_mysql_cmd_t cmd) -{ -#if defined(EXTRA_SS_DEBUG) - server_command_t* c; -#endif - - if (p->protocol_state != MYSQL_PROTOCOL_ACTIVE) - { - return; - } - /** this is the only server command in protocol */ - if (p->protocol_command.scom_cmd == MXS_COM_UNDEFINED) - { - /** write into structure */ - server_command_init(&p->protocol_command, cmd); - } - else - { - /** add to the end of list */ - p->protocol_command.scom_next = server_command_init(NULL, cmd); - } -#if defined(EXTRA_SS_DEBUG) - MXS_INFO("Added command %s to fd %d.", - STRPACKETTYPE(cmd), - p->owner_dcb->fd); - - c = &p->protocol_command; - - while (c != NULL && c->scom_cmd != MXS_COM_UNDEFINED) - { - MXS_INFO("fd %d : %d %s", - p->owner_dcb->fd, - c->scom_cmd, - STRPACKETTYPE(c->scom_cmd)); - c = c->scom_next; - } -#endif -} - - -/** - * If router processes separate statements, every stmt has corresponding MySQL - * command stored in MySQLProtocol structure. - * - * Remove current (=oldest) command. - */ -void protocol_remove_srv_command(MySQLProtocol* p) -{ - server_command_t* s; - - s = &p->protocol_command; -#if defined(EXTRA_SS_DEBUG) - MXS_INFO("Removed command %s from fd %d.", - STRPACKETTYPE(s->scom_cmd), - p->owner_dcb->fd); -#endif - if (s->scom_next == NULL) - { - p->protocol_command.scom_cmd = MXS_COM_UNDEFINED; - } - else - { - p->protocol_command = *(s->scom_next); - MXS_FREE(s->scom_next); - } -} - -mxs_mysql_cmd_t protocol_get_srv_command(MySQLProtocol* p, - bool removep) -{ - mxs_mysql_cmd_t cmd; - - cmd = p->protocol_command.scom_cmd; - - if (removep) - { - protocol_remove_srv_command(p); - } - MXS_DEBUG("Read command %s for fd %d.", STRPACKETTYPE(cmd), p->owner_dcb->fd); - return cmd; -} - -void mysql_num_response_packets(GWBUF *buf, uint8_t cmd, int *npackets, size_t *nbytes) -{ - uint8_t readbuf[3]; - int nparam = 0; - int nattr = 0; - - /** Read command byte */ - gwbuf_copy_data(buf, MYSQL_HEADER_LEN, 1, readbuf); - - if (readbuf[0] == 0xff) /*< error */ - { - *npackets = 1; - } - else - { - switch (cmd) - { - case MXS_COM_STMT_PREPARE: - gwbuf_copy_data(buf, 9, 2, readbuf); - nparam = gw_mysql_get_byte2(readbuf); - gwbuf_copy_data(buf, 11, 2, readbuf); - nattr = gw_mysql_get_byte2(readbuf); - *npackets = 1 + nparam + MXS_MIN(1, nparam) + nattr + MXS_MIN(nattr, 1); - break; - - case MXS_COM_QUIT: - case MXS_COM_STMT_SEND_LONG_DATA: - case MXS_COM_STMT_CLOSE: - *npackets = 0; /*< these don't reply anything */ - break; - - default: - /** - * assume that other session commands respond - * OK or ERR - */ - *npackets = 1; - break; - } - } - - gwbuf_copy_data(buf, 0, 3, readbuf); - *nbytes = gw_mysql_get_byte3(readbuf) + MYSQL_HEADER_LEN; -} - -/** - * Examine command type and the readbuf. Conclude response packet count - * from the command type or from the first packet content. Fails if read - * buffer doesn't include enough data to read the packet length. - */ -void init_response_status(GWBUF* buf, uint8_t cmd, int *npackets, size_t *nbytes_left) -{ - ss_dassert(gwbuf_length(buf) >= 3); - mysql_num_response_packets(buf, cmd, npackets, nbytes_left); - ss_dassert(*nbytes_left > 0); - ss_dassert(*npackets > 0); -} - -/** - * Read how many packets are left from current response and how many bytes there - * is still to be read from the current packet. - */ -bool protocol_get_response_status(MySQLProtocol* p, - int* npackets, - size_t* nbytes) -{ - bool succp; - - CHK_PROTOCOL(p); - - *npackets = p->protocol_command.scom_nresponse_packets; - *nbytes = (size_t)p->protocol_command.scom_nbytes_to_read; - - if (*npackets < 0 && *nbytes == 0) - { - succp = false; - } - else - { - succp = true; - } - - return succp; -} - -void protocol_set_response_status(MySQLProtocol* p, - int npackets_left, - size_t nbytes) -{ - CHK_PROTOCOL(p); - - p->protocol_command.scom_nbytes_to_read = nbytes; - ss_dassert(p->protocol_command.scom_nbytes_to_read >= 0); - - p->protocol_command.scom_nresponse_packets = npackets_left; -} - char* create_auth_failed_msg(GWBUF*readbuf, char* hostaddr, uint8_t* sha1)