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