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