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