blob: 6def7f5ce8af734b803b34c2fc35387ae92e2ffb [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>
Emeric Brun2b920a12010-09-23 18:30:22 +020014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
Willy Tarreau8db34cc2021-10-06 17:53:19 +020022#include <import/eb32tree.h>
23#include <import/ebmbtree.h>
24#include <import/ebpttree.h>
25
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/applet.h>
Willy Tarreau8bd146d2022-07-19 20:17:38 +020028#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020029#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020031#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020032#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020034#include <haproxy/frontend.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020035#include <haproxy/net_helper.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020036#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020037#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020038#include <haproxy/proxy.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020039#include <haproxy/sc_strm.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020040#include <haproxy/session-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020041#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020042#include <haproxy/stats-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020043#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020044#include <haproxy/stick_table.h>
45#include <haproxy/stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/task.h>
47#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020048#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020049#include <haproxy/tools.h>
Frédéric Lécailled8659352020-11-10 16:18:03 +010050#include <haproxy/trace.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020051
Emeric Brun2b920a12010-09-23 18:30:22 +020052
53/*******************************/
54/* Current peer learning state */
55/*******************************/
56
57/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020058/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020059/******************************/
Emeric Brunccdfbae2021-04-28 12:59:35 +020060#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
61#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
62#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
63#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
64#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
65#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
66#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
67#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
68#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
69#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
70#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
71#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
72#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
73#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
74#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
75#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
76 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020077
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010078#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
79#define PEERS_RESYNC_FROMLOCAL 0x00000000
80#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
81#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020082
83/***********************************/
84/* Current shared table sync state */
85/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010086#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
87#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020088
89/******************************/
90/* Remote peer teaching state */
91/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010092#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
93#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
94#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
95#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
96#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
97#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
98#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
99#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 +0200100
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100101#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
102#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +0200103
Frédéric Lécaille54bff832019-03-26 10:25:20 +0100104#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
105#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Christopher Fauletab4b0942022-07-26 19:14:36 +0200106#define PEER_LOCAL_RECONNECT_TIMEOUT 500 /* 500ms */
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100107#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
108
Willy Tarreau8bd146d2022-07-19 20:17:38 +0200109/* default maximum of updates sent at once */
110#define PEER_DEF_MAX_UPDATES_AT_ONCE 200
111
Willy Tarreau49962b52021-02-12 16:56:22 +0100112/* flags for "show peers" */
113#define PEERS_SHOW_F_DICT 0x00000001 /* also show the contents of the dictionary */
114
Emeric Brunb3971ab2015-05-12 18:49:09 +0200115/*****************************/
116/* Sync message class */
117/*****************************/
118enum {
119 PEER_MSG_CLASS_CONTROL = 0,
120 PEER_MSG_CLASS_ERROR,
121 PEER_MSG_CLASS_STICKTABLE = 10,
122 PEER_MSG_CLASS_RESERVED = 255,
123};
124
125/*****************************/
126/* control message types */
127/*****************************/
128enum {
129 PEER_MSG_CTRL_RESYNCREQ = 0,
130 PEER_MSG_CTRL_RESYNCFINISHED,
131 PEER_MSG_CTRL_RESYNCPARTIAL,
132 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100133 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200134};
135
136/*****************************/
137/* error message types */
138/*****************************/
139enum {
140 PEER_MSG_ERR_PROTOCOL = 0,
141 PEER_MSG_ERR_SIZELIMIT,
142};
143
Emeric Brun530ba382020-06-02 11:17:42 +0200144/* network key types;
145 * network types were directly and mistakenly
146 * mapped on sample types, to keep backward
147 * compatiblitiy we keep those values but
148 * we now use a internal/network mapping
149 * to avoid further mistakes adding or
150 * modifying internals types
151 */
152enum {
153 PEER_KT_ANY = 0, /* any type */
154 PEER_KT_RESV1, /* UNUSED */
155 PEER_KT_SINT, /* signed 64bits integer type */
156 PEER_KT_RESV3, /* UNUSED */
157 PEER_KT_IPV4, /* ipv4 type */
158 PEER_KT_IPV6, /* ipv6 type */
159 PEER_KT_STR, /* char string type */
160 PEER_KT_BIN, /* buffer type */
161 PEER_KT_TYPES /* number of types, must always be last */
162};
163
164/* Map used to retrieve network type from internal type
165 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
166 */
167static int peer_net_key_type[SMP_TYPES] = {
168 [SMP_T_SINT] = PEER_KT_SINT,
169 [SMP_T_IPV4] = PEER_KT_IPV4,
170 [SMP_T_IPV6] = PEER_KT_IPV6,
171 [SMP_T_STR] = PEER_KT_STR,
172 [SMP_T_BIN] = PEER_KT_BIN,
173};
174
175/* Map used to retrieve internal type from external type
176 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
177 */
178static int peer_int_key_type[PEER_KT_TYPES] = {
179 [PEER_KT_SINT] = SMP_T_SINT,
180 [PEER_KT_IPV4] = SMP_T_IPV4,
181 [PEER_KT_IPV6] = SMP_T_IPV6,
182 [PEER_KT_STR] = SMP_T_STR,
183 [PEER_KT_BIN] = SMP_T_BIN,
184};
185
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100186/*
187 * Parameters used by functions to build peer protocol messages. */
188struct peer_prep_params {
189 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100190 struct peer *peer;
191 } hello;
192 struct {
193 unsigned int st1;
194 } error_status;
195 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100196 struct stksess *stksess;
197 struct shared_table *shared_table;
198 unsigned int updateid;
199 int use_identifier;
200 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200201 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100202 } updt;
203 struct {
204 struct shared_table *shared_table;
205 } swtch;
206 struct {
207 struct shared_table *shared_table;
208 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100209 struct {
210 unsigned char head[2];
211 } control;
212 struct {
213 unsigned char head[2];
214 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100215};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200216
217/*******************************/
218/* stick table sync mesg types */
219/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500220/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200221/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200222#define PEER_MSG_STKT_UPDATE 0x80
223#define PEER_MSG_STKT_INCUPDATE 0x81
224#define PEER_MSG_STKT_DEFINE 0x82
225#define PEER_MSG_STKT_SWITCH 0x83
226#define PEER_MSG_STKT_ACK 0x84
227#define PEER_MSG_STKT_UPDATE_TIMED 0x85
228#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200229/* All the stick-table message identifiers abova have the #7 bit set */
230#define PEER_MSG_STKT_BIT 7
231#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200232
Frédéric Lécaille39143342019-05-24 14:32:27 +0200233/* The maximum length of an encoded data length. */
234#define PEER_MSG_ENC_LENGTH_MAXLEN 5
235
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200236/* Minimum 64-bits value encoded with 2 bytes */
237#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
238/* 3 bytes */
239#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
240/* 4 bytes */
241#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
242/* 5 bytes */
243#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
244/* 6 bytes */
245#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
246/* 7 bytes */
247#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
248/* 8 bytes */
249#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
250/* 9 bytes */
251#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
252/* 10 bytes */
253#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
254
255/* #7 bit used to detect the last byte to be encoded */
256#define PEER_ENC_STOP_BIT 7
257/* The byte minimum value with #7 bit set */
258#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
259/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
260#define PEER_ENC_2BYTES_MIN_BITS 4
261
Frédéric Lécaille39143342019-05-24 14:32:27 +0200262#define PEER_MSG_HEADER_LEN 2
263
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200264#define PEER_STKT_CACHE_MAX_ENTRIES 128
265
Emeric Brun2b920a12010-09-23 18:30:22 +0200266/**********************************/
267/* Peer Session IO handler states */
268/**********************************/
269
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100270enum {
271 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
272 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
273 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
274 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100275 /* after this point, data were possibly exchanged */
276 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
277 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
278 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
279 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
280 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200281 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
282 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100283 PEER_SESS_ST_END, /* Killed session */
284};
Emeric Brun2b920a12010-09-23 18:30:22 +0200285
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100286/***************************************************/
287/* Peer Session status code - part of the protocol */
288/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200289
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100290#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
291#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200292
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100293#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200294
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100295#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200296
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100297#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
298#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
299#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
300#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200301
302#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200303#define PEER_MAJOR_VER 2
304#define PEER_MINOR_VER 1
305#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200306
Willy Tarreau6254a922019-01-29 17:45:23 +0100307static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200308struct peers *cfg_peers = NULL;
Willy Tarreau8bd146d2022-07-19 20:17:38 +0200309static int peers_max_updates_at_once = PEER_DEF_MAX_UPDATES_AT_ONCE;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200310static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200311
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200312static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
313 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200314static inline void flush_dcache(struct peer *peer);
315
Frédéric Lécailled8659352020-11-10 16:18:03 +0100316/* trace source and events */
317static void peers_trace(enum trace_level level, uint64_t mask,
318 const struct trace_source *src,
319 const struct ist where, const struct ist func,
320 const void *a1, const void *a2, const void *a3, const void *a4);
321
322static const struct trace_event peers_trace_events[] = {
323#define PEERS_EV_UPDTMSG (1 << 0)
324 { .mask = PEERS_EV_UPDTMSG, .name = "updtmsg", .desc = "update message received" },
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100325#define PEERS_EV_ACKMSG (1 << 1)
326 { .mask = PEERS_EV_ACKMSG, .name = "ackmsg", .desc = "ack message received" },
327#define PEERS_EV_SWTCMSG (1 << 2)
328 { .mask = PEERS_EV_SWTCMSG, .name = "swtcmsg", .desc = "switch message received" },
329#define PEERS_EV_DEFMSG (1 << 3)
330 { .mask = PEERS_EV_DEFMSG, .name = "defmsg", .desc = "definition message received" },
331#define PEERS_EV_CTRLMSG (1 << 4)
332 { .mask = PEERS_EV_CTRLMSG, .name = "ctrlmsg", .desc = "control message sent/received" },
333#define PEERS_EV_SESSREL (1 << 5)
334 { .mask = PEERS_EV_SESSREL, .name = "sessrl", .desc = "peer session releasing" },
335#define PEERS_EV_PROTOERR (1 << 6)
336 { .mask = PEERS_EV_PROTOERR, .name = "protoerr", .desc = "protocol error" },
Willy Tarreau9c407e42024-04-12 12:01:31 +0200337 { }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100338};
339
340static const struct name_desc peers_trace_lockon_args[4] = {
341 /* arg1 */ { /* already used by the connection */ },
342 /* arg2 */ { .name="peers", .desc="Peers protocol" },
343 /* arg3 */ { },
344 /* arg4 */ { }
345};
346
347static const struct name_desc peers_trace_decoding[] = {
348#define PEERS_VERB_CLEAN 1
349 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
350 { /* end */ }
351};
352
353
354struct trace_source trace_peers = {
355 .name = IST("peers"),
356 .desc = "Peers protocol",
357 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
358 .default_cb = peers_trace,
359 .known_events = peers_trace_events,
360 .lockon_args = peers_trace_lockon_args,
361 .decoding = peers_trace_decoding,
362 .report_events = ~0, /* report everything by default */
363};
364
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100365/* Return peer control message types as strings (only for debugging purpose). */
366static inline char *ctrl_msg_type_str(unsigned int type)
367{
368 switch (type) {
369 case PEER_MSG_CTRL_RESYNCREQ:
370 return "RESYNCREQ";
371 case PEER_MSG_CTRL_RESYNCFINISHED:
372 return "RESYNCFINISHED";
373 case PEER_MSG_CTRL_RESYNCPARTIAL:
374 return "RESYNCPARTIAL";
375 case PEER_MSG_CTRL_RESYNCCONFIRM:
376 return "RESYNCCONFIRM";
377 case PEER_MSG_CTRL_HEARTBEAT:
378 return "HEARTBEAT";
379 default:
380 return "???";
381 }
382}
383
Frédéric Lécailled8659352020-11-10 16:18:03 +0100384#define TRACE_SOURCE &trace_peers
385INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
386
387static void peers_trace(enum trace_level level, uint64_t mask,
388 const struct trace_source *src,
389 const struct ist where, const struct ist func,
390 const void *a1, const void *a2, const void *a3, const void *a4)
391{
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100392 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécailled8659352020-11-10 16:18:03 +0100393 if (a2) {
394 const struct peer *peer = a2;
395
396 chunk_appendf(&trace_buf, " peer=%s", peer->id);
397 }
398 if (a3) {
399 const char *p = a3;
400
401 chunk_appendf(&trace_buf, " @%p", p);
402 }
403 if (a4) {
404 const size_t *val = a4;
405
406 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
407 }
408 }
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100409
410 if (mask & PEERS_EV_DEFMSG) {
411 if (a2) {
412 const struct peer *peer = a2;
413
414 chunk_appendf(&trace_buf, " peer=%s", peer->id);
415 }
416 if (a3) {
417 const char *p = a3;
418
419 chunk_appendf(&trace_buf, " @%p", p);
420 }
421 if (a4) {
422 const int *val = a4;
423
424 chunk_appendf(&trace_buf, " %d", *val);
425 }
426 }
427
428 if (mask & PEERS_EV_CTRLMSG) {
429 if (a2) {
430 const unsigned char *ctrl_msg_type = a2;
431
432 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
433
434 }
435 if (a3) {
436 const char *local_peer = a3;
437
438 chunk_appendf(&trace_buf, " %s", local_peer);
439 }
440
441 if (a4) {
442 const char *remote_peer = a4;
443
444 chunk_appendf(&trace_buf, " -> %s", remote_peer);
445 }
446 }
447
448 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
449 if (a2) {
450 const struct peer *peer = a2;
451 struct peers *peers = NULL;
452
Christopher Faulet387e7972022-05-12 14:47:52 +0200453 if (peer->appctx)
454 peers = peer->peers;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100455
456 if (peers)
457 chunk_appendf(&trace_buf, " %s", peers->local->id);
Christopher Fauletd9e6b352021-11-15 09:40:57 +0100458 chunk_appendf(&trace_buf, " -> %s", peer->id);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100459 }
460
461 if (a3) {
462 const int *prev_state = a3;
463
464 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
465 }
466 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100467}
468
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200469static const char *statuscode_str(int statuscode)
470{
471 switch (statuscode) {
472 case PEER_SESS_SC_CONNECTCODE:
473 return "CONN";
474 case PEER_SESS_SC_CONNECTEDCODE:
475 return "HSHK";
476 case PEER_SESS_SC_SUCCESSCODE:
477 return "ESTA";
478 case PEER_SESS_SC_TRYAGAIN:
479 return "RETR";
480 case PEER_SESS_SC_ERRPROTO:
481 return "PROT";
482 case PEER_SESS_SC_ERRVERSION:
483 return "VERS";
484 case PEER_SESS_SC_ERRHOST:
485 return "NAME";
486 case PEER_SESS_SC_ERRPEER:
487 return "UNKN";
488 default:
489 return "NONE";
490 }
491}
492
Emeric Brun18928af2017-03-29 16:32:53 +0200493/* This function encode an uint64 to 'dynamic' length format.
494 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500495 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200496 At return, the *str is set at the next Byte after then
497 encoded integer. The function returns then length of the
498 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200499int intencode(uint64_t i, char **str) {
500 int idx = 0;
501 unsigned char *msg;
502
Emeric Brunb3971ab2015-05-12 18:49:09 +0200503 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200504 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200505 msg[0] = (unsigned char)i;
506 *str = (char *)&msg[idx+1];
507 return (idx+1);
508 }
509
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200510 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
511 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
512 while (i >= PEER_ENC_STOP_BYTE) {
513 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
514 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200515 }
516 msg[++idx] = (unsigned char)i;
517 *str = (char *)&msg[idx+1];
518 return (idx+1);
519}
520
521
Emeric Brun5ea07d92021-06-30 13:21:58 +0200522/* This function returns a decoded 64bits unsigned integer
523 * from a varint
524 *
525 * Calling:
526 * - *str must point on the first byte of the buffer to decode.
527 * - end must point on the next byte after the end of the buffer
528 * we are authorized to parse (buf + buflen)
529 *
530 * At return:
531 *
532 * On success *str will point at the byte following
533 * the fully decoded integer into the buffer. and
534 * the decoded value is returned.
535 *
536 * If end is reached before the integer was fully decoded,
537 * *str is set to NULL and the caller have to check this
538 * to know there is a decoding error. In this case
539 * the returned integer is also forced to 0
540 */
Emeric Brun18928af2017-03-29 16:32:53 +0200541uint64_t intdecode(char **str, char *end)
542{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200543 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200544 uint64_t i;
545 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200546
547 if (!*str)
548 return 0;
549
550 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200551 if (msg >= (unsigned char *)end)
552 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200553
Emeric Brun18928af2017-03-29 16:32:53 +0200554 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200555 if (i >= PEER_ENC_2BYTES_MIN) {
556 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200557 do {
558 if (msg >= (unsigned char *)end)
559 goto fail;
560 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200561 shift += PEER_ENC_STOP_BIT;
562 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200563 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100564 *str = (char *)msg;
565 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200566
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100567 fail:
568 *str = NULL;
569 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200570}
Emeric Brun2b920a12010-09-23 18:30:22 +0200571
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100572/*
573 * Build a "hello" peer protocol message.
574 * Return the number of written bytes written to build this messages if succeeded,
575 * 0 if not.
576 */
577static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
578{
579 int min_ver, ret;
580 struct peer *peer;
581
582 peer = p->hello.peer;
583 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
584 /* Prepare headers */
Willy Tarreau2645b342022-04-12 08:28:18 +0200585 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %d.%d\n%s\n%s %d %d\n",
586 (int)PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), (int)1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100587 if (ret >= size)
588 return 0;
589
590 return ret;
591}
592
593/*
594 * Build a "handshake succeeded" status message.
595 * Return the number of written bytes written to build this messages if succeeded,
596 * 0 if not.
597 */
598static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
599{
600 int ret;
601
Willy Tarreau2645b342022-04-12 08:28:18 +0200602 ret = snprintf(msg, size, "%d\n", (int)PEER_SESS_SC_SUCCESSCODE);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100603 if (ret >= size)
604 return 0;
605
606 return ret;
607}
608
609/*
610 * Build an error status message.
611 * Return the number of written bytes written to build this messages if succeeded,
612 * 0 if not.
613 */
614static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
615{
616 int ret;
617 unsigned int st1;
618
619 st1 = p->error_status.st1;
Willy Tarreaud05aa382022-11-24 15:32:20 +0100620 ret = snprintf(msg, size, "%u\n", st1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100621 if (ret >= size)
622 return 0;
623
624 return ret;
625}
626
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200627/* Set the stick-table UPDATE message type byte at <msg_type> address,
628 * depending on <use_identifier> and <use_timed> boolean parameters.
629 * Always successful.
630 */
631static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
632{
633 if (use_timed) {
634 if (use_identifier)
635 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
636 else
637 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
638 }
639 else {
640 if (use_identifier)
641 *msg_type = PEER_MSG_STKT_UPDATE;
642 else
643 *msg_type = PEER_MSG_STKT_INCUPDATE;
644 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200645}
Emeric Brun2b920a12010-09-23 18:30:22 +0200646/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200647 * This prepare the data update message on the stick session <ts>, <st> is the considered
648 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800649 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200650 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
651 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200652 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100653static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200654{
655 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200656 unsigned short datalen;
657 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200658 unsigned int data_type;
659 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100660 struct stksess *ts;
661 struct shared_table *st;
662 unsigned int updateid;
663 int use_identifier;
664 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200665 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100666
667 ts = p->updt.stksess;
668 st = p->updt.shared_table;
669 updateid = p->updt.updateid;
670 use_identifier = p->updt.use_identifier;
671 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200672 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200673
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200674 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200675
Emeric Brun2b920a12010-09-23 18:30:22 +0200676 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200677
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500678 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200679 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200680 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200681 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200682
683 /* encode update identifier if needed */
684 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200685 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200686 memcpy(cursor, &netinteger, sizeof(netinteger));
687 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200688 }
689
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200690 if (use_timed) {
691 netinteger = htonl(tick_remain(now_ms, ts->expire));
692 memcpy(cursor, &netinteger, sizeof(netinteger));
693 cursor += sizeof(netinteger);
694 }
695
Emeric Brunb3971ab2015-05-12 18:49:09 +0200696 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200697 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200698 int stlen = strlen((char *)ts->key.key);
699
Emeric Brunb3971ab2015-05-12 18:49:09 +0200700 intencode(stlen, &cursor);
701 memcpy(cursor, ts->key.key, stlen);
702 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200703 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200704 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100705 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200706 memcpy(cursor, &netinteger, sizeof(netinteger));
707 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200708 }
709 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200710 memcpy(cursor, ts->key.key, st->table->key_size);
711 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200712 }
713
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100714 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200715 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200716 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200717
Emeric Brun94900952015-06-11 18:25:54 +0200718 data_ptr = stktable_data_ptr(st->table, ts, data_type);
719 if (data_ptr) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200720 /* in case of array all elements use
721 * the same std_type and they are linearly
722 * encoded.
723 */
724 if (stktable_data_types[data_type].is_array) {
725 unsigned int idx = 0;
726
727 switch (stktable_data_types[data_type].std_type) {
728 case STD_T_SINT: {
729 int data;
730
731 do {
732 data = stktable_data_cast(data_ptr, std_t_sint);
733 intencode(data, &cursor);
734
735 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
736 } while(data_ptr);
737 break;
738 }
739 case STD_T_UINT: {
740 unsigned int data;
741
742 do {
743 data = stktable_data_cast(data_ptr, std_t_uint);
744 intencode(data, &cursor);
745
746 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
747 } while(data_ptr);
748 break;
749 }
750 case STD_T_ULL: {
751 unsigned long long data;
752
753 do {
754 data = stktable_data_cast(data_ptr, std_t_ull);
755 intencode(data, &cursor);
756
757 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
758 } while(data_ptr);
759 break;
760 }
761 case STD_T_FRQP: {
762 struct freq_ctr *frqp;
763
764 do {
765 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
766 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
767 intencode(frqp->curr_ctr, &cursor);
768 intencode(frqp->prev_ctr, &cursor);
769
770 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
771 } while(data_ptr);
772 break;
773 }
774 }
775
776 /* array elements fully encoded
777 * proceed next data_type.
778 */
779 continue;
780 }
Emeric Brun94900952015-06-11 18:25:54 +0200781 switch (stktable_data_types[data_type].std_type) {
782 case STD_T_SINT: {
783 int data;
784
785 data = stktable_data_cast(data_ptr, std_t_sint);
786 intencode(data, &cursor);
787 break;
788 }
789 case STD_T_UINT: {
790 unsigned int data;
791
792 data = stktable_data_cast(data_ptr, std_t_uint);
793 intencode(data, &cursor);
794 break;
795 }
796 case STD_T_ULL: {
797 unsigned long long data;
798
799 data = stktable_data_cast(data_ptr, std_t_ull);
800 intencode(data, &cursor);
801 break;
802 }
803 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200804 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200805
806 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
807 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
808 intencode(frqp->curr_ctr, &cursor);
809 intencode(frqp->prev_ctr, &cursor);
810 break;
811 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200812 case STD_T_DICT: {
813 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200814 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200815 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200816 char *beg, *end;
817 size_t value_len, data_len;
818 struct dcache *dc;
819
820 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100821 if (!de) {
822 /* No entry */
823 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200824 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100825 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200826
827 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200828 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200829 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200830 if (cached_de == &cde.entry) {
831 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
832 break;
833 /* Encode the length of the remaining data -> 1 */
834 intencode(1, &cursor);
835 /* Encode the cache entry ID */
836 intencode(cde.id + 1, &cursor);
837 }
838 else {
839 /* Leave enough room to encode the remaining data length. */
840 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
841 /* Encode the dictionary entry key */
842 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200843 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200844 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200845 intencode(value_len, &end);
846 /* Copy the data */
847 memcpy(end, de->value.key, value_len);
848 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200849 /* Encode the length of the data */
850 data_len = end - beg;
851 intencode(data_len, &cursor);
852 memmove(cursor, beg, data_len);
853 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200854 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200855 break;
856 }
Emeric Brun94900952015-06-11 18:25:54 +0200857 }
858 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200859 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100860 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200861
862 /* Compute datalen */
863 datalen = (cursor - datamsg);
864
865 /* prepare message header */
866 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200867 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200868 cursor = &msg[2];
869 intencode(datalen, &cursor);
870
871 /* move data after header */
872 memmove(cursor, datamsg, datalen);
873
874 /* return header size + data_len */
875 return (cursor - msg) + datalen;
876}
877
878/*
879 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800880 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200881 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
882 * check size)
883 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100884static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200885{
886 int len;
887 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200888 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200889 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200891 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100892 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200893
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100894 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200895 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200896
897 /* Encode data */
898
899 /* encode local id */
900 intencode(st->local_id, &cursor);
901
902 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100903 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200904 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100905 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200906 cursor += len;
907
908 /* encode table type */
909
Emeric Brun530ba382020-06-02 11:17:42 +0200910 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200911
912 /* encode table key size */
913 intencode(st->table->key_size, &cursor);
914
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200915 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200916 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200917 /* encode available known data types in table */
918 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
919 if (st->table->data_ofs[data_type]) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200920 /* stored data types parameters are all linearly encoded
921 * at the end of the 'table definition' message.
922 *
923 * Currently only array data_types and and data_types
924 * using freq_counter base type have parameters:
925 *
926 * - array has always at least one parameter set to the
927 * number of elements.
928 *
929 * - array of base-type freq_counters has an additional
930 * parameter set to the period used to compute those
931 * freq_counters.
932 *
933 * - simple freq counter has a parameter set to the period
934 * used to compute
935 *
936 * A set of parameter for a datatype MUST BE prefixed
937 * by the data-type id itself:
938 * This is useless because the data_types are ordered and
939 * the data_type bitfield already gives the information of
940 * stored types, but it was designed this way when the
941 * push of period parameter was added for freq counters
942 * and we don't want to break the compatibility.
943 *
944 */
945 if (stktable_data_types[data_type].is_array) {
946 /* This is an array type so we first encode
947 * the data_type itself to prefix parameters
948 */
949 intencode(data_type, &chunkq);
950
951 /* We encode the first parameter which is
952 * the number of elements of this array
953 */
954 intencode(st->table->data_nbelem[data_type], &chunkq);
955
Ilya Shipitsin01881082021-08-07 14:41:56 +0500956 /* for array of freq counters, there is an additional
Emeric Brun90a9b672021-06-22 16:09:55 +0200957 * period parameter to encode
958 */
959 if (stktable_data_types[data_type].std_type == STD_T_FRQP)
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200960 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200961 }
Emeric Brun90a9b672021-06-22 16:09:55 +0200962 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
963 /* this datatype is a simple freq counter not part
964 * of an array. We encode the data_type itself
965 * to prefix the 'period' parameter
966 */
967 intencode(data_type, &chunkq);
968 intencode(st->table->data_arg[data_type].u, &chunkq);
969 }
970 /* set the bit corresponding to stored data type */
971 data |= 1ULL << data_type;
Emeric Brun94900952015-06-11 18:25:54 +0200972 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200973 }
974 intencode(data, &cursor);
975
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200976 /* Encode stick-table entries duration. */
977 intencode(st->table->expire, &cursor);
978
979 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200980 chunk->data = chunkq - chunkp;
981 memcpy(cursor, chunk->area, chunk->data);
982 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200983 }
984
Emeric Brunb3971ab2015-05-12 18:49:09 +0200985 /* Compute datalen */
986 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200987
Emeric Brunb3971ab2015-05-12 18:49:09 +0200988 /* prepare message header */
989 msg[0] = PEER_MSG_CLASS_STICKTABLE;
990 msg[1] = PEER_MSG_STKT_DEFINE;
991 cursor = &msg[2];
992 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200993
Emeric Brunb3971ab2015-05-12 18:49:09 +0200994 /* move data after header */
995 memmove(cursor, datamsg, datalen);
996
997 /* return header size + data_len */
998 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200999}
1000
Emeric Brunb3971ab2015-05-12 18:49:09 +02001001/*
1002 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
1003 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -08001004 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +02001005 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
1006 * check size)
1007 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001008static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001009{
1010 unsigned short datalen;
1011 char *cursor, *datamsg;
1012 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001013 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001014
Frédéric Lécaille39143342019-05-24 14:32:27 +02001015 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001016
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001017 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001018 intencode(st->remote_id, &cursor);
1019 netinteger = htonl(st->last_get);
1020 memcpy(cursor, &netinteger, sizeof(netinteger));
1021 cursor += sizeof(netinteger);
1022
1023 /* Compute datalen */
1024 datalen = (cursor - datamsg);
1025
1026 /* prepare message header */
1027 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +02001028 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001029 cursor = &msg[2];
1030 intencode(datalen, &cursor);
1031
1032 /* move data after header */
1033 memmove(cursor, datamsg, datalen);
1034
1035 /* return header size + data_len */
1036 return (cursor - msg) + datalen;
1037}
Emeric Brun2b920a12010-09-23 18:30:22 +02001038
1039/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001040 * Function to deinit connected peer
1041 */
1042void __peer_session_deinit(struct peer *peer)
1043{
Christopher Faulet387e7972022-05-12 14:47:52 +02001044 struct peers *peers = peer->peers;
Maciej Zdebd01be2a2022-05-16 17:26:20 +02001045 int thr;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001046
Christopher Faulet387e7972022-05-12 14:47:52 +02001047 if (!peers || !peer->appctx)
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001048 return;
1049
Willy Tarreau29ffe262022-06-15 14:31:38 +02001050 thr = peer->appctx->t->tid;
Maciej Zdebd01be2a2022-05-16 17:26:20 +02001051 HA_ATOMIC_DEC(&peers->applet_count[thr]);
1052
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001053 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02001054 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001055
Willy Tarreau4781b152021-04-06 13:53:36 +02001056 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001057
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001058 flush_dcache(peer);
1059
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001060 /* Re-init current table pointers to force announcement on re-connect */
Christopher Fauletedd1d062023-10-16 08:14:50 +02001061 peer->remote_table = peer->last_local_table = peer->stop_local_table = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001062 peer->appctx = NULL;
1063 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1064 /* unassign current peer for learning */
1065 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
1066 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1067
Emeric Brunccdfbae2021-04-28 12:59:35 +02001068 if (peer->local)
1069 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
1070 else
1071 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001072 /* reschedule a resync */
1073 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1074 }
1075 /* reset teaching and learning flags to 0 */
1076 peer->flags &= PEER_TEACH_RESET;
1077 peer->flags &= PEER_LEARN_RESET;
1078 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1079}
1080
Christopher Faulet6712dc62022-05-12 15:36:11 +02001081static int peer_session_init(struct appctx *appctx)
1082{
1083 struct peer *peer = appctx->svcctx;
1084 struct stream *s;
1085 struct sockaddr_storage *addr = NULL;
1086
1087 if (!sockaddr_alloc(&addr, &peer->addr, sizeof(peer->addr)))
1088 goto out_error;
1089
1090 if (appctx_finalize_startup(appctx, peer->peers->peers_fe, &BUF_NULL) == -1)
1091 goto out_free_addr;
1092
1093 s = appctx_strm(appctx);
1094 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001095 applet_need_more_data(appctx);
Christopher Faulet6712dc62022-05-12 15:36:11 +02001096 appctx_wakeup(appctx);
1097
1098 /* initiate an outgoing connection */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001099 s->scb->dst = addr;
Christopher Faulet9a790f62023-03-16 14:40:03 +01001100 s->scb->flags |= (SC_FL_RCV_ONCE|SC_FL_NOLINGER);
Christopher Faulet6712dc62022-05-12 15:36:11 +02001101 s->flags = SF_ASSIGNED;
1102 s->target = peer_session_target(peer, s);
1103
1104 s->do_log = NULL;
1105 s->uniq_id = 0;
1106
Christopher Faulet6712dc62022-05-12 15:36:11 +02001107 _HA_ATOMIC_INC(&active_peers);
1108 return 0;
1109
1110 out_free_addr:
1111 sockaddr_free(&addr);
1112 out_error:
1113 return -1;
1114}
1115
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001116/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001117 * Callback to release a session with a peer
1118 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001119static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001120{
Willy Tarreau455caef2022-05-05 20:16:16 +02001121 struct peer *peer = appctx->svcctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001122
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001123 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau455caef2022-05-05 20:16:16 +02001124 /* appctx->svcctx is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001125 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +02001126 return;
1127
1128 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001129 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001130 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001131 if (peer->appctx == appctx)
1132 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02001133 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001134 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001135 }
1136}
1137
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001138/* Retrieve the major and minor versions of peers protocol
1139 * announced by a remote peer. <str> is a null-terminated
1140 * string with the following format: "<maj_ver>.<min_ver>".
1141 */
1142static int peer_get_version(const char *str,
1143 unsigned int *maj_ver, unsigned int *min_ver)
1144{
1145 unsigned int majv, minv;
1146 const char *pos, *saved;
1147 const char *end;
1148
1149 saved = pos = str;
1150 end = str + strlen(str);
1151
1152 majv = read_uint(&pos, end);
1153 if (saved == pos || *pos++ != '.')
1154 return -1;
1155
1156 saved = pos;
1157 minv = read_uint(&pos, end);
1158 if (saved == pos || pos != end)
1159 return -1;
1160
1161 *maj_ver = majv;
1162 *min_ver = minv;
1163
1164 return 0;
1165}
Emeric Brun2b920a12010-09-23 18:30:22 +02001166
1167/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001168 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1169 * '\n' character.
1170 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1171 * a line could not be read because the communication channel is closed.
1172 */
1173static inline int peer_getline(struct appctx *appctx)
1174{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001175 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001176 int n;
Frédéric Lécaillece025572019-01-21 13:38:06 +01001177
Willy Tarreauda304902022-05-27 10:44:04 +02001178 n = co_getline(sc_oc(sc), trash.area, trash.size);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001179 if (!n)
1180 return 0;
1181
1182 if (n < 0 || trash.area[n - 1] != '\n') {
1183 appctx->st0 = PEER_SESS_ST_END;
1184 return -1;
1185 }
1186
1187 if (n > 1 && (trash.area[n - 2] == '\r'))
1188 trash.area[n - 2] = 0;
1189 else
1190 trash.area[n - 1] = 0;
1191
Willy Tarreauda304902022-05-27 10:44:04 +02001192 co_skip(sc_oc(sc), n);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001193
1194 return n;
1195}
1196
1197/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001198 * Send a message after having called <peer_prepare_msg> to build it.
1199 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1200 * Returns -1 if there was not enough room left to send the message,
1201 * any other negative returned value must be considered as an error with an appcxt st0
1202 * returned value equal to PEER_SESS_ST_END.
1203 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001204static inline int peer_send_msg(struct appctx *appctx,
1205 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1206 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001207{
1208 int ret, msglen;
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001209
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001210 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001211 if (!msglen) {
1212 /* internal error: message does not fit in trash */
1213 appctx->st0 = PEER_SESS_ST_END;
1214 return 0;
1215 }
1216
1217 /* message to buffer */
Christopher Faulet7a48b722023-05-05 11:00:50 +02001218 ret = applet_putblk(appctx, trash.area, msglen);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001219 if (ret <= 0) {
Christopher Faulet7a48b722023-05-05 11:00:50 +02001220 if (ret != -1)
1221 appctx->st0 = PEER_SESS_ST_END;
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001222 }
1223
1224 return ret;
1225}
1226
1227/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001228 * Send a hello message.
1229 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1230 * Returns -1 if there was not enough room left to send the message,
1231 * any other negative returned value must be considered as an error with an appcxt st0
1232 * returned value equal to PEER_SESS_ST_END.
1233 */
1234static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1235{
1236 struct peer_prep_params p = {
1237 .hello.peer = peer,
1238 };
1239
1240 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1241}
1242
1243/*
1244 * Send a success peer handshake status message.
1245 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1246 * Returns -1 if there was not enough room left to send the message,
1247 * any other negative returned value must be considered as an error with an appcxt st0
1248 * returned value equal to PEER_SESS_ST_END.
1249 */
1250static inline int peer_send_status_successmsg(struct appctx *appctx)
1251{
1252 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1253}
1254
1255/*
1256 * Send a peer handshake status error message.
1257 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1258 * Returns -1 if there was not enough room left to send the message,
1259 * any other negative returned value must be considered as an error with an appcxt st0
1260 * returned value equal to PEER_SESS_ST_END.
1261 */
1262static inline int peer_send_status_errormsg(struct appctx *appctx)
1263{
1264 struct peer_prep_params p = {
1265 .error_status.st1 = appctx->st1,
1266 };
1267
1268 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1269}
1270
1271/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001272 * Send a stick-table switch message.
1273 * Return 0 if the message could not be built modifying the appcxt 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 appcxt st0
1276 * returned value equal to PEER_SESS_ST_END.
1277 */
1278static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1279{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001280 struct peer_prep_params p = {
1281 .swtch.shared_table = st,
1282 };
1283
1284 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001285}
1286
1287/*
1288 * Send a stick-table update acknowledgement message.
1289 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1290 * Returns -1 if there was not enough room left to send the message,
1291 * any other negative returned value must be considered as an error with an appcxt st0
1292 * returned value equal to PEER_SESS_ST_END.
1293 */
1294static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1295{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001296 struct peer_prep_params p = {
1297 .ack.shared_table = st,
1298 };
1299
1300 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001301}
1302
1303/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001304 * Send a stick-table update message.
1305 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1306 * Returns -1 if there was not enough room left to send the message,
1307 * any other negative returned value must be considered as an error with an appcxt st0
1308 * returned value equal to PEER_SESS_ST_END.
1309 */
1310static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1311 unsigned int updateid, int use_identifier, int use_timed)
1312{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001313 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001314 .updt = {
1315 .stksess = ts,
1316 .shared_table = st,
1317 .updateid = updateid,
1318 .use_identifier = use_identifier,
1319 .use_timed = use_timed,
Willy Tarreau455caef2022-05-05 20:16:16 +02001320 .peer = appctx->svcctx,
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001321 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001322 };
1323
1324 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001325}
1326
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001327/*
1328 * Build a peer protocol control class message.
1329 * Returns the number of written bytes used to build the message if succeeded,
1330 * 0 if not.
1331 */
1332static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1333{
1334 if (size < sizeof p->control.head)
1335 return 0;
1336
1337 msg[0] = p->control.head[0];
1338 msg[1] = p->control.head[1];
1339
1340 return 2;
1341}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001342
1343/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001344 * Send a stick-table synchronization request message.
1345 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1346 * Returns -1 if there was not enough room left to send the message,
1347 * any other negative returned value must be considered as an error with an appctx st0
1348 * returned value equal to PEER_SESS_ST_END.
1349 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001350static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1351 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001352{
1353 struct peer_prep_params p = {
1354 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1355 };
1356
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001357 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1358 NULL, &p.control.head[1], peers->local->id, peer->id);
1359
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001360 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1361}
1362
1363/*
1364 * Send a stick-table synchronization confirmation message.
1365 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1366 * Returns -1 if there was not enough room left to send the message,
1367 * any other negative returned value must be considered as an error with an appctx st0
1368 * returned value equal to PEER_SESS_ST_END.
1369 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001370static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1371 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001372{
1373 struct peer_prep_params p = {
1374 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1375 };
1376
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001377 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1378 NULL, &p.control.head[1], peers->local->id, peer->id);
1379
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001380 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1381}
1382
1383/*
1384 * Send a stick-table synchronization finished message.
1385 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1386 * Returns -1 if there was not enough room left to send the message,
1387 * any other negative returned value must be considered as an error with an appctx st0
1388 * returned value equal to PEER_SESS_ST_END.
1389 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001390static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1391 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001392{
1393 struct peer_prep_params p = {
1394 .control.head = { PEER_MSG_CLASS_CONTROL, },
1395 };
1396
Emeric Brun70de43b2020-03-16 10:51:01 +01001397 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001398 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1399
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001400 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1401 NULL, &p.control.head[1], peers->local->id, peer->id);
1402
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001403 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1404}
1405
1406/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001407 * Send a heartbeat message.
1408 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1409 * Returns -1 if there was not enough room left to send the message,
1410 * any other negative returned value must be considered as an error with an appctx st0
1411 * returned value equal to PEER_SESS_ST_END.
1412 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001413static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1414 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001415{
1416 struct peer_prep_params p = {
1417 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1418 };
1419
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001420 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1421 NULL, &p.control.head[1], peers->local->id, peer->id);
1422
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001423 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1424}
1425
1426/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001427 * Build a peer protocol error class message.
1428 * Returns the number of written bytes used to build the message if succeeded,
1429 * 0 if not.
1430 */
1431static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1432{
1433 if (size < sizeof p->error.head)
1434 return 0;
1435
1436 msg[0] = p->error.head[0];
1437 msg[1] = p->error.head[1];
1438
1439 return 2;
1440}
1441
1442/*
1443 * Send a "size limit reached" error message.
1444 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1445 * Returns -1 if there was not enough room left to send the message,
1446 * any other negative returned value must be considered as an error with an appctx st0
1447 * returned value equal to PEER_SESS_ST_END.
1448 */
1449static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1450{
1451 struct peer_prep_params p = {
1452 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1453 };
1454
1455 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1456}
1457
1458/*
1459 * Send a "peer protocol" error message.
1460 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1461 * Returns -1 if there was not enough room left to send the message,
1462 * any other negative returned value must be considered as an error with an appctx st0
1463 * returned value equal to PEER_SESS_ST_END.
1464 */
1465static inline int peer_send_error_protomsg(struct appctx *appctx)
1466{
1467 struct peer_prep_params p = {
1468 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1469 };
1470
1471 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1472}
1473
1474/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001475 * Function used to lookup for recent stick-table updates associated with
1476 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1477 */
1478static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1479{
1480 struct eb32_node *eb;
1481
1482 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1483 if (!eb) {
1484 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001485 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001486 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1487 return NULL;
1488 }
1489 }
1490
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001491 /* if distance between the last pushed and the retrieved key
1492 * is greater than the distance last_pushed and the local_update
1493 * this means we are beyond localupdate.
1494 */
1495 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001496 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1497 return NULL;
1498 }
1499
1500 return eb32_entry(eb, struct stksess, upd);
1501}
1502
1503/*
1504 * Function used to lookup for recent stick-table updates associated with
1505 * <st> shared stick-table during teach state 1 step.
1506 */
1507static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1508{
1509 struct eb32_node *eb;
1510
1511 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1512 if (!eb) {
1513 st->flags |= SHTABLE_F_TEACH_STAGE1;
1514 eb = eb32_first(&st->table->updates);
1515 if (eb)
1516 st->last_pushed = eb->key - 1;
1517 return NULL;
1518 }
1519
1520 return eb32_entry(eb, struct stksess, upd);
1521}
1522
1523/*
1524 * Function used to lookup for recent stick-table updates associated with
1525 * <st> shared stick-table during teach state 2 step.
1526 */
1527static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1528{
1529 struct eb32_node *eb;
1530
1531 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1532 if (!eb || eb->key > st->teaching_origin) {
1533 st->flags |= SHTABLE_F_TEACH_STAGE2;
1534 return NULL;
1535 }
1536
1537 return eb32_entry(eb, struct stksess, upd);
1538}
1539
1540/*
1541 * Generic function to emit update messages for <st> stick-table when a lesson must
1542 * be taught to the peer <p>.
1543 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1544 * this function, 0 if not.
1545 *
1546 * This function temporary unlock/lock <st> when it sends stick-table updates or
1547 * when decrementing its refcount in case of any error when it sends this updates.
1548 *
1549 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1550 * Returns -1 if there was not enough room left to send the message,
1551 * any other negative returned value must be considered as an error with an appcxt st0
1552 * returned value equal to PEER_SESS_ST_END.
1553 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1554 * unlocked if not already locked when entering this function.
1555 */
1556static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1557 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1558 struct shared_table *st, int locked)
1559{
1560 int ret, new_pushed, use_timed;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001561 int updates_sent = 0;
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001562
1563 ret = 1;
1564 use_timed = 0;
1565 if (st != p->last_local_table) {
1566 ret = peer_send_switchmsg(st, appctx);
1567 if (ret <= 0)
1568 return ret;
1569
1570 p->last_local_table = st;
1571 }
1572
1573 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1574 use_timed = !(p->flags & PEER_F_DWNGRD);
1575
1576 /* We force new pushed to 1 to force identifier in update message */
1577 new_pushed = 1;
1578
1579 if (!locked)
Willy Tarreau76642222022-10-11 12:02:50 +02001580 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001581
1582 while (1) {
1583 struct stksess *ts;
1584 unsigned updateid;
1585
1586 /* push local updates */
1587 ts = peer_stksess_lookup(st);
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001588 if (!ts) {
1589 ret = 1; // done
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001590 break;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001591 }
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001592
1593 updateid = ts->upd.key;
Frédéric Lécaille36d15652022-10-17 14:58:19 +02001594 if (p->srv->shard && ts->shard != p->srv->shard) {
1595 /* Skip this entry */
1596 st->last_pushed = updateid;
1597 new_pushed = 1;
1598 continue;
1599 }
1600
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001601 ts->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +02001602 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001603
1604 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1605 if (ret <= 0) {
Willy Tarreau76642222022-10-11 12:02:50 +02001606 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001607 ts->ref_cnt--;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001608 break;
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001609 }
1610
Willy Tarreau76642222022-10-11 12:02:50 +02001611 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001612 ts->ref_cnt--;
1613 st->last_pushed = updateid;
1614
1615 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1616 (int)(st->last_pushed - st->table->commitupdate) > 0)
1617 st->table->commitupdate = st->last_pushed;
1618
1619 /* identifier may not needed in next update message */
1620 new_pushed = 0;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001621
1622 updates_sent++;
1623 if (updates_sent >= peers_max_updates_at_once) {
Christopher Fauletaaa665f2024-04-18 09:05:11 +02001624 applet_have_more_data(appctx);
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001625 ret = -1;
1626 break;
1627 }
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001628 }
1629
1630 out:
1631 if (!locked)
Willy Tarreau76642222022-10-11 12:02:50 +02001632 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &st->table->lock);
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001633 return ret;
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001634}
1635
1636/*
1637 * Function to emit update messages for <st> stick-table when a lesson must
1638 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1639 *
1640 * Note that <st> shared stick-table is locked when calling this function.
1641 *
1642 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1643 * Returns -1 if there was not enough room left to send the message,
1644 * any other negative returned value must be considered as an error with an appcxt st0
1645 * returned value equal to PEER_SESS_ST_END.
1646 */
1647static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1648 struct shared_table *st)
1649{
1650 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1651}
1652
1653/*
1654 * Function to emit update messages for <st> stick-table when a lesson must
1655 * be taught to the peer <p> during teach state 1 step.
1656 *
1657 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1658 * Returns -1 if there was not enough room left to send the message,
1659 * any other negative returned value must be considered as an error with an appcxt st0
1660 * returned value equal to PEER_SESS_ST_END.
1661 */
1662static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1663 struct shared_table *st)
1664{
1665 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1666}
1667
1668/*
1669 * Function to emit update messages for <st> stick-table when a lesson must
1670 * be taught to the peer <p> during teach state 1 step.
1671 *
1672 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1673 * Returns -1 if there was not enough room left to send the message,
1674 * any other negative returned value must be considered as an error with an appcxt st0
1675 * returned value equal to PEER_SESS_ST_END.
1676 */
1677static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1678 struct shared_table *st)
1679{
1680 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1681}
1682
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001683
1684/*
1685 * Function used to parse a stick-table update message after it has been received
1686 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1687 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1688 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1689 * was encountered.
1690 * <exp> must be set if the stick-table entry expires.
1691 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001692 * 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 +01001693 * update ID.
1694 * <totl> is the length of the stick-table update message computed upon receipt.
1695 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001696static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1697 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001698{
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001699 struct shared_table *st = p->remote_table;
1700 struct stksess *ts, *newts;
1701 uint32_t update;
1702 int expire;
1703 unsigned int data_type;
Willy Tarreaub12be7c2022-11-29 17:43:10 +01001704 size_t keylen;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001705 void *data_ptr;
1706
Frédéric Lécailled8659352020-11-10 16:18:03 +01001707 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001708 /* Here we have data message */
1709 if (!st)
1710 goto ignore_msg;
1711
1712 expire = MS_TO_TICKS(st->table->expire);
1713
1714 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001715 if (msg_len < sizeof(update)) {
1716 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001717 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001718 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001719
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001720 memcpy(&update, *msg_cur, sizeof(update));
1721 *msg_cur += sizeof(update);
1722 st->last_get = htonl(update);
1723 }
1724 else {
1725 st->last_get++;
1726 }
1727
1728 if (exp) {
1729 size_t expire_sz = sizeof expire;
1730
Frédéric Lécailled8659352020-11-10 16:18:03 +01001731 if (*msg_cur + expire_sz > msg_end) {
1732 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1733 NULL, p, *msg_cur);
1734 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1735 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001736 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001737 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001738
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001739 memcpy(&expire, *msg_cur, expire_sz);
1740 *msg_cur += expire_sz;
1741 expire = ntohl(expire);
1742 }
1743
1744 newts = stksess_new(st->table, NULL);
1745 if (!newts)
1746 goto ignore_msg;
1747
1748 if (st->table->type == SMP_T_STR) {
1749 unsigned int to_read, to_store;
1750
1751 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001752 if (!*msg_cur) {
1753 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001754 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001755 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001756
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001757 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001758 if (*msg_cur + to_store > msg_end) {
1759 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1760 NULL, p, *msg_cur);
1761 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1762 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001763 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001764 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001765
Willy Tarreaub12be7c2022-11-29 17:43:10 +01001766 keylen = to_store;
1767 memcpy(newts->key.key, *msg_cur, keylen);
1768 newts->key.key[keylen] = 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001769 *msg_cur += to_read;
1770 }
1771 else if (st->table->type == SMP_T_SINT) {
1772 unsigned int netinteger;
1773
Frédéric Lécailled8659352020-11-10 16:18:03 +01001774 if (*msg_cur + sizeof(netinteger) > msg_end) {
1775 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1776 NULL, p, *msg_cur);
1777 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1778 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001779 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001780 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001781
Willy Tarreaub12be7c2022-11-29 17:43:10 +01001782 keylen = sizeof(netinteger);
1783 memcpy(&netinteger, *msg_cur, keylen);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001784 netinteger = ntohl(netinteger);
Willy Tarreaub12be7c2022-11-29 17:43:10 +01001785 memcpy(newts->key.key, &netinteger, keylen);
1786 *msg_cur += keylen;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001787 }
1788 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001789 if (*msg_cur + st->table->key_size > msg_end) {
1790 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1791 NULL, p, *msg_cur);
1792 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1793 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001794 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001795 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001796
Willy Tarreaub12be7c2022-11-29 17:43:10 +01001797 keylen = st->table->key_size;
1798 memcpy(newts->key.key, *msg_cur, keylen);
1799 *msg_cur += keylen;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001800 }
1801
Willy Tarreau4ede46b2022-11-29 18:01:28 +01001802 newts->shard = stktable_get_key_shard(st->table, newts->key.key, keylen);
1803
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001804 /* lookup for existing entry */
1805 ts = stktable_set_entry(st->table, newts);
1806 if (ts != newts) {
1807 stksess_free(st->table, newts);
1808 newts = NULL;
1809 }
1810
1811 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1812
1813 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001814 uint64_t decoded_int;
Emeric Brun90a9b672021-06-22 16:09:55 +02001815 unsigned int idx;
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001816 int ignore;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001817
Emeric Brun08b0f672021-07-01 18:54:05 +02001818 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001819 continue;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001820
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001821 ignore = stktable_data_types[data_type].is_local;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001822
Emeric Brun90a9b672021-06-22 16:09:55 +02001823 if (stktable_data_types[data_type].is_array) {
1824 /* in case of array all elements
1825 * use the same std_type and they
1826 * are linearly encoded.
1827 * The number of elements was provided
1828 * by table definition message
1829 */
1830 switch (stktable_data_types[data_type].std_type) {
1831 case STD_T_SINT:
1832 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1833 decoded_int = intdecode(msg_cur, msg_end);
1834 if (!*msg_cur) {
1835 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1836 goto malformed_unlock;
1837 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001838
Emeric Brun90a9b672021-06-22 16:09:55 +02001839 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001840 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001841 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
1842 }
1843 break;
1844 case STD_T_UINT:
1845 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1846 decoded_int = intdecode(msg_cur, msg_end);
1847 if (!*msg_cur) {
1848 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1849 goto malformed_unlock;
1850 }
1851
1852 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001853 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001854 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
1855 }
1856 break;
1857 case STD_T_ULL:
1858 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1859 decoded_int = intdecode(msg_cur, msg_end);
1860 if (!*msg_cur) {
1861 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1862 goto malformed_unlock;
1863 }
1864
1865 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001866 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001867 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
1868 }
1869 break;
1870 case STD_T_FRQP:
1871 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1872 struct freq_ctr data;
1873
1874 /* First bit is reserved for the freq_ctr lock
1875 * Note: here we're still protected by the stksess lock
1876 * so we don't need to update the update the freq_ctr
1877 * using its internal lock.
1878 */
1879
1880 decoded_int = intdecode(msg_cur, msg_end);
1881 if (!*msg_cur) {
1882 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1883 goto malformed_unlock;
1884 }
1885
1886 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
1887 data.curr_ctr = intdecode(msg_cur, msg_end);
1888 if (!*msg_cur) {
1889 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1890 goto malformed_unlock;
1891 }
1892
1893 data.prev_ctr = intdecode(msg_cur, msg_end);
1894 if (!*msg_cur) {
1895 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1896 goto malformed_unlock;
1897 }
1898
1899 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001900 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001901 stktable_data_cast(data_ptr, std_t_frqp) = data;
1902 }
1903 break;
1904 }
1905
1906 /* array is fully decoded
1907 * proceed next data_type.
1908 */
1909 continue;
1910 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001911 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001912 if (!*msg_cur) {
1913 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001914 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001915 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001916
Willy Tarreau1e82a142019-01-29 11:08:06 +01001917 switch (stktable_data_types[data_type].std_type) {
1918 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001919 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001920 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001921 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001922 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001923
Willy Tarreau1e82a142019-01-29 11:08:06 +01001924 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001925 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001926 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001927 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001928 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001929
Willy Tarreau1e82a142019-01-29 11:08:06 +01001930 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001931 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001932 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001933 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001934 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001935
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001936 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001937 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001938
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001939 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001940 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001941 so we don't need to update the update the freq_ctr
Emeric Brun90a9b672021-06-22 16:09:55 +02001942 using its internal lock.
1943 */
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001944
Willy Tarreau1e82a142019-01-29 11:08:06 +01001945 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001946 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001947 if (!*msg_cur) {
1948 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001949 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001950 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001951
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001952 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001953 if (!*msg_cur) {
1954 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001955 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001956 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001957
1958 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001959 if (data_ptr && !ignore)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001960 stktable_data_cast(data_ptr, std_t_frqp) = data;
1961 break;
1962 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001963 case STD_T_DICT: {
1964 struct buffer *chunk;
1965 size_t data_len, value_len;
1966 unsigned int id;
1967 struct dict_entry *de;
1968 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001969 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001970
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001971 if (!decoded_int) {
1972 /* No entry. */
1973 break;
1974 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001975 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001976 if (*msg_cur + data_len > msg_end) {
1977 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1978 NULL, p, *msg_cur);
1979 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1980 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001981 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001982 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001983
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001984 /* Compute the end of the current data, <msg_end> being at the end of
1985 * the entire message.
1986 */
1987 end = *msg_cur + data_len;
1988 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001989 if (!*msg_cur || !id) {
1990 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1991 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001992 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001993 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001994
1995 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001996 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001997 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001998 if (id > dc->max_entries) {
1999 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
2000 NULL, p, NULL, &id);
2001 goto malformed_unlock;
2002 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002003 /* IDs sent over the network are numbered from 1. */
2004 de = dc->rx[id - 1].de;
2005 }
2006 else {
2007 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02002008 value_len = intdecode(msg_cur, end);
2009 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01002010 unlikely(value_len + 1 >= chunk->size)) {
2011 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
2012 NULL, p, *msg_cur, &value_len);
2013 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
2014 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002015 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002016 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002017
2018 chunk_memcpy(chunk, *msg_cur, value_len);
2019 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02002020 *msg_cur += value_len;
2021
Thayne McCombs92149f92020-11-20 01:28:26 -07002022 de = dict_insert(&server_key_dict, chunk->area);
2023 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002024 dc->rx[id - 1].de = de;
2025 }
2026 if (de) {
2027 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01002028 if (data_ptr && !ignore) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002029 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002030 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07002031 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002032 }
2033 break;
2034 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002035 }
2036 }
2037 /* Force new expiration */
2038 ts->expire = tick_add(now_ms, expire);
2039
2040 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2041 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002042
2043 ignore_msg:
Emeric Brun0909f622022-11-18 14:52:54 +01002044 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
2045 return 1;
Willy Tarreau1e82a142019-01-29 11:08:06 +01002046
2047 malformed_unlock:
2048 /* malformed message */
2049 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2050 stktable_touch_remote(st->table, ts, 1);
2051 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002052 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002053 return 0;
2054
2055 malformed_free_newts:
2056 /* malformed message */
2057 stksess_free(st->table, newts);
2058 malformed_exit:
2059 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002060 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002061 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002062}
2063
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01002064/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002065 * Function used to parse a stick-table update acknowledgement message after it
2066 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
2067 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2068 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2069 * was encountered.
2070 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2071 */
2072static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
2073 char **msg_cur, char *msg_end)
2074{
2075 /* ack message */
2076 uint32_t table_id ;
2077 uint32_t update;
2078 struct shared_table *st;
2079
Emeric Brunb0d60be2021-03-04 10:27:10 +01002080 /* ignore ack during teaching process */
2081 if (p->flags & PEER_F_TEACH_PROCESS)
2082 return 1;
2083
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002084 table_id = intdecode(msg_cur, msg_end);
2085 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
2086 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002087
2088 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
2089 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002090 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2091 return 0;
2092 }
2093
2094 memcpy(&update, *msg_cur, sizeof(update));
2095 update = ntohl(update);
2096
2097 for (st = p->tables; st; st = st->next) {
2098 if (st->local_id == table_id) {
2099 st->update = update;
2100 break;
2101 }
2102 }
2103
2104 return 1;
2105}
2106
2107/*
2108 * Function used to parse a stick-table switch message after it has been received
2109 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2110 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2111 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2112 * was encountered.
2113 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2114 */
2115static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
2116 char **msg_cur, char *msg_end)
2117{
2118 struct shared_table *st;
2119 int table_id;
2120
2121 table_id = intdecode(msg_cur, msg_end);
2122 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002123 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002124 /* malformed message */
2125 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2126 return 0;
2127 }
2128
2129 p->remote_table = NULL;
2130 for (st = p->tables; st; st = st->next) {
2131 if (st->remote_id == table_id) {
2132 p->remote_table = st;
2133 break;
2134 }
2135 }
2136
2137 return 1;
2138}
2139
2140/*
2141 * Function used to parse a stick-table definition message after it has been received
2142 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2143 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2144 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2145 * was encountered.
2146 * <totl> is the length of the stick-table update message computed upon receipt.
2147 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2148 */
2149static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
2150 char **msg_cur, char *msg_end, int totl)
2151{
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002152 int table_id_len;
2153 struct shared_table *st;
2154 int table_type;
2155 int table_keylen;
2156 int table_id;
2157 uint64_t table_data;
2158
2159 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002160 if (!*msg_cur) {
2161 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002162 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002163 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002164
2165 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002166 if (!*msg_cur) {
2167 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002168 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002169 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002170
2171 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002172 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
2173 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002174 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002175 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002176
2177 for (st = p->tables; st; st = st->next) {
2178 /* Reset IDs */
2179 if (st->remote_id == table_id)
2180 st->remote_id = 0;
2181
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01002182 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
2183 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002184 p->remote_table = st;
2185 }
2186
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002187 if (!p->remote_table) {
2188 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002189 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002190 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002191
2192 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002193 if (*msg_cur >= msg_end) {
2194 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002195 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002196 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002197
2198 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002199 if (!*msg_cur) {
2200 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002201 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002202 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002203
2204 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002205 if (!*msg_cur) {
2206 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002207 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002208 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002209
2210 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002211 if (!*msg_cur) {
2212 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002213 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002214 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002215
Emeric Brun530ba382020-06-02 11:17:42 +02002216 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002217 || p->remote_table->table->key_size != table_keylen) {
2218 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002219 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002220 goto ignore_msg;
2221 }
2222
Ilya Shipitsin01881082021-08-07 14:41:56 +05002223 /* Check if there there is the additional expire data */
Emeric Brun90a9b672021-06-22 16:09:55 +02002224 intdecode(msg_cur, msg_end);
2225 if (*msg_cur) {
2226 uint64_t data_type;
2227 uint64_t type;
2228
2229 /* This define contains the expire data so we consider
2230 * it also contain all data_types parameters.
2231 */
2232 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2233 if (table_data & (1ULL << data_type)) {
2234 if (stktable_data_types[data_type].is_array) {
2235 /* This should be an array
2236 * so we parse the data_type prefix
2237 * because we must have parameters.
2238 */
2239 type = intdecode(msg_cur, msg_end);
2240 if (!*msg_cur) {
2241 p->remote_table = NULL;
2242 TRACE_PROTO("missing meta data for array", PEERS_EV_DEFMSG, NULL, p);
2243 goto ignore_msg;
2244 }
2245
2246 /* check if the data_type match the current from the bitfield */
2247 if (type != data_type) {
2248 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002249 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002250 goto ignore_msg;
2251 }
2252
2253 /* decode the nbelem of the array */
2254 p->remote_table->remote_data_nbelem[type] = intdecode(msg_cur, msg_end);
2255 if (!*msg_cur) {
2256 p->remote_table = NULL;
2257 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2258 goto ignore_msg;
2259 }
2260
2261 /* if it is an array of frqp, we must also have the period to decode */
2262 if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2263 intdecode(msg_cur, msg_end);
2264 if (!*msg_cur) {
2265 p->remote_table = NULL;
2266 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2267 goto ignore_msg;
2268 }
2269 }
2270 }
2271 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2272 /* This should be a std freq counter data_type
2273 * so we parse the data_type prefix
2274 * because we must have parameters.
2275 */
2276 type = intdecode(msg_cur, msg_end);
2277 if (!*msg_cur) {
2278 p->remote_table = NULL;
2279 TRACE_PROTO("missing meta data for frqp", PEERS_EV_DEFMSG, NULL, p);
2280 goto ignore_msg;
2281 }
2282
2283 /* check if the data_type match the current from the bitfield */
2284 if (type != data_type) {
2285 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002286 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002287 goto ignore_msg;
2288 }
2289
2290 /* decode the period */
2291 intdecode(msg_cur, msg_end);
2292 if (!*msg_cur) {
2293 p->remote_table = NULL;
2294 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2295 goto ignore_msg;
2296 }
2297 }
2298 }
2299 }
2300 }
2301 else {
2302 uint64_t data_type;
2303
2304 /* There is not additional data but
2305 * array size parameter is mandatory to parse array
2306 * so we consider an error if an array data_type is define
2307 * but there is no additional data.
2308 */
2309 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2310 if (table_data & (1ULL << data_type)) {
2311 if (stktable_data_types[data_type].is_array) {
2312 p->remote_table = NULL;
2313 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2314 goto ignore_msg;
2315 }
2316 }
2317 }
2318 }
2319
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002320 p->remote_table->remote_data = table_data;
2321 p->remote_table->remote_id = table_id;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002322
2323 ignore_msg:
Emeric Brun0909f622022-11-18 14:52:54 +01002324 return 1;
Willy Tarreau6f731f32019-01-29 11:11:23 +01002325
2326 malformed_exit:
2327 /* malformed message */
2328 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2329 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002330}
2331
2332/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002333 * Receive a stick-table message or pre-parse any other message.
2334 * The message's header will be sent into <msg_head> which must be at least
2335 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
2336 * The first two bytes are always read, and the rest is only read if the
2337 * first bytes indicate a stick-table message. If the message is a stick-table
2338 * message, the varint is decoded and the equivalent number of bytes will be
2339 * copied into the trash at trash.area. <totl> is incremented by the number of
2340 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002341 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
2342 * -1 if there was an error updating the appctx state st0 accordingly.
2343 */
2344static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2345 uint32_t *msg_len, int *totl)
2346{
2347 int reql;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002348 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002349 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002350
Willy Tarreauda304902022-05-27 10:44:04 +02002351 reql = co_getblk(sc_oc(sc), msg_head, 2 * sizeof(char), *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002352 if (reql <= 0) /* closed or EOL not found */
2353 goto incomplete;
2354
2355 *totl += reql;
2356
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002357 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002358 return 1;
2359
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002360 /* This is a stick-table message, let's go on */
2361
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002362 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002363 msg_head += *totl;
2364 msg_head_sz -= *totl;
Willy Tarreauda304902022-05-27 10:44:04 +02002365 reql = co_data(sc_oc(sc)) - *totl;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002366 if (reql > msg_head_sz)
2367 reql = msg_head_sz;
2368
Willy Tarreauda304902022-05-27 10:44:04 +02002369 reql = co_getblk(sc_oc(sc), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002370 if (reql <= 0) /* closed */
2371 goto incomplete;
2372
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002373 cur = msg_head;
2374 *msg_len = intdecode(&cur, cur + reql);
2375 if (!cur) {
2376 /* the number is truncated, did we read enough ? */
2377 if (reql < msg_head_sz)
2378 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002379
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002380 /* malformed message */
2381 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2382 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2383 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002384 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002385 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002386
2387 /* Read message content */
2388 if (*msg_len) {
2389 if (*msg_len > trash.size) {
2390 /* Status code is not success, abort */
2391 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2392 return -1;
2393 }
2394
Willy Tarreauda304902022-05-27 10:44:04 +02002395 reql = co_getblk(sc_oc(sc), trash.area, *msg_len, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002396 if (reql <= 0) /* closed */
2397 goto incomplete;
2398 *totl += reql;
2399 }
2400
2401 return 1;
2402
2403 incomplete:
Christopher Fauletecd32252023-12-05 09:21:38 +01002404 if (reql < 0 || (sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002405 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002406 appctx->st0 = PEER_SESS_ST_END;
2407 return -1;
2408 }
2409
2410 return 0;
2411}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002412
2413/*
2414 * Treat the awaited message with <msg_head> as header.*
2415 * Return 1 if succeeded, 0 if not.
2416 */
2417static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2418 char **msg_cur, char *msg_end, int msg_len, int totl)
2419{
Christopher Faulet387e7972022-05-12 14:47:52 +02002420 struct peers *peers = peer->peers;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002421
2422 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2423 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2424 struct shared_table *st;
2425 /* Reset message: remote need resync */
2426
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002427 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2428 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002429 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002430 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002431 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002432 st->flags = 0;
2433 }
2434
2435 /* reset teaching flags to 0 */
2436 peer->flags &= PEER_TEACH_RESET;
2437
2438 /* flag to start to teach lesson */
2439 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002440 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002441 }
2442 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002443 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2444 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002445 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brunac556082022-10-24 10:04:59 +02002446 int commit_a_finish = 1;
2447
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002448 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2449 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunac556082022-10-24 10:04:59 +02002450 if (peer->srv->shard) {
2451 struct peer *ps;
2452
2453 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
2454 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
2455 for (ps = peers->remote; ps; ps = ps->next) {
2456 if (ps->srv->shard == peer->srv->shard) {
2457 /* flag all peers from same shard
2458 * notup2date to disable request
2459 * of a resync frm them
2460 */
2461 ps->flags |= PEER_F_LEARN_NOTUP2DATE;
2462 }
2463 else if (ps->srv->shard && !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2464 /* it remains some other shards not requested
2465 * we don't commit a resync finish to request
2466 * the other shards
2467 */
2468 commit_a_finish = 0;
2469 }
2470 }
2471
2472 if (!commit_a_finish) {
2473 /* it remains some shard to request, we schedule a new request
2474 */
2475 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
2476 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2477 }
2478 }
2479
2480 if (commit_a_finish) {
2481 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
2482 if (peer->local)
2483 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2484 else
2485 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
2486 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002487 }
2488 peer->confirm++;
2489 }
2490 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002491 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2492 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002493 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2494 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2495 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2496
Emeric Brunccdfbae2021-04-28 12:59:35 +02002497 if (peer->local)
2498 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2499 else
2500 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002501 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002502 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002503 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2504 }
2505 peer->confirm++;
2506 }
2507 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2508 struct shared_table *st;
2509
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002510 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2511 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002512 /* If stopping state */
2513 if (stopping) {
2514 /* Close session, push resync no more needed */
2515 peer->flags |= PEER_F_TEACH_COMPLETE;
2516 appctx->st0 = PEER_SESS_ST_END;
2517 return 0;
2518 }
2519 for (st = peer->tables; st; st = st->next) {
2520 st->update = st->last_pushed = st->teaching_origin;
2521 st->flags = 0;
2522 }
2523
2524 /* reset teaching flags to 0 */
2525 peer->flags &= PEER_TEACH_RESET;
2526 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002527 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002528 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2529 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002530 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002531 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002532 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002533 }
2534 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2535 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2536 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2537 return 0;
2538 }
2539 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2540 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2541 return 0;
2542 }
2543 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2544 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2545 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2546 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2547 int update, expire;
2548
2549 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2550 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2551 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2552 msg_cur, msg_end, msg_len, totl))
2553 return 0;
2554
2555 }
2556 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2557 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2558 return 0;
2559 }
2560 }
2561 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2562 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2563 return 0;
2564 }
2565
2566 return 1;
2567}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002568
2569
2570/*
2571 * Send any message to <peer> peer.
2572 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002573 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002574 * to a peer state change (from the peer I/O handler point of view).
Christopher Fauletedd1d062023-10-16 08:14:50 +02002575 *
2576 * - peer->last_local_table is the last table for which we send an update
2577 * messages.
2578 *
2579 * - peer->stop_local_table is the last evaluated table. It is unset when the
2580 * teaching process starts. But we use it as a
2581 * restart point when the loop is interrupted. It is
2582 * especially useful whe the number of tables exceeds
2583 * peers_max_updates_at_once value.
2584 *
2585 * When a teaching lopp is started, the peer's last_local_table is saved in a
2586 * local variable. This variable is used as a finish point. When the crrent
2587 * table is equal to it, it means all tables were evaluated, all updates where
2588 * sent and the teaching process is finished.
2589 *
2590 * peer->stop_local_table is always NULL when the teaching process begins. It is
2591 * only reset at the end. In the mean time, it always point on a table.
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002592 */
Christopher Fauletedd1d062023-10-16 08:14:50 +02002593
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002594static inline int peer_send_msgs(struct appctx *appctx,
2595 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002596{
2597 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002598
2599 /* Need to request a resync */
2600 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2601 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2602 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2603
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002604 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002605 if (repl <= 0)
2606 return repl;
2607
2608 peers->flags |= PEERS_F_RESYNC_PROCESS;
2609 }
2610
2611 /* Nothing to read, now we start to write */
2612 if (peer->tables) {
2613 struct shared_table *st;
2614 struct shared_table *last_local_table;
Christopher Fauletedd1d062023-10-16 08:14:50 +02002615 int updates = 0;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002616
2617 last_local_table = peer->last_local_table;
2618 if (!last_local_table)
2619 last_local_table = peer->tables;
Christopher Fauletedd1d062023-10-16 08:14:50 +02002620 if (!peer->stop_local_table)
2621 peer->stop_local_table = last_local_table;
2622 st = peer->stop_local_table->next;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002623
2624 while (1) {
2625 if (!st)
2626 st = peer->tables;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002627 /* It remains some updates to ack */
2628 if (st->last_get != st->last_acked) {
2629 repl = peer_send_ackmsg(st, appctx);
2630 if (repl <= 0)
2631 return repl;
2632
2633 st->last_acked = st->last_get;
2634 }
2635
2636 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
Willy Tarreau76642222022-10-11 12:02:50 +02002637 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002638 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002639 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002640
2641 repl = peer_send_teach_process_msgs(appctx, peer, st);
2642 if (repl <= 0) {
Willy Tarreau76642222022-10-11 12:02:50 +02002643 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &st->table->lock);
Christopher Fauletedd1d062023-10-16 08:14:50 +02002644 peer->stop_local_table = peer->last_local_table;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002645 return repl;
2646 }
2647 }
Willy Tarreau76642222022-10-11 12:02:50 +02002648 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002649 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002650 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002651 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2652 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
Christopher Fauletedd1d062023-10-16 08:14:50 +02002653 if (repl <= 0) {
2654 peer->stop_local_table = peer->last_local_table;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002655 return repl;
Christopher Fauletedd1d062023-10-16 08:14:50 +02002656 }
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002657 }
2658
2659 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2660 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
Christopher Fauletedd1d062023-10-16 08:14:50 +02002661 if (repl <= 0) {
2662 peer->stop_local_table = peer->last_local_table;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002663 return repl;
Christopher Fauletedd1d062023-10-16 08:14:50 +02002664 }
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002665 }
2666 }
2667
Christopher Fauletedd1d062023-10-16 08:14:50 +02002668 if (st == last_local_table) {
2669 peer->stop_local_table = NULL;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002670 break;
Christopher Fauletedd1d062023-10-16 08:14:50 +02002671 }
Willy Tarreau8bd146d2022-07-19 20:17:38 +02002672
Christopher Fauletedd1d062023-10-16 08:14:50 +02002673 /* This one is to be sure to restart from <st->next> if we are interrupted
2674 * because of peer_send_teach_stage2_msgs or because buffer is full
2675 * when sedning an ackmsg. In both cases current <st> was evaluated and
2676 * we must restart from <st->next>
2677 */
2678 peer->stop_local_table = st;
2679
2680 updates++;
2681 if (updates >= peers_max_updates_at_once) {
Christopher Fauletaaa665f2024-04-18 09:05:11 +02002682 applet_have_more_data(appctx);
Willy Tarreau8bd146d2022-07-19 20:17:38 +02002683 return -1;
2684 }
Christopher Fauletedd1d062023-10-16 08:14:50 +02002685
2686 st = st->next;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002687 }
2688 }
2689
2690 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002691 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002692 if (repl <= 0)
2693 return repl;
2694
2695 /* flag finished message sent */
2696 peer->flags |= PEER_F_TEACH_FINISHED;
2697 }
2698
2699 /* Confirm finished or partial messages */
2700 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002701 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002702 if (repl <= 0)
2703 return repl;
2704
2705 peer->confirm--;
2706 }
2707
2708 return 1;
2709}
2710
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002711/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002712 * Read and parse a first line of a "hello" peer protocol message.
2713 * Returns 0 if could not read a line, -1 if there was a read error or
2714 * the line is malformed, 1 if succeeded.
2715 */
2716static inline int peer_getline_version(struct appctx *appctx,
2717 unsigned int *maj_ver, unsigned int *min_ver)
2718{
2719 int reql;
2720
2721 reql = peer_getline(appctx);
2722 if (!reql)
2723 return 0;
2724
2725 if (reql < 0)
2726 return -1;
2727
2728 /* test protocol */
2729 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2730 appctx->st0 = PEER_SESS_ST_EXIT;
2731 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2732 return -1;
2733 }
2734 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2735 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2736 appctx->st0 = PEER_SESS_ST_EXIT;
2737 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2738 return -1;
2739 }
2740
2741 return 1;
2742}
2743
2744/*
2745 * Read and parse a second line of a "hello" peer protocol message.
2746 * Returns 0 if could not read a line, -1 if there was a read error or
2747 * the line is malformed, 1 if succeeded.
2748 */
2749static inline int peer_getline_host(struct appctx *appctx)
2750{
2751 int reql;
2752
2753 reql = peer_getline(appctx);
2754 if (!reql)
2755 return 0;
2756
2757 if (reql < 0)
2758 return -1;
2759
2760 /* test hostname match */
2761 if (strcmp(localpeer, trash.area) != 0) {
2762 appctx->st0 = PEER_SESS_ST_EXIT;
2763 appctx->st1 = PEER_SESS_SC_ERRHOST;
2764 return -1;
2765 }
2766
2767 return 1;
2768}
2769
2770/*
2771 * Read and parse a last line of a "hello" peer protocol message.
2772 * Returns 0 if could not read a character, -1 if there was a read error or
2773 * the line is malformed, 1 if succeeded.
2774 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2775 */
2776static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2777{
2778 char *p;
2779 int reql;
2780 struct peer *peer;
Willy Tarreau0698c802022-05-11 14:09:57 +02002781 struct stream *s = appctx_strm(appctx);
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002782 struct peers *peers = strm_fe(s)->parent;
2783
2784 reql = peer_getline(appctx);
2785 if (!reql)
2786 return 0;
2787
2788 if (reql < 0)
2789 return -1;
2790
2791 /* parse line "<peer name> <pid> <relative_pid>" */
2792 p = strchr(trash.area, ' ');
2793 if (!p) {
2794 appctx->st0 = PEER_SESS_ST_EXIT;
2795 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2796 return -1;
2797 }
2798 *p = 0;
2799
2800 /* lookup known peer */
2801 for (peer = peers->remote; peer; peer = peer->next) {
2802 if (strcmp(peer->id, trash.area) == 0)
2803 break;
2804 }
2805
2806 /* if unknown peer */
2807 if (!peer) {
2808 appctx->st0 = PEER_SESS_ST_EXIT;
2809 appctx->st1 = PEER_SESS_SC_ERRPEER;
2810 return -1;
2811 }
2812 *curpeer = peer;
2813
2814 return 1;
2815}
2816
2817/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002818 * Init <peer> peer after having accepted it at peer protocol level.
2819 */
2820static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2821{
2822 struct shared_table *st;
2823
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002824 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002825 /* Register status code */
2826 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002827 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002828
2829 /* Awake main task */
2830 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2831
2832 /* Init confirm counter */
2833 peer->confirm = 0;
2834
2835 /* Init cursors */
2836 for (st = peer->tables; st ; st = st->next) {
2837 st->last_get = st->last_acked = 0;
Willy Tarreau76642222022-10-11 12:02:50 +02002838 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brund9729da2021-02-23 16:50:53 +01002839 /* if st->update appears to be in future it means
2840 * that the last acked value is very old and we
2841 * remain unconnected a too long time to use this
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05002842 * acknowledgement as a reset.
Emeric Brund9729da2021-02-23 16:50:53 +01002843 * We should update the protocol to be able to
2844 * signal the remote peer that it needs a full resync.
2845 * Here a partial fix consist to set st->update at
2846 * the max past value
2847 */
2848 if ((int)(st->table->localupdate - st->update) < 0)
2849 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002850 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002851 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002852 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2853 st->table->commitupdate = st->last_pushed;
Willy Tarreau76642222022-10-11 12:02:50 +02002854 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002855 }
2856
2857 /* reset teaching and learning flags to 0 */
2858 peer->flags &= PEER_TEACH_RESET;
2859 peer->flags &= PEER_LEARN_RESET;
2860
2861 /* if current peer is local */
2862 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002863 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002864 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2865 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2866 /* assign local peer for a lesson, consider lesson already requested */
2867 peer->flags |= PEER_F_LEARN_ASSIGN;
2868 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002869 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002870 }
2871
2872 }
2873 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2874 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2875 /* assign peer for a lesson */
2876 peer->flags |= PEER_F_LEARN_ASSIGN;
2877 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002878 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002879 }
2880}
2881
2882/*
2883 * Init <peer> peer after having connected it at peer protocol level.
2884 */
2885static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2886{
2887 struct shared_table *st;
2888
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002889 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002890 /* Init cursors */
2891 for (st = peer->tables; st ; st = st->next) {
2892 st->last_get = st->last_acked = 0;
Willy Tarreau76642222022-10-11 12:02:50 +02002893 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brund9729da2021-02-23 16:50:53 +01002894 /* if st->update appears to be in future it means
2895 * that the last acked value is very old and we
2896 * remain unconnected a too long time to use this
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05002897 * acknowledgement as a reset.
Emeric Brund9729da2021-02-23 16:50:53 +01002898 * We should update the protocol to be able to
2899 * signal the remote peer that it needs a full resync.
2900 * Here a partial fix consist to set st->update at
2901 * the max past value.
2902 */
2903 if ((int)(st->table->localupdate - st->update) < 0)
2904 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002905 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002906 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002907 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2908 st->table->commitupdate = st->last_pushed;
Willy Tarreau76642222022-10-11 12:02:50 +02002909 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002910 }
2911
2912 /* Init confirm counter */
2913 peer->confirm = 0;
2914
2915 /* reset teaching and learning flags to 0 */
2916 peer->flags &= PEER_TEACH_RESET;
2917 peer->flags &= PEER_LEARN_RESET;
2918
2919 /* If current peer is local */
2920 if (peer->local) {
2921 /* flag to start to teach lesson */
2922 peer->flags |= PEER_F_TEACH_PROCESS;
2923 }
2924 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2925 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2926 /* If peer is remote and resync from remote is needed,
2927 and no peer currently assigned */
2928
2929 /* assign peer for a lesson */
2930 peer->flags |= PEER_F_LEARN_ASSIGN;
2931 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002932 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002933 }
2934}
2935
2936/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002937 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002938 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002939static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002940{
Willy Tarreauc12b3212022-05-27 11:08:15 +02002941 struct stconn *sc = appctx_sc(appctx);
Willy Tarreauda304902022-05-27 10:44:04 +02002942 struct stream *s = __sc_strm(sc);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002943 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002944 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002945 int reql = 0;
2946 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002947 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002948 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002949
Christopher Faulet3d949012023-03-31 11:21:06 +02002950 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
2951 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
2952 goto out;
2953 }
2954
Joseph Herlant82b2f542018-11-15 12:19:14 -08002955 /* Check if the input buffer is available. */
Willy Tarreauda304902022-05-27 10:44:04 +02002956 if (sc_ib(sc)->size == 0) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02002957 sc_need_room(sc, 0);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002958 goto out;
2959 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002960
Emeric Brun2b920a12010-09-23 18:30:22 +02002961 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002962 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002963switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002964 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002965 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002966 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002967 prev_state = appctx->st0;
Willy Tarreau455caef2022-05-05 20:16:16 +02002968 appctx->svcctx = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002969 appctx->st0 = PEER_SESS_ST_GETVERSION;
Willy Tarreau79108252022-11-14 07:13:22 +01002970 __fallthrough;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002971 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002972 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002973 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2974 if (reql <= 0) {
2975 if (!reql)
2976 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002977 goto switchstate;
2978 }
2979
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002980 appctx->st0 = PEER_SESS_ST_GETHOST;
Willy Tarreau79108252022-11-14 07:13:22 +01002981 __fallthrough;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002982 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002983 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002984 reql = peer_getline_host(appctx);
2985 if (reql <= 0) {
2986 if (!reql)
2987 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002988 goto switchstate;
2989 }
2990
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002991 appctx->st0 = PEER_SESS_ST_GETPEER;
Willy Tarreau79108252022-11-14 07:13:22 +01002992 __fallthrough;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002993 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002994 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002995 reql = peer_getline_last(appctx, &curpeer);
2996 if (reql <= 0) {
2997 if (!reql)
2998 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002999 goto switchstate;
3000 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003001
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003002 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01003003 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003004 if (curpeer->local) {
3005 /* Local connection, reply a retry */
3006 appctx->st0 = PEER_SESS_ST_EXIT;
3007 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
3008 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02003009 }
Emeric Brun80527f52017-06-19 17:46:37 +02003010
3011 /* we're killing a connection, we must apply a random delay before
3012 * retrying otherwise the other end will do the same and we can loop
3013 * for a while.
3014 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01003015 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003016 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003017 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003018 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02003019 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02003020 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
3021 if (min_ver == PEER_DWNGRD_MINOR_VER) {
3022 curpeer->flags |= PEER_F_DWNGRD;
3023 }
3024 else {
3025 curpeer->flags &= ~PEER_F_DWNGRD;
3026 }
3027 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003028 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003029 curpeer->flags |= PEER_F_ALIVE;
Willy Tarreau455caef2022-05-05 20:16:16 +02003030 appctx->svcctx = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003031 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02003032 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02003033 }
Willy Tarreau79108252022-11-14 07:13:22 +01003034 __fallthrough;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003035 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003036 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003037 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02003038 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003039 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003040 if (curpeer->appctx != appctx) {
3041 appctx->st0 = PEER_SESS_ST_END;
3042 goto switchstate;
3043 }
3044 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003045
3046 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003047 if (repl <= 0) {
3048 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003049 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003050 goto switchstate;
3051 }
3052
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01003053 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003054
Emeric Brun2b920a12010-09-23 18:30:22 +02003055 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02003056 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003057 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02003058 goto switchstate;
3059 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003060 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003061 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003062 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02003063 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003064 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003065 if (curpeer->appctx != appctx) {
3066 appctx->st0 = PEER_SESS_ST_END;
3067 goto switchstate;
3068 }
3069 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003070
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003071 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02003072 if (repl <= 0) {
3073 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003074 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003075 goto switchstate;
3076 }
3077
3078 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003079 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02003080 }
Willy Tarreau79108252022-11-14 07:13:22 +01003081 __fallthrough;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003082 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003083 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003084 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02003085 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003086 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003087 if (curpeer->appctx != appctx) {
3088 appctx->st0 = PEER_SESS_ST_END;
3089 goto switchstate;
3090 }
3091 }
3092
Christopher Faulet642170a2022-07-27 10:49:31 +02003093 if (sc_ic(sc)->flags & CF_WROTE_DATA)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003094 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02003095
Frédéric Lécaillece025572019-01-21 13:38:06 +01003096 reql = peer_getline(appctx);
3097 if (!reql)
3098 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003099
Frédéric Lécaillece025572019-01-21 13:38:06 +01003100 if (reql < 0)
3101 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02003102
3103 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003104 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003105 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003106
3107 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003108 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02003109
3110 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003111 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01003112 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02003113 }
3114 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02003115 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
3116 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02003117 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003118 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02003119 goto switchstate;
3120 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003121 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003122 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02003123 }
Willy Tarreau79108252022-11-14 07:13:22 +01003124 __fallthrough;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003125 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003126 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003127 char *msg_cur = trash.area;
3128 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01003129 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02003130 int totl = 0;
3131
Willy Tarreau2d372c22018-11-05 17:12:27 +01003132 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003133 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02003134 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003135 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003136 if (curpeer->appctx != appctx) {
3137 appctx->st0 = PEER_SESS_ST_END;
3138 goto switchstate;
3139 }
3140 }
3141
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003142 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
3143 if (reql <= 0) {
3144 if (reql == -1)
3145 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003146 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003147 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01003148
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003149 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01003150 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02003151 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003152
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003153 curpeer->flags |= PEER_F_ALIVE;
3154
Emeric Brun2b920a12010-09-23 18:30:22 +02003155 /* skip consumed message */
Willy Tarreauda304902022-05-27 10:44:04 +02003156 co_skip(sc_oc(sc), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02003157 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02003158 goto switchstate;
3159
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003160send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003161 if (curpeer->flags & PEER_F_HEARTBEAT) {
3162 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003163 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003164 if (repl <= 0) {
3165 if (repl == -1)
3166 goto out;
3167 goto switchstate;
3168 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01003169 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003170 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003171 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003172 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01003173 if (repl <= 0) {
3174 if (repl == -1)
3175 goto out;
3176 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02003177 }
3178
Emeric Brun2b920a12010-09-23 18:30:22 +02003179 /* noting more to do */
3180 goto out;
3181 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003182 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01003183 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003184 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003185 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003186 if (peer_send_status_errormsg(appctx) == -1)
3187 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003188 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003189 goto switchstate;
3190 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003191 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003192 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003193 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003194 if (peer_send_error_size_limitmsg(appctx) == -1)
3195 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003196 appctx->st0 = PEER_SESS_ST_END;
3197 goto switchstate;
3198 }
3199 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003200 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
3201 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003202 if (curpeer)
3203 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003204 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003205 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003206 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003207 if (peer_send_error_protomsg(appctx) == -1) {
3208 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003209 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003210 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003211 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003212 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003213 }
Willy Tarreau79108252022-11-14 07:13:22 +01003214 __fallthrough;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003215 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003216 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003217 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003218 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003219 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003220 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003221 curpeer = NULL;
3222 }
Christopher Faulet3d949012023-03-31 11:21:06 +02003223 se_fl_set(appctx->sedesc, SE_FL_EOS|SE_FL_EOI);
3224 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
Willy Tarreau828824a2015-04-19 17:20:03 +02003225 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003226 }
3227 }
3228 }
3229out:
Christopher Faulet9a790f62023-03-16 14:40:03 +01003230 sc_opposite(sc)->flags |= SC_FL_RCV_ONCE;
Emeric Brun80527f52017-06-19 17:46:37 +02003231
3232 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003233 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02003234 return;
3235}
3236
Willy Tarreau30576452015-04-13 13:50:30 +02003237static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003238 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003239 .name = "<PEER>", /* used for logging */
3240 .fct = peer_io_handler,
Christopher Faulet6712dc62022-05-12 15:36:11 +02003241 .init = peer_session_init,
Aman Gupta9a13e842012-04-02 18:57:53 -07003242 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003243};
Emeric Brun2b920a12010-09-23 18:30:22 +02003244
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003245
Emeric Brun2b920a12010-09-23 18:30:22 +02003246/*
3247 * Use this function to force a close of a peer session
3248 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003249static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003250{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003251 struct appctx *appctx = peer->appctx;
3252
Frédéric Lécaille5df11902017-06-13 16:39:57 +02003253 /* Note that the peer sessions which have just been created
3254 * (->st0 == PEER_SESS_ST_CONNECT) must not
3255 * be shutdown, if not, the TCP session will never be closed
3256 * and stay in CLOSE_WAIT state after having been closed by
3257 * the remote side.
3258 */
3259 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003260 return;
3261
Willy Tarreau81bc3b02016-10-31 17:37:39 +01003262 if (appctx->applet != &peer_applet)
3263 return;
3264
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003265 __peer_session_deinit(peer);
3266
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003267 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01003268 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003269}
3270
Willy Tarreau91d96282015-03-13 15:47:26 +01003271/* Pre-configures a peers frontend to accept incoming connections */
3272void peers_setup_frontend(struct proxy *fe)
3273{
Willy Tarreau69530f52023-04-28 09:16:15 +02003274 fe->last_change = ns_to_sec(now_ns);
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02003275 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02003276 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01003277 fe->maxconn = 0;
3278 fe->conn_retries = CONN_RETRIES;
Christopher Faulet13db4bd2022-08-29 11:32:26 +02003279 fe->timeout.connect = MS_TO_TICKS(1000);
Willy Tarreau91d96282015-03-13 15:47:26 +01003280 fe->timeout.client = MS_TO_TICKS(5000);
Christopher Faulet13db4bd2022-08-29 11:32:26 +02003281 fe->timeout.server = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01003282 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01003283 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01003284 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
3285}
3286
Emeric Brun2b920a12010-09-23 18:30:22 +02003287/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01003288 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02003289 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003290static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003291{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003292 struct appctx *appctx;
Maciej Zdeb34e40852022-05-16 17:27:28 +02003293 unsigned int thr = 0;
3294 int idx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003295
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01003296 peer->new_conn++;
Christopher Fauletab4b0942022-07-26 19:14:36 +02003297 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 +02003298 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003299 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003300 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003301
Maciej Zdeb34e40852022-05-16 17:27:28 +02003302 for (idx = 0; idx < global.nbthread; idx++)
3303 thr = peers->applet_count[idx] < peers->applet_count[thr] ? idx : thr;
3304 appctx = appctx_new_on(&peer_applet, NULL, thr);
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003305 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003306 goto out_close;
Willy Tarreau455caef2022-05-05 20:16:16 +02003307 appctx->svcctx = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003308
Christopher Faulet6712dc62022-05-12 15:36:11 +02003309 appctx->st0 = PEER_SESS_ST_CONNECT;
3310 peer->appctx = appctx;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003311
Maciej Zdebd01be2a2022-05-16 17:26:20 +02003312 HA_ATOMIC_INC(&peers->applet_count[thr]);
Maciej Zdeb34e40852022-05-16 17:27:28 +02003313 appctx_wakeup(appctx);
Willy Tarreau9df94c22016-10-31 18:42:52 +01003314 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003315
Emeric Brun2b920a12010-09-23 18:30:22 +02003316 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01003317 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003318}
3319
3320/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003321 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01003322 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
3323 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02003324 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003325struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02003326{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003327 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003328 struct peer *ps;
3329 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02003330
3331 task->expire = TICK_ETERNITY;
3332
Emeric Brun80527f52017-06-19 17:46:37 +02003333 /* Acquire lock for all peers of the section */
3334 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003335 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003336
Emeric Brun2b920a12010-09-23 18:30:22 +02003337 if (!stopping) {
3338 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02003339
Emeric Brun2c4ab412021-04-21 16:06:35 +02003340 /* resync timeout set to TICK_ETERNITY means we just start
3341 * a new process and timer was not initialized.
3342 * We must arm this timer to switch to a request to a remote
3343 * node if incoming connection from old local process never
3344 * comes.
3345 */
3346 if (peers->resync_timeout == TICK_ETERNITY)
3347 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3348
Emeric Brunb3971ab2015-05-12 18:49:09 +02003349 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
3350 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
3351 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003352 /* Resync from local peer needed
3353 no peer was assigned for the lesson
3354 and no old local peer found
3355 or resync timeout expire */
3356
3357 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003358 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003359 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003360
3361 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003362 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02003363 }
3364
3365 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003366 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003367 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003368 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003369 if (!ps->appctx) {
3370 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003371 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003372 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003373 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003374 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003375 tick_is_expired(ps->reconnect, now_ms))) {
3376 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003377 * or previous peer connection established with success
3378 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003379 * and reconnection timer is expired */
3380
3381 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003382 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003383 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003384 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003385 /* If previous session failed during connection
3386 * but reconnection timer is not expired */
3387
3388 /* reschedule task for reconnect */
3389 task->expire = tick_first(task->expire, ps->reconnect);
3390 }
3391 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003392 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003393 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003394 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003395 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3396 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003397 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3398 /* Resync from a remote is needed
3399 * and no peer was assigned for lesson
3400 * and current peer may be up2date */
3401
3402 /* assign peer for the lesson */
3403 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003404 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003405 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003406
Willy Tarreau9df94c22016-10-31 18:42:52 +01003407 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003408 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003409 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003410 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003411 int update_to_push = 0;
3412
Emeric Brunb3971ab2015-05-12 18:49:09 +02003413 /* Awake session if there is data to push */
3414 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003415 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003416 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003417 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003418 /* There is no need to send a heartbeat message
3419 * when some updates must be pushed. The remote
3420 * peer will consider <ps> peer as alive when it will
3421 * receive these updates.
3422 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003423 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003424 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003425 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Christopher Fauletd4f7e082023-10-16 07:48:26 +02003426 /* Refresh reconnect if necessary */
3427 if (tick_is_expired(ps->reconnect, now_ms))
3428 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003429 /* We are going to send updates, let's ensure we will
3430 * come back to send heartbeat messages or to reconnect.
3431 */
3432 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003433 appctx_wakeup(ps->appctx);
3434 break;
3435 }
3436 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003437 /* When there are updates to send we do not reconnect
3438 * and do not send heartbeat message either.
3439 */
3440 if (!update_to_push) {
3441 if (tick_is_expired(ps->reconnect, now_ms)) {
3442 if (ps->flags & PEER_F_ALIVE) {
3443 /* This peer was alive during a 'reconnect' period.
3444 * Flag it as not alive again for the next period.
3445 */
3446 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003447 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003448 }
3449 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003450 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003451 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003452 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003453 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003454 }
3455 }
3456 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3457 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3458 ps->flags |= PEER_F_HEARTBEAT;
3459 appctx_wakeup(ps->appctx);
3460 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003461 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003462 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003463 }
3464 /* else do nothing */
3465 } /* SUCCESSCODE */
3466 } /* !ps->peer->local */
3467 } /* for */
3468
3469 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003470 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3471 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3472 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003473 /* Resync from remote peer needed
3474 * no peer was assigned for the lesson
3475 * and resync timeout expire */
3476
3477 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003478 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003479 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003480 }
3481
Emeric Brunb3971ab2015-05-12 18:49:09 +02003482 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003483 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003484 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3485 if (!tick_is_expired(peers->resync_timeout, now_ms))
3486 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003487 }
3488 } /* !stopping */
3489 else {
3490 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003491 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003492 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003493 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003494 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003495 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003496 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003497
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003498 /* disconnect all connected peers to process a local sync
3499 * this must be done only the first time we are switching
3500 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003501 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003502 for (ps = peers->remote; ps; ps = ps->next) {
3503 /* we're killing a connection, we must apply a random delay before
3504 * retrying otherwise the other end will do the same and we can loop
3505 * for a while.
3506 */
3507 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3508 if (ps->appctx) {
3509 peer_session_forceshutdown(ps);
3510 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003511 }
Christopher Faulet160fff62022-07-26 19:19:18 +02003512
3513 /* Set resync timeout for the local peer and request a immediate reconnect */
3514 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3515 peers->local->reconnect = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003516 }
3517 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003518
Emeric Brunb3971ab2015-05-12 18:49:09 +02003519 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003520 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003521 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003522 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003523 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003524 peers->flags &= ~PEERS_F_DONOTSTOP;
3525 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003526 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003527 }
3528 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003529 else if (!ps->appctx) {
Christopher Faulet19a82b92022-08-26 18:40:46 +02003530 /* Re-arm resync timeout if necessary */
3531 if (!tick_isset(peers->resync_timeout))
3532 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3533
Willy Tarreau9df94c22016-10-31 18:42:52 +01003534 /* If there's no active peer connection */
Christopher Fauletb372f162022-08-26 18:46:16 +02003535 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED &&
3536 !tick_is_expired(peers->resync_timeout, now_ms) &&
Christopher Faulet160fff62022-07-26 19:19:18 +02003537 (ps->statuscode == 0 ||
3538 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3539 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3540 ps->statuscode == PEER_SESS_SC_TRYAGAIN)) {
Christopher Fauletb372f162022-08-26 18:46:16 +02003541 /* The resync is finished for the local peer and
3542 * the resync timeout is not expired and
Christopher Faulet160fff62022-07-26 19:19:18 +02003543 * connection never tried
3544 * or previous peer connection was successfully established
3545 * or previous tcp connect succeeded but init state incomplete
3546 * or during previous connect, peer replies a try again statuscode */
Emeric Brun2b920a12010-09-23 18:30:22 +02003547
Christopher Faulet160fff62022-07-26 19:19:18 +02003548 if (!tick_is_expired(ps->reconnect, now_ms)) {
3549 /* reconnection timer is not expired. reschedule task for reconnect */
3550 task->expire = tick_first(task->expire, ps->reconnect);
3551 }
3552 else {
3553 /* connect to the local peer if we must push a local sync */
3554 if (peers->flags & PEERS_F_DONOTSTOP) {
3555 peer_session_create(peers, ps);
3556 }
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003557 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003558 }
3559 else {
3560 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003561 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003562 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003563 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003564 peers->flags &= ~PEERS_F_DONOTSTOP;
3565 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003566 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003567 }
3568 }
3569 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003570 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Christopher Faulet19a82b92022-08-26 18:40:46 +02003571 /* Reset resync timeout during a resync */
3572 peers->resync_timeout = TICK_ETERNITY;
3573
Willy Tarreau9df94c22016-10-31 18:42:52 +01003574 /* current peer connection is active and established
3575 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003576 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003577 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003578 appctx_wakeup(ps->appctx);
3579 break;
3580 }
3581 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003582 }
3583 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003584
3585 /* Release lock for all peers of the section */
3586 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003587 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003588
Emeric Brun2b920a12010-09-23 18:30:22 +02003589 /* Wakeup for re-connect */
3590 return task;
3591}
3592
Emeric Brunb3971ab2015-05-12 18:49:09 +02003593
Emeric Brun2b920a12010-09-23 18:30:22 +02003594/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003595 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003596 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003597int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003598{
Emeric Brun2b920a12010-09-23 18:30:22 +02003599 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003600
Emeric Brun2b920a12010-09-23 18:30:22 +02003601 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003602 peers->peers_fe->maxconn += 3;
3603 }
3604
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003605 peers->sync_task = task_new_anywhere();
Willy Tarreaud9443442018-10-15 11:18:03 +02003606 if (!peers->sync_task)
3607 return 0;
3608
Maciej Zdebd01be2a2022-05-16 17:26:20 +02003609 memset(peers->applet_count, 0, sizeof(peers->applet_count));
Emeric Brunb3971ab2015-05-12 18:49:09 +02003610 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003611 peers->sync_task->context = (void *)peers;
3612 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3613 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003614 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003615}
3616
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003617/*
3618 * Allocate a cache a dictionary entries used upon transmission.
3619 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003620static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003621{
3622 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003623 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003624
3625 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003626 entries = calloc(max_entries, sizeof *entries);
3627 if (!d || !entries)
3628 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003629
3630 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003631 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003632 d->cached_entries = EB_ROOT_UNIQUE;
3633 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003634
3635 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003636
3637 err:
3638 free(d);
3639 free(entries);
3640 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003641}
3642
3643/*
3644 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3645 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003646 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003647 * Must be deallocated calling free_dcache().
3648 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003649static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003650{
3651 struct dcache_tx *dc_tx;
3652 struct dcache *dc;
3653 struct dcache_rx *dc_rx;
3654
3655 dc = calloc(1, sizeof *dc);
3656 dc_tx = new_dcache_tx(max_entries);
3657 dc_rx = calloc(max_entries, sizeof *dc_rx);
3658 if (!dc || !dc_tx || !dc_rx)
3659 goto err;
3660
3661 dc->tx = dc_tx;
3662 dc->rx = dc_rx;
3663 dc->max_entries = max_entries;
3664
3665 return dc;
3666
3667 err:
3668 free(dc);
3669 free(dc_tx);
3670 free(dc_rx);
3671 return NULL;
3672}
3673
3674/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003675 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3676 * entries used upon transmission.
3677 * Return the entry if found, NULL if not.
3678 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003679static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3680 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003681{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003682 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003683}
3684
3685/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003686 * Flush <dc> cache.
3687 * Always succeeds.
3688 */
3689static inline void flush_dcache(struct peer *peer)
3690{
3691 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003692 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003693
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003694 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003695 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003696 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003697 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3698 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003699 }
3700 dc->tx->prev_lookup = NULL;
3701 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003702
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003703 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003704}
3705
3706/*
3707 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3708 * with information provided by <i> dictionary cache entry (especially the value
3709 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003710 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003711 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003712static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003713{
3714 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003715 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003716
3717 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003718
3719 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3720 o = dc_tx->prev_lookup;
3721 } else {
3722 o = dcache_tx_lookup_value(dc_tx, i);
3723 if (o) {
3724 /* Save it */
3725 dc_tx->prev_lookup = o;
3726 }
3727 }
3728
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003729 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003730 /* Copy the ID. */
3731 i->id = o - dc->tx->entries;
3732 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003733 }
3734
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003735 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003736 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003737
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003738 ebpt_delete(o);
3739 o->key = i->entry.key;
3740 ebpt_insert(&dc_tx->cached_entries, o);
3741 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003742
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003743 /* Update the index for the next entry to put in cache */
3744 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003745
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003746 return o;
3747}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003748
3749/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003750 * Allocate a dictionary cache for each peer of <peers> section.
3751 * Return 1 if succeeded, 0 if not.
3752 */
3753int peers_alloc_dcache(struct peers *peers)
3754{
3755 struct peer *p;
3756
3757 for (p = peers->remote; p; p = p->next) {
3758 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3759 if (!p->dcache)
3760 return 0;
3761 }
3762
3763 return 1;
3764}
3765
3766/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003767 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003768 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003769 */
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003770int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003771{
3772 struct shared_table *st;
3773 struct peer * curpeer;
3774 int id = 0;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003775 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003776
3777 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003778 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003779 if (!st) {
3780 retval = 1;
3781 break;
3782 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003783 st->table = table;
3784 st->next = curpeer->tables;
3785 if (curpeer->tables)
3786 id = curpeer->tables->local_id;
3787 st->local_id = id + 1;
3788
Emeric Brun2cc201f2021-04-23 12:21:26 +02003789 /* If peer is local we inc table
3790 * refcnt to protect against flush
3791 * until this process pushed all
3792 * table content to the new one
3793 */
3794 if (curpeer->local)
3795 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003796 curpeer->tables = st;
3797 }
3798
3799 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003800
3801 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003802}
3803
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003804/* context used by a "show peers" command */
3805struct show_peers_ctx {
Willy Tarreau3a31e372022-05-03 14:58:47 +02003806 void *target; /* if non-null, dump only this section and stop */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003807 struct peers *peers; /* "peers" section being currently dumped. */
3808 struct peer *peer; /* "peer" being currently dumped. */
3809 int flags; /* non-zero if "dict" dump requested */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003810 enum {
Willy Tarreauce9123c2022-05-03 15:04:25 +02003811 STATE_HEAD = 0, /* dump the section's header */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003812 STATE_PEER, /* dump the whole peer */
3813 STATE_DONE, /* finished */
3814 } state; /* parser's state */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003815};
3816
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003817/*
3818 * Parse the "show peers" command arguments.
3819 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3820 * error message.
3821 */
3822static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3823{
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003824 struct show_peers_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003825
Willy Tarreau49962b52021-02-12 16:56:22 +01003826 if (strcmp(args[2], "dict") == 0) {
3827 /* show the dictionaries (large dump) */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003828 ctx->flags |= PEERS_SHOW_F_DICT;
Willy Tarreau49962b52021-02-12 16:56:22 +01003829 args++;
3830 } else if (strcmp(args[2], "-") == 0)
3831 args++; // allows to show a section called "dict"
3832
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003833 if (*args[2]) {
3834 struct peers *p;
3835
3836 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003837 if (strcmp(p->id, args[2]) == 0) {
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003838 ctx->target = p;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003839 break;
3840 }
3841 }
3842
Willy Tarreau9d008692019-08-09 11:21:01 +02003843 if (!p)
3844 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003845 }
3846
Willy Tarreauce9123c2022-05-03 15:04:25 +02003847 /* where to start from */
3848 ctx->peers = ctx->target ? ctx->target : cfg_peers;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003849 return 0;
3850}
3851
3852/*
3853 * This function dumps the peer state information of <peers> "peers" section.
3854 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3855 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3856 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003857static int peers_dump_head(struct buffer *msg, struct appctx *appctx, struct peers *peers)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003858{
3859 struct tm tm;
3860
3861 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003862 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 +02003863 peers,
3864 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3865 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003866 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003867 peers->resync_timeout ?
3868 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3869 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003870 TICKS_TO_MS(1000)) : "<NEVER>",
3871 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003872
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003873 if (applet_putchk(appctx, msg) == -1)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003874 return 0;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003875
3876 return 1;
3877}
3878
3879/*
3880 * This function dumps <peer> state information.
3881 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3882 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3883 */
Willy Tarreau50e77b22022-05-31 08:55:54 +02003884static int peers_dump_peer(struct buffer *msg, struct appctx *appctx, struct peer *peer, int flags)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003885{
3886 struct connection *conn;
3887 char pn[INET6_ADDRSTRLEN];
Willy Tarreau4596fe22022-05-17 19:07:51 +02003888 struct stconn *peer_cs;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003889 struct stream *peer_s;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003890 struct shared_table *st;
3891
3892 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003893 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003894 peer, peer->id,
3895 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003896 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003897 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003898 statuscode_str(peer->statuscode));
3899
3900 chunk_appendf(msg, " last_hdshk=%s\n",
3901 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3902 TICKS_TO_MS(1000)) : "<NEVER>");
3903
3904 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003905 peer->reconnect ?
3906 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3907 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003908 TICKS_TO_MS(1000)) : "<NEVER>");
3909
3910 chunk_appendf(msg, " heartbeat=%s",
3911 peer->heartbeat ?
3912 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3913 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3914 TICKS_TO_MS(1000)) : "<NEVER>");
3915
3916 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 +01003917 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003918 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003919
3920 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3921
Willy Tarreaufc505992022-05-31 08:53:25 +02003922 if (!peer->appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003923 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003924
Willy Tarreaufc505992022-05-31 08:53:25 +02003925 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u",
3926 peer->appctx, peer->appctx->st0, peer->appctx->st1,
3927 peer->appctx->t ? peer->appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003928
Willy Tarreauc12b3212022-05-27 11:08:15 +02003929 peer_cs = appctx_sc(peer->appctx);
Willy Tarreau03926122023-01-12 17:09:34 +01003930 if (!peer_cs) {
3931 /* the appctx might exist but not yet be initialized due to
3932 * deferred initialization used to balance applets across
3933 * threads.
3934 */
3935 goto table_info;
3936 }
3937
Willy Tarreauea27f482022-05-18 16:10:52 +02003938 peer_s = __sc_strm(peer_cs);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003939
Willy Tarreau74568cf2022-05-27 09:03:30 +02003940 chunk_appendf(&trash, " state=%s", sc_state_str(sc_opposite(peer_cs)->state));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003941
3942 conn = objt_conn(strm_orig(peer_s));
3943 if (conn)
3944 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3945
Willy Tarreau3ca14902019-07-17 14:53:15 +02003946 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 +02003947 case AF_INET:
3948 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003949 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003950 break;
3951 case AF_UNIX:
3952 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3953 break;
3954 }
3955
Willy Tarreau3ca14902019-07-17 14:53:15 +02003956 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 +02003957 case AF_INET:
3958 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003959 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003960 break;
3961 case AF_UNIX:
3962 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3963 break;
3964 }
3965
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003966 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003967 if (peer->remote_table)
3968 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3969 peer->remote_table,
3970 peer->remote_table->table->id,
3971 peer->remote_table->local_id,
3972 peer->remote_table->remote_id);
3973
3974 if (peer->last_local_table)
3975 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3976 peer->last_local_table,
3977 peer->last_local_table->table->id,
3978 peer->last_local_table->local_id,
3979 peer->last_local_table->remote_id);
3980
3981 if (peer->tables) {
3982 chunk_appendf(&trash, "\n shared tables:");
3983 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003984 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003985 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003986 struct dcache *dcache;
3987
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003988 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003989 dcache = peer->dcache;
3990
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003991 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3992 "flags=0x%x remote_data=0x%llx",
3993 st, st->local_id, st->remote_id,
3994 st->flags, (unsigned long long)st->remote_data);
3995 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3996 " teaching_origin=%u update=%u",
3997 st->last_acked, st->last_pushed, st->last_get,
3998 st->teaching_origin, st->update);
3999 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02004000 " commitupdate=%u refcnt=%u",
4001 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01004002 if (flags & PEERS_SHOW_F_DICT) {
4003 chunk_appendf(&trash, "\n TX dictionary cache:");
4004 count = 0;
4005 for (i = 0; i < dcache->max_entries; i++) {
4006 struct ebpt_node *node;
4007 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02004008
Willy Tarreau49962b52021-02-12 16:56:22 +01004009 node = &dcache->tx->entries[i];
4010 if (!node->key)
4011 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02004012
Willy Tarreau49962b52021-02-12 16:56:22 +01004013 if (!count++)
4014 chunk_appendf(&trash, "\n ");
4015 de = node->key;
4016 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
4017 count &= 0x3;
4018 }
4019 chunk_appendf(&trash, "\n RX dictionary cache:");
4020 count = 0;
4021 for (i = 0; i < dcache->max_entries; i++) {
4022 if (!count++)
4023 chunk_appendf(&trash, "\n ");
4024 chunk_appendf(&trash, " %3u -> %s", i,
4025 dcache->rx[i].de ?
4026 (char *)dcache->rx[i].de->value.key : "-");
4027 count &= 0x3;
4028 }
4029 } else {
4030 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02004031 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004032 }
4033 }
4034
4035 end:
4036 chunk_appendf(&trash, "\n");
Willy Tarreau50e77b22022-05-31 08:55:54 +02004037 if (applet_putchk(appctx, msg) == -1)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004038 return 0;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004039
4040 return 1;
4041}
4042
4043/*
4044 * This function dumps all the peers of "peers" section.
4045 * Returns 0 if the output buffer is full and needs to be called
4046 * again, non-zero if not. It proceeds in an isolated thread, so
4047 * there is no thread safety issue here.
4048 */
4049static int cli_io_handler_show_peers(struct appctx *appctx)
4050{
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004051 struct show_peers_ctx *ctx = appctx->svcctx;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004052 int ret = 0, first_peers = 1;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004053
4054 thread_isolate();
4055
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004056 chunk_reset(&trash);
4057
Willy Tarreau3a31e372022-05-03 14:58:47 +02004058 while (ctx->state != STATE_DONE) {
4059 switch (ctx->state) {
Willy Tarreau3a31e372022-05-03 14:58:47 +02004060 case STATE_HEAD:
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004061 if (!ctx->peers) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004062 /* No more peers list. */
Willy Tarreau3a31e372022-05-03 14:58:47 +02004063 ctx->state = STATE_DONE;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004064 }
4065 else {
4066 if (!first_peers)
4067 chunk_appendf(&trash, "\n");
4068 else
4069 first_peers = 0;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004070 if (!peers_dump_head(&trash, appctx, ctx->peers))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004071 goto out;
4072
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004073 ctx->peer = ctx->peers->remote;
4074 ctx->peers = ctx->peers->next;
Willy Tarreau3a31e372022-05-03 14:58:47 +02004075 ctx->state = STATE_PEER;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004076 }
4077 break;
4078
Willy Tarreau3a31e372022-05-03 14:58:47 +02004079 case STATE_PEER:
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004080 if (!ctx->peer) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004081 /* End of peer list */
Willy Tarreauce9123c2022-05-03 15:04:25 +02004082 if (!ctx->target)
4083 ctx->state = STATE_HEAD; // next one
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004084 else
Willy Tarreau3a31e372022-05-03 14:58:47 +02004085 ctx->state = STATE_DONE;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004086 }
4087 else {
Willy Tarreau50e77b22022-05-31 08:55:54 +02004088 if (!peers_dump_peer(&trash, appctx, ctx->peer, ctx->flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004089 goto out;
4090
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004091 ctx->peer = ctx->peer->next;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004092 }
Willy Tarreau3a31e372022-05-03 14:58:47 +02004093 break;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004094
Willy Tarreau3a31e372022-05-03 14:58:47 +02004095 default:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004096 break;
4097 }
4098 }
4099 ret = 1;
4100 out:
4101 thread_release();
4102 return ret;
4103}
4104
Willy Tarreau8bd146d2022-07-19 20:17:38 +02004105/* config parser for global "tune.peers.max-updates-at-once" */
4106static int cfg_parse_max_updt_at_once(char **args, int section_type, struct proxy *curpx,
4107 const struct proxy *defpx, const char *file, int line,
4108 char **err)
4109{
4110 int arg = -1;
4111
4112 if (too_many_args(1, args, err, NULL))
4113 return -1;
4114
4115 if (*(args[1]) != 0)
4116 arg = atoi(args[1]);
4117
4118 if (arg < 1) {
4119 memprintf(err, "'%s' expects an integer argument greater than 0.", args[0]);
4120 return -1;
4121 }
4122
4123 peers_max_updates_at_once = arg;
4124 return 0;
4125}
4126
4127/* config keyword parsers */
4128static struct cfg_kw_list cfg_kws = {ILH, {
4129 { CFG_GLOBAL, "tune.peers.max-updates-at-once", cfg_parse_max_updt_at_once },
4130 { 0, NULL, NULL }
4131}};
4132
4133INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4134
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004135/*
4136 * CLI keywords.
4137 */
4138static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004139 { { "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 +02004140 {},
4141}};
4142
4143/* Register cli keywords */
4144INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);