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