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