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