blob: d4aa69f232eacb408549e7f93aa090306a41a50e [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" },
Frédéric Lécailled8659352020-11-10 16:18:03 +0100337};
338
339static const struct name_desc peers_trace_lockon_args[4] = {
340 /* arg1 */ { /* already used by the connection */ },
341 /* arg2 */ { .name="peers", .desc="Peers protocol" },
342 /* arg3 */ { },
343 /* arg4 */ { }
344};
345
346static const struct name_desc peers_trace_decoding[] = {
347#define PEERS_VERB_CLEAN 1
348 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
349 { /* end */ }
350};
351
352
353struct trace_source trace_peers = {
354 .name = IST("peers"),
355 .desc = "Peers protocol",
356 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
357 .default_cb = peers_trace,
358 .known_events = peers_trace_events,
359 .lockon_args = peers_trace_lockon_args,
360 .decoding = peers_trace_decoding,
361 .report_events = ~0, /* report everything by default */
362};
363
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100364/* Return peer control message types as strings (only for debugging purpose). */
365static inline char *ctrl_msg_type_str(unsigned int type)
366{
367 switch (type) {
368 case PEER_MSG_CTRL_RESYNCREQ:
369 return "RESYNCREQ";
370 case PEER_MSG_CTRL_RESYNCFINISHED:
371 return "RESYNCFINISHED";
372 case PEER_MSG_CTRL_RESYNCPARTIAL:
373 return "RESYNCPARTIAL";
374 case PEER_MSG_CTRL_RESYNCCONFIRM:
375 return "RESYNCCONFIRM";
376 case PEER_MSG_CTRL_HEARTBEAT:
377 return "HEARTBEAT";
378 default:
379 return "???";
380 }
381}
382
Frédéric Lécailled8659352020-11-10 16:18:03 +0100383#define TRACE_SOURCE &trace_peers
384INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
385
386static void peers_trace(enum trace_level level, uint64_t mask,
387 const struct trace_source *src,
388 const struct ist where, const struct ist func,
389 const void *a1, const void *a2, const void *a3, const void *a4)
390{
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100391 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécailled8659352020-11-10 16:18:03 +0100392 if (a2) {
393 const struct peer *peer = a2;
394
395 chunk_appendf(&trace_buf, " peer=%s", peer->id);
396 }
397 if (a3) {
398 const char *p = a3;
399
400 chunk_appendf(&trace_buf, " @%p", p);
401 }
402 if (a4) {
403 const size_t *val = a4;
404
405 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
406 }
407 }
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100408
409 if (mask & PEERS_EV_DEFMSG) {
410 if (a2) {
411 const struct peer *peer = a2;
412
413 chunk_appendf(&trace_buf, " peer=%s", peer->id);
414 }
415 if (a3) {
416 const char *p = a3;
417
418 chunk_appendf(&trace_buf, " @%p", p);
419 }
420 if (a4) {
421 const int *val = a4;
422
423 chunk_appendf(&trace_buf, " %d", *val);
424 }
425 }
426
427 if (mask & PEERS_EV_CTRLMSG) {
428 if (a2) {
429 const unsigned char *ctrl_msg_type = a2;
430
431 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
432
433 }
434 if (a3) {
435 const char *local_peer = a3;
436
437 chunk_appendf(&trace_buf, " %s", local_peer);
438 }
439
440 if (a4) {
441 const char *remote_peer = a4;
442
443 chunk_appendf(&trace_buf, " -> %s", remote_peer);
444 }
445 }
446
447 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
448 if (a2) {
449 const struct peer *peer = a2;
450 struct peers *peers = NULL;
451
Christopher Faulet387e7972022-05-12 14:47:52 +0200452 if (peer->appctx)
453 peers = peer->peers;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100454
455 if (peers)
456 chunk_appendf(&trace_buf, " %s", peers->local->id);
Christopher Fauletd9e6b352021-11-15 09:40:57 +0100457 chunk_appendf(&trace_buf, " -> %s", peer->id);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100458 }
459
460 if (a3) {
461 const int *prev_state = a3;
462
463 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
464 }
465 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100466}
467
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200468static const char *statuscode_str(int statuscode)
469{
470 switch (statuscode) {
471 case PEER_SESS_SC_CONNECTCODE:
472 return "CONN";
473 case PEER_SESS_SC_CONNECTEDCODE:
474 return "HSHK";
475 case PEER_SESS_SC_SUCCESSCODE:
476 return "ESTA";
477 case PEER_SESS_SC_TRYAGAIN:
478 return "RETR";
479 case PEER_SESS_SC_ERRPROTO:
480 return "PROT";
481 case PEER_SESS_SC_ERRVERSION:
482 return "VERS";
483 case PEER_SESS_SC_ERRHOST:
484 return "NAME";
485 case PEER_SESS_SC_ERRPEER:
486 return "UNKN";
487 default:
488 return "NONE";
489 }
490}
491
Emeric Brun18928af2017-03-29 16:32:53 +0200492/* This function encode an uint64 to 'dynamic' length format.
493 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500494 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200495 At return, the *str is set at the next Byte after then
496 encoded integer. The function returns then length of the
497 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200498int intencode(uint64_t i, char **str) {
499 int idx = 0;
500 unsigned char *msg;
501
Emeric Brunb3971ab2015-05-12 18:49:09 +0200502 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200503 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200504 msg[0] = (unsigned char)i;
505 *str = (char *)&msg[idx+1];
506 return (idx+1);
507 }
508
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200509 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
510 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
511 while (i >= PEER_ENC_STOP_BYTE) {
512 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
513 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200514 }
515 msg[++idx] = (unsigned char)i;
516 *str = (char *)&msg[idx+1];
517 return (idx+1);
518}
519
520
Emeric Brun5ea07d92021-06-30 13:21:58 +0200521/* This function returns a decoded 64bits unsigned integer
522 * from a varint
523 *
524 * Calling:
525 * - *str must point on the first byte of the buffer to decode.
526 * - end must point on the next byte after the end of the buffer
527 * we are authorized to parse (buf + buflen)
528 *
529 * At return:
530 *
531 * On success *str will point at the byte following
532 * the fully decoded integer into the buffer. and
533 * the decoded value is returned.
534 *
535 * If end is reached before the integer was fully decoded,
536 * *str is set to NULL and the caller have to check this
537 * to know there is a decoding error. In this case
538 * the returned integer is also forced to 0
539 */
Emeric Brun18928af2017-03-29 16:32:53 +0200540uint64_t intdecode(char **str, char *end)
541{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200542 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200543 uint64_t i;
544 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200545
546 if (!*str)
547 return 0;
548
549 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200550 if (msg >= (unsigned char *)end)
551 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200552
Emeric Brun18928af2017-03-29 16:32:53 +0200553 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200554 if (i >= PEER_ENC_2BYTES_MIN) {
555 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200556 do {
557 if (msg >= (unsigned char *)end)
558 goto fail;
559 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200560 shift += PEER_ENC_STOP_BIT;
561 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200562 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100563 *str = (char *)msg;
564 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200565
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100566 fail:
567 *str = NULL;
568 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200569}
Emeric Brun2b920a12010-09-23 18:30:22 +0200570
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100571/*
572 * Build a "hello" peer protocol message.
573 * Return the number of written bytes written to build this messages if succeeded,
574 * 0 if not.
575 */
576static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
577{
578 int min_ver, ret;
579 struct peer *peer;
580
581 peer = p->hello.peer;
582 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
583 /* Prepare headers */
Willy Tarreau2645b342022-04-12 08:28:18 +0200584 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %d.%d\n%s\n%s %d %d\n",
585 (int)PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), (int)1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100586 if (ret >= size)
587 return 0;
588
589 return ret;
590}
591
592/*
593 * Build a "handshake succeeded" status message.
594 * Return the number of written bytes written to build this messages if succeeded,
595 * 0 if not.
596 */
597static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
598{
599 int ret;
600
Willy Tarreau2645b342022-04-12 08:28:18 +0200601 ret = snprintf(msg, size, "%d\n", (int)PEER_SESS_SC_SUCCESSCODE);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100602 if (ret >= size)
603 return 0;
604
605 return ret;
606}
607
608/*
609 * Build an error status message.
610 * Return the number of written bytes written to build this messages if succeeded,
611 * 0 if not.
612 */
613static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
614{
615 int ret;
616 unsigned int st1;
617
618 st1 = p->error_status.st1;
619 ret = snprintf(msg, size, "%d\n", st1);
620 if (ret >= size)
621 return 0;
622
623 return ret;
624}
625
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200626/* Set the stick-table UPDATE message type byte at <msg_type> address,
627 * depending on <use_identifier> and <use_timed> boolean parameters.
628 * Always successful.
629 */
630static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
631{
632 if (use_timed) {
633 if (use_identifier)
634 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
635 else
636 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
637 }
638 else {
639 if (use_identifier)
640 *msg_type = PEER_MSG_STKT_UPDATE;
641 else
642 *msg_type = PEER_MSG_STKT_INCUPDATE;
643 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200644}
Emeric Brun2b920a12010-09-23 18:30:22 +0200645/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200646 * This prepare the data update message on the stick session <ts>, <st> is the considered
647 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800648 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200649 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
650 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200651 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100652static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200653{
654 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200655 unsigned short datalen;
656 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200657 unsigned int data_type;
658 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100659 struct stksess *ts;
660 struct shared_table *st;
661 unsigned int updateid;
662 int use_identifier;
663 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200664 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100665
666 ts = p->updt.stksess;
667 st = p->updt.shared_table;
668 updateid = p->updt.updateid;
669 use_identifier = p->updt.use_identifier;
670 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200671 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200672
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200673 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200674
Emeric Brun2b920a12010-09-23 18:30:22 +0200675 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200676
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500677 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200678 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200679 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200680 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200681
682 /* encode update identifier if needed */
683 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200684 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200685 memcpy(cursor, &netinteger, sizeof(netinteger));
686 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200687 }
688
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200689 if (use_timed) {
690 netinteger = htonl(tick_remain(now_ms, ts->expire));
691 memcpy(cursor, &netinteger, sizeof(netinteger));
692 cursor += sizeof(netinteger);
693 }
694
Emeric Brunb3971ab2015-05-12 18:49:09 +0200695 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200696 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200697 int stlen = strlen((char *)ts->key.key);
698
Emeric Brunb3971ab2015-05-12 18:49:09 +0200699 intencode(stlen, &cursor);
700 memcpy(cursor, ts->key.key, stlen);
701 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200702 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200703 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100704 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200705 memcpy(cursor, &netinteger, sizeof(netinteger));
706 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200707 }
708 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200709 memcpy(cursor, ts->key.key, st->table->key_size);
710 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200711 }
712
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100713 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200714 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200715 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200716
Emeric Brun94900952015-06-11 18:25:54 +0200717 data_ptr = stktable_data_ptr(st->table, ts, data_type);
718 if (data_ptr) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200719 /* in case of array all elements use
720 * the same std_type and they are linearly
721 * encoded.
722 */
723 if (stktable_data_types[data_type].is_array) {
724 unsigned int idx = 0;
725
726 switch (stktable_data_types[data_type].std_type) {
727 case STD_T_SINT: {
728 int data;
729
730 do {
731 data = stktable_data_cast(data_ptr, std_t_sint);
732 intencode(data, &cursor);
733
734 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
735 } while(data_ptr);
736 break;
737 }
738 case STD_T_UINT: {
739 unsigned int data;
740
741 do {
742 data = stktable_data_cast(data_ptr, std_t_uint);
743 intencode(data, &cursor);
744
745 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
746 } while(data_ptr);
747 break;
748 }
749 case STD_T_ULL: {
750 unsigned long long data;
751
752 do {
753 data = stktable_data_cast(data_ptr, std_t_ull);
754 intencode(data, &cursor);
755
756 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
757 } while(data_ptr);
758 break;
759 }
760 case STD_T_FRQP: {
761 struct freq_ctr *frqp;
762
763 do {
764 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
765 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
766 intencode(frqp->curr_ctr, &cursor);
767 intencode(frqp->prev_ctr, &cursor);
768
769 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
770 } while(data_ptr);
771 break;
772 }
773 }
774
775 /* array elements fully encoded
776 * proceed next data_type.
777 */
778 continue;
779 }
Emeric Brun94900952015-06-11 18:25:54 +0200780 switch (stktable_data_types[data_type].std_type) {
781 case STD_T_SINT: {
782 int data;
783
784 data = stktable_data_cast(data_ptr, std_t_sint);
785 intencode(data, &cursor);
786 break;
787 }
788 case STD_T_UINT: {
789 unsigned int data;
790
791 data = stktable_data_cast(data_ptr, std_t_uint);
792 intencode(data, &cursor);
793 break;
794 }
795 case STD_T_ULL: {
796 unsigned long long data;
797
798 data = stktable_data_cast(data_ptr, std_t_ull);
799 intencode(data, &cursor);
800 break;
801 }
802 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200803 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200804
805 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
806 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
807 intencode(frqp->curr_ctr, &cursor);
808 intencode(frqp->prev_ctr, &cursor);
809 break;
810 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200811 case STD_T_DICT: {
812 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200813 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200814 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200815 char *beg, *end;
816 size_t value_len, data_len;
817 struct dcache *dc;
818
819 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100820 if (!de) {
821 /* No entry */
822 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200823 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100824 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200825
826 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200827 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200828 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200829 if (cached_de == &cde.entry) {
830 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
831 break;
832 /* Encode the length of the remaining data -> 1 */
833 intencode(1, &cursor);
834 /* Encode the cache entry ID */
835 intencode(cde.id + 1, &cursor);
836 }
837 else {
838 /* Leave enough room to encode the remaining data length. */
839 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
840 /* Encode the dictionary entry key */
841 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200842 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200843 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200844 intencode(value_len, &end);
845 /* Copy the data */
846 memcpy(end, de->value.key, value_len);
847 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200848 /* Encode the length of the data */
849 data_len = end - beg;
850 intencode(data_len, &cursor);
851 memmove(cursor, beg, data_len);
852 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200853 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200854 break;
855 }
Emeric Brun94900952015-06-11 18:25:54 +0200856 }
857 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200858 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100859 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200860
861 /* Compute datalen */
862 datalen = (cursor - datamsg);
863
864 /* prepare message header */
865 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200866 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200867 cursor = &msg[2];
868 intencode(datalen, &cursor);
869
870 /* move data after header */
871 memmove(cursor, datamsg, datalen);
872
873 /* return header size + data_len */
874 return (cursor - msg) + datalen;
875}
876
877/*
878 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800879 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200880 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
881 * check size)
882 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100883static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200884{
885 int len;
886 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200887 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200888 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200889 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200890 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100891 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200892
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100893 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200894 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200895
896 /* Encode data */
897
898 /* encode local id */
899 intencode(st->local_id, &cursor);
900
901 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100902 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200903 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100904 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200905 cursor += len;
906
907 /* encode table type */
908
Emeric Brun530ba382020-06-02 11:17:42 +0200909 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200910
911 /* encode table key size */
912 intencode(st->table->key_size, &cursor);
913
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200914 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200915 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200916 /* encode available known data types in table */
917 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
918 if (st->table->data_ofs[data_type]) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200919 /* stored data types parameters are all linearly encoded
920 * at the end of the 'table definition' message.
921 *
922 * Currently only array data_types and and data_types
923 * using freq_counter base type have parameters:
924 *
925 * - array has always at least one parameter set to the
926 * number of elements.
927 *
928 * - array of base-type freq_counters has an additional
929 * parameter set to the period used to compute those
930 * freq_counters.
931 *
932 * - simple freq counter has a parameter set to the period
933 * used to compute
934 *
935 * A set of parameter for a datatype MUST BE prefixed
936 * by the data-type id itself:
937 * This is useless because the data_types are ordered and
938 * the data_type bitfield already gives the information of
939 * stored types, but it was designed this way when the
940 * push of period parameter was added for freq counters
941 * and we don't want to break the compatibility.
942 *
943 */
944 if (stktable_data_types[data_type].is_array) {
945 /* This is an array type so we first encode
946 * the data_type itself to prefix parameters
947 */
948 intencode(data_type, &chunkq);
949
950 /* We encode the first parameter which is
951 * the number of elements of this array
952 */
953 intencode(st->table->data_nbelem[data_type], &chunkq);
954
Ilya Shipitsin01881082021-08-07 14:41:56 +0500955 /* for array of freq counters, there is an additional
Emeric Brun90a9b672021-06-22 16:09:55 +0200956 * period parameter to encode
957 */
958 if (stktable_data_types[data_type].std_type == STD_T_FRQP)
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200959 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200960 }
Emeric Brun90a9b672021-06-22 16:09:55 +0200961 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
962 /* this datatype is a simple freq counter not part
963 * of an array. We encode the data_type itself
964 * to prefix the 'period' parameter
965 */
966 intencode(data_type, &chunkq);
967 intencode(st->table->data_arg[data_type].u, &chunkq);
968 }
969 /* set the bit corresponding to stored data type */
970 data |= 1ULL << data_type;
Emeric Brun94900952015-06-11 18:25:54 +0200971 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200972 }
973 intencode(data, &cursor);
974
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200975 /* Encode stick-table entries duration. */
976 intencode(st->table->expire, &cursor);
977
978 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200979 chunk->data = chunkq - chunkp;
980 memcpy(cursor, chunk->area, chunk->data);
981 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200982 }
983
Emeric Brunb3971ab2015-05-12 18:49:09 +0200984 /* Compute datalen */
985 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200986
Emeric Brunb3971ab2015-05-12 18:49:09 +0200987 /* prepare message header */
988 msg[0] = PEER_MSG_CLASS_STICKTABLE;
989 msg[1] = PEER_MSG_STKT_DEFINE;
990 cursor = &msg[2];
991 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200992
Emeric Brunb3971ab2015-05-12 18:49:09 +0200993 /* move data after header */
994 memmove(cursor, datamsg, datalen);
995
996 /* return header size + data_len */
997 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200998}
999
Emeric Brunb3971ab2015-05-12 18:49:09 +02001000/*
1001 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
1002 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -08001003 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +02001004 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
1005 * check size)
1006 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001007static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001008{
1009 unsigned short datalen;
1010 char *cursor, *datamsg;
1011 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001012 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001013
Frédéric Lécaille39143342019-05-24 14:32:27 +02001014 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001015
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001016 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001017 intencode(st->remote_id, &cursor);
1018 netinteger = htonl(st->last_get);
1019 memcpy(cursor, &netinteger, sizeof(netinteger));
1020 cursor += sizeof(netinteger);
1021
1022 /* Compute datalen */
1023 datalen = (cursor - datamsg);
1024
1025 /* prepare message header */
1026 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +02001027 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001028 cursor = &msg[2];
1029 intencode(datalen, &cursor);
1030
1031 /* move data after header */
1032 memmove(cursor, datamsg, datalen);
1033
1034 /* return header size + data_len */
1035 return (cursor - msg) + datalen;
1036}
Emeric Brun2b920a12010-09-23 18:30:22 +02001037
1038/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001039 * Function to deinit connected peer
1040 */
1041void __peer_session_deinit(struct peer *peer)
1042{
Christopher Faulet387e7972022-05-12 14:47:52 +02001043 struct peers *peers = peer->peers;
Maciej Zdebd01be2a2022-05-16 17:26:20 +02001044 int thr;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001045
Christopher Faulet387e7972022-05-12 14:47:52 +02001046 if (!peers || !peer->appctx)
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001047 return;
1048
Willy Tarreau29ffe262022-06-15 14:31:38 +02001049 thr = peer->appctx->t->tid;
Maciej Zdebd01be2a2022-05-16 17:26:20 +02001050 HA_ATOMIC_DEC(&peers->applet_count[thr]);
1051
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001052 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02001053 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001054
Willy Tarreau4781b152021-04-06 13:53:36 +02001055 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001056
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001057 flush_dcache(peer);
1058
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001059 /* Re-init current table pointers to force announcement on re-connect */
1060 peer->remote_table = peer->last_local_table = NULL;
1061 peer->appctx = NULL;
1062 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1063 /* unassign current peer for learning */
1064 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
1065 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1066
Emeric Brunccdfbae2021-04-28 12:59:35 +02001067 if (peer->local)
1068 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
1069 else
1070 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001071 /* reschedule a resync */
1072 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1073 }
1074 /* reset teaching and learning flags to 0 */
1075 peer->flags &= PEER_TEACH_RESET;
1076 peer->flags &= PEER_LEARN_RESET;
1077 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1078}
1079
Christopher Faulet6712dc62022-05-12 15:36:11 +02001080static int peer_session_init(struct appctx *appctx)
1081{
1082 struct peer *peer = appctx->svcctx;
1083 struct stream *s;
1084 struct sockaddr_storage *addr = NULL;
1085
1086 if (!sockaddr_alloc(&addr, &peer->addr, sizeof(peer->addr)))
1087 goto out_error;
1088
1089 if (appctx_finalize_startup(appctx, peer->peers->peers_fe, &BUF_NULL) == -1)
1090 goto out_free_addr;
1091
1092 s = appctx_strm(appctx);
1093 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001094 applet_need_more_data(appctx);
Christopher Faulet6712dc62022-05-12 15:36:11 +02001095 appctx_wakeup(appctx);
1096
1097 /* initiate an outgoing connection */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001098 s->scb->dst = addr;
Willy Tarreaucb041662022-05-17 19:44:42 +02001099 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulet6712dc62022-05-12 15:36:11 +02001100 s->flags = SF_ASSIGNED;
1101 s->target = peer_session_target(peer, s);
1102
1103 s->do_log = NULL;
1104 s->uniq_id = 0;
1105
1106 s->res.flags |= CF_READ_DONTWAIT;
1107
1108 _HA_ATOMIC_INC(&active_peers);
1109 return 0;
1110
1111 out_free_addr:
1112 sockaddr_free(&addr);
1113 out_error:
1114 return -1;
1115}
1116
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001117/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001118 * Callback to release a session with a peer
1119 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001120static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001121{
Willy Tarreau455caef2022-05-05 20:16:16 +02001122 struct peer *peer = appctx->svcctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001123
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001124 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau455caef2022-05-05 20:16:16 +02001125 /* appctx->svcctx is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001126 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +02001127 return;
1128
1129 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001130 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001131 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001132 if (peer->appctx == appctx)
1133 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02001134 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001135 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001136 }
1137}
1138
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001139/* Retrieve the major and minor versions of peers protocol
1140 * announced by a remote peer. <str> is a null-terminated
1141 * string with the following format: "<maj_ver>.<min_ver>".
1142 */
1143static int peer_get_version(const char *str,
1144 unsigned int *maj_ver, unsigned int *min_ver)
1145{
1146 unsigned int majv, minv;
1147 const char *pos, *saved;
1148 const char *end;
1149
1150 saved = pos = str;
1151 end = str + strlen(str);
1152
1153 majv = read_uint(&pos, end);
1154 if (saved == pos || *pos++ != '.')
1155 return -1;
1156
1157 saved = pos;
1158 minv = read_uint(&pos, end);
1159 if (saved == pos || pos != end)
1160 return -1;
1161
1162 *maj_ver = majv;
1163 *min_ver = minv;
1164
1165 return 0;
1166}
Emeric Brun2b920a12010-09-23 18:30:22 +02001167
1168/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001169 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1170 * '\n' character.
1171 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1172 * a line could not be read because the communication channel is closed.
1173 */
1174static inline int peer_getline(struct appctx *appctx)
1175{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001176 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001177 int n;
Frédéric Lécaillece025572019-01-21 13:38:06 +01001178
Willy Tarreauda304902022-05-27 10:44:04 +02001179 n = co_getline(sc_oc(sc), trash.area, trash.size);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001180 if (!n)
1181 return 0;
1182
1183 if (n < 0 || trash.area[n - 1] != '\n') {
1184 appctx->st0 = PEER_SESS_ST_END;
1185 return -1;
1186 }
1187
1188 if (n > 1 && (trash.area[n - 2] == '\r'))
1189 trash.area[n - 2] = 0;
1190 else
1191 trash.area[n - 1] = 0;
1192
Willy Tarreauda304902022-05-27 10:44:04 +02001193 co_skip(sc_oc(sc), n);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001194
1195 return n;
1196}
1197
1198/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001199 * Send a message after having called <peer_prepare_msg> to build it.
1200 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1201 * Returns -1 if there was not enough room left to send the message,
1202 * any other negative returned value must be considered as an error with an appcxt st0
1203 * returned value equal to PEER_SESS_ST_END.
1204 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001205static inline int peer_send_msg(struct appctx *appctx,
1206 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1207 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001208{
1209 int ret, msglen;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001210 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001211
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001212 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001213 if (!msglen) {
1214 /* internal error: message does not fit in trash */
1215 appctx->st0 = PEER_SESS_ST_END;
1216 return 0;
1217 }
1218
1219 /* message to buffer */
Willy Tarreauda304902022-05-27 10:44:04 +02001220 ret = ci_putblk(sc_ic(sc), trash.area, msglen);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001221 if (ret <= 0) {
1222 if (ret == -1) {
1223 /* No more write possible */
Willy Tarreauda304902022-05-27 10:44:04 +02001224 sc_need_room(sc);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001225 return -1;
1226 }
1227 appctx->st0 = PEER_SESS_ST_END;
1228 }
1229
1230 return ret;
1231}
1232
1233/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001234 * Send a hello message.
1235 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1236 * Returns -1 if there was not enough room left to send the message,
1237 * any other negative returned value must be considered as an error with an appcxt st0
1238 * returned value equal to PEER_SESS_ST_END.
1239 */
1240static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1241{
1242 struct peer_prep_params p = {
1243 .hello.peer = peer,
1244 };
1245
1246 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1247}
1248
1249/*
1250 * Send a success peer handshake status message.
1251 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1252 * Returns -1 if there was not enough room left to send the message,
1253 * any other negative returned value must be considered as an error with an appcxt st0
1254 * returned value equal to PEER_SESS_ST_END.
1255 */
1256static inline int peer_send_status_successmsg(struct appctx *appctx)
1257{
1258 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1259}
1260
1261/*
1262 * Send a peer handshake status error message.
1263 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1264 * Returns -1 if there was not enough room left to send the message,
1265 * any other negative returned value must be considered as an error with an appcxt st0
1266 * returned value equal to PEER_SESS_ST_END.
1267 */
1268static inline int peer_send_status_errormsg(struct appctx *appctx)
1269{
1270 struct peer_prep_params p = {
1271 .error_status.st1 = appctx->st1,
1272 };
1273
1274 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1275}
1276
1277/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001278 * Send a stick-table switch message.
1279 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1280 * Returns -1 if there was not enough room left to send the message,
1281 * any other negative returned value must be considered as an error with an appcxt st0
1282 * returned value equal to PEER_SESS_ST_END.
1283 */
1284static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1285{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001286 struct peer_prep_params p = {
1287 .swtch.shared_table = st,
1288 };
1289
1290 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001291}
1292
1293/*
1294 * Send a stick-table update acknowledgement message.
1295 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1296 * Returns -1 if there was not enough room left to send the message,
1297 * any other negative returned value must be considered as an error with an appcxt st0
1298 * returned value equal to PEER_SESS_ST_END.
1299 */
1300static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1301{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001302 struct peer_prep_params p = {
1303 .ack.shared_table = st,
1304 };
1305
1306 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001307}
1308
1309/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001310 * Send a stick-table update message.
1311 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1312 * Returns -1 if there was not enough room left to send the message,
1313 * any other negative returned value must be considered as an error with an appcxt st0
1314 * returned value equal to PEER_SESS_ST_END.
1315 */
1316static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1317 unsigned int updateid, int use_identifier, int use_timed)
1318{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001319 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001320 .updt = {
1321 .stksess = ts,
1322 .shared_table = st,
1323 .updateid = updateid,
1324 .use_identifier = use_identifier,
1325 .use_timed = use_timed,
Willy Tarreau455caef2022-05-05 20:16:16 +02001326 .peer = appctx->svcctx,
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001327 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001328 };
1329
1330 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001331}
1332
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001333/*
1334 * Build a peer protocol control class message.
1335 * Returns the number of written bytes used to build the message if succeeded,
1336 * 0 if not.
1337 */
1338static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1339{
1340 if (size < sizeof p->control.head)
1341 return 0;
1342
1343 msg[0] = p->control.head[0];
1344 msg[1] = p->control.head[1];
1345
1346 return 2;
1347}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001348
1349/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001350 * Send a stick-table synchronization request message.
1351 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1352 * Returns -1 if there was not enough room left to send the message,
1353 * any other negative returned value must be considered as an error with an appctx st0
1354 * returned value equal to PEER_SESS_ST_END.
1355 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001356static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1357 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001358{
1359 struct peer_prep_params p = {
1360 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1361 };
1362
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001363 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1364 NULL, &p.control.head[1], peers->local->id, peer->id);
1365
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001366 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1367}
1368
1369/*
1370 * Send a stick-table synchronization confirmation message.
1371 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1372 * Returns -1 if there was not enough room left to send the message,
1373 * any other negative returned value must be considered as an error with an appctx st0
1374 * returned value equal to PEER_SESS_ST_END.
1375 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001376static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1377 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001378{
1379 struct peer_prep_params p = {
1380 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1381 };
1382
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001383 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1384 NULL, &p.control.head[1], peers->local->id, peer->id);
1385
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001386 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1387}
1388
1389/*
1390 * Send a stick-table synchronization finished message.
1391 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1392 * Returns -1 if there was not enough room left to send the message,
1393 * any other negative returned value must be considered as an error with an appctx st0
1394 * returned value equal to PEER_SESS_ST_END.
1395 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001396static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1397 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001398{
1399 struct peer_prep_params p = {
1400 .control.head = { PEER_MSG_CLASS_CONTROL, },
1401 };
1402
Emeric Brun70de43b2020-03-16 10:51:01 +01001403 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001404 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1405
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001406 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1407 NULL, &p.control.head[1], peers->local->id, peer->id);
1408
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001409 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1410}
1411
1412/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001413 * Send a heartbeat message.
1414 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1415 * Returns -1 if there was not enough room left to send the message,
1416 * any other negative returned value must be considered as an error with an appctx st0
1417 * returned value equal to PEER_SESS_ST_END.
1418 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001419static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1420 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001421{
1422 struct peer_prep_params p = {
1423 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1424 };
1425
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001426 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1427 NULL, &p.control.head[1], peers->local->id, peer->id);
1428
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001429 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1430}
1431
1432/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001433 * Build a peer protocol error class message.
1434 * Returns the number of written bytes used to build the message if succeeded,
1435 * 0 if not.
1436 */
1437static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1438{
1439 if (size < sizeof p->error.head)
1440 return 0;
1441
1442 msg[0] = p->error.head[0];
1443 msg[1] = p->error.head[1];
1444
1445 return 2;
1446}
1447
1448/*
1449 * Send a "size limit reached" error message.
1450 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1451 * Returns -1 if there was not enough room left to send the message,
1452 * any other negative returned value must be considered as an error with an appctx st0
1453 * returned value equal to PEER_SESS_ST_END.
1454 */
1455static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1456{
1457 struct peer_prep_params p = {
1458 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1459 };
1460
1461 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1462}
1463
1464/*
1465 * Send a "peer protocol" error message.
1466 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1467 * Returns -1 if there was not enough room left to send the message,
1468 * any other negative returned value must be considered as an error with an appctx st0
1469 * returned value equal to PEER_SESS_ST_END.
1470 */
1471static inline int peer_send_error_protomsg(struct appctx *appctx)
1472{
1473 struct peer_prep_params p = {
1474 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1475 };
1476
1477 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1478}
1479
1480/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001481 * Function used to lookup for recent stick-table updates associated with
1482 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1483 */
1484static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1485{
1486 struct eb32_node *eb;
1487
1488 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1489 if (!eb) {
1490 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001491 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001492 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1493 return NULL;
1494 }
1495 }
1496
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001497 /* if distance between the last pushed and the retrieved key
1498 * is greater than the distance last_pushed and the local_update
1499 * this means we are beyond localupdate.
1500 */
1501 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001502 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1503 return NULL;
1504 }
1505
1506 return eb32_entry(eb, struct stksess, upd);
1507}
1508
1509/*
1510 * Function used to lookup for recent stick-table updates associated with
1511 * <st> shared stick-table during teach state 1 step.
1512 */
1513static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1514{
1515 struct eb32_node *eb;
1516
1517 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1518 if (!eb) {
1519 st->flags |= SHTABLE_F_TEACH_STAGE1;
1520 eb = eb32_first(&st->table->updates);
1521 if (eb)
1522 st->last_pushed = eb->key - 1;
1523 return NULL;
1524 }
1525
1526 return eb32_entry(eb, struct stksess, upd);
1527}
1528
1529/*
1530 * Function used to lookup for recent stick-table updates associated with
1531 * <st> shared stick-table during teach state 2 step.
1532 */
1533static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1534{
1535 struct eb32_node *eb;
1536
1537 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1538 if (!eb || eb->key > st->teaching_origin) {
1539 st->flags |= SHTABLE_F_TEACH_STAGE2;
1540 return NULL;
1541 }
1542
1543 return eb32_entry(eb, struct stksess, upd);
1544}
1545
1546/*
1547 * Generic function to emit update messages for <st> stick-table when a lesson must
1548 * be taught to the peer <p>.
1549 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1550 * this function, 0 if not.
1551 *
1552 * This function temporary unlock/lock <st> when it sends stick-table updates or
1553 * when decrementing its refcount in case of any error when it sends this updates.
1554 *
1555 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1556 * Returns -1 if there was not enough room left to send the message,
1557 * any other negative returned value must be considered as an error with an appcxt st0
1558 * returned value equal to PEER_SESS_ST_END.
1559 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1560 * unlocked if not already locked when entering this function.
1561 */
1562static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1563 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1564 struct shared_table *st, int locked)
1565{
1566 int ret, new_pushed, use_timed;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001567 int updates_sent = 0;
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001568
1569 ret = 1;
1570 use_timed = 0;
1571 if (st != p->last_local_table) {
1572 ret = peer_send_switchmsg(st, appctx);
1573 if (ret <= 0)
1574 return ret;
1575
1576 p->last_local_table = st;
1577 }
1578
1579 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1580 use_timed = !(p->flags & PEER_F_DWNGRD);
1581
1582 /* We force new pushed to 1 to force identifier in update message */
1583 new_pushed = 1;
1584
1585 if (!locked)
1586 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1587
1588 while (1) {
1589 struct stksess *ts;
1590 unsigned updateid;
1591
1592 /* push local updates */
1593 ts = peer_stksess_lookup(st);
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001594 if (!ts) {
1595 ret = 1; // done
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001596 break;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001597 }
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001598
1599 updateid = ts->upd.key;
1600 ts->ref_cnt++;
1601 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1602
1603 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1604 if (ret <= 0) {
1605 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1606 ts->ref_cnt--;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001607 break;
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001608 }
1609
1610 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1611 ts->ref_cnt--;
1612 st->last_pushed = updateid;
1613
1614 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1615 (int)(st->last_pushed - st->table->commitupdate) > 0)
1616 st->table->commitupdate = st->last_pushed;
1617
1618 /* identifier may not needed in next update message */
1619 new_pushed = 0;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001620
1621 updates_sent++;
1622 if (updates_sent >= peers_max_updates_at_once) {
1623 /* pretend we're full so that we get back ASAP */
1624 struct stconn *sc = appctx_sc(appctx);
1625
1626 sc_need_room(sc);
1627 ret = -1;
1628 break;
1629 }
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001630 }
1631
1632 out:
1633 if (!locked)
1634 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Willy Tarreau8bd146d2022-07-19 20:17:38 +02001635 return ret;
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001636}
1637
1638/*
1639 * Function to emit update messages for <st> stick-table when a lesson must
1640 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1641 *
1642 * Note that <st> shared stick-table is locked when calling this function.
1643 *
1644 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1645 * Returns -1 if there was not enough room left to send the message,
1646 * any other negative returned value must be considered as an error with an appcxt st0
1647 * returned value equal to PEER_SESS_ST_END.
1648 */
1649static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1650 struct shared_table *st)
1651{
1652 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1653}
1654
1655/*
1656 * Function to emit update messages for <st> stick-table when a lesson must
1657 * be taught to the peer <p> during teach state 1 step.
1658 *
1659 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1660 * Returns -1 if there was not enough room left to send the message,
1661 * any other negative returned value must be considered as an error with an appcxt st0
1662 * returned value equal to PEER_SESS_ST_END.
1663 */
1664static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1665 struct shared_table *st)
1666{
1667 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1668}
1669
1670/*
1671 * Function to emit update messages for <st> stick-table when a lesson must
1672 * be taught to the peer <p> during teach state 1 step.
1673 *
1674 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1675 * Returns -1 if there was not enough room left to send the message,
1676 * any other negative returned value must be considered as an error with an appcxt st0
1677 * returned value equal to PEER_SESS_ST_END.
1678 */
1679static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1680 struct shared_table *st)
1681{
1682 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1683}
1684
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001685
1686/*
1687 * Function used to parse a stick-table update message after it has been received
1688 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1689 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1690 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1691 * was encountered.
1692 * <exp> must be set if the stick-table entry expires.
1693 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001694 * 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 +01001695 * update ID.
1696 * <totl> is the length of the stick-table update message computed upon receipt.
1697 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001698static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1699 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001700{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001701 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001702 struct shared_table *st = p->remote_table;
1703 struct stksess *ts, *newts;
1704 uint32_t update;
1705 int expire;
1706 unsigned int data_type;
1707 void *data_ptr;
1708
Frédéric Lécailled8659352020-11-10 16:18:03 +01001709 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001710 /* Here we have data message */
1711 if (!st)
1712 goto ignore_msg;
1713
1714 expire = MS_TO_TICKS(st->table->expire);
1715
1716 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001717 if (msg_len < sizeof(update)) {
1718 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001719 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001720 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001721
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001722 memcpy(&update, *msg_cur, sizeof(update));
1723 *msg_cur += sizeof(update);
1724 st->last_get = htonl(update);
1725 }
1726 else {
1727 st->last_get++;
1728 }
1729
1730 if (exp) {
1731 size_t expire_sz = sizeof expire;
1732
Frédéric Lécailled8659352020-11-10 16:18:03 +01001733 if (*msg_cur + expire_sz > msg_end) {
1734 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1735 NULL, p, *msg_cur);
1736 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1737 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001738 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001739 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001740
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001741 memcpy(&expire, *msg_cur, expire_sz);
1742 *msg_cur += expire_sz;
1743 expire = ntohl(expire);
1744 }
1745
1746 newts = stksess_new(st->table, NULL);
1747 if (!newts)
1748 goto ignore_msg;
1749
1750 if (st->table->type == SMP_T_STR) {
1751 unsigned int to_read, to_store;
1752
1753 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001754 if (!*msg_cur) {
1755 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001756 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001757 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001758
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001759 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001760 if (*msg_cur + to_store > msg_end) {
1761 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1762 NULL, p, *msg_cur);
1763 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1764 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001765 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001766 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001767
1768 memcpy(newts->key.key, *msg_cur, to_store);
1769 newts->key.key[to_store] = 0;
1770 *msg_cur += to_read;
1771 }
1772 else if (st->table->type == SMP_T_SINT) {
1773 unsigned int netinteger;
1774
Frédéric Lécailled8659352020-11-10 16:18:03 +01001775 if (*msg_cur + sizeof(netinteger) > msg_end) {
1776 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1777 NULL, p, *msg_cur);
1778 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1779 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001780 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001781 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001782
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001783 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1784 netinteger = ntohl(netinteger);
1785 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1786 *msg_cur += sizeof(netinteger);
1787 }
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
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001797 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1798 *msg_cur += st->table->key_size;
1799 }
1800
1801 /* lookup for existing entry */
1802 ts = stktable_set_entry(st->table, newts);
1803 if (ts != newts) {
1804 stksess_free(st->table, newts);
1805 newts = NULL;
1806 }
1807
1808 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1809
1810 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001811 uint64_t decoded_int;
Emeric Brun90a9b672021-06-22 16:09:55 +02001812 unsigned int idx;
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001813 int ignore;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001814
Emeric Brun08b0f672021-07-01 18:54:05 +02001815 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001816 continue;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001817
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001818 ignore = stktable_data_types[data_type].is_local;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001819
Emeric Brun90a9b672021-06-22 16:09:55 +02001820 if (stktable_data_types[data_type].is_array) {
1821 /* in case of array all elements
1822 * use the same std_type and they
1823 * are linearly encoded.
1824 * The number of elements was provided
1825 * by table definition message
1826 */
1827 switch (stktable_data_types[data_type].std_type) {
1828 case STD_T_SINT:
1829 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1830 decoded_int = intdecode(msg_cur, msg_end);
1831 if (!*msg_cur) {
1832 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1833 goto malformed_unlock;
1834 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001835
Emeric Brun90a9b672021-06-22 16:09:55 +02001836 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001837 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001838 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
1839 }
1840 break;
1841 case STD_T_UINT:
1842 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1843 decoded_int = intdecode(msg_cur, msg_end);
1844 if (!*msg_cur) {
1845 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1846 goto malformed_unlock;
1847 }
1848
1849 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001850 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001851 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
1852 }
1853 break;
1854 case STD_T_ULL:
1855 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1856 decoded_int = intdecode(msg_cur, msg_end);
1857 if (!*msg_cur) {
1858 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1859 goto malformed_unlock;
1860 }
1861
1862 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001863 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001864 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
1865 }
1866 break;
1867 case STD_T_FRQP:
1868 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1869 struct freq_ctr data;
1870
1871 /* First bit is reserved for the freq_ctr lock
1872 * Note: here we're still protected by the stksess lock
1873 * so we don't need to update the update the freq_ctr
1874 * using its internal lock.
1875 */
1876
1877 decoded_int = intdecode(msg_cur, msg_end);
1878 if (!*msg_cur) {
1879 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1880 goto malformed_unlock;
1881 }
1882
1883 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
1884 data.curr_ctr = intdecode(msg_cur, msg_end);
1885 if (!*msg_cur) {
1886 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1887 goto malformed_unlock;
1888 }
1889
1890 data.prev_ctr = intdecode(msg_cur, msg_end);
1891 if (!*msg_cur) {
1892 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1893 goto malformed_unlock;
1894 }
1895
1896 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001897 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001898 stktable_data_cast(data_ptr, std_t_frqp) = data;
1899 }
1900 break;
1901 }
1902
1903 /* array is fully decoded
1904 * proceed next data_type.
1905 */
1906 continue;
1907 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001908 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001909 if (!*msg_cur) {
1910 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001911 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001912 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001913
Willy Tarreau1e82a142019-01-29 11:08:06 +01001914 switch (stktable_data_types[data_type].std_type) {
1915 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001916 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001917 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001918 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001919 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001920
Willy Tarreau1e82a142019-01-29 11:08:06 +01001921 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001922 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001923 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001924 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001925 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001926
Willy Tarreau1e82a142019-01-29 11:08:06 +01001927 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001928 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001929 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001930 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001931 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001932
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001933 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001934 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001935
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001936 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001937 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001938 so we don't need to update the update the freq_ctr
Emeric Brun90a9b672021-06-22 16:09:55 +02001939 using its internal lock.
1940 */
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001941
Willy Tarreau1e82a142019-01-29 11:08:06 +01001942 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001943 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001944 if (!*msg_cur) {
1945 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001946 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001947 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001948
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001949 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001950 if (!*msg_cur) {
1951 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001952 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001953 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001954
1955 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001956 if (data_ptr && !ignore)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001957 stktable_data_cast(data_ptr, std_t_frqp) = data;
1958 break;
1959 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001960 case STD_T_DICT: {
1961 struct buffer *chunk;
1962 size_t data_len, value_len;
1963 unsigned int id;
1964 struct dict_entry *de;
1965 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001966 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001967
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001968 if (!decoded_int) {
1969 /* No entry. */
1970 break;
1971 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001972 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001973 if (*msg_cur + data_len > msg_end) {
1974 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1975 NULL, p, *msg_cur);
1976 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1977 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001978 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001979 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001980
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001981 /* Compute the end of the current data, <msg_end> being at the end of
1982 * the entire message.
1983 */
1984 end = *msg_cur + data_len;
1985 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001986 if (!*msg_cur || !id) {
1987 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1988 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001989 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001990 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001991
1992 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001993 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001994 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001995 if (id > dc->max_entries) {
1996 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1997 NULL, p, NULL, &id);
1998 goto malformed_unlock;
1999 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002000 /* IDs sent over the network are numbered from 1. */
2001 de = dc->rx[id - 1].de;
2002 }
2003 else {
2004 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02002005 value_len = intdecode(msg_cur, end);
2006 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01002007 unlikely(value_len + 1 >= chunk->size)) {
2008 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
2009 NULL, p, *msg_cur, &value_len);
2010 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
2011 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002012 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002013 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002014
2015 chunk_memcpy(chunk, *msg_cur, value_len);
2016 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02002017 *msg_cur += value_len;
2018
Thayne McCombs92149f92020-11-20 01:28:26 -07002019 de = dict_insert(&server_key_dict, chunk->area);
2020 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002021 dc->rx[id - 1].de = de;
2022 }
2023 if (de) {
2024 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01002025 if (data_ptr && !ignore) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002026 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002027 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07002028 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002029 }
2030 break;
2031 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002032 }
2033 }
2034 /* Force new expiration */
2035 ts->expire = tick_add(now_ms, expire);
2036
2037 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2038 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002039 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002040 return 1;
2041
2042 ignore_msg:
2043 /* skip consumed message */
Willy Tarreauda304902022-05-27 10:44:04 +02002044 co_skip(sc_oc(sc), totl);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002045 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002046 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01002047
2048 malformed_unlock:
2049 /* malformed message */
2050 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2051 stktable_touch_remote(st->table, ts, 1);
2052 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002053 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002054 return 0;
2055
2056 malformed_free_newts:
2057 /* malformed message */
2058 stksess_free(st->table, newts);
2059 malformed_exit:
2060 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002061 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002062 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002063}
2064
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01002065/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002066 * Function used to parse a stick-table update acknowledgement message after it
2067 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
2068 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2069 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2070 * was encountered.
2071 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2072 */
2073static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
2074 char **msg_cur, char *msg_end)
2075{
2076 /* ack message */
2077 uint32_t table_id ;
2078 uint32_t update;
2079 struct shared_table *st;
2080
Emeric Brunb0d60be2021-03-04 10:27:10 +01002081 /* ignore ack during teaching process */
2082 if (p->flags & PEER_F_TEACH_PROCESS)
2083 return 1;
2084
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002085 table_id = intdecode(msg_cur, msg_end);
2086 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
2087 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002088
2089 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
2090 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002091 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2092 return 0;
2093 }
2094
2095 memcpy(&update, *msg_cur, sizeof(update));
2096 update = ntohl(update);
2097
2098 for (st = p->tables; st; st = st->next) {
2099 if (st->local_id == table_id) {
2100 st->update = update;
2101 break;
2102 }
2103 }
2104
2105 return 1;
2106}
2107
2108/*
2109 * Function used to parse a stick-table switch message after it has been received
2110 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2111 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2112 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2113 * was encountered.
2114 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2115 */
2116static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
2117 char **msg_cur, char *msg_end)
2118{
2119 struct shared_table *st;
2120 int table_id;
2121
2122 table_id = intdecode(msg_cur, msg_end);
2123 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002124 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002125 /* malformed message */
2126 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2127 return 0;
2128 }
2129
2130 p->remote_table = NULL;
2131 for (st = p->tables; st; st = st->next) {
2132 if (st->remote_id == table_id) {
2133 p->remote_table = st;
2134 break;
2135 }
2136 }
2137
2138 return 1;
2139}
2140
2141/*
2142 * Function used to parse a stick-table definition message after it has been received
2143 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2144 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2145 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2146 * was encountered.
2147 * <totl> is the length of the stick-table update message computed upon receipt.
2148 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2149 */
2150static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
2151 char **msg_cur, char *msg_end, int totl)
2152{
Willy Tarreauc12b3212022-05-27 11:08:15 +02002153 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002154 int table_id_len;
2155 struct shared_table *st;
2156 int table_type;
2157 int table_keylen;
2158 int table_id;
2159 uint64_t table_data;
2160
2161 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002162 if (!*msg_cur) {
2163 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002164 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002165 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002166
2167 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002168 if (!*msg_cur) {
2169 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002170 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002171 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002172
2173 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002174 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
2175 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002176 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002177 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002178
2179 for (st = p->tables; st; st = st->next) {
2180 /* Reset IDs */
2181 if (st->remote_id == table_id)
2182 st->remote_id = 0;
2183
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01002184 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
2185 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002186 p->remote_table = st;
2187 }
2188
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002189 if (!p->remote_table) {
2190 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002191 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002192 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002193
2194 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002195 if (*msg_cur >= msg_end) {
2196 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002197 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002198 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002199
2200 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002201 if (!*msg_cur) {
2202 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002203 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002204 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002205
2206 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002207 if (!*msg_cur) {
2208 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002209 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002210 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002211
2212 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002213 if (!*msg_cur) {
2214 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002215 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002216 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002217
Emeric Brun530ba382020-06-02 11:17:42 +02002218 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002219 || p->remote_table->table->key_size != table_keylen) {
2220 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002221 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002222 goto ignore_msg;
2223 }
2224
Ilya Shipitsin01881082021-08-07 14:41:56 +05002225 /* Check if there there is the additional expire data */
Emeric Brun90a9b672021-06-22 16:09:55 +02002226 intdecode(msg_cur, msg_end);
2227 if (*msg_cur) {
2228 uint64_t data_type;
2229 uint64_t type;
2230
2231 /* This define contains the expire data so we consider
2232 * it also contain all data_types parameters.
2233 */
2234 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2235 if (table_data & (1ULL << data_type)) {
2236 if (stktable_data_types[data_type].is_array) {
2237 /* This should be an array
2238 * so we parse the data_type prefix
2239 * because we must have parameters.
2240 */
2241 type = intdecode(msg_cur, msg_end);
2242 if (!*msg_cur) {
2243 p->remote_table = NULL;
2244 TRACE_PROTO("missing meta data for array", PEERS_EV_DEFMSG, NULL, p);
2245 goto ignore_msg;
2246 }
2247
2248 /* check if the data_type match the current from the bitfield */
2249 if (type != data_type) {
2250 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002251 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002252 goto ignore_msg;
2253 }
2254
2255 /* decode the nbelem of the array */
2256 p->remote_table->remote_data_nbelem[type] = intdecode(msg_cur, msg_end);
2257 if (!*msg_cur) {
2258 p->remote_table = NULL;
2259 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2260 goto ignore_msg;
2261 }
2262
2263 /* if it is an array of frqp, we must also have the period to decode */
2264 if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2265 intdecode(msg_cur, msg_end);
2266 if (!*msg_cur) {
2267 p->remote_table = NULL;
2268 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2269 goto ignore_msg;
2270 }
2271 }
2272 }
2273 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2274 /* This should be a std freq counter data_type
2275 * so we parse the data_type prefix
2276 * because we must have parameters.
2277 */
2278 type = intdecode(msg_cur, msg_end);
2279 if (!*msg_cur) {
2280 p->remote_table = NULL;
2281 TRACE_PROTO("missing meta data for frqp", PEERS_EV_DEFMSG, NULL, p);
2282 goto ignore_msg;
2283 }
2284
2285 /* check if the data_type match the current from the bitfield */
2286 if (type != data_type) {
2287 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002288 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002289 goto ignore_msg;
2290 }
2291
2292 /* decode the period */
2293 intdecode(msg_cur, msg_end);
2294 if (!*msg_cur) {
2295 p->remote_table = NULL;
2296 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2297 goto ignore_msg;
2298 }
2299 }
2300 }
2301 }
2302 }
2303 else {
2304 uint64_t data_type;
2305
2306 /* There is not additional data but
2307 * array size parameter is mandatory to parse array
2308 * so we consider an error if an array data_type is define
2309 * but there is no additional data.
2310 */
2311 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2312 if (table_data & (1ULL << data_type)) {
2313 if (stktable_data_types[data_type].is_array) {
2314 p->remote_table = NULL;
2315 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2316 goto ignore_msg;
2317 }
2318 }
2319 }
2320 }
2321
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002322 p->remote_table->remote_data = table_data;
2323 p->remote_table->remote_id = table_id;
2324 return 1;
2325
2326 ignore_msg:
Willy Tarreauda304902022-05-27 10:44:04 +02002327 co_skip(sc_oc(sc), totl);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002328 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01002329
2330 malformed_exit:
2331 /* malformed message */
2332 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2333 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002334}
2335
2336/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002337 * Receive a stick-table message or pre-parse any other message.
2338 * The message's header will be sent into <msg_head> which must be at least
2339 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
2340 * The first two bytes are always read, and the rest is only read if the
2341 * first bytes indicate a stick-table message. If the message is a stick-table
2342 * message, the varint is decoded and the equivalent number of bytes will be
2343 * copied into the trash at trash.area. <totl> is incremented by the number of
2344 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002345 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
2346 * -1 if there was an error updating the appctx state st0 accordingly.
2347 */
2348static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2349 uint32_t *msg_len, int *totl)
2350{
2351 int reql;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002352 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002353 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002354
Willy Tarreauda304902022-05-27 10:44:04 +02002355 reql = co_getblk(sc_oc(sc), msg_head, 2 * sizeof(char), *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002356 if (reql <= 0) /* closed or EOL not found */
2357 goto incomplete;
2358
2359 *totl += reql;
2360
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002361 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002362 return 1;
2363
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002364 /* This is a stick-table message, let's go on */
2365
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002366 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002367 msg_head += *totl;
2368 msg_head_sz -= *totl;
Willy Tarreauda304902022-05-27 10:44:04 +02002369 reql = co_data(sc_oc(sc)) - *totl;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002370 if (reql > msg_head_sz)
2371 reql = msg_head_sz;
2372
Willy Tarreauda304902022-05-27 10:44:04 +02002373 reql = co_getblk(sc_oc(sc), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002374 if (reql <= 0) /* closed */
2375 goto incomplete;
2376
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002377 cur = msg_head;
2378 *msg_len = intdecode(&cur, cur + reql);
2379 if (!cur) {
2380 /* the number is truncated, did we read enough ? */
2381 if (reql < msg_head_sz)
2382 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002383
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002384 /* malformed message */
2385 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2386 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2387 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002388 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002389 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002390
2391 /* Read message content */
2392 if (*msg_len) {
2393 if (*msg_len > trash.size) {
2394 /* Status code is not success, abort */
2395 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2396 return -1;
2397 }
2398
Willy Tarreauda304902022-05-27 10:44:04 +02002399 reql = co_getblk(sc_oc(sc), trash.area, *msg_len, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002400 if (reql <= 0) /* closed */
2401 goto incomplete;
2402 *totl += reql;
2403 }
2404
2405 return 1;
2406
2407 incomplete:
Willy Tarreauda304902022-05-27 10:44:04 +02002408 if (reql < 0 || (sc_oc(sc)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002409 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002410 appctx->st0 = PEER_SESS_ST_END;
2411 return -1;
2412 }
2413
2414 return 0;
2415}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002416
2417/*
2418 * Treat the awaited message with <msg_head> as header.*
2419 * Return 1 if succeeded, 0 if not.
2420 */
2421static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2422 char **msg_cur, char *msg_end, int msg_len, int totl)
2423{
Christopher Faulet387e7972022-05-12 14:47:52 +02002424 struct peers *peers = peer->peers;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002425
2426 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2427 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2428 struct shared_table *st;
2429 /* Reset message: remote need resync */
2430
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002431 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2432 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002433 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002434 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002435 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002436 st->flags = 0;
2437 }
2438
2439 /* reset teaching flags to 0 */
2440 peer->flags &= PEER_TEACH_RESET;
2441
2442 /* flag to start to teach lesson */
2443 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002444 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002445 }
2446 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002447 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2448 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002449 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2450 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2451 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2452 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002453 if (peer->local)
2454 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2455 else
2456 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002457 }
2458 peer->confirm++;
2459 }
2460 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002461 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2462 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002463 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2464 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2465 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2466
Emeric Brunccdfbae2021-04-28 12:59:35 +02002467 if (peer->local)
2468 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2469 else
2470 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002471 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002472 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002473 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2474 }
2475 peer->confirm++;
2476 }
2477 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2478 struct shared_table *st;
2479
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002480 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2481 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002482 /* If stopping state */
2483 if (stopping) {
2484 /* Close session, push resync no more needed */
2485 peer->flags |= PEER_F_TEACH_COMPLETE;
2486 appctx->st0 = PEER_SESS_ST_END;
2487 return 0;
2488 }
2489 for (st = peer->tables; st; st = st->next) {
2490 st->update = st->last_pushed = st->teaching_origin;
2491 st->flags = 0;
2492 }
2493
2494 /* reset teaching flags to 0 */
2495 peer->flags &= PEER_TEACH_RESET;
2496 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002497 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002498 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2499 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002500 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002501 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002502 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002503 }
2504 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2505 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2506 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2507 return 0;
2508 }
2509 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2510 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2511 return 0;
2512 }
2513 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2514 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2515 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2516 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2517 int update, expire;
2518
2519 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2520 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2521 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2522 msg_cur, msg_end, msg_len, totl))
2523 return 0;
2524
2525 }
2526 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2527 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2528 return 0;
2529 }
2530 }
2531 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2532 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2533 return 0;
2534 }
2535
2536 return 1;
2537}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002538
2539
2540/*
2541 * Send any message to <peer> peer.
2542 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002543 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002544 * to a peer state change (from the peer I/O handler point of view).
2545 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002546static inline int peer_send_msgs(struct appctx *appctx,
2547 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002548{
2549 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002550
2551 /* Need to request a resync */
2552 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2553 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2554 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2555
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002556 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002557 if (repl <= 0)
2558 return repl;
2559
2560 peers->flags |= PEERS_F_RESYNC_PROCESS;
2561 }
2562
2563 /* Nothing to read, now we start to write */
2564 if (peer->tables) {
2565 struct shared_table *st;
2566 struct shared_table *last_local_table;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02002567 int updates_sent = 0;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002568
2569 last_local_table = peer->last_local_table;
2570 if (!last_local_table)
2571 last_local_table = peer->tables;
2572 st = last_local_table->next;
2573
2574 while (1) {
2575 if (!st)
2576 st = peer->tables;
2577
2578 /* It remains some updates to ack */
2579 if (st->last_get != st->last_acked) {
2580 repl = peer_send_ackmsg(st, appctx);
2581 if (repl <= 0)
2582 return repl;
2583
2584 st->last_acked = st->last_get;
2585 }
2586
2587 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2588 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2589 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002590 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002591
2592 repl = peer_send_teach_process_msgs(appctx, peer, st);
2593 if (repl <= 0) {
2594 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2595 return repl;
2596 }
2597 }
2598 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2599 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002600 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002601 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2602 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2603 if (repl <= 0)
2604 return repl;
2605 }
2606
2607 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2608 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2609 if (repl <= 0)
2610 return repl;
2611 }
2612 }
2613
2614 if (st == last_local_table)
2615 break;
2616 st = st->next;
Willy Tarreau8bd146d2022-07-19 20:17:38 +02002617
2618 updates_sent++;
2619 if (updates_sent >= peers_max_updates_at_once) {
2620 /* pretend we're full so that we get back ASAP */
2621 struct stconn *sc = appctx_sc(appctx);
2622
2623 sc_need_room(sc);
2624 return -1;
2625 }
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002626 }
2627 }
2628
2629 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002630 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002631 if (repl <= 0)
2632 return repl;
2633
2634 /* flag finished message sent */
2635 peer->flags |= PEER_F_TEACH_FINISHED;
2636 }
2637
2638 /* Confirm finished or partial messages */
2639 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002640 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002641 if (repl <= 0)
2642 return repl;
2643
2644 peer->confirm--;
2645 }
2646
2647 return 1;
2648}
2649
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002650/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002651 * Read and parse a first line of a "hello" peer protocol message.
2652 * Returns 0 if could not read a line, -1 if there was a read error or
2653 * the line is malformed, 1 if succeeded.
2654 */
2655static inline int peer_getline_version(struct appctx *appctx,
2656 unsigned int *maj_ver, unsigned int *min_ver)
2657{
2658 int reql;
2659
2660 reql = peer_getline(appctx);
2661 if (!reql)
2662 return 0;
2663
2664 if (reql < 0)
2665 return -1;
2666
2667 /* test protocol */
2668 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2669 appctx->st0 = PEER_SESS_ST_EXIT;
2670 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2671 return -1;
2672 }
2673 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2674 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2675 appctx->st0 = PEER_SESS_ST_EXIT;
2676 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2677 return -1;
2678 }
2679
2680 return 1;
2681}
2682
2683/*
2684 * Read and parse a second line of a "hello" peer protocol message.
2685 * Returns 0 if could not read a line, -1 if there was a read error or
2686 * the line is malformed, 1 if succeeded.
2687 */
2688static inline int peer_getline_host(struct appctx *appctx)
2689{
2690 int reql;
2691
2692 reql = peer_getline(appctx);
2693 if (!reql)
2694 return 0;
2695
2696 if (reql < 0)
2697 return -1;
2698
2699 /* test hostname match */
2700 if (strcmp(localpeer, trash.area) != 0) {
2701 appctx->st0 = PEER_SESS_ST_EXIT;
2702 appctx->st1 = PEER_SESS_SC_ERRHOST;
2703 return -1;
2704 }
2705
2706 return 1;
2707}
2708
2709/*
2710 * Read and parse a last line of a "hello" peer protocol message.
2711 * Returns 0 if could not read a character, -1 if there was a read error or
2712 * the line is malformed, 1 if succeeded.
2713 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2714 */
2715static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2716{
2717 char *p;
2718 int reql;
2719 struct peer *peer;
Willy Tarreau0698c802022-05-11 14:09:57 +02002720 struct stream *s = appctx_strm(appctx);
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002721 struct peers *peers = strm_fe(s)->parent;
2722
2723 reql = peer_getline(appctx);
2724 if (!reql)
2725 return 0;
2726
2727 if (reql < 0)
2728 return -1;
2729
2730 /* parse line "<peer name> <pid> <relative_pid>" */
2731 p = strchr(trash.area, ' ');
2732 if (!p) {
2733 appctx->st0 = PEER_SESS_ST_EXIT;
2734 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2735 return -1;
2736 }
2737 *p = 0;
2738
2739 /* lookup known peer */
2740 for (peer = peers->remote; peer; peer = peer->next) {
2741 if (strcmp(peer->id, trash.area) == 0)
2742 break;
2743 }
2744
2745 /* if unknown peer */
2746 if (!peer) {
2747 appctx->st0 = PEER_SESS_ST_EXIT;
2748 appctx->st1 = PEER_SESS_SC_ERRPEER;
2749 return -1;
2750 }
2751 *curpeer = peer;
2752
2753 return 1;
2754}
2755
2756/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002757 * Init <peer> peer after having accepted it at peer protocol level.
2758 */
2759static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2760{
2761 struct shared_table *st;
2762
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002763 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002764 /* Register status code */
2765 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002766 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002767
2768 /* Awake main task */
2769 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2770
2771 /* Init confirm counter */
2772 peer->confirm = 0;
2773
2774 /* Init cursors */
2775 for (st = peer->tables; st ; st = st->next) {
2776 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002777 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2778 /* if st->update appears to be in future it means
2779 * that the last acked value is very old and we
2780 * remain unconnected a too long time to use this
2781 * acknowlegement as a reset.
2782 * We should update the protocol to be able to
2783 * signal the remote peer that it needs a full resync.
2784 * Here a partial fix consist to set st->update at
2785 * the max past value
2786 */
2787 if ((int)(st->table->localupdate - st->update) < 0)
2788 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002789 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002790 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002791 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2792 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002793 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002794 }
2795
2796 /* reset teaching and learning flags to 0 */
2797 peer->flags &= PEER_TEACH_RESET;
2798 peer->flags &= PEER_LEARN_RESET;
2799
2800 /* if current peer is local */
2801 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002802 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002803 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2804 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2805 /* assign local peer for a lesson, consider lesson already requested */
2806 peer->flags |= PEER_F_LEARN_ASSIGN;
2807 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002808 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002809 }
2810
2811 }
2812 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2813 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2814 /* assign peer for a lesson */
2815 peer->flags |= PEER_F_LEARN_ASSIGN;
2816 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002817 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002818 }
2819}
2820
2821/*
2822 * Init <peer> peer after having connected it at peer protocol level.
2823 */
2824static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2825{
2826 struct shared_table *st;
2827
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002828 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002829 /* Init cursors */
2830 for (st = peer->tables; st ; st = st->next) {
2831 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002832 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2833 /* if st->update appears to be in future it means
2834 * that the last acked value is very old and we
2835 * remain unconnected a too long time to use this
2836 * acknowlegement as a reset.
2837 * We should update the protocol to be able to
2838 * signal the remote peer that it needs a full resync.
2839 * Here a partial fix consist to set st->update at
2840 * the max past value.
2841 */
2842 if ((int)(st->table->localupdate - st->update) < 0)
2843 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002844 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002845 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002846 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2847 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002848 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002849 }
2850
2851 /* Init confirm counter */
2852 peer->confirm = 0;
2853
2854 /* reset teaching and learning flags to 0 */
2855 peer->flags &= PEER_TEACH_RESET;
2856 peer->flags &= PEER_LEARN_RESET;
2857
2858 /* If current peer is local */
2859 if (peer->local) {
2860 /* flag to start to teach lesson */
2861 peer->flags |= PEER_F_TEACH_PROCESS;
2862 }
2863 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2864 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2865 /* If peer is remote and resync from remote is needed,
2866 and no peer currently assigned */
2867
2868 /* assign peer for a lesson */
2869 peer->flags |= PEER_F_LEARN_ASSIGN;
2870 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002871 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002872 }
2873}
2874
2875/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002876 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002877 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002878static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002879{
Willy Tarreauc12b3212022-05-27 11:08:15 +02002880 struct stconn *sc = appctx_sc(appctx);
Willy Tarreauda304902022-05-27 10:44:04 +02002881 struct stream *s = __sc_strm(sc);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002882 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002883 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002884 int reql = 0;
2885 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002886 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002887 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002888
Joseph Herlant82b2f542018-11-15 12:19:14 -08002889 /* Check if the input buffer is available. */
Willy Tarreauda304902022-05-27 10:44:04 +02002890 if (sc_ib(sc)->size == 0) {
2891 sc_need_room(sc);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002892 goto out;
2893 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002894
Emeric Brun2b920a12010-09-23 18:30:22 +02002895 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002896 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002897switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002898 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002899 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002900 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002901 prev_state = appctx->st0;
Willy Tarreau455caef2022-05-05 20:16:16 +02002902 appctx->svcctx = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002903 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002904 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002905 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002906 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002907 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2908 if (reql <= 0) {
2909 if (!reql)
2910 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002911 goto switchstate;
2912 }
2913
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002914 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002915 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002916 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002917 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002918 reql = peer_getline_host(appctx);
2919 if (reql <= 0) {
2920 if (!reql)
2921 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002922 goto switchstate;
2923 }
2924
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002925 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002926 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002927 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002928 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002929 reql = peer_getline_last(appctx, &curpeer);
2930 if (reql <= 0) {
2931 if (!reql)
2932 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002933 goto switchstate;
2934 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002935
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002936 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002937 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002938 if (curpeer->local) {
2939 /* Local connection, reply a retry */
2940 appctx->st0 = PEER_SESS_ST_EXIT;
2941 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2942 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002943 }
Emeric Brun80527f52017-06-19 17:46:37 +02002944
2945 /* we're killing a connection, we must apply a random delay before
2946 * retrying otherwise the other end will do the same and we can loop
2947 * for a while.
2948 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002949 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002950 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002951 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002952 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002953 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002954 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2955 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2956 curpeer->flags |= PEER_F_DWNGRD;
2957 }
2958 else {
2959 curpeer->flags &= ~PEER_F_DWNGRD;
2960 }
2961 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002962 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002963 curpeer->flags |= PEER_F_ALIVE;
Willy Tarreau455caef2022-05-05 20:16:16 +02002964 appctx->svcctx = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002965 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002966 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002967 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002968 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002969 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002970 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002971 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02002972 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002973 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002974 if (curpeer->appctx != appctx) {
2975 appctx->st0 = PEER_SESS_ST_END;
2976 goto switchstate;
2977 }
2978 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002979
2980 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002981 if (repl <= 0) {
2982 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002983 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002984 goto switchstate;
2985 }
2986
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002987 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002988
Emeric Brun2b920a12010-09-23 18:30:22 +02002989 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002990 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002991 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002992 goto switchstate;
2993 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002994 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002995 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002996 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02002997 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002998 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002999 if (curpeer->appctx != appctx) {
3000 appctx->st0 = PEER_SESS_ST_END;
3001 goto switchstate;
3002 }
3003 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003004
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003005 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02003006 if (repl <= 0) {
3007 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003008 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003009 goto switchstate;
3010 }
3011
3012 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003013 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02003014 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003015 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003016 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003017 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003018 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02003019 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003020 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003021 if (curpeer->appctx != appctx) {
3022 appctx->st0 = PEER_SESS_ST_END;
3023 goto switchstate;
3024 }
3025 }
3026
Christopher Faulet642170a2022-07-27 10:49:31 +02003027 if (sc_ic(sc)->flags & CF_WROTE_DATA)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003028 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02003029
Frédéric Lécaillece025572019-01-21 13:38:06 +01003030 reql = peer_getline(appctx);
3031 if (!reql)
3032 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003033
Frédéric Lécaillece025572019-01-21 13:38:06 +01003034 if (reql < 0)
3035 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02003036
3037 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003038 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003039 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003040
3041 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003042 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02003043
3044 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003045 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01003046 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02003047 }
3048 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02003049 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
3050 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02003051 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003052 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02003053 goto switchstate;
3054 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003055 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003056 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02003057 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003058 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003059 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003060 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 char *msg_cur = trash.area;
3062 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01003063 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02003064 int totl = 0;
3065
Willy Tarreau2d372c22018-11-05 17:12:27 +01003066 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003067 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02003068 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003069 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003070 if (curpeer->appctx != appctx) {
3071 appctx->st0 = PEER_SESS_ST_END;
3072 goto switchstate;
3073 }
3074 }
3075
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003076 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
3077 if (reql <= 0) {
3078 if (reql == -1)
3079 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003080 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003081 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01003082
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003083 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01003084 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02003085 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003086
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003087 curpeer->flags |= PEER_F_ALIVE;
3088
Emeric Brun2b920a12010-09-23 18:30:22 +02003089 /* skip consumed message */
Willy Tarreauda304902022-05-27 10:44:04 +02003090 co_skip(sc_oc(sc), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02003091 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02003092 goto switchstate;
3093
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003094send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003095 if (curpeer->flags & PEER_F_HEARTBEAT) {
3096 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003097 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003098 if (repl <= 0) {
3099 if (repl == -1)
3100 goto out;
3101 goto switchstate;
3102 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01003103 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003104 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003105 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003106 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01003107 if (repl <= 0) {
3108 if (repl == -1)
3109 goto out;
3110 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02003111 }
3112
Emeric Brun2b920a12010-09-23 18:30:22 +02003113 /* noting more to do */
3114 goto out;
3115 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003116 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01003117 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003118 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003119 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003120 if (peer_send_status_errormsg(appctx) == -1)
3121 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003122 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003123 goto switchstate;
3124 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003125 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003126 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003127 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003128 if (peer_send_error_size_limitmsg(appctx) == -1)
3129 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003130 appctx->st0 = PEER_SESS_ST_END;
3131 goto switchstate;
3132 }
3133 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003134 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
3135 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003136 if (curpeer)
3137 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003138 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003139 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003140 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003141 if (peer_send_error_protomsg(appctx) == -1) {
3142 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003143 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003144 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003145 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003146 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003147 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003148 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003149 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003150 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003151 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003152 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003153 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003154 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003155 curpeer = NULL;
3156 }
Willy Tarreauda304902022-05-27 10:44:04 +02003157 sc_shutw(sc);
3158 sc_shutr(sc);
3159 sc_ic(sc)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02003160 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003161 }
3162 }
3163 }
3164out:
Willy Tarreauda304902022-05-27 10:44:04 +02003165 sc_oc(sc)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02003166
3167 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003168 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02003169 return;
3170}
3171
Willy Tarreau30576452015-04-13 13:50:30 +02003172static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003173 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003174 .name = "<PEER>", /* used for logging */
3175 .fct = peer_io_handler,
Christopher Faulet6712dc62022-05-12 15:36:11 +02003176 .init = peer_session_init,
Aman Gupta9a13e842012-04-02 18:57:53 -07003177 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003178};
Emeric Brun2b920a12010-09-23 18:30:22 +02003179
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003180
Emeric Brun2b920a12010-09-23 18:30:22 +02003181/*
3182 * Use this function to force a close of a peer session
3183 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003184static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003185{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003186 struct appctx *appctx = peer->appctx;
3187
Frédéric Lécaille5df11902017-06-13 16:39:57 +02003188 /* Note that the peer sessions which have just been created
3189 * (->st0 == PEER_SESS_ST_CONNECT) must not
3190 * be shutdown, if not, the TCP session will never be closed
3191 * and stay in CLOSE_WAIT state after having been closed by
3192 * the remote side.
3193 */
3194 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003195 return;
3196
Willy Tarreau81bc3b02016-10-31 17:37:39 +01003197 if (appctx->applet != &peer_applet)
3198 return;
3199
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003200 __peer_session_deinit(peer);
3201
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003202 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01003203 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003204}
3205
Willy Tarreau91d96282015-03-13 15:47:26 +01003206/* Pre-configures a peers frontend to accept incoming connections */
3207void peers_setup_frontend(struct proxy *fe)
3208{
3209 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02003210 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02003211 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01003212 fe->maxconn = 0;
3213 fe->conn_retries = CONN_RETRIES;
Christopher Faulet13db4bd2022-08-29 11:32:26 +02003214 fe->timeout.connect = MS_TO_TICKS(1000);
Willy Tarreau91d96282015-03-13 15:47:26 +01003215 fe->timeout.client = MS_TO_TICKS(5000);
Christopher Faulet13db4bd2022-08-29 11:32:26 +02003216 fe->timeout.server = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01003217 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01003218 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01003219 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
3220}
3221
Emeric Brun2b920a12010-09-23 18:30:22 +02003222/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01003223 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02003224 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003225static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003226{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003227 struct appctx *appctx;
Maciej Zdeb34e40852022-05-16 17:27:28 +02003228 unsigned int thr = 0;
3229 int idx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003230
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01003231 peer->new_conn++;
Christopher Fauletab4b0942022-07-26 19:14:36 +02003232 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 +02003233 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003234 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003235 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003236
Maciej Zdeb34e40852022-05-16 17:27:28 +02003237 for (idx = 0; idx < global.nbthread; idx++)
3238 thr = peers->applet_count[idx] < peers->applet_count[thr] ? idx : thr;
3239 appctx = appctx_new_on(&peer_applet, NULL, thr);
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003240 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003241 goto out_close;
Willy Tarreau455caef2022-05-05 20:16:16 +02003242 appctx->svcctx = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003243
Christopher Faulet6712dc62022-05-12 15:36:11 +02003244 appctx->st0 = PEER_SESS_ST_CONNECT;
3245 peer->appctx = appctx;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003246
Maciej Zdebd01be2a2022-05-16 17:26:20 +02003247 HA_ATOMIC_INC(&peers->applet_count[thr]);
Maciej Zdeb34e40852022-05-16 17:27:28 +02003248 appctx_wakeup(appctx);
Willy Tarreau9df94c22016-10-31 18:42:52 +01003249 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003250
Emeric Brun2b920a12010-09-23 18:30:22 +02003251 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01003252 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003253}
3254
3255/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003256 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01003257 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
3258 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02003259 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003260struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02003261{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003262 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003263 struct peer *ps;
3264 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02003265
3266 task->expire = TICK_ETERNITY;
3267
Emeric Brunb3971ab2015-05-12 18:49:09 +02003268 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003269 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003270 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003271 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02003272 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003273 return NULL;
3274 }
3275
Emeric Brun80527f52017-06-19 17:46:37 +02003276 /* Acquire lock for all peers of the section */
3277 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003278 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003279
Emeric Brun2b920a12010-09-23 18:30:22 +02003280 if (!stopping) {
3281 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02003282
Emeric Brun2c4ab412021-04-21 16:06:35 +02003283 /* resync timeout set to TICK_ETERNITY means we just start
3284 * a new process and timer was not initialized.
3285 * We must arm this timer to switch to a request to a remote
3286 * node if incoming connection from old local process never
3287 * comes.
3288 */
3289 if (peers->resync_timeout == TICK_ETERNITY)
3290 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3291
Emeric Brunb3971ab2015-05-12 18:49:09 +02003292 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
3293 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
3294 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003295 /* Resync from local peer needed
3296 no peer was assigned for the lesson
3297 and no old local peer found
3298 or resync timeout expire */
3299
3300 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003301 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003302 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003303
3304 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003305 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02003306 }
3307
3308 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003309 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003310 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003311 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003312 if (!ps->appctx) {
3313 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003314 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003315 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003316 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003317 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003318 tick_is_expired(ps->reconnect, now_ms))) {
3319 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003320 * or previous peer connection established with success
3321 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003322 * and reconnection timer is expired */
3323
3324 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003325 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003326 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003327 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003328 /* If previous session failed during connection
3329 * but reconnection timer is not expired */
3330
3331 /* reschedule task for reconnect */
3332 task->expire = tick_first(task->expire, ps->reconnect);
3333 }
3334 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003335 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003336 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003337 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003338 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3339 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003340 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3341 /* Resync from a remote is needed
3342 * and no peer was assigned for lesson
3343 * and current peer may be up2date */
3344
3345 /* assign peer for the lesson */
3346 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003347 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003348 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003349
Willy Tarreau9df94c22016-10-31 18:42:52 +01003350 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003351 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003352 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003353 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003354 int update_to_push = 0;
3355
Emeric Brunb3971ab2015-05-12 18:49:09 +02003356 /* Awake session if there is data to push */
3357 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003358 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003359 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003360 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003361 /* There is no need to send a heartbeat message
3362 * when some updates must be pushed. The remote
3363 * peer will consider <ps> peer as alive when it will
3364 * receive these updates.
3365 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003366 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003367 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003368 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003369 /* We are going to send updates, let's ensure we will
3370 * come back to send heartbeat messages or to reconnect.
3371 */
3372 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003373 appctx_wakeup(ps->appctx);
3374 break;
3375 }
3376 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003377 /* When there are updates to send we do not reconnect
3378 * and do not send heartbeat message either.
3379 */
3380 if (!update_to_push) {
3381 if (tick_is_expired(ps->reconnect, now_ms)) {
3382 if (ps->flags & PEER_F_ALIVE) {
3383 /* This peer was alive during a 'reconnect' period.
3384 * Flag it as not alive again for the next period.
3385 */
3386 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003387 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003388 }
3389 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003390 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003391 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003392 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003393 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003394 }
3395 }
3396 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3397 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3398 ps->flags |= PEER_F_HEARTBEAT;
3399 appctx_wakeup(ps->appctx);
3400 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003401 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003402 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003403 }
3404 /* else do nothing */
3405 } /* SUCCESSCODE */
3406 } /* !ps->peer->local */
3407 } /* for */
3408
3409 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003410 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3411 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3412 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003413 /* Resync from remote peer needed
3414 * no peer was assigned for the lesson
3415 * and resync timeout expire */
3416
3417 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003418 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003419 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003420 }
3421
Emeric Brunb3971ab2015-05-12 18:49:09 +02003422 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003423 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003424 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3425 if (!tick_is_expired(peers->resync_timeout, now_ms))
3426 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003427 }
3428 } /* !stopping */
3429 else {
3430 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003431 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003432 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003433 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003434 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003435 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003436 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003437
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003438 /* disconnect all connected peers to process a local sync
3439 * this must be done only the first time we are switching
3440 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003441 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003442 for (ps = peers->remote; ps; ps = ps->next) {
3443 /* we're killing a connection, we must apply a random delay before
3444 * retrying otherwise the other end will do the same and we can loop
3445 * for a while.
3446 */
3447 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3448 if (ps->appctx) {
3449 peer_session_forceshutdown(ps);
3450 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003451 }
Christopher Faulet160fff62022-07-26 19:19:18 +02003452
3453 /* Set resync timeout for the local peer and request a immediate reconnect */
3454 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3455 peers->local->reconnect = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003456 }
3457 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003458
Emeric Brunb3971ab2015-05-12 18:49:09 +02003459 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003460 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003461 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003462 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003463 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003464 peers->flags &= ~PEERS_F_DONOTSTOP;
3465 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003466 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003467 }
3468 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003469 else if (!ps->appctx) {
Christopher Faulet19a82b92022-08-26 18:40:46 +02003470 /* Re-arm resync timeout if necessary */
3471 if (!tick_isset(peers->resync_timeout))
3472 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3473
Willy Tarreau9df94c22016-10-31 18:42:52 +01003474 /* If there's no active peer connection */
Christopher Fauletb372f162022-08-26 18:46:16 +02003475 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED &&
3476 !tick_is_expired(peers->resync_timeout, now_ms) &&
Christopher Faulet160fff62022-07-26 19:19:18 +02003477 (ps->statuscode == 0 ||
3478 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3479 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3480 ps->statuscode == PEER_SESS_SC_TRYAGAIN)) {
Christopher Fauletb372f162022-08-26 18:46:16 +02003481 /* The resync is finished for the local peer and
3482 * the resync timeout is not expired and
Christopher Faulet160fff62022-07-26 19:19:18 +02003483 * connection never tried
3484 * or previous peer connection was successfully established
3485 * or previous tcp connect succeeded but init state incomplete
3486 * or during previous connect, peer replies a try again statuscode */
Emeric Brun2b920a12010-09-23 18:30:22 +02003487
Christopher Faulet160fff62022-07-26 19:19:18 +02003488 if (!tick_is_expired(ps->reconnect, now_ms)) {
3489 /* reconnection timer is not expired. reschedule task for reconnect */
3490 task->expire = tick_first(task->expire, ps->reconnect);
3491 }
3492 else {
3493 /* connect to the local peer if we must push a local sync */
3494 if (peers->flags & PEERS_F_DONOTSTOP) {
3495 peer_session_create(peers, ps);
3496 }
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003497 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003498 }
3499 else {
3500 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003501 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003502 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003503 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003504 peers->flags &= ~PEERS_F_DONOTSTOP;
3505 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003506 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003507 }
3508 }
3509 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003510 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Christopher Faulet19a82b92022-08-26 18:40:46 +02003511 /* Reset resync timeout during a resync */
3512 peers->resync_timeout = TICK_ETERNITY;
3513
Willy Tarreau9df94c22016-10-31 18:42:52 +01003514 /* current peer connection is active and established
3515 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003516 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003517 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003518 appctx_wakeup(ps->appctx);
3519 break;
3520 }
3521 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003522 }
3523 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003524
3525 /* Release lock for all peers of the section */
3526 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003527 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003528
Emeric Brun2b920a12010-09-23 18:30:22 +02003529 /* Wakeup for re-connect */
3530 return task;
3531}
3532
Emeric Brunb3971ab2015-05-12 18:49:09 +02003533
Emeric Brun2b920a12010-09-23 18:30:22 +02003534/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003535 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003536 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003537int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003538{
Emeric Brun2b920a12010-09-23 18:30:22 +02003539 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003540
Emeric Brun2b920a12010-09-23 18:30:22 +02003541 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003542 peers->peers_fe->maxconn += 3;
3543 }
3544
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003545 peers->sync_task = task_new_anywhere();
Willy Tarreaud9443442018-10-15 11:18:03 +02003546 if (!peers->sync_task)
3547 return 0;
3548
Maciej Zdebd01be2a2022-05-16 17:26:20 +02003549 memset(peers->applet_count, 0, sizeof(peers->applet_count));
Emeric Brunb3971ab2015-05-12 18:49:09 +02003550 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003551 peers->sync_task->context = (void *)peers;
3552 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3553 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003554 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003555}
3556
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003557/*
3558 * Allocate a cache a dictionary entries used upon transmission.
3559 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003560static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003561{
3562 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003563 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003564
3565 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003566 entries = calloc(max_entries, sizeof *entries);
3567 if (!d || !entries)
3568 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003569
3570 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003571 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003572 d->cached_entries = EB_ROOT_UNIQUE;
3573 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003574
3575 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003576
3577 err:
3578 free(d);
3579 free(entries);
3580 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003581}
3582
3583/*
3584 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3585 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003586 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003587 * Must be deallocated calling free_dcache().
3588 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003589static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003590{
3591 struct dcache_tx *dc_tx;
3592 struct dcache *dc;
3593 struct dcache_rx *dc_rx;
3594
3595 dc = calloc(1, sizeof *dc);
3596 dc_tx = new_dcache_tx(max_entries);
3597 dc_rx = calloc(max_entries, sizeof *dc_rx);
3598 if (!dc || !dc_tx || !dc_rx)
3599 goto err;
3600
3601 dc->tx = dc_tx;
3602 dc->rx = dc_rx;
3603 dc->max_entries = max_entries;
3604
3605 return dc;
3606
3607 err:
3608 free(dc);
3609 free(dc_tx);
3610 free(dc_rx);
3611 return NULL;
3612}
3613
3614/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003615 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3616 * entries used upon transmission.
3617 * Return the entry if found, NULL if not.
3618 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003619static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3620 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003621{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003622 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003623}
3624
3625/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003626 * Flush <dc> cache.
3627 * Always succeeds.
3628 */
3629static inline void flush_dcache(struct peer *peer)
3630{
3631 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003632 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003633
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003634 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003635 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003636 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003637 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3638 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003639 }
3640 dc->tx->prev_lookup = NULL;
3641 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003642
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003643 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003644}
3645
3646/*
3647 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3648 * with information provided by <i> dictionary cache entry (especially the value
3649 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003650 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003651 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003652static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003653{
3654 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003655 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003656
3657 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003658
3659 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3660 o = dc_tx->prev_lookup;
3661 } else {
3662 o = dcache_tx_lookup_value(dc_tx, i);
3663 if (o) {
3664 /* Save it */
3665 dc_tx->prev_lookup = o;
3666 }
3667 }
3668
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003669 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003670 /* Copy the ID. */
3671 i->id = o - dc->tx->entries;
3672 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003673 }
3674
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003675 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003676 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003677
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003678 ebpt_delete(o);
3679 o->key = i->entry.key;
3680 ebpt_insert(&dc_tx->cached_entries, o);
3681 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003682
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003683 /* Update the index for the next entry to put in cache */
3684 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003685
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003686 return o;
3687}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003688
3689/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003690 * Allocate a dictionary cache for each peer of <peers> section.
3691 * Return 1 if succeeded, 0 if not.
3692 */
3693int peers_alloc_dcache(struct peers *peers)
3694{
3695 struct peer *p;
3696
3697 for (p = peers->remote; p; p = p->next) {
3698 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3699 if (!p->dcache)
3700 return 0;
3701 }
3702
3703 return 1;
3704}
3705
3706/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003707 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003708 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003709 */
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003710int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003711{
3712 struct shared_table *st;
3713 struct peer * curpeer;
3714 int id = 0;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003715 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003716
3717 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003718 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003719 if (!st) {
3720 retval = 1;
3721 break;
3722 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003723 st->table = table;
3724 st->next = curpeer->tables;
3725 if (curpeer->tables)
3726 id = curpeer->tables->local_id;
3727 st->local_id = id + 1;
3728
Emeric Brun2cc201f2021-04-23 12:21:26 +02003729 /* If peer is local we inc table
3730 * refcnt to protect against flush
3731 * until this process pushed all
3732 * table content to the new one
3733 */
3734 if (curpeer->local)
3735 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003736 curpeer->tables = st;
3737 }
3738
3739 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003740
3741 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003742}
3743
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003744/* context used by a "show peers" command */
3745struct show_peers_ctx {
Willy Tarreau3a31e372022-05-03 14:58:47 +02003746 void *target; /* if non-null, dump only this section and stop */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003747 struct peers *peers; /* "peers" section being currently dumped. */
3748 struct peer *peer; /* "peer" being currently dumped. */
3749 int flags; /* non-zero if "dict" dump requested */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003750 enum {
Willy Tarreauce9123c2022-05-03 15:04:25 +02003751 STATE_HEAD = 0, /* dump the section's header */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003752 STATE_PEER, /* dump the whole peer */
3753 STATE_DONE, /* finished */
3754 } state; /* parser's state */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003755};
3756
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003757/*
3758 * Parse the "show peers" command arguments.
3759 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3760 * error message.
3761 */
3762static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3763{
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003764 struct show_peers_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003765
Willy Tarreau49962b52021-02-12 16:56:22 +01003766 if (strcmp(args[2], "dict") == 0) {
3767 /* show the dictionaries (large dump) */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003768 ctx->flags |= PEERS_SHOW_F_DICT;
Willy Tarreau49962b52021-02-12 16:56:22 +01003769 args++;
3770 } else if (strcmp(args[2], "-") == 0)
3771 args++; // allows to show a section called "dict"
3772
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003773 if (*args[2]) {
3774 struct peers *p;
3775
3776 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003777 if (strcmp(p->id, args[2]) == 0) {
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003778 ctx->target = p;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003779 break;
3780 }
3781 }
3782
Willy Tarreau9d008692019-08-09 11:21:01 +02003783 if (!p)
3784 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003785 }
3786
Willy Tarreauce9123c2022-05-03 15:04:25 +02003787 /* where to start from */
3788 ctx->peers = ctx->target ? ctx->target : cfg_peers;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003789 return 0;
3790}
3791
3792/*
3793 * This function dumps the peer state information of <peers> "peers" section.
3794 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3795 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3796 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003797static int peers_dump_head(struct buffer *msg, struct appctx *appctx, struct peers *peers)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003798{
3799 struct tm tm;
3800
3801 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003802 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 +02003803 peers,
3804 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3805 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003806 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003807 peers->resync_timeout ?
3808 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3809 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003810 TICKS_TO_MS(1000)) : "<NEVER>",
3811 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003812
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003813 if (applet_putchk(appctx, msg) == -1)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003814 return 0;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003815
3816 return 1;
3817}
3818
3819/*
3820 * This function dumps <peer> state information.
3821 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3822 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3823 */
Willy Tarreau50e77b22022-05-31 08:55:54 +02003824static 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 +02003825{
3826 struct connection *conn;
3827 char pn[INET6_ADDRSTRLEN];
Willy Tarreau4596fe22022-05-17 19:07:51 +02003828 struct stconn *peer_cs;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003829 struct stream *peer_s;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003830 struct shared_table *st;
3831
3832 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003833 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003834 peer, peer->id,
3835 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003836 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003837 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003838 statuscode_str(peer->statuscode));
3839
3840 chunk_appendf(msg, " last_hdshk=%s\n",
3841 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3842 TICKS_TO_MS(1000)) : "<NEVER>");
3843
3844 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003845 peer->reconnect ?
3846 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3847 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003848 TICKS_TO_MS(1000)) : "<NEVER>");
3849
3850 chunk_appendf(msg, " heartbeat=%s",
3851 peer->heartbeat ?
3852 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3853 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3854 TICKS_TO_MS(1000)) : "<NEVER>");
3855
3856 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 +01003857 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003858 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003859
3860 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3861
Willy Tarreaufc505992022-05-31 08:53:25 +02003862 if (!peer->appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003863 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003864
Willy Tarreaufc505992022-05-31 08:53:25 +02003865 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u",
3866 peer->appctx, peer->appctx->st0, peer->appctx->st1,
3867 peer->appctx->t ? peer->appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003868
Willy Tarreauc12b3212022-05-27 11:08:15 +02003869 peer_cs = appctx_sc(peer->appctx);
Willy Tarreauea27f482022-05-18 16:10:52 +02003870 peer_s = __sc_strm(peer_cs);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003871
Willy Tarreau74568cf2022-05-27 09:03:30 +02003872 chunk_appendf(&trash, " state=%s", sc_state_str(sc_opposite(peer_cs)->state));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003873
3874 conn = objt_conn(strm_orig(peer_s));
3875 if (conn)
3876 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3877
Willy Tarreau3ca14902019-07-17 14:53:15 +02003878 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 +02003879 case AF_INET:
3880 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003881 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003882 break;
3883 case AF_UNIX:
3884 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3885 break;
3886 }
3887
Willy Tarreau3ca14902019-07-17 14:53:15 +02003888 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 +02003889 case AF_INET:
3890 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003891 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003892 break;
3893 case AF_UNIX:
3894 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3895 break;
3896 }
3897
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003898 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003899 if (peer->remote_table)
3900 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3901 peer->remote_table,
3902 peer->remote_table->table->id,
3903 peer->remote_table->local_id,
3904 peer->remote_table->remote_id);
3905
3906 if (peer->last_local_table)
3907 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3908 peer->last_local_table,
3909 peer->last_local_table->table->id,
3910 peer->last_local_table->local_id,
3911 peer->last_local_table->remote_id);
3912
3913 if (peer->tables) {
3914 chunk_appendf(&trash, "\n shared tables:");
3915 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003916 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003917 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003918 struct dcache *dcache;
3919
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003920 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003921 dcache = peer->dcache;
3922
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003923 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3924 "flags=0x%x remote_data=0x%llx",
3925 st, st->local_id, st->remote_id,
3926 st->flags, (unsigned long long)st->remote_data);
3927 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3928 " teaching_origin=%u update=%u",
3929 st->last_acked, st->last_pushed, st->last_get,
3930 st->teaching_origin, st->update);
3931 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003932 " commitupdate=%u refcnt=%u",
3933 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003934 if (flags & PEERS_SHOW_F_DICT) {
3935 chunk_appendf(&trash, "\n TX dictionary cache:");
3936 count = 0;
3937 for (i = 0; i < dcache->max_entries; i++) {
3938 struct ebpt_node *node;
3939 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003940
Willy Tarreau49962b52021-02-12 16:56:22 +01003941 node = &dcache->tx->entries[i];
3942 if (!node->key)
3943 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003944
Willy Tarreau49962b52021-02-12 16:56:22 +01003945 if (!count++)
3946 chunk_appendf(&trash, "\n ");
3947 de = node->key;
3948 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3949 count &= 0x3;
3950 }
3951 chunk_appendf(&trash, "\n RX dictionary cache:");
3952 count = 0;
3953 for (i = 0; i < dcache->max_entries; i++) {
3954 if (!count++)
3955 chunk_appendf(&trash, "\n ");
3956 chunk_appendf(&trash, " %3u -> %s", i,
3957 dcache->rx[i].de ?
3958 (char *)dcache->rx[i].de->value.key : "-");
3959 count &= 0x3;
3960 }
3961 } else {
3962 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003963 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003964 }
3965 }
3966
3967 end:
3968 chunk_appendf(&trash, "\n");
Willy Tarreau50e77b22022-05-31 08:55:54 +02003969 if (applet_putchk(appctx, msg) == -1)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003970 return 0;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003971
3972 return 1;
3973}
3974
3975/*
3976 * This function dumps all the peers of "peers" section.
3977 * Returns 0 if the output buffer is full and needs to be called
3978 * again, non-zero if not. It proceeds in an isolated thread, so
3979 * there is no thread safety issue here.
3980 */
3981static int cli_io_handler_show_peers(struct appctx *appctx)
3982{
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003983 struct show_peers_ctx *ctx = appctx->svcctx;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003984 int ret = 0, first_peers = 1;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003985
3986 thread_isolate();
3987
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003988 chunk_reset(&trash);
3989
Willy Tarreau3a31e372022-05-03 14:58:47 +02003990 while (ctx->state != STATE_DONE) {
3991 switch (ctx->state) {
Willy Tarreau3a31e372022-05-03 14:58:47 +02003992 case STATE_HEAD:
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003993 if (!ctx->peers) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003994 /* No more peers list. */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003995 ctx->state = STATE_DONE;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003996 }
3997 else {
3998 if (!first_peers)
3999 chunk_appendf(&trash, "\n");
4000 else
4001 first_peers = 0;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004002 if (!peers_dump_head(&trash, appctx, ctx->peers))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004003 goto out;
4004
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004005 ctx->peer = ctx->peers->remote;
4006 ctx->peers = ctx->peers->next;
Willy Tarreau3a31e372022-05-03 14:58:47 +02004007 ctx->state = STATE_PEER;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004008 }
4009 break;
4010
Willy Tarreau3a31e372022-05-03 14:58:47 +02004011 case STATE_PEER:
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004012 if (!ctx->peer) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004013 /* End of peer list */
Willy Tarreauce9123c2022-05-03 15:04:25 +02004014 if (!ctx->target)
4015 ctx->state = STATE_HEAD; // next one
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004016 else
Willy Tarreau3a31e372022-05-03 14:58:47 +02004017 ctx->state = STATE_DONE;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004018 }
4019 else {
Willy Tarreau50e77b22022-05-31 08:55:54 +02004020 if (!peers_dump_peer(&trash, appctx, ctx->peer, ctx->flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004021 goto out;
4022
Willy Tarreaucb8bf172022-05-03 14:26:31 +02004023 ctx->peer = ctx->peer->next;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004024 }
Willy Tarreau3a31e372022-05-03 14:58:47 +02004025 break;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004026
Willy Tarreau3a31e372022-05-03 14:58:47 +02004027 default:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004028 break;
4029 }
4030 }
4031 ret = 1;
4032 out:
4033 thread_release();
4034 return ret;
4035}
4036
Willy Tarreau8bd146d2022-07-19 20:17:38 +02004037/* config parser for global "tune.peers.max-updates-at-once" */
4038static int cfg_parse_max_updt_at_once(char **args, int section_type, struct proxy *curpx,
4039 const struct proxy *defpx, const char *file, int line,
4040 char **err)
4041{
4042 int arg = -1;
4043
4044 if (too_many_args(1, args, err, NULL))
4045 return -1;
4046
4047 if (*(args[1]) != 0)
4048 arg = atoi(args[1]);
4049
4050 if (arg < 1) {
4051 memprintf(err, "'%s' expects an integer argument greater than 0.", args[0]);
4052 return -1;
4053 }
4054
4055 peers_max_updates_at_once = arg;
4056 return 0;
4057}
4058
4059/* config keyword parsers */
4060static struct cfg_kw_list cfg_kws = {ILH, {
4061 { CFG_GLOBAL, "tune.peers.max-updates-at-once", cfg_parse_max_updt_at_once },
4062 { 0, NULL, NULL }
4063}};
4064
4065INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
4066
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004067/*
4068 * CLI keywords.
4069 */
4070static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004071 { { "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 +02004072 {},
4073}};
4074
4075/* Register cli keywords */
4076INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);