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