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