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