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