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