Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1 | /* |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2 | * Peer 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 | /******************************/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 55 | /* Current peers section resync state */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 56 | /******************************/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 57 | #define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */ |
| 58 | #define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */ |
| 59 | #define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */ |
| 60 | #define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */ |
| 61 | #define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 62 | to push data to new process */ |
| 63 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 64 | #define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE) |
| 65 | #define PEERS_RESYNC_FROMLOCAL 0x00000000 |
| 66 | #define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL |
| 67 | #define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE) |
| 68 | |
| 69 | /***********************************/ |
| 70 | /* Current shared table sync state */ |
| 71 | /***********************************/ |
| 72 | #define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */ |
| 73 | #define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 74 | |
| 75 | /******************************/ |
| 76 | /* Remote peer teaching state */ |
| 77 | /******************************/ |
| 78 | #define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 79 | #define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */ |
| 80 | #define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */ |
| 81 | #define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */ |
| 82 | #define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */ |
| 83 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 84 | #define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 85 | #define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE) |
| 86 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 87 | /*****************************/ |
| 88 | /* Sync message class */ |
| 89 | /*****************************/ |
| 90 | enum { |
| 91 | PEER_MSG_CLASS_CONTROL = 0, |
| 92 | PEER_MSG_CLASS_ERROR, |
| 93 | PEER_MSG_CLASS_STICKTABLE = 10, |
| 94 | PEER_MSG_CLASS_RESERVED = 255, |
| 95 | }; |
| 96 | |
| 97 | /*****************************/ |
| 98 | /* control message types */ |
| 99 | /*****************************/ |
| 100 | enum { |
| 101 | PEER_MSG_CTRL_RESYNCREQ = 0, |
| 102 | PEER_MSG_CTRL_RESYNCFINISHED, |
| 103 | PEER_MSG_CTRL_RESYNCPARTIAL, |
| 104 | PEER_MSG_CTRL_RESYNCCONFIRM, |
| 105 | }; |
| 106 | |
| 107 | /*****************************/ |
| 108 | /* error message types */ |
| 109 | /*****************************/ |
| 110 | enum { |
| 111 | PEER_MSG_ERR_PROTOCOL = 0, |
| 112 | PEER_MSG_ERR_SIZELIMIT, |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | /*******************************/ |
| 117 | /* stick table sync mesg types */ |
| 118 | /* Note: ids >= 128 contains */ |
| 119 | /* id message cotains data */ |
| 120 | /*******************************/ |
| 121 | enum { |
| 122 | PEER_MSG_STKT_UPDATE = 128, |
| 123 | PEER_MSG_STKT_INCUPDATE, |
| 124 | PEER_MSG_STKT_DEFINE, |
| 125 | PEER_MSG_STKT_SWITCH, |
| 126 | PEER_MSG_STKT_ACK, |
| 127 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 128 | |
| 129 | /**********************************/ |
| 130 | /* Peer Session IO handler states */ |
| 131 | /**********************************/ |
| 132 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 133 | enum { |
| 134 | PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */ |
| 135 | PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */ |
| 136 | PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */ |
| 137 | PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 138 | /* after this point, data were possibly exchanged */ |
| 139 | PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */ |
| 140 | PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */ |
| 141 | PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */ |
| 142 | PEER_SESS_ST_WAITMSG, /* Wait for data messages */ |
| 143 | PEER_SESS_ST_EXIT, /* Exit with status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 144 | PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */ |
| 145 | PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 146 | PEER_SESS_ST_END, /* Killed session */ |
| 147 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 148 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 149 | /***************************************************/ |
| 150 | /* Peer Session status code - part of the protocol */ |
| 151 | /***************************************************/ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 153 | #define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */ |
| 154 | #define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 155 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 156 | #define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 157 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 158 | #define PEER_SESS_SC_TRYAGAIN 300 /* try again later */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 160 | #define PEER_SESS_SC_ERRPROTO 501 /* error protocol */ |
| 161 | #define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */ |
| 162 | #define PEER_SESS_SC_ERRHOST 503 /* bad host name */ |
| 163 | #define PEER_SESS_SC_ERRPEER 504 /* unknown peer */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 164 | |
| 165 | #define PEER_SESSION_PROTO_NAME "HAProxyS" |
| 166 | |
| 167 | struct peers *peers = NULL; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 168 | static void peer_session_forceshutdown(struct stream * stream); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 169 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 170 | int intencode(uint64_t i, char **str) { |
| 171 | int idx = 0; |
| 172 | unsigned char *msg; |
| 173 | |
| 174 | if (!*str) |
| 175 | return 0; |
| 176 | |
| 177 | msg = (unsigned char *)*str; |
| 178 | if (i < 240) { |
| 179 | msg[0] = (unsigned char)i; |
| 180 | *str = (char *)&msg[idx+1]; |
| 181 | return (idx+1); |
| 182 | } |
| 183 | |
| 184 | msg[idx] =(unsigned char)i | 240; |
| 185 | i = (i - 240) >> 4; |
| 186 | while (i >= 128) { |
| 187 | msg[++idx] = (unsigned char)i | 128; |
| 188 | i = (i - 128) >> 7; |
| 189 | } |
| 190 | msg[++idx] = (unsigned char)i; |
| 191 | *str = (char *)&msg[idx+1]; |
| 192 | return (idx+1); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* This function returns the decoded integer or 0 |
| 197 | if decode failed |
| 198 | *str point on the beginning of the integer to decode |
| 199 | at the end of decoding *str point on the end of the |
| 200 | encoded integer or to null if end is reached */ |
| 201 | uint64_t intdecode(char **str, char *end) { |
| 202 | uint64_t i; |
| 203 | int idx = 0; |
| 204 | unsigned char *msg; |
| 205 | |
| 206 | if (!*str) |
| 207 | return 0; |
| 208 | |
| 209 | msg = (unsigned char *)*str; |
| 210 | if (msg >= (unsigned char *)end) { |
| 211 | *str = NULL; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | if (msg[idx] < 240) { |
| 216 | *str = (char *)&msg[idx+1]; |
| 217 | return msg[idx]; |
| 218 | } |
| 219 | i = msg[idx]; |
| 220 | do { |
| 221 | idx++; |
| 222 | if (msg >= (unsigned char *)end) { |
| 223 | *str = NULL; |
| 224 | return 0; |
| 225 | } |
| 226 | i += (uint64_t)msg[idx] << (4 + 7*(idx-1)); |
| 227 | } |
| 228 | while (msg[idx] > 128); |
| 229 | *str = (char *)&msg[idx+1]; |
| 230 | return i; |
| 231 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 232 | |
| 233 | /* |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 234 | * This prepare the data update message on the stick session <ts>, <st> is the considered |
| 235 | * stick table. |
| 236 | * <msg> is a buffer of <size> to recieve data message content |
| 237 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 238 | * check size) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 239 | */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 240 | static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size, int use_identifier) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 241 | { |
| 242 | uint32_t netinteger; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 243 | unsigned short datalen; |
| 244 | char *cursor, *datamsg; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 245 | unsigned int data_type; |
| 246 | void *data_ptr; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 247 | |
| 248 | cursor = datamsg = msg + 1 + 5; |
| 249 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 250 | /* construct message */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 251 | |
| 252 | /* check if we need to send the update identifer */ |
| 253 | if (st->last_pushed && ts->upd.key > st->last_pushed && (ts->upd.key - st->last_pushed) == 1) { |
| 254 | use_identifier = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 255 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 256 | |
| 257 | /* encode update identifier if needed */ |
| 258 | if (use_identifier) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 259 | netinteger = htonl(ts->upd.key); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 260 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 261 | cursor += sizeof(netinteger); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 264 | /* encode the key */ |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 265 | if (st->table->type == SMP_T_STR) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 266 | int stlen = strlen((char *)ts->key.key); |
| 267 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 268 | intencode(stlen, &cursor); |
| 269 | memcpy(cursor, ts->key.key, stlen); |
| 270 | cursor += stlen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 271 | } |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 272 | else if (st->table->type == SMP_T_SINT) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 273 | netinteger = htonl(*((uint32_t *)ts->key.key)); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 274 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 275 | cursor += sizeof(netinteger); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 276 | } |
| 277 | else { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 278 | memcpy(cursor, ts->key.key, st->table->key_size); |
| 279 | cursor += st->table->key_size; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 282 | /* encode values */ |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 283 | for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 284 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 285 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 286 | if (data_ptr) { |
| 287 | switch (stktable_data_types[data_type].std_type) { |
| 288 | case STD_T_SINT: { |
| 289 | int data; |
| 290 | |
| 291 | data = stktable_data_cast(data_ptr, std_t_sint); |
| 292 | intencode(data, &cursor); |
| 293 | break; |
| 294 | } |
| 295 | case STD_T_UINT: { |
| 296 | unsigned int data; |
| 297 | |
| 298 | data = stktable_data_cast(data_ptr, std_t_uint); |
| 299 | intencode(data, &cursor); |
| 300 | break; |
| 301 | } |
| 302 | case STD_T_ULL: { |
| 303 | unsigned long long data; |
| 304 | |
| 305 | data = stktable_data_cast(data_ptr, std_t_ull); |
| 306 | intencode(data, &cursor); |
| 307 | break; |
| 308 | } |
| 309 | case STD_T_FRQP: { |
| 310 | struct freq_ctr_period *frqp; |
| 311 | |
| 312 | frqp = &stktable_data_cast(data_ptr, std_t_frqp); |
| 313 | intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor); |
| 314 | intencode(frqp->curr_ctr, &cursor); |
| 315 | intencode(frqp->prev_ctr, &cursor); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* Compute datalen */ |
| 323 | datalen = (cursor - datamsg); |
| 324 | |
| 325 | /* prepare message header */ |
| 326 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
| 327 | if (use_identifier) |
| 328 | msg[1] = PEER_MSG_STKT_UPDATE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 329 | else |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 330 | msg[1] = PEER_MSG_STKT_INCUPDATE; |
| 331 | |
| 332 | cursor = &msg[2]; |
| 333 | intencode(datalen, &cursor); |
| 334 | |
| 335 | /* move data after header */ |
| 336 | memmove(cursor, datamsg, datalen); |
| 337 | |
| 338 | /* return header size + data_len */ |
| 339 | return (cursor - msg) + datalen; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * This prepare the switch table message to targeted share table <st>. |
| 344 | * <msg> is a buffer of <size> to recieve data message content |
| 345 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 346 | * check size) |
| 347 | */ |
| 348 | static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size) |
| 349 | { |
| 350 | int len; |
| 351 | unsigned short datalen; |
| 352 | char *cursor, *datamsg; |
| 353 | uint64_t data = 0; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 354 | unsigned int data_type; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 355 | |
| 356 | cursor = datamsg = msg + 2 + 5; |
| 357 | |
| 358 | /* Encode data */ |
| 359 | |
| 360 | /* encode local id */ |
| 361 | intencode(st->local_id, &cursor); |
| 362 | |
| 363 | /* encode table name */ |
| 364 | len = strlen(st->table->id); |
| 365 | intencode(len, &cursor); |
| 366 | memcpy(cursor, st->table->id, len); |
| 367 | cursor += len; |
| 368 | |
| 369 | /* encode table type */ |
| 370 | |
| 371 | intencode(st->table->type, &cursor); |
| 372 | |
| 373 | /* encode table key size */ |
| 374 | intencode(st->table->key_size, &cursor); |
| 375 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 376 | /* encode available known data types in table */ |
| 377 | for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) { |
| 378 | if (st->table->data_ofs[data_type]) { |
| 379 | switch (stktable_data_types[data_type].std_type) { |
| 380 | case STD_T_SINT: |
| 381 | case STD_T_UINT: |
| 382 | case STD_T_ULL: |
| 383 | case STD_T_FRQP: |
| 384 | data |= 1 << data_type; |
| 385 | break; |
| 386 | } |
| 387 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 388 | } |
| 389 | intencode(data, &cursor); |
| 390 | |
| 391 | /* Compute datalen */ |
| 392 | datalen = (cursor - datamsg); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 393 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 394 | /* prepare message header */ |
| 395 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
| 396 | msg[1] = PEER_MSG_STKT_DEFINE; |
| 397 | cursor = &msg[2]; |
| 398 | intencode(datalen, &cursor); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 399 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 400 | /* move data after header */ |
| 401 | memmove(cursor, datamsg, datalen); |
| 402 | |
| 403 | /* return header size + data_len */ |
| 404 | return (cursor - msg) + datalen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 407 | /* |
| 408 | * This prepare the acknowledge message on the stick session <ts>, <st> is the considered |
| 409 | * stick table. |
| 410 | * <msg> is a buffer of <size> to recieve data message content |
| 411 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 412 | * check size) |
| 413 | */ |
| 414 | static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size) |
| 415 | { |
| 416 | unsigned short datalen; |
| 417 | char *cursor, *datamsg; |
| 418 | uint32_t netinteger; |
| 419 | |
| 420 | cursor = datamsg = trash.str + 2 + 5; |
| 421 | |
| 422 | intencode(st->remote_id, &cursor); |
| 423 | netinteger = htonl(st->last_get); |
| 424 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 425 | cursor += sizeof(netinteger); |
| 426 | |
| 427 | /* Compute datalen */ |
| 428 | datalen = (cursor - datamsg); |
| 429 | |
| 430 | /* prepare message header */ |
| 431 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
Emeric Brun | e1ab808 | 2015-08-21 11:48:54 +0200 | [diff] [blame] | 432 | msg[1] = PEER_MSG_STKT_ACK; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 433 | cursor = &msg[2]; |
| 434 | intencode(datalen, &cursor); |
| 435 | |
| 436 | /* move data after header */ |
| 437 | memmove(cursor, datamsg, datalen); |
| 438 | |
| 439 | /* return header size + data_len */ |
| 440 | return (cursor - msg) + datalen; |
| 441 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Callback to release a session with a peer |
| 445 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 446 | static void peer_session_release(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 447 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 448 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 449 | struct stream *s = si_strm(si); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 450 | struct peer *peer = (struct peer *)appctx->ctx.peers.ptr; |
| 451 | struct peers *peers = (struct peers *)strm_fe(s)->parent; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 452 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 453 | /* appctx->ctx.peers.ptr is not a peer session */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 454 | if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 455 | return; |
| 456 | |
| 457 | /* peer session identified */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 458 | if (peer) { |
| 459 | if (peer->stream == s) { |
Emeric Brun | b157d73 | 2015-08-21 12:00:30 +0200 | [diff] [blame] | 460 | /* Re-init current table pointers to force announcement on re-connect */ |
| 461 | peer->remote_table = peer->last_local_table = NULL; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 462 | peer->stream = NULL; |
| 463 | peer->appctx = NULL; |
| 464 | if (peer->flags & PEER_F_LEARN_ASSIGN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 465 | /* unassign current peer for learning */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 466 | peer->flags &= ~(PEER_F_LEARN_ASSIGN); |
| 467 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 468 | |
| 469 | /* reschedule a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 470 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 471 | } |
| 472 | /* reset teaching and learning flags to 0 */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 473 | peer->flags &= PEER_TEACH_RESET; |
| 474 | peer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 475 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 476 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /* |
| 482 | * IO Handler to handle message exchance with a peer |
| 483 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 484 | static void peer_io_handler(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 485 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 486 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 487 | struct stream *s = si_strm(si); |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 488 | struct peers *curpeers = (struct peers *)strm_fe(s)->parent; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 489 | int reql = 0; |
| 490 | int repl = 0; |
| 491 | |
| 492 | while (1) { |
| 493 | switchstate: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 494 | switch(appctx->st0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 495 | case PEER_SESS_ST_ACCEPT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 496 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 497 | appctx->st0 = PEER_SESS_ST_GETVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 498 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 499 | case PEER_SESS_ST_GETVERSION: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 500 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 501 | if (reql <= 0) { /* closed or EOL not found */ |
| 502 | if (reql == 0) |
| 503 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 504 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 505 | goto switchstate; |
| 506 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 507 | if (trash.str[reql-1] != '\n') { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 508 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 509 | goto switchstate; |
| 510 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 511 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 512 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 513 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 514 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 515 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 516 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 517 | |
| 518 | /* test version */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 519 | if (strcmp(PEER_SESSION_PROTO_NAME " 2.0", trash.str) != 0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 520 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 521 | appctx->st1 = PEER_SESS_SC_ERRVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 522 | /* test protocol */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 523 | 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] | 524 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 525 | goto switchstate; |
| 526 | } |
| 527 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 528 | appctx->st0 = PEER_SESS_ST_GETHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 529 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 530 | case PEER_SESS_ST_GETHOST: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 531 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 532 | if (reql <= 0) { /* closed or EOL not found */ |
| 533 | if (reql == 0) |
| 534 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 535 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 536 | goto switchstate; |
| 537 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 538 | if (trash.str[reql-1] != '\n') { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 539 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 540 | goto switchstate; |
| 541 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 542 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 543 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 544 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 545 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 546 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 547 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 548 | |
| 549 | /* test hostname match */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 550 | if (strcmp(localpeer, trash.str) != 0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 551 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 552 | appctx->st1 = PEER_SESS_SC_ERRHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 553 | goto switchstate; |
| 554 | } |
| 555 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 556 | appctx->st0 = PEER_SESS_ST_GETPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 557 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 558 | case PEER_SESS_ST_GETPEER: { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 559 | struct peer *curpeer; |
| 560 | char *p; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 561 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 562 | if (reql <= 0) { /* closed or EOL not found */ |
| 563 | if (reql == 0) |
| 564 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 565 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 566 | goto switchstate; |
| 567 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 568 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 569 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 570 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 571 | goto switchstate; |
| 572 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 573 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 574 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 575 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 576 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 577 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 578 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 579 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 580 | /* parse line "<peer name> <pid> <relative_pid>" */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 581 | p = strchr(trash.str, ' '); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 582 | if (!p) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 583 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 584 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 585 | goto switchstate; |
| 586 | } |
| 587 | *p = 0; |
| 588 | |
| 589 | /* lookup known peer */ |
| 590 | for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 591 | if (strcmp(curpeer->id, trash.str) == 0) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 592 | break; |
| 593 | } |
| 594 | |
| 595 | /* if unknown peer */ |
| 596 | if (!curpeer) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 597 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 598 | appctx->st1 = PEER_SESS_SC_ERRPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 599 | goto switchstate; |
| 600 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 601 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 602 | if (curpeer->stream && curpeer->stream != s) { |
| 603 | if (curpeer->local) { |
| 604 | /* Local connection, reply a retry */ |
| 605 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 606 | appctx->st1 = PEER_SESS_SC_TRYAGAIN; |
| 607 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 608 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 609 | peer_session_forceshutdown(curpeer->stream); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 610 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 611 | curpeer->stream = s; |
| 612 | curpeer->appctx = appctx; |
| 613 | appctx->ctx.peers.ptr = curpeer; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 614 | appctx->st0 = PEER_SESS_ST_SENDSUCCESS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 615 | /* fall through */ |
| 616 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 617 | case PEER_SESS_ST_SENDSUCCESS: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 618 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
| 619 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 620 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 621 | repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 622 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 623 | if (repl <= 0) { |
| 624 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 625 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 626 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 627 | goto switchstate; |
| 628 | } |
| 629 | |
| 630 | /* Register status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 631 | curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 632 | |
| 633 | /* Awake main task */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 634 | task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 635 | |
| 636 | /* Init confirm counter */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 637 | curpeer->confirm = 0; |
| 638 | |
| 639 | /* Init cursors */ |
| 640 | for (st = curpeer->tables; st ; st = st->next) { |
| 641 | st->last_get = st->last_acked = 0; |
| 642 | st->teaching_origin = st->last_pushed = st->update; |
| 643 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 644 | |
| 645 | /* reset teaching and learning flags to 0 */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 646 | curpeer->flags &= PEER_TEACH_RESET; |
| 647 | curpeer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 648 | |
| 649 | /* if current peer is local */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 650 | if (curpeer->local) { |
| 651 | /* if current host need resyncfrom local and no process assined */ |
| 652 | if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL && |
| 653 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 654 | /* assign local peer for a lesson, consider lesson already requested */ |
| 655 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 656 | peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 657 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 658 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 659 | } |
| 660 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 661 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 662 | /* assign peer for a lesson */ |
| 663 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 664 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
| 665 | } |
| 666 | |
| 667 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 668 | /* switch to waiting message state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 669 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 670 | goto switchstate; |
| 671 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 672 | case PEER_SESS_ST_CONNECT: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 673 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 674 | |
| 675 | /* Send headers */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 676 | repl = snprintf(trash.str, trash.size, |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 677 | PEER_SESSION_PROTO_NAME " 2.0\n%s\n%s %d %d\n", |
| 678 | curpeer->id, |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 679 | localpeer, |
Willy Tarreau | 7b77c9f | 2012-01-07 22:52:12 +0100 | [diff] [blame] | 680 | (int)getpid(), |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 681 | relative_pid); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 682 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 683 | if (repl >= trash.size) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 684 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 685 | goto switchstate; |
| 686 | } |
| 687 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 688 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 689 | if (repl <= 0) { |
| 690 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 691 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 692 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 693 | goto switchstate; |
| 694 | } |
| 695 | |
| 696 | /* switch to the waiting statuscode state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 697 | appctx->st0 = PEER_SESS_ST_GETSTATUS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 698 | /* fall through */ |
| 699 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 700 | case PEER_SESS_ST_GETSTATUS: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 701 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
| 702 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 703 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 704 | if (si_ic(si)->flags & CF_WRITE_PARTIAL) |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 705 | curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 706 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 707 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 708 | if (reql <= 0) { /* closed or EOL not found */ |
| 709 | if (reql == 0) |
| 710 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 711 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 712 | goto switchstate; |
| 713 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 714 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 715 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 716 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 717 | goto switchstate; |
| 718 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 719 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 720 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 721 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 722 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 723 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 724 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 725 | |
| 726 | /* Register status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 727 | curpeer->statuscode = atoi(trash.str); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 728 | |
| 729 | /* Awake main task */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 730 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 731 | |
| 732 | /* If status code is success */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 733 | if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 734 | /* Init cursors */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 735 | for (st = curpeer->tables; st ; st = st->next) { |
| 736 | st->last_get = st->last_acked = 0; |
| 737 | st->teaching_origin = st->last_pushed = st->update; |
| 738 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 739 | |
| 740 | /* Init confirm counter */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 741 | curpeer->confirm = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 742 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 743 | /* reset teaching and learning flags to 0 */ |
| 744 | curpeer->flags &= PEER_TEACH_RESET; |
| 745 | curpeer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 746 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 747 | /* If current peer is local */ |
| 748 | if (curpeer->local) { |
| 749 | /* flag to start to teach lesson */ |
| 750 | curpeer->flags |= PEER_F_TEACH_PROCESS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 751 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 752 | } |
| 753 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 754 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 755 | /* If peer is remote and resync from remote is needed, |
| 756 | and no peer currently assigned */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 757 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 758 | /* assign peer for a lesson */ |
| 759 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 760 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | } |
| 764 | else { |
| 765 | /* Status code is not success, abort */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 766 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 767 | goto switchstate; |
| 768 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 769 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 770 | /* fall through */ |
| 771 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 772 | case PEER_SESS_ST_WAITMSG: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 773 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 774 | struct stksess *ts, *newts = NULL; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 775 | uint32_t msg_len = 0; |
| 776 | char *msg_cur = trash.str; |
| 777 | char *msg_end = trash.str; |
| 778 | unsigned char msg_head[7]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 779 | int totl = 0; |
| 780 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 781 | reql = bo_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 782 | if (reql <= 0) /* closed or EOL not found */ |
| 783 | goto incomplete; |
| 784 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 785 | totl += reql; |
| 786 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 787 | if (msg_head[1] >= 128) { |
| 788 | /* Read and Decode message length */ |
| 789 | reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl); |
| 790 | if (reql <= 0) /* closed */ |
| 791 | goto incomplete; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 792 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 793 | totl += reql; |
| 794 | |
| 795 | if (msg_head[2] < 240) { |
| 796 | msg_len = msg_head[2]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 797 | } |
| 798 | else { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 799 | int i; |
| 800 | char *cur; |
| 801 | char *end; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 802 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 803 | for (i = 3 ; i < sizeof(msg_head) ; i++) { |
| 804 | reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl); |
| 805 | if (reql <= 0) /* closed */ |
| 806 | goto incomplete; |
| 807 | |
| 808 | totl += reql; |
| 809 | |
| 810 | if (!(msg_head[i] & 0x80)) |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | if (i == sizeof(msg_head)) { |
| 815 | /* malformed message */ |
| 816 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 817 | goto switchstate; |
| 818 | |
| 819 | } |
| 820 | end = (char *)msg_head + sizeof(msg_head); |
| 821 | cur = (char *)&msg_head[2]; |
| 822 | msg_len = intdecode(&cur, end); |
| 823 | if (!cur) { |
| 824 | /* malformed message */ |
| 825 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 826 | goto switchstate; |
| 827 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 830 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 831 | /* Read message content */ |
| 832 | if (msg_len) { |
| 833 | if (msg_len > trash.size) { |
| 834 | /* Status code is not success, abort */ |
| 835 | appctx->st0 = PEER_SESS_ST_ERRSIZE; |
| 836 | goto switchstate; |
| 837 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 838 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 839 | reql = bo_getblk(si_oc(si), trash.str, msg_len, totl); |
| 840 | if (reql <= 0) /* closed */ |
| 841 | goto incomplete; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 842 | totl += reql; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 843 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 844 | msg_end += msg_len; |
| 845 | } |
| 846 | } |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 847 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 848 | if (msg_head[0] == PEER_MSG_CLASS_CONTROL) { |
| 849 | if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) { |
| 850 | struct shared_table *st; |
| 851 | /* Reset message: remote need resync */ |
| 852 | |
| 853 | /* prepare tables fot a global push */ |
| 854 | for (st = curpeer->tables; st; st = st->next) { |
| 855 | st->teaching_origin = st->last_pushed = st->table->update; |
| 856 | st->flags = 0; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 857 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 858 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 859 | /* reset teaching flags to 0 */ |
| 860 | curpeer->flags &= PEER_TEACH_RESET; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 861 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 862 | /* flag to start to teach lesson */ |
| 863 | curpeer->flags |= PEER_F_TEACH_PROCESS; |
| 864 | |
| 865 | |
| 866 | } |
| 867 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) { |
| 868 | |
| 869 | if (curpeer->flags & PEER_F_LEARN_ASSIGN) { |
| 870 | curpeer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 871 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 872 | peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE); |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 873 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 874 | curpeer->confirm++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 875 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 876 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) { |
| 877 | |
| 878 | if (curpeer->flags & PEER_F_LEARN_ASSIGN) { |
| 879 | curpeer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 880 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 881 | |
| 882 | curpeer->flags |= PEER_F_LEARN_NOTUP2DATE; |
| 883 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 884 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Cyril Bonté | 9a60ff9 | 2014-02-16 01:07:07 +0100 | [diff] [blame] | 885 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 886 | curpeer->confirm++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 887 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 888 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) { |
| 889 | |
| 890 | /* If stopping state */ |
| 891 | if (stopping) { |
| 892 | /* Close session, push resync no more needed */ |
| 893 | curpeer->flags |= PEER_F_TEACH_COMPLETE; |
| 894 | appctx->st0 = PEER_SESS_ST_END; |
| 895 | goto switchstate; |
| 896 | } |
| 897 | |
| 898 | /* reset teaching flags to 0 */ |
| 899 | curpeer->flags &= PEER_TEACH_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 900 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 901 | } |
| 902 | else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) { |
| 903 | if (msg_head[1] == PEER_MSG_STKT_DEFINE) { |
| 904 | int table_id_len; |
| 905 | struct shared_table *st; |
| 906 | int table_type; |
| 907 | int table_keylen; |
| 908 | int table_id; |
| 909 | uint64_t table_data; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 910 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 911 | table_id = intdecode(&msg_cur, msg_end); |
| 912 | if (!msg_cur) { |
| 913 | /* malformed message */ |
| 914 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 915 | goto switchstate; |
| 916 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 917 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 918 | table_id_len = intdecode(&msg_cur, msg_end); |
| 919 | if (!msg_cur) { |
| 920 | /* malformed message */ |
| 921 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 922 | goto switchstate; |
| 923 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 924 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 925 | curpeer->remote_table = NULL; |
| 926 | if (!table_id_len || (msg_cur + table_id_len) >= msg_end) { |
| 927 | /* malformed message */ |
| 928 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 929 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 930 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 931 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 932 | for (st = curpeer->tables; st; st = st->next) { |
| 933 | /* Reset IDs */ |
| 934 | if (st->remote_id == table_id) |
| 935 | st->remote_id = 0; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 936 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 937 | if (!curpeer->remote_table |
| 938 | && (table_id_len == strlen(st->table->id)) |
| 939 | && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) { |
| 940 | curpeer->remote_table = st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 944 | if (!curpeer->remote_table) { |
| 945 | goto ignore_msg; |
| 946 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 947 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 948 | msg_cur += table_id_len; |
| 949 | if (msg_cur >= msg_end) { |
| 950 | /* malformed message */ |
| 951 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 952 | goto switchstate; |
| 953 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 954 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 955 | table_type = intdecode(&msg_cur, msg_end); |
| 956 | if (!msg_cur) { |
| 957 | /* malformed message */ |
| 958 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 959 | goto switchstate; |
| 960 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 961 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 962 | table_keylen = intdecode(&msg_cur, msg_end); |
| 963 | if (!msg_cur) { |
| 964 | /* malformed message */ |
| 965 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 966 | goto switchstate; |
| 967 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 968 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 969 | table_data = intdecode(&msg_cur, msg_end); |
| 970 | if (!msg_cur) { |
| 971 | /* malformed message */ |
| 972 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 973 | goto switchstate; |
| 974 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 975 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 976 | if (curpeer->remote_table->table->type != table_type |
| 977 | || curpeer->remote_table->table->key_size != table_keylen) { |
| 978 | curpeer->remote_table = NULL; |
| 979 | goto ignore_msg; |
| 980 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 981 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 982 | curpeer->remote_table->remote_data = table_data; |
| 983 | curpeer->remote_table->remote_id = table_id; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 984 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 985 | else if (msg_head[1] == PEER_MSG_STKT_SWITCH) { |
| 986 | struct shared_table *st; |
| 987 | int table_id; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 988 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 989 | table_id = intdecode(&msg_cur, msg_end); |
| 990 | if (!msg_cur) { |
| 991 | /* malformed message */ |
| 992 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 993 | goto switchstate; |
| 994 | } |
| 995 | curpeer->remote_table = NULL; |
| 996 | for (st = curpeer->tables; st; st = st->next) { |
| 997 | if (st->remote_id == table_id) { |
| 998 | curpeer->remote_table = st; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1003 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1004 | else if (msg_head[1] == PEER_MSG_STKT_UPDATE |
| 1005 | || msg_head[1] == PEER_MSG_STKT_INCUPDATE) { |
| 1006 | struct shared_table *st = curpeer->remote_table; |
| 1007 | uint32_t update; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1008 | unsigned int data_type; |
| 1009 | void *data_ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1010 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1011 | /* Here we have data message */ |
| 1012 | if (!st) |
| 1013 | goto ignore_msg; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1014 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1015 | if (msg_head[1] == PEER_MSG_STKT_UPDATE) { |
| 1016 | if (msg_len < sizeof(update)) { |
| 1017 | /* malformed message */ |
| 1018 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1019 | goto switchstate; |
| 1020 | } |
| 1021 | memcpy(&update, msg_cur, sizeof(update)); |
| 1022 | msg_cur += sizeof(update); |
| 1023 | st->last_get = htonl(update); |
| 1024 | } |
| 1025 | else { |
| 1026 | st->last_get++; |
| 1027 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1028 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1029 | newts = stksess_new(st->table, NULL); |
| 1030 | if (!newts) |
| 1031 | goto ignore_msg; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1032 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 1033 | if (st->table->type == SMP_T_STR) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1034 | unsigned int to_read, to_store; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1035 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1036 | to_read = intdecode(&msg_cur, msg_end); |
| 1037 | if (!msg_cur) { |
| 1038 | /* malformed message */ |
| 1039 | stksess_free(st->table, newts); |
| 1040 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1041 | goto switchstate; |
| 1042 | } |
| 1043 | to_store = MIN(to_read, st->table->key_size - 1); |
| 1044 | if (msg_cur + to_store > msg_end) { |
| 1045 | /* malformed message */ |
| 1046 | stksess_free(st->table, newts); |
| 1047 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1048 | goto switchstate; |
| 1049 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1050 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1051 | memcpy(newts->key.key, msg_cur, to_store); |
| 1052 | newts->key.key[to_store] = 0; |
| 1053 | msg_cur += to_read; |
| 1054 | } |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 1055 | else if (st->table->type == SMP_T_SINT) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1056 | unsigned int netinteger; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1057 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1058 | if (msg_cur + sizeof(netinteger) > msg_end) { |
| 1059 | /* malformed message */ |
| 1060 | stksess_free(st->table, newts); |
| 1061 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1062 | goto switchstate; |
| 1063 | } |
| 1064 | memcpy(&netinteger, msg_cur, sizeof(netinteger)); |
| 1065 | netinteger = ntohl(netinteger); |
| 1066 | memcpy(newts->key.key, &netinteger, sizeof(netinteger)); |
| 1067 | msg_cur += sizeof(netinteger); |
| 1068 | } |
| 1069 | else { |
| 1070 | if (msg_cur + st->table->key_size > msg_end) { |
| 1071 | /* malformed message */ |
| 1072 | stksess_free(st->table, newts); |
| 1073 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1074 | goto switchstate; |
| 1075 | } |
| 1076 | memcpy(newts->key.key, msg_cur, st->table->key_size); |
| 1077 | msg_cur += st->table->key_size; |
| 1078 | } |
| 1079 | |
| 1080 | /* lookup for existing entry */ |
| 1081 | ts = stktable_lookup(st->table, newts); |
| 1082 | if (ts) { |
| 1083 | /* the entry already exist, we can free ours */ |
| 1084 | stktable_touch(st->table, ts, 0); |
| 1085 | stksess_free(st->table, newts); |
| 1086 | newts = NULL; |
| 1087 | } |
| 1088 | else { |
| 1089 | struct eb32_node *eb; |
| 1090 | |
| 1091 | /* create new entry */ |
| 1092 | ts = stktable_store(st->table, newts, 0); |
| 1093 | newts = NULL; /* don't reuse it */ |
| 1094 | |
| 1095 | ts->upd.key= (++st->table->update)+(2^31); |
| 1096 | eb = eb32_insert(&st->table->updates, &ts->upd); |
| 1097 | if (eb != &ts->upd) { |
| 1098 | eb32_delete(eb); |
| 1099 | eb32_insert(&st->table->updates, &ts->upd); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1103 | for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1104 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1105 | if ((1 << data_type) & st->remote_data) { |
| 1106 | switch (stktable_data_types[data_type].std_type) { |
| 1107 | case STD_T_SINT: { |
| 1108 | int data; |
| 1109 | |
| 1110 | data = intdecode(&msg_cur, msg_end); |
| 1111 | if (!msg_cur) { |
| 1112 | /* malformed message */ |
| 1113 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1114 | goto switchstate; |
| 1115 | } |
| 1116 | |
| 1117 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1118 | if (data_ptr) |
| 1119 | stktable_data_cast(data_ptr, std_t_sint) = data; |
| 1120 | break; |
| 1121 | } |
| 1122 | case STD_T_UINT: { |
| 1123 | unsigned int data; |
| 1124 | |
| 1125 | data = intdecode(&msg_cur, msg_end); |
| 1126 | if (!msg_cur) { |
| 1127 | /* malformed message */ |
| 1128 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1129 | goto switchstate; |
| 1130 | } |
| 1131 | |
| 1132 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1133 | if (data_ptr) |
| 1134 | stktable_data_cast(data_ptr, std_t_uint) = data; |
| 1135 | break; |
| 1136 | } |
| 1137 | case STD_T_ULL: { |
| 1138 | unsigned long long data; |
| 1139 | |
| 1140 | data = intdecode(&msg_cur, msg_end); |
| 1141 | if (!msg_cur) { |
| 1142 | /* malformed message */ |
| 1143 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1144 | goto switchstate; |
| 1145 | } |
| 1146 | |
| 1147 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1148 | if (data_ptr) |
| 1149 | stktable_data_cast(data_ptr, std_t_ull) = data; |
| 1150 | break; |
| 1151 | } |
| 1152 | case STD_T_FRQP: { |
| 1153 | struct freq_ctr_period data; |
| 1154 | |
| 1155 | data.curr_tick = tick_add(now_ms, intdecode(&msg_cur, msg_end)); |
| 1156 | if (!msg_cur) { |
| 1157 | /* malformed message */ |
| 1158 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1159 | goto switchstate; |
| 1160 | } |
| 1161 | data.curr_ctr = intdecode(&msg_cur, msg_end); |
| 1162 | if (!msg_cur) { |
| 1163 | /* malformed message */ |
| 1164 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1165 | goto switchstate; |
| 1166 | } |
| 1167 | data.prev_ctr = intdecode(&msg_cur, msg_end); |
| 1168 | if (!msg_cur) { |
| 1169 | /* malformed message */ |
| 1170 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1171 | goto switchstate; |
| 1172 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1173 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1174 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1175 | if (data_ptr) |
| 1176 | stktable_data_cast(data_ptr, std_t_frqp) = data; |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | else if (msg_head[1] == PEER_MSG_STKT_ACK) { |
| 1184 | /* ack message */ |
| 1185 | uint32_t table_id ; |
| 1186 | uint32_t update; |
| 1187 | struct shared_table *st; |
| 1188 | |
| 1189 | table_id = intdecode(&msg_cur, msg_end); |
| 1190 | if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) { |
| 1191 | /* malformed message */ |
| 1192 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1193 | goto switchstate; |
| 1194 | } |
| 1195 | memcpy(&update, msg_cur, sizeof(update)); |
| 1196 | update = ntohl(update); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1197 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1198 | for (st = curpeer->tables; st; st = st->next) { |
| 1199 | if (st->local_id == table_id) { |
| 1200 | st->update = update; |
| 1201 | break; |
| 1202 | } |
| 1203 | } |
| 1204 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1205 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1206 | else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) { |
| 1207 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1208 | goto switchstate; |
| 1209 | } |
| 1210 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1211 | ignore_msg: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1212 | /* skip consumed message */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1213 | bo_skip(si_oc(si), totl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1214 | /* loop on that state to peek next message */ |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1215 | goto switchstate; |
| 1216 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1217 | incomplete: |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 1218 | /* we get here when a bo_getblk() returns <= 0 in reql */ |
| 1219 | |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1220 | if (reql < 0) { |
| 1221 | /* there was an error */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1222 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1223 | goto switchstate; |
| 1224 | } |
| 1225 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1226 | |
| 1227 | /* Confirm finished or partial messages */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1228 | while (curpeer->confirm) { |
| 1229 | unsigned char msg[2]; |
| 1230 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1231 | /* There is a confirm messages to send */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1232 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1233 | msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM; |
| 1234 | |
| 1235 | /* message to buffer */ |
| 1236 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1237 | if (repl <= 0) { |
| 1238 | /* no more write possible */ |
| 1239 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1240 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1241 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1242 | goto switchstate; |
| 1243 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1244 | curpeer->confirm--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1247 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1248 | /* Need to request a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1249 | if ((curpeer->flags & PEER_F_LEARN_ASSIGN) && |
| 1250 | (peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 1251 | !(peers->flags & PEERS_F_RESYNC_PROCESS)) { |
| 1252 | unsigned char msg[2]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1253 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1254 | /* Current peer was elected to request a resync */ |
| 1255 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1256 | msg[1] = PEER_MSG_CTRL_RESYNCREQ; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1257 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1258 | /* message to buffer */ |
| 1259 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
| 1260 | if (repl <= 0) { |
| 1261 | /* no more write possible */ |
| 1262 | if (repl == -1) |
| 1263 | goto full; |
| 1264 | appctx->st0 = PEER_SESS_ST_END; |
| 1265 | goto switchstate; |
| 1266 | } |
| 1267 | peers->flags |= PEERS_F_RESYNC_PROCESS; |
| 1268 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1269 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1270 | /* Nothing to read, now we start to write */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1271 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1272 | if (curpeer->tables) { |
| 1273 | struct shared_table *st; |
| 1274 | struct shared_table *last_local_table; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1275 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1276 | last_local_table = curpeer->last_local_table; |
| 1277 | if (!last_local_table) |
| 1278 | last_local_table = curpeer->tables; |
| 1279 | st = last_local_table->next; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1280 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1281 | while (1) { |
| 1282 | if (!st) |
| 1283 | st = curpeer->tables; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1284 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1285 | /* It remains some updates to ack */ |
| 1286 | if (st->last_get != st->last_acked) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1287 | int msglen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1288 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1289 | msglen = peer_prepare_ackmsg(st, trash.str, trash.size); |
| 1290 | if (!msglen) { |
| 1291 | /* internal error: message does not fit in trash */ |
| 1292 | appctx->st0 = PEER_SESS_ST_END; |
| 1293 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1296 | /* message to buffer */ |
| 1297 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1298 | if (repl <= 0) { |
| 1299 | /* no more write possible */ |
| 1300 | if (repl == -1) { |
| 1301 | goto full; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1302 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1303 | appctx->st0 = PEER_SESS_ST_END; |
| 1304 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1305 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1306 | st->last_acked = st->last_get; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1307 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1308 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1309 | if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) { |
| 1310 | if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) && |
| 1311 | ((int)(st->last_pushed - st->table->localupdate) < 0)) { |
| 1312 | struct eb32_node *eb; |
| 1313 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1314 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1315 | if (st != curpeer->last_local_table) { |
| 1316 | int msglen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1317 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1318 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1319 | if (!msglen) { |
| 1320 | /* internal error: message does not fit in trash */ |
| 1321 | appctx->st0 = PEER_SESS_ST_END; |
| 1322 | goto switchstate; |
| 1323 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1324 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1325 | /* message to buffer */ |
| 1326 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1327 | if (repl <= 0) { |
| 1328 | /* no more write possible */ |
| 1329 | if (repl == -1) { |
| 1330 | goto full; |
| 1331 | } |
| 1332 | appctx->st0 = PEER_SESS_ST_END; |
| 1333 | goto switchstate; |
| 1334 | } |
| 1335 | curpeer->last_local_table = st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1336 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1337 | |
| 1338 | /* We force new pushed to 1 to force identifier in update message */ |
| 1339 | new_pushed = 1; |
| 1340 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1341 | while (1) { |
| 1342 | uint32_t msglen; |
| 1343 | struct stksess *ts; |
| 1344 | |
| 1345 | /* push local updates */ |
| 1346 | if (!eb) { |
| 1347 | eb = eb32_first(&st->table->updates); |
| 1348 | if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 1349 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1350 | break; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | if ((int)(eb->key - st->table->localupdate) > 0) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 1355 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1356 | break; |
| 1357 | } |
| 1358 | |
| 1359 | ts = eb32_entry(eb, struct stksess, upd); |
| 1360 | msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed); |
| 1361 | if (!msglen) { |
| 1362 | /* internal error: message does not fit in trash */ |
| 1363 | appctx->st0 = PEER_SESS_ST_END; |
| 1364 | goto switchstate; |
| 1365 | } |
| 1366 | |
| 1367 | /* message to buffer */ |
| 1368 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1369 | if (repl <= 0) { |
| 1370 | /* no more write possible */ |
| 1371 | if (repl == -1) { |
| 1372 | goto full; |
| 1373 | } |
| 1374 | appctx->st0 = PEER_SESS_ST_END; |
| 1375 | goto switchstate; |
| 1376 | } |
| 1377 | st->last_pushed = ts->upd.key; |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 1378 | if ((int)(st->last_pushed - st->table->commitupdate) > 0) |
| 1379 | st->table->commitupdate = st->last_pushed; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1380 | /* identifier may not needed in next update message */ |
| 1381 | new_pushed = 0; |
| 1382 | |
| 1383 | eb = eb32_next(eb); |
| 1384 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1385 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1386 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1387 | else { |
| 1388 | if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) { |
| 1389 | struct eb32_node *eb; |
| 1390 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1391 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1392 | if (st != curpeer->last_local_table) { |
| 1393 | int msglen; |
| 1394 | |
| 1395 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1396 | if (!msglen) { |
| 1397 | /* internal error: message does not fit in trash */ |
| 1398 | appctx->st0 = PEER_SESS_ST_END; |
| 1399 | goto switchstate; |
| 1400 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1401 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1402 | /* message to buffer */ |
| 1403 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1404 | if (repl <= 0) { |
| 1405 | /* no more write possible */ |
| 1406 | if (repl == -1) { |
| 1407 | goto full; |
| 1408 | } |
| 1409 | appctx->st0 = PEER_SESS_ST_END; |
| 1410 | goto switchstate; |
| 1411 | } |
| 1412 | curpeer->last_local_table = st; |
| 1413 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1414 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1415 | /* We force new pushed to 1 to force identifier in update message */ |
| 1416 | new_pushed = 1; |
| 1417 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1418 | while (1) { |
| 1419 | uint32_t msglen; |
| 1420 | struct stksess *ts; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1421 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1422 | /* push local updates */ |
| 1423 | if (!eb) { |
| 1424 | st->flags |= SHTABLE_F_TEACH_STAGE1; |
| 1425 | eb = eb32_first(&st->table->updates); |
| 1426 | if (eb) |
| 1427 | st->last_pushed = eb->key - 1; |
| 1428 | break; |
| 1429 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1430 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1431 | ts = eb32_entry(eb, struct stksess, upd); |
| 1432 | msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed); |
| 1433 | if (!msglen) { |
| 1434 | /* internal error: message does not fit in trash */ |
| 1435 | appctx->st0 = PEER_SESS_ST_END; |
| 1436 | goto switchstate; |
| 1437 | } |
| 1438 | |
| 1439 | /* message to buffer */ |
| 1440 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1441 | if (repl <= 0) { |
| 1442 | /* no more write possible */ |
| 1443 | if (repl == -1) { |
| 1444 | goto full; |
| 1445 | } |
| 1446 | appctx->st0 = PEER_SESS_ST_END; |
| 1447 | goto switchstate; |
| 1448 | } |
| 1449 | st->last_pushed = ts->upd.key; |
| 1450 | /* identifier may not needed in next update message */ |
| 1451 | new_pushed = 0; |
| 1452 | |
| 1453 | eb = eb32_next(eb); |
| 1454 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1455 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1456 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1457 | if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) { |
| 1458 | struct eb32_node *eb; |
| 1459 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1460 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1461 | if (st != curpeer->last_local_table) { |
| 1462 | int msglen; |
| 1463 | |
| 1464 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1465 | if (!msglen) { |
| 1466 | /* internal error: message does not fit in trash */ |
| 1467 | appctx->st0 = PEER_SESS_ST_END; |
| 1468 | goto switchstate; |
| 1469 | } |
| 1470 | |
| 1471 | /* message to buffer */ |
| 1472 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1473 | if (repl <= 0) { |
| 1474 | /* no more write possible */ |
| 1475 | if (repl == -1) { |
| 1476 | goto full; |
| 1477 | } |
| 1478 | appctx->st0 = PEER_SESS_ST_END; |
| 1479 | goto switchstate; |
| 1480 | } |
| 1481 | curpeer->last_local_table = st; |
| 1482 | } |
| 1483 | |
| 1484 | /* We force new pushed to 1 to force identifier in update message */ |
| 1485 | new_pushed = 1; |
| 1486 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1487 | while (1) { |
| 1488 | uint32_t msglen; |
| 1489 | struct stksess *ts; |
| 1490 | |
| 1491 | /* push local updates */ |
| 1492 | if (!eb || eb->key > st->teaching_origin) { |
| 1493 | st->flags |= SHTABLE_F_TEACH_STAGE2; |
| 1494 | eb = eb32_first(&st->table->updates); |
| 1495 | if (eb) |
| 1496 | st->last_pushed = eb->key - 1; |
| 1497 | break; |
| 1498 | } |
| 1499 | |
| 1500 | ts = eb32_entry(eb, struct stksess, upd); |
| 1501 | msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed); |
| 1502 | if (!msglen) { |
| 1503 | /* internal error: message does not fit in trash */ |
| 1504 | appctx->st0 = PEER_SESS_ST_END; |
| 1505 | goto switchstate; |
| 1506 | } |
| 1507 | |
| 1508 | /* message to buffer */ |
| 1509 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1510 | if (repl <= 0) { |
| 1511 | /* no more write possible */ |
| 1512 | if (repl == -1) { |
| 1513 | goto full; |
| 1514 | } |
| 1515 | appctx->st0 = PEER_SESS_ST_END; |
| 1516 | goto switchstate; |
| 1517 | } |
| 1518 | st->last_pushed = ts->upd.key; |
| 1519 | /* identifier may not needed in next update message */ |
| 1520 | new_pushed = 0; |
| 1521 | |
| 1522 | eb = eb32_next(eb); |
| 1523 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1524 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1525 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1526 | |
| 1527 | if (st == last_local_table) |
| 1528 | break; |
| 1529 | st = st->next; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1530 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) { |
| 1535 | unsigned char msg[2]; |
| 1536 | |
| 1537 | /* Current peer was elected to request a resync */ |
| 1538 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1539 | msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL; |
| 1540 | /* process final lesson message */ |
| 1541 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
| 1542 | if (repl <= 0) { |
| 1543 | /* no more write possible */ |
| 1544 | if (repl == -1) |
| 1545 | goto full; |
| 1546 | appctx->st0 = PEER_SESS_ST_END; |
| 1547 | goto switchstate; |
| 1548 | } |
| 1549 | /* flag finished message sent */ |
| 1550 | curpeer->flags |= PEER_F_TEACH_FINISHED; |
| 1551 | } |
| 1552 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1553 | /* noting more to do */ |
| 1554 | goto out; |
| 1555 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1556 | case PEER_SESS_ST_EXIT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1557 | repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1558 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1559 | if (bi_putblk(si_ic(si), trash.str, repl) == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1560 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1561 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1562 | goto switchstate; |
| 1563 | case PEER_SESS_ST_ERRSIZE: { |
| 1564 | unsigned char msg[2]; |
| 1565 | |
| 1566 | msg[0] = PEER_MSG_CLASS_ERROR; |
| 1567 | msg[1] = PEER_MSG_ERR_SIZELIMIT; |
| 1568 | |
| 1569 | if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1) |
| 1570 | goto full; |
| 1571 | appctx->st0 = PEER_SESS_ST_END; |
| 1572 | goto switchstate; |
| 1573 | } |
| 1574 | case PEER_SESS_ST_ERRPROTO: { |
| 1575 | unsigned char msg[2]; |
| 1576 | |
| 1577 | msg[0] = PEER_MSG_CLASS_ERROR; |
| 1578 | msg[1] = PEER_MSG_ERR_PROTOCOL; |
| 1579 | |
| 1580 | if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1) |
| 1581 | goto full; |
| 1582 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1583 | /* fall through */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1584 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1585 | case PEER_SESS_ST_END: { |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1586 | si_shutw(si); |
| 1587 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1588 | si_ic(si)->flags |= CF_READ_NULL; |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1589 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | out: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1594 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1595 | return; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1596 | full: |
Willy Tarreau | fe12793 | 2015-04-21 19:23:39 +0200 | [diff] [blame] | 1597 | si_applet_cant_put(si); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1598 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 1601 | static struct applet peer_applet = { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1602 | .obj_type = OBJ_TYPE_APPLET, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1603 | .name = "<PEER>", /* used for logging */ |
| 1604 | .fct = peer_io_handler, |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 1605 | .release = peer_session_release, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1606 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1607 | |
| 1608 | /* |
| 1609 | * Use this function to force a close of a peer session |
| 1610 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1611 | static void peer_session_forceshutdown(struct stream * stream) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1612 | { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1613 | struct appctx *appctx = NULL; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1614 | struct peer *ps; |
| 1615 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1616 | int i; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1617 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1618 | for (i = 0; i <= 1; i++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1619 | appctx = objt_appctx(stream->si[i].end); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1620 | if (!appctx) |
| 1621 | continue; |
| 1622 | if (appctx->applet != &peer_applet) |
| 1623 | continue; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1624 | break; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1627 | if (!appctx) |
| 1628 | return; |
| 1629 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1630 | ps = (struct peer *)appctx->ctx.peers.ptr; |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1631 | /* we're killing a connection, we must apply a random delay before |
| 1632 | * retrying otherwise the other end will do the same and we can loop |
| 1633 | * for a while. |
| 1634 | */ |
| 1635 | if (ps) |
| 1636 | ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000)); |
| 1637 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1638 | /* call release to reinit resync states if needed */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1639 | peer_session_release(appctx); |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1640 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1641 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1642 | task_wakeup(stream->task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1645 | /* Pre-configures a peers frontend to accept incoming connections */ |
| 1646 | void peers_setup_frontend(struct proxy *fe) |
| 1647 | { |
| 1648 | fe->last_change = now.tv_sec; |
| 1649 | fe->cap = PR_CAP_FE; |
| 1650 | fe->maxconn = 0; |
| 1651 | fe->conn_retries = CONN_RETRIES; |
| 1652 | fe->timeout.client = MS_TO_TICKS(5000); |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 1653 | fe->accept = frontend_accept; |
Willy Tarreau | f87ab94 | 2015-03-13 15:55:16 +0100 | [diff] [blame] | 1654 | fe->default_target = &peer_applet.obj_type; |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1655 | fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
Willy Tarreau | 0fca483 | 2015-05-01 19:12:05 +0200 | [diff] [blame] | 1656 | fe->bind_proc = 0; /* will be filled by users */ |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1657 | } |
| 1658 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1659 | /* |
Willy Tarreau | bd55e31 | 2010-11-11 10:55:09 +0100 | [diff] [blame] | 1660 | * Create a new peer session in assigned state (connect will start automatically) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1661 | */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1662 | static struct stream *peer_session_create(struct peers *peers, struct peer *peer) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1663 | { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1664 | struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1665 | struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1666 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1667 | struct session *sess; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1668 | struct stream *s; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1669 | struct task *t; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1670 | struct connection *conn; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1671 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1672 | peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 1673 | peer->statuscode = PEER_SESS_SC_CONNECTCODE; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1674 | s = NULL; |
| 1675 | |
| 1676 | appctx = appctx_new(&peer_applet); |
| 1677 | if (!appctx) |
| 1678 | goto out_close; |
| 1679 | |
| 1680 | appctx->st0 = PEER_SESS_ST_CONNECT; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1681 | appctx->ctx.peers.ptr = (void *)peer; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1682 | |
Willy Tarreau | 4099a7c | 2015-04-05 00:39:55 +0200 | [diff] [blame] | 1683 | sess = session_new(p, l, &appctx->obj_type); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1684 | if (!sess) { |
Godbach | 430f291 | 2013-06-20 13:28:38 +0800 | [diff] [blame] | 1685 | Alert("out of memory in peer_session_create().\n"); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1686 | goto out_free_appctx; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1687 | } |
| 1688 | |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1689 | if ((t = task_new()) == NULL) { |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1690 | Alert("out of memory in peer_session_create().\n"); |
| 1691 | goto out_free_sess; |
| 1692 | } |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1693 | t->nice = l->nice; |
| 1694 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 1695 | if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) { |
Willy Tarreau | 342bfb1 | 2015-04-05 01:35:34 +0200 | [diff] [blame] | 1696 | Alert("Failed to initialize stream in peer_session_create().\n"); |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1697 | goto out_free_task; |
| 1698 | } |
| 1699 | |
Willy Tarreau | 342bfb1 | 2015-04-05 01:35:34 +0200 | [diff] [blame] | 1700 | /* The tasks below are normally what is supposed to be done by |
| 1701 | * fe->accept(). |
| 1702 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1703 | s->flags = SF_ASSIGNED|SF_ADDR_SET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1704 | |
Willy Tarreau | 6e2979c | 2015-04-27 13:21:15 +0200 | [diff] [blame] | 1705 | /* applet is waiting for data */ |
| 1706 | si_applet_cant_get(&s->si[0]); |
| 1707 | appctx_wakeup(appctx); |
| 1708 | |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1709 | /* initiate an outgoing connection */ |
| 1710 | si_set_state(&s->si[1], SI_ST_ASS); |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1711 | |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1712 | /* automatically prepare the stream interface to connect to the |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1713 | * pre-initialized connection in si->conn. |
| 1714 | */ |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1715 | if (unlikely((conn = conn_new()) == NULL)) |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1716 | goto out_free_strm; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1717 | |
| 1718 | conn_prepare(conn, peer->proto, peer->xprt); |
| 1719 | si_attach_conn(&s->si[1], conn); |
| 1720 | |
| 1721 | conn->target = s->target = &s->be->obj_type; |
| 1722 | memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1723 | s->do_log = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1724 | s->uniq_id = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1725 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1726 | s->res.flags |= CF_READ_DONTWAIT; |
Willy Tarreau | 696a291 | 2014-11-24 11:36:57 +0100 | [diff] [blame] | 1727 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1728 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 1729 | p->feconn++;/* beconn will be increased later */ |
| 1730 | jobs++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1731 | if (!(s->sess->listener->options & LI_O_UNLIMITED)) |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1732 | actconn++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1733 | totalconn++; |
| 1734 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1735 | peer->appctx = appctx; |
| 1736 | peer->stream = s; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1737 | return s; |
| 1738 | |
| 1739 | /* Error unrolling */ |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1740 | out_free_strm: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1741 | LIST_DEL(&s->list); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1742 | pool_free2(pool2_stream, s); |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1743 | out_free_task: |
| 1744 | task_free(t); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1745 | out_free_sess: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 1746 | session_free(sess); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1747 | out_free_appctx: |
| 1748 | appctx_free(appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1749 | out_close: |
| 1750 | return s; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * Task processing function to manage re-connect and peer session |
| 1755 | * tasks wakeup on local update. |
| 1756 | */ |
Simon Horman | 9655377 | 2011-06-08 09:18:51 +0900 | [diff] [blame] | 1757 | static struct task *process_peer_sync(struct task * task) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1758 | { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1759 | struct peers *peers = (struct peers *)task->context; |
| 1760 | struct peer *ps; |
| 1761 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1762 | |
| 1763 | task->expire = TICK_ETERNITY; |
| 1764 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1765 | if (!peers->peers_fe) { |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 1766 | /* this one was never started, kill it */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1767 | signal_unregister_handler(peers->sighandler); |
| 1768 | peers->sync_task = NULL; |
| 1769 | task_delete(peers->sync_task); |
| 1770 | task_free(peers->sync_task); |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 1771 | return NULL; |
| 1772 | } |
| 1773 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1774 | if (!stopping) { |
| 1775 | /* Normal case (not soft stop)*/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1776 | |
| 1777 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) && |
| 1778 | (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) && |
| 1779 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1780 | /* Resync from local peer needed |
| 1781 | no peer was assigned for the lesson |
| 1782 | and no old local peer found |
| 1783 | or resync timeout expire */ |
| 1784 | |
| 1785 | /* flag no more resync from local, to try resync from remotes */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1786 | peers->flags |= PEERS_F_RESYNC_LOCAL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1787 | |
| 1788 | /* reschedule a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1789 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | /* For each session */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1793 | for (ps = peers->remote; ps; ps = ps->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1794 | /* For each remote peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1795 | if (!ps->local) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1796 | if (!ps->stream) { |
| 1797 | /* no active stream */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1798 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1799 | ((ps->statuscode == PEER_SESS_SC_CONNECTCODE || |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1800 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1801 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1802 | tick_is_expired(ps->reconnect, now_ms))) { |
| 1803 | /* connection never tried |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1804 | * or previous stream established with success |
| 1805 | * or previous stream failed during connection |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1806 | * and reconnection timer is expired */ |
| 1807 | |
| 1808 | /* retry a connect */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1809 | ps->stream = peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1810 | } |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1811 | else if (!tick_is_expired(ps->reconnect, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1812 | /* If previous session failed during connection |
| 1813 | * but reconnection timer is not expired */ |
| 1814 | |
| 1815 | /* reschedule task for reconnect */ |
| 1816 | task->expire = tick_first(task->expire, ps->reconnect); |
| 1817 | } |
| 1818 | /* else do nothing */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1819 | } /* !ps->stream */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1820 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1821 | /* current stream is active and established */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1822 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 1823 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1824 | !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) { |
| 1825 | /* Resync from a remote is needed |
| 1826 | * and no peer was assigned for lesson |
| 1827 | * and current peer may be up2date */ |
| 1828 | |
| 1829 | /* assign peer for the lesson */ |
| 1830 | ps->flags |= PEER_F_LEARN_ASSIGN; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1831 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1832 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1833 | /* awake peer stream task to handle a request of resync */ |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 1834 | appctx_wakeup(ps->appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1835 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1836 | else { |
| 1837 | /* Awake session if there is data to push */ |
| 1838 | for (st = ps->tables; st ; st = st->next) { |
| 1839 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
| 1840 | /* awake peer stream task to push local updates */ |
| 1841 | appctx_wakeup(ps->appctx); |
| 1842 | break; |
| 1843 | } |
| 1844 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1845 | } |
| 1846 | /* else do nothing */ |
| 1847 | } /* SUCCESSCODE */ |
| 1848 | } /* !ps->peer->local */ |
| 1849 | } /* for */ |
| 1850 | |
| 1851 | /* Resync from remotes expired: consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1852 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 1853 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 1854 | tick_is_expired(peers->resync_timeout, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1855 | /* Resync from remote peer needed |
| 1856 | * no peer was assigned for the lesson |
| 1857 | * and resync timeout expire */ |
| 1858 | |
| 1859 | /* flag no more resync from remote, consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1860 | peers->flags |= PEERS_F_RESYNC_REMOTE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1861 | } |
| 1862 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1863 | if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1864 | /* Resync not finished*/ |
| 1865 | /* reschedule task to resync timeout, to ended resync if needed */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1866 | task->expire = tick_first(task->expire, peers->resync_timeout); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1867 | } |
| 1868 | } /* !stopping */ |
| 1869 | else { |
| 1870 | /* soft stop case */ |
| 1871 | if (task->state & TASK_WOKEN_SIGNAL) { |
| 1872 | /* We've just recieved the signal */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1873 | if (!(peers->flags & PEERS_F_DONOTSTOP)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1874 | /* add DO NOT STOP flag if not present */ |
| 1875 | jobs++; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1876 | peers->flags |= PEERS_F_DONOTSTOP; |
| 1877 | ps = peers->local; |
| 1878 | for (st = ps->tables; st ; st = st->next) |
| 1879 | st->table->syncing++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | /* disconnect all connected peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1883 | for (ps = peers->remote; ps; ps = ps->next) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1884 | if (ps->stream) { |
| 1885 | peer_session_forceshutdown(ps->stream); |
| 1886 | ps->stream = NULL; |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 1887 | ps->appctx = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1891 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1892 | ps = peers->local; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1893 | if (ps->flags & PEER_F_TEACH_COMPLETE) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1894 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1895 | /* resync of new process was complete, current process can die now */ |
| 1896 | jobs--; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1897 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 1898 | for (st = ps->tables; st ; st = st->next) |
| 1899 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1900 | } |
| 1901 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1902 | else if (!ps->stream) { |
| 1903 | /* If stream is not active */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1904 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1905 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
| 1906 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE || |
| 1907 | ps->statuscode == PEER_SESS_SC_TRYAGAIN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1908 | /* connection never tried |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1909 | * or previous stream was successfully established |
| 1910 | * or previous stream tcp connect success but init state incomplete |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1911 | * or during previous connect, peer replies a try again statuscode */ |
| 1912 | |
| 1913 | /* connect to the peer */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1914 | peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1915 | } |
| 1916 | else { |
| 1917 | /* Other error cases */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1918 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1919 | /* unable to resync new process, current process can die now */ |
| 1920 | jobs--; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1921 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 1922 | for (st = ps->tables; st ; st = st->next) |
| 1923 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1927 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1928 | /* current stream active and established |
| 1929 | awake stream to push remaining local updates */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1930 | for (st = ps->tables; st ; st = st->next) { |
| 1931 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
| 1932 | /* awake peer stream task to push local updates */ |
| 1933 | appctx_wakeup(ps->appctx); |
| 1934 | break; |
| 1935 | } |
| 1936 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1937 | } |
| 1938 | } /* stopping */ |
| 1939 | /* Wakeup for re-connect */ |
| 1940 | return task; |
| 1941 | } |
| 1942 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1943 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1944 | /* |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1945 | * |
| 1946 | */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1947 | void peers_init_sync(struct peers *peers) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1948 | { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1949 | struct peer * curpeer; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1950 | struct listener *listener; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1951 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1952 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1953 | peers->peers_fe->maxconn += 3; |
| 1954 | } |
| 1955 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1956 | list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe) |
| 1957 | listener->maxconn = peers->peers_fe->maxconn; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1958 | peers->sync_task = task_new(); |
| 1959 | peers->sync_task->process = process_peer_sync; |
| 1960 | peers->sync_task->expire = TICK_ETERNITY; |
| 1961 | peers->sync_task->context = (void *)peers; |
| 1962 | peers->sighandler = signal_register_task(0, peers->sync_task, 0); |
| 1963 | task_wakeup(peers->sync_task, TASK_WOKEN_INIT); |
| 1964 | } |
| 1965 | |
| 1966 | |
| 1967 | |
| 1968 | /* |
| 1969 | * Function used to register a table for sync on a group of peers |
| 1970 | * |
| 1971 | */ |
| 1972 | void peers_register_table(struct peers *peers, struct stktable *table) |
| 1973 | { |
| 1974 | struct shared_table *st; |
| 1975 | struct peer * curpeer; |
| 1976 | int id = 0; |
| 1977 | |
| 1978 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
| 1979 | st = (struct shared_table *)calloc(1,sizeof(struct shared_table)); |
| 1980 | st->table = table; |
| 1981 | st->next = curpeer->tables; |
| 1982 | if (curpeer->tables) |
| 1983 | id = curpeer->tables->local_id; |
| 1984 | st->local_id = id + 1; |
| 1985 | |
| 1986 | curpeer->tables = st; |
| 1987 | } |
| 1988 | |
| 1989 | table->sync_task = peers->sync_task; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1990 | } |
| 1991 | |