blob: 1d316e7d84551cb2d4c3ddfad5c3d14eac52b99f [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
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 Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/applet.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020025#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020027#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020029#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020030#include <haproxy/frontend.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020032#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020033#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020035#include <haproxy/session-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020036#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stick_table.h>
39#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020040#include <haproxy/stream_interface.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/task.h>
42#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020043#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020044#include <haproxy/tools.h>
Frédéric Lécailled8659352020-11-10 16:18:03 +010045#include <haproxy/trace.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020046
Emeric Brun2b920a12010-09-23 18:30:22 +020047
48/*******************************/
49/* Current peer learning state */
50/*******************************/
51
52/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020053/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020054/******************************/
Emeric Brunccdfbae2021-04-28 12:59:35 +020055#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
56#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
57#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
58#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
59#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
60#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
61#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
62#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
63#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
64#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
65#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
66#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
67#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
68#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
69#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
70#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
71 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020072
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010073#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
74#define PEERS_RESYNC_FROMLOCAL 0x00000000
75#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
76#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020077
78/***********************************/
79/* Current shared table sync state */
80/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010081#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
82#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020083
84/******************************/
85/* Remote peer teaching state */
86/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010087#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
88#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
89#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
90#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
91#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
92#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
93#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
94#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020095
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010096#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
97#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020098
Frédéric Lécaille54bff832019-03-26 10:25:20 +010099#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
100#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Christopher Fauletd73a5cc2022-07-26 19:14:36 +0200101#define PEER_LOCAL_RECONNECT_TIMEOUT 500 /* 500ms */
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100102#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
103
Willy Tarreau49962b52021-02-12 16:56:22 +0100104/* flags for "show peers" */
105#define PEERS_SHOW_F_DICT 0x00000001 /* also show the contents of the dictionary */
106
Emeric Brunb3971ab2015-05-12 18:49:09 +0200107/*****************************/
108/* Sync message class */
109/*****************************/
110enum {
111 PEER_MSG_CLASS_CONTROL = 0,
112 PEER_MSG_CLASS_ERROR,
113 PEER_MSG_CLASS_STICKTABLE = 10,
114 PEER_MSG_CLASS_RESERVED = 255,
115};
116
117/*****************************/
118/* control message types */
119/*****************************/
120enum {
121 PEER_MSG_CTRL_RESYNCREQ = 0,
122 PEER_MSG_CTRL_RESYNCFINISHED,
123 PEER_MSG_CTRL_RESYNCPARTIAL,
124 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100125 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200126};
127
128/*****************************/
129/* error message types */
130/*****************************/
131enum {
132 PEER_MSG_ERR_PROTOCOL = 0,
133 PEER_MSG_ERR_SIZELIMIT,
134};
135
Emeric Brun530ba382020-06-02 11:17:42 +0200136/* network key types;
137 * network types were directly and mistakenly
138 * mapped on sample types, to keep backward
139 * compatiblitiy we keep those values but
140 * we now use a internal/network mapping
141 * to avoid further mistakes adding or
142 * modifying internals types
143 */
144enum {
145 PEER_KT_ANY = 0, /* any type */
146 PEER_KT_RESV1, /* UNUSED */
147 PEER_KT_SINT, /* signed 64bits integer type */
148 PEER_KT_RESV3, /* UNUSED */
149 PEER_KT_IPV4, /* ipv4 type */
150 PEER_KT_IPV6, /* ipv6 type */
151 PEER_KT_STR, /* char string type */
152 PEER_KT_BIN, /* buffer type */
153 PEER_KT_TYPES /* number of types, must always be last */
154};
155
156/* Map used to retrieve network type from internal type
157 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
158 */
159static int peer_net_key_type[SMP_TYPES] = {
160 [SMP_T_SINT] = PEER_KT_SINT,
161 [SMP_T_IPV4] = PEER_KT_IPV4,
162 [SMP_T_IPV6] = PEER_KT_IPV6,
163 [SMP_T_STR] = PEER_KT_STR,
164 [SMP_T_BIN] = PEER_KT_BIN,
165};
166
167/* Map used to retrieve internal type from external type
168 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
169 */
170static int peer_int_key_type[PEER_KT_TYPES] = {
171 [PEER_KT_SINT] = SMP_T_SINT,
172 [PEER_KT_IPV4] = SMP_T_IPV4,
173 [PEER_KT_IPV6] = SMP_T_IPV6,
174 [PEER_KT_STR] = SMP_T_STR,
175 [PEER_KT_BIN] = SMP_T_BIN,
176};
177
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100178/*
179 * Parameters used by functions to build peer protocol messages. */
180struct peer_prep_params {
181 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100182 struct peer *peer;
183 } hello;
184 struct {
185 unsigned int st1;
186 } error_status;
187 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100188 struct stksess *stksess;
189 struct shared_table *shared_table;
190 unsigned int updateid;
191 int use_identifier;
192 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200193 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100194 } updt;
195 struct {
196 struct shared_table *shared_table;
197 } swtch;
198 struct {
199 struct shared_table *shared_table;
200 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100201 struct {
202 unsigned char head[2];
203 } control;
204 struct {
205 unsigned char head[2];
206 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100207};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200208
209/*******************************/
210/* stick table sync mesg types */
211/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500212/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200213/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200214#define PEER_MSG_STKT_UPDATE 0x80
215#define PEER_MSG_STKT_INCUPDATE 0x81
216#define PEER_MSG_STKT_DEFINE 0x82
217#define PEER_MSG_STKT_SWITCH 0x83
218#define PEER_MSG_STKT_ACK 0x84
219#define PEER_MSG_STKT_UPDATE_TIMED 0x85
220#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200221/* All the stick-table message identifiers abova have the #7 bit set */
222#define PEER_MSG_STKT_BIT 7
223#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200224
Frédéric Lécaille39143342019-05-24 14:32:27 +0200225/* The maximum length of an encoded data length. */
226#define PEER_MSG_ENC_LENGTH_MAXLEN 5
227
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200228/* Minimum 64-bits value encoded with 2 bytes */
229#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
230/* 3 bytes */
231#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
232/* 4 bytes */
233#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
234/* 5 bytes */
235#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
236/* 6 bytes */
237#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
238/* 7 bytes */
239#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
240/* 8 bytes */
241#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
242/* 9 bytes */
243#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
244/* 10 bytes */
245#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
246
247/* #7 bit used to detect the last byte to be encoded */
248#define PEER_ENC_STOP_BIT 7
249/* The byte minimum value with #7 bit set */
250#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
251/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
252#define PEER_ENC_2BYTES_MIN_BITS 4
253
Frédéric Lécaille39143342019-05-24 14:32:27 +0200254#define PEER_MSG_HEADER_LEN 2
255
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200256#define PEER_STKT_CACHE_MAX_ENTRIES 128
257
Emeric Brun2b920a12010-09-23 18:30:22 +0200258/**********************************/
259/* Peer Session IO handler states */
260/**********************************/
261
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100262enum {
263 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
264 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
265 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
266 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100267 /* after this point, data were possibly exchanged */
268 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
269 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
270 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
271 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
272 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200273 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
274 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100275 PEER_SESS_ST_END, /* Killed session */
276};
Emeric Brun2b920a12010-09-23 18:30:22 +0200277
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100278/***************************************************/
279/* Peer Session status code - part of the protocol */
280/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200281
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100282#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
283#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200284
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100285#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200286
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100287#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200288
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100289#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
290#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
291#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
292#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200293
294#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200295#define PEER_MAJOR_VER 2
296#define PEER_MINOR_VER 1
297#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200298
Willy Tarreau6254a922019-01-29 17:45:23 +0100299static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200300struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200301static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200302
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200303static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
304 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200305static inline void flush_dcache(struct peer *peer);
306
Frédéric Lécailled8659352020-11-10 16:18:03 +0100307/* trace source and events */
308static void peers_trace(enum trace_level level, uint64_t mask,
309 const struct trace_source *src,
310 const struct ist where, const struct ist func,
311 const void *a1, const void *a2, const void *a3, const void *a4);
312
313static const struct trace_event peers_trace_events[] = {
314#define PEERS_EV_UPDTMSG (1 << 0)
315 { .mask = PEERS_EV_UPDTMSG, .name = "updtmsg", .desc = "update message received" },
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100316#define PEERS_EV_ACKMSG (1 << 1)
317 { .mask = PEERS_EV_ACKMSG, .name = "ackmsg", .desc = "ack message received" },
318#define PEERS_EV_SWTCMSG (1 << 2)
319 { .mask = PEERS_EV_SWTCMSG, .name = "swtcmsg", .desc = "switch message received" },
320#define PEERS_EV_DEFMSG (1 << 3)
321 { .mask = PEERS_EV_DEFMSG, .name = "defmsg", .desc = "definition message received" },
322#define PEERS_EV_CTRLMSG (1 << 4)
323 { .mask = PEERS_EV_CTRLMSG, .name = "ctrlmsg", .desc = "control message sent/received" },
324#define PEERS_EV_SESSREL (1 << 5)
325 { .mask = PEERS_EV_SESSREL, .name = "sessrl", .desc = "peer session releasing" },
326#define PEERS_EV_PROTOERR (1 << 6)
327 { .mask = PEERS_EV_PROTOERR, .name = "protoerr", .desc = "protocol error" },
Willy Tarreaua65b07d2024-04-12 12:01:31 +0200328 { }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100329};
330
331static const struct name_desc peers_trace_lockon_args[4] = {
332 /* arg1 */ { /* already used by the connection */ },
333 /* arg2 */ { .name="peers", .desc="Peers protocol" },
334 /* arg3 */ { },
335 /* arg4 */ { }
336};
337
338static const struct name_desc peers_trace_decoding[] = {
339#define PEERS_VERB_CLEAN 1
340 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
341 { /* end */ }
342};
343
344
345struct trace_source trace_peers = {
346 .name = IST("peers"),
347 .desc = "Peers protocol",
348 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
349 .default_cb = peers_trace,
350 .known_events = peers_trace_events,
351 .lockon_args = peers_trace_lockon_args,
352 .decoding = peers_trace_decoding,
353 .report_events = ~0, /* report everything by default */
354};
355
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100356/* Return peer control message types as strings (only for debugging purpose). */
357static inline char *ctrl_msg_type_str(unsigned int type)
358{
359 switch (type) {
360 case PEER_MSG_CTRL_RESYNCREQ:
361 return "RESYNCREQ";
362 case PEER_MSG_CTRL_RESYNCFINISHED:
363 return "RESYNCFINISHED";
364 case PEER_MSG_CTRL_RESYNCPARTIAL:
365 return "RESYNCPARTIAL";
366 case PEER_MSG_CTRL_RESYNCCONFIRM:
367 return "RESYNCCONFIRM";
368 case PEER_MSG_CTRL_HEARTBEAT:
369 return "HEARTBEAT";
370 default:
371 return "???";
372 }
373}
374
Frédéric Lécailled8659352020-11-10 16:18:03 +0100375#define TRACE_SOURCE &trace_peers
376INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
377
378static void peers_trace(enum trace_level level, uint64_t mask,
379 const struct trace_source *src,
380 const struct ist where, const struct ist func,
381 const void *a1, const void *a2, const void *a3, const void *a4)
382{
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100383 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécailled8659352020-11-10 16:18:03 +0100384 if (a2) {
385 const struct peer *peer = a2;
386
387 chunk_appendf(&trace_buf, " peer=%s", peer->id);
388 }
389 if (a3) {
390 const char *p = a3;
391
392 chunk_appendf(&trace_buf, " @%p", p);
393 }
394 if (a4) {
395 const size_t *val = a4;
396
397 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
398 }
399 }
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100400
401 if (mask & PEERS_EV_DEFMSG) {
402 if (a2) {
403 const struct peer *peer = a2;
404
405 chunk_appendf(&trace_buf, " peer=%s", peer->id);
406 }
407 if (a3) {
408 const char *p = a3;
409
410 chunk_appendf(&trace_buf, " @%p", p);
411 }
412 if (a4) {
413 const int *val = a4;
414
415 chunk_appendf(&trace_buf, " %d", *val);
416 }
417 }
418
419 if (mask & PEERS_EV_CTRLMSG) {
420 if (a2) {
421 const unsigned char *ctrl_msg_type = a2;
422
423 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
424
425 }
426 if (a3) {
427 const char *local_peer = a3;
428
429 chunk_appendf(&trace_buf, " %s", local_peer);
430 }
431
432 if (a4) {
433 const char *remote_peer = a4;
434
435 chunk_appendf(&trace_buf, " -> %s", remote_peer);
436 }
437 }
438
439 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
440 if (a2) {
441 const struct peer *peer = a2;
442 struct peers *peers = NULL;
443
Frédéric Lécaille4b1a05f2021-01-17 13:08:39 +0100444 if (peer && peer->appctx) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100445 struct stream_interface *si;
446
447 si = peer->appctx->owner;
448 if (si) {
449 struct stream *s = si_strm(si);
450
451 peers = strm_fe(s)->parent;
452 }
453 }
454
455 if (peers)
456 chunk_appendf(&trace_buf, " %s", peers->local->id);
457 if (peer)
458 chunk_appendf(&trace_buf, " -> %s", peer->id);
459 }
460
461 if (a3) {
462 const int *prev_state = a3;
463
464 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
465 }
466 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100467}
468
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200469static const char *statuscode_str(int statuscode)
470{
471 switch (statuscode) {
472 case PEER_SESS_SC_CONNECTCODE:
473 return "CONN";
474 case PEER_SESS_SC_CONNECTEDCODE:
475 return "HSHK";
476 case PEER_SESS_SC_SUCCESSCODE:
477 return "ESTA";
478 case PEER_SESS_SC_TRYAGAIN:
479 return "RETR";
480 case PEER_SESS_SC_ERRPROTO:
481 return "PROT";
482 case PEER_SESS_SC_ERRVERSION:
483 return "VERS";
484 case PEER_SESS_SC_ERRHOST:
485 return "NAME";
486 case PEER_SESS_SC_ERRPEER:
487 return "UNKN";
488 default:
489 return "NONE";
490 }
491}
492
Emeric Brun18928af2017-03-29 16:32:53 +0200493/* This function encode an uint64 to 'dynamic' length format.
494 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500495 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200496 At return, the *str is set at the next Byte after then
497 encoded integer. The function returns then length of the
498 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200499int intencode(uint64_t i, char **str) {
500 int idx = 0;
501 unsigned char *msg;
502
Emeric Brunb3971ab2015-05-12 18:49:09 +0200503 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200504 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200505 msg[0] = (unsigned char)i;
506 *str = (char *)&msg[idx+1];
507 return (idx+1);
508 }
509
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200510 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
511 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
512 while (i >= PEER_ENC_STOP_BYTE) {
513 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
514 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200515 }
516 msg[++idx] = (unsigned char)i;
517 *str = (char *)&msg[idx+1];
518 return (idx+1);
519}
520
521
522/* This function returns the decoded integer or 0
523 if decode failed
524 *str point on the beginning of the integer to decode
525 at the end of decoding *str point on the end of the
526 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200527uint64_t intdecode(char **str, char *end)
528{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200529 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200530 uint64_t i;
531 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200532
533 if (!*str)
534 return 0;
535
536 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200537 if (msg >= (unsigned char *)end)
538 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200539
Emeric Brun18928af2017-03-29 16:32:53 +0200540 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200541 if (i >= PEER_ENC_2BYTES_MIN) {
542 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200543 do {
544 if (msg >= (unsigned char *)end)
545 goto fail;
546 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200547 shift += PEER_ENC_STOP_BIT;
548 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200549 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100550 *str = (char *)msg;
551 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200552
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100553 fail:
554 *str = NULL;
555 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200556}
Emeric Brun2b920a12010-09-23 18:30:22 +0200557
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100558/*
559 * Build a "hello" peer protocol message.
560 * Return the number of written bytes written to build this messages if succeeded,
561 * 0 if not.
562 */
563static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
564{
565 int min_ver, ret;
566 struct peer *peer;
567
568 peer = p->hello.peer;
569 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
570 /* Prepare headers */
571 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
572 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
573 if (ret >= size)
574 return 0;
575
576 return ret;
577}
578
579/*
580 * Build a "handshake succeeded" status message.
581 * Return the number of written bytes written to build this messages if succeeded,
582 * 0 if not.
583 */
584static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
585{
586 int ret;
587
588 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
589 if (ret >= size)
590 return 0;
591
592 return ret;
593}
594
595/*
596 * Build an error status message.
597 * Return the number of written bytes written to build this messages if succeeded,
598 * 0 if not.
599 */
600static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
601{
602 int ret;
603 unsigned int st1;
604
605 st1 = p->error_status.st1;
606 ret = snprintf(msg, size, "%d\n", st1);
607 if (ret >= size)
608 return 0;
609
610 return ret;
611}
612
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200613/* Set the stick-table UPDATE message type byte at <msg_type> address,
614 * depending on <use_identifier> and <use_timed> boolean parameters.
615 * Always successful.
616 */
617static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
618{
619 if (use_timed) {
620 if (use_identifier)
621 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
622 else
623 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
624 }
625 else {
626 if (use_identifier)
627 *msg_type = PEER_MSG_STKT_UPDATE;
628 else
629 *msg_type = PEER_MSG_STKT_INCUPDATE;
630 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200631}
Emeric Brun2b920a12010-09-23 18:30:22 +0200632/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200633 * This prepare the data update message on the stick session <ts>, <st> is the considered
634 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800635 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200636 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
637 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200638 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100639static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200640{
641 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200642 unsigned short datalen;
643 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200644 unsigned int data_type;
645 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100646 struct stksess *ts;
647 struct shared_table *st;
648 unsigned int updateid;
649 int use_identifier;
650 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200651 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100652
653 ts = p->updt.stksess;
654 st = p->updt.shared_table;
655 updateid = p->updt.updateid;
656 use_identifier = p->updt.use_identifier;
657 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200658 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200659
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200660 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200661
Emeric Brun2b920a12010-09-23 18:30:22 +0200662 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200663
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500664 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200665 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200666 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200667 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200668
669 /* encode update identifier if needed */
670 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200671 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200672 memcpy(cursor, &netinteger, sizeof(netinteger));
673 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200674 }
675
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200676 if (use_timed) {
677 netinteger = htonl(tick_remain(now_ms, ts->expire));
678 memcpy(cursor, &netinteger, sizeof(netinteger));
679 cursor += sizeof(netinteger);
680 }
681
Emeric Brunb3971ab2015-05-12 18:49:09 +0200682 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200683 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200684 int stlen = strlen((char *)ts->key.key);
685
Emeric Brunb3971ab2015-05-12 18:49:09 +0200686 intencode(stlen, &cursor);
687 memcpy(cursor, ts->key.key, stlen);
688 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200689 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200690 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100691 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200692 memcpy(cursor, &netinteger, sizeof(netinteger));
693 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200694 }
695 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200696 memcpy(cursor, ts->key.key, st->table->key_size);
697 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200698 }
699
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100700 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200701 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200702 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200703
Emeric Brun94900952015-06-11 18:25:54 +0200704 data_ptr = stktable_data_ptr(st->table, ts, data_type);
705 if (data_ptr) {
706 switch (stktable_data_types[data_type].std_type) {
707 case STD_T_SINT: {
708 int data;
709
710 data = stktable_data_cast(data_ptr, std_t_sint);
711 intencode(data, &cursor);
712 break;
713 }
714 case STD_T_UINT: {
715 unsigned int data;
716
717 data = stktable_data_cast(data_ptr, std_t_uint);
718 intencode(data, &cursor);
719 break;
720 }
721 case STD_T_ULL: {
722 unsigned long long data;
723
724 data = stktable_data_cast(data_ptr, std_t_ull);
725 intencode(data, &cursor);
726 break;
727 }
728 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200729 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200730
731 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
732 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
733 intencode(frqp->curr_ctr, &cursor);
734 intencode(frqp->prev_ctr, &cursor);
735 break;
736 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200737 case STD_T_DICT: {
738 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200739 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200740 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200741 char *beg, *end;
742 size_t value_len, data_len;
743 struct dcache *dc;
744
745 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100746 if (!de) {
747 /* No entry */
748 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200749 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100750 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200751
752 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200753 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200754 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200755 if (cached_de == &cde.entry) {
756 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
757 break;
758 /* Encode the length of the remaining data -> 1 */
759 intencode(1, &cursor);
760 /* Encode the cache entry ID */
761 intencode(cde.id + 1, &cursor);
762 }
763 else {
764 /* Leave enough room to encode the remaining data length. */
765 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
766 /* Encode the dictionary entry key */
767 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200768 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200769 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200770 intencode(value_len, &end);
771 /* Copy the data */
772 memcpy(end, de->value.key, value_len);
773 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200774 /* Encode the length of the data */
775 data_len = end - beg;
776 intencode(data_len, &cursor);
777 memmove(cursor, beg, data_len);
778 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200779 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200780 break;
781 }
Emeric Brun94900952015-06-11 18:25:54 +0200782 }
783 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200784 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100785 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200786
787 /* Compute datalen */
788 datalen = (cursor - datamsg);
789
790 /* prepare message header */
791 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200792 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200793 cursor = &msg[2];
794 intencode(datalen, &cursor);
795
796 /* move data after header */
797 memmove(cursor, datamsg, datalen);
798
799 /* return header size + data_len */
800 return (cursor - msg) + datalen;
801}
802
803/*
804 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800805 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200806 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
807 * check size)
808 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100809static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200810{
811 int len;
812 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200813 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200814 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200815 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200816 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100817 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200818
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100819 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200820 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200821
822 /* Encode data */
823
824 /* encode local id */
825 intencode(st->local_id, &cursor);
826
827 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100828 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200829 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100830 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200831 cursor += len;
832
833 /* encode table type */
834
Emeric Brun530ba382020-06-02 11:17:42 +0200835 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200836
837 /* encode table key size */
838 intencode(st->table->key_size, &cursor);
839
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200840 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200841 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200842 /* encode available known data types in table */
843 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
844 if (st->table->data_ofs[data_type]) {
845 switch (stktable_data_types[data_type].std_type) {
846 case STD_T_SINT:
847 case STD_T_UINT:
848 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200849 case STD_T_DICT:
Emeric Brun45f12b02021-07-01 18:54:05 +0200850 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200851 break;
Emeric Brun94900952015-06-11 18:25:54 +0200852 case STD_T_FRQP:
Emeric Brun45f12b02021-07-01 18:54:05 +0200853 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200854 intencode(data_type, &chunkq);
855 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200856 break;
857 }
858 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200859 }
860 intencode(data, &cursor);
861
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200862 /* Encode stick-table entries duration. */
863 intencode(st->table->expire, &cursor);
864
865 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200866 chunk->data = chunkq - chunkp;
867 memcpy(cursor, chunk->area, chunk->data);
868 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200869 }
870
Emeric Brunb3971ab2015-05-12 18:49:09 +0200871 /* Compute datalen */
872 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200873
Emeric Brunb3971ab2015-05-12 18:49:09 +0200874 /* prepare message header */
875 msg[0] = PEER_MSG_CLASS_STICKTABLE;
876 msg[1] = PEER_MSG_STKT_DEFINE;
877 cursor = &msg[2];
878 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200879
Emeric Brunb3971ab2015-05-12 18:49:09 +0200880 /* move data after header */
881 memmove(cursor, datamsg, datalen);
882
883 /* return header size + data_len */
884 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200885}
886
Emeric Brunb3971ab2015-05-12 18:49:09 +0200887/*
888 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
889 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800890 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200891 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
892 * check size)
893 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100894static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200895{
896 unsigned short datalen;
897 char *cursor, *datamsg;
898 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100899 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200900
Frédéric Lécaille39143342019-05-24 14:32:27 +0200901 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200902
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100903 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200904 intencode(st->remote_id, &cursor);
905 netinteger = htonl(st->last_get);
906 memcpy(cursor, &netinteger, sizeof(netinteger));
907 cursor += sizeof(netinteger);
908
909 /* Compute datalen */
910 datalen = (cursor - datamsg);
911
912 /* prepare message header */
913 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200914 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200915 cursor = &msg[2];
916 intencode(datalen, &cursor);
917
918 /* move data after header */
919 memmove(cursor, datamsg, datalen);
920
921 /* return header size + data_len */
922 return (cursor - msg) + datalen;
923}
Emeric Brun2b920a12010-09-23 18:30:22 +0200924
925/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200926 * Function to deinit connected peer
927 */
928void __peer_session_deinit(struct peer *peer)
929{
930 struct stream_interface *si;
931 struct stream *s;
932 struct peers *peers;
933
934 if (!peer->appctx)
935 return;
936
937 si = peer->appctx->owner;
938 if (!si)
939 return;
940
941 s = si_strm(si);
942 if (!s)
943 return;
944
945 peers = strm_fe(s)->parent;
946 if (!peers)
947 return;
948
949 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +0200950 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200951
Willy Tarreau4781b152021-04-06 13:53:36 +0200952 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200953
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200954 flush_dcache(peer);
955
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200956 /* Re-init current table pointers to force announcement on re-connect */
957 peer->remote_table = peer->last_local_table = NULL;
958 peer->appctx = NULL;
959 if (peer->flags & PEER_F_LEARN_ASSIGN) {
960 /* unassign current peer for learning */
961 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
962 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
963
Emeric Brunccdfbae2021-04-28 12:59:35 +0200964 if (peer->local)
965 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
966 else
967 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200968 /* reschedule a resync */
969 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
970 }
971 /* reset teaching and learning flags to 0 */
972 peer->flags &= PEER_TEACH_RESET;
973 peer->flags &= PEER_LEARN_RESET;
974 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
975}
976
977/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200978 * Callback to release a session with a peer
979 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200980static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200981{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200982 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200983
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100984 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100985 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100986 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200987 return;
988
989 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200990 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100991 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200992 if (peer->appctx == appctx)
993 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +0200994 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100995 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200996 }
997}
998
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200999/* Retrieve the major and minor versions of peers protocol
1000 * announced by a remote peer. <str> is a null-terminated
1001 * string with the following format: "<maj_ver>.<min_ver>".
1002 */
1003static int peer_get_version(const char *str,
1004 unsigned int *maj_ver, unsigned int *min_ver)
1005{
1006 unsigned int majv, minv;
1007 const char *pos, *saved;
1008 const char *end;
1009
1010 saved = pos = str;
1011 end = str + strlen(str);
1012
1013 majv = read_uint(&pos, end);
1014 if (saved == pos || *pos++ != '.')
1015 return -1;
1016
1017 saved = pos;
1018 minv = read_uint(&pos, end);
1019 if (saved == pos || pos != end)
1020 return -1;
1021
1022 *maj_ver = majv;
1023 *min_ver = minv;
1024
1025 return 0;
1026}
Emeric Brun2b920a12010-09-23 18:30:22 +02001027
1028/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001029 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1030 * '\n' character.
1031 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1032 * a line could not be read because the communication channel is closed.
1033 */
1034static inline int peer_getline(struct appctx *appctx)
1035{
1036 int n;
1037 struct stream_interface *si = appctx->owner;
1038
1039 n = co_getline(si_oc(si), trash.area, trash.size);
1040 if (!n)
1041 return 0;
1042
1043 if (n < 0 || trash.area[n - 1] != '\n') {
1044 appctx->st0 = PEER_SESS_ST_END;
1045 return -1;
1046 }
1047
1048 if (n > 1 && (trash.area[n - 2] == '\r'))
1049 trash.area[n - 2] = 0;
1050 else
1051 trash.area[n - 1] = 0;
1052
1053 co_skip(si_oc(si), n);
1054
1055 return n;
1056}
1057
1058/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001059 * Send a message after having called <peer_prepare_msg> to build it.
1060 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1061 * Returns -1 if there was not enough room left to send the message,
1062 * any other negative returned value must be considered as an error with an appcxt st0
1063 * returned value equal to PEER_SESS_ST_END.
1064 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001065static inline int peer_send_msg(struct appctx *appctx,
1066 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1067 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001068{
1069 int ret, msglen;
1070 struct stream_interface *si = appctx->owner;
1071
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001072 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001073 if (!msglen) {
1074 /* internal error: message does not fit in trash */
1075 appctx->st0 = PEER_SESS_ST_END;
1076 return 0;
1077 }
1078
1079 /* message to buffer */
1080 ret = ci_putblk(si_ic(si), trash.area, msglen);
1081 if (ret <= 0) {
1082 if (ret == -1) {
1083 /* No more write possible */
1084 si_rx_room_blk(si);
1085 return -1;
1086 }
1087 appctx->st0 = PEER_SESS_ST_END;
1088 }
1089
1090 return ret;
1091}
1092
1093/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001094 * Send a hello message.
1095 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1096 * Returns -1 if there was not enough room left to send the message,
1097 * any other negative returned value must be considered as an error with an appcxt st0
1098 * returned value equal to PEER_SESS_ST_END.
1099 */
1100static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1101{
1102 struct peer_prep_params p = {
1103 .hello.peer = peer,
1104 };
1105
1106 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1107}
1108
1109/*
1110 * Send a success peer handshake status message.
1111 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1112 * Returns -1 if there was not enough room left to send the message,
1113 * any other negative returned value must be considered as an error with an appcxt st0
1114 * returned value equal to PEER_SESS_ST_END.
1115 */
1116static inline int peer_send_status_successmsg(struct appctx *appctx)
1117{
1118 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1119}
1120
1121/*
1122 * Send a peer handshake status error message.
1123 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1124 * Returns -1 if there was not enough room left to send the message,
1125 * any other negative returned value must be considered as an error with an appcxt st0
1126 * returned value equal to PEER_SESS_ST_END.
1127 */
1128static inline int peer_send_status_errormsg(struct appctx *appctx)
1129{
1130 struct peer_prep_params p = {
1131 .error_status.st1 = appctx->st1,
1132 };
1133
1134 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1135}
1136
1137/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001138 * Send a stick-table switch message.
1139 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1140 * Returns -1 if there was not enough room left to send the message,
1141 * any other negative returned value must be considered as an error with an appcxt st0
1142 * returned value equal to PEER_SESS_ST_END.
1143 */
1144static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1145{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001146 struct peer_prep_params p = {
1147 .swtch.shared_table = st,
1148 };
1149
1150 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001151}
1152
1153/*
1154 * Send a stick-table update acknowledgement message.
1155 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1156 * Returns -1 if there was not enough room left to send the message,
1157 * any other negative returned value must be considered as an error with an appcxt st0
1158 * returned value equal to PEER_SESS_ST_END.
1159 */
1160static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1161{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001162 struct peer_prep_params p = {
1163 .ack.shared_table = st,
1164 };
1165
1166 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001167}
1168
1169/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001170 * Send a stick-table update message.
1171 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1172 * Returns -1 if there was not enough room left to send the message,
1173 * any other negative returned value must be considered as an error with an appcxt st0
1174 * returned value equal to PEER_SESS_ST_END.
1175 */
1176static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1177 unsigned int updateid, int use_identifier, int use_timed)
1178{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001179 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001180 .updt = {
1181 .stksess = ts,
1182 .shared_table = st,
1183 .updateid = updateid,
1184 .use_identifier = use_identifier,
1185 .use_timed = use_timed,
1186 .peer = appctx->ctx.peers.ptr,
1187 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001188 };
1189
1190 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001191}
1192
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001193/*
1194 * Build a peer protocol control class message.
1195 * Returns the number of written bytes used to build the message if succeeded,
1196 * 0 if not.
1197 */
1198static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1199{
1200 if (size < sizeof p->control.head)
1201 return 0;
1202
1203 msg[0] = p->control.head[0];
1204 msg[1] = p->control.head[1];
1205
1206 return 2;
1207}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001208
1209/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001210 * Send a stick-table synchronization request 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 appctx st0
1214 * returned value equal to PEER_SESS_ST_END.
1215 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001216static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1217 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001218{
1219 struct peer_prep_params p = {
1220 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1221 };
1222
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001223 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1224 NULL, &p.control.head[1], peers->local->id, peer->id);
1225
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001226 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1227}
1228
1229/*
1230 * Send a stick-table synchronization confirmation message.
1231 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1232 * Returns -1 if there was not enough room left to send the message,
1233 * any other negative returned value must be considered as an error with an appctx st0
1234 * returned value equal to PEER_SESS_ST_END.
1235 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001236static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1237 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001238{
1239 struct peer_prep_params p = {
1240 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1241 };
1242
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001243 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1244 NULL, &p.control.head[1], peers->local->id, peer->id);
1245
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001246 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1247}
1248
1249/*
1250 * Send a stick-table synchronization finished message.
1251 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1252 * Returns -1 if there was not enough room left to send the message,
1253 * any other negative returned value must be considered as an error with an appctx st0
1254 * returned value equal to PEER_SESS_ST_END.
1255 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001256static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1257 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001258{
1259 struct peer_prep_params p = {
1260 .control.head = { PEER_MSG_CLASS_CONTROL, },
1261 };
1262
Emeric Brun70de43b2020-03-16 10:51:01 +01001263 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001264 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1265
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001266 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1267 NULL, &p.control.head[1], peers->local->id, peer->id);
1268
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001269 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1270}
1271
1272/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001273 * Send a heartbeat message.
1274 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1275 * Returns -1 if there was not enough room left to send the message,
1276 * any other negative returned value must be considered as an error with an appctx st0
1277 * returned value equal to PEER_SESS_ST_END.
1278 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001279static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1280 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001281{
1282 struct peer_prep_params p = {
1283 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1284 };
1285
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001286 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1287 NULL, &p.control.head[1], peers->local->id, peer->id);
1288
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001289 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1290}
1291
1292/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001293 * Build a peer protocol error class message.
1294 * Returns the number of written bytes used to build the message if succeeded,
1295 * 0 if not.
1296 */
1297static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1298{
1299 if (size < sizeof p->error.head)
1300 return 0;
1301
1302 msg[0] = p->error.head[0];
1303 msg[1] = p->error.head[1];
1304
1305 return 2;
1306}
1307
1308/*
1309 * Send a "size limit reached" error message.
1310 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1311 * Returns -1 if there was not enough room left to send the message,
1312 * any other negative returned value must be considered as an error with an appctx st0
1313 * returned value equal to PEER_SESS_ST_END.
1314 */
1315static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1316{
1317 struct peer_prep_params p = {
1318 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1319 };
1320
1321 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1322}
1323
1324/*
1325 * Send a "peer protocol" error message.
1326 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1327 * Returns -1 if there was not enough room left to send the message,
1328 * any other negative returned value must be considered as an error with an appctx st0
1329 * returned value equal to PEER_SESS_ST_END.
1330 */
1331static inline int peer_send_error_protomsg(struct appctx *appctx)
1332{
1333 struct peer_prep_params p = {
1334 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1335 };
1336
1337 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1338}
1339
1340/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001341 * Function used to lookup for recent stick-table updates associated with
1342 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1343 */
1344static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1345{
1346 struct eb32_node *eb;
1347
1348 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1349 if (!eb) {
1350 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001351 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001352 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1353 return NULL;
1354 }
1355 }
1356
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001357 /* if distance between the last pushed and the retrieved key
1358 * is greater than the distance last_pushed and the local_update
1359 * this means we are beyond localupdate.
1360 */
1361 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001362 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1363 return NULL;
1364 }
1365
1366 return eb32_entry(eb, struct stksess, upd);
1367}
1368
1369/*
1370 * Function used to lookup for recent stick-table updates associated with
1371 * <st> shared stick-table during teach state 1 step.
1372 */
1373static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1374{
1375 struct eb32_node *eb;
1376
1377 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1378 if (!eb) {
1379 st->flags |= SHTABLE_F_TEACH_STAGE1;
1380 eb = eb32_first(&st->table->updates);
1381 if (eb)
1382 st->last_pushed = eb->key - 1;
1383 return NULL;
1384 }
1385
1386 return eb32_entry(eb, struct stksess, upd);
1387}
1388
1389/*
1390 * Function used to lookup for recent stick-table updates associated with
1391 * <st> shared stick-table during teach state 2 step.
1392 */
1393static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1394{
1395 struct eb32_node *eb;
1396
1397 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1398 if (!eb || eb->key > st->teaching_origin) {
1399 st->flags |= SHTABLE_F_TEACH_STAGE2;
1400 return NULL;
1401 }
1402
1403 return eb32_entry(eb, struct stksess, upd);
1404}
1405
1406/*
1407 * Generic function to emit update messages for <st> stick-table when a lesson must
1408 * be taught to the peer <p>.
1409 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1410 * this function, 0 if not.
1411 *
1412 * This function temporary unlock/lock <st> when it sends stick-table updates or
1413 * when decrementing its refcount in case of any error when it sends this updates.
1414 *
1415 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1416 * Returns -1 if there was not enough room left to send the message,
1417 * any other negative returned value must be considered as an error with an appcxt st0
1418 * returned value equal to PEER_SESS_ST_END.
1419 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1420 * unlocked if not already locked when entering this function.
1421 */
1422static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1423 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1424 struct shared_table *st, int locked)
1425{
1426 int ret, new_pushed, use_timed;
1427
1428 ret = 1;
1429 use_timed = 0;
1430 if (st != p->last_local_table) {
1431 ret = peer_send_switchmsg(st, appctx);
1432 if (ret <= 0)
1433 return ret;
1434
1435 p->last_local_table = st;
1436 }
1437
1438 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1439 use_timed = !(p->flags & PEER_F_DWNGRD);
1440
1441 /* We force new pushed to 1 to force identifier in update message */
1442 new_pushed = 1;
1443
1444 if (!locked)
1445 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1446
1447 while (1) {
1448 struct stksess *ts;
1449 unsigned updateid;
1450
1451 /* push local updates */
1452 ts = peer_stksess_lookup(st);
1453 if (!ts)
1454 break;
1455
1456 updateid = ts->upd.key;
1457 ts->ref_cnt++;
1458 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1459
1460 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1461 if (ret <= 0) {
1462 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1463 ts->ref_cnt--;
1464 if (!locked)
1465 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1466 return ret;
1467 }
1468
1469 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1470 ts->ref_cnt--;
1471 st->last_pushed = updateid;
1472
1473 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1474 (int)(st->last_pushed - st->table->commitupdate) > 0)
1475 st->table->commitupdate = st->last_pushed;
1476
1477 /* identifier may not needed in next update message */
1478 new_pushed = 0;
1479 }
1480
1481 out:
1482 if (!locked)
1483 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1484 return 1;
1485}
1486
1487/*
1488 * Function to emit update messages for <st> stick-table when a lesson must
1489 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1490 *
1491 * Note that <st> shared stick-table is locked when calling this function.
1492 *
1493 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1494 * Returns -1 if there was not enough room left to send the message,
1495 * any other negative returned value must be considered as an error with an appcxt st0
1496 * returned value equal to PEER_SESS_ST_END.
1497 */
1498static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1499 struct shared_table *st)
1500{
1501 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1502}
1503
1504/*
1505 * Function to emit update messages for <st> stick-table when a lesson must
1506 * be taught to the peer <p> during teach state 1 step.
1507 *
1508 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1509 * Returns -1 if there was not enough room left to send the message,
1510 * any other negative returned value must be considered as an error with an appcxt st0
1511 * returned value equal to PEER_SESS_ST_END.
1512 */
1513static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1514 struct shared_table *st)
1515{
1516 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1517}
1518
1519/*
1520 * Function to emit update messages for <st> stick-table when a lesson must
1521 * be taught to the peer <p> during teach state 1 step.
1522 *
1523 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1524 * Returns -1 if there was not enough room left to send the message,
1525 * any other negative returned value must be considered as an error with an appcxt st0
1526 * returned value equal to PEER_SESS_ST_END.
1527 */
1528static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1529 struct shared_table *st)
1530{
1531 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1532}
1533
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001534
1535/*
1536 * Function used to parse a stick-table update message after it has been received
1537 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1538 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1539 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1540 * was encountered.
1541 * <exp> must be set if the stick-table entry expires.
1542 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001543 * messages, in this case the stick-table update message is received with a stick-table
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001544 * update ID.
1545 * <totl> is the length of the stick-table update message computed upon receipt.
1546 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001547static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1548 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001549{
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001550 struct shared_table *st = p->remote_table;
1551 struct stksess *ts, *newts;
1552 uint32_t update;
1553 int expire;
1554 unsigned int data_type;
1555 void *data_ptr;
1556
Frédéric Lécailled8659352020-11-10 16:18:03 +01001557 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001558 /* Here we have data message */
1559 if (!st)
1560 goto ignore_msg;
1561
1562 expire = MS_TO_TICKS(st->table->expire);
1563
1564 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001565 if (msg_len < sizeof(update)) {
1566 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001567 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001568 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001569
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001570 memcpy(&update, *msg_cur, sizeof(update));
1571 *msg_cur += sizeof(update);
1572 st->last_get = htonl(update);
1573 }
1574 else {
1575 st->last_get++;
1576 }
1577
1578 if (exp) {
1579 size_t expire_sz = sizeof expire;
1580
Frédéric Lécailled8659352020-11-10 16:18:03 +01001581 if (*msg_cur + expire_sz > msg_end) {
1582 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1583 NULL, p, *msg_cur);
1584 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1585 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001586 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001587 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001588
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001589 memcpy(&expire, *msg_cur, expire_sz);
1590 *msg_cur += expire_sz;
1591 expire = ntohl(expire);
1592 }
1593
1594 newts = stksess_new(st->table, NULL);
1595 if (!newts)
1596 goto ignore_msg;
1597
1598 if (st->table->type == SMP_T_STR) {
1599 unsigned int to_read, to_store;
1600
1601 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001602 if (!*msg_cur) {
1603 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001604 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001605 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001606
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001607 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001608 if (*msg_cur + to_store > msg_end) {
1609 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1610 NULL, p, *msg_cur);
1611 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1612 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001613 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001614 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001615
1616 memcpy(newts->key.key, *msg_cur, to_store);
1617 newts->key.key[to_store] = 0;
1618 *msg_cur += to_read;
1619 }
1620 else if (st->table->type == SMP_T_SINT) {
1621 unsigned int netinteger;
1622
Frédéric Lécailled8659352020-11-10 16:18:03 +01001623 if (*msg_cur + sizeof(netinteger) > msg_end) {
1624 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1625 NULL, p, *msg_cur);
1626 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1627 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001628 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001629 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001630
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001631 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1632 netinteger = ntohl(netinteger);
1633 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1634 *msg_cur += sizeof(netinteger);
1635 }
1636 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001637 if (*msg_cur + st->table->key_size > msg_end) {
1638 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1639 NULL, p, *msg_cur);
1640 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1641 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001642 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001643 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001644
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001645 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1646 *msg_cur += st->table->key_size;
1647 }
1648
1649 /* lookup for existing entry */
1650 ts = stktable_set_entry(st->table, newts);
1651 if (ts != newts) {
1652 stksess_free(st->table, newts);
1653 newts = NULL;
1654 }
1655
1656 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1657
1658 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001659 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001660
Emeric Brun45f12b02021-07-01 18:54:05 +02001661 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001662 continue;
1663
Willy Tarreau1e82a142019-01-29 11:08:06 +01001664 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001665 if (!*msg_cur) {
1666 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001667 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001668 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001669
Willy Tarreau1e82a142019-01-29 11:08:06 +01001670 switch (stktable_data_types[data_type].std_type) {
1671 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001672 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1673 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001674 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001675 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001676
Willy Tarreau1e82a142019-01-29 11:08:06 +01001677 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001678 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1679 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001680 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001681 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001682
Willy Tarreau1e82a142019-01-29 11:08:06 +01001683 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001684 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1685 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001686 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001687 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001688
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001689 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001690 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001691
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001692 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001693 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001694 so we don't need to update the update the freq_ctr
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001695 using its internal lock */
1696
Willy Tarreau1e82a142019-01-29 11:08:06 +01001697 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001698 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001699 if (!*msg_cur) {
1700 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001701 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001702 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001703
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001704 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001705 if (!*msg_cur) {
1706 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001707 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001708 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001709
1710 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1711 if (data_ptr)
1712 stktable_data_cast(data_ptr, std_t_frqp) = data;
1713 break;
1714 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001715 case STD_T_DICT: {
1716 struct buffer *chunk;
1717 size_t data_len, value_len;
1718 unsigned int id;
1719 struct dict_entry *de;
1720 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001721 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001722
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001723 if (!decoded_int) {
1724 /* No entry. */
1725 break;
1726 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001727 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001728 if (*msg_cur + data_len > msg_end) {
1729 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1730 NULL, p, *msg_cur);
1731 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1732 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001733 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001734 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001735
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001736 /* Compute the end of the current data, <msg_end> being at the end of
1737 * the entire message.
1738 */
1739 end = *msg_cur + data_len;
1740 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001741 if (!*msg_cur || !id) {
1742 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1743 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001744 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001745 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001746
1747 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001748 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001749 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001750 if (id > dc->max_entries) {
1751 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1752 NULL, p, NULL, &id);
1753 goto malformed_unlock;
1754 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001755 /* IDs sent over the network are numbered from 1. */
1756 de = dc->rx[id - 1].de;
1757 }
1758 else {
1759 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001760 value_len = intdecode(msg_cur, end);
1761 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01001762 unlikely(value_len + 1 >= chunk->size)) {
1763 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1764 NULL, p, *msg_cur, &value_len);
1765 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1766 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001767 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001768 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001769
1770 chunk_memcpy(chunk, *msg_cur, value_len);
1771 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001772 *msg_cur += value_len;
1773
Thayne McCombs92149f92020-11-20 01:28:26 -07001774 de = dict_insert(&server_key_dict, chunk->area);
1775 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001776 dc->rx[id - 1].de = de;
1777 }
1778 if (de) {
1779 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Thayne McCombs92149f92020-11-20 01:28:26 -07001780 if (data_ptr) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001781 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001782 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07001783 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001784 }
1785 break;
1786 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001787 }
1788 }
1789 /* Force new expiration */
1790 ts->expire = tick_add(now_ms, expire);
1791
1792 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1793 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001794
1795 ignore_msg:
Emeric Brun37d96622022-11-18 14:52:54 +01001796 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
1797 return 1;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001798
1799 malformed_unlock:
1800 /* malformed message */
1801 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1802 stktable_touch_remote(st->table, ts, 1);
1803 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001804 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001805 return 0;
1806
1807 malformed_free_newts:
1808 /* malformed message */
1809 stksess_free(st->table, newts);
1810 malformed_exit:
1811 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001812 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001813 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001814}
1815
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001816/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001817 * Function used to parse a stick-table update acknowledgement message after it
1818 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1819 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1820 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1821 * was encountered.
1822 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1823 */
1824static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1825 char **msg_cur, char *msg_end)
1826{
1827 /* ack message */
1828 uint32_t table_id ;
1829 uint32_t update;
1830 struct shared_table *st;
1831
Emeric Brunb0d60be2021-03-04 10:27:10 +01001832 /* ignore ack during teaching process */
1833 if (p->flags & PEER_F_TEACH_PROCESS)
1834 return 1;
1835
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001836 table_id = intdecode(msg_cur, msg_end);
1837 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1838 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001839
1840 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
1841 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001842 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1843 return 0;
1844 }
1845
1846 memcpy(&update, *msg_cur, sizeof(update));
1847 update = ntohl(update);
1848
1849 for (st = p->tables; st; st = st->next) {
1850 if (st->local_id == table_id) {
1851 st->update = update;
1852 break;
1853 }
1854 }
1855
1856 return 1;
1857}
1858
1859/*
1860 * Function used to parse a stick-table switch message after it has been received
1861 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1862 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1863 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1864 * was encountered.
1865 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1866 */
1867static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1868 char **msg_cur, char *msg_end)
1869{
1870 struct shared_table *st;
1871 int table_id;
1872
1873 table_id = intdecode(msg_cur, msg_end);
1874 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001875 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001876 /* malformed message */
1877 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1878 return 0;
1879 }
1880
1881 p->remote_table = NULL;
1882 for (st = p->tables; st; st = st->next) {
1883 if (st->remote_id == table_id) {
1884 p->remote_table = st;
1885 break;
1886 }
1887 }
1888
1889 return 1;
1890}
1891
1892/*
1893 * Function used to parse a stick-table definition message after it has been received
1894 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1895 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1896 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1897 * was encountered.
1898 * <totl> is the length of the stick-table update message computed upon receipt.
1899 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1900 */
1901static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1902 char **msg_cur, char *msg_end, int totl)
1903{
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001904 int table_id_len;
1905 struct shared_table *st;
1906 int table_type;
1907 int table_keylen;
1908 int table_id;
1909 uint64_t table_data;
1910
1911 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001912 if (!*msg_cur) {
1913 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001914 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001915 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001916
1917 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001918 if (!*msg_cur) {
1919 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001920 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001921 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001922
1923 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001924 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1925 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001926 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001927 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001928
1929 for (st = p->tables; st; st = st->next) {
1930 /* Reset IDs */
1931 if (st->remote_id == table_id)
1932 st->remote_id = 0;
1933
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001934 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1935 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001936 p->remote_table = st;
1937 }
1938
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001939 if (!p->remote_table) {
1940 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001941 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001942 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001943
1944 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001945 if (*msg_cur >= msg_end) {
1946 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001947 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001948 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001949
1950 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001951 if (!*msg_cur) {
1952 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001953 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001954 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001955
1956 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001957 if (!*msg_cur) {
1958 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001959 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001960 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001961
1962 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001963 if (!*msg_cur) {
1964 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001965 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001966 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001967
Emeric Brun530ba382020-06-02 11:17:42 +02001968 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001969 || p->remote_table->table->key_size != table_keylen) {
1970 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001971 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001972 goto ignore_msg;
1973 }
1974
1975 p->remote_table->remote_data = table_data;
1976 p->remote_table->remote_id = table_id;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001977
1978 ignore_msg:
Emeric Brun37d96622022-11-18 14:52:54 +01001979 return 1;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001980
1981 malformed_exit:
1982 /* malformed message */
1983 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1984 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001985}
1986
1987/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01001988 * Receive a stick-table message or pre-parse any other message.
1989 * The message's header will be sent into <msg_head> which must be at least
1990 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
1991 * The first two bytes are always read, and the rest is only read if the
1992 * first bytes indicate a stick-table message. If the message is a stick-table
1993 * message, the varint is decoded and the equivalent number of bytes will be
1994 * copied into the trash at trash.area. <totl> is incremented by the number of
1995 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001996 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1997 * -1 if there was an error updating the appctx state st0 accordingly.
1998 */
1999static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2000 uint32_t *msg_len, int *totl)
2001{
2002 int reql;
2003 struct stream_interface *si = appctx->owner;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002004 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002005
2006 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
2007 if (reql <= 0) /* closed or EOL not found */
2008 goto incomplete;
2009
2010 *totl += reql;
2011
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002012 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002013 return 1;
2014
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002015 /* This is a stick-table message, let's go on */
2016
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002017 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002018 msg_head += *totl;
2019 msg_head_sz -= *totl;
2020 reql = co_data(si_oc(si)) - *totl;
2021 if (reql > msg_head_sz)
2022 reql = msg_head_sz;
2023
2024 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002025 if (reql <= 0) /* closed */
2026 goto incomplete;
2027
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002028 cur = msg_head;
2029 *msg_len = intdecode(&cur, cur + reql);
2030 if (!cur) {
2031 /* the number is truncated, did we read enough ? */
2032 if (reql < msg_head_sz)
2033 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002034
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002035 /* malformed message */
2036 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2037 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2038 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002039 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002040 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002041
2042 /* Read message content */
2043 if (*msg_len) {
2044 if (*msg_len > trash.size) {
2045 /* Status code is not success, abort */
2046 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2047 return -1;
2048 }
2049
2050 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
2051 if (reql <= 0) /* closed */
2052 goto incomplete;
2053 *totl += reql;
2054 }
2055
2056 return 1;
2057
2058 incomplete:
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002059 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
2060 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002061 appctx->st0 = PEER_SESS_ST_END;
2062 return -1;
2063 }
2064
2065 return 0;
2066}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002067
2068/*
2069 * Treat the awaited message with <msg_head> as header.*
2070 * Return 1 if succeeded, 0 if not.
2071 */
2072static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2073 char **msg_cur, char *msg_end, int msg_len, int totl)
2074{
2075 struct stream_interface *si = appctx->owner;
2076 struct stream *s = si_strm(si);
2077 struct peers *peers = strm_fe(s)->parent;
2078
2079 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2080 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2081 struct shared_table *st;
2082 /* Reset message: remote need resync */
2083
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002084 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2085 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002086 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002087 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002088 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002089 st->flags = 0;
2090 }
2091
2092 /* reset teaching flags to 0 */
2093 peer->flags &= PEER_TEACH_RESET;
2094
2095 /* flag to start to teach lesson */
2096 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002097 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002098 }
2099 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002100 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2101 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002102 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2103 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2104 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2105 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002106 if (peer->local)
2107 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2108 else
2109 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002110 }
2111 peer->confirm++;
2112 }
2113 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002114 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2115 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002116 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2117 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2118 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2119
Emeric Brunccdfbae2021-04-28 12:59:35 +02002120 if (peer->local)
2121 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2122 else
2123 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002124 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002125 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002126 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2127 }
2128 peer->confirm++;
2129 }
2130 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2131 struct shared_table *st;
2132
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002133 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2134 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002135 /* If stopping state */
2136 if (stopping) {
2137 /* Close session, push resync no more needed */
2138 peer->flags |= PEER_F_TEACH_COMPLETE;
2139 appctx->st0 = PEER_SESS_ST_END;
2140 return 0;
2141 }
2142 for (st = peer->tables; st; st = st->next) {
2143 st->update = st->last_pushed = st->teaching_origin;
2144 st->flags = 0;
2145 }
2146
2147 /* reset teaching flags to 0 */
2148 peer->flags &= PEER_TEACH_RESET;
2149 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002150 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002151 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2152 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002153 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002154 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002155 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002156 }
2157 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2158 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2159 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2160 return 0;
2161 }
2162 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2163 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2164 return 0;
2165 }
2166 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2167 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2168 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2169 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2170 int update, expire;
2171
2172 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2173 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2174 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2175 msg_cur, msg_end, msg_len, totl))
2176 return 0;
2177
2178 }
2179 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2180 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2181 return 0;
2182 }
2183 }
2184 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2185 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2186 return 0;
2187 }
2188
2189 return 1;
2190}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002191
2192
2193/*
2194 * Send any message to <peer> peer.
2195 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002196 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002197 * to a peer state change (from the peer I/O handler point of view).
2198 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002199static inline int peer_send_msgs(struct appctx *appctx,
2200 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002201{
2202 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002203
2204 /* Need to request a resync */
2205 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2206 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2207 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2208
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002209 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002210 if (repl <= 0)
2211 return repl;
2212
2213 peers->flags |= PEERS_F_RESYNC_PROCESS;
2214 }
2215
2216 /* Nothing to read, now we start to write */
2217 if (peer->tables) {
2218 struct shared_table *st;
2219 struct shared_table *last_local_table;
2220
2221 last_local_table = peer->last_local_table;
2222 if (!last_local_table)
2223 last_local_table = peer->tables;
2224 st = last_local_table->next;
2225
2226 while (1) {
2227 if (!st)
2228 st = peer->tables;
2229
2230 /* It remains some updates to ack */
2231 if (st->last_get != st->last_acked) {
2232 repl = peer_send_ackmsg(st, appctx);
2233 if (repl <= 0)
2234 return repl;
2235
2236 st->last_acked = st->last_get;
2237 }
2238
2239 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2240 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2241 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002242 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002243
2244 repl = peer_send_teach_process_msgs(appctx, peer, st);
2245 if (repl <= 0) {
2246 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2247 return repl;
2248 }
2249 }
2250 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2251 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002252 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002253 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2254 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2255 if (repl <= 0)
2256 return repl;
2257 }
2258
2259 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2260 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2261 if (repl <= 0)
2262 return repl;
2263 }
2264 }
2265
2266 if (st == last_local_table)
2267 break;
2268 st = st->next;
2269 }
2270 }
2271
2272 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002273 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002274 if (repl <= 0)
2275 return repl;
2276
2277 /* flag finished message sent */
2278 peer->flags |= PEER_F_TEACH_FINISHED;
2279 }
2280
2281 /* Confirm finished or partial messages */
2282 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002283 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002284 if (repl <= 0)
2285 return repl;
2286
2287 peer->confirm--;
2288 }
2289
2290 return 1;
2291}
2292
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002293/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002294 * Read and parse a first line of a "hello" peer protocol message.
2295 * Returns 0 if could not read a line, -1 if there was a read error or
2296 * the line is malformed, 1 if succeeded.
2297 */
2298static inline int peer_getline_version(struct appctx *appctx,
2299 unsigned int *maj_ver, unsigned int *min_ver)
2300{
2301 int reql;
2302
2303 reql = peer_getline(appctx);
2304 if (!reql)
2305 return 0;
2306
2307 if (reql < 0)
2308 return -1;
2309
2310 /* test protocol */
2311 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2312 appctx->st0 = PEER_SESS_ST_EXIT;
2313 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2314 return -1;
2315 }
2316 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2317 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2318 appctx->st0 = PEER_SESS_ST_EXIT;
2319 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2320 return -1;
2321 }
2322
2323 return 1;
2324}
2325
2326/*
2327 * Read and parse a second line of a "hello" peer protocol message.
2328 * Returns 0 if could not read a line, -1 if there was a read error or
2329 * the line is malformed, 1 if succeeded.
2330 */
2331static inline int peer_getline_host(struct appctx *appctx)
2332{
2333 int reql;
2334
2335 reql = peer_getline(appctx);
2336 if (!reql)
2337 return 0;
2338
2339 if (reql < 0)
2340 return -1;
2341
2342 /* test hostname match */
2343 if (strcmp(localpeer, trash.area) != 0) {
2344 appctx->st0 = PEER_SESS_ST_EXIT;
2345 appctx->st1 = PEER_SESS_SC_ERRHOST;
2346 return -1;
2347 }
2348
2349 return 1;
2350}
2351
2352/*
2353 * Read and parse a last line of a "hello" peer protocol message.
2354 * Returns 0 if could not read a character, -1 if there was a read error or
2355 * the line is malformed, 1 if succeeded.
2356 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2357 */
2358static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2359{
2360 char *p;
2361 int reql;
2362 struct peer *peer;
2363 struct stream_interface *si = appctx->owner;
2364 struct stream *s = si_strm(si);
2365 struct peers *peers = strm_fe(s)->parent;
2366
2367 reql = peer_getline(appctx);
2368 if (!reql)
2369 return 0;
2370
2371 if (reql < 0)
2372 return -1;
2373
2374 /* parse line "<peer name> <pid> <relative_pid>" */
2375 p = strchr(trash.area, ' ');
2376 if (!p) {
2377 appctx->st0 = PEER_SESS_ST_EXIT;
2378 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2379 return -1;
2380 }
2381 *p = 0;
2382
2383 /* lookup known peer */
2384 for (peer = peers->remote; peer; peer = peer->next) {
2385 if (strcmp(peer->id, trash.area) == 0)
2386 break;
2387 }
2388
2389 /* if unknown peer */
2390 if (!peer) {
2391 appctx->st0 = PEER_SESS_ST_EXIT;
2392 appctx->st1 = PEER_SESS_SC_ERRPEER;
2393 return -1;
2394 }
2395 *curpeer = peer;
2396
2397 return 1;
2398}
2399
2400/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002401 * Init <peer> peer after having accepted it at peer protocol level.
2402 */
2403static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2404{
2405 struct shared_table *st;
2406
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002407 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002408 /* Register status code */
2409 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002410 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002411
2412 /* Awake main task */
2413 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2414
2415 /* Init confirm counter */
2416 peer->confirm = 0;
2417
2418 /* Init cursors */
2419 for (st = peer->tables; st ; st = st->next) {
2420 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002421 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2422 /* if st->update appears to be in future it means
2423 * that the last acked value is very old and we
2424 * remain unconnected a too long time to use this
2425 * acknowlegement as a reset.
2426 * We should update the protocol to be able to
2427 * signal the remote peer that it needs a full resync.
2428 * Here a partial fix consist to set st->update at
2429 * the max past value
2430 */
2431 if ((int)(st->table->localupdate - st->update) < 0)
2432 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002433 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002434 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002435 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2436 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002437 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002438 }
2439
2440 /* reset teaching and learning flags to 0 */
2441 peer->flags &= PEER_TEACH_RESET;
2442 peer->flags &= PEER_LEARN_RESET;
2443
2444 /* if current peer is local */
2445 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002446 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002447 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2448 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2449 /* assign local peer for a lesson, consider lesson already requested */
2450 peer->flags |= PEER_F_LEARN_ASSIGN;
2451 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002452 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002453 }
2454
2455 }
2456 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2457 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2458 /* assign peer for a lesson */
2459 peer->flags |= PEER_F_LEARN_ASSIGN;
2460 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002461 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002462 }
2463}
2464
2465/*
2466 * Init <peer> peer after having connected it at peer protocol level.
2467 */
2468static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2469{
2470 struct shared_table *st;
2471
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002472 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002473 /* Init cursors */
2474 for (st = peer->tables; st ; st = st->next) {
2475 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002476 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2477 /* if st->update appears to be in future it means
2478 * that the last acked value is very old and we
2479 * remain unconnected a too long time to use this
2480 * acknowlegement as a reset.
2481 * We should update the protocol to be able to
2482 * signal the remote peer that it needs a full resync.
2483 * Here a partial fix consist to set st->update at
2484 * the max past value.
2485 */
2486 if ((int)(st->table->localupdate - st->update) < 0)
2487 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002488 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002489 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002490 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2491 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002492 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002493 }
2494
2495 /* Init confirm counter */
2496 peer->confirm = 0;
2497
2498 /* reset teaching and learning flags to 0 */
2499 peer->flags &= PEER_TEACH_RESET;
2500 peer->flags &= PEER_LEARN_RESET;
2501
2502 /* If current peer is local */
2503 if (peer->local) {
2504 /* flag to start to teach lesson */
2505 peer->flags |= PEER_F_TEACH_PROCESS;
2506 }
2507 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2508 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2509 /* If peer is remote and resync from remote is needed,
2510 and no peer currently assigned */
2511
2512 /* assign peer for a lesson */
2513 peer->flags |= PEER_F_LEARN_ASSIGN;
2514 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002515 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002516 }
2517}
2518
2519/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002520 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002521 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002522static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002523{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002524 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002525 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002526 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002527 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002528 int reql = 0;
2529 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002530 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002531 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002532
Joseph Herlant82b2f542018-11-15 12:19:14 -08002533 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002534 if (si_ic(si)->buf.size == 0) {
2535 si_rx_room_blk(si);
2536 goto out;
2537 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002538
Emeric Brun2b920a12010-09-23 18:30:22 +02002539 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002540 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002541switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002542 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002543 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002544 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002545 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002546 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002547 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002548 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002549 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002550 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002551 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2552 if (reql <= 0) {
2553 if (!reql)
2554 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002555 goto switchstate;
2556 }
2557
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002558 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002559 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002560 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002561 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002562 reql = peer_getline_host(appctx);
2563 if (reql <= 0) {
2564 if (!reql)
2565 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002566 goto switchstate;
2567 }
2568
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002569 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002570 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002571 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002572 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002573 reql = peer_getline_last(appctx, &curpeer);
2574 if (reql <= 0) {
2575 if (!reql)
2576 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002577 goto switchstate;
2578 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002579
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002580 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002581 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002582 if (curpeer->local) {
2583 /* Local connection, reply a retry */
2584 appctx->st0 = PEER_SESS_ST_EXIT;
2585 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2586 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002587 }
Emeric Brun80527f52017-06-19 17:46:37 +02002588
2589 /* we're killing a connection, we must apply a random delay before
2590 * retrying otherwise the other end will do the same and we can loop
2591 * for a while.
2592 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002593 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002594 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002595 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002596 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002597 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002598 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2599 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2600 curpeer->flags |= PEER_F_DWNGRD;
2601 }
2602 else {
2603 curpeer->flags &= ~PEER_F_DWNGRD;
2604 }
2605 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002606 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002607 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002608 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002609 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002610 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002611 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002612 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002613 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002614 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002615 if (!curpeer) {
2616 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002617 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002618 if (curpeer->appctx != appctx) {
2619 appctx->st0 = PEER_SESS_ST_END;
2620 goto switchstate;
2621 }
2622 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002623
2624 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002625 if (repl <= 0) {
2626 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002627 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002628 goto switchstate;
2629 }
2630
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002631 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002632
Emeric Brun2b920a12010-09-23 18:30:22 +02002633 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002634 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002635 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002636 goto switchstate;
2637 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002638 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002639 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002640 if (!curpeer) {
2641 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002642 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002643 if (curpeer->appctx != appctx) {
2644 appctx->st0 = PEER_SESS_ST_END;
2645 goto switchstate;
2646 }
2647 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002648
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002649 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002650 if (repl <= 0) {
2651 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002652 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002653 goto switchstate;
2654 }
2655
2656 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002657 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002658 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002659 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002660 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002661 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002662 if (!curpeer) {
2663 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002664 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002665 if (curpeer->appctx != appctx) {
2666 appctx->st0 = PEER_SESS_ST_END;
2667 goto switchstate;
2668 }
2669 }
2670
Christopher Faulet3c053242022-07-27 10:49:31 +02002671 if (si_ic(si)->flags & CF_WROTE_DATA)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002672 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002673
Frédéric Lécaillece025572019-01-21 13:38:06 +01002674 reql = peer_getline(appctx);
2675 if (!reql)
2676 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002677
Frédéric Lécaillece025572019-01-21 13:38:06 +01002678 if (reql < 0)
2679 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002680
2681 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002682 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002683 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02002684
2685 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002686 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002687
2688 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002689 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002690 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002691 }
2692 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002693 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2694 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002695 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002696 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002697 goto switchstate;
2698 }
Willy Tarreau4781b152021-04-06 13:53:36 +02002699 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002700 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002701 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002702 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002703 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002704 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002705 char *msg_cur = trash.area;
2706 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002707 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02002708 int totl = 0;
2709
Willy Tarreau2d372c22018-11-05 17:12:27 +01002710 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002711 if (!curpeer) {
2712 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002713 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002714 if (curpeer->appctx != appctx) {
2715 appctx->st0 = PEER_SESS_ST_END;
2716 goto switchstate;
2717 }
2718 }
2719
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002720 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2721 if (reql <= 0) {
2722 if (reql == -1)
2723 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002724 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002725 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002726
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002727 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002728 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002729 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002730
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002731 curpeer->flags |= PEER_F_ALIVE;
2732
Emeric Brun2b920a12010-09-23 18:30:22 +02002733 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002734 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002735 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002736 goto switchstate;
2737
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002738send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002739 if (curpeer->flags & PEER_F_HEARTBEAT) {
2740 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002741 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002742 if (repl <= 0) {
2743 if (repl == -1)
2744 goto out;
2745 goto switchstate;
2746 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002747 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002748 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002749 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002750 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002751 if (repl <= 0) {
2752 if (repl == -1)
2753 goto out;
2754 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002755 }
2756
Emeric Brun2b920a12010-09-23 18:30:22 +02002757 /* noting more to do */
2758 goto out;
2759 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002760 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002761 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002762 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002763 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002764 if (peer_send_status_errormsg(appctx) == -1)
2765 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002766 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002767 goto switchstate;
2768 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002769 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002770 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002771 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002772 if (peer_send_error_size_limitmsg(appctx) == -1)
2773 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002774 appctx->st0 = PEER_SESS_ST_END;
2775 goto switchstate;
2776 }
2777 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002778 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
2779 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002780 if (curpeer)
2781 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002782 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002783 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002784 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002785 if (peer_send_error_protomsg(appctx) == -1) {
2786 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002787 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002788 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002789 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002790 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002791 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002792 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002793 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002794 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002795 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002796 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002797 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002798 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002799 curpeer = NULL;
2800 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002801 si_shutw(si);
2802 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002803 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002804 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002805 }
2806 }
2807 }
2808out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002809 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002810
2811 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002812 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002813 return;
2814}
2815
Willy Tarreau30576452015-04-13 13:50:30 +02002816static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002817 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002818 .name = "<PEER>", /* used for logging */
2819 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002820 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002821};
Emeric Brun2b920a12010-09-23 18:30:22 +02002822
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002823
Emeric Brun2b920a12010-09-23 18:30:22 +02002824/*
2825 * Use this function to force a close of a peer session
2826 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002827static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002828{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002829 struct appctx *appctx = peer->appctx;
2830
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002831 /* Note that the peer sessions which have just been created
2832 * (->st0 == PEER_SESS_ST_CONNECT) must not
2833 * be shutdown, if not, the TCP session will never be closed
2834 * and stay in CLOSE_WAIT state after having been closed by
2835 * the remote side.
2836 */
2837 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002838 return;
2839
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002840 if (appctx->applet != &peer_applet)
2841 return;
2842
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002843 __peer_session_deinit(peer);
2844
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002845 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002846 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002847}
2848
Willy Tarreau91d96282015-03-13 15:47:26 +01002849/* Pre-configures a peers frontend to accept incoming connections */
2850void peers_setup_frontend(struct proxy *fe)
2851{
2852 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002853 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002854 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01002855 fe->maxconn = 0;
2856 fe->conn_retries = CONN_RETRIES;
Christopher Faulet2d370282022-08-29 11:32:26 +02002857 fe->timeout.connect = MS_TO_TICKS(1000);
Willy Tarreau91d96282015-03-13 15:47:26 +01002858 fe->timeout.client = MS_TO_TICKS(5000);
Christopher Faulet2d370282022-08-29 11:32:26 +02002859 fe->timeout.server = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002860 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002861 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002862 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002863 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002864}
2865
Emeric Brun2b920a12010-09-23 18:30:22 +02002866/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002867 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002868 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002869static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002870{
Willy Tarreau04b92862017-09-15 11:01:04 +02002871 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002872 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002873 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002874 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02002875
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01002876 peer->new_conn++;
Christopher Fauletd73a5cc2022-07-26 19:14:36 +02002877 peer->reconnect = tick_add(now_ms, (stopping ? MS_TO_TICKS(PEER_LOCAL_RECONNECT_TIMEOUT) : MS_TO_TICKS(PEER_RECONNECT_TIMEOUT)));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002878 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002879 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002880 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002881 s = NULL;
2882
Emeric Brun1138fd02017-06-19 12:38:55 +02002883 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002884 if (!appctx)
2885 goto out_close;
2886
2887 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002888 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002889
Willy Tarreau04b92862017-09-15 11:01:04 +02002890 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002891 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002892 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002893 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002894 }
2895
Christopher Faulet26256f82020-09-14 11:40:13 +02002896 if ((s = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002897 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002898 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002899 }
2900
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002901 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002902 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002903 appctx_wakeup(appctx);
2904
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002905 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002906 s->target = peer_session_target(peer, s);
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002907 if (!sockaddr_alloc(&s->target_addr, &peer->addr, sizeof(peer->addr)))
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002908 goto out_free_strm;
Willy Tarreau02efeda2019-07-18 17:21:24 +02002909 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Willy Tarreaudbd02672017-12-06 17:39:53 +01002910 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002911
Emeric Brun2b920a12010-09-23 18:30:22 +02002912 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002913 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002914
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002915 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002916
Emeric Brunb3971ab2015-05-12 18:49:09 +02002917 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002918 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau4781b152021-04-06 13:53:36 +02002919 _HA_ATOMIC_INC(&active_peers);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002920 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002921
2922 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02002923 out_free_strm:
Willy Tarreau2b718102021-04-21 07:32:39 +02002924 LIST_DELETE(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002925 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002926 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002927 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002928 out_free_appctx:
2929 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002930 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002931 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002932}
2933
2934/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002935 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01002936 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
2937 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02002938 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002939struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002940{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002941 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002942 struct peer *ps;
2943 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002944
2945 task->expire = TICK_ETERNITY;
2946
Emeric Brunb3971ab2015-05-12 18:49:09 +02002947 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002948 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002949 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002950 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002951 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002952 return NULL;
2953 }
2954
Emeric Brun80527f52017-06-19 17:46:37 +02002955 /* Acquire lock for all peers of the section */
2956 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002957 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002958
Emeric Brun2b920a12010-09-23 18:30:22 +02002959 if (!stopping) {
2960 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002961
Emeric Brun2c4ab412021-04-21 16:06:35 +02002962 /* resync timeout set to TICK_ETERNITY means we just start
2963 * a new process and timer was not initialized.
2964 * We must arm this timer to switch to a request to a remote
2965 * node if incoming connection from old local process never
2966 * comes.
2967 */
2968 if (peers->resync_timeout == TICK_ETERNITY)
2969 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
2970
Emeric Brunb3971ab2015-05-12 18:49:09 +02002971 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2972 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2973 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002974 /* Resync from local peer needed
2975 no peer was assigned for the lesson
2976 and no old local peer found
2977 or resync timeout expire */
2978
2979 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002980 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002981 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02002982
2983 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002984 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002985 }
2986
2987 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002988 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002989 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002990 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002991 if (!ps->appctx) {
2992 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002993 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002994 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002995 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002996 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002997 tick_is_expired(ps->reconnect, now_ms))) {
2998 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002999 * or previous peer connection established with success
3000 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003001 * and reconnection timer is expired */
3002
3003 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003004 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003005 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003006 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003007 /* If previous session failed during connection
3008 * but reconnection timer is not expired */
3009
3010 /* reschedule task for reconnect */
3011 task->expire = tick_first(task->expire, ps->reconnect);
3012 }
3013 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003014 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003015 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003016 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003017 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3018 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003019 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3020 /* Resync from a remote is needed
3021 * and no peer was assigned for lesson
3022 * and current peer may be up2date */
3023
3024 /* assign peer for the lesson */
3025 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003026 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003027 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003028
Willy Tarreau9df94c22016-10-31 18:42:52 +01003029 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003030 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003031 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003032 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003033 int update_to_push = 0;
3034
Emeric Brunb3971ab2015-05-12 18:49:09 +02003035 /* Awake session if there is data to push */
3036 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003037 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003038 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003039 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003040 /* There is no need to send a heartbeat message
3041 * when some updates must be pushed. The remote
3042 * peer will consider <ps> peer as alive when it will
3043 * receive these updates.
3044 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003045 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003046 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003047 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Christopher Fauletba853282023-10-16 07:48:26 +02003048 /* Refresh reconnect if necessary */
3049 if (tick_is_expired(ps->reconnect, now_ms))
3050 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003051 /* We are going to send updates, let's ensure we will
3052 * come back to send heartbeat messages or to reconnect.
3053 */
3054 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003055 appctx_wakeup(ps->appctx);
3056 break;
3057 }
3058 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003059 /* When there are updates to send we do not reconnect
3060 * and do not send heartbeat message either.
3061 */
3062 if (!update_to_push) {
3063 if (tick_is_expired(ps->reconnect, now_ms)) {
3064 if (ps->flags & PEER_F_ALIVE) {
3065 /* This peer was alive during a 'reconnect' period.
3066 * Flag it as not alive again for the next period.
3067 */
3068 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003069 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003070 }
3071 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003072 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003073 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003074 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003075 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003076 }
3077 }
3078 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3079 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3080 ps->flags |= PEER_F_HEARTBEAT;
3081 appctx_wakeup(ps->appctx);
3082 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003083 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003084 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003085 }
3086 /* else do nothing */
3087 } /* SUCCESSCODE */
3088 } /* !ps->peer->local */
3089 } /* for */
3090
3091 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003092 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3093 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3094 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003095 /* Resync from remote peer needed
3096 * no peer was assigned for the lesson
3097 * and resync timeout expire */
3098
3099 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003100 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003101 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003102 }
3103
Emeric Brunb3971ab2015-05-12 18:49:09 +02003104 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003105 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003106 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3107 if (!tick_is_expired(peers->resync_timeout, now_ms))
3108 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003109 }
3110 } /* !stopping */
3111 else {
3112 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003113 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003114 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003115 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003116 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003117 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003118 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003119
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003120 /* disconnect all connected peers to process a local sync
3121 * this must be done only the first time we are switching
3122 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003123 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003124 for (ps = peers->remote; ps; ps = ps->next) {
3125 /* we're killing a connection, we must apply a random delay before
3126 * retrying otherwise the other end will do the same and we can loop
3127 * for a while.
3128 */
3129 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3130 if (ps->appctx) {
3131 peer_session_forceshutdown(ps);
3132 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003133 }
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003134
3135 /* Set resync timeout for the local peer and request a immediate reconnect */
3136 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3137 peers->local->reconnect = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003138 }
3139 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003140
Emeric Brunb3971ab2015-05-12 18:49:09 +02003141 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003142 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003143 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003144 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003145 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003146 peers->flags &= ~PEERS_F_DONOTSTOP;
3147 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003148 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003149 }
3150 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003151 else if (!ps->appctx) {
Christopher Fauletda21ebc2022-08-26 18:40:46 +02003152 /* Re-arm resync timeout if necessary */
3153 if (!tick_isset(peers->resync_timeout))
3154 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3155
Willy Tarreau9df94c22016-10-31 18:42:52 +01003156 /* If there's no active peer connection */
Christopher Faulet87da1db2022-08-26 18:46:16 +02003157 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED &&
3158 !tick_is_expired(peers->resync_timeout, now_ms) &&
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003159 (ps->statuscode == 0 ||
3160 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3161 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3162 ps->statuscode == PEER_SESS_SC_TRYAGAIN)) {
Christopher Faulet87da1db2022-08-26 18:46:16 +02003163 /* The resync is finished for the local peer and
3164 * the resync timeout is not expired and
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003165 * connection never tried
3166 * or previous peer connection was successfully established
3167 * or previous tcp connect succeeded but init state incomplete
3168 * or during previous connect, peer replies a try again statuscode */
Emeric Brun2b920a12010-09-23 18:30:22 +02003169
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003170 if (!tick_is_expired(ps->reconnect, now_ms)) {
3171 /* reconnection timer is not expired. reschedule task for reconnect */
3172 task->expire = tick_first(task->expire, ps->reconnect);
3173 }
3174 else {
3175 /* connect to the local peer if we must push a local sync */
3176 if (peers->flags & PEERS_F_DONOTSTOP) {
3177 peer_session_create(peers, ps);
3178 }
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003179 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003180 }
3181 else {
3182 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003183 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003184 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003185 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003186 peers->flags &= ~PEERS_F_DONOTSTOP;
3187 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003188 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003189 }
3190 }
3191 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003192 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Christopher Fauletda21ebc2022-08-26 18:40:46 +02003193 /* Reset resync timeout during a resync */
3194 peers->resync_timeout = TICK_ETERNITY;
3195
Willy Tarreau9df94c22016-10-31 18:42:52 +01003196 /* current peer connection is active and established
3197 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003198 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003199 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003200 appctx_wakeup(ps->appctx);
3201 break;
3202 }
3203 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003204 }
3205 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003206
3207 /* Release lock for all peers of the section */
3208 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003209 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003210
Emeric Brun2b920a12010-09-23 18:30:22 +02003211 /* Wakeup for re-connect */
3212 return task;
3213}
3214
Emeric Brunb3971ab2015-05-12 18:49:09 +02003215
Emeric Brun2b920a12010-09-23 18:30:22 +02003216/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003217 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003218 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003219int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003220{
Emeric Brun2b920a12010-09-23 18:30:22 +02003221 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003222
Emeric Brun2b920a12010-09-23 18:30:22 +02003223 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003224 peers->peers_fe->maxconn += 3;
3225 }
3226
Emeric Brunc60def82017-09-27 14:59:38 +02003227 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02003228 if (!peers->sync_task)
3229 return 0;
3230
Emeric Brunb3971ab2015-05-12 18:49:09 +02003231 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003232 peers->sync_task->context = (void *)peers;
3233 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3234 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003235 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003236}
3237
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003238/*
3239 * Allocate a cache a dictionary entries used upon transmission.
3240 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003241static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003242{
3243 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003244 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003245
3246 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003247 entries = calloc(max_entries, sizeof *entries);
3248 if (!d || !entries)
3249 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003250
3251 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003252 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003253 d->cached_entries = EB_ROOT_UNIQUE;
3254 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003255
3256 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003257
3258 err:
3259 free(d);
3260 free(entries);
3261 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003262}
3263
3264/*
3265 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3266 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003267 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003268 * Must be deallocated calling free_dcache().
3269 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003270static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003271{
3272 struct dcache_tx *dc_tx;
3273 struct dcache *dc;
3274 struct dcache_rx *dc_rx;
3275
3276 dc = calloc(1, sizeof *dc);
3277 dc_tx = new_dcache_tx(max_entries);
3278 dc_rx = calloc(max_entries, sizeof *dc_rx);
3279 if (!dc || !dc_tx || !dc_rx)
3280 goto err;
3281
3282 dc->tx = dc_tx;
3283 dc->rx = dc_rx;
3284 dc->max_entries = max_entries;
3285
3286 return dc;
3287
3288 err:
3289 free(dc);
3290 free(dc_tx);
3291 free(dc_rx);
3292 return NULL;
3293}
3294
3295/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003296 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3297 * entries used upon transmission.
3298 * Return the entry if found, NULL if not.
3299 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003300static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3301 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003302{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003303 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003304}
3305
3306/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003307 * Flush <dc> cache.
3308 * Always succeeds.
3309 */
3310static inline void flush_dcache(struct peer *peer)
3311{
3312 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003313 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003314
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003315 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003316 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003317 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003318 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3319 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003320 }
3321 dc->tx->prev_lookup = NULL;
3322 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003323
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003324 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003325}
3326
3327/*
3328 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3329 * with information provided by <i> dictionary cache entry (especially the value
3330 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003331 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003332 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003333static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003334{
3335 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003336 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003337
3338 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003339
3340 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3341 o = dc_tx->prev_lookup;
3342 } else {
3343 o = dcache_tx_lookup_value(dc_tx, i);
3344 if (o) {
3345 /* Save it */
3346 dc_tx->prev_lookup = o;
3347 }
3348 }
3349
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003350 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003351 /* Copy the ID. */
3352 i->id = o - dc->tx->entries;
3353 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003354 }
3355
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003356 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003357 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003358
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003359 ebpt_delete(o);
3360 o->key = i->entry.key;
3361 ebpt_insert(&dc_tx->cached_entries, o);
3362 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003363
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003364 /* Update the index for the next entry to put in cache */
3365 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003366
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003367 return o;
3368}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003369
3370/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003371 * Allocate a dictionary cache for each peer of <peers> section.
3372 * Return 1 if succeeded, 0 if not.
3373 */
3374int peers_alloc_dcache(struct peers *peers)
3375{
3376 struct peer *p;
3377
3378 for (p = peers->remote; p; p = p->next) {
3379 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3380 if (!p->dcache)
3381 return 0;
3382 }
3383
3384 return 1;
3385}
3386
3387/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003388 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003389 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003390 */
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003391int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003392{
3393 struct shared_table *st;
3394 struct peer * curpeer;
3395 int id = 0;
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003396 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003397
3398 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003399 st = calloc(1,sizeof(*st));
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003400 if (!st) {
3401 retval = 1;
3402 break;
3403 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003404 st->table = table;
3405 st->next = curpeer->tables;
3406 if (curpeer->tables)
3407 id = curpeer->tables->local_id;
3408 st->local_id = id + 1;
3409
Emeric Brun2cc201f2021-04-23 12:21:26 +02003410 /* If peer is local we inc table
3411 * refcnt to protect against flush
3412 * until this process pushed all
3413 * table content to the new one
3414 */
3415 if (curpeer->local)
3416 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003417 curpeer->tables = st;
3418 }
3419
3420 table->sync_task = peers->sync_task;
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003421
3422 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003423}
3424
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003425/*
3426 * Parse the "show peers" command arguments.
3427 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3428 * error message.
3429 */
3430static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3431{
3432 appctx->ctx.cfgpeers.target = NULL;
3433
Willy Tarreau49962b52021-02-12 16:56:22 +01003434 if (strcmp(args[2], "dict") == 0) {
3435 /* show the dictionaries (large dump) */
3436 appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT;
3437 args++;
3438 } else if (strcmp(args[2], "-") == 0)
3439 args++; // allows to show a section called "dict"
3440
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003441 if (*args[2]) {
3442 struct peers *p;
3443
3444 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003445 if (strcmp(p->id, args[2]) == 0) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003446 appctx->ctx.cfgpeers.target = p;
3447 break;
3448 }
3449 }
3450
Willy Tarreau9d008692019-08-09 11:21:01 +02003451 if (!p)
3452 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003453 }
3454
3455 return 0;
3456}
3457
3458/*
3459 * This function dumps the peer state information of <peers> "peers" section.
3460 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3461 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3462 */
3463static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3464{
3465 struct tm tm;
3466
3467 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003468 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écaille95679dc2019-04-15 10:25:27 +02003469 peers,
3470 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3471 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003472 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003473 peers->resync_timeout ?
3474 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3475 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003476 TICKS_TO_MS(1000)) : "<NEVER>",
3477 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003478
3479 if (ci_putchk(si_ic(si), msg) == -1) {
3480 si_rx_room_blk(si);
3481 return 0;
3482 }
3483
3484 return 1;
3485}
3486
3487/*
3488 * This function dumps <peer> state information.
3489 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3490 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3491 */
Willy Tarreau49962b52021-02-12 16:56:22 +01003492static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer, int flags)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003493{
3494 struct connection *conn;
3495 char pn[INET6_ADDRSTRLEN];
3496 struct stream_interface *peer_si;
3497 struct stream *peer_s;
3498 struct appctx *appctx;
3499 struct shared_table *st;
3500
3501 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003502 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003503 peer, peer->id,
3504 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003505 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003506 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003507 statuscode_str(peer->statuscode));
3508
3509 chunk_appendf(msg, " last_hdshk=%s\n",
3510 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3511 TICKS_TO_MS(1000)) : "<NEVER>");
3512
3513 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003514 peer->reconnect ?
3515 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3516 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003517 TICKS_TO_MS(1000)) : "<NEVER>");
3518
3519 chunk_appendf(msg, " heartbeat=%s",
3520 peer->heartbeat ?
3521 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3522 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3523 TICKS_TO_MS(1000)) : "<NEVER>");
3524
3525 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écailleec1c10b2019-11-07 15:22:33 +01003526 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003527 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003528
3529 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3530
3531 appctx = peer->appctx;
3532 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003533 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003534
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003535 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3536 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003537
3538 peer_si = peer->appctx->owner;
3539 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003540 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003541
3542 peer_s = si_strm(peer_si);
3543 if (!peer_s)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003544 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003545
3546 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3547
3548 conn = objt_conn(strm_orig(peer_s));
3549 if (conn)
3550 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3551
Willy Tarreau3ca14902019-07-17 14:53:15 +02003552 switch (conn && conn_get_src(conn) ? addr_to_str(conn->src, pn, sizeof(pn)) : AF_UNSPEC) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003553 case AF_INET:
3554 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003555 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003556 break;
3557 case AF_UNIX:
3558 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3559 break;
3560 }
3561
Willy Tarreau3ca14902019-07-17 14:53:15 +02003562 switch (conn && conn_get_dst(conn) ? addr_to_str(conn->dst, pn, sizeof(pn)) : AF_UNSPEC) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003563 case AF_INET:
3564 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003565 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003566 break;
3567 case AF_UNIX:
3568 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3569 break;
3570 }
3571
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003572 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003573 if (peer->remote_table)
3574 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3575 peer->remote_table,
3576 peer->remote_table->table->id,
3577 peer->remote_table->local_id,
3578 peer->remote_table->remote_id);
3579
3580 if (peer->last_local_table)
3581 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3582 peer->last_local_table,
3583 peer->last_local_table->table->id,
3584 peer->last_local_table->local_id,
3585 peer->last_local_table->remote_id);
3586
3587 if (peer->tables) {
3588 chunk_appendf(&trash, "\n shared tables:");
3589 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003590 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003591 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003592 struct dcache *dcache;
3593
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003594 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003595 dcache = peer->dcache;
3596
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003597 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3598 "flags=0x%x remote_data=0x%llx",
3599 st, st->local_id, st->remote_id,
3600 st->flags, (unsigned long long)st->remote_data);
3601 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3602 " teaching_origin=%u update=%u",
3603 st->last_acked, st->last_pushed, st->last_get,
3604 st->teaching_origin, st->update);
3605 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003606 " commitupdate=%u refcnt=%u",
3607 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003608 if (flags & PEERS_SHOW_F_DICT) {
3609 chunk_appendf(&trash, "\n TX dictionary cache:");
3610 count = 0;
3611 for (i = 0; i < dcache->max_entries; i++) {
3612 struct ebpt_node *node;
3613 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003614
Willy Tarreau49962b52021-02-12 16:56:22 +01003615 node = &dcache->tx->entries[i];
3616 if (!node->key)
3617 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003618
Willy Tarreau49962b52021-02-12 16:56:22 +01003619 if (!count++)
3620 chunk_appendf(&trash, "\n ");
3621 de = node->key;
3622 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3623 count &= 0x3;
3624 }
3625 chunk_appendf(&trash, "\n RX dictionary cache:");
3626 count = 0;
3627 for (i = 0; i < dcache->max_entries; i++) {
3628 if (!count++)
3629 chunk_appendf(&trash, "\n ");
3630 chunk_appendf(&trash, " %3u -> %s", i,
3631 dcache->rx[i].de ?
3632 (char *)dcache->rx[i].de->value.key : "-");
3633 count &= 0x3;
3634 }
3635 } else {
3636 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003637 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003638 }
3639 }
3640
3641 end:
3642 chunk_appendf(&trash, "\n");
3643 if (ci_putchk(si_ic(si), msg) == -1) {
3644 si_rx_room_blk(si);
3645 return 0;
3646 }
3647
3648 return 1;
3649}
3650
3651/*
3652 * This function dumps all the peers of "peers" section.
3653 * Returns 0 if the output buffer is full and needs to be called
3654 * again, non-zero if not. It proceeds in an isolated thread, so
3655 * there is no thread safety issue here.
3656 */
3657static int cli_io_handler_show_peers(struct appctx *appctx)
3658{
3659 int show_all;
3660 int ret = 0, first_peers = 1;
3661 struct stream_interface *si = appctx->owner;
3662
3663 thread_isolate();
3664
3665 show_all = !appctx->ctx.cfgpeers.target;
3666
3667 chunk_reset(&trash);
3668
3669 while (appctx->st2 != STAT_ST_FIN) {
3670 switch (appctx->st2) {
3671 case STAT_ST_INIT:
3672 if (show_all)
3673 appctx->ctx.cfgpeers.peers = cfg_peers;
3674 else
3675 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3676
3677 appctx->st2 = STAT_ST_LIST;
3678 /* fall through */
3679
3680 case STAT_ST_LIST:
3681 if (!appctx->ctx.cfgpeers.peers) {
3682 /* No more peers list. */
3683 appctx->st2 = STAT_ST_END;
3684 }
3685 else {
3686 if (!first_peers)
3687 chunk_appendf(&trash, "\n");
3688 else
3689 first_peers = 0;
3690 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3691 goto out;
3692
3693 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3694 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3695 appctx->st2 = STAT_ST_INFO;
3696 }
3697 break;
3698
3699 case STAT_ST_INFO:
3700 if (!appctx->ctx.cfgpeers.peer) {
3701 /* End of peer list */
3702 if (show_all)
3703 appctx->st2 = STAT_ST_LIST;
3704 else
3705 appctx->st2 = STAT_ST_END;
3706 }
3707 else {
Willy Tarreau49962b52021-02-12 16:56:22 +01003708 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer, appctx->ctx.cfgpeers.flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003709 goto out;
3710
3711 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3712 }
3713 break;
3714
3715 case STAT_ST_END:
3716 appctx->st2 = STAT_ST_FIN;
3717 break;
3718 }
3719 }
3720 ret = 1;
3721 out:
3722 thread_release();
3723 return ret;
3724}
3725
3726/*
3727 * CLI keywords.
3728 */
3729static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003730 { { "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écaille95679dc2019-04-15 10:25:27 +02003731 {},
3732}};
3733
3734/* Register cli keywords */
3735INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3736