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