/* We get here after reading the value in set/add/replace * commands. The command has been stored in c->cmd, and * the item is ready in c->item. */ void cproxy_process_upstream_ascii_nread(conn *c) { assert(c != NULL); assert(c->next == NULL); item *it = c->item; assert(it != NULL); // pthread_mutex_lock(&c->thread->stats.mutex); // c->thread->stats.slab_stats[it->slabs_clsid].set_cmds++; // pthread_mutex_unlock(&c->thread->stats.mutex); if (strncmp(ITEM_data(it) + it->nbytes - 2, "\r\n", 2) == 0) { proxy_td *ptd = c->extra; assert(ptd != NULL); cproxy_pause_upstream_for_downstream(ptd, c); } else { out_string(c, "CLIENT_ERROR bad data chunk"); } }
void cproxy_process_upstream_binary(conn *c) { cb_assert(c != NULL); cb_assert(c->cmd >= 0); cb_assert(c->next == NULL); cb_assert(c->item == NULL); cb_assert(IS_BINARY(c->protocol)); cb_assert(IS_PROXY(c->protocol)); proxy_td *ptd = c->extra; cb_assert(ptd != NULL); if (!cproxy_prep_conn_for_write(c)) { ptd->stats.stats.err_upstream_write_prep++; conn_set_state(c, conn_closing); return; } c->cmd_curr = -1; c->cmd_start = NULL; c->cmd_start_time = msec_current_time; c->cmd_retries = 0; int extlen = c->binary_header.request.extlen; int keylen = c->binary_header.request.keylen; uint32_t bodylen = c->binary_header.request.bodylen; cb_assert(bodylen >= (uint32_t) keylen + extlen); if (settings.verbose > 2) { moxi_log_write("<%d cproxy_process_upstream_binary %x %d %d %u\n", c->sfd, c->cmd, extlen, keylen, bodylen); } process_bin_noreply(c); /* Map quiet c->cmd values into non-quiet. */ if (c->cmd == PROTOCOL_BINARY_CMD_VERSION || c->cmd == PROTOCOL_BINARY_CMD_QUIT) { dispatch_bin_command(c); return; } /* Alloc an item and continue with an rest-of-body nread if */ /* necessary. The item will hold the entire request message */ /* (the header + body). */ char *ikey = "u"; int ikeylen = 1; c->item = item_alloc(ikey, ikeylen, 0, 0, sizeof(c->binary_header) + bodylen); if (c->item != NULL) { item *it = c->item; void *rb = c->rcurr; cb_assert(it->refcount == 1); memcpy(ITEM_data(it), rb, sizeof(c->binary_header)); if (bodylen > 0) { c->ritem = ITEM_data(it) + sizeof(c->binary_header); c->rlbytes = bodylen; c->substate = bin_read_set_value; conn_set_state(c, conn_nread); } else { /* Since we have no body bytes, we can go immediately to */ /* the nread completed processing step. */ if (c->binary_header.request.opcode == PROTOCOL_BINARY_CMD_SASL_LIST_MECHS) { /* TODO: One day handle more than just PLAIN sasl auth. */ write_bin_response(c, "PLAIN", 0, 0, strlen("PLAIN")); return; } cproxy_pause_upstream_for_downstream(ptd, c); } } else { if (settings.verbose > 2) { moxi_log_write("<%d cproxy_process_upstream_binary OOM\n", c->sfd); } ptd->stats.stats.err_oom++; cproxy_close_conn(c); } }
/* We get here after reading the header+body into an item. */ void cproxy_process_upstream_binary_nread(conn *c) { cb_assert(c != NULL); cb_assert(c->cmd >= 0); cb_assert(c->next == NULL); cb_assert(c->cmd_start == NULL); cb_assert(IS_BINARY(c->protocol)); cb_assert(IS_PROXY(c->protocol)); protocol_binary_request_header *header = (protocol_binary_request_header *) &c->binary_header; int extlen = header->request.extlen; int keylen = header->request.keylen; uint32_t bodylen = header->request.bodylen; if (settings.verbose > 2) { moxi_log_write("<%d cproxy_process_upstream_binary_nread %x %d %d %u\n", c->sfd, c->cmd, extlen, keylen, bodylen); } /* pthread_mutex_lock(&c->thread->stats.mutex); */ /* c->thread->stats.slab_stats[it->slabs_clsid].set_cmds++; */ /* pthread_mutex_unlock(&c->thread->stats.mutex); */ proxy_td *ptd = c->extra; cb_assert(ptd != NULL); if (header->request.opcode == PROTOCOL_BINARY_CMD_SASL_AUTH) { item *it = c->item; cb_assert(it); cproxy_sasl_plain_auth(c, (char *) ITEM_data(it)); return; } if (header->request.opcode == PROTOCOL_BINARY_CMD_SASL_STEP) { write_bin_error(c, PROTOCOL_BINARY_RESPONSE_AUTH_ERROR, 0); return; } if (c->binary_header.request.opcode == PROTOCOL_BINARY_CMD_STAT) { char *subcommand = binary_get_key(c); size_t nkey = c->binary_header.request.keylen; if (nkey == 13 && memcmp(subcommand, "proxy buckets", 13) == 0) { process_bin_proxy_stats(c); return; } } if (c->noreply) { if (settings.verbose > 2) { moxi_log_write("<%d cproxy_process_upstream_binary_nread " "corking quiet command %x %d\n", c->sfd, c->cmd, (c->corked != NULL)); } /* TODO: We currently don't support binary FLUSHQ. */ /* Rather than having the downstream connections get */ /* into a wonky state, prevent it. */ if (header->request.opcode == PROTOCOL_BINARY_CMD_FLUSHQ) { /* Note: don't use cproxy_close_conn(c), as it goes */ /* through the drive_machine() loop again. */ /* cproxy_close_conn(c); */ conn_set_state(c, conn_closing); return; } /* Hold onto or 'cork' all the binary quiet commands */ /* until there's a later non-quiet command. */ if (cproxy_binary_cork_cmd(c)) { conn_set_state(c, conn_new_cmd); } else { ptd->stats.stats.err_oom++; cproxy_close_conn(c); } return; } cb_assert(c->item == NULL || ((item *) c->item)->refcount == 1); cproxy_pause_upstream_for_downstream(ptd, c); }
void cproxy_process_upstream_ascii(conn *c, char *line) { assert(c != NULL); assert(c->next == NULL); assert(c->extra != NULL); assert(c->cmd == -1); assert(c->item == NULL); assert(line != NULL); assert(line == c->rcurr); assert(IS_ASCII(c->protocol)); assert(IS_PROXY(c->protocol)); if (settings.verbose > 2) { moxi_log_write("<%d cproxy_process_upstream_ascii %s\n", c->sfd, line); } // Snapshot rcurr, because the caller, try_read_command(), changes it. // c->cmd_curr = -1; c->cmd_start = c->rcurr; c->cmd_start_time = msec_current_time; c->cmd_retries = 0; proxy_td *ptd = c->extra; assert(ptd != NULL); /* For commands set/add/replace, we build an item and read the data * directly into it, then continue in nread_complete(). */ if (!cproxy_prep_conn_for_write(c)) { ptd->stats.stats.err_upstream_write_prep++; conn_set_state(c, conn_closing); return; } bool mcmux_command = false; bool self_command = false; /* Check for proxy pattern - A:host:port or B:host:port */ if (true == settings.enable_mcmux_mode && ((*line == 'A' || *line == 'B') && *(line + 1) == ':')) { mcmux_command = true; } else if (true == settings.enable_mcmux_mode) { self_command = true; } c->peer_protocol = 0; c->peer_host = NULL; c->peer_port = 0; if (mcmux_command) { char *peer_port = NULL; int i = 0; c->peer_protocol = (*line == 'A') ? proxy_downstream_ascii_prot : proxy_downstream_binary_prot; line += 2; c->peer_host = line; while (*line != ' ' && *line != '\0' && *line != ':' && ++i < MAX_HOSTNAME_LEN) { line++; } if (*line == '\0' || line - c->peer_host <= 0) { out_string(c, "ERROR"); moxi_log_write("Malformed request line"); return; } *line = '\0'; line++; peer_port = line; i = 0; while (*line != ' ' && *line != '\0' && ++i <= MAX_PORT_LEN) { line++; } if (*line == '\0' || line - peer_port <= 0) { out_string(c, "ERROR"); moxi_log_write("Malformed request line"); return; } c->peer_port = atoi(peer_port); *line++ = '\0'; c->cmd_start = line; } int cmd_len = 0; token_t tokens[MAX_TOKENS]; size_t ntokens = scan_tokens(line, tokens, MAX_TOKENS, &cmd_len); char *cmd = tokens[COMMAND_TOKEN].value; int cmdx = -1; int cmd_st = STATS_CMD_TYPE_REGULAR; int comm; #define SEEN(cmd_id, is_cas, cmd_len) \ cmd_st = c->noreply ? \ STATS_CMD_TYPE_QUIET : STATS_CMD_TYPE_REGULAR; \ ptd->stats.stats_cmd[cmd_st][cmd_id].seen++; \ ptd->stats.stats_cmd[cmd_st][cmd_id].read_bytes += cmd_len; \ if (is_cas) { \ ptd->stats.stats_cmd[cmd_st][cmd_id].cas++; \ } if (ntokens >= 3 && (false == self_command) && (strncmp(cmd, "get", 3) == 0)) { if (cmd[3] == 'l') { c->cmd_curr = PROTOCOL_BINARY_CMD_GETL; } else if (ntokens == 3) { // Single-key get/gets optimization. // c->cmd_curr = PROTOCOL_BINARY_CMD_GETK; } else { c->cmd_curr = PROTOCOL_BINARY_CMD_GETKQ; } // Handles get and gets. // cproxy_pause_upstream_for_downstream(ptd, c); // The cmd_len from scan_tokens might not include // all the keys, so cmd_len might not == strlen(command). // Handle read_bytes during multiget broadcast. // if (cmd[3] == 'l') { SEEN(STATS_CMD_GETL, true, 0); } else { SEEN(STATS_CMD_GET, cmd[3] == 's', 0); } } else if ((ntokens == 6 || ntokens == 7) && (false == self_command) && ((strncmp(cmd, "add", 3) == 0 && (comm = NREAD_ADD) && (cmdx = STATS_CMD_ADD) && (c->cmd_curr = PROTOCOL_BINARY_CMD_ADD)) || (strncmp(cmd, "set", 3) == 0 && (comm = NREAD_SET) && (cmdx = STATS_CMD_SET) && (c->cmd_curr = PROTOCOL_BINARY_CMD_SET)) || (strncmp(cmd, "replace", 7) == 0 && (comm = NREAD_REPLACE) && (cmdx = STATS_CMD_REPLACE) && (c->cmd_curr = PROTOCOL_BINARY_CMD_REPLACE)) || (strncmp(cmd, "prepend", 7) == 0 && (comm = NREAD_PREPEND) && (cmdx = STATS_CMD_PREPEND) && (c->cmd_curr = PROTOCOL_BINARY_CMD_PREPEND)) || (strncmp(cmd, "append", 6) == 0 && (comm = NREAD_APPEND) && (cmdx = STATS_CMD_APPEND) && (c->cmd_curr = PROTOCOL_BINARY_CMD_APPEND)))) { assert(c->item == NULL); c->item = NULL; process_update_command(c, tokens, ntokens, comm, false); if (cmdx >= 0) { item *it = c->item; if (it != NULL) { SEEN(cmdx, false, cmd_len + it->nbytes); } else { SEEN(cmdx, false, cmd_len); ptd->stats.stats_cmd[cmd_st][cmdx].misses++; } } } else if ((ntokens == 7 || ntokens == 8) && (false == self_command) && (strncmp(cmd, "cas", 3) == 0 && (comm = NREAD_CAS) && (c->cmd_curr = PROTOCOL_BINARY_CMD_SET))) { assert(c->item == NULL); c->item = NULL; process_update_command(c, tokens, ntokens, comm, true); item *it = c->item; if (it != NULL) { SEEN(STATS_CMD_CAS, true, cmd_len + it->nbytes); } else { SEEN(STATS_CMD_CAS, true, cmd_len); ptd->stats.stats_cmd[cmd_st][STATS_CMD_CAS].misses++; } } else if ((ntokens == 4 || ntokens == 5) && (false == self_command) && (strncmp(cmd, "incr", 4) == 0) && (c->cmd_curr = PROTOCOL_BINARY_CMD_INCREMENT)) { set_noreply_maybe(c, tokens, ntokens); cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_INCR, false, cmd_len); } else if ((ntokens == 4 || ntokens == 5) && (false == self_command) && (strncmp(cmd, "decr", 4) == 0) && (c->cmd_curr = PROTOCOL_BINARY_CMD_DECREMENT)) { set_noreply_maybe(c, tokens, ntokens); cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_DECR, false, cmd_len); } else if (ntokens >= 3 && ntokens <= 4 && (false == self_command) && (strncmp(cmd, "delete", 6) == 0) && (c->cmd_curr = PROTOCOL_BINARY_CMD_DELETE)) { set_noreply_maybe(c, tokens, ntokens); cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_DELETE, false, cmd_len); } else if (ntokens >= 2 && ntokens <= 4 && (false == self_command) && (strncmp(cmd, "flush_all", 9) == 0) && (c->cmd_curr = PROTOCOL_BINARY_CMD_FLUSH)) { set_noreply_maybe(c, tokens, ntokens); cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_FLUSH_ALL, false, cmd_len); } else if (ntokens >= 3 && ntokens <= 4 && (strncmp(cmd, "stats proxy", 10) == 0)) { process_stats_proxy_command(c, tokens, ntokens); SEEN(STATS_CMD_STATS, false, cmd_len); } else if (ntokens == 3 && (false == self_command) && (strcmp(cmd, "stats reset") == 0) && (c->cmd_curr = PROTOCOL_BINARY_CMD_STAT)) { cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_STATS_RESET, false, cmd_len); } else if (ntokens == 2 && (false == self_command) && (strcmp(cmd, "stats") == 0) && (c->cmd_curr = PROTOCOL_BINARY_CMD_STAT)) { // Even though we've coded to handle advanced stats // like stats cachedump, prevent those here to avoid // locking downstream servers. // cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_STATS, false, cmd_len); } else if (ntokens == 2 && (true == mcmux_command) && (strncmp(cmd, "version", 7) == 0) && (c->cmd_curr = PROTOCOL_BINARY_CMD_VERSION)) { /* downstream version command */ cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_VERSION, false, cmd_len); } else if (ntokens == 2 && (strncmp(cmd, "version", 7) == 0)) { out_string(c, "VERSION " VERSION); SEEN(STATS_CMD_VERSION, false, cmd_len); } else if ((ntokens == 3 || ntokens == 4) && (strncmp(cmd, "verbosity", 9) == 0)) { process_verbosity_command(c, tokens, ntokens); SEEN(STATS_CMD_VERBOSITY, false, cmd_len); } else if (ntokens == 2 && (strncmp(cmd, "quit", 4) == 0)) { conn_set_state(c, conn_closing); SEEN(STATS_CMD_QUIT, false, cmd_len); } else if (ntokens == 4 && (strncmp(cmd, "unl", 3) == 0) && (false == self_command) && (c->cmd_curr = PROTOCOL_BINARY_CMD_UNL)) { cproxy_pause_upstream_for_downstream(ptd, c); SEEN(STATS_CMD_UNL, false, cmd_len); } else { out_string(c, "ERROR"); SEEN(STATS_CMD_ERROR, false, cmd_len); } }