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