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 | |
| 550 | while (1) { |
| 551 | switchstate: |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 552 | maj_ver = min_ver = (unsigned int)-1; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 553 | switch(appctx->st0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 554 | case PEER_SESS_ST_ACCEPT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 555 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 556 | appctx->st0 = PEER_SESS_ST_GETVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 557 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 558 | case PEER_SESS_ST_GETVERSION: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 559 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 560 | if (reql <= 0) { /* closed or EOL not found */ |
| 561 | if (reql == 0) |
| 562 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 563 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 564 | goto switchstate; |
| 565 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 566 | if (trash.str[reql-1] != '\n') { |
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 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 571 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 572 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 573 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 574 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 575 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 576 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 577 | /* test protocol */ |
| 578 | if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, proto_len + 1) != 0) { |
| 579 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 580 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
| 581 | goto switchstate; |
| 582 | } |
| 583 | if (peer_get_version(trash.str + proto_len + 1, &maj_ver, &min_ver) == -1 || |
| 584 | maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 585 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 586 | appctx->st1 = PEER_SESS_SC_ERRVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 587 | goto switchstate; |
| 588 | } |
| 589 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 590 | appctx->st0 = PEER_SESS_ST_GETHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 591 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 592 | case PEER_SESS_ST_GETHOST: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 593 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 594 | if (reql <= 0) { /* closed or EOL not found */ |
| 595 | if (reql == 0) |
| 596 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 597 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 598 | goto switchstate; |
| 599 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 600 | if (trash.str[reql-1] != '\n') { |
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 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 605 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 606 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 607 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 608 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 609 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 610 | |
| 611 | /* test hostname match */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 612 | if (strcmp(localpeer, trash.str) != 0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 613 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 614 | appctx->st1 = PEER_SESS_SC_ERRHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 615 | goto switchstate; |
| 616 | } |
| 617 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 618 | appctx->st0 = PEER_SESS_ST_GETPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 619 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 620 | case PEER_SESS_ST_GETPEER: { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 621 | struct peer *curpeer; |
| 622 | char *p; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 623 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 624 | if (reql <= 0) { /* closed or EOL not found */ |
| 625 | if (reql == 0) |
| 626 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 627 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 628 | goto switchstate; |
| 629 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 630 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 631 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 632 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 633 | goto switchstate; |
| 634 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 635 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 636 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 637 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 638 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 639 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 640 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 641 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 642 | /* parse line "<peer name> <pid> <relative_pid>" */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 643 | p = strchr(trash.str, ' '); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 644 | if (!p) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 645 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 646 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 647 | goto switchstate; |
| 648 | } |
| 649 | *p = 0; |
| 650 | |
| 651 | /* lookup known peer */ |
| 652 | for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 653 | if (strcmp(curpeer->id, trash.str) == 0) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | |
| 657 | /* if unknown peer */ |
| 658 | if (!curpeer) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 659 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 660 | appctx->st1 = PEER_SESS_SC_ERRPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 661 | goto switchstate; |
| 662 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 663 | |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 664 | if (curpeer->appctx && curpeer->appctx != appctx) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 665 | if (curpeer->local) { |
| 666 | /* Local connection, reply a retry */ |
| 667 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 668 | appctx->st1 = PEER_SESS_SC_TRYAGAIN; |
| 669 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 670 | } |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 671 | peer_session_forceshutdown(curpeer->appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 672 | } |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 673 | if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) { |
| 674 | if (min_ver == PEER_DWNGRD_MINOR_VER) { |
| 675 | curpeer->flags |= PEER_F_DWNGRD; |
| 676 | } |
| 677 | else { |
| 678 | curpeer->flags &= ~PEER_F_DWNGRD; |
| 679 | } |
| 680 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 681 | curpeer->appctx = appctx; |
| 682 | appctx->ctx.peers.ptr = curpeer; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 683 | appctx->st0 = PEER_SESS_ST_SENDSUCCESS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 684 | /* fall through */ |
| 685 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 686 | case PEER_SESS_ST_SENDSUCCESS: { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 687 | struct peer *curpeer = appctx->ctx.peers.ptr; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 688 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 689 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 690 | repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 691 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 692 | if (repl <= 0) { |
| 693 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 694 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 695 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 696 | goto switchstate; |
| 697 | } |
| 698 | |
| 699 | /* Register status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 700 | curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 701 | |
| 702 | /* Awake main task */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 703 | task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 704 | |
| 705 | /* Init confirm counter */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 706 | curpeer->confirm = 0; |
| 707 | |
| 708 | /* Init cursors */ |
| 709 | for (st = curpeer->tables; st ; st = st->next) { |
| 710 | st->last_get = st->last_acked = 0; |
| 711 | st->teaching_origin = st->last_pushed = st->update; |
| 712 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 713 | |
| 714 | /* reset teaching and learning flags to 0 */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 715 | curpeer->flags &= PEER_TEACH_RESET; |
| 716 | curpeer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 717 | |
| 718 | /* if current peer is local */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 719 | if (curpeer->local) { |
| 720 | /* if current host need resyncfrom local and no process assined */ |
| 721 | if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL && |
| 722 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 723 | /* assign local peer for a lesson, consider lesson already requested */ |
| 724 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 725 | peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 726 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 727 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 728 | } |
| 729 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 730 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 731 | /* assign peer for a lesson */ |
| 732 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 733 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
| 734 | } |
| 735 | |
| 736 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 737 | /* switch to waiting message state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 738 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 739 | goto switchstate; |
| 740 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 741 | case PEER_SESS_ST_CONNECT: { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 742 | struct peer *curpeer = appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 743 | |
| 744 | /* Send headers */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 745 | repl = snprintf(trash.str, trash.size, |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 746 | PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n", |
| 747 | PEER_MAJOR_VER, |
| 748 | (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER, |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 749 | curpeer->id, |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 750 | localpeer, |
Willy Tarreau | 7b77c9f | 2012-01-07 22:52:12 +0100 | [diff] [blame] | 751 | (int)getpid(), |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 752 | relative_pid); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 753 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 754 | if (repl >= trash.size) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 755 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 756 | goto switchstate; |
| 757 | } |
| 758 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 759 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 760 | if (repl <= 0) { |
| 761 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 762 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 763 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 764 | goto switchstate; |
| 765 | } |
| 766 | |
| 767 | /* switch to the waiting statuscode state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 768 | appctx->st0 = PEER_SESS_ST_GETSTATUS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 769 | /* fall through */ |
| 770 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 771 | case PEER_SESS_ST_GETSTATUS: { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 772 | struct peer *curpeer = appctx->ctx.peers.ptr; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 773 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 774 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 775 | if (si_ic(si)->flags & CF_WRITE_PARTIAL) |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 776 | curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 777 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 778 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 779 | if (reql <= 0) { /* closed or EOL not found */ |
| 780 | if (reql == 0) |
| 781 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 782 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 783 | goto switchstate; |
| 784 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 785 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 786 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 787 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 788 | goto switchstate; |
| 789 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 790 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 791 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 792 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 793 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 794 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 795 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 796 | |
| 797 | /* Register status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 798 | curpeer->statuscode = atoi(trash.str); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 799 | |
| 800 | /* Awake main task */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 801 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 802 | |
| 803 | /* If status code is success */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 804 | if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 805 | /* Init cursors */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 806 | for (st = curpeer->tables; st ; st = st->next) { |
| 807 | st->last_get = st->last_acked = 0; |
| 808 | st->teaching_origin = st->last_pushed = st->update; |
| 809 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 810 | |
| 811 | /* Init confirm counter */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 812 | curpeer->confirm = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 813 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 814 | /* reset teaching and learning flags to 0 */ |
| 815 | curpeer->flags &= PEER_TEACH_RESET; |
| 816 | curpeer->flags &= PEER_LEARN_RESET; |
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 | /* If current peer is local */ |
| 819 | if (curpeer->local) { |
| 820 | /* flag to start to teach lesson */ |
| 821 | curpeer->flags |= PEER_F_TEACH_PROCESS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 822 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 823 | } |
| 824 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 825 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 826 | /* If peer is remote and resync from remote is needed, |
| 827 | and no peer currently assigned */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 828 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 829 | /* assign peer for a lesson */ |
| 830 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 831 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | } |
| 835 | else { |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 836 | if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION) |
| 837 | curpeer->flags |= PEER_F_DWNGRD; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 838 | /* Status code is not success, abort */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 839 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 840 | goto switchstate; |
| 841 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 842 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 843 | /* fall through */ |
| 844 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 845 | case PEER_SESS_ST_WAITMSG: { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 846 | struct peer *curpeer = appctx->ctx.peers.ptr; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 847 | struct stksess *ts, *newts = NULL; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 848 | uint32_t msg_len = 0; |
| 849 | char *msg_cur = trash.str; |
| 850 | char *msg_end = trash.str; |
| 851 | unsigned char msg_head[7]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 852 | int totl = 0; |
| 853 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 854 | 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] | 855 | if (reql <= 0) /* closed or EOL not found */ |
| 856 | goto incomplete; |
| 857 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 858 | totl += reql; |
| 859 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 860 | if (msg_head[1] >= 128) { |
| 861 | /* Read and Decode message length */ |
| 862 | reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl); |
| 863 | if (reql <= 0) /* closed */ |
| 864 | goto incomplete; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 865 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 866 | totl += reql; |
| 867 | |
| 868 | if (msg_head[2] < 240) { |
| 869 | msg_len = msg_head[2]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 870 | } |
| 871 | else { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 872 | int i; |
| 873 | char *cur; |
| 874 | char *end; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 875 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 876 | for (i = 3 ; i < sizeof(msg_head) ; i++) { |
| 877 | reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl); |
| 878 | if (reql <= 0) /* closed */ |
| 879 | goto incomplete; |
| 880 | |
| 881 | totl += reql; |
| 882 | |
| 883 | if (!(msg_head[i] & 0x80)) |
| 884 | break; |
| 885 | } |
| 886 | |
| 887 | if (i == sizeof(msg_head)) { |
| 888 | /* malformed message */ |
| 889 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 890 | goto switchstate; |
| 891 | |
| 892 | } |
| 893 | end = (char *)msg_head + sizeof(msg_head); |
| 894 | cur = (char *)&msg_head[2]; |
| 895 | msg_len = intdecode(&cur, end); |
| 896 | if (!cur) { |
| 897 | /* malformed message */ |
| 898 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 899 | goto switchstate; |
| 900 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 903 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 904 | /* Read message content */ |
| 905 | if (msg_len) { |
| 906 | if (msg_len > trash.size) { |
| 907 | /* Status code is not success, abort */ |
| 908 | appctx->st0 = PEER_SESS_ST_ERRSIZE; |
| 909 | goto switchstate; |
| 910 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 911 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 912 | reql = bo_getblk(si_oc(si), trash.str, msg_len, totl); |
| 913 | if (reql <= 0) /* closed */ |
| 914 | goto incomplete; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 915 | totl += reql; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 916 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 917 | msg_end += msg_len; |
| 918 | } |
| 919 | } |
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 | if (msg_head[0] == PEER_MSG_CLASS_CONTROL) { |
| 922 | if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) { |
| 923 | struct shared_table *st; |
| 924 | /* Reset message: remote need resync */ |
| 925 | |
| 926 | /* prepare tables fot a global push */ |
| 927 | for (st = curpeer->tables; st; st = st->next) { |
| 928 | st->teaching_origin = st->last_pushed = st->table->update; |
| 929 | st->flags = 0; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 930 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 931 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 932 | /* reset teaching flags to 0 */ |
| 933 | curpeer->flags &= PEER_TEACH_RESET; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 934 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 935 | /* flag to start to teach lesson */ |
| 936 | curpeer->flags |= PEER_F_TEACH_PROCESS; |
| 937 | |
| 938 | |
| 939 | } |
| 940 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) { |
| 941 | |
| 942 | if (curpeer->flags & PEER_F_LEARN_ASSIGN) { |
| 943 | curpeer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 944 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 945 | peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE); |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 946 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 947 | curpeer->confirm++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 948 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 949 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) { |
| 950 | |
| 951 | if (curpeer->flags & PEER_F_LEARN_ASSIGN) { |
| 952 | curpeer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 953 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 954 | |
| 955 | curpeer->flags |= PEER_F_LEARN_NOTUP2DATE; |
| 956 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 957 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Cyril Bonté | 9a60ff9 | 2014-02-16 01:07:07 +0100 | [diff] [blame] | 958 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 959 | curpeer->confirm++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 960 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 961 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) { |
Emeric Brun | 597b26e | 2016-08-12 11:23:31 +0200 | [diff] [blame] | 962 | struct shared_table *st; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 963 | |
| 964 | /* If stopping state */ |
| 965 | if (stopping) { |
| 966 | /* Close session, push resync no more needed */ |
| 967 | curpeer->flags |= PEER_F_TEACH_COMPLETE; |
| 968 | appctx->st0 = PEER_SESS_ST_END; |
| 969 | goto switchstate; |
| 970 | } |
Emeric Brun | 597b26e | 2016-08-12 11:23:31 +0200 | [diff] [blame] | 971 | for (st = curpeer->tables; st; st = st->next) { |
| 972 | st->update = st->last_pushed = st->teaching_origin; |
| 973 | st->flags = 0; |
| 974 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 975 | |
| 976 | /* reset teaching flags to 0 */ |
| 977 | curpeer->flags &= PEER_TEACH_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 978 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 979 | } |
| 980 | else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) { |
| 981 | if (msg_head[1] == PEER_MSG_STKT_DEFINE) { |
| 982 | int table_id_len; |
| 983 | struct shared_table *st; |
| 984 | int table_type; |
| 985 | int table_keylen; |
| 986 | int table_id; |
| 987 | uint64_t table_data; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 988 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 989 | table_id = intdecode(&msg_cur, msg_end); |
| 990 | if (!msg_cur) { |
| 991 | /* malformed message */ |
| 992 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 993 | goto switchstate; |
| 994 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 995 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 996 | table_id_len = intdecode(&msg_cur, msg_end); |
| 997 | if (!msg_cur) { |
| 998 | /* malformed message */ |
| 999 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1000 | goto switchstate; |
| 1001 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1002 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1003 | curpeer->remote_table = NULL; |
| 1004 | if (!table_id_len || (msg_cur + table_id_len) >= msg_end) { |
| 1005 | /* malformed message */ |
| 1006 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1007 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1008 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1009 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1010 | for (st = curpeer->tables; st; st = st->next) { |
| 1011 | /* Reset IDs */ |
| 1012 | if (st->remote_id == table_id) |
| 1013 | st->remote_id = 0; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 1014 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1015 | if (!curpeer->remote_table |
| 1016 | && (table_id_len == strlen(st->table->id)) |
| 1017 | && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) { |
| 1018 | curpeer->remote_table = st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1022 | if (!curpeer->remote_table) { |
| 1023 | goto ignore_msg; |
| 1024 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1025 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1026 | msg_cur += table_id_len; |
| 1027 | if (msg_cur >= msg_end) { |
| 1028 | /* malformed message */ |
| 1029 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1030 | goto switchstate; |
| 1031 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1032 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1033 | table_type = intdecode(&msg_cur, msg_end); |
| 1034 | if (!msg_cur) { |
| 1035 | /* malformed message */ |
| 1036 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1037 | goto switchstate; |
| 1038 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1039 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1040 | table_keylen = intdecode(&msg_cur, msg_end); |
| 1041 | if (!msg_cur) { |
| 1042 | /* malformed message */ |
| 1043 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1044 | goto switchstate; |
| 1045 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1046 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1047 | table_data = intdecode(&msg_cur, msg_end); |
| 1048 | if (!msg_cur) { |
| 1049 | /* malformed message */ |
| 1050 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1051 | goto switchstate; |
| 1052 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1053 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1054 | if (curpeer->remote_table->table->type != table_type |
| 1055 | || curpeer->remote_table->table->key_size != table_keylen) { |
| 1056 | curpeer->remote_table = NULL; |
| 1057 | goto ignore_msg; |
| 1058 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1059 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1060 | curpeer->remote_table->remote_data = table_data; |
| 1061 | curpeer->remote_table->remote_id = table_id; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1062 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1063 | else if (msg_head[1] == PEER_MSG_STKT_SWITCH) { |
| 1064 | struct shared_table *st; |
| 1065 | int 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 | table_id = intdecode(&msg_cur, msg_end); |
| 1068 | if (!msg_cur) { |
| 1069 | /* malformed message */ |
| 1070 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1071 | goto switchstate; |
| 1072 | } |
| 1073 | curpeer->remote_table = NULL; |
| 1074 | for (st = curpeer->tables; st; st = st->next) { |
| 1075 | if (st->remote_id == table_id) { |
| 1076 | curpeer->remote_table = st; |
| 1077 | break; |
| 1078 | } |
| 1079 | } |
| 1080 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1081 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1082 | else if (msg_head[1] == PEER_MSG_STKT_UPDATE |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1083 | || msg_head[1] == PEER_MSG_STKT_INCUPDATE |
| 1084 | || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED |
| 1085 | || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1086 | struct shared_table *st = curpeer->remote_table; |
| 1087 | uint32_t update; |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1088 | int expire; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1089 | unsigned int data_type; |
| 1090 | void *data_ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1091 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1092 | /* Here we have data message */ |
| 1093 | if (!st) |
| 1094 | goto ignore_msg; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1095 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1096 | expire = MS_TO_TICKS(st->table->expire); |
| 1097 | |
| 1098 | if (msg_head[1] == PEER_MSG_STKT_UPDATE || |
| 1099 | msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1100 | if (msg_len < sizeof(update)) { |
| 1101 | /* malformed message */ |
| 1102 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1103 | goto switchstate; |
| 1104 | } |
| 1105 | memcpy(&update, msg_cur, sizeof(update)); |
| 1106 | msg_cur += sizeof(update); |
| 1107 | st->last_get = htonl(update); |
| 1108 | } |
| 1109 | else { |
| 1110 | st->last_get++; |
| 1111 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1112 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1113 | if (msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || |
| 1114 | msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) { |
| 1115 | size_t expire_sz = sizeof expire; |
| 1116 | |
| 1117 | if (msg_cur + expire_sz > msg_end) { |
| 1118 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1119 | goto switchstate; |
| 1120 | } |
| 1121 | memcpy(&expire, msg_cur, expire_sz); |
| 1122 | msg_cur += expire_sz; |
| 1123 | expire = ntohl(expire); |
| 1124 | } |
| 1125 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1126 | newts = stksess_new(st->table, NULL); |
| 1127 | if (!newts) |
| 1128 | goto ignore_msg; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1129 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 1130 | if (st->table->type == SMP_T_STR) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1131 | unsigned int to_read, to_store; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1132 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1133 | to_read = intdecode(&msg_cur, msg_end); |
| 1134 | if (!msg_cur) { |
| 1135 | /* malformed message */ |
| 1136 | stksess_free(st->table, newts); |
| 1137 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1138 | goto switchstate; |
| 1139 | } |
| 1140 | to_store = MIN(to_read, st->table->key_size - 1); |
| 1141 | if (msg_cur + to_store > msg_end) { |
| 1142 | /* malformed message */ |
| 1143 | stksess_free(st->table, newts); |
| 1144 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1145 | goto switchstate; |
| 1146 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1147 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1148 | memcpy(newts->key.key, msg_cur, to_store); |
| 1149 | newts->key.key[to_store] = 0; |
| 1150 | msg_cur += to_read; |
| 1151 | } |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 1152 | else if (st->table->type == SMP_T_SINT) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1153 | unsigned int netinteger; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1154 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1155 | if (msg_cur + sizeof(netinteger) > msg_end) { |
| 1156 | /* malformed message */ |
| 1157 | stksess_free(st->table, newts); |
| 1158 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1159 | goto switchstate; |
| 1160 | } |
| 1161 | memcpy(&netinteger, msg_cur, sizeof(netinteger)); |
| 1162 | netinteger = ntohl(netinteger); |
| 1163 | memcpy(newts->key.key, &netinteger, sizeof(netinteger)); |
| 1164 | msg_cur += sizeof(netinteger); |
| 1165 | } |
| 1166 | else { |
| 1167 | if (msg_cur + st->table->key_size > msg_end) { |
| 1168 | /* malformed message */ |
| 1169 | stksess_free(st->table, newts); |
| 1170 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1171 | goto switchstate; |
| 1172 | } |
| 1173 | memcpy(newts->key.key, msg_cur, st->table->key_size); |
| 1174 | msg_cur += st->table->key_size; |
| 1175 | } |
| 1176 | |
| 1177 | /* lookup for existing entry */ |
| 1178 | ts = stktable_lookup(st->table, newts); |
| 1179 | if (ts) { |
| 1180 | /* the entry already exist, we can free ours */ |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1181 | 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] | 1182 | stksess_free(st->table, newts); |
| 1183 | newts = NULL; |
| 1184 | } |
| 1185 | else { |
| 1186 | struct eb32_node *eb; |
| 1187 | |
| 1188 | /* create new entry */ |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1189 | 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] | 1190 | newts = NULL; /* don't reuse it */ |
| 1191 | |
Emeric Brun | 234fc3c | 2015-12-16 15:16:46 +0100 | [diff] [blame] | 1192 | ts->upd.key= (++st->table->update)+(2147483648U); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1193 | eb = eb32_insert(&st->table->updates, &ts->upd); |
| 1194 | if (eb != &ts->upd) { |
| 1195 | eb32_delete(eb); |
| 1196 | eb32_insert(&st->table->updates, &ts->upd); |
| 1197 | } |
| 1198 | } |
| 1199 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1200 | for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1201 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1202 | if ((1 << data_type) & st->remote_data) { |
| 1203 | switch (stktable_data_types[data_type].std_type) { |
| 1204 | case STD_T_SINT: { |
| 1205 | int data; |
| 1206 | |
| 1207 | data = intdecode(&msg_cur, msg_end); |
| 1208 | if (!msg_cur) { |
| 1209 | /* malformed message */ |
| 1210 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1211 | goto switchstate; |
| 1212 | } |
| 1213 | |
| 1214 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1215 | if (data_ptr) |
| 1216 | stktable_data_cast(data_ptr, std_t_sint) = data; |
| 1217 | break; |
| 1218 | } |
| 1219 | case STD_T_UINT: { |
| 1220 | unsigned int data; |
| 1221 | |
| 1222 | data = intdecode(&msg_cur, msg_end); |
| 1223 | if (!msg_cur) { |
| 1224 | /* malformed message */ |
| 1225 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1226 | goto switchstate; |
| 1227 | } |
| 1228 | |
| 1229 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1230 | if (data_ptr) |
| 1231 | stktable_data_cast(data_ptr, std_t_uint) = data; |
| 1232 | break; |
| 1233 | } |
| 1234 | case STD_T_ULL: { |
| 1235 | unsigned long long data; |
| 1236 | |
| 1237 | data = intdecode(&msg_cur, msg_end); |
| 1238 | if (!msg_cur) { |
| 1239 | /* malformed message */ |
| 1240 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1241 | goto switchstate; |
| 1242 | } |
| 1243 | |
| 1244 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1245 | if (data_ptr) |
| 1246 | stktable_data_cast(data_ptr, std_t_ull) = data; |
| 1247 | break; |
| 1248 | } |
| 1249 | case STD_T_FRQP: { |
| 1250 | struct freq_ctr_period data; |
| 1251 | |
Willy Tarreau | 3bb4617 | 2016-03-25 18:17:47 +0100 | [diff] [blame] | 1252 | data.curr_tick = tick_add(now_ms, -intdecode(&msg_cur, msg_end)); |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1253 | if (!msg_cur) { |
| 1254 | /* malformed message */ |
| 1255 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1256 | goto switchstate; |
| 1257 | } |
| 1258 | data.curr_ctr = intdecode(&msg_cur, msg_end); |
| 1259 | if (!msg_cur) { |
| 1260 | /* malformed message */ |
| 1261 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1262 | goto switchstate; |
| 1263 | } |
| 1264 | data.prev_ctr = intdecode(&msg_cur, msg_end); |
| 1265 | if (!msg_cur) { |
| 1266 | /* malformed message */ |
| 1267 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1268 | goto switchstate; |
| 1269 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1270 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 1271 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1272 | if (data_ptr) |
| 1273 | stktable_data_cast(data_ptr, std_t_frqp) = data; |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | else if (msg_head[1] == PEER_MSG_STKT_ACK) { |
| 1281 | /* ack message */ |
| 1282 | uint32_t table_id ; |
| 1283 | uint32_t update; |
| 1284 | struct shared_table *st; |
| 1285 | |
| 1286 | table_id = intdecode(&msg_cur, msg_end); |
| 1287 | if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) { |
| 1288 | /* malformed message */ |
| 1289 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1290 | goto switchstate; |
| 1291 | } |
| 1292 | memcpy(&update, msg_cur, sizeof(update)); |
| 1293 | update = ntohl(update); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1294 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1295 | for (st = curpeer->tables; st; st = st->next) { |
| 1296 | if (st->local_id == table_id) { |
| 1297 | st->update = update; |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1302 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1303 | else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) { |
| 1304 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1305 | goto switchstate; |
| 1306 | } |
| 1307 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1308 | ignore_msg: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1309 | /* skip consumed message */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1310 | bo_skip(si_oc(si), totl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1311 | /* loop on that state to peek next message */ |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1312 | goto switchstate; |
| 1313 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1314 | incomplete: |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 1315 | /* we get here when a bo_getblk() returns <= 0 in reql */ |
| 1316 | |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1317 | if (reql < 0) { |
| 1318 | /* there was an error */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1319 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1320 | goto switchstate; |
| 1321 | } |
| 1322 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1323 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1324 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1325 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1326 | /* Need to request a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1327 | if ((curpeer->flags & PEER_F_LEARN_ASSIGN) && |
| 1328 | (peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 1329 | !(peers->flags & PEERS_F_RESYNC_PROCESS)) { |
| 1330 | unsigned char msg[2]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1331 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1332 | /* Current peer was elected to request a resync */ |
| 1333 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1334 | msg[1] = PEER_MSG_CTRL_RESYNCREQ; |
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 | /* message to buffer */ |
| 1337 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
| 1338 | if (repl <= 0) { |
| 1339 | /* no more write possible */ |
| 1340 | if (repl == -1) |
| 1341 | goto full; |
| 1342 | appctx->st0 = PEER_SESS_ST_END; |
| 1343 | goto switchstate; |
| 1344 | } |
| 1345 | peers->flags |= PEERS_F_RESYNC_PROCESS; |
| 1346 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1347 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1348 | /* Nothing to read, now we start to write */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1349 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1350 | if (curpeer->tables) { |
| 1351 | struct shared_table *st; |
| 1352 | struct shared_table *last_local_table; |
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 | last_local_table = curpeer->last_local_table; |
| 1355 | if (!last_local_table) |
| 1356 | last_local_table = curpeer->tables; |
| 1357 | st = last_local_table->next; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1358 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1359 | while (1) { |
| 1360 | if (!st) |
| 1361 | st = curpeer->tables; |
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 | /* It remains some updates to ack */ |
| 1364 | if (st->last_get != st->last_acked) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1365 | int msglen; |
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 | msglen = peer_prepare_ackmsg(st, trash.str, trash.size); |
| 1368 | if (!msglen) { |
| 1369 | /* internal error: message does not fit in trash */ |
| 1370 | appctx->st0 = PEER_SESS_ST_END; |
| 1371 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1374 | /* message to buffer */ |
| 1375 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1376 | if (repl <= 0) { |
| 1377 | /* no more write possible */ |
| 1378 | if (repl == -1) { |
| 1379 | goto full; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1380 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1381 | appctx->st0 = PEER_SESS_ST_END; |
| 1382 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1383 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1384 | st->last_acked = st->last_get; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1385 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1386 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1387 | if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) { |
| 1388 | if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) && |
| 1389 | ((int)(st->last_pushed - st->table->localupdate) < 0)) { |
| 1390 | struct eb32_node *eb; |
| 1391 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1392 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1393 | if (st != curpeer->last_local_table) { |
| 1394 | int msglen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1395 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1396 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1397 | if (!msglen) { |
| 1398 | /* internal error: message does not fit in trash */ |
| 1399 | appctx->st0 = PEER_SESS_ST_END; |
| 1400 | goto switchstate; |
| 1401 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1402 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1403 | /* message to buffer */ |
| 1404 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1405 | if (repl <= 0) { |
| 1406 | /* no more write possible */ |
| 1407 | if (repl == -1) { |
| 1408 | goto full; |
| 1409 | } |
| 1410 | appctx->st0 = PEER_SESS_ST_END; |
| 1411 | goto switchstate; |
| 1412 | } |
| 1413 | curpeer->last_local_table = st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1414 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1415 | |
| 1416 | /* We force new pushed to 1 to force identifier in update message */ |
| 1417 | new_pushed = 1; |
| 1418 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1419 | while (1) { |
| 1420 | uint32_t msglen; |
| 1421 | struct stksess *ts; |
| 1422 | |
| 1423 | /* push local updates */ |
| 1424 | if (!eb) { |
| 1425 | eb = eb32_first(&st->table->updates); |
| 1426 | if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 1427 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | if ((int)(eb->key - st->table->localupdate) > 0) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 1433 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1434 | break; |
| 1435 | } |
| 1436 | |
| 1437 | ts = eb32_entry(eb, struct stksess, upd); |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1438 | 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] | 1439 | if (!msglen) { |
| 1440 | /* internal error: message does not fit in trash */ |
| 1441 | appctx->st0 = PEER_SESS_ST_END; |
| 1442 | goto switchstate; |
| 1443 | } |
| 1444 | |
| 1445 | /* message to buffer */ |
| 1446 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1447 | if (repl <= 0) { |
| 1448 | /* no more write possible */ |
| 1449 | if (repl == -1) { |
| 1450 | goto full; |
| 1451 | } |
| 1452 | appctx->st0 = PEER_SESS_ST_END; |
| 1453 | goto switchstate; |
| 1454 | } |
| 1455 | st->last_pushed = ts->upd.key; |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 1456 | if ((int)(st->last_pushed - st->table->commitupdate) > 0) |
| 1457 | st->table->commitupdate = st->last_pushed; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1458 | /* identifier may not needed in next update message */ |
| 1459 | new_pushed = 0; |
| 1460 | |
| 1461 | eb = eb32_next(eb); |
| 1462 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1463 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1464 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1465 | else { |
| 1466 | if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) { |
| 1467 | struct eb32_node *eb; |
| 1468 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1469 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1470 | if (st != curpeer->last_local_table) { |
| 1471 | int msglen; |
| 1472 | |
| 1473 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1474 | if (!msglen) { |
| 1475 | /* internal error: message does not fit in trash */ |
| 1476 | appctx->st0 = PEER_SESS_ST_END; |
| 1477 | goto switchstate; |
| 1478 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1479 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1480 | /* message to buffer */ |
| 1481 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1482 | if (repl <= 0) { |
| 1483 | /* no more write possible */ |
| 1484 | if (repl == -1) { |
| 1485 | goto full; |
| 1486 | } |
| 1487 | appctx->st0 = PEER_SESS_ST_END; |
| 1488 | goto switchstate; |
| 1489 | } |
| 1490 | curpeer->last_local_table = st; |
| 1491 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1492 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1493 | /* We force new pushed to 1 to force identifier in update message */ |
| 1494 | new_pushed = 1; |
| 1495 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1496 | while (1) { |
| 1497 | uint32_t msglen; |
| 1498 | struct stksess *ts; |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1499 | int use_timed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1500 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1501 | /* push local updates */ |
| 1502 | if (!eb) { |
| 1503 | st->flags |= SHTABLE_F_TEACH_STAGE1; |
| 1504 | eb = eb32_first(&st->table->updates); |
| 1505 | if (eb) |
| 1506 | st->last_pushed = eb->key - 1; |
| 1507 | break; |
| 1508 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1509 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1510 | ts = eb32_entry(eb, struct stksess, upd); |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1511 | use_timed = !(curpeer->flags & PEER_F_DWNGRD); |
| 1512 | 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] | 1513 | if (!msglen) { |
| 1514 | /* internal error: message does not fit in trash */ |
| 1515 | appctx->st0 = PEER_SESS_ST_END; |
| 1516 | goto switchstate; |
| 1517 | } |
| 1518 | |
| 1519 | /* message to buffer */ |
| 1520 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1521 | if (repl <= 0) { |
| 1522 | /* no more write possible */ |
| 1523 | if (repl == -1) { |
| 1524 | goto full; |
| 1525 | } |
| 1526 | appctx->st0 = PEER_SESS_ST_END; |
| 1527 | goto switchstate; |
| 1528 | } |
| 1529 | st->last_pushed = ts->upd.key; |
| 1530 | /* identifier may not needed in next update message */ |
| 1531 | new_pushed = 0; |
| 1532 | |
| 1533 | eb = eb32_next(eb); |
| 1534 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1535 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1536 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1537 | if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) { |
| 1538 | struct eb32_node *eb; |
| 1539 | int new_pushed; |
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 != curpeer->last_local_table) { |
| 1542 | int msglen; |
| 1543 | |
| 1544 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1545 | if (!msglen) { |
| 1546 | /* internal error: message does not fit in trash */ |
| 1547 | appctx->st0 = PEER_SESS_ST_END; |
| 1548 | goto switchstate; |
| 1549 | } |
| 1550 | |
| 1551 | /* message to buffer */ |
| 1552 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1553 | if (repl <= 0) { |
| 1554 | /* no more write possible */ |
| 1555 | if (repl == -1) { |
| 1556 | goto full; |
| 1557 | } |
| 1558 | appctx->st0 = PEER_SESS_ST_END; |
| 1559 | goto switchstate; |
| 1560 | } |
| 1561 | curpeer->last_local_table = st; |
| 1562 | } |
| 1563 | |
| 1564 | /* We force new pushed to 1 to force identifier in update message */ |
| 1565 | new_pushed = 1; |
| 1566 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1567 | while (1) { |
| 1568 | uint32_t msglen; |
| 1569 | struct stksess *ts; |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1570 | int use_timed; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1571 | |
| 1572 | /* push local updates */ |
| 1573 | if (!eb || eb->key > st->teaching_origin) { |
| 1574 | st->flags |= SHTABLE_F_TEACH_STAGE2; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1575 | break; |
| 1576 | } |
| 1577 | |
| 1578 | ts = eb32_entry(eb, struct stksess, upd); |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1579 | use_timed = !(curpeer->flags & PEER_F_DWNGRD); |
| 1580 | 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] | 1581 | if (!msglen) { |
| 1582 | /* internal error: message does not fit in trash */ |
| 1583 | appctx->st0 = PEER_SESS_ST_END; |
| 1584 | goto switchstate; |
| 1585 | } |
| 1586 | |
| 1587 | /* message to buffer */ |
| 1588 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1589 | if (repl <= 0) { |
| 1590 | /* no more write possible */ |
| 1591 | if (repl == -1) { |
| 1592 | goto full; |
| 1593 | } |
| 1594 | appctx->st0 = PEER_SESS_ST_END; |
| 1595 | goto switchstate; |
| 1596 | } |
| 1597 | st->last_pushed = ts->upd.key; |
| 1598 | /* identifier may not needed in next update message */ |
| 1599 | new_pushed = 0; |
| 1600 | |
| 1601 | eb = eb32_next(eb); |
| 1602 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1603 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1604 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1605 | |
| 1606 | if (st == last_local_table) |
| 1607 | break; |
| 1608 | st = st->next; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1609 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | |
| 1613 | if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) { |
| 1614 | unsigned char msg[2]; |
| 1615 | |
| 1616 | /* Current peer was elected to request a resync */ |
| 1617 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1618 | msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL; |
| 1619 | /* process final lesson message */ |
| 1620 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
| 1621 | if (repl <= 0) { |
| 1622 | /* no more write possible */ |
| 1623 | if (repl == -1) |
| 1624 | goto full; |
| 1625 | appctx->st0 = PEER_SESS_ST_END; |
| 1626 | goto switchstate; |
| 1627 | } |
| 1628 | /* flag finished message sent */ |
| 1629 | curpeer->flags |= PEER_F_TEACH_FINISHED; |
| 1630 | } |
| 1631 | |
Emeric Brun | 597b26e | 2016-08-12 11:23:31 +0200 | [diff] [blame] | 1632 | /* Confirm finished or partial messages */ |
| 1633 | while (curpeer->confirm) { |
| 1634 | unsigned char msg[2]; |
| 1635 | |
| 1636 | /* There is a confirm messages to send */ |
| 1637 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1638 | msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM; |
| 1639 | |
| 1640 | /* message to buffer */ |
| 1641 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
| 1642 | if (repl <= 0) { |
| 1643 | /* no more write possible */ |
| 1644 | if (repl == -1) |
| 1645 | goto full; |
| 1646 | appctx->st0 = PEER_SESS_ST_END; |
| 1647 | goto switchstate; |
| 1648 | } |
| 1649 | curpeer->confirm--; |
| 1650 | } |
| 1651 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1652 | /* noting more to do */ |
| 1653 | goto out; |
| 1654 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1655 | case PEER_SESS_ST_EXIT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1656 | repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1657 | if (bi_putblk(si_ic(si), trash.str, repl) == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1658 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1659 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1660 | goto switchstate; |
| 1661 | case PEER_SESS_ST_ERRSIZE: { |
| 1662 | unsigned char msg[2]; |
| 1663 | |
| 1664 | msg[0] = PEER_MSG_CLASS_ERROR; |
| 1665 | msg[1] = PEER_MSG_ERR_SIZELIMIT; |
| 1666 | |
| 1667 | if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1) |
| 1668 | goto full; |
| 1669 | appctx->st0 = PEER_SESS_ST_END; |
| 1670 | goto switchstate; |
| 1671 | } |
| 1672 | case PEER_SESS_ST_ERRPROTO: { |
| 1673 | unsigned char msg[2]; |
| 1674 | |
| 1675 | msg[0] = PEER_MSG_CLASS_ERROR; |
| 1676 | msg[1] = PEER_MSG_ERR_PROTOCOL; |
| 1677 | |
| 1678 | if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1) |
| 1679 | goto full; |
| 1680 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1681 | /* fall through */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1682 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1683 | case PEER_SESS_ST_END: { |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1684 | si_shutw(si); |
| 1685 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1686 | si_ic(si)->flags |= CF_READ_NULL; |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1687 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | out: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1692 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1693 | return; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1694 | full: |
Willy Tarreau | fe12793 | 2015-04-21 19:23:39 +0200 | [diff] [blame] | 1695 | si_applet_cant_put(si); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1696 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 1699 | static struct applet peer_applet = { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1700 | .obj_type = OBJ_TYPE_APPLET, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1701 | .name = "<PEER>", /* used for logging */ |
| 1702 | .fct = peer_io_handler, |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 1703 | .release = peer_session_release, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1704 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1705 | |
| 1706 | /* |
| 1707 | * Use this function to force a close of a peer session |
| 1708 | */ |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 1709 | static void peer_session_forceshutdown(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1710 | { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1711 | struct peer *ps; |
| 1712 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1713 | if (!appctx) |
| 1714 | return; |
| 1715 | |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 1716 | if (appctx->applet != &peer_applet) |
| 1717 | return; |
| 1718 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1719 | ps = appctx->ctx.peers.ptr; |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1720 | /* we're killing a connection, we must apply a random delay before |
| 1721 | * retrying otherwise the other end will do the same and we can loop |
| 1722 | * for a while. |
| 1723 | */ |
| 1724 | if (ps) |
| 1725 | ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000)); |
| 1726 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1727 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1728 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | 78c0c50 | 2016-10-31 17:32:20 +0100 | [diff] [blame] | 1729 | appctx_wakeup(appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1730 | } |
| 1731 | |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1732 | /* Pre-configures a peers frontend to accept incoming connections */ |
| 1733 | void peers_setup_frontend(struct proxy *fe) |
| 1734 | { |
| 1735 | fe->last_change = now.tv_sec; |
| 1736 | fe->cap = PR_CAP_FE; |
| 1737 | fe->maxconn = 0; |
| 1738 | fe->conn_retries = CONN_RETRIES; |
| 1739 | fe->timeout.client = MS_TO_TICKS(5000); |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 1740 | fe->accept = frontend_accept; |
Willy Tarreau | f87ab94 | 2015-03-13 15:55:16 +0100 | [diff] [blame] | 1741 | fe->default_target = &peer_applet.obj_type; |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1742 | fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
Willy Tarreau | 0fca483 | 2015-05-01 19:12:05 +0200 | [diff] [blame] | 1743 | fe->bind_proc = 0; /* will be filled by users */ |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1744 | } |
| 1745 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1746 | /* |
Willy Tarreau | bd55e31 | 2010-11-11 10:55:09 +0100 | [diff] [blame] | 1747 | * Create a new peer session in assigned state (connect will start automatically) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1748 | */ |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1749 | static struct appctx *peer_session_create(struct peers *peers, struct peer *peer) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1750 | { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1751 | struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1752 | struct proxy *p = l->frontend; /* attached frontend */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1753 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1754 | struct session *sess; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1755 | struct stream *s; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1756 | struct task *t; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1757 | struct connection *conn; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1758 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1759 | peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 1760 | peer->statuscode = PEER_SESS_SC_CONNECTCODE; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1761 | s = NULL; |
| 1762 | |
| 1763 | appctx = appctx_new(&peer_applet); |
| 1764 | if (!appctx) |
| 1765 | goto out_close; |
| 1766 | |
| 1767 | appctx->st0 = PEER_SESS_ST_CONNECT; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1768 | appctx->ctx.peers.ptr = (void *)peer; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1769 | |
Willy Tarreau | 4099a7c | 2015-04-05 00:39:55 +0200 | [diff] [blame] | 1770 | sess = session_new(p, l, &appctx->obj_type); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1771 | if (!sess) { |
Godbach | 430f291 | 2013-06-20 13:28:38 +0800 | [diff] [blame] | 1772 | Alert("out of memory in peer_session_create().\n"); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1773 | goto out_free_appctx; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1776 | if ((t = task_new()) == NULL) { |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1777 | Alert("out of memory in peer_session_create().\n"); |
| 1778 | goto out_free_sess; |
| 1779 | } |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1780 | t->nice = l->nice; |
| 1781 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 1782 | if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) { |
Willy Tarreau | 342bfb1 | 2015-04-05 01:35:34 +0200 | [diff] [blame] | 1783 | Alert("Failed to initialize stream in peer_session_create().\n"); |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1784 | goto out_free_task; |
| 1785 | } |
| 1786 | |
Willy Tarreau | 342bfb1 | 2015-04-05 01:35:34 +0200 | [diff] [blame] | 1787 | /* The tasks below are normally what is supposed to be done by |
| 1788 | * fe->accept(). |
| 1789 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1790 | s->flags = SF_ASSIGNED|SF_ADDR_SET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1791 | |
Willy Tarreau | 6e2979c | 2015-04-27 13:21:15 +0200 | [diff] [blame] | 1792 | /* applet is waiting for data */ |
| 1793 | si_applet_cant_get(&s->si[0]); |
| 1794 | appctx_wakeup(appctx); |
| 1795 | |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1796 | /* initiate an outgoing connection */ |
| 1797 | si_set_state(&s->si[1], SI_ST_ASS); |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1798 | |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1799 | /* automatically prepare the stream interface to connect to the |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1800 | * pre-initialized connection in si->conn. |
| 1801 | */ |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1802 | if (unlikely((conn = conn_new()) == NULL)) |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1803 | goto out_free_strm; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1804 | |
| 1805 | conn_prepare(conn, peer->proto, peer->xprt); |
| 1806 | si_attach_conn(&s->si[1], conn); |
| 1807 | |
| 1808 | conn->target = s->target = &s->be->obj_type; |
| 1809 | memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1810 | s->do_log = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1811 | s->uniq_id = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1812 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1813 | s->res.flags |= CF_READ_DONTWAIT; |
Willy Tarreau | 696a291 | 2014-11-24 11:36:57 +0100 | [diff] [blame] | 1814 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1815 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 1816 | p->feconn++;/* beconn will be increased later */ |
| 1817 | jobs++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1818 | if (!(s->sess->listener->options & LI_O_UNLIMITED)) |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1819 | actconn++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1820 | totalconn++; |
| 1821 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1822 | peer->appctx = appctx; |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1823 | return appctx; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1824 | |
| 1825 | /* Error unrolling */ |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1826 | out_free_strm: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1827 | LIST_DEL(&s->list); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1828 | pool_free2(pool2_stream, s); |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1829 | out_free_task: |
| 1830 | task_free(t); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1831 | out_free_sess: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 1832 | session_free(sess); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1833 | out_free_appctx: |
| 1834 | appctx_free(appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1835 | out_close: |
Willy Tarreau | b21d08e | 2016-10-31 17:46:57 +0100 | [diff] [blame] | 1836 | return NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * Task processing function to manage re-connect and peer session |
| 1841 | * tasks wakeup on local update. |
| 1842 | */ |
Simon Horman | 9655377 | 2011-06-08 09:18:51 +0900 | [diff] [blame] | 1843 | static struct task *process_peer_sync(struct task * task) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1844 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1845 | struct peers *peers = task->context; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1846 | struct peer *ps; |
| 1847 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1848 | |
| 1849 | task->expire = TICK_ETERNITY; |
| 1850 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1851 | if (!peers->peers_fe) { |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 1852 | /* this one was never started, kill it */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1853 | signal_unregister_handler(peers->sighandler); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1854 | task_delete(peers->sync_task); |
| 1855 | task_free(peers->sync_task); |
Willy Tarreau | 37bb7be | 2015-09-21 15:24:58 +0200 | [diff] [blame] | 1856 | peers->sync_task = NULL; |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 1857 | return NULL; |
| 1858 | } |
| 1859 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1860 | if (!stopping) { |
| 1861 | /* Normal case (not soft stop)*/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1862 | |
| 1863 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) && |
| 1864 | (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) && |
| 1865 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1866 | /* Resync from local peer needed |
| 1867 | no peer was assigned for the lesson |
| 1868 | and no old local peer found |
| 1869 | or resync timeout expire */ |
| 1870 | |
| 1871 | /* flag no more resync from local, to try resync from remotes */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1872 | peers->flags |= PEERS_F_RESYNC_LOCAL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1873 | |
| 1874 | /* reschedule a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1875 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | /* For each session */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1879 | for (ps = peers->remote; ps; ps = ps->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1880 | /* For each remote peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1881 | if (!ps->local) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1882 | if (!ps->appctx) { |
| 1883 | /* no active peer connection */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1884 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1885 | ((ps->statuscode == PEER_SESS_SC_CONNECTCODE || |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1886 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1887 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1888 | tick_is_expired(ps->reconnect, now_ms))) { |
| 1889 | /* connection never tried |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1890 | * or previous peer connection established with success |
| 1891 | * or previous peer connection failed while connecting |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1892 | * and reconnection timer is expired */ |
| 1893 | |
| 1894 | /* retry a connect */ |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1895 | ps->appctx = peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1896 | } |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1897 | else if (!tick_is_expired(ps->reconnect, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1898 | /* If previous session failed during connection |
| 1899 | * but reconnection timer is not expired */ |
| 1900 | |
| 1901 | /* reschedule task for reconnect */ |
| 1902 | task->expire = tick_first(task->expire, ps->reconnect); |
| 1903 | } |
| 1904 | /* else do nothing */ |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1905 | } /* !ps->appctx */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1906 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1907 | /* current peer connection is active and established */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1908 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 1909 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1910 | !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) { |
| 1911 | /* Resync from a remote is needed |
| 1912 | * and no peer was assigned for lesson |
| 1913 | * and current peer may be up2date */ |
| 1914 | |
| 1915 | /* assign peer for the lesson */ |
| 1916 | ps->flags |= PEER_F_LEARN_ASSIGN; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1917 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1918 | |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1919 | /* wake up peer handler to handle a request of resync */ |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 1920 | appctx_wakeup(ps->appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1921 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1922 | else { |
| 1923 | /* Awake session if there is data to push */ |
| 1924 | for (st = ps->tables; st ; st = st->next) { |
| 1925 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1926 | /* wake up the peer handler to push local updates */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1927 | appctx_wakeup(ps->appctx); |
| 1928 | break; |
| 1929 | } |
| 1930 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1931 | } |
| 1932 | /* else do nothing */ |
| 1933 | } /* SUCCESSCODE */ |
| 1934 | } /* !ps->peer->local */ |
| 1935 | } /* for */ |
| 1936 | |
| 1937 | /* Resync from remotes expired: consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1938 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 1939 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 1940 | tick_is_expired(peers->resync_timeout, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1941 | /* Resync from remote peer needed |
| 1942 | * no peer was assigned for the lesson |
| 1943 | * and resync timeout expire */ |
| 1944 | |
| 1945 | /* flag no more resync from remote, consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1946 | peers->flags |= PEERS_F_RESYNC_REMOTE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1949 | if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1950 | /* Resync not finished*/ |
| 1951 | /* reschedule task to resync timeout, to ended resync if needed */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1952 | task->expire = tick_first(task->expire, peers->resync_timeout); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1953 | } |
| 1954 | } /* !stopping */ |
| 1955 | else { |
| 1956 | /* soft stop case */ |
| 1957 | if (task->state & TASK_WOKEN_SIGNAL) { |
| 1958 | /* We've just recieved the signal */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1959 | if (!(peers->flags & PEERS_F_DONOTSTOP)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1960 | /* add DO NOT STOP flag if not present */ |
| 1961 | jobs++; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1962 | peers->flags |= PEERS_F_DONOTSTOP; |
| 1963 | ps = peers->local; |
| 1964 | for (st = ps->tables; st ; st = st->next) |
| 1965 | st->table->syncing++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | /* disconnect all connected peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1969 | for (ps = peers->remote; ps; ps = ps->next) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1970 | if (ps->appctx) { |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 1971 | peer_session_forceshutdown(ps->appctx); |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 1972 | ps->appctx = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1973 | } |
| 1974 | } |
| 1975 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1976 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1977 | ps = peers->local; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1978 | if (ps->flags & PEER_F_TEACH_COMPLETE) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1979 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1980 | /* resync of new process was complete, current process can die now */ |
| 1981 | jobs--; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1982 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 1983 | for (st = ps->tables; st ; st = st->next) |
| 1984 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1985 | } |
| 1986 | } |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1987 | else if (!ps->appctx) { |
| 1988 | /* If there's no active peer connection */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1989 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1990 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
| 1991 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE || |
| 1992 | ps->statuscode == PEER_SESS_SC_TRYAGAIN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1993 | /* connection never tried |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 1994 | * or previous peer connection was successfully established |
| 1995 | * or previous tcp connect succeeded but init state incomplete |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1996 | * or during previous connect, peer replies a try again statuscode */ |
| 1997 | |
| 1998 | /* connect to the peer */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1999 | peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2000 | } |
| 2001 | else { |
| 2002 | /* Other error cases */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2003 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2004 | /* unable to resync new process, current process can die now */ |
| 2005 | jobs--; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2006 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 2007 | for (st = ps->tables; st ; st = st->next) |
| 2008 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2009 | } |
| 2010 | } |
| 2011 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2012 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2013 | /* current peer connection is active and established |
| 2014 | * wake up all peer handlers to push remaining local updates */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2015 | for (st = ps->tables; st ; st = st->next) { |
| 2016 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2017 | appctx_wakeup(ps->appctx); |
| 2018 | break; |
| 2019 | } |
| 2020 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2021 | } |
| 2022 | } /* stopping */ |
| 2023 | /* Wakeup for re-connect */ |
| 2024 | return task; |
| 2025 | } |
| 2026 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2027 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2028 | /* |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2029 | * |
| 2030 | */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2031 | void peers_init_sync(struct peers *peers) |
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 | struct peer * curpeer; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2034 | struct listener *listener; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2035 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2036 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2037 | peers->peers_fe->maxconn += 3; |
| 2038 | } |
| 2039 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 2040 | list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe) |
| 2041 | listener->maxconn = peers->peers_fe->maxconn; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2042 | peers->sync_task = task_new(); |
| 2043 | peers->sync_task->process = process_peer_sync; |
| 2044 | peers->sync_task->expire = TICK_ETERNITY; |
| 2045 | peers->sync_task->context = (void *)peers; |
| 2046 | peers->sighandler = signal_register_task(0, peers->sync_task, 0); |
| 2047 | task_wakeup(peers->sync_task, TASK_WOKEN_INIT); |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | |
| 2052 | /* |
| 2053 | * Function used to register a table for sync on a group of peers |
| 2054 | * |
| 2055 | */ |
| 2056 | void peers_register_table(struct peers *peers, struct stktable *table) |
| 2057 | { |
| 2058 | struct shared_table *st; |
| 2059 | struct peer * curpeer; |
| 2060 | int id = 0; |
| 2061 | |
| 2062 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 2063 | st = calloc(1,sizeof(*st)); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2064 | st->table = table; |
| 2065 | st->next = curpeer->tables; |
| 2066 | if (curpeer->tables) |
| 2067 | id = curpeer->tables->local_id; |
| 2068 | st->local_id = id + 1; |
| 2069 | |
| 2070 | curpeer->tables = st; |
| 2071 | } |
| 2072 | |
| 2073 | table->sync_task = peers->sync_task; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |