blob: ece789d738bce097bd7e7397b3b058951f2ab1b8 [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" },
Frédéric Lécailled8659352020-11-10 16:18:03 +0100328};
329
330static const struct name_desc peers_trace_lockon_args[4] = {
331 /* arg1 */ { /* already used by the connection */ },
332 /* arg2 */ { .name="peers", .desc="Peers protocol" },
333 /* arg3 */ { },
334 /* arg4 */ { }
335};
336
337static const struct name_desc peers_trace_decoding[] = {
338#define PEERS_VERB_CLEAN 1
339 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
340 { /* end */ }
341};
342
343
344struct trace_source trace_peers = {
345 .name = IST("peers"),
346 .desc = "Peers protocol",
347 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
348 .default_cb = peers_trace,
349 .known_events = peers_trace_events,
350 .lockon_args = peers_trace_lockon_args,
351 .decoding = peers_trace_decoding,
352 .report_events = ~0, /* report everything by default */
353};
354
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100355/* Return peer control message types as strings (only for debugging purpose). */
356static inline char *ctrl_msg_type_str(unsigned int type)
357{
358 switch (type) {
359 case PEER_MSG_CTRL_RESYNCREQ:
360 return "RESYNCREQ";
361 case PEER_MSG_CTRL_RESYNCFINISHED:
362 return "RESYNCFINISHED";
363 case PEER_MSG_CTRL_RESYNCPARTIAL:
364 return "RESYNCPARTIAL";
365 case PEER_MSG_CTRL_RESYNCCONFIRM:
366 return "RESYNCCONFIRM";
367 case PEER_MSG_CTRL_HEARTBEAT:
368 return "HEARTBEAT";
369 default:
370 return "???";
371 }
372}
373
Frédéric Lécailled8659352020-11-10 16:18:03 +0100374#define TRACE_SOURCE &trace_peers
375INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
376
377static void peers_trace(enum trace_level level, uint64_t mask,
378 const struct trace_source *src,
379 const struct ist where, const struct ist func,
380 const void *a1, const void *a2, const void *a3, const void *a4)
381{
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100382 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécailled8659352020-11-10 16:18:03 +0100383 if (a2) {
384 const struct peer *peer = a2;
385
386 chunk_appendf(&trace_buf, " peer=%s", peer->id);
387 }
388 if (a3) {
389 const char *p = a3;
390
391 chunk_appendf(&trace_buf, " @%p", p);
392 }
393 if (a4) {
394 const size_t *val = a4;
395
396 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
397 }
398 }
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100399
400 if (mask & PEERS_EV_DEFMSG) {
401 if (a2) {
402 const struct peer *peer = a2;
403
404 chunk_appendf(&trace_buf, " peer=%s", peer->id);
405 }
406 if (a3) {
407 const char *p = a3;
408
409 chunk_appendf(&trace_buf, " @%p", p);
410 }
411 if (a4) {
412 const int *val = a4;
413
414 chunk_appendf(&trace_buf, " %d", *val);
415 }
416 }
417
418 if (mask & PEERS_EV_CTRLMSG) {
419 if (a2) {
420 const unsigned char *ctrl_msg_type = a2;
421
422 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
423
424 }
425 if (a3) {
426 const char *local_peer = a3;
427
428 chunk_appendf(&trace_buf, " %s", local_peer);
429 }
430
431 if (a4) {
432 const char *remote_peer = a4;
433
434 chunk_appendf(&trace_buf, " -> %s", remote_peer);
435 }
436 }
437
438 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
439 if (a2) {
440 const struct peer *peer = a2;
441 struct peers *peers = NULL;
442
Frédéric Lécaille4b1a05f2021-01-17 13:08:39 +0100443 if (peer && peer->appctx) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100444 struct stream_interface *si;
445
446 si = peer->appctx->owner;
447 if (si) {
448 struct stream *s = si_strm(si);
449
450 peers = strm_fe(s)->parent;
451 }
452 }
453
454 if (peers)
455 chunk_appendf(&trace_buf, " %s", peers->local->id);
456 if (peer)
457 chunk_appendf(&trace_buf, " -> %s", peer->id);
458 }
459
460 if (a3) {
461 const int *prev_state = a3;
462
463 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
464 }
465 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100466}
467
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200468static const char *statuscode_str(int statuscode)
469{
470 switch (statuscode) {
471 case PEER_SESS_SC_CONNECTCODE:
472 return "CONN";
473 case PEER_SESS_SC_CONNECTEDCODE:
474 return "HSHK";
475 case PEER_SESS_SC_SUCCESSCODE:
476 return "ESTA";
477 case PEER_SESS_SC_TRYAGAIN:
478 return "RETR";
479 case PEER_SESS_SC_ERRPROTO:
480 return "PROT";
481 case PEER_SESS_SC_ERRVERSION:
482 return "VERS";
483 case PEER_SESS_SC_ERRHOST:
484 return "NAME";
485 case PEER_SESS_SC_ERRPEER:
486 return "UNKN";
487 default:
488 return "NONE";
489 }
490}
491
Emeric Brun18928af2017-03-29 16:32:53 +0200492/* This function encode an uint64 to 'dynamic' length format.
493 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500494 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200495 At return, the *str is set at the next Byte after then
496 encoded integer. The function returns then length of the
497 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200498int intencode(uint64_t i, char **str) {
499 int idx = 0;
500 unsigned char *msg;
501
Emeric Brunb3971ab2015-05-12 18:49:09 +0200502 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200503 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200504 msg[0] = (unsigned char)i;
505 *str = (char *)&msg[idx+1];
506 return (idx+1);
507 }
508
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200509 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
510 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
511 while (i >= PEER_ENC_STOP_BYTE) {
512 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
513 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200514 }
515 msg[++idx] = (unsigned char)i;
516 *str = (char *)&msg[idx+1];
517 return (idx+1);
518}
519
520
521/* This function returns the decoded integer or 0
522 if decode failed
523 *str point on the beginning of the integer to decode
524 at the end of decoding *str point on the end of the
525 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200526uint64_t intdecode(char **str, char *end)
527{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200528 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200529 uint64_t i;
530 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531
532 if (!*str)
533 return 0;
534
535 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200536 if (msg >= (unsigned char *)end)
537 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200538
Emeric Brun18928af2017-03-29 16:32:53 +0200539 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200540 if (i >= PEER_ENC_2BYTES_MIN) {
541 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200542 do {
543 if (msg >= (unsigned char *)end)
544 goto fail;
545 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200546 shift += PEER_ENC_STOP_BIT;
547 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200548 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100549 *str = (char *)msg;
550 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200551
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100552 fail:
553 *str = NULL;
554 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200555}
Emeric Brun2b920a12010-09-23 18:30:22 +0200556
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100557/*
558 * Build a "hello" peer protocol message.
559 * Return the number of written bytes written to build this messages if succeeded,
560 * 0 if not.
561 */
562static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
563{
564 int min_ver, ret;
565 struct peer *peer;
566
567 peer = p->hello.peer;
568 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
569 /* Prepare headers */
570 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
571 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
572 if (ret >= size)
573 return 0;
574
575 return ret;
576}
577
578/*
579 * Build a "handshake succeeded" status message.
580 * Return the number of written bytes written to build this messages if succeeded,
581 * 0 if not.
582 */
583static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
584{
585 int ret;
586
587 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
588 if (ret >= size)
589 return 0;
590
591 return ret;
592}
593
594/*
595 * Build an error status message.
596 * Return the number of written bytes written to build this messages if succeeded,
597 * 0 if not.
598 */
599static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
600{
601 int ret;
602 unsigned int st1;
603
604 st1 = p->error_status.st1;
605 ret = snprintf(msg, size, "%d\n", st1);
606 if (ret >= size)
607 return 0;
608
609 return ret;
610}
611
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200612/* Set the stick-table UPDATE message type byte at <msg_type> address,
613 * depending on <use_identifier> and <use_timed> boolean parameters.
614 * Always successful.
615 */
616static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
617{
618 if (use_timed) {
619 if (use_identifier)
620 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
621 else
622 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
623 }
624 else {
625 if (use_identifier)
626 *msg_type = PEER_MSG_STKT_UPDATE;
627 else
628 *msg_type = PEER_MSG_STKT_INCUPDATE;
629 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200630}
Emeric Brun2b920a12010-09-23 18:30:22 +0200631/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200632 * This prepare the data update message on the stick session <ts>, <st> is the considered
633 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800634 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200635 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
636 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200637 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100638static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200639{
640 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200641 unsigned short datalen;
642 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200643 unsigned int data_type;
644 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100645 struct stksess *ts;
646 struct shared_table *st;
647 unsigned int updateid;
648 int use_identifier;
649 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200650 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100651
652 ts = p->updt.stksess;
653 st = p->updt.shared_table;
654 updateid = p->updt.updateid;
655 use_identifier = p->updt.use_identifier;
656 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200657 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200658
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200659 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200660
Emeric Brun2b920a12010-09-23 18:30:22 +0200661 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200662
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500663 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200664 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200665 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200666 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200667
668 /* encode update identifier if needed */
669 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200670 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200671 memcpy(cursor, &netinteger, sizeof(netinteger));
672 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200673 }
674
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200675 if (use_timed) {
676 netinteger = htonl(tick_remain(now_ms, ts->expire));
677 memcpy(cursor, &netinteger, sizeof(netinteger));
678 cursor += sizeof(netinteger);
679 }
680
Emeric Brunb3971ab2015-05-12 18:49:09 +0200681 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200682 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200683 int stlen = strlen((char *)ts->key.key);
684
Emeric Brunb3971ab2015-05-12 18:49:09 +0200685 intencode(stlen, &cursor);
686 memcpy(cursor, ts->key.key, stlen);
687 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200688 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200689 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100690 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200691 memcpy(cursor, &netinteger, sizeof(netinteger));
692 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200693 }
694 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200695 memcpy(cursor, ts->key.key, st->table->key_size);
696 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200697 }
698
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100699 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200700 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200701 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200702
Emeric Brun94900952015-06-11 18:25:54 +0200703 data_ptr = stktable_data_ptr(st->table, ts, data_type);
704 if (data_ptr) {
705 switch (stktable_data_types[data_type].std_type) {
706 case STD_T_SINT: {
707 int data;
708
709 data = stktable_data_cast(data_ptr, std_t_sint);
710 intencode(data, &cursor);
711 break;
712 }
713 case STD_T_UINT: {
714 unsigned int data;
715
716 data = stktable_data_cast(data_ptr, std_t_uint);
717 intencode(data, &cursor);
718 break;
719 }
720 case STD_T_ULL: {
721 unsigned long long data;
722
723 data = stktable_data_cast(data_ptr, std_t_ull);
724 intencode(data, &cursor);
725 break;
726 }
727 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200728 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200729
730 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
731 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
732 intencode(frqp->curr_ctr, &cursor);
733 intencode(frqp->prev_ctr, &cursor);
734 break;
735 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200736 case STD_T_DICT: {
737 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200738 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200739 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200740 char *beg, *end;
741 size_t value_len, data_len;
742 struct dcache *dc;
743
744 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100745 if (!de) {
746 /* No entry */
747 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200748 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100749 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200750
751 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200752 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200753 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200754 if (cached_de == &cde.entry) {
755 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
756 break;
757 /* Encode the length of the remaining data -> 1 */
758 intencode(1, &cursor);
759 /* Encode the cache entry ID */
760 intencode(cde.id + 1, &cursor);
761 }
762 else {
763 /* Leave enough room to encode the remaining data length. */
764 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
765 /* Encode the dictionary entry key */
766 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200767 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200768 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200769 intencode(value_len, &end);
770 /* Copy the data */
771 memcpy(end, de->value.key, value_len);
772 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200773 /* Encode the length of the data */
774 data_len = end - beg;
775 intencode(data_len, &cursor);
776 memmove(cursor, beg, data_len);
777 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200778 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200779 break;
780 }
Emeric Brun94900952015-06-11 18:25:54 +0200781 }
782 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200783 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100784 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200785
786 /* Compute datalen */
787 datalen = (cursor - datamsg);
788
789 /* prepare message header */
790 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200791 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200792 cursor = &msg[2];
793 intencode(datalen, &cursor);
794
795 /* move data after header */
796 memmove(cursor, datamsg, datalen);
797
798 /* return header size + data_len */
799 return (cursor - msg) + datalen;
800}
801
802/*
803 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800804 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200805 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
806 * check size)
807 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100808static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200809{
810 int len;
811 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200812 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200813 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200814 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200815 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100816 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200817
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100818 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200819 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200820
821 /* Encode data */
822
823 /* encode local id */
824 intencode(st->local_id, &cursor);
825
826 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100827 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200828 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100829 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200830 cursor += len;
831
832 /* encode table type */
833
Emeric Brun530ba382020-06-02 11:17:42 +0200834 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200835
836 /* encode table key size */
837 intencode(st->table->key_size, &cursor);
838
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200839 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200840 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200841 /* encode available known data types in table */
842 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
843 if (st->table->data_ofs[data_type]) {
844 switch (stktable_data_types[data_type].std_type) {
845 case STD_T_SINT:
846 case STD_T_UINT:
847 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200848 case STD_T_DICT:
Emeric Brun45f12b02021-07-01 18:54:05 +0200849 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200850 break;
Emeric Brun94900952015-06-11 18:25:54 +0200851 case STD_T_FRQP:
Emeric Brun45f12b02021-07-01 18:54:05 +0200852 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200853 intencode(data_type, &chunkq);
854 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200855 break;
856 }
857 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200858 }
859 intencode(data, &cursor);
860
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200861 /* Encode stick-table entries duration. */
862 intencode(st->table->expire, &cursor);
863
864 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200865 chunk->data = chunkq - chunkp;
866 memcpy(cursor, chunk->area, chunk->data);
867 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200868 }
869
Emeric Brunb3971ab2015-05-12 18:49:09 +0200870 /* Compute datalen */
871 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200872
Emeric Brunb3971ab2015-05-12 18:49:09 +0200873 /* prepare message header */
874 msg[0] = PEER_MSG_CLASS_STICKTABLE;
875 msg[1] = PEER_MSG_STKT_DEFINE;
876 cursor = &msg[2];
877 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200878
Emeric Brunb3971ab2015-05-12 18:49:09 +0200879 /* move data after header */
880 memmove(cursor, datamsg, datalen);
881
882 /* return header size + data_len */
883 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200884}
885
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886/*
887 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
888 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800889 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
891 * check size)
892 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100893static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200894{
895 unsigned short datalen;
896 char *cursor, *datamsg;
897 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100898 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200899
Frédéric Lécaille39143342019-05-24 14:32:27 +0200900 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200901
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100902 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200903 intencode(st->remote_id, &cursor);
904 netinteger = htonl(st->last_get);
905 memcpy(cursor, &netinteger, sizeof(netinteger));
906 cursor += sizeof(netinteger);
907
908 /* Compute datalen */
909 datalen = (cursor - datamsg);
910
911 /* prepare message header */
912 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200913 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200914 cursor = &msg[2];
915 intencode(datalen, &cursor);
916
917 /* move data after header */
918 memmove(cursor, datamsg, datalen);
919
920 /* return header size + data_len */
921 return (cursor - msg) + datalen;
922}
Emeric Brun2b920a12010-09-23 18:30:22 +0200923
924/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200925 * Function to deinit connected peer
926 */
927void __peer_session_deinit(struct peer *peer)
928{
929 struct stream_interface *si;
930 struct stream *s;
931 struct peers *peers;
932
933 if (!peer->appctx)
934 return;
935
936 si = peer->appctx->owner;
937 if (!si)
938 return;
939
940 s = si_strm(si);
941 if (!s)
942 return;
943
944 peers = strm_fe(s)->parent;
945 if (!peers)
946 return;
947
948 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +0200949 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200950
Willy Tarreau4781b152021-04-06 13:53:36 +0200951 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200952
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200953 flush_dcache(peer);
954
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200955 /* Re-init current table pointers to force announcement on re-connect */
956 peer->remote_table = peer->last_local_table = NULL;
957 peer->appctx = NULL;
958 if (peer->flags & PEER_F_LEARN_ASSIGN) {
959 /* unassign current peer for learning */
960 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
961 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
962
Emeric Brunccdfbae2021-04-28 12:59:35 +0200963 if (peer->local)
964 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
965 else
966 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200967 /* reschedule a resync */
968 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
969 }
970 /* reset teaching and learning flags to 0 */
971 peer->flags &= PEER_TEACH_RESET;
972 peer->flags &= PEER_LEARN_RESET;
973 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
974}
975
976/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200977 * Callback to release a session with a peer
978 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200979static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200980{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200981 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200982
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100983 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100984 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100985 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200986 return;
987
988 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200989 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100990 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200991 if (peer->appctx == appctx)
992 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +0200993 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100994 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200995 }
996}
997
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200998/* Retrieve the major and minor versions of peers protocol
999 * announced by a remote peer. <str> is a null-terminated
1000 * string with the following format: "<maj_ver>.<min_ver>".
1001 */
1002static int peer_get_version(const char *str,
1003 unsigned int *maj_ver, unsigned int *min_ver)
1004{
1005 unsigned int majv, minv;
1006 const char *pos, *saved;
1007 const char *end;
1008
1009 saved = pos = str;
1010 end = str + strlen(str);
1011
1012 majv = read_uint(&pos, end);
1013 if (saved == pos || *pos++ != '.')
1014 return -1;
1015
1016 saved = pos;
1017 minv = read_uint(&pos, end);
1018 if (saved == pos || pos != end)
1019 return -1;
1020
1021 *maj_ver = majv;
1022 *min_ver = minv;
1023
1024 return 0;
1025}
Emeric Brun2b920a12010-09-23 18:30:22 +02001026
1027/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001028 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1029 * '\n' character.
1030 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1031 * a line could not be read because the communication channel is closed.
1032 */
1033static inline int peer_getline(struct appctx *appctx)
1034{
1035 int n;
1036 struct stream_interface *si = appctx->owner;
1037
1038 n = co_getline(si_oc(si), trash.area, trash.size);
1039 if (!n)
1040 return 0;
1041
1042 if (n < 0 || trash.area[n - 1] != '\n') {
1043 appctx->st0 = PEER_SESS_ST_END;
1044 return -1;
1045 }
1046
1047 if (n > 1 && (trash.area[n - 2] == '\r'))
1048 trash.area[n - 2] = 0;
1049 else
1050 trash.area[n - 1] = 0;
1051
1052 co_skip(si_oc(si), n);
1053
1054 return n;
1055}
1056
1057/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001058 * Send a message after having called <peer_prepare_msg> to build it.
1059 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1060 * Returns -1 if there was not enough room left to send the message,
1061 * any other negative returned value must be considered as an error with an appcxt st0
1062 * returned value equal to PEER_SESS_ST_END.
1063 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001064static inline int peer_send_msg(struct appctx *appctx,
1065 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1066 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001067{
1068 int ret, msglen;
1069 struct stream_interface *si = appctx->owner;
1070
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001071 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001072 if (!msglen) {
1073 /* internal error: message does not fit in trash */
1074 appctx->st0 = PEER_SESS_ST_END;
1075 return 0;
1076 }
1077
1078 /* message to buffer */
1079 ret = ci_putblk(si_ic(si), trash.area, msglen);
1080 if (ret <= 0) {
1081 if (ret == -1) {
1082 /* No more write possible */
1083 si_rx_room_blk(si);
1084 return -1;
1085 }
1086 appctx->st0 = PEER_SESS_ST_END;
1087 }
1088
1089 return ret;
1090}
1091
1092/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001093 * Send a hello message.
1094 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1095 * Returns -1 if there was not enough room left to send the message,
1096 * any other negative returned value must be considered as an error with an appcxt st0
1097 * returned value equal to PEER_SESS_ST_END.
1098 */
1099static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1100{
1101 struct peer_prep_params p = {
1102 .hello.peer = peer,
1103 };
1104
1105 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1106}
1107
1108/*
1109 * Send a success peer handshake status message.
1110 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1111 * Returns -1 if there was not enough room left to send the message,
1112 * any other negative returned value must be considered as an error with an appcxt st0
1113 * returned value equal to PEER_SESS_ST_END.
1114 */
1115static inline int peer_send_status_successmsg(struct appctx *appctx)
1116{
1117 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1118}
1119
1120/*
1121 * Send a peer handshake status error message.
1122 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1123 * Returns -1 if there was not enough room left to send the message,
1124 * any other negative returned value must be considered as an error with an appcxt st0
1125 * returned value equal to PEER_SESS_ST_END.
1126 */
1127static inline int peer_send_status_errormsg(struct appctx *appctx)
1128{
1129 struct peer_prep_params p = {
1130 .error_status.st1 = appctx->st1,
1131 };
1132
1133 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1134}
1135
1136/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001137 * Send a stick-table switch message.
1138 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1139 * Returns -1 if there was not enough room left to send the message,
1140 * any other negative returned value must be considered as an error with an appcxt st0
1141 * returned value equal to PEER_SESS_ST_END.
1142 */
1143static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1144{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001145 struct peer_prep_params p = {
1146 .swtch.shared_table = st,
1147 };
1148
1149 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001150}
1151
1152/*
1153 * Send a stick-table update acknowledgement message.
1154 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1155 * Returns -1 if there was not enough room left to send the message,
1156 * any other negative returned value must be considered as an error with an appcxt st0
1157 * returned value equal to PEER_SESS_ST_END.
1158 */
1159static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1160{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001161 struct peer_prep_params p = {
1162 .ack.shared_table = st,
1163 };
1164
1165 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001166}
1167
1168/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001169 * Send a stick-table update message.
1170 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1171 * Returns -1 if there was not enough room left to send the message,
1172 * any other negative returned value must be considered as an error with an appcxt st0
1173 * returned value equal to PEER_SESS_ST_END.
1174 */
1175static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1176 unsigned int updateid, int use_identifier, int use_timed)
1177{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001178 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001179 .updt = {
1180 .stksess = ts,
1181 .shared_table = st,
1182 .updateid = updateid,
1183 .use_identifier = use_identifier,
1184 .use_timed = use_timed,
1185 .peer = appctx->ctx.peers.ptr,
1186 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001187 };
1188
1189 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001190}
1191
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001192/*
1193 * Build a peer protocol control class message.
1194 * Returns the number of written bytes used to build the message if succeeded,
1195 * 0 if not.
1196 */
1197static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1198{
1199 if (size < sizeof p->control.head)
1200 return 0;
1201
1202 msg[0] = p->control.head[0];
1203 msg[1] = p->control.head[1];
1204
1205 return 2;
1206}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001207
1208/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001209 * Send a stick-table synchronization request message.
1210 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1211 * Returns -1 if there was not enough room left to send the message,
1212 * any other negative returned value must be considered as an error with an appctx st0
1213 * returned value equal to PEER_SESS_ST_END.
1214 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001215static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1216 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001217{
1218 struct peer_prep_params p = {
1219 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1220 };
1221
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001222 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1223 NULL, &p.control.head[1], peers->local->id, peer->id);
1224
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001225 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1226}
1227
1228/*
1229 * Send a stick-table synchronization confirmation message.
1230 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1231 * Returns -1 if there was not enough room left to send the message,
1232 * any other negative returned value must be considered as an error with an appctx st0
1233 * returned value equal to PEER_SESS_ST_END.
1234 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001235static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1236 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001237{
1238 struct peer_prep_params p = {
1239 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1240 };
1241
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001242 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1243 NULL, &p.control.head[1], peers->local->id, peer->id);
1244
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001245 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1246}
1247
1248/*
1249 * Send a stick-table synchronization finished message.
1250 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1251 * Returns -1 if there was not enough room left to send the message,
1252 * any other negative returned value must be considered as an error with an appctx st0
1253 * returned value equal to PEER_SESS_ST_END.
1254 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001255static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1256 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001257{
1258 struct peer_prep_params p = {
1259 .control.head = { PEER_MSG_CLASS_CONTROL, },
1260 };
1261
Emeric Brun70de43b2020-03-16 10:51:01 +01001262 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001263 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1264
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001265 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1266 NULL, &p.control.head[1], peers->local->id, peer->id);
1267
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001268 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1269}
1270
1271/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001272 * Send a heartbeat message.
1273 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1274 * Returns -1 if there was not enough room left to send the message,
1275 * any other negative returned value must be considered as an error with an appctx st0
1276 * returned value equal to PEER_SESS_ST_END.
1277 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001278static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1279 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001280{
1281 struct peer_prep_params p = {
1282 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1283 };
1284
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001285 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1286 NULL, &p.control.head[1], peers->local->id, peer->id);
1287
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001288 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1289}
1290
1291/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001292 * Build a peer protocol error class message.
1293 * Returns the number of written bytes used to build the message if succeeded,
1294 * 0 if not.
1295 */
1296static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1297{
1298 if (size < sizeof p->error.head)
1299 return 0;
1300
1301 msg[0] = p->error.head[0];
1302 msg[1] = p->error.head[1];
1303
1304 return 2;
1305}
1306
1307/*
1308 * Send a "size limit reached" error message.
1309 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1310 * Returns -1 if there was not enough room left to send the message,
1311 * any other negative returned value must be considered as an error with an appctx st0
1312 * returned value equal to PEER_SESS_ST_END.
1313 */
1314static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1315{
1316 struct peer_prep_params p = {
1317 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1318 };
1319
1320 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1321}
1322
1323/*
1324 * Send a "peer protocol" error message.
1325 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1326 * Returns -1 if there was not enough room left to send the message,
1327 * any other negative returned value must be considered as an error with an appctx st0
1328 * returned value equal to PEER_SESS_ST_END.
1329 */
1330static inline int peer_send_error_protomsg(struct appctx *appctx)
1331{
1332 struct peer_prep_params p = {
1333 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1334 };
1335
1336 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1337}
1338
1339/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001340 * Function used to lookup for recent stick-table updates associated with
1341 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1342 */
1343static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1344{
1345 struct eb32_node *eb;
1346
1347 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1348 if (!eb) {
1349 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001350 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001351 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1352 return NULL;
1353 }
1354 }
1355
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001356 /* if distance between the last pushed and the retrieved key
1357 * is greater than the distance last_pushed and the local_update
1358 * this means we are beyond localupdate.
1359 */
1360 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001361 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1362 return NULL;
1363 }
1364
1365 return eb32_entry(eb, struct stksess, upd);
1366}
1367
1368/*
1369 * Function used to lookup for recent stick-table updates associated with
1370 * <st> shared stick-table during teach state 1 step.
1371 */
1372static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1373{
1374 struct eb32_node *eb;
1375
1376 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1377 if (!eb) {
1378 st->flags |= SHTABLE_F_TEACH_STAGE1;
1379 eb = eb32_first(&st->table->updates);
1380 if (eb)
1381 st->last_pushed = eb->key - 1;
1382 return NULL;
1383 }
1384
1385 return eb32_entry(eb, struct stksess, upd);
1386}
1387
1388/*
1389 * Function used to lookup for recent stick-table updates associated with
1390 * <st> shared stick-table during teach state 2 step.
1391 */
1392static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1393{
1394 struct eb32_node *eb;
1395
1396 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1397 if (!eb || eb->key > st->teaching_origin) {
1398 st->flags |= SHTABLE_F_TEACH_STAGE2;
1399 return NULL;
1400 }
1401
1402 return eb32_entry(eb, struct stksess, upd);
1403}
1404
1405/*
1406 * Generic function to emit update messages for <st> stick-table when a lesson must
1407 * be taught to the peer <p>.
1408 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1409 * this function, 0 if not.
1410 *
1411 * This function temporary unlock/lock <st> when it sends stick-table updates or
1412 * when decrementing its refcount in case of any error when it sends this updates.
1413 *
1414 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1415 * Returns -1 if there was not enough room left to send the message,
1416 * any other negative returned value must be considered as an error with an appcxt st0
1417 * returned value equal to PEER_SESS_ST_END.
1418 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1419 * unlocked if not already locked when entering this function.
1420 */
1421static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1422 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1423 struct shared_table *st, int locked)
1424{
1425 int ret, new_pushed, use_timed;
1426
1427 ret = 1;
1428 use_timed = 0;
1429 if (st != p->last_local_table) {
1430 ret = peer_send_switchmsg(st, appctx);
1431 if (ret <= 0)
1432 return ret;
1433
1434 p->last_local_table = st;
1435 }
1436
1437 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1438 use_timed = !(p->flags & PEER_F_DWNGRD);
1439
1440 /* We force new pushed to 1 to force identifier in update message */
1441 new_pushed = 1;
1442
1443 if (!locked)
1444 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1445
1446 while (1) {
1447 struct stksess *ts;
1448 unsigned updateid;
1449
1450 /* push local updates */
1451 ts = peer_stksess_lookup(st);
1452 if (!ts)
1453 break;
1454
1455 updateid = ts->upd.key;
1456 ts->ref_cnt++;
1457 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1458
1459 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1460 if (ret <= 0) {
1461 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1462 ts->ref_cnt--;
1463 if (!locked)
1464 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1465 return ret;
1466 }
1467
1468 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1469 ts->ref_cnt--;
1470 st->last_pushed = updateid;
1471
1472 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1473 (int)(st->last_pushed - st->table->commitupdate) > 0)
1474 st->table->commitupdate = st->last_pushed;
1475
1476 /* identifier may not needed in next update message */
1477 new_pushed = 0;
1478 }
1479
1480 out:
1481 if (!locked)
1482 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1483 return 1;
1484}
1485
1486/*
1487 * Function to emit update messages for <st> stick-table when a lesson must
1488 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1489 *
1490 * Note that <st> shared stick-table is locked when calling this function.
1491 *
1492 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1493 * Returns -1 if there was not enough room left to send the message,
1494 * any other negative returned value must be considered as an error with an appcxt st0
1495 * returned value equal to PEER_SESS_ST_END.
1496 */
1497static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1498 struct shared_table *st)
1499{
1500 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1501}
1502
1503/*
1504 * Function to emit update messages for <st> stick-table when a lesson must
1505 * be taught to the peer <p> during teach state 1 step.
1506 *
1507 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1508 * Returns -1 if there was not enough room left to send the message,
1509 * any other negative returned value must be considered as an error with an appcxt st0
1510 * returned value equal to PEER_SESS_ST_END.
1511 */
1512static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1513 struct shared_table *st)
1514{
1515 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1516}
1517
1518/*
1519 * Function to emit update messages for <st> stick-table when a lesson must
1520 * be taught to the peer <p> during teach state 1 step.
1521 *
1522 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1523 * Returns -1 if there was not enough room left to send the message,
1524 * any other negative returned value must be considered as an error with an appcxt st0
1525 * returned value equal to PEER_SESS_ST_END.
1526 */
1527static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1528 struct shared_table *st)
1529{
1530 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1531}
1532
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001533
1534/*
1535 * Function used to parse a stick-table update message after it has been received
1536 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1537 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1538 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1539 * was encountered.
1540 * <exp> must be set if the stick-table entry expires.
1541 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001542 * 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 +01001543 * update ID.
1544 * <totl> is the length of the stick-table update message computed upon receipt.
1545 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001546static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1547 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001548{
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001549 struct shared_table *st = p->remote_table;
1550 struct stksess *ts, *newts;
1551 uint32_t update;
1552 int expire;
1553 unsigned int data_type;
1554 void *data_ptr;
1555
Frédéric Lécailled8659352020-11-10 16:18:03 +01001556 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001557 /* Here we have data message */
1558 if (!st)
1559 goto ignore_msg;
1560
1561 expire = MS_TO_TICKS(st->table->expire);
1562
1563 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001564 if (msg_len < sizeof(update)) {
1565 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001566 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001567 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001568
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001569 memcpy(&update, *msg_cur, sizeof(update));
1570 *msg_cur += sizeof(update);
1571 st->last_get = htonl(update);
1572 }
1573 else {
1574 st->last_get++;
1575 }
1576
1577 if (exp) {
1578 size_t expire_sz = sizeof expire;
1579
Frédéric Lécailled8659352020-11-10 16:18:03 +01001580 if (*msg_cur + expire_sz > msg_end) {
1581 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1582 NULL, p, *msg_cur);
1583 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1584 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001585 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001586 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001587
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001588 memcpy(&expire, *msg_cur, expire_sz);
1589 *msg_cur += expire_sz;
1590 expire = ntohl(expire);
1591 }
1592
1593 newts = stksess_new(st->table, NULL);
1594 if (!newts)
1595 goto ignore_msg;
1596
1597 if (st->table->type == SMP_T_STR) {
1598 unsigned int to_read, to_store;
1599
1600 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001601 if (!*msg_cur) {
1602 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001603 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001604 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001605
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001606 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001607 if (*msg_cur + to_store > msg_end) {
1608 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1609 NULL, p, *msg_cur);
1610 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1611 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001612 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001613 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001614
1615 memcpy(newts->key.key, *msg_cur, to_store);
1616 newts->key.key[to_store] = 0;
1617 *msg_cur += to_read;
1618 }
1619 else if (st->table->type == SMP_T_SINT) {
1620 unsigned int netinteger;
1621
Frédéric Lécailled8659352020-11-10 16:18:03 +01001622 if (*msg_cur + sizeof(netinteger) > msg_end) {
1623 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1624 NULL, p, *msg_cur);
1625 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1626 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001627 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001628 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001629
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001630 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1631 netinteger = ntohl(netinteger);
1632 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1633 *msg_cur += sizeof(netinteger);
1634 }
1635 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001636 if (*msg_cur + st->table->key_size > msg_end) {
1637 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1638 NULL, p, *msg_cur);
1639 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1640 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001641 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001642 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001643
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001644 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1645 *msg_cur += st->table->key_size;
1646 }
1647
1648 /* lookup for existing entry */
1649 ts = stktable_set_entry(st->table, newts);
1650 if (ts != newts) {
1651 stksess_free(st->table, newts);
1652 newts = NULL;
1653 }
1654
1655 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1656
1657 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001658 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001659
Emeric Brun45f12b02021-07-01 18:54:05 +02001660 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001661 continue;
1662
Willy Tarreau1e82a142019-01-29 11:08:06 +01001663 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001664 if (!*msg_cur) {
1665 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001666 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001667 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001668
Willy Tarreau1e82a142019-01-29 11:08:06 +01001669 switch (stktable_data_types[data_type].std_type) {
1670 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001671 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1672 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001673 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001674 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001675
Willy Tarreau1e82a142019-01-29 11:08:06 +01001676 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001677 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1678 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001679 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001680 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001681
Willy Tarreau1e82a142019-01-29 11:08:06 +01001682 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001683 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1684 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001685 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001686 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001687
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001688 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001689 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001690
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001691 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001692 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001693 so we don't need to update the update the freq_ctr
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001694 using its internal lock */
1695
Willy Tarreau1e82a142019-01-29 11:08:06 +01001696 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001697 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001698 if (!*msg_cur) {
1699 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001700 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001701 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001702
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001703 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001704 if (!*msg_cur) {
1705 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001706 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001707 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001708
1709 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1710 if (data_ptr)
1711 stktable_data_cast(data_ptr, std_t_frqp) = data;
1712 break;
1713 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001714 case STD_T_DICT: {
1715 struct buffer *chunk;
1716 size_t data_len, value_len;
1717 unsigned int id;
1718 struct dict_entry *de;
1719 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001720 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001721
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001722 if (!decoded_int) {
1723 /* No entry. */
1724 break;
1725 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001726 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001727 if (*msg_cur + data_len > msg_end) {
1728 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1729 NULL, p, *msg_cur);
1730 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1731 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001732 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001733 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001734
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001735 /* Compute the end of the current data, <msg_end> being at the end of
1736 * the entire message.
1737 */
1738 end = *msg_cur + data_len;
1739 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001740 if (!*msg_cur || !id) {
1741 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1742 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001743 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001744 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001745
1746 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001747 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001748 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001749 if (id > dc->max_entries) {
1750 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1751 NULL, p, NULL, &id);
1752 goto malformed_unlock;
1753 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001754 /* IDs sent over the network are numbered from 1. */
1755 de = dc->rx[id - 1].de;
1756 }
1757 else {
1758 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001759 value_len = intdecode(msg_cur, end);
1760 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01001761 unlikely(value_len + 1 >= chunk->size)) {
1762 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1763 NULL, p, *msg_cur, &value_len);
1764 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1765 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001766 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001767 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001768
1769 chunk_memcpy(chunk, *msg_cur, value_len);
1770 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001771 *msg_cur += value_len;
1772
Thayne McCombs92149f92020-11-20 01:28:26 -07001773 de = dict_insert(&server_key_dict, chunk->area);
1774 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001775 dc->rx[id - 1].de = de;
1776 }
1777 if (de) {
1778 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Thayne McCombs92149f92020-11-20 01:28:26 -07001779 if (data_ptr) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001780 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001781 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07001782 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001783 }
1784 break;
1785 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001786 }
1787 }
1788 /* Force new expiration */
1789 ts->expire = tick_add(now_ms, expire);
1790
1791 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1792 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001793
1794 ignore_msg:
Emeric Brun37d96622022-11-18 14:52:54 +01001795 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
1796 return 1;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001797
1798 malformed_unlock:
1799 /* malformed message */
1800 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1801 stktable_touch_remote(st->table, ts, 1);
1802 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001803 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001804 return 0;
1805
1806 malformed_free_newts:
1807 /* malformed message */
1808 stksess_free(st->table, newts);
1809 malformed_exit:
1810 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001811 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001812 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001813}
1814
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001815/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001816 * Function used to parse a stick-table update acknowledgement message after it
1817 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1818 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1819 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1820 * was encountered.
1821 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1822 */
1823static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1824 char **msg_cur, char *msg_end)
1825{
1826 /* ack message */
1827 uint32_t table_id ;
1828 uint32_t update;
1829 struct shared_table *st;
1830
Emeric Brunb0d60be2021-03-04 10:27:10 +01001831 /* ignore ack during teaching process */
1832 if (p->flags & PEER_F_TEACH_PROCESS)
1833 return 1;
1834
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001835 table_id = intdecode(msg_cur, msg_end);
1836 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1837 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001838
1839 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
1840 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001841 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1842 return 0;
1843 }
1844
1845 memcpy(&update, *msg_cur, sizeof(update));
1846 update = ntohl(update);
1847
1848 for (st = p->tables; st; st = st->next) {
1849 if (st->local_id == table_id) {
1850 st->update = update;
1851 break;
1852 }
1853 }
1854
1855 return 1;
1856}
1857
1858/*
1859 * Function used to parse a stick-table switch message after it has been received
1860 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1861 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1862 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1863 * was encountered.
1864 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1865 */
1866static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1867 char **msg_cur, char *msg_end)
1868{
1869 struct shared_table *st;
1870 int table_id;
1871
1872 table_id = intdecode(msg_cur, msg_end);
1873 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001874 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001875 /* malformed message */
1876 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1877 return 0;
1878 }
1879
1880 p->remote_table = NULL;
1881 for (st = p->tables; st; st = st->next) {
1882 if (st->remote_id == table_id) {
1883 p->remote_table = st;
1884 break;
1885 }
1886 }
1887
1888 return 1;
1889}
1890
1891/*
1892 * Function used to parse a stick-table definition message after it has been received
1893 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1894 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1895 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1896 * was encountered.
1897 * <totl> is the length of the stick-table update message computed upon receipt.
1898 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1899 */
1900static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1901 char **msg_cur, char *msg_end, int totl)
1902{
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001903 int table_id_len;
1904 struct shared_table *st;
1905 int table_type;
1906 int table_keylen;
1907 int table_id;
1908 uint64_t table_data;
1909
1910 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001911 if (!*msg_cur) {
1912 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001913 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001914 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001915
1916 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001917 if (!*msg_cur) {
1918 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001919 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001920 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001921
1922 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001923 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1924 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001925 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001926 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001927
1928 for (st = p->tables; st; st = st->next) {
1929 /* Reset IDs */
1930 if (st->remote_id == table_id)
1931 st->remote_id = 0;
1932
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001933 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1934 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001935 p->remote_table = st;
1936 }
1937
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001938 if (!p->remote_table) {
1939 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001940 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001941 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001942
1943 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001944 if (*msg_cur >= msg_end) {
1945 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001946 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001947 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001948
1949 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001950 if (!*msg_cur) {
1951 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001952 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001953 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001954
1955 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001956 if (!*msg_cur) {
1957 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001958 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001959 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001960
1961 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001962 if (!*msg_cur) {
1963 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001964 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001965 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001966
Emeric Brun530ba382020-06-02 11:17:42 +02001967 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001968 || p->remote_table->table->key_size != table_keylen) {
1969 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001970 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001971 goto ignore_msg;
1972 }
1973
1974 p->remote_table->remote_data = table_data;
1975 p->remote_table->remote_id = table_id;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001976
1977 ignore_msg:
Emeric Brun37d96622022-11-18 14:52:54 +01001978 return 1;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001979
1980 malformed_exit:
1981 /* malformed message */
1982 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1983 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001984}
1985
1986/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01001987 * Receive a stick-table message or pre-parse any other message.
1988 * The message's header will be sent into <msg_head> which must be at least
1989 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
1990 * The first two bytes are always read, and the rest is only read if the
1991 * first bytes indicate a stick-table message. If the message is a stick-table
1992 * message, the varint is decoded and the equivalent number of bytes will be
1993 * copied into the trash at trash.area. <totl> is incremented by the number of
1994 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001995 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1996 * -1 if there was an error updating the appctx state st0 accordingly.
1997 */
1998static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1999 uint32_t *msg_len, int *totl)
2000{
2001 int reql;
2002 struct stream_interface *si = appctx->owner;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002003 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002004
2005 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
2006 if (reql <= 0) /* closed or EOL not found */
2007 goto incomplete;
2008
2009 *totl += reql;
2010
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002011 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002012 return 1;
2013
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002014 /* This is a stick-table message, let's go on */
2015
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002016 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002017 msg_head += *totl;
2018 msg_head_sz -= *totl;
2019 reql = co_data(si_oc(si)) - *totl;
2020 if (reql > msg_head_sz)
2021 reql = msg_head_sz;
2022
2023 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002024 if (reql <= 0) /* closed */
2025 goto incomplete;
2026
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002027 cur = msg_head;
2028 *msg_len = intdecode(&cur, cur + reql);
2029 if (!cur) {
2030 /* the number is truncated, did we read enough ? */
2031 if (reql < msg_head_sz)
2032 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002033
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002034 /* malformed message */
2035 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2036 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2037 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002038 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002039 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002040
2041 /* Read message content */
2042 if (*msg_len) {
2043 if (*msg_len > trash.size) {
2044 /* Status code is not success, abort */
2045 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2046 return -1;
2047 }
2048
2049 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
2050 if (reql <= 0) /* closed */
2051 goto incomplete;
2052 *totl += reql;
2053 }
2054
2055 return 1;
2056
2057 incomplete:
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002058 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
2059 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002060 appctx->st0 = PEER_SESS_ST_END;
2061 return -1;
2062 }
2063
2064 return 0;
2065}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002066
2067/*
2068 * Treat the awaited message with <msg_head> as header.*
2069 * Return 1 if succeeded, 0 if not.
2070 */
2071static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2072 char **msg_cur, char *msg_end, int msg_len, int totl)
2073{
2074 struct stream_interface *si = appctx->owner;
2075 struct stream *s = si_strm(si);
2076 struct peers *peers = strm_fe(s)->parent;
2077
2078 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2079 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2080 struct shared_table *st;
2081 /* Reset message: remote need resync */
2082
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002083 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2084 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002085 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002086 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002087 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002088 st->flags = 0;
2089 }
2090
2091 /* reset teaching flags to 0 */
2092 peer->flags &= PEER_TEACH_RESET;
2093
2094 /* flag to start to teach lesson */
2095 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002096 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002097 }
2098 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002099 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2100 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002101 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2102 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2103 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2104 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002105 if (peer->local)
2106 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2107 else
2108 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002109 }
2110 peer->confirm++;
2111 }
2112 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002113 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2114 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002115 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2116 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2117 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2118
Emeric Brunccdfbae2021-04-28 12:59:35 +02002119 if (peer->local)
2120 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2121 else
2122 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002123 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002124 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002125 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2126 }
2127 peer->confirm++;
2128 }
2129 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2130 struct shared_table *st;
2131
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002132 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2133 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002134 /* If stopping state */
2135 if (stopping) {
2136 /* Close session, push resync no more needed */
2137 peer->flags |= PEER_F_TEACH_COMPLETE;
2138 appctx->st0 = PEER_SESS_ST_END;
2139 return 0;
2140 }
2141 for (st = peer->tables; st; st = st->next) {
2142 st->update = st->last_pushed = st->teaching_origin;
2143 st->flags = 0;
2144 }
2145
2146 /* reset teaching flags to 0 */
2147 peer->flags &= PEER_TEACH_RESET;
2148 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002149 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002150 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2151 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002152 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002153 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002154 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002155 }
2156 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2157 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2158 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2159 return 0;
2160 }
2161 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2162 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2163 return 0;
2164 }
2165 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2166 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2167 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2168 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2169 int update, expire;
2170
2171 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2172 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2173 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2174 msg_cur, msg_end, msg_len, totl))
2175 return 0;
2176
2177 }
2178 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2179 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2180 return 0;
2181 }
2182 }
2183 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2184 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2185 return 0;
2186 }
2187
2188 return 1;
2189}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002190
2191
2192/*
2193 * Send any message to <peer> peer.
2194 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002195 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002196 * to a peer state change (from the peer I/O handler point of view).
2197 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002198static inline int peer_send_msgs(struct appctx *appctx,
2199 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002200{
2201 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002202
2203 /* Need to request a resync */
2204 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2205 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2206 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2207
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002208 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002209 if (repl <= 0)
2210 return repl;
2211
2212 peers->flags |= PEERS_F_RESYNC_PROCESS;
2213 }
2214
2215 /* Nothing to read, now we start to write */
2216 if (peer->tables) {
2217 struct shared_table *st;
2218 struct shared_table *last_local_table;
2219
2220 last_local_table = peer->last_local_table;
2221 if (!last_local_table)
2222 last_local_table = peer->tables;
2223 st = last_local_table->next;
2224
2225 while (1) {
2226 if (!st)
2227 st = peer->tables;
2228
2229 /* It remains some updates to ack */
2230 if (st->last_get != st->last_acked) {
2231 repl = peer_send_ackmsg(st, appctx);
2232 if (repl <= 0)
2233 return repl;
2234
2235 st->last_acked = st->last_get;
2236 }
2237
2238 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2239 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2240 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002241 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002242
2243 repl = peer_send_teach_process_msgs(appctx, peer, st);
2244 if (repl <= 0) {
2245 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2246 return repl;
2247 }
2248 }
2249 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2250 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002251 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002252 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2253 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2254 if (repl <= 0)
2255 return repl;
2256 }
2257
2258 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2259 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2260 if (repl <= 0)
2261 return repl;
2262 }
2263 }
2264
2265 if (st == last_local_table)
2266 break;
2267 st = st->next;
2268 }
2269 }
2270
2271 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002272 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002273 if (repl <= 0)
2274 return repl;
2275
2276 /* flag finished message sent */
2277 peer->flags |= PEER_F_TEACH_FINISHED;
2278 }
2279
2280 /* Confirm finished or partial messages */
2281 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002282 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002283 if (repl <= 0)
2284 return repl;
2285
2286 peer->confirm--;
2287 }
2288
2289 return 1;
2290}
2291
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002292/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002293 * Read and parse a first line of a "hello" peer protocol message.
2294 * Returns 0 if could not read a line, -1 if there was a read error or
2295 * the line is malformed, 1 if succeeded.
2296 */
2297static inline int peer_getline_version(struct appctx *appctx,
2298 unsigned int *maj_ver, unsigned int *min_ver)
2299{
2300 int reql;
2301
2302 reql = peer_getline(appctx);
2303 if (!reql)
2304 return 0;
2305
2306 if (reql < 0)
2307 return -1;
2308
2309 /* test protocol */
2310 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2311 appctx->st0 = PEER_SESS_ST_EXIT;
2312 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2313 return -1;
2314 }
2315 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2316 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2317 appctx->st0 = PEER_SESS_ST_EXIT;
2318 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2319 return -1;
2320 }
2321
2322 return 1;
2323}
2324
2325/*
2326 * Read and parse a second line of a "hello" peer protocol message.
2327 * Returns 0 if could not read a line, -1 if there was a read error or
2328 * the line is malformed, 1 if succeeded.
2329 */
2330static inline int peer_getline_host(struct appctx *appctx)
2331{
2332 int reql;
2333
2334 reql = peer_getline(appctx);
2335 if (!reql)
2336 return 0;
2337
2338 if (reql < 0)
2339 return -1;
2340
2341 /* test hostname match */
2342 if (strcmp(localpeer, trash.area) != 0) {
2343 appctx->st0 = PEER_SESS_ST_EXIT;
2344 appctx->st1 = PEER_SESS_SC_ERRHOST;
2345 return -1;
2346 }
2347
2348 return 1;
2349}
2350
2351/*
2352 * Read and parse a last line of a "hello" peer protocol message.
2353 * Returns 0 if could not read a character, -1 if there was a read error or
2354 * the line is malformed, 1 if succeeded.
2355 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2356 */
2357static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2358{
2359 char *p;
2360 int reql;
2361 struct peer *peer;
2362 struct stream_interface *si = appctx->owner;
2363 struct stream *s = si_strm(si);
2364 struct peers *peers = strm_fe(s)->parent;
2365
2366 reql = peer_getline(appctx);
2367 if (!reql)
2368 return 0;
2369
2370 if (reql < 0)
2371 return -1;
2372
2373 /* parse line "<peer name> <pid> <relative_pid>" */
2374 p = strchr(trash.area, ' ');
2375 if (!p) {
2376 appctx->st0 = PEER_SESS_ST_EXIT;
2377 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2378 return -1;
2379 }
2380 *p = 0;
2381
2382 /* lookup known peer */
2383 for (peer = peers->remote; peer; peer = peer->next) {
2384 if (strcmp(peer->id, trash.area) == 0)
2385 break;
2386 }
2387
2388 /* if unknown peer */
2389 if (!peer) {
2390 appctx->st0 = PEER_SESS_ST_EXIT;
2391 appctx->st1 = PEER_SESS_SC_ERRPEER;
2392 return -1;
2393 }
2394 *curpeer = peer;
2395
2396 return 1;
2397}
2398
2399/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002400 * Init <peer> peer after having accepted it at peer protocol level.
2401 */
2402static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2403{
2404 struct shared_table *st;
2405
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002406 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002407 /* Register status code */
2408 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002409 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002410
2411 /* Awake main task */
2412 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2413
2414 /* Init confirm counter */
2415 peer->confirm = 0;
2416
2417 /* Init cursors */
2418 for (st = peer->tables; st ; st = st->next) {
2419 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002420 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2421 /* if st->update appears to be in future it means
2422 * that the last acked value is very old and we
2423 * remain unconnected a too long time to use this
2424 * acknowlegement as a reset.
2425 * We should update the protocol to be able to
2426 * signal the remote peer that it needs a full resync.
2427 * Here a partial fix consist to set st->update at
2428 * the max past value
2429 */
2430 if ((int)(st->table->localupdate - st->update) < 0)
2431 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002432 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002433 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002434 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2435 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002436 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002437 }
2438
2439 /* reset teaching and learning flags to 0 */
2440 peer->flags &= PEER_TEACH_RESET;
2441 peer->flags &= PEER_LEARN_RESET;
2442
2443 /* if current peer is local */
2444 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002445 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002446 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2447 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2448 /* assign local peer for a lesson, consider lesson already requested */
2449 peer->flags |= PEER_F_LEARN_ASSIGN;
2450 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002451 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002452 }
2453
2454 }
2455 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2456 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2457 /* assign peer for a lesson */
2458 peer->flags |= PEER_F_LEARN_ASSIGN;
2459 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002460 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002461 }
2462}
2463
2464/*
2465 * Init <peer> peer after having connected it at peer protocol level.
2466 */
2467static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2468{
2469 struct shared_table *st;
2470
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002471 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002472 /* Init cursors */
2473 for (st = peer->tables; st ; st = st->next) {
2474 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002475 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2476 /* if st->update appears to be in future it means
2477 * that the last acked value is very old and we
2478 * remain unconnected a too long time to use this
2479 * acknowlegement as a reset.
2480 * We should update the protocol to be able to
2481 * signal the remote peer that it needs a full resync.
2482 * Here a partial fix consist to set st->update at
2483 * the max past value.
2484 */
2485 if ((int)(st->table->localupdate - st->update) < 0)
2486 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002487 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002488 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002489 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2490 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002491 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002492 }
2493
2494 /* Init confirm counter */
2495 peer->confirm = 0;
2496
2497 /* reset teaching and learning flags to 0 */
2498 peer->flags &= PEER_TEACH_RESET;
2499 peer->flags &= PEER_LEARN_RESET;
2500
2501 /* If current peer is local */
2502 if (peer->local) {
2503 /* flag to start to teach lesson */
2504 peer->flags |= PEER_F_TEACH_PROCESS;
2505 }
2506 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2507 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2508 /* If peer is remote and resync from remote is needed,
2509 and no peer currently assigned */
2510
2511 /* assign peer for a lesson */
2512 peer->flags |= PEER_F_LEARN_ASSIGN;
2513 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002514 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002515 }
2516}
2517
2518/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002519 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002520 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002521static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002522{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002523 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002524 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002525 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002526 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002527 int reql = 0;
2528 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002529 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002530 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002531
Joseph Herlant82b2f542018-11-15 12:19:14 -08002532 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002533 if (si_ic(si)->buf.size == 0) {
2534 si_rx_room_blk(si);
2535 goto out;
2536 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002537
Emeric Brun2b920a12010-09-23 18:30:22 +02002538 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002539 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002540switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002541 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002542 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002543 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002544 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002545 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002546 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002547 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002548 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002549 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002550 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2551 if (reql <= 0) {
2552 if (!reql)
2553 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002554 goto switchstate;
2555 }
2556
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002557 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002558 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002559 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002560 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002561 reql = peer_getline_host(appctx);
2562 if (reql <= 0) {
2563 if (!reql)
2564 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002565 goto switchstate;
2566 }
2567
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002568 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002569 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002570 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002571 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002572 reql = peer_getline_last(appctx, &curpeer);
2573 if (reql <= 0) {
2574 if (!reql)
2575 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002576 goto switchstate;
2577 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002578
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002579 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002580 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002581 if (curpeer->local) {
2582 /* Local connection, reply a retry */
2583 appctx->st0 = PEER_SESS_ST_EXIT;
2584 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2585 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002586 }
Emeric Brun80527f52017-06-19 17:46:37 +02002587
2588 /* we're killing a connection, we must apply a random delay before
2589 * retrying otherwise the other end will do the same and we can loop
2590 * for a while.
2591 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002592 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002593 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002594 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002595 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002596 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002597 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2598 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2599 curpeer->flags |= PEER_F_DWNGRD;
2600 }
2601 else {
2602 curpeer->flags &= ~PEER_F_DWNGRD;
2603 }
2604 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002605 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002606 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002607 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002608 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002609 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002610 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002611 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002612 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002613 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002614 if (!curpeer) {
2615 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002616 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002617 if (curpeer->appctx != appctx) {
2618 appctx->st0 = PEER_SESS_ST_END;
2619 goto switchstate;
2620 }
2621 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002622
2623 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002624 if (repl <= 0) {
2625 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002626 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002627 goto switchstate;
2628 }
2629
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002630 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002631
Emeric Brun2b920a12010-09-23 18:30:22 +02002632 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002633 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002634 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002635 goto switchstate;
2636 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002637 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002638 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002639 if (!curpeer) {
2640 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002641 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002642 if (curpeer->appctx != appctx) {
2643 appctx->st0 = PEER_SESS_ST_END;
2644 goto switchstate;
2645 }
2646 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002647
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002648 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002649 if (repl <= 0) {
2650 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002651 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002652 goto switchstate;
2653 }
2654
2655 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002656 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002657 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002658 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002659 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002660 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002661 if (!curpeer) {
2662 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002663 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002664 if (curpeer->appctx != appctx) {
2665 appctx->st0 = PEER_SESS_ST_END;
2666 goto switchstate;
2667 }
2668 }
2669
Christopher Faulet3c053242022-07-27 10:49:31 +02002670 if (si_ic(si)->flags & CF_WROTE_DATA)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002671 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002672
Frédéric Lécaillece025572019-01-21 13:38:06 +01002673 reql = peer_getline(appctx);
2674 if (!reql)
2675 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002676
Frédéric Lécaillece025572019-01-21 13:38:06 +01002677 if (reql < 0)
2678 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002679
2680 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002681 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002682 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02002683
2684 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002685 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002686
2687 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002688 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002689 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002690 }
2691 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002692 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2693 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002694 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002695 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002696 goto switchstate;
2697 }
Willy Tarreau4781b152021-04-06 13:53:36 +02002698 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002699 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002700 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002701 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002702 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002703 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002704 char *msg_cur = trash.area;
2705 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002706 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02002707 int totl = 0;
2708
Willy Tarreau2d372c22018-11-05 17:12:27 +01002709 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002710 if (!curpeer) {
2711 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002712 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002713 if (curpeer->appctx != appctx) {
2714 appctx->st0 = PEER_SESS_ST_END;
2715 goto switchstate;
2716 }
2717 }
2718
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002719 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2720 if (reql <= 0) {
2721 if (reql == -1)
2722 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002723 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002724 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002725
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002726 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002727 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002728 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002729
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002730 curpeer->flags |= PEER_F_ALIVE;
2731
Emeric Brun2b920a12010-09-23 18:30:22 +02002732 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002733 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002734 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002735 goto switchstate;
2736
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002737send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002738 if (curpeer->flags & PEER_F_HEARTBEAT) {
2739 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002740 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002741 if (repl <= 0) {
2742 if (repl == -1)
2743 goto out;
2744 goto switchstate;
2745 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002746 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002747 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002748 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002749 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002750 if (repl <= 0) {
2751 if (repl == -1)
2752 goto out;
2753 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002754 }
2755
Emeric Brun2b920a12010-09-23 18:30:22 +02002756 /* noting more to do */
2757 goto out;
2758 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002759 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002760 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002761 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002762 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002763 if (peer_send_status_errormsg(appctx) == -1)
2764 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002765 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002766 goto switchstate;
2767 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002768 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002769 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002770 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002771 if (peer_send_error_size_limitmsg(appctx) == -1)
2772 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002773 appctx->st0 = PEER_SESS_ST_END;
2774 goto switchstate;
2775 }
2776 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002777 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
2778 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002779 if (curpeer)
2780 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002781 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002782 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002783 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002784 if (peer_send_error_protomsg(appctx) == -1) {
2785 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002786 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002787 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002788 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002789 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002790 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002791 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002792 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002793 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002794 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002795 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002796 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002797 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002798 curpeer = NULL;
2799 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002800 si_shutw(si);
2801 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002802 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002803 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002804 }
2805 }
2806 }
2807out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002808 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002809
2810 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002811 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002812 return;
2813}
2814
Willy Tarreau30576452015-04-13 13:50:30 +02002815static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002816 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002817 .name = "<PEER>", /* used for logging */
2818 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002819 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002820};
Emeric Brun2b920a12010-09-23 18:30:22 +02002821
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002822
Emeric Brun2b920a12010-09-23 18:30:22 +02002823/*
2824 * Use this function to force a close of a peer session
2825 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002826static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002827{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002828 struct appctx *appctx = peer->appctx;
2829
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002830 /* Note that the peer sessions which have just been created
2831 * (->st0 == PEER_SESS_ST_CONNECT) must not
2832 * be shutdown, if not, the TCP session will never be closed
2833 * and stay in CLOSE_WAIT state after having been closed by
2834 * the remote side.
2835 */
2836 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002837 return;
2838
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002839 if (appctx->applet != &peer_applet)
2840 return;
2841
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002842 __peer_session_deinit(peer);
2843
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002844 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002845 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002846}
2847
Willy Tarreau91d96282015-03-13 15:47:26 +01002848/* Pre-configures a peers frontend to accept incoming connections */
2849void peers_setup_frontend(struct proxy *fe)
2850{
2851 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002852 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002853 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01002854 fe->maxconn = 0;
2855 fe->conn_retries = CONN_RETRIES;
Christopher Faulet2d370282022-08-29 11:32:26 +02002856 fe->timeout.connect = MS_TO_TICKS(1000);
Willy Tarreau91d96282015-03-13 15:47:26 +01002857 fe->timeout.client = MS_TO_TICKS(5000);
Christopher Faulet2d370282022-08-29 11:32:26 +02002858 fe->timeout.server = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002859 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002860 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002861 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002862 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002863}
2864
Emeric Brun2b920a12010-09-23 18:30:22 +02002865/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002866 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002867 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002868static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002869{
Willy Tarreau04b92862017-09-15 11:01:04 +02002870 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002871 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002872 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002873 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02002874
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01002875 peer->new_conn++;
Christopher Fauletd73a5cc2022-07-26 19:14:36 +02002876 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 +02002877 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002878 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002879 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002880 s = NULL;
2881
Emeric Brun1138fd02017-06-19 12:38:55 +02002882 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002883 if (!appctx)
2884 goto out_close;
2885
2886 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002887 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002888
Willy Tarreau04b92862017-09-15 11:01:04 +02002889 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002890 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002891 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002892 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002893 }
2894
Christopher Faulet26256f82020-09-14 11:40:13 +02002895 if ((s = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002896 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002897 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002898 }
2899
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002900 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002901 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002902 appctx_wakeup(appctx);
2903
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002904 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002905 s->target = peer_session_target(peer, s);
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002906 if (!sockaddr_alloc(&s->target_addr, &peer->addr, sizeof(peer->addr)))
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002907 goto out_free_strm;
Willy Tarreau02efeda2019-07-18 17:21:24 +02002908 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Willy Tarreaudbd02672017-12-06 17:39:53 +01002909 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002910
Emeric Brun2b920a12010-09-23 18:30:22 +02002911 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002912 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002913
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002914 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002915
Emeric Brunb3971ab2015-05-12 18:49:09 +02002916 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002917 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau4781b152021-04-06 13:53:36 +02002918 _HA_ATOMIC_INC(&active_peers);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002919 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002920
2921 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02002922 out_free_strm:
Willy Tarreau2b718102021-04-21 07:32:39 +02002923 LIST_DELETE(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002924 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002925 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002926 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002927 out_free_appctx:
2928 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002929 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002930 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002931}
2932
2933/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002934 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01002935 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
2936 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02002937 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002938struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002939{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002940 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002941 struct peer *ps;
2942 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002943
2944 task->expire = TICK_ETERNITY;
2945
Emeric Brunb3971ab2015-05-12 18:49:09 +02002946 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002947 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002948 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002949 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002950 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002951 return NULL;
2952 }
2953
Emeric Brun80527f52017-06-19 17:46:37 +02002954 /* Acquire lock for all peers of the section */
2955 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002956 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002957
Emeric Brun2b920a12010-09-23 18:30:22 +02002958 if (!stopping) {
2959 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002960
Emeric Brun2c4ab412021-04-21 16:06:35 +02002961 /* resync timeout set to TICK_ETERNITY means we just start
2962 * a new process and timer was not initialized.
2963 * We must arm this timer to switch to a request to a remote
2964 * node if incoming connection from old local process never
2965 * comes.
2966 */
2967 if (peers->resync_timeout == TICK_ETERNITY)
2968 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
2969
Emeric Brunb3971ab2015-05-12 18:49:09 +02002970 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2971 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2972 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002973 /* Resync from local peer needed
2974 no peer was assigned for the lesson
2975 and no old local peer found
2976 or resync timeout expire */
2977
2978 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002979 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002980 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02002981
2982 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002983 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002984 }
2985
2986 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002987 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002988 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002989 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002990 if (!ps->appctx) {
2991 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002992 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002993 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002994 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002995 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002996 tick_is_expired(ps->reconnect, now_ms))) {
2997 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002998 * or previous peer connection established with success
2999 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003000 * and reconnection timer is expired */
3001
3002 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003003 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003004 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003005 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003006 /* If previous session failed during connection
3007 * but reconnection timer is not expired */
3008
3009 /* reschedule task for reconnect */
3010 task->expire = tick_first(task->expire, ps->reconnect);
3011 }
3012 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003013 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003014 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003015 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003016 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3017 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003018 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3019 /* Resync from a remote is needed
3020 * and no peer was assigned for lesson
3021 * and current peer may be up2date */
3022
3023 /* assign peer for the lesson */
3024 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003025 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003026 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003027
Willy Tarreau9df94c22016-10-31 18:42:52 +01003028 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003029 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003030 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003031 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003032 int update_to_push = 0;
3033
Emeric Brunb3971ab2015-05-12 18:49:09 +02003034 /* Awake session if there is data to push */
3035 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003036 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003037 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003038 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003039 /* There is no need to send a heartbeat message
3040 * when some updates must be pushed. The remote
3041 * peer will consider <ps> peer as alive when it will
3042 * receive these updates.
3043 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003044 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003045 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003046 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003047 /* We are going to send updates, let's ensure we will
3048 * come back to send heartbeat messages or to reconnect.
3049 */
3050 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003051 appctx_wakeup(ps->appctx);
3052 break;
3053 }
3054 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003055 /* When there are updates to send we do not reconnect
3056 * and do not send heartbeat message either.
3057 */
3058 if (!update_to_push) {
3059 if (tick_is_expired(ps->reconnect, now_ms)) {
3060 if (ps->flags & PEER_F_ALIVE) {
3061 /* This peer was alive during a 'reconnect' period.
3062 * Flag it as not alive again for the next period.
3063 */
3064 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003065 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003066 }
3067 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003068 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003069 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003070 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003071 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003072 }
3073 }
3074 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3075 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3076 ps->flags |= PEER_F_HEARTBEAT;
3077 appctx_wakeup(ps->appctx);
3078 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003079 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003080 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003081 }
3082 /* else do nothing */
3083 } /* SUCCESSCODE */
3084 } /* !ps->peer->local */
3085 } /* for */
3086
3087 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003088 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3089 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3090 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003091 /* Resync from remote peer needed
3092 * no peer was assigned for the lesson
3093 * and resync timeout expire */
3094
3095 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003096 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003097 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003098 }
3099
Emeric Brunb3971ab2015-05-12 18:49:09 +02003100 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003101 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003102 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3103 if (!tick_is_expired(peers->resync_timeout, now_ms))
3104 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003105 }
3106 } /* !stopping */
3107 else {
3108 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003109 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003110 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003111 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003112 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003113 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003114 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003115
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003116 /* disconnect all connected peers to process a local sync
3117 * this must be done only the first time we are switching
3118 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003119 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003120 for (ps = peers->remote; ps; ps = ps->next) {
3121 /* we're killing a connection, we must apply a random delay before
3122 * retrying otherwise the other end will do the same and we can loop
3123 * for a while.
3124 */
3125 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3126 if (ps->appctx) {
3127 peer_session_forceshutdown(ps);
3128 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003129 }
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003130
3131 /* Set resync timeout for the local peer and request a immediate reconnect */
3132 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3133 peers->local->reconnect = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003134 }
3135 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003136
Emeric Brunb3971ab2015-05-12 18:49:09 +02003137 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003138 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003139 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003140 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003141 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003142 peers->flags &= ~PEERS_F_DONOTSTOP;
3143 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003144 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003145 }
3146 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003147 else if (!ps->appctx) {
Christopher Fauletda21ebc2022-08-26 18:40:46 +02003148 /* Re-arm resync timeout if necessary */
3149 if (!tick_isset(peers->resync_timeout))
3150 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3151
Willy Tarreau9df94c22016-10-31 18:42:52 +01003152 /* If there's no active peer connection */
Christopher Faulet87da1db2022-08-26 18:46:16 +02003153 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED &&
3154 !tick_is_expired(peers->resync_timeout, now_ms) &&
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003155 (ps->statuscode == 0 ||
3156 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3157 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3158 ps->statuscode == PEER_SESS_SC_TRYAGAIN)) {
Christopher Faulet87da1db2022-08-26 18:46:16 +02003159 /* The resync is finished for the local peer and
3160 * the resync timeout is not expired and
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003161 * connection never tried
3162 * or previous peer connection was successfully established
3163 * or previous tcp connect succeeded but init state incomplete
3164 * or during previous connect, peer replies a try again statuscode */
Emeric Brun2b920a12010-09-23 18:30:22 +02003165
Christopher Faulet1f8342f2022-07-26 19:19:18 +02003166 if (!tick_is_expired(ps->reconnect, now_ms)) {
3167 /* reconnection timer is not expired. reschedule task for reconnect */
3168 task->expire = tick_first(task->expire, ps->reconnect);
3169 }
3170 else {
3171 /* connect to the local peer if we must push a local sync */
3172 if (peers->flags & PEERS_F_DONOTSTOP) {
3173 peer_session_create(peers, ps);
3174 }
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003175 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003176 }
3177 else {
3178 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003179 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003180 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003181 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003182 peers->flags &= ~PEERS_F_DONOTSTOP;
3183 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003184 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003185 }
3186 }
3187 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003188 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Christopher Fauletda21ebc2022-08-26 18:40:46 +02003189 /* Reset resync timeout during a resync */
3190 peers->resync_timeout = TICK_ETERNITY;
3191
Willy Tarreau9df94c22016-10-31 18:42:52 +01003192 /* current peer connection is active and established
3193 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003194 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003195 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003196 appctx_wakeup(ps->appctx);
3197 break;
3198 }
3199 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003200 }
3201 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003202
3203 /* Release lock for all peers of the section */
3204 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003205 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003206
Emeric Brun2b920a12010-09-23 18:30:22 +02003207 /* Wakeup for re-connect */
3208 return task;
3209}
3210
Emeric Brunb3971ab2015-05-12 18:49:09 +02003211
Emeric Brun2b920a12010-09-23 18:30:22 +02003212/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003213 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003214 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003215int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003216{
Emeric Brun2b920a12010-09-23 18:30:22 +02003217 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003218
Emeric Brun2b920a12010-09-23 18:30:22 +02003219 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003220 peers->peers_fe->maxconn += 3;
3221 }
3222
Emeric Brunc60def82017-09-27 14:59:38 +02003223 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02003224 if (!peers->sync_task)
3225 return 0;
3226
Emeric Brunb3971ab2015-05-12 18:49:09 +02003227 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003228 peers->sync_task->context = (void *)peers;
3229 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3230 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003231 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003232}
3233
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003234/*
3235 * Allocate a cache a dictionary entries used upon transmission.
3236 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003237static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003238{
3239 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003240 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003241
3242 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003243 entries = calloc(max_entries, sizeof *entries);
3244 if (!d || !entries)
3245 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003246
3247 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003248 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003249 d->cached_entries = EB_ROOT_UNIQUE;
3250 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003251
3252 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003253
3254 err:
3255 free(d);
3256 free(entries);
3257 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003258}
3259
3260/*
3261 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3262 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003263 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003264 * Must be deallocated calling free_dcache().
3265 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003266static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003267{
3268 struct dcache_tx *dc_tx;
3269 struct dcache *dc;
3270 struct dcache_rx *dc_rx;
3271
3272 dc = calloc(1, sizeof *dc);
3273 dc_tx = new_dcache_tx(max_entries);
3274 dc_rx = calloc(max_entries, sizeof *dc_rx);
3275 if (!dc || !dc_tx || !dc_rx)
3276 goto err;
3277
3278 dc->tx = dc_tx;
3279 dc->rx = dc_rx;
3280 dc->max_entries = max_entries;
3281
3282 return dc;
3283
3284 err:
3285 free(dc);
3286 free(dc_tx);
3287 free(dc_rx);
3288 return NULL;
3289}
3290
3291/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003292 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3293 * entries used upon transmission.
3294 * Return the entry if found, NULL if not.
3295 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003296static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3297 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003298{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003299 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003300}
3301
3302/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003303 * Flush <dc> cache.
3304 * Always succeeds.
3305 */
3306static inline void flush_dcache(struct peer *peer)
3307{
3308 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003309 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003310
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003311 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003312 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003313 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003314 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3315 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003316 }
3317 dc->tx->prev_lookup = NULL;
3318 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003319
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003320 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003321}
3322
3323/*
3324 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3325 * with information provided by <i> dictionary cache entry (especially the value
3326 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003327 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003328 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003329static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003330{
3331 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003332 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003333
3334 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003335
3336 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3337 o = dc_tx->prev_lookup;
3338 } else {
3339 o = dcache_tx_lookup_value(dc_tx, i);
3340 if (o) {
3341 /* Save it */
3342 dc_tx->prev_lookup = o;
3343 }
3344 }
3345
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003346 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003347 /* Copy the ID. */
3348 i->id = o - dc->tx->entries;
3349 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003350 }
3351
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003352 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003353 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003354
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003355 ebpt_delete(o);
3356 o->key = i->entry.key;
3357 ebpt_insert(&dc_tx->cached_entries, o);
3358 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003359
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003360 /* Update the index for the next entry to put in cache */
3361 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003362
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003363 return o;
3364}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003365
3366/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003367 * Allocate a dictionary cache for each peer of <peers> section.
3368 * Return 1 if succeeded, 0 if not.
3369 */
3370int peers_alloc_dcache(struct peers *peers)
3371{
3372 struct peer *p;
3373
3374 for (p = peers->remote; p; p = p->next) {
3375 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3376 if (!p->dcache)
3377 return 0;
3378 }
3379
3380 return 1;
3381}
3382
3383/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003384 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003385 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003386 */
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003387int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003388{
3389 struct shared_table *st;
3390 struct peer * curpeer;
3391 int id = 0;
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003392 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003393
3394 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003395 st = calloc(1,sizeof(*st));
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003396 if (!st) {
3397 retval = 1;
3398 break;
3399 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003400 st->table = table;
3401 st->next = curpeer->tables;
3402 if (curpeer->tables)
3403 id = curpeer->tables->local_id;
3404 st->local_id = id + 1;
3405
Emeric Brun2cc201f2021-04-23 12:21:26 +02003406 /* If peer is local we inc table
3407 * refcnt to protect against flush
3408 * until this process pushed all
3409 * table content to the new one
3410 */
3411 if (curpeer->local)
3412 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003413 curpeer->tables = st;
3414 }
3415
3416 table->sync_task = peers->sync_task;
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +02003417
3418 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003419}
3420
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003421/*
3422 * Parse the "show peers" command arguments.
3423 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3424 * error message.
3425 */
3426static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3427{
3428 appctx->ctx.cfgpeers.target = NULL;
3429
Willy Tarreau49962b52021-02-12 16:56:22 +01003430 if (strcmp(args[2], "dict") == 0) {
3431 /* show the dictionaries (large dump) */
3432 appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT;
3433 args++;
3434 } else if (strcmp(args[2], "-") == 0)
3435 args++; // allows to show a section called "dict"
3436
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003437 if (*args[2]) {
3438 struct peers *p;
3439
3440 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003441 if (strcmp(p->id, args[2]) == 0) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003442 appctx->ctx.cfgpeers.target = p;
3443 break;
3444 }
3445 }
3446
Willy Tarreau9d008692019-08-09 11:21:01 +02003447 if (!p)
3448 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003449 }
3450
3451 return 0;
3452}
3453
3454/*
3455 * This function dumps the peer state information of <peers> "peers" section.
3456 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3457 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3458 */
3459static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3460{
3461 struct tm tm;
3462
3463 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003464 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 +02003465 peers,
3466 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3467 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003468 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003469 peers->resync_timeout ?
3470 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3471 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003472 TICKS_TO_MS(1000)) : "<NEVER>",
3473 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003474
3475 if (ci_putchk(si_ic(si), msg) == -1) {
3476 si_rx_room_blk(si);
3477 return 0;
3478 }
3479
3480 return 1;
3481}
3482
3483/*
3484 * This function dumps <peer> state information.
3485 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3486 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3487 */
Willy Tarreau49962b52021-02-12 16:56:22 +01003488static 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 +02003489{
3490 struct connection *conn;
3491 char pn[INET6_ADDRSTRLEN];
3492 struct stream_interface *peer_si;
3493 struct stream *peer_s;
3494 struct appctx *appctx;
3495 struct shared_table *st;
3496
3497 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003498 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003499 peer, peer->id,
3500 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003501 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003502 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003503 statuscode_str(peer->statuscode));
3504
3505 chunk_appendf(msg, " last_hdshk=%s\n",
3506 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3507 TICKS_TO_MS(1000)) : "<NEVER>");
3508
3509 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003510 peer->reconnect ?
3511 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3512 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003513 TICKS_TO_MS(1000)) : "<NEVER>");
3514
3515 chunk_appendf(msg, " heartbeat=%s",
3516 peer->heartbeat ?
3517 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3518 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3519 TICKS_TO_MS(1000)) : "<NEVER>");
3520
3521 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 +01003522 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003523 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003524
3525 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3526
3527 appctx = peer->appctx;
3528 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003529 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003530
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003531 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3532 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003533
3534 peer_si = peer->appctx->owner;
3535 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003536 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003537
3538 peer_s = si_strm(peer_si);
3539 if (!peer_s)
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 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3543
3544 conn = objt_conn(strm_orig(peer_s));
3545 if (conn)
3546 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3547
Willy Tarreau3ca14902019-07-17 14:53:15 +02003548 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 +02003549 case AF_INET:
3550 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003551 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003552 break;
3553 case AF_UNIX:
3554 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3555 break;
3556 }
3557
Willy Tarreau3ca14902019-07-17 14:53:15 +02003558 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 +02003559 case AF_INET:
3560 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003561 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003562 break;
3563 case AF_UNIX:
3564 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3565 break;
3566 }
3567
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003568 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003569 if (peer->remote_table)
3570 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3571 peer->remote_table,
3572 peer->remote_table->table->id,
3573 peer->remote_table->local_id,
3574 peer->remote_table->remote_id);
3575
3576 if (peer->last_local_table)
3577 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3578 peer->last_local_table,
3579 peer->last_local_table->table->id,
3580 peer->last_local_table->local_id,
3581 peer->last_local_table->remote_id);
3582
3583 if (peer->tables) {
3584 chunk_appendf(&trash, "\n shared tables:");
3585 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003586 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003587 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003588 struct dcache *dcache;
3589
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003590 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003591 dcache = peer->dcache;
3592
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003593 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3594 "flags=0x%x remote_data=0x%llx",
3595 st, st->local_id, st->remote_id,
3596 st->flags, (unsigned long long)st->remote_data);
3597 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3598 " teaching_origin=%u update=%u",
3599 st->last_acked, st->last_pushed, st->last_get,
3600 st->teaching_origin, st->update);
3601 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003602 " commitupdate=%u refcnt=%u",
3603 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003604 if (flags & PEERS_SHOW_F_DICT) {
3605 chunk_appendf(&trash, "\n TX dictionary cache:");
3606 count = 0;
3607 for (i = 0; i < dcache->max_entries; i++) {
3608 struct ebpt_node *node;
3609 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003610
Willy Tarreau49962b52021-02-12 16:56:22 +01003611 node = &dcache->tx->entries[i];
3612 if (!node->key)
3613 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003614
Willy Tarreau49962b52021-02-12 16:56:22 +01003615 if (!count++)
3616 chunk_appendf(&trash, "\n ");
3617 de = node->key;
3618 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3619 count &= 0x3;
3620 }
3621 chunk_appendf(&trash, "\n RX dictionary cache:");
3622 count = 0;
3623 for (i = 0; i < dcache->max_entries; i++) {
3624 if (!count++)
3625 chunk_appendf(&trash, "\n ");
3626 chunk_appendf(&trash, " %3u -> %s", i,
3627 dcache->rx[i].de ?
3628 (char *)dcache->rx[i].de->value.key : "-");
3629 count &= 0x3;
3630 }
3631 } else {
3632 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003633 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003634 }
3635 }
3636
3637 end:
3638 chunk_appendf(&trash, "\n");
3639 if (ci_putchk(si_ic(si), msg) == -1) {
3640 si_rx_room_blk(si);
3641 return 0;
3642 }
3643
3644 return 1;
3645}
3646
3647/*
3648 * This function dumps all the peers of "peers" section.
3649 * Returns 0 if the output buffer is full and needs to be called
3650 * again, non-zero if not. It proceeds in an isolated thread, so
3651 * there is no thread safety issue here.
3652 */
3653static int cli_io_handler_show_peers(struct appctx *appctx)
3654{
3655 int show_all;
3656 int ret = 0, first_peers = 1;
3657 struct stream_interface *si = appctx->owner;
3658
3659 thread_isolate();
3660
3661 show_all = !appctx->ctx.cfgpeers.target;
3662
3663 chunk_reset(&trash);
3664
3665 while (appctx->st2 != STAT_ST_FIN) {
3666 switch (appctx->st2) {
3667 case STAT_ST_INIT:
3668 if (show_all)
3669 appctx->ctx.cfgpeers.peers = cfg_peers;
3670 else
3671 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3672
3673 appctx->st2 = STAT_ST_LIST;
3674 /* fall through */
3675
3676 case STAT_ST_LIST:
3677 if (!appctx->ctx.cfgpeers.peers) {
3678 /* No more peers list. */
3679 appctx->st2 = STAT_ST_END;
3680 }
3681 else {
3682 if (!first_peers)
3683 chunk_appendf(&trash, "\n");
3684 else
3685 first_peers = 0;
3686 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3687 goto out;
3688
3689 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3690 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3691 appctx->st2 = STAT_ST_INFO;
3692 }
3693 break;
3694
3695 case STAT_ST_INFO:
3696 if (!appctx->ctx.cfgpeers.peer) {
3697 /* End of peer list */
3698 if (show_all)
3699 appctx->st2 = STAT_ST_LIST;
3700 else
3701 appctx->st2 = STAT_ST_END;
3702 }
3703 else {
Willy Tarreau49962b52021-02-12 16:56:22 +01003704 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer, appctx->ctx.cfgpeers.flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003705 goto out;
3706
3707 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3708 }
3709 break;
3710
3711 case STAT_ST_END:
3712 appctx->st2 = STAT_ST_FIN;
3713 break;
3714 }
3715 }
3716 ret = 1;
3717 out:
3718 thread_release();
3719 return ret;
3720}
3721
3722/*
3723 * CLI keywords.
3724 */
3725static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003726 { { "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 +02003727 {},
3728}};
3729
3730/* Register cli keywords */
3731INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3732