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