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