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