Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | bd55e31 | 2010-11-11 10:55:09 +0100 | [diff] [blame] | 2 | * Stick table synchro management. |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <common/compat.h> |
| 24 | #include <common/config.h> |
| 25 | #include <common/time.h> |
| 26 | |
| 27 | #include <types/global.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 28 | #include <types/listener.h> |
| 29 | #include <types/obj_type.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 30 | #include <types/peers.h> |
| 31 | |
| 32 | #include <proto/acl.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 33 | #include <proto/channel.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 34 | #include <proto/fd.h> |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 35 | #include <proto/frontend.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 36 | #include <proto/log.h> |
| 37 | #include <proto/hdr_idx.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 38 | #include <proto/proto_tcp.h> |
| 39 | #include <proto/proto_http.h> |
| 40 | #include <proto/proxy.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 41 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 42 | #include <proto/stream.h> |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 43 | #include <proto/signal.h> |
| 44 | #include <proto/stick_table.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 45 | #include <proto/stream_interface.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 46 | #include <proto/task.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 47 | |
| 48 | |
| 49 | /*******************************/ |
| 50 | /* Current peer learning state */ |
| 51 | /*******************************/ |
| 52 | |
| 53 | /******************************/ |
| 54 | /* Current table resync state */ |
| 55 | /******************************/ |
| 56 | #define SHTABLE_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */ |
| 57 | #define SHTABLE_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */ |
| 58 | #define SHTABLE_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */ |
| 59 | #define SHTABLE_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */ |
| 60 | #define SHTABLE_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop |
| 61 | to push data to new process */ |
| 62 | |
| 63 | #define SHTABLE_RESYNC_STATEMASK (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE) |
| 64 | #define SHTABLE_RESYNC_FROMLOCAL 0x00000000 |
| 65 | #define SHTABLE_RESYNC_FROMREMOTE SHTABLE_F_RESYNC_LOCAL |
| 66 | #define SHTABLE_RESYNC_FINISHED (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE) |
| 67 | |
| 68 | /******************************/ |
| 69 | /* Remote peer teaching state */ |
| 70 | /******************************/ |
| 71 | #define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */ |
| 72 | #define PEER_F_TEACH_STAGE1 0x00000002 /* Teach state 1 complete */ |
| 73 | #define PEER_F_TEACH_STAGE2 0x00000004 /* Teach stage 2 complete */ |
| 74 | #define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */ |
| 75 | #define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */ |
| 76 | #define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */ |
| 77 | #define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */ |
| 78 | |
| 79 | #define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_STAGE1|PEER_F_TEACH_STAGE2|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */ |
| 80 | #define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE) |
| 81 | |
| 82 | |
| 83 | /**********************************/ |
| 84 | /* Peer Session IO handler states */ |
| 85 | /**********************************/ |
| 86 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 87 | enum { |
| 88 | PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */ |
| 89 | PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */ |
| 90 | PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */ |
| 91 | PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */ |
| 92 | PEER_SESS_ST_GETTABLE, /* Search into registered table for a table with same id and validate type and size */ |
| 93 | /* after this point, data were possibly exchanged */ |
| 94 | PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */ |
| 95 | PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */ |
| 96 | PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */ |
| 97 | PEER_SESS_ST_WAITMSG, /* Wait for data messages */ |
| 98 | PEER_SESS_ST_EXIT, /* Exit with status code */ |
| 99 | PEER_SESS_ST_END, /* Killed session */ |
| 100 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 101 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 102 | /***************************************************/ |
| 103 | /* Peer Session status code - part of the protocol */ |
| 104 | /***************************************************/ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 105 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 106 | #define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */ |
| 107 | #define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 108 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 109 | #define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 111 | #define PEER_SESS_SC_TRYAGAIN 300 /* try again later */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 112 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 113 | #define PEER_SESS_SC_ERRPROTO 501 /* error protocol */ |
| 114 | #define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */ |
| 115 | #define PEER_SESS_SC_ERRHOST 503 /* bad host name */ |
| 116 | #define PEER_SESS_SC_ERRPEER 504 /* unknown peer */ |
| 117 | #define PEER_SESS_SC_ERRTYPE 505 /* table key type mismatch */ |
| 118 | #define PEER_SESS_SC_ERRSIZE 506 /* table key size mismatch */ |
| 119 | #define PEER_SESS_SC_ERRTABLE 507 /* unknown table */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 120 | |
| 121 | #define PEER_SESSION_PROTO_NAME "HAProxyS" |
| 122 | |
| 123 | struct peers *peers = NULL; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 124 | static void peer_session_forceshutdown(struct stream * stream); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 125 | |
| 126 | |
| 127 | /* |
| 128 | * This prepare the data update message of the stick session <ts>, <ps> is the the peer session |
| 129 | * where the data going to be pushed, <msg> is a buffer of <size> to recieve data message content |
| 130 | */ |
Simon Horman | 9655377 | 2011-06-08 09:18:51 +0900 | [diff] [blame] | 131 | static int peer_prepare_datamsg(struct stksess *ts, struct peer_session *ps, char *msg, size_t size) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 132 | { |
| 133 | uint32_t netinteger; |
| 134 | int len; |
| 135 | /* construct message */ |
| 136 | if (ps->lastpush && ts->upd.key > ps->lastpush && (ts->upd.key - ps->lastpush) <= 127) { |
| 137 | msg[0] = 0x80 + ts->upd.key - ps->lastpush; |
| 138 | len = sizeof(char); |
| 139 | } |
| 140 | else { |
| 141 | msg[0] = 'D'; |
| 142 | netinteger = htonl(ts->upd.key); |
| 143 | memcpy(&msg[sizeof(char)], &netinteger, sizeof(netinteger)); |
| 144 | len = sizeof(char) + sizeof(netinteger); |
| 145 | } |
| 146 | |
| 147 | if (ps->table->table->type == STKTABLE_TYPE_STRING) { |
| 148 | int stlen = strlen((char *)ts->key.key); |
| 149 | |
| 150 | netinteger = htonl(strlen((char *)ts->key.key)); |
| 151 | memcpy(&msg[len], &netinteger, sizeof(netinteger)); |
| 152 | memcpy(&msg[len+sizeof(netinteger)], ts->key.key, stlen); |
| 153 | len += sizeof(netinteger) + stlen; |
| 154 | |
| 155 | } |
| 156 | else if (ps->table->table->type == STKTABLE_TYPE_INTEGER) { |
| 157 | netinteger = htonl(*((uint32_t *)ts->key.key)); |
| 158 | memcpy(&msg[len], &netinteger, sizeof(netinteger)); |
| 159 | len += sizeof(netinteger); |
| 160 | } |
| 161 | else { |
| 162 | memcpy(&msg[len], ts->key.key, ps->table->table->key_size); |
| 163 | len += ps->table->table->key_size; |
| 164 | } |
| 165 | |
| 166 | if (stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID)) |
| 167 | netinteger = htonl(stktable_data_cast(stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID), server_id)); |
| 168 | else |
| 169 | netinteger = 0; |
| 170 | |
| 171 | memcpy(&msg[len], &netinteger , sizeof(netinteger)); |
| 172 | len += sizeof(netinteger); |
| 173 | |
| 174 | return len; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /* |
| 179 | * Callback to release a session with a peer |
| 180 | */ |
Simon Horman | 9655377 | 2011-06-08 09:18:51 +0900 | [diff] [blame] | 181 | static void peer_session_release(struct stream_interface *si) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 182 | { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 183 | struct stream *s = si_strm(si); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 184 | struct appctx *appctx = objt_appctx(si->end); |
| 185 | struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 186 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 187 | /* appctx->ctx.peers.ptr is not a peer session */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 188 | if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 189 | return; |
| 190 | |
| 191 | /* peer session identified */ |
| 192 | if (ps) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 193 | if (ps->stream == s) { |
| 194 | ps->stream = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 195 | if (ps->flags & PEER_F_LEARN_ASSIGN) { |
| 196 | /* unassign current peer for learning */ |
| 197 | ps->flags &= ~(PEER_F_LEARN_ASSIGN); |
| 198 | ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); |
| 199 | |
| 200 | /* reschedule a resync */ |
| 201 | ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 202 | } |
| 203 | /* reset teaching and learning flags to 0 */ |
| 204 | ps->flags &= PEER_TEACH_RESET; |
| 205 | ps->flags &= PEER_LEARN_RESET; |
| 206 | } |
| 207 | task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* |
| 213 | * IO Handler to handle message exchance with a peer |
| 214 | */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 215 | static void peer_io_handler(struct stream_interface *si) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 216 | { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 217 | struct stream *s = si_strm(si); |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 218 | struct peers *curpeers = (struct peers *)strm_fe(s)->parent; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 219 | struct appctx *appctx = objt_appctx(si->end); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 220 | int reql = 0; |
| 221 | int repl = 0; |
| 222 | |
| 223 | while (1) { |
| 224 | switchstate: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 225 | switch(appctx->st0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 226 | case PEER_SESS_ST_ACCEPT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 227 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 228 | appctx->st0 = PEER_SESS_ST_GETVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 229 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 230 | case PEER_SESS_ST_GETVERSION: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 231 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 232 | if (reql <= 0) { /* closed or EOL not found */ |
| 233 | if (reql == 0) |
| 234 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 235 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 236 | goto switchstate; |
| 237 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 238 | if (trash.str[reql-1] != '\n') { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 239 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 240 | goto switchstate; |
| 241 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 242 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 243 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 244 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 245 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 246 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 247 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 248 | |
| 249 | /* test version */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 250 | if (strcmp(PEER_SESSION_PROTO_NAME " 1.0", trash.str) != 0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 251 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 252 | appctx->st1 = PEER_SESS_SC_ERRVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 253 | /* test protocol */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 254 | if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, strlen(PEER_SESSION_PROTO_NAME)+1) != 0) |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 255 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 256 | goto switchstate; |
| 257 | } |
| 258 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 259 | appctx->st0 = PEER_SESS_ST_GETHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 260 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 261 | case PEER_SESS_ST_GETHOST: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 262 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 263 | if (reql <= 0) { /* closed or EOL not found */ |
| 264 | if (reql == 0) |
| 265 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 266 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 267 | goto switchstate; |
| 268 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 269 | if (trash.str[reql-1] != '\n') { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 270 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 271 | goto switchstate; |
| 272 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 273 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 274 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 275 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 276 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 277 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 278 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 279 | |
| 280 | /* test hostname match */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 281 | if (strcmp(localpeer, trash.str) != 0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 282 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 283 | appctx->st1 = PEER_SESS_SC_ERRHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 284 | goto switchstate; |
| 285 | } |
| 286 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 287 | appctx->st0 = PEER_SESS_ST_GETPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 288 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 289 | case PEER_SESS_ST_GETPEER: { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 290 | struct peer *curpeer; |
| 291 | char *p; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 292 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 293 | if (reql <= 0) { /* closed or EOL not found */ |
| 294 | if (reql == 0) |
| 295 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 296 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 297 | goto switchstate; |
| 298 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 299 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 300 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 301 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 302 | goto switchstate; |
| 303 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 304 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 305 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 306 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 307 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 308 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 309 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 310 | |
| 311 | /* parse line "<peer name> <pid>" */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 312 | p = strchr(trash.str, ' '); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 313 | if (!p) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 314 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 315 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 316 | goto switchstate; |
| 317 | } |
| 318 | *p = 0; |
| 319 | |
| 320 | /* lookup known peer */ |
| 321 | for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 322 | if (strcmp(curpeer->id, trash.str) == 0) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | |
| 326 | /* if unknown peer */ |
| 327 | if (!curpeer) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 328 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 329 | appctx->st1 = PEER_SESS_SC_ERRPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 330 | goto switchstate; |
| 331 | } |
| 332 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 333 | appctx->ctx.peers.ptr = curpeer; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 334 | appctx->st0 = PEER_SESS_ST_GETTABLE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 335 | /* fall through */ |
| 336 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 337 | case PEER_SESS_ST_GETTABLE: { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 338 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 339 | struct shared_table *st; |
| 340 | struct peer_session *ps = NULL; |
| 341 | unsigned long key_type; |
| 342 | size_t key_size; |
| 343 | char *p; |
| 344 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 345 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 346 | if (reql <= 0) { /* closed or EOL not found */ |
| 347 | if (reql == 0) |
| 348 | goto out; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 349 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 350 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 351 | goto switchstate; |
| 352 | } |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 353 | /* Re init appctx->ctx.peers.ptr to null, to handle correctly a release case */ |
| 354 | appctx->ctx.peers.ptr = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 355 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 356 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 357 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 358 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 359 | goto switchstate; |
| 360 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 361 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 362 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 363 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 364 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 365 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 366 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 367 | |
| 368 | /* Parse line "<table name> <type> <size>" */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 369 | p = strchr(trash.str, ' '); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 370 | if (!p) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 371 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 372 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 373 | goto switchstate; |
| 374 | } |
| 375 | *p = 0; |
| 376 | key_type = (unsigned long)atol(p+1); |
| 377 | |
| 378 | p = strchr(p+1, ' '); |
| 379 | if (!p) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 380 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 381 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 382 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 383 | goto switchstate; |
| 384 | } |
| 385 | |
| 386 | key_size = (size_t)atoi(p); |
| 387 | for (st = curpeers->tables; st; st = st->next) { |
| 388 | /* If table name matches */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 389 | if (strcmp(st->table->id, trash.str) == 0) { |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 390 | /* Check key size mismatches, except for strings |
| 391 | * which may be truncated as long as they fit in |
| 392 | * a buffer. |
| 393 | */ |
| 394 | if (key_size != st->table->key_size && |
| 395 | (key_type != STKTABLE_TYPE_STRING || |
| 396 | 1 + 4 + 4 + key_size - 1 >= trash.size)) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 397 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 398 | appctx->st1 = PEER_SESS_SC_ERRSIZE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 399 | goto switchstate; |
| 400 | } |
| 401 | |
| 402 | /* If key type mismatches */ |
| 403 | if (key_type != st->table->type) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 404 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 405 | appctx->st1 = PEER_SESS_SC_ERRTYPE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 406 | goto switchstate; |
| 407 | } |
| 408 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 409 | /* lookup peer stream of current peer */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 410 | for (ps = st->sessions; ps; ps = ps->next) { |
| 411 | if (ps->peer == curpeer) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 412 | /* If stream already active, replaced by new one */ |
| 413 | if (ps->stream && ps->stream != s) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 414 | if (ps->peer->local) { |
| 415 | /* Local connection, reply a retry */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 416 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 417 | appctx->st1 = PEER_SESS_SC_TRYAGAIN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 418 | goto switchstate; |
| 419 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 420 | peer_session_forceshutdown(ps->stream); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 421 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 422 | ps->stream = s; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 423 | break; |
| 424 | } |
| 425 | } |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* If table not found */ |
| 431 | if (!st){ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 432 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 433 | appctx->st1 = PEER_SESS_SC_ERRTABLE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 434 | goto switchstate; |
| 435 | } |
| 436 | |
| 437 | /* If no peer session for current peer */ |
| 438 | if (!ps) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 439 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 440 | appctx->st1 = PEER_SESS_SC_ERRPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 441 | goto switchstate; |
| 442 | } |
| 443 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 444 | appctx->ctx.peers.ptr = ps; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 445 | appctx->st0 = PEER_SESS_ST_SENDSUCCESS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 446 | /* fall through */ |
| 447 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 448 | case PEER_SESS_ST_SENDSUCCESS: { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 449 | struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 450 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 451 | repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 452 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 453 | if (repl <= 0) { |
| 454 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 455 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 456 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 457 | goto switchstate; |
| 458 | } |
| 459 | |
| 460 | /* Register status code */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 461 | ps->statuscode = PEER_SESS_SC_SUCCESSCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 462 | |
| 463 | /* Awake main task */ |
| 464 | task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); |
| 465 | |
| 466 | /* Init cursors */ |
| 467 | ps->teaching_origin =ps->lastpush = ps->lastack = ps->pushack = 0; |
| 468 | ps->pushed = ps->update; |
| 469 | |
| 470 | /* Init confirm counter */ |
| 471 | ps->confirm = 0; |
| 472 | |
| 473 | /* reset teaching and learning flags to 0 */ |
| 474 | ps->flags &= PEER_TEACH_RESET; |
| 475 | ps->flags &= PEER_LEARN_RESET; |
| 476 | |
| 477 | /* if current peer is local */ |
| 478 | if (ps->peer->local) { |
| 479 | /* if table need resyncfrom local and no process assined */ |
| 480 | if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMLOCAL && |
| 481 | !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { |
| 482 | /* assign local peer for a lesson, consider lesson already requested */ |
| 483 | ps->flags |= PEER_F_LEARN_ASSIGN; |
| 484 | ps->table->flags |= (SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); |
| 485 | } |
| 486 | |
| 487 | } |
| 488 | else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE && |
| 489 | !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { |
| 490 | /* assign peer for a lesson */ |
| 491 | ps->flags |= PEER_F_LEARN_ASSIGN; |
| 492 | ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN; |
| 493 | } |
| 494 | /* switch to waiting message state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 495 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 496 | goto switchstate; |
| 497 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 498 | case PEER_SESS_ST_CONNECT: { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 499 | struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 500 | |
| 501 | /* Send headers */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 502 | repl = snprintf(trash.str, trash.size, |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 503 | PEER_SESSION_PROTO_NAME " 1.0\n%s\n%s %d\n%s %lu %d\n", |
| 504 | ps->peer->id, |
| 505 | localpeer, |
Willy Tarreau | 7b77c9f | 2012-01-07 22:52:12 +0100 | [diff] [blame] | 506 | (int)getpid(), |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 507 | ps->table->table->id, |
| 508 | ps->table->table->type, |
Willy Tarreau | bd55e31 | 2010-11-11 10:55:09 +0100 | [diff] [blame] | 509 | (int)ps->table->table->key_size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 510 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 511 | if (repl >= trash.size) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 512 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 513 | goto switchstate; |
| 514 | } |
| 515 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 516 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 517 | if (repl <= 0) { |
| 518 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 519 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 520 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 521 | goto switchstate; |
| 522 | } |
| 523 | |
| 524 | /* switch to the waiting statuscode state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 525 | appctx->st0 = PEER_SESS_ST_GETSTATUS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 526 | /* fall through */ |
| 527 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 528 | case PEER_SESS_ST_GETSTATUS: { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 529 | struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 530 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 531 | if (si_ic(si)->flags & CF_WRITE_PARTIAL) |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 532 | ps->statuscode = PEER_SESS_SC_CONNECTEDCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 533 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 534 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 535 | if (reql <= 0) { /* closed or EOL not found */ |
| 536 | if (reql == 0) |
| 537 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 538 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 539 | goto switchstate; |
| 540 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 541 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 542 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 543 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 544 | goto switchstate; |
| 545 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 546 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 547 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 548 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 549 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 550 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 551 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 552 | |
| 553 | /* Register status code */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 554 | ps->statuscode = atoi(trash.str); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 555 | |
| 556 | /* Awake main task */ |
| 557 | task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); |
| 558 | |
| 559 | /* If status code is success */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 560 | if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 561 | /* Init cursors */ |
| 562 | ps->teaching_origin = ps->lastpush = ps->lastack = ps->pushack = 0; |
| 563 | ps->pushed = ps->update; |
| 564 | |
| 565 | /* Init confirm counter */ |
| 566 | ps->confirm = 0; |
| 567 | |
| 568 | /* reset teaching and learning flags to 0 */ |
| 569 | ps->flags &= PEER_TEACH_RESET; |
| 570 | ps->flags &= PEER_LEARN_RESET; |
| 571 | |
| 572 | /* If current peer is local */ |
| 573 | if (ps->peer->local) { |
| 574 | /* Init cursors to push a resync */ |
| 575 | ps->teaching_origin = ps->pushed = ps->table->table->update; |
| 576 | /* flag to start to teach lesson */ |
| 577 | ps->flags |= PEER_F_TEACH_PROCESS; |
| 578 | |
| 579 | } |
| 580 | else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE && |
| 581 | !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { |
| 582 | /* If peer is remote and resync from remote is needed, |
| 583 | and no peer currently assigned */ |
| 584 | |
| 585 | /* assign peer for a lesson */ |
| 586 | ps->flags |= PEER_F_LEARN_ASSIGN; |
| 587 | ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN; |
| 588 | } |
| 589 | |
| 590 | } |
| 591 | else { |
| 592 | /* Status code is not success, abort */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 593 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 594 | goto switchstate; |
| 595 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 596 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 597 | /* fall through */ |
| 598 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 599 | case PEER_SESS_ST_WAITMSG: { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 600 | struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 601 | struct stksess *ts, *newts = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 602 | char c; |
| 603 | int totl = 0; |
| 604 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 605 | reql = bo_getblk(si_oc(si), (char *)&c, sizeof(c), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 606 | if (reql <= 0) /* closed or EOL not found */ |
| 607 | goto incomplete; |
| 608 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 609 | totl += reql; |
| 610 | |
| 611 | if ((c & 0x80) || (c == 'D')) { |
| 612 | /* Here we have data message */ |
| 613 | unsigned int pushack; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 614 | int srvid; |
| 615 | uint32_t netinteger; |
| 616 | |
| 617 | /* Compute update remote version */ |
| 618 | if (c & 0x80) { |
| 619 | pushack = ps->pushack + (unsigned int)(c & 0x7F); |
| 620 | } |
| 621 | else { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 622 | reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 623 | if (reql <= 0) /* closed or EOL not found */ |
| 624 | goto incomplete; |
| 625 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 626 | totl += reql; |
| 627 | pushack = ntohl(netinteger); |
| 628 | } |
| 629 | |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 630 | /* Read key. The string keys are read in two steps, the first step |
| 631 | * consists in reading whatever fits into the table directly into |
| 632 | * the pre-allocated key. The second step consists in simply |
| 633 | * draining all exceeding data. This can happen for example after a |
| 634 | * config reload with a smaller key size for the stick table than |
| 635 | * what was previously set, or when facing the impossibility to |
| 636 | * allocate a new stksess (for example when the table is full with |
| 637 | * "nopurge"). |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 638 | */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 639 | if (ps->table->table->type == STKTABLE_TYPE_STRING) { |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 640 | unsigned int to_read, to_store; |
| 641 | |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 642 | /* read size first */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 643 | reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 644 | if (reql <= 0) /* closed or EOL not found */ |
| 645 | goto incomplete; |
| 646 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 647 | totl += reql; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 648 | |
| 649 | to_store = 0; |
| 650 | to_read = ntohl(netinteger); |
| 651 | |
Willy Tarreau | 4e4292b | 2014-11-28 12:18:45 +0100 | [diff] [blame] | 652 | if (to_read + totl > si_ob(si)->size) { |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 653 | /* impossible to read a key this large, abort */ |
| 654 | reql = -1; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 655 | goto incomplete; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 656 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 657 | |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 658 | newts = stksess_new(ps->table->table, NULL); |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 659 | if (newts) |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 660 | to_store = MIN(to_read, ps->table->table->key_size - 1); |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 662 | /* we read up to two blocks, the first one goes into the key, |
| 663 | * the rest is drained into the trash. |
| 664 | */ |
| 665 | if (to_store) { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 666 | reql = bo_getblk(si_oc(si), (char *)newts->key.key, to_store, totl); |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 667 | if (reql <= 0) /* closed or incomplete */ |
| 668 | goto incomplete; |
| 669 | newts->key.key[reql] = 0; |
| 670 | totl += reql; |
| 671 | to_read -= reql; |
| 672 | } |
| 673 | if (to_read) { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 674 | reql = bo_getblk(si_oc(si), trash.str, to_read, totl); |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 675 | if (reql <= 0) /* closed or incomplete */ |
| 676 | goto incomplete; |
| 677 | totl += reql; |
| 678 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 679 | } |
| 680 | else if (ps->table->table->type == STKTABLE_TYPE_INTEGER) { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 681 | reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 682 | if (reql <= 0) /* closed or EOL not found */ |
| 683 | goto incomplete; |
Cyril Bonté | 9a60ff9 | 2014-02-16 01:07:07 +0100 | [diff] [blame] | 684 | newts = stksess_new(ps->table->table, NULL); |
| 685 | if (newts) { |
| 686 | netinteger = ntohl(netinteger); |
| 687 | memcpy(newts->key.key, &netinteger, sizeof(netinteger)); |
| 688 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 689 | totl += reql; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 690 | } |
| 691 | else { |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 692 | /* type ip or binary */ |
| 693 | newts = stksess_new(ps->table->table, NULL); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 694 | reql = bo_getblk(si_oc(si), newts ? (char *)newts->key.key : trash.str, ps->table->table->key_size, totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 695 | if (reql <= 0) /* closed or EOL not found */ |
| 696 | goto incomplete; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 697 | totl += reql; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* read server id */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 701 | reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 702 | if (reql <= 0) /* closed or EOL not found */ |
| 703 | goto incomplete; |
| 704 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 705 | totl += reql; |
| 706 | srvid = ntohl(netinteger); |
| 707 | |
| 708 | /* update entry */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 709 | if (newts) { |
| 710 | /* lookup for existing entry */ |
| 711 | ts = stktable_lookup(ps->table->table, newts); |
| 712 | if (ts) { |
| 713 | /* the entry already exist, we can free ours */ |
| 714 | stktable_touch(ps->table->table, ts, 0); |
| 715 | stksess_free(ps->table->table, newts); |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 716 | newts = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 717 | } |
| 718 | else { |
| 719 | struct eb32_node *eb; |
| 720 | |
| 721 | /* create new entry */ |
| 722 | ts = stktable_store(ps->table->table, newts, 0); |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 723 | newts = NULL; /* don't reuse it */ |
| 724 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 725 | ts->upd.key= (++ps->table->table->update)+(2^31); |
| 726 | eb = eb32_insert(&ps->table->table->updates, &ts->upd); |
| 727 | if (eb != &ts->upd) { |
| 728 | eb32_delete(eb); |
| 729 | eb32_insert(&ps->table->table->updates, &ts->upd); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* update entry */ |
| 734 | if (srvid && stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID)) |
| 735 | stktable_data_cast(stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID), server_id) = srvid; |
| 736 | ps->pushack = pushack; |
| 737 | } |
| 738 | |
| 739 | } |
| 740 | else if (c == 'R') { |
| 741 | /* Reset message: remote need resync */ |
| 742 | |
| 743 | /* reinit counters for a resync */ |
| 744 | ps->lastpush = 0; |
| 745 | ps->teaching_origin = ps->pushed = ps->table->table->update; |
| 746 | |
| 747 | /* reset teaching flags to 0 */ |
| 748 | ps->flags &= PEER_TEACH_RESET; |
| 749 | |
| 750 | /* flag to start to teach lesson */ |
| 751 | ps->flags |= PEER_F_TEACH_PROCESS; |
| 752 | } |
| 753 | else if (c == 'F') { |
| 754 | /* Finish message, all known updates have been pushed by remote */ |
| 755 | /* and remote is up to date */ |
| 756 | |
| 757 | /* If resync is in progress with remote peer */ |
| 758 | if (ps->flags & PEER_F_LEARN_ASSIGN) { |
| 759 | |
| 760 | /* unassign current peer for learning */ |
| 761 | ps->flags &= ~PEER_F_LEARN_ASSIGN; |
| 762 | ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); |
| 763 | |
| 764 | /* Consider table is now up2date, resync resync no more needed from local neither remote */ |
| 765 | ps->table->flags |= (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE); |
| 766 | } |
| 767 | /* Increase confirm counter to launch a confirm message */ |
| 768 | ps->confirm++; |
| 769 | } |
| 770 | else if (c == 'c') { |
| 771 | /* confirm message, remote peer is now up to date with us */ |
| 772 | |
| 773 | /* If stopping state */ |
| 774 | if (stopping) { |
| 775 | /* Close session, push resync no more needed */ |
| 776 | ps->flags |= PEER_F_TEACH_COMPLETE; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 777 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 778 | goto switchstate; |
| 779 | } |
| 780 | |
| 781 | /* reset teaching flags to 0 */ |
| 782 | ps->flags &= PEER_TEACH_RESET; |
| 783 | } |
| 784 | else if (c == 'C') { |
| 785 | /* Continue message, all known updates have been pushed by remote */ |
| 786 | /* but remote is not up to date */ |
| 787 | |
| 788 | /* If resync is in progress with current peer */ |
| 789 | if (ps->flags & PEER_F_LEARN_ASSIGN) { |
| 790 | |
| 791 | /* unassign current peer */ |
| 792 | ps->flags &= ~PEER_F_LEARN_ASSIGN; |
| 793 | ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); |
| 794 | |
| 795 | /* flag current peer is not up 2 date to try from an other */ |
| 796 | ps->flags |= PEER_F_LEARN_NOTUP2DATE; |
| 797 | |
| 798 | /* reschedule a resync */ |
| 799 | ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 800 | task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); |
| 801 | } |
| 802 | ps->confirm++; |
| 803 | } |
| 804 | else if (c == 'A') { |
| 805 | /* ack message */ |
| 806 | uint32_t netinteger; |
| 807 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 808 | reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 809 | if (reql <= 0) /* closed or EOL not found */ |
| 810 | goto incomplete; |
| 811 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 812 | totl += reql; |
| 813 | |
| 814 | /* Consider remote is up to date with "acked" version */ |
| 815 | ps->update = ntohl(netinteger); |
| 816 | } |
| 817 | else { |
| 818 | /* Unknown message */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 819 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 820 | goto switchstate; |
| 821 | } |
| 822 | |
| 823 | /* skip consumed message */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 824 | bo_skip(si_oc(si), totl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 825 | |
| 826 | /* loop on that state to peek next message */ |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 827 | goto switchstate; |
| 828 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 829 | incomplete: |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 830 | /* we get here when a bo_getblk() returns <= 0 in reql */ |
| 831 | |
| 832 | /* first, we may have to release newts */ |
| 833 | if (newts) { |
| 834 | stksess_free(ps->table->table, newts); |
| 835 | newts = NULL; |
| 836 | } |
| 837 | |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 838 | if (reql < 0) { |
| 839 | /* there was an error */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 840 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 841 | goto switchstate; |
| 842 | } |
| 843 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 844 | /* Nothing to read, now we start to write */ |
| 845 | |
| 846 | /* Confirm finished or partial messages */ |
| 847 | while (ps->confirm) { |
| 848 | /* There is a confirm messages to send */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 849 | repl = bi_putchr(si_ic(si), 'c'); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 850 | if (repl <= 0) { |
| 851 | /* no more write possible */ |
| 852 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 853 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 854 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 855 | goto switchstate; |
| 856 | } |
| 857 | ps->confirm--; |
| 858 | } |
| 859 | |
| 860 | /* Need to request a resync */ |
| 861 | if ((ps->flags & PEER_F_LEARN_ASSIGN) && |
| 862 | (ps->table->flags & SHTABLE_F_RESYNC_ASSIGN) && |
| 863 | !(ps->table->flags & SHTABLE_F_RESYNC_PROCESS)) { |
| 864 | /* Current peer was elected to request a resync */ |
| 865 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 866 | repl = bi_putchr(si_ic(si), 'R'); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 867 | if (repl <= 0) { |
| 868 | /* no more write possible */ |
| 869 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 870 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 871 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 872 | goto switchstate; |
| 873 | } |
| 874 | ps->table->flags |= SHTABLE_F_RESYNC_PROCESS; |
| 875 | } |
| 876 | |
| 877 | /* It remains some updates to ack */ |
| 878 | if (ps->pushack != ps->lastack) { |
| 879 | uint32_t netinteger; |
| 880 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 881 | trash.str[0] = 'A'; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 882 | netinteger = htonl(ps->pushack); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 883 | memcpy(&trash.str[1], &netinteger, sizeof(netinteger)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 884 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 885 | repl = bi_putblk(si_ic(si), trash.str, 1+sizeof(netinteger)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 886 | if (repl <= 0) { |
| 887 | /* no more write possible */ |
| 888 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 889 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 890 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 891 | goto switchstate; |
| 892 | } |
| 893 | ps->lastack = ps->pushack; |
| 894 | } |
| 895 | |
| 896 | if (ps->flags & PEER_F_TEACH_PROCESS) { |
| 897 | /* current peer was requested for a lesson */ |
| 898 | |
| 899 | if (!(ps->flags & PEER_F_TEACH_STAGE1)) { |
| 900 | /* lesson stage 1 not complete */ |
| 901 | struct eb32_node *eb; |
| 902 | |
| 903 | eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); |
| 904 | while (1) { |
| 905 | int msglen; |
| 906 | struct stksess *ts; |
| 907 | |
| 908 | if (!eb) { |
| 909 | /* flag lesson stage1 complete */ |
| 910 | ps->flags |= PEER_F_TEACH_STAGE1; |
| 911 | eb = eb32_first(&ps->table->table->updates); |
| 912 | if (eb) |
| 913 | ps->pushed = eb->key - 1; |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | ts = eb32_entry(eb, struct stksess, upd); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 918 | msglen = peer_prepare_datamsg(ts, ps, trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 919 | if (msglen) { |
| 920 | /* message to buffer */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 921 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 922 | if (repl <= 0) { |
| 923 | /* no more write possible */ |
| 924 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 925 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 926 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 927 | goto switchstate; |
| 928 | } |
| 929 | ps->lastpush = ps->pushed = ts->upd.key; |
| 930 | } |
| 931 | eb = eb32_next(eb); |
| 932 | } |
| 933 | } /* !TEACH_STAGE1 */ |
| 934 | |
| 935 | if (!(ps->flags & PEER_F_TEACH_STAGE2)) { |
| 936 | /* lesson stage 2 not complete */ |
| 937 | struct eb32_node *eb; |
| 938 | |
| 939 | eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); |
| 940 | while (1) { |
| 941 | int msglen; |
| 942 | struct stksess *ts; |
| 943 | |
| 944 | if (!eb || eb->key > ps->teaching_origin) { |
| 945 | /* flag lesson stage1 complete */ |
| 946 | ps->flags |= PEER_F_TEACH_STAGE2; |
| 947 | ps->pushed = ps->teaching_origin; |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | ts = eb32_entry(eb, struct stksess, upd); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 952 | msglen = peer_prepare_datamsg(ts, ps, trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 953 | if (msglen) { |
| 954 | /* message to buffer */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 955 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 956 | if (repl <= 0) { |
| 957 | /* no more write possible */ |
| 958 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 959 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 960 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 961 | goto switchstate; |
| 962 | } |
| 963 | ps->lastpush = ps->pushed = ts->upd.key; |
| 964 | } |
| 965 | eb = eb32_next(eb); |
| 966 | } |
| 967 | } /* !TEACH_STAGE2 */ |
| 968 | |
| 969 | if (!(ps->flags & PEER_F_TEACH_FINISHED)) { |
| 970 | /* process final lesson message */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 971 | repl = bi_putchr(si_ic(si), ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FINISHED) ? 'F' : 'C'); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 972 | if (repl <= 0) { |
| 973 | /* no more write possible */ |
| 974 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 975 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 976 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 977 | goto switchstate; |
| 978 | } |
| 979 | |
| 980 | /* flag finished message sent */ |
| 981 | ps->flags |= PEER_F_TEACH_FINISHED; |
| 982 | } /* !TEACH_FINISHED */ |
| 983 | } /* TEACH_PROCESS */ |
| 984 | |
| 985 | if (!(ps->flags & PEER_F_LEARN_ASSIGN) && |
| 986 | (int)(ps->pushed - ps->table->table->localupdate) < 0) { |
| 987 | /* Push local updates, only if no learning in progress (to avoid ping-pong effects) */ |
| 988 | struct eb32_node *eb; |
| 989 | |
| 990 | eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); |
| 991 | while (1) { |
| 992 | int msglen; |
| 993 | struct stksess *ts; |
| 994 | |
| 995 | /* push local updates */ |
| 996 | if (!eb) { |
| 997 | eb = eb32_first(&ps->table->table->updates); |
| 998 | if (!eb || ((int)(eb->key - ps->pushed) <= 0)) { |
| 999 | ps->pushed = ps->table->table->localupdate; |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | if ((int)(eb->key - ps->table->table->localupdate) > 0) { |
| 1005 | ps->pushed = ps->table->table->localupdate; |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | ts = eb32_entry(eb, struct stksess, upd); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1010 | msglen = peer_prepare_datamsg(ts, ps, trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1011 | if (msglen) { |
| 1012 | /* message to buffer */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1013 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1014 | if (repl <= 0) { |
| 1015 | /* no more write possible */ |
| 1016 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1017 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1018 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1019 | goto switchstate; |
| 1020 | } |
| 1021 | ps->lastpush = ps->pushed = ts->upd.key; |
| 1022 | } |
| 1023 | eb = eb32_next(eb); |
| 1024 | } |
| 1025 | } /* ! LEARN_ASSIGN */ |
| 1026 | /* noting more to do */ |
| 1027 | goto out; |
| 1028 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1029 | case PEER_SESS_ST_EXIT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1030 | repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1031 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1032 | if (bi_putblk(si_ic(si), trash.str, repl) == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1033 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1034 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1035 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1036 | case PEER_SESS_ST_END: { |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1037 | si_shutw(si); |
| 1038 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1039 | si_ic(si)->flags |= CF_READ_NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1040 | goto quit; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | out: |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1045 | si_update(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1046 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1047 | /* we don't want to expire timeouts while we're processing requests */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1048 | si_ic(si)->rex = TICK_ETERNITY; |
| 1049 | si_oc(si)->wex = TICK_ETERNITY; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1050 | quit: |
| 1051 | return; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1052 | full: |
| 1053 | si->flags |= SI_FL_WAIT_ROOM; |
| 1054 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1057 | static struct si_applet peer_applet = { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1058 | .obj_type = OBJ_TYPE_APPLET, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1059 | .name = "<PEER>", /* used for logging */ |
| 1060 | .fct = peer_io_handler, |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 1061 | .release = peer_session_release, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1062 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * Use this function to force a close of a peer session |
| 1066 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1067 | static void peer_session_forceshutdown(struct stream * stream) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1068 | { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1069 | struct stream_interface *oldsi = NULL; |
| 1070 | struct appctx *appctx = NULL; |
| 1071 | int i; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1072 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1073 | for (i = 0; i <= 1; i++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1074 | appctx = objt_appctx(stream->si[i].end); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1075 | if (!appctx) |
| 1076 | continue; |
| 1077 | if (appctx->applet != &peer_applet) |
| 1078 | continue; |
| 1079 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1080 | oldsi = &stream->si[i]; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1081 | break; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1084 | if (!appctx) |
| 1085 | return; |
| 1086 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1087 | /* call release to reinit resync states if needed */ |
| 1088 | peer_session_release(oldsi); |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1089 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1090 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1091 | task_wakeup(stream->task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1094 | /* Pre-configures a peers frontend to accept incoming connections */ |
| 1095 | void peers_setup_frontend(struct proxy *fe) |
| 1096 | { |
| 1097 | fe->last_change = now.tv_sec; |
| 1098 | fe->cap = PR_CAP_FE; |
| 1099 | fe->maxconn = 0; |
| 1100 | fe->conn_retries = CONN_RETRIES; |
| 1101 | fe->timeout.client = MS_TO_TICKS(5000); |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 1102 | fe->accept = frontend_accept; |
Willy Tarreau | f87ab94 | 2015-03-13 15:55:16 +0100 | [diff] [blame] | 1103 | fe->default_target = &peer_applet.obj_type; |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1104 | fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
| 1105 | } |
| 1106 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1107 | /* |
Willy Tarreau | bd55e31 | 2010-11-11 10:55:09 +0100 | [diff] [blame] | 1108 | * Create a new peer session in assigned state (connect will start automatically) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1109 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1110 | static struct stream *peer_session_create(struct peer *peer, struct peer_session *ps) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1111 | { |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1112 | struct listener *l = LIST_NEXT(&peer->peers->peers_fe->conf.listeners, struct listener *, by_fe); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1113 | struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1114 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1115 | struct session *sess; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1116 | struct stream *s; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1117 | struct task *t; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1118 | struct connection *conn; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1119 | |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1120 | sess = pool_alloc2(pool2_session); |
| 1121 | if (!sess) { |
Godbach | 430f291 | 2013-06-20 13:28:38 +0800 | [diff] [blame] | 1122 | Alert("out of memory in peer_session_create().\n"); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1123 | goto out_close; |
| 1124 | } |
| 1125 | |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1126 | sess->listener = l; |
| 1127 | sess->fe = p; |
Willy Tarreau | 7ea671b | 2015-04-04 14:46:56 +0200 | [diff] [blame] | 1128 | sess->accept_date = date; /* user-visible date for logging */ |
| 1129 | sess->tv_accept = now; /* corrected date for internal use */ |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1130 | |
| 1131 | if ((s = pool_alloc2(pool2_stream)) == NULL) { /* disable this proxy for a while */ |
| 1132 | Alert("out of memory in peer_session_create().\n"); |
| 1133 | goto out_free_sess; |
| 1134 | } |
| 1135 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1136 | LIST_ADDQ(&streams, &s->list); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1137 | LIST_INIT(&s->back_refs); |
Willy Tarreau | 2d7ec46 | 2015-02-14 14:14:57 +0100 | [diff] [blame] | 1138 | LIST_INIT(&s->buffer_wait); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1139 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1140 | s->flags = SF_ASSIGNED|SF_ADDR_SET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1141 | |
| 1142 | /* if this session comes from a known monitoring system, we want to ignore |
| 1143 | * it as soon as possible, which means closing it immediately for TCP. |
| 1144 | */ |
| 1145 | if ((t = task_new()) == NULL) { /* disable this proxy for a while */ |
Godbach | 430f291 | 2013-06-20 13:28:38 +0800 | [diff] [blame] | 1146 | Alert("out of memory in peer_session_create().\n"); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1147 | goto out_free_strm; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | ps->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1151 | ps->statuscode = PEER_SESS_SC_CONNECTCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1152 | |
| 1153 | t->process = l->handler; |
| 1154 | t->context = s; |
| 1155 | t->nice = l->nice; |
| 1156 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1157 | s->task = t; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1158 | s->sess = sess; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1159 | s->be = s->sess->fe; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1160 | s->req.buf = s->res.buf = NULL; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1161 | s->req_cap = NULL; |
| 1162 | s->res_cap = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1163 | |
Willy Tarreau | a5f5d8d | 2014-11-28 11:26:07 +0100 | [diff] [blame] | 1164 | s->si[0].flags = SI_FL_NONE; |
| 1165 | s->si[1].flags = SI_FL_ISBACK; |
| 1166 | |
Willy Tarreau | 819d332 | 2014-11-28 12:12:34 +0100 | [diff] [blame] | 1167 | si_reset(&s->si[0]); |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1168 | si_set_state(&s->si[0], SI_ST_EST); |
| 1169 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1170 | if (s->sess->fe->options2 & PR_O2_INDEPSTR) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1171 | s->si[0].flags |= SI_FL_INDEP_STR; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1172 | |
Willy Tarreau | 1fbe1c9 | 2013-12-01 09:35:41 +0100 | [diff] [blame] | 1173 | appctx = stream_int_register_handler(&s->si[0], &peer_applet); |
| 1174 | if (!appctx) |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1175 | goto out_free_task; |
| 1176 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1177 | appctx->st0 = PEER_SESS_ST_CONNECT; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1178 | appctx->ctx.peers.ptr = (void *)ps; |
Willy Tarreau | 40606ab | 2015-04-03 18:08:29 +0200 | [diff] [blame] | 1179 | s->sess->origin = &appctx->obj_type; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1180 | |
Willy Tarreau | 819d332 | 2014-11-28 12:12:34 +0100 | [diff] [blame] | 1181 | si_reset(&s->si[1]); |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1182 | |
| 1183 | /* initiate an outgoing connection */ |
| 1184 | si_set_state(&s->si[1], SI_ST_ASS); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1185 | s->si[1].conn_retries = p->conn_retries; |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1186 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1187 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 1188 | s->si[1].flags |= SI_FL_INDEP_STR; |
| 1189 | |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1190 | /* automatically prepare the stream interface to connect to the |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1191 | * pre-initialized connection in si->conn. |
| 1192 | */ |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1193 | if (unlikely((conn = conn_new()) == NULL)) |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1194 | goto out_free_strm; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1195 | |
| 1196 | conn_prepare(conn, peer->proto, peer->xprt); |
| 1197 | si_attach_conn(&s->si[1], conn); |
| 1198 | |
| 1199 | conn->target = s->target = &s->be->obj_type; |
| 1200 | memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to)); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1201 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1202 | stream_init_srv_conn(s); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1203 | s->pend_pos = NULL; |
| 1204 | |
| 1205 | /* init store persistence */ |
| 1206 | s->store_count = 0; |
Willy Tarreau | d5ca9ab | 2013-05-28 17:40:25 +0200 | [diff] [blame] | 1207 | memset(s->stkctr, 0, sizeof(s->stkctr)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1208 | |
| 1209 | /* FIXME: the logs are horribly complicated now, because they are |
Willy Tarreau | ae727bf | 2013-10-01 17:06:10 +0200 | [diff] [blame] | 1210 | * defined in <p>, <p>, and later <be> and <be>. We still initialize |
| 1211 | * a few of them to help troubleshooting (eg: show sess shows them). |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1212 | */ |
| 1213 | |
| 1214 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1215 | s->logs.level = 0; |
Willy Tarreau | 7ea671b | 2015-04-04 14:46:56 +0200 | [diff] [blame] | 1216 | s->logs.accept_date = sess->accept_date; /* user-visible date for logging */ |
| 1217 | s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1218 | s->do_log = NULL; |
| 1219 | |
| 1220 | /* default error reporting function, may be changed by analysers */ |
| 1221 | s->srv_error = default_srv_error; |
| 1222 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1223 | s->uniq_id = 0; |
Willy Tarreau | bd83314 | 2012-05-08 15:51:44 +0200 | [diff] [blame] | 1224 | s->unique_id = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1225 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1226 | s->txn = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1227 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1228 | channel_init(&s->req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1229 | s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1230 | |
| 1231 | /* activate default analysers enabled for this listener */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1232 | s->req.analysers = l->analysers; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1233 | |
| 1234 | /* note: this should not happen anymore since there's always at least the switching rules */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1235 | if (!s->req.analysers) { |
| 1236 | channel_auto_connect(&s->req);/* don't wait to establish connection */ |
| 1237 | channel_auto_close(&s->req);/* let the producer forward close requests */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1240 | s->req.rto = s->sess->fe->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1241 | s->req.wto = s->be->timeout.server; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1242 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1243 | channel_init(&s->res); |
Willy Tarreau | ef573c0 | 2014-11-28 14:17:09 +0100 | [diff] [blame] | 1244 | s->res.flags |= CF_ISRESP; |
| 1245 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1246 | s->res.rto = s->be->timeout.server; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1247 | s->res.wto = s->sess->fe->timeout.client; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1248 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1249 | s->req.rex = TICK_ETERNITY; |
| 1250 | s->req.wex = TICK_ETERNITY; |
| 1251 | s->req.analyse_exp = TICK_ETERNITY; |
| 1252 | s->res.rex = TICK_ETERNITY; |
| 1253 | s->res.wex = TICK_ETERNITY; |
| 1254 | s->res.analyse_exp = TICK_ETERNITY; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1255 | t->expire = TICK_ETERNITY; |
| 1256 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1257 | s->res.flags |= CF_READ_DONTWAIT; |
Willy Tarreau | 696a291 | 2014-11-24 11:36:57 +0100 | [diff] [blame] | 1258 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1259 | /* it is important not to call the wakeup function directly but to |
| 1260 | * pass through task_wakeup(), because this one knows how to apply |
| 1261 | * priorities to tasks. |
| 1262 | */ |
| 1263 | task_wakeup(t, TASK_WOKEN_INIT); |
| 1264 | |
| 1265 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 1266 | p->feconn++;/* beconn will be increased later */ |
| 1267 | jobs++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1268 | if (!(s->sess->listener->options & LI_O_UNLIMITED)) |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1269 | actconn++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1270 | totalconn++; |
| 1271 | |
| 1272 | return s; |
| 1273 | |
| 1274 | /* Error unrolling */ |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 1275 | out_free_task: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1276 | task_free(t); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1277 | out_free_strm: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1278 | LIST_DEL(&s->list); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1279 | pool_free2(pool2_stream, s); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1280 | out_free_sess: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame^] | 1281 | session_free(sess); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1282 | out_close: |
| 1283 | return s; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| 1287 | * Task processing function to manage re-connect and peer session |
| 1288 | * tasks wakeup on local update. |
| 1289 | */ |
Simon Horman | 9655377 | 2011-06-08 09:18:51 +0900 | [diff] [blame] | 1290 | static struct task *process_peer_sync(struct task * task) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1291 | { |
| 1292 | struct shared_table *st = (struct shared_table *)task->context; |
| 1293 | struct peer_session *ps; |
| 1294 | |
| 1295 | task->expire = TICK_ETERNITY; |
| 1296 | |
| 1297 | if (!stopping) { |
| 1298 | /* Normal case (not soft stop)*/ |
| 1299 | if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMLOCAL) && |
| 1300 | (!nb_oldpids || tick_is_expired(st->resync_timeout, now_ms)) && |
| 1301 | !(st->flags & SHTABLE_F_RESYNC_ASSIGN)) { |
| 1302 | /* Resync from local peer needed |
| 1303 | no peer was assigned for the lesson |
| 1304 | and no old local peer found |
| 1305 | or resync timeout expire */ |
| 1306 | |
| 1307 | /* flag no more resync from local, to try resync from remotes */ |
| 1308 | st->flags |= SHTABLE_F_RESYNC_LOCAL; |
| 1309 | |
| 1310 | /* reschedule a resync */ |
| 1311 | st->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 1312 | } |
| 1313 | |
| 1314 | /* For each session */ |
| 1315 | for (ps = st->sessions; ps; ps = ps->next) { |
| 1316 | /* For each remote peers */ |
| 1317 | if (!ps->peer->local) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1318 | if (!ps->stream) { |
| 1319 | /* no active stream */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1320 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1321 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
| 1322 | ((ps->statuscode == PEER_SESS_SC_CONNECTCODE || |
| 1323 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1324 | tick_is_expired(ps->reconnect, now_ms))) { |
| 1325 | /* connection never tried |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1326 | * or previous stream established with success |
| 1327 | * or previous stream failed during connection |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1328 | * and reconnection timer is expired */ |
| 1329 | |
| 1330 | /* retry a connect */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1331 | ps->stream = peer_session_create(ps->peer, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1332 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1333 | else if (ps->statuscode == PEER_SESS_SC_CONNECTCODE || |
| 1334 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1335 | /* If previous session failed during connection |
| 1336 | * but reconnection timer is not expired */ |
| 1337 | |
| 1338 | /* reschedule task for reconnect */ |
| 1339 | task->expire = tick_first(task->expire, ps->reconnect); |
| 1340 | } |
| 1341 | /* else do nothing */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1342 | } /* !ps->stream */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1343 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1344 | /* current stream is active and established */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1345 | if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE) && |
| 1346 | !(st->flags & SHTABLE_F_RESYNC_ASSIGN) && |
| 1347 | !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) { |
| 1348 | /* Resync from a remote is needed |
| 1349 | * and no peer was assigned for lesson |
| 1350 | * and current peer may be up2date */ |
| 1351 | |
| 1352 | /* assign peer for the lesson */ |
| 1353 | ps->flags |= PEER_F_LEARN_ASSIGN; |
| 1354 | st->flags |= SHTABLE_F_RESYNC_ASSIGN; |
| 1355 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1356 | /* awake peer stream task to handle a request of resync */ |
| 1357 | task_wakeup(ps->stream->task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1358 | } |
| 1359 | else if ((int)(ps->pushed - ps->table->table->localupdate) < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1360 | /* awake peer stream task to push local updates */ |
| 1361 | task_wakeup(ps->stream->task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1362 | } |
| 1363 | /* else do nothing */ |
| 1364 | } /* SUCCESSCODE */ |
| 1365 | } /* !ps->peer->local */ |
| 1366 | } /* for */ |
| 1367 | |
| 1368 | /* Resync from remotes expired: consider resync is finished */ |
| 1369 | if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE) && |
| 1370 | !(st->flags & SHTABLE_F_RESYNC_ASSIGN) && |
| 1371 | tick_is_expired(st->resync_timeout, now_ms)) { |
| 1372 | /* Resync from remote peer needed |
| 1373 | * no peer was assigned for the lesson |
| 1374 | * and resync timeout expire */ |
| 1375 | |
| 1376 | /* flag no more resync from remote, consider resync is finished */ |
| 1377 | st->flags |= SHTABLE_F_RESYNC_REMOTE; |
| 1378 | } |
| 1379 | |
| 1380 | if ((st->flags & SHTABLE_RESYNC_STATEMASK) != SHTABLE_RESYNC_FINISHED) { |
| 1381 | /* Resync not finished*/ |
| 1382 | /* reschedule task to resync timeout, to ended resync if needed */ |
| 1383 | task->expire = tick_first(task->expire, st->resync_timeout); |
| 1384 | } |
| 1385 | } /* !stopping */ |
| 1386 | else { |
| 1387 | /* soft stop case */ |
| 1388 | if (task->state & TASK_WOKEN_SIGNAL) { |
| 1389 | /* We've just recieved the signal */ |
| 1390 | if (!(st->flags & SHTABLE_F_DONOTSTOP)) { |
| 1391 | /* add DO NOT STOP flag if not present */ |
| 1392 | jobs++; |
| 1393 | st->flags |= SHTABLE_F_DONOTSTOP; |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 1394 | st->table->syncing++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /* disconnect all connected peers */ |
| 1398 | for (ps = st->sessions; ps; ps = ps->next) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1399 | if (ps->stream) { |
| 1400 | peer_session_forceshutdown(ps->stream); |
| 1401 | ps->stream = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | ps = st->local_session; |
| 1406 | |
| 1407 | if (ps->flags & PEER_F_TEACH_COMPLETE) { |
| 1408 | if (st->flags & SHTABLE_F_DONOTSTOP) { |
| 1409 | /* resync of new process was complete, current process can die now */ |
| 1410 | jobs--; |
| 1411 | st->flags &= ~SHTABLE_F_DONOTSTOP; |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 1412 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1413 | } |
| 1414 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1415 | else if (!ps->stream) { |
| 1416 | /* If stream is not active */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1417 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1418 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
| 1419 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE || |
| 1420 | ps->statuscode == PEER_SESS_SC_TRYAGAIN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1421 | /* connection never tried |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1422 | * or previous stream was successfully established |
| 1423 | * or previous stream tcp connect success but init state incomplete |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1424 | * or during previous connect, peer replies a try again statuscode */ |
| 1425 | |
| 1426 | /* connect to the peer */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1427 | ps->stream = peer_session_create(ps->peer, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1428 | } |
| 1429 | else { |
| 1430 | /* Other error cases */ |
| 1431 | if (st->flags & SHTABLE_F_DONOTSTOP) { |
| 1432 | /* unable to resync new process, current process can die now */ |
| 1433 | jobs--; |
| 1434 | st->flags &= ~SHTABLE_F_DONOTSTOP; |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 1435 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1439 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1440 | (int)(ps->pushed - ps->table->table->localupdate) < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1441 | /* current stream active and established |
| 1442 | awake stream to push remaining local updates */ |
| 1443 | task_wakeup(ps->stream->task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1444 | } |
| 1445 | } /* stopping */ |
| 1446 | /* Wakeup for re-connect */ |
| 1447 | return task; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Function used to register a table for sync on a group of peers |
| 1452 | * |
| 1453 | */ |
| 1454 | void peers_register_table(struct peers *peers, struct stktable *table) |
| 1455 | { |
| 1456 | struct shared_table *st; |
| 1457 | struct peer * curpeer; |
| 1458 | struct peer_session *ps; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1459 | struct listener *listener; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1460 | |
| 1461 | st = (struct shared_table *)calloc(1,sizeof(struct shared_table)); |
| 1462 | st->table = table; |
| 1463 | st->next = peers->tables; |
| 1464 | st->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 1465 | peers->tables = st; |
| 1466 | |
| 1467 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
| 1468 | ps = (struct peer_session *)calloc(1,sizeof(struct peer_session)); |
| 1469 | ps->table = st; |
| 1470 | ps->peer = curpeer; |
| 1471 | if (curpeer->local) |
| 1472 | st->local_session = ps; |
| 1473 | ps->next = st->sessions; |
| 1474 | ps->reconnect = now_ms; |
| 1475 | st->sessions = ps; |
| 1476 | peers->peers_fe->maxconn += 3; |
| 1477 | } |
| 1478 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1479 | list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe) |
| 1480 | listener->maxconn = peers->peers_fe->maxconn; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1481 | st->sync_task = task_new(); |
| 1482 | st->sync_task->process = process_peer_sync; |
| 1483 | st->sync_task->expire = TICK_ETERNITY; |
| 1484 | st->sync_task->context = (void *)st; |
| 1485 | table->sync_task =st->sync_task; |
| 1486 | signal_register_task(0, table->sync_task, 0); |
| 1487 | task_wakeup(st->sync_task, TASK_WOKEN_INIT); |
| 1488 | } |
| 1489 | |