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