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