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