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