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