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