blob: e2bcee7c9fe8207f797fb6b723627427c2e50b61 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau8db34cc2021-10-06 17:53:19 +020023#include <import/eb32tree.h>
24#include <import/ebmbtree.h>
25#include <import/ebpttree.h>
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet.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 Tarreau48d25b32020-06-04 18:58:52 +020039#include <haproxy/session-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020040#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020041#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stick_table.h>
43#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020044#include <haproxy/stream_interface.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/task.h>
46#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020047#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020048#include <haproxy/tools.h>
Frédéric Lécailled8659352020-11-10 16:18:03 +010049#include <haproxy/trace.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
Emeric Brun2b920a12010-09-23 18:30:22 +020051
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Emeric Brunccdfbae2021-04-28 12:59:35 +020059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
64#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
65#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
66#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
67#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
68#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
69#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
70#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
71#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
72#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
73#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
74#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
75 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010077#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
78#define PEERS_RESYNC_FROMLOCAL 0x00000000
79#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
80#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020081
82/***********************************/
83/* Current shared table sync state */
84/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010085#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
86#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020087
88/******************************/
89/* Remote peer teaching state */
90/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010091#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
92#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
93#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
94#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
95#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
96#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
97#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
98#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020099
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100100#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
101#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +0200102
Frédéric Lécaille54bff832019-03-26 10:25:20 +0100103#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
104#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100105#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
106
Willy Tarreau49962b52021-02-12 16:56:22 +0100107/* flags for "show peers" */
108#define PEERS_SHOW_F_DICT 0x00000001 /* also show the contents of the dictionary */
109
Emeric Brunb3971ab2015-05-12 18:49:09 +0200110/*****************************/
111/* Sync message class */
112/*****************************/
113enum {
114 PEER_MSG_CLASS_CONTROL = 0,
115 PEER_MSG_CLASS_ERROR,
116 PEER_MSG_CLASS_STICKTABLE = 10,
117 PEER_MSG_CLASS_RESERVED = 255,
118};
119
120/*****************************/
121/* control message types */
122/*****************************/
123enum {
124 PEER_MSG_CTRL_RESYNCREQ = 0,
125 PEER_MSG_CTRL_RESYNCFINISHED,
126 PEER_MSG_CTRL_RESYNCPARTIAL,
127 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100128 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200129};
130
131/*****************************/
132/* error message types */
133/*****************************/
134enum {
135 PEER_MSG_ERR_PROTOCOL = 0,
136 PEER_MSG_ERR_SIZELIMIT,
137};
138
Emeric Brun530ba382020-06-02 11:17:42 +0200139/* network key types;
140 * network types were directly and mistakenly
141 * mapped on sample types, to keep backward
142 * compatiblitiy we keep those values but
143 * we now use a internal/network mapping
144 * to avoid further mistakes adding or
145 * modifying internals types
146 */
147enum {
148 PEER_KT_ANY = 0, /* any type */
149 PEER_KT_RESV1, /* UNUSED */
150 PEER_KT_SINT, /* signed 64bits integer type */
151 PEER_KT_RESV3, /* UNUSED */
152 PEER_KT_IPV4, /* ipv4 type */
153 PEER_KT_IPV6, /* ipv6 type */
154 PEER_KT_STR, /* char string type */
155 PEER_KT_BIN, /* buffer type */
156 PEER_KT_TYPES /* number of types, must always be last */
157};
158
159/* Map used to retrieve network type from internal type
160 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
161 */
162static int peer_net_key_type[SMP_TYPES] = {
163 [SMP_T_SINT] = PEER_KT_SINT,
164 [SMP_T_IPV4] = PEER_KT_IPV4,
165 [SMP_T_IPV6] = PEER_KT_IPV6,
166 [SMP_T_STR] = PEER_KT_STR,
167 [SMP_T_BIN] = PEER_KT_BIN,
168};
169
170/* Map used to retrieve internal type from external type
171 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
172 */
173static int peer_int_key_type[PEER_KT_TYPES] = {
174 [PEER_KT_SINT] = SMP_T_SINT,
175 [PEER_KT_IPV4] = SMP_T_IPV4,
176 [PEER_KT_IPV6] = SMP_T_IPV6,
177 [PEER_KT_STR] = SMP_T_STR,
178 [PEER_KT_BIN] = SMP_T_BIN,
179};
180
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100181/*
182 * Parameters used by functions to build peer protocol messages. */
183struct peer_prep_params {
184 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100185 struct peer *peer;
186 } hello;
187 struct {
188 unsigned int st1;
189 } error_status;
190 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100191 struct stksess *stksess;
192 struct shared_table *shared_table;
193 unsigned int updateid;
194 int use_identifier;
195 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200196 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100197 } updt;
198 struct {
199 struct shared_table *shared_table;
200 } swtch;
201 struct {
202 struct shared_table *shared_table;
203 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100204 struct {
205 unsigned char head[2];
206 } control;
207 struct {
208 unsigned char head[2];
209 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100210};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200211
212/*******************************/
213/* stick table sync mesg types */
214/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500215/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200216/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200217#define PEER_MSG_STKT_UPDATE 0x80
218#define PEER_MSG_STKT_INCUPDATE 0x81
219#define PEER_MSG_STKT_DEFINE 0x82
220#define PEER_MSG_STKT_SWITCH 0x83
221#define PEER_MSG_STKT_ACK 0x84
222#define PEER_MSG_STKT_UPDATE_TIMED 0x85
223#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200224/* All the stick-table message identifiers abova have the #7 bit set */
225#define PEER_MSG_STKT_BIT 7
226#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200227
Frédéric Lécaille39143342019-05-24 14:32:27 +0200228/* The maximum length of an encoded data length. */
229#define PEER_MSG_ENC_LENGTH_MAXLEN 5
230
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200231/* Minimum 64-bits value encoded with 2 bytes */
232#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
233/* 3 bytes */
234#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
235/* 4 bytes */
236#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
237/* 5 bytes */
238#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
239/* 6 bytes */
240#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
241/* 7 bytes */
242#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
243/* 8 bytes */
244#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
245/* 9 bytes */
246#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
247/* 10 bytes */
248#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
249
250/* #7 bit used to detect the last byte to be encoded */
251#define PEER_ENC_STOP_BIT 7
252/* The byte minimum value with #7 bit set */
253#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
254/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
255#define PEER_ENC_2BYTES_MIN_BITS 4
256
Frédéric Lécaille39143342019-05-24 14:32:27 +0200257#define PEER_MSG_HEADER_LEN 2
258
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200259#define PEER_STKT_CACHE_MAX_ENTRIES 128
260
Emeric Brun2b920a12010-09-23 18:30:22 +0200261/**********************************/
262/* Peer Session IO handler states */
263/**********************************/
264
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100265enum {
266 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
267 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
268 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
269 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100270 /* after this point, data were possibly exchanged */
271 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
272 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
273 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
274 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
275 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200276 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
277 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100278 PEER_SESS_ST_END, /* Killed session */
279};
Emeric Brun2b920a12010-09-23 18:30:22 +0200280
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100281/***************************************************/
282/* Peer Session status code - part of the protocol */
283/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200284
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100285#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
286#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200287
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100288#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200289
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100290#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200291
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100292#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
293#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
294#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
295#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200296
297#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200298#define PEER_MAJOR_VER 2
299#define PEER_MINOR_VER 1
300#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200301
Willy Tarreau6254a922019-01-29 17:45:23 +0100302static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200303struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200304static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200305
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200306static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
307 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200308static inline void flush_dcache(struct peer *peer);
309
Frédéric Lécailled8659352020-11-10 16:18:03 +0100310/* trace source and events */
311static void peers_trace(enum trace_level level, uint64_t mask,
312 const struct trace_source *src,
313 const struct ist where, const struct ist func,
314 const void *a1, const void *a2, const void *a3, const void *a4);
315
316static const struct trace_event peers_trace_events[] = {
317#define PEERS_EV_UPDTMSG (1 << 0)
318 { .mask = PEERS_EV_UPDTMSG, .name = "updtmsg", .desc = "update message received" },
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100319#define PEERS_EV_ACKMSG (1 << 1)
320 { .mask = PEERS_EV_ACKMSG, .name = "ackmsg", .desc = "ack message received" },
321#define PEERS_EV_SWTCMSG (1 << 2)
322 { .mask = PEERS_EV_SWTCMSG, .name = "swtcmsg", .desc = "switch message received" },
323#define PEERS_EV_DEFMSG (1 << 3)
324 { .mask = PEERS_EV_DEFMSG, .name = "defmsg", .desc = "definition message received" },
325#define PEERS_EV_CTRLMSG (1 << 4)
326 { .mask = PEERS_EV_CTRLMSG, .name = "ctrlmsg", .desc = "control message sent/received" },
327#define PEERS_EV_SESSREL (1 << 5)
328 { .mask = PEERS_EV_SESSREL, .name = "sessrl", .desc = "peer session releasing" },
329#define PEERS_EV_PROTOERR (1 << 6)
330 { .mask = PEERS_EV_PROTOERR, .name = "protoerr", .desc = "protocol error" },
Frédéric Lécailled8659352020-11-10 16:18:03 +0100331};
332
333static const struct name_desc peers_trace_lockon_args[4] = {
334 /* arg1 */ { /* already used by the connection */ },
335 /* arg2 */ { .name="peers", .desc="Peers protocol" },
336 /* arg3 */ { },
337 /* arg4 */ { }
338};
339
340static const struct name_desc peers_trace_decoding[] = {
341#define PEERS_VERB_CLEAN 1
342 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
343 { /* end */ }
344};
345
346
347struct trace_source trace_peers = {
348 .name = IST("peers"),
349 .desc = "Peers protocol",
350 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
351 .default_cb = peers_trace,
352 .known_events = peers_trace_events,
353 .lockon_args = peers_trace_lockon_args,
354 .decoding = peers_trace_decoding,
355 .report_events = ~0, /* report everything by default */
356};
357
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100358/* Return peer control message types as strings (only for debugging purpose). */
359static inline char *ctrl_msg_type_str(unsigned int type)
360{
361 switch (type) {
362 case PEER_MSG_CTRL_RESYNCREQ:
363 return "RESYNCREQ";
364 case PEER_MSG_CTRL_RESYNCFINISHED:
365 return "RESYNCFINISHED";
366 case PEER_MSG_CTRL_RESYNCPARTIAL:
367 return "RESYNCPARTIAL";
368 case PEER_MSG_CTRL_RESYNCCONFIRM:
369 return "RESYNCCONFIRM";
370 case PEER_MSG_CTRL_HEARTBEAT:
371 return "HEARTBEAT";
372 default:
373 return "???";
374 }
375}
376
Frédéric Lécailled8659352020-11-10 16:18:03 +0100377#define TRACE_SOURCE &trace_peers
378INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
379
380static void peers_trace(enum trace_level level, uint64_t mask,
381 const struct trace_source *src,
382 const struct ist where, const struct ist func,
383 const void *a1, const void *a2, const void *a3, const void *a4)
384{
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100385 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécailled8659352020-11-10 16:18:03 +0100386 if (a2) {
387 const struct peer *peer = a2;
388
389 chunk_appendf(&trace_buf, " peer=%s", peer->id);
390 }
391 if (a3) {
392 const char *p = a3;
393
394 chunk_appendf(&trace_buf, " @%p", p);
395 }
396 if (a4) {
397 const size_t *val = a4;
398
399 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
400 }
401 }
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100402
403 if (mask & PEERS_EV_DEFMSG) {
404 if (a2) {
405 const struct peer *peer = a2;
406
407 chunk_appendf(&trace_buf, " peer=%s", peer->id);
408 }
409 if (a3) {
410 const char *p = a3;
411
412 chunk_appendf(&trace_buf, " @%p", p);
413 }
414 if (a4) {
415 const int *val = a4;
416
417 chunk_appendf(&trace_buf, " %d", *val);
418 }
419 }
420
421 if (mask & PEERS_EV_CTRLMSG) {
422 if (a2) {
423 const unsigned char *ctrl_msg_type = a2;
424
425 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
426
427 }
428 if (a3) {
429 const char *local_peer = a3;
430
431 chunk_appendf(&trace_buf, " %s", local_peer);
432 }
433
434 if (a4) {
435 const char *remote_peer = a4;
436
437 chunk_appendf(&trace_buf, " -> %s", remote_peer);
438 }
439 }
440
441 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
442 if (a2) {
443 const struct peer *peer = a2;
444 struct peers *peers = NULL;
445
Christopher Fauletd9e6b352021-11-15 09:40:57 +0100446 if (peer->appctx) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100447 struct stream_interface *si;
448
Christopher Faulet86e1c332021-12-20 17:09:39 +0100449 si = cs_si(peer->appctx->owner);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100450 if (si) {
451 struct stream *s = si_strm(si);
452
453 peers = strm_fe(s)->parent;
454 }
455 }
456
457 if (peers)
458 chunk_appendf(&trace_buf, " %s", peers->local->id);
Christopher Fauletd9e6b352021-11-15 09:40:57 +0100459 chunk_appendf(&trace_buf, " -> %s", peer->id);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100460 }
461
462 if (a3) {
463 const int *prev_state = a3;
464
465 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
466 }
467 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100468}
469
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200470static const char *statuscode_str(int statuscode)
471{
472 switch (statuscode) {
473 case PEER_SESS_SC_CONNECTCODE:
474 return "CONN";
475 case PEER_SESS_SC_CONNECTEDCODE:
476 return "HSHK";
477 case PEER_SESS_SC_SUCCESSCODE:
478 return "ESTA";
479 case PEER_SESS_SC_TRYAGAIN:
480 return "RETR";
481 case PEER_SESS_SC_ERRPROTO:
482 return "PROT";
483 case PEER_SESS_SC_ERRVERSION:
484 return "VERS";
485 case PEER_SESS_SC_ERRHOST:
486 return "NAME";
487 case PEER_SESS_SC_ERRPEER:
488 return "UNKN";
489 default:
490 return "NONE";
491 }
492}
493
Emeric Brun18928af2017-03-29 16:32:53 +0200494/* This function encode an uint64 to 'dynamic' length format.
495 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500496 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200497 At return, the *str is set at the next Byte after then
498 encoded integer. The function returns then length of the
499 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200500int intencode(uint64_t i, char **str) {
501 int idx = 0;
502 unsigned char *msg;
503
Emeric Brunb3971ab2015-05-12 18:49:09 +0200504 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200505 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200506 msg[0] = (unsigned char)i;
507 *str = (char *)&msg[idx+1];
508 return (idx+1);
509 }
510
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200511 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
512 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
513 while (i >= PEER_ENC_STOP_BYTE) {
514 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
515 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200516 }
517 msg[++idx] = (unsigned char)i;
518 *str = (char *)&msg[idx+1];
519 return (idx+1);
520}
521
522
Emeric Brun5ea07d92021-06-30 13:21:58 +0200523/* This function returns a decoded 64bits unsigned integer
524 * from a varint
525 *
526 * Calling:
527 * - *str must point on the first byte of the buffer to decode.
528 * - end must point on the next byte after the end of the buffer
529 * we are authorized to parse (buf + buflen)
530 *
531 * At return:
532 *
533 * On success *str will point at the byte following
534 * the fully decoded integer into the buffer. and
535 * the decoded value is returned.
536 *
537 * If end is reached before the integer was fully decoded,
538 * *str is set to NULL and the caller have to check this
539 * to know there is a decoding error. In this case
540 * the returned integer is also forced to 0
541 */
Emeric Brun18928af2017-03-29 16:32:53 +0200542uint64_t intdecode(char **str, char *end)
543{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200544 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200545 uint64_t i;
546 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200547
548 if (!*str)
549 return 0;
550
551 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200552 if (msg >= (unsigned char *)end)
553 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200554
Emeric Brun18928af2017-03-29 16:32:53 +0200555 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200556 if (i >= PEER_ENC_2BYTES_MIN) {
557 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200558 do {
559 if (msg >= (unsigned char *)end)
560 goto fail;
561 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200562 shift += PEER_ENC_STOP_BIT;
563 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200564 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100565 *str = (char *)msg;
566 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200567
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100568 fail:
569 *str = NULL;
570 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200571}
Emeric Brun2b920a12010-09-23 18:30:22 +0200572
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100573/*
574 * Build a "hello" peer protocol message.
575 * Return the number of written bytes written to build this messages if succeeded,
576 * 0 if not.
577 */
578static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
579{
580 int min_ver, ret;
581 struct peer *peer;
582
583 peer = p->hello.peer;
584 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
585 /* Prepare headers */
Willy Tarreau2645b342022-04-12 08:28:18 +0200586 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %d.%d\n%s\n%s %d %d\n",
587 (int)PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), (int)1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100588 if (ret >= size)
589 return 0;
590
591 return ret;
592}
593
594/*
595 * Build a "handshake succeeded" status message.
596 * Return the number of written bytes written to build this messages if succeeded,
597 * 0 if not.
598 */
599static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
600{
601 int ret;
602
Willy Tarreau2645b342022-04-12 08:28:18 +0200603 ret = snprintf(msg, size, "%d\n", (int)PEER_SESS_SC_SUCCESSCODE);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100604 if (ret >= size)
605 return 0;
606
607 return ret;
608}
609
610/*
611 * Build an error status message.
612 * Return the number of written bytes written to build this messages if succeeded,
613 * 0 if not.
614 */
615static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
616{
617 int ret;
618 unsigned int st1;
619
620 st1 = p->error_status.st1;
621 ret = snprintf(msg, size, "%d\n", st1);
622 if (ret >= size)
623 return 0;
624
625 return ret;
626}
627
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200628/* Set the stick-table UPDATE message type byte at <msg_type> address,
629 * depending on <use_identifier> and <use_timed> boolean parameters.
630 * Always successful.
631 */
632static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
633{
634 if (use_timed) {
635 if (use_identifier)
636 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
637 else
638 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
639 }
640 else {
641 if (use_identifier)
642 *msg_type = PEER_MSG_STKT_UPDATE;
643 else
644 *msg_type = PEER_MSG_STKT_INCUPDATE;
645 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200646}
Emeric Brun2b920a12010-09-23 18:30:22 +0200647/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200648 * This prepare the data update message on the stick session <ts>, <st> is the considered
649 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800650 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200651 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
652 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200653 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100654static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200655{
656 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200657 unsigned short datalen;
658 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200659 unsigned int data_type;
660 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100661 struct stksess *ts;
662 struct shared_table *st;
663 unsigned int updateid;
664 int use_identifier;
665 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200666 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100667
668 ts = p->updt.stksess;
669 st = p->updt.shared_table;
670 updateid = p->updt.updateid;
671 use_identifier = p->updt.use_identifier;
672 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200673 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200674
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200675 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200676
Emeric Brun2b920a12010-09-23 18:30:22 +0200677 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200678
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500679 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200680 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200681 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200682 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200683
684 /* encode update identifier if needed */
685 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200686 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200687 memcpy(cursor, &netinteger, sizeof(netinteger));
688 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200689 }
690
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200691 if (use_timed) {
692 netinteger = htonl(tick_remain(now_ms, ts->expire));
693 memcpy(cursor, &netinteger, sizeof(netinteger));
694 cursor += sizeof(netinteger);
695 }
696
Emeric Brunb3971ab2015-05-12 18:49:09 +0200697 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200698 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200699 int stlen = strlen((char *)ts->key.key);
700
Emeric Brunb3971ab2015-05-12 18:49:09 +0200701 intencode(stlen, &cursor);
702 memcpy(cursor, ts->key.key, stlen);
703 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200704 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200705 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100706 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200707 memcpy(cursor, &netinteger, sizeof(netinteger));
708 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200709 }
710 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200711 memcpy(cursor, ts->key.key, st->table->key_size);
712 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200713 }
714
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100715 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200716 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200717 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200718
Emeric Brun94900952015-06-11 18:25:54 +0200719 data_ptr = stktable_data_ptr(st->table, ts, data_type);
720 if (data_ptr) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200721 /* in case of array all elements use
722 * the same std_type and they are linearly
723 * encoded.
724 */
725 if (stktable_data_types[data_type].is_array) {
726 unsigned int idx = 0;
727
728 switch (stktable_data_types[data_type].std_type) {
729 case STD_T_SINT: {
730 int data;
731
732 do {
733 data = stktable_data_cast(data_ptr, std_t_sint);
734 intencode(data, &cursor);
735
736 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
737 } while(data_ptr);
738 break;
739 }
740 case STD_T_UINT: {
741 unsigned int data;
742
743 do {
744 data = stktable_data_cast(data_ptr, std_t_uint);
745 intencode(data, &cursor);
746
747 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
748 } while(data_ptr);
749 break;
750 }
751 case STD_T_ULL: {
752 unsigned long long data;
753
754 do {
755 data = stktable_data_cast(data_ptr, std_t_ull);
756 intencode(data, &cursor);
757
758 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
759 } while(data_ptr);
760 break;
761 }
762 case STD_T_FRQP: {
763 struct freq_ctr *frqp;
764
765 do {
766 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
767 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
768 intencode(frqp->curr_ctr, &cursor);
769 intencode(frqp->prev_ctr, &cursor);
770
771 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
772 } while(data_ptr);
773 break;
774 }
775 }
776
777 /* array elements fully encoded
778 * proceed next data_type.
779 */
780 continue;
781 }
Emeric Brun94900952015-06-11 18:25:54 +0200782 switch (stktable_data_types[data_type].std_type) {
783 case STD_T_SINT: {
784 int data;
785
786 data = stktable_data_cast(data_ptr, std_t_sint);
787 intencode(data, &cursor);
788 break;
789 }
790 case STD_T_UINT: {
791 unsigned int data;
792
793 data = stktable_data_cast(data_ptr, std_t_uint);
794 intencode(data, &cursor);
795 break;
796 }
797 case STD_T_ULL: {
798 unsigned long long data;
799
800 data = stktable_data_cast(data_ptr, std_t_ull);
801 intencode(data, &cursor);
802 break;
803 }
804 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200805 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200806
807 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
808 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
809 intencode(frqp->curr_ctr, &cursor);
810 intencode(frqp->prev_ctr, &cursor);
811 break;
812 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200813 case STD_T_DICT: {
814 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200815 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200816 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200817 char *beg, *end;
818 size_t value_len, data_len;
819 struct dcache *dc;
820
821 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100822 if (!de) {
823 /* No entry */
824 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200825 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100826 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200827
828 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200829 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200830 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200831 if (cached_de == &cde.entry) {
832 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
833 break;
834 /* Encode the length of the remaining data -> 1 */
835 intencode(1, &cursor);
836 /* Encode the cache entry ID */
837 intencode(cde.id + 1, &cursor);
838 }
839 else {
840 /* Leave enough room to encode the remaining data length. */
841 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
842 /* Encode the dictionary entry key */
843 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200844 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200845 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200846 intencode(value_len, &end);
847 /* Copy the data */
848 memcpy(end, de->value.key, value_len);
849 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200850 /* Encode the length of the data */
851 data_len = end - beg;
852 intencode(data_len, &cursor);
853 memmove(cursor, beg, data_len);
854 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200855 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200856 break;
857 }
Emeric Brun94900952015-06-11 18:25:54 +0200858 }
859 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200860 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100861 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200862
863 /* Compute datalen */
864 datalen = (cursor - datamsg);
865
866 /* prepare message header */
867 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200868 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200869 cursor = &msg[2];
870 intencode(datalen, &cursor);
871
872 /* move data after header */
873 memmove(cursor, datamsg, datalen);
874
875 /* return header size + data_len */
876 return (cursor - msg) + datalen;
877}
878
879/*
880 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800881 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200882 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
883 * check size)
884 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100885static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886{
887 int len;
888 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200889 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200890 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200891 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200892 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100893 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200894
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100895 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200896 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200897
898 /* Encode data */
899
900 /* encode local id */
901 intencode(st->local_id, &cursor);
902
903 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100904 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200905 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100906 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200907 cursor += len;
908
909 /* encode table type */
910
Emeric Brun530ba382020-06-02 11:17:42 +0200911 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200912
913 /* encode table key size */
914 intencode(st->table->key_size, &cursor);
915
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200916 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200917 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200918 /* encode available known data types in table */
919 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
920 if (st->table->data_ofs[data_type]) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200921 /* stored data types parameters are all linearly encoded
922 * at the end of the 'table definition' message.
923 *
924 * Currently only array data_types and and data_types
925 * using freq_counter base type have parameters:
926 *
927 * - array has always at least one parameter set to the
928 * number of elements.
929 *
930 * - array of base-type freq_counters has an additional
931 * parameter set to the period used to compute those
932 * freq_counters.
933 *
934 * - simple freq counter has a parameter set to the period
935 * used to compute
936 *
937 * A set of parameter for a datatype MUST BE prefixed
938 * by the data-type id itself:
939 * This is useless because the data_types are ordered and
940 * the data_type bitfield already gives the information of
941 * stored types, but it was designed this way when the
942 * push of period parameter was added for freq counters
943 * and we don't want to break the compatibility.
944 *
945 */
946 if (stktable_data_types[data_type].is_array) {
947 /* This is an array type so we first encode
948 * the data_type itself to prefix parameters
949 */
950 intencode(data_type, &chunkq);
951
952 /* We encode the first parameter which is
953 * the number of elements of this array
954 */
955 intencode(st->table->data_nbelem[data_type], &chunkq);
956
Ilya Shipitsin01881082021-08-07 14:41:56 +0500957 /* for array of freq counters, there is an additional
Emeric Brun90a9b672021-06-22 16:09:55 +0200958 * period parameter to encode
959 */
960 if (stktable_data_types[data_type].std_type == STD_T_FRQP)
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200961 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200962 }
Emeric Brun90a9b672021-06-22 16:09:55 +0200963 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
964 /* this datatype is a simple freq counter not part
965 * of an array. We encode the data_type itself
966 * to prefix the 'period' parameter
967 */
968 intencode(data_type, &chunkq);
969 intencode(st->table->data_arg[data_type].u, &chunkq);
970 }
971 /* set the bit corresponding to stored data type */
972 data |= 1ULL << data_type;
Emeric Brun94900952015-06-11 18:25:54 +0200973 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200974 }
975 intencode(data, &cursor);
976
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200977 /* Encode stick-table entries duration. */
978 intencode(st->table->expire, &cursor);
979
980 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200981 chunk->data = chunkq - chunkp;
982 memcpy(cursor, chunk->area, chunk->data);
983 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200984 }
985
Emeric Brunb3971ab2015-05-12 18:49:09 +0200986 /* Compute datalen */
987 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200988
Emeric Brunb3971ab2015-05-12 18:49:09 +0200989 /* prepare message header */
990 msg[0] = PEER_MSG_CLASS_STICKTABLE;
991 msg[1] = PEER_MSG_STKT_DEFINE;
992 cursor = &msg[2];
993 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200994
Emeric Brunb3971ab2015-05-12 18:49:09 +0200995 /* move data after header */
996 memmove(cursor, datamsg, datalen);
997
998 /* return header size + data_len */
999 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001000}
1001
Emeric Brunb3971ab2015-05-12 18:49:09 +02001002/*
1003 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
1004 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -08001005 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +02001006 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
1007 * check size)
1008 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001009static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001010{
1011 unsigned short datalen;
1012 char *cursor, *datamsg;
1013 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001014 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001015
Frédéric Lécaille39143342019-05-24 14:32:27 +02001016 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001017
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001018 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001019 intencode(st->remote_id, &cursor);
1020 netinteger = htonl(st->last_get);
1021 memcpy(cursor, &netinteger, sizeof(netinteger));
1022 cursor += sizeof(netinteger);
1023
1024 /* Compute datalen */
1025 datalen = (cursor - datamsg);
1026
1027 /* prepare message header */
1028 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +02001029 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001030 cursor = &msg[2];
1031 intencode(datalen, &cursor);
1032
1033 /* move data after header */
1034 memmove(cursor, datamsg, datalen);
1035
1036 /* return header size + data_len */
1037 return (cursor - msg) + datalen;
1038}
Emeric Brun2b920a12010-09-23 18:30:22 +02001039
1040/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001041 * Function to deinit connected peer
1042 */
1043void __peer_session_deinit(struct peer *peer)
1044{
1045 struct stream_interface *si;
1046 struct stream *s;
1047 struct peers *peers;
1048
1049 if (!peer->appctx)
1050 return;
1051
Christopher Faulet86e1c332021-12-20 17:09:39 +01001052 si = cs_si(peer->appctx->owner);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001053 if (!si)
1054 return;
1055
1056 s = si_strm(si);
1057 if (!s)
1058 return;
1059
1060 peers = strm_fe(s)->parent;
1061 if (!peers)
1062 return;
1063
1064 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02001065 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001066
Willy Tarreau4781b152021-04-06 13:53:36 +02001067 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001068
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001069 flush_dcache(peer);
1070
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001071 /* Re-init current table pointers to force announcement on re-connect */
1072 peer->remote_table = peer->last_local_table = NULL;
1073 peer->appctx = NULL;
1074 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1075 /* unassign current peer for learning */
1076 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
1077 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1078
Emeric Brunccdfbae2021-04-28 12:59:35 +02001079 if (peer->local)
1080 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
1081 else
1082 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001083 /* reschedule a resync */
1084 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1085 }
1086 /* reset teaching and learning flags to 0 */
1087 peer->flags &= PEER_TEACH_RESET;
1088 peer->flags &= PEER_LEARN_RESET;
1089 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1090}
1091
1092/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001093 * Callback to release a session with a peer
1094 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001095static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001096{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001097 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001098
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001099 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001100 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001101 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +02001102 return;
1103
1104 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001105 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001106 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001107 if (peer->appctx == appctx)
1108 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02001109 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001110 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001111 }
1112}
1113
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001114/* Retrieve the major and minor versions of peers protocol
1115 * announced by a remote peer. <str> is a null-terminated
1116 * string with the following format: "<maj_ver>.<min_ver>".
1117 */
1118static int peer_get_version(const char *str,
1119 unsigned int *maj_ver, unsigned int *min_ver)
1120{
1121 unsigned int majv, minv;
1122 const char *pos, *saved;
1123 const char *end;
1124
1125 saved = pos = str;
1126 end = str + strlen(str);
1127
1128 majv = read_uint(&pos, end);
1129 if (saved == pos || *pos++ != '.')
1130 return -1;
1131
1132 saved = pos;
1133 minv = read_uint(&pos, end);
1134 if (saved == pos || pos != end)
1135 return -1;
1136
1137 *maj_ver = majv;
1138 *min_ver = minv;
1139
1140 return 0;
1141}
Emeric Brun2b920a12010-09-23 18:30:22 +02001142
1143/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001144 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1145 * '\n' character.
1146 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1147 * a line could not be read because the communication channel is closed.
1148 */
1149static inline int peer_getline(struct appctx *appctx)
1150{
1151 int n;
Christopher Faulet86e1c332021-12-20 17:09:39 +01001152 struct stream_interface *si = cs_si(appctx->owner);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001153
1154 n = co_getline(si_oc(si), trash.area, trash.size);
1155 if (!n)
1156 return 0;
1157
1158 if (n < 0 || trash.area[n - 1] != '\n') {
1159 appctx->st0 = PEER_SESS_ST_END;
1160 return -1;
1161 }
1162
1163 if (n > 1 && (trash.area[n - 2] == '\r'))
1164 trash.area[n - 2] = 0;
1165 else
1166 trash.area[n - 1] = 0;
1167
1168 co_skip(si_oc(si), n);
1169
1170 return n;
1171}
1172
1173/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001174 * Send a message after having called <peer_prepare_msg> to build it.
1175 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1176 * Returns -1 if there was not enough room left to send the message,
1177 * any other negative returned value must be considered as an error with an appcxt st0
1178 * returned value equal to PEER_SESS_ST_END.
1179 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001180static inline int peer_send_msg(struct appctx *appctx,
1181 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1182 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001183{
1184 int ret, msglen;
Christopher Faulet86e1c332021-12-20 17:09:39 +01001185 struct stream_interface *si = cs_si(appctx->owner);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001186
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001187 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001188 if (!msglen) {
1189 /* internal error: message does not fit in trash */
1190 appctx->st0 = PEER_SESS_ST_END;
1191 return 0;
1192 }
1193
1194 /* message to buffer */
1195 ret = ci_putblk(si_ic(si), trash.area, msglen);
1196 if (ret <= 0) {
1197 if (ret == -1) {
1198 /* No more write possible */
1199 si_rx_room_blk(si);
1200 return -1;
1201 }
1202 appctx->st0 = PEER_SESS_ST_END;
1203 }
1204
1205 return ret;
1206}
1207
1208/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001209 * Send a hello message.
1210 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1211 * Returns -1 if there was not enough room left to send the message,
1212 * any other negative returned value must be considered as an error with an appcxt st0
1213 * returned value equal to PEER_SESS_ST_END.
1214 */
1215static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1216{
1217 struct peer_prep_params p = {
1218 .hello.peer = peer,
1219 };
1220
1221 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1222}
1223
1224/*
1225 * Send a success peer handshake status message.
1226 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1227 * Returns -1 if there was not enough room left to send the message,
1228 * any other negative returned value must be considered as an error with an appcxt st0
1229 * returned value equal to PEER_SESS_ST_END.
1230 */
1231static inline int peer_send_status_successmsg(struct appctx *appctx)
1232{
1233 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1234}
1235
1236/*
1237 * Send a peer handshake status error message.
1238 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1239 * Returns -1 if there was not enough room left to send the message,
1240 * any other negative returned value must be considered as an error with an appcxt st0
1241 * returned value equal to PEER_SESS_ST_END.
1242 */
1243static inline int peer_send_status_errormsg(struct appctx *appctx)
1244{
1245 struct peer_prep_params p = {
1246 .error_status.st1 = appctx->st1,
1247 };
1248
1249 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1250}
1251
1252/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001253 * Send a stick-table switch message.
1254 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1255 * Returns -1 if there was not enough room left to send the message,
1256 * any other negative returned value must be considered as an error with an appcxt st0
1257 * returned value equal to PEER_SESS_ST_END.
1258 */
1259static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1260{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001261 struct peer_prep_params p = {
1262 .swtch.shared_table = st,
1263 };
1264
1265 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001266}
1267
1268/*
1269 * Send a stick-table update acknowledgement message.
1270 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1271 * Returns -1 if there was not enough room left to send the message,
1272 * any other negative returned value must be considered as an error with an appcxt st0
1273 * returned value equal to PEER_SESS_ST_END.
1274 */
1275static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1276{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001277 struct peer_prep_params p = {
1278 .ack.shared_table = st,
1279 };
1280
1281 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001282}
1283
1284/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001285 * Send a stick-table update message.
1286 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1287 * Returns -1 if there was not enough room left to send the message,
1288 * any other negative returned value must be considered as an error with an appcxt st0
1289 * returned value equal to PEER_SESS_ST_END.
1290 */
1291static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1292 unsigned int updateid, int use_identifier, int use_timed)
1293{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001294 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001295 .updt = {
1296 .stksess = ts,
1297 .shared_table = st,
1298 .updateid = updateid,
1299 .use_identifier = use_identifier,
1300 .use_timed = use_timed,
1301 .peer = appctx->ctx.peers.ptr,
1302 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001303 };
1304
1305 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001306}
1307
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001308/*
1309 * Build a peer protocol control class message.
1310 * Returns the number of written bytes used to build the message if succeeded,
1311 * 0 if not.
1312 */
1313static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1314{
1315 if (size < sizeof p->control.head)
1316 return 0;
1317
1318 msg[0] = p->control.head[0];
1319 msg[1] = p->control.head[1];
1320
1321 return 2;
1322}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001323
1324/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001325 * Send a stick-table synchronization request message.
1326 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1327 * Returns -1 if there was not enough room left to send the message,
1328 * any other negative returned value must be considered as an error with an appctx st0
1329 * returned value equal to PEER_SESS_ST_END.
1330 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001331static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1332 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001333{
1334 struct peer_prep_params p = {
1335 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1336 };
1337
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001338 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1339 NULL, &p.control.head[1], peers->local->id, peer->id);
1340
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001341 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1342}
1343
1344/*
1345 * Send a stick-table synchronization confirmation message.
1346 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1347 * Returns -1 if there was not enough room left to send the message,
1348 * any other negative returned value must be considered as an error with an appctx st0
1349 * returned value equal to PEER_SESS_ST_END.
1350 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001351static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1352 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001353{
1354 struct peer_prep_params p = {
1355 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1356 };
1357
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001358 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1359 NULL, &p.control.head[1], peers->local->id, peer->id);
1360
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001361 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1362}
1363
1364/*
1365 * Send a stick-table synchronization finished message.
1366 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1367 * Returns -1 if there was not enough room left to send the message,
1368 * any other negative returned value must be considered as an error with an appctx st0
1369 * returned value equal to PEER_SESS_ST_END.
1370 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001371static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1372 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001373{
1374 struct peer_prep_params p = {
1375 .control.head = { PEER_MSG_CLASS_CONTROL, },
1376 };
1377
Emeric Brun70de43b2020-03-16 10:51:01 +01001378 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001379 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1380
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001381 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1382 NULL, &p.control.head[1], peers->local->id, peer->id);
1383
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001384 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1385}
1386
1387/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001388 * Send a heartbeat message.
1389 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1390 * Returns -1 if there was not enough room left to send the message,
1391 * any other negative returned value must be considered as an error with an appctx st0
1392 * returned value equal to PEER_SESS_ST_END.
1393 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001394static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1395 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001396{
1397 struct peer_prep_params p = {
1398 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1399 };
1400
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001401 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1402 NULL, &p.control.head[1], peers->local->id, peer->id);
1403
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001404 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1405}
1406
1407/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001408 * Build a peer protocol error class message.
1409 * Returns the number of written bytes used to build the message if succeeded,
1410 * 0 if not.
1411 */
1412static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1413{
1414 if (size < sizeof p->error.head)
1415 return 0;
1416
1417 msg[0] = p->error.head[0];
1418 msg[1] = p->error.head[1];
1419
1420 return 2;
1421}
1422
1423/*
1424 * Send a "size limit reached" error message.
1425 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1426 * Returns -1 if there was not enough room left to send the message,
1427 * any other negative returned value must be considered as an error with an appctx st0
1428 * returned value equal to PEER_SESS_ST_END.
1429 */
1430static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1431{
1432 struct peer_prep_params p = {
1433 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1434 };
1435
1436 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1437}
1438
1439/*
1440 * Send a "peer protocol" error message.
1441 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1442 * Returns -1 if there was not enough room left to send the message,
1443 * any other negative returned value must be considered as an error with an appctx st0
1444 * returned value equal to PEER_SESS_ST_END.
1445 */
1446static inline int peer_send_error_protomsg(struct appctx *appctx)
1447{
1448 struct peer_prep_params p = {
1449 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1450 };
1451
1452 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1453}
1454
1455/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001456 * Function used to lookup for recent stick-table updates associated with
1457 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1458 */
1459static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1460{
1461 struct eb32_node *eb;
1462
1463 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1464 if (!eb) {
1465 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001466 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001467 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1468 return NULL;
1469 }
1470 }
1471
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001472 /* if distance between the last pushed and the retrieved key
1473 * is greater than the distance last_pushed and the local_update
1474 * this means we are beyond localupdate.
1475 */
1476 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001477 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1478 return NULL;
1479 }
1480
1481 return eb32_entry(eb, struct stksess, upd);
1482}
1483
1484/*
1485 * Function used to lookup for recent stick-table updates associated with
1486 * <st> shared stick-table during teach state 1 step.
1487 */
1488static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1489{
1490 struct eb32_node *eb;
1491
1492 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1493 if (!eb) {
1494 st->flags |= SHTABLE_F_TEACH_STAGE1;
1495 eb = eb32_first(&st->table->updates);
1496 if (eb)
1497 st->last_pushed = eb->key - 1;
1498 return NULL;
1499 }
1500
1501 return eb32_entry(eb, struct stksess, upd);
1502}
1503
1504/*
1505 * Function used to lookup for recent stick-table updates associated with
1506 * <st> shared stick-table during teach state 2 step.
1507 */
1508static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1509{
1510 struct eb32_node *eb;
1511
1512 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1513 if (!eb || eb->key > st->teaching_origin) {
1514 st->flags |= SHTABLE_F_TEACH_STAGE2;
1515 return NULL;
1516 }
1517
1518 return eb32_entry(eb, struct stksess, upd);
1519}
1520
1521/*
1522 * Generic function to emit update messages for <st> stick-table when a lesson must
1523 * be taught to the peer <p>.
1524 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1525 * this function, 0 if not.
1526 *
1527 * This function temporary unlock/lock <st> when it sends stick-table updates or
1528 * when decrementing its refcount in case of any error when it sends this updates.
1529 *
1530 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1531 * Returns -1 if there was not enough room left to send the message,
1532 * any other negative returned value must be considered as an error with an appcxt st0
1533 * returned value equal to PEER_SESS_ST_END.
1534 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1535 * unlocked if not already locked when entering this function.
1536 */
1537static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1538 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1539 struct shared_table *st, int locked)
1540{
1541 int ret, new_pushed, use_timed;
1542
1543 ret = 1;
1544 use_timed = 0;
1545 if (st != p->last_local_table) {
1546 ret = peer_send_switchmsg(st, appctx);
1547 if (ret <= 0)
1548 return ret;
1549
1550 p->last_local_table = st;
1551 }
1552
1553 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1554 use_timed = !(p->flags & PEER_F_DWNGRD);
1555
1556 /* We force new pushed to 1 to force identifier in update message */
1557 new_pushed = 1;
1558
1559 if (!locked)
1560 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1561
1562 while (1) {
1563 struct stksess *ts;
1564 unsigned updateid;
1565
1566 /* push local updates */
1567 ts = peer_stksess_lookup(st);
1568 if (!ts)
1569 break;
1570
1571 updateid = ts->upd.key;
1572 ts->ref_cnt++;
1573 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1574
1575 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1576 if (ret <= 0) {
1577 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1578 ts->ref_cnt--;
1579 if (!locked)
1580 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1581 return ret;
1582 }
1583
1584 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1585 ts->ref_cnt--;
1586 st->last_pushed = updateid;
1587
1588 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1589 (int)(st->last_pushed - st->table->commitupdate) > 0)
1590 st->table->commitupdate = st->last_pushed;
1591
1592 /* identifier may not needed in next update message */
1593 new_pushed = 0;
1594 }
1595
1596 out:
1597 if (!locked)
1598 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1599 return 1;
1600}
1601
1602/*
1603 * Function to emit update messages for <st> stick-table when a lesson must
1604 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1605 *
1606 * Note that <st> shared stick-table is locked when calling this function.
1607 *
1608 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1609 * Returns -1 if there was not enough room left to send the message,
1610 * any other negative returned value must be considered as an error with an appcxt st0
1611 * returned value equal to PEER_SESS_ST_END.
1612 */
1613static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1614 struct shared_table *st)
1615{
1616 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1617}
1618
1619/*
1620 * Function to emit update messages for <st> stick-table when a lesson must
1621 * be taught to the peer <p> during teach state 1 step.
1622 *
1623 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1624 * Returns -1 if there was not enough room left to send the message,
1625 * any other negative returned value must be considered as an error with an appcxt st0
1626 * returned value equal to PEER_SESS_ST_END.
1627 */
1628static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1629 struct shared_table *st)
1630{
1631 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1632}
1633
1634/*
1635 * Function to emit update messages for <st> stick-table when a lesson must
1636 * be taught to the peer <p> during teach state 1 step.
1637 *
1638 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1639 * Returns -1 if there was not enough room left to send the message,
1640 * any other negative returned value must be considered as an error with an appcxt st0
1641 * returned value equal to PEER_SESS_ST_END.
1642 */
1643static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1644 struct shared_table *st)
1645{
1646 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1647}
1648
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001649
1650/*
1651 * Function used to parse a stick-table update message after it has been received
1652 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1653 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1654 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1655 * was encountered.
1656 * <exp> must be set if the stick-table entry expires.
1657 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001658 * 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 +01001659 * update ID.
1660 * <totl> is the length of the stick-table update message computed upon receipt.
1661 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001662static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1663 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001664{
Christopher Faulet86e1c332021-12-20 17:09:39 +01001665 struct stream_interface *si = cs_si(appctx->owner);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001666 struct shared_table *st = p->remote_table;
1667 struct stksess *ts, *newts;
1668 uint32_t update;
1669 int expire;
1670 unsigned int data_type;
1671 void *data_ptr;
1672
Frédéric Lécailled8659352020-11-10 16:18:03 +01001673 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001674 /* Here we have data message */
1675 if (!st)
1676 goto ignore_msg;
1677
1678 expire = MS_TO_TICKS(st->table->expire);
1679
1680 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001681 if (msg_len < sizeof(update)) {
1682 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001683 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001684 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001685
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001686 memcpy(&update, *msg_cur, sizeof(update));
1687 *msg_cur += sizeof(update);
1688 st->last_get = htonl(update);
1689 }
1690 else {
1691 st->last_get++;
1692 }
1693
1694 if (exp) {
1695 size_t expire_sz = sizeof expire;
1696
Frédéric Lécailled8659352020-11-10 16:18:03 +01001697 if (*msg_cur + expire_sz > msg_end) {
1698 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1699 NULL, p, *msg_cur);
1700 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1701 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001702 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001703 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001704
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001705 memcpy(&expire, *msg_cur, expire_sz);
1706 *msg_cur += expire_sz;
1707 expire = ntohl(expire);
1708 }
1709
1710 newts = stksess_new(st->table, NULL);
1711 if (!newts)
1712 goto ignore_msg;
1713
1714 if (st->table->type == SMP_T_STR) {
1715 unsigned int to_read, to_store;
1716
1717 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001718 if (!*msg_cur) {
1719 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001720 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001721 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001722
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001723 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001724 if (*msg_cur + to_store > msg_end) {
1725 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1726 NULL, p, *msg_cur);
1727 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1728 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001729 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001730 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001731
1732 memcpy(newts->key.key, *msg_cur, to_store);
1733 newts->key.key[to_store] = 0;
1734 *msg_cur += to_read;
1735 }
1736 else if (st->table->type == SMP_T_SINT) {
1737 unsigned int netinteger;
1738
Frédéric Lécailled8659352020-11-10 16:18:03 +01001739 if (*msg_cur + sizeof(netinteger) > msg_end) {
1740 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1741 NULL, p, *msg_cur);
1742 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1743 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001744 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001745 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001746
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001747 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1748 netinteger = ntohl(netinteger);
1749 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1750 *msg_cur += sizeof(netinteger);
1751 }
1752 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001753 if (*msg_cur + st->table->key_size > msg_end) {
1754 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1755 NULL, p, *msg_cur);
1756 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1757 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001758 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001759 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001760
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001761 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1762 *msg_cur += st->table->key_size;
1763 }
1764
1765 /* lookup for existing entry */
1766 ts = stktable_set_entry(st->table, newts);
1767 if (ts != newts) {
1768 stksess_free(st->table, newts);
1769 newts = NULL;
1770 }
1771
1772 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1773
1774 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001775 uint64_t decoded_int;
Emeric Brun90a9b672021-06-22 16:09:55 +02001776 unsigned int idx;
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001777 int ignore;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001778
Emeric Brun08b0f672021-07-01 18:54:05 +02001779 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001780 continue;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001781
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001782 ignore = stktable_data_types[data_type].is_local;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001783
Emeric Brun90a9b672021-06-22 16:09:55 +02001784 if (stktable_data_types[data_type].is_array) {
1785 /* in case of array all elements
1786 * use the same std_type and they
1787 * are linearly encoded.
1788 * The number of elements was provided
1789 * by table definition message
1790 */
1791 switch (stktable_data_types[data_type].std_type) {
1792 case STD_T_SINT:
1793 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1794 decoded_int = intdecode(msg_cur, msg_end);
1795 if (!*msg_cur) {
1796 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1797 goto malformed_unlock;
1798 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001799
Emeric Brun90a9b672021-06-22 16:09:55 +02001800 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001801 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001802 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
1803 }
1804 break;
1805 case STD_T_UINT:
1806 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1807 decoded_int = intdecode(msg_cur, msg_end);
1808 if (!*msg_cur) {
1809 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1810 goto malformed_unlock;
1811 }
1812
1813 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001814 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001815 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
1816 }
1817 break;
1818 case STD_T_ULL:
1819 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1820 decoded_int = intdecode(msg_cur, msg_end);
1821 if (!*msg_cur) {
1822 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1823 goto malformed_unlock;
1824 }
1825
1826 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001827 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001828 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
1829 }
1830 break;
1831 case STD_T_FRQP:
1832 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1833 struct freq_ctr data;
1834
1835 /* First bit is reserved for the freq_ctr lock
1836 * Note: here we're still protected by the stksess lock
1837 * so we don't need to update the update the freq_ctr
1838 * using its internal lock.
1839 */
1840
1841 decoded_int = intdecode(msg_cur, msg_end);
1842 if (!*msg_cur) {
1843 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1844 goto malformed_unlock;
1845 }
1846
1847 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
1848 data.curr_ctr = intdecode(msg_cur, msg_end);
1849 if (!*msg_cur) {
1850 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1851 goto malformed_unlock;
1852 }
1853
1854 data.prev_ctr = intdecode(msg_cur, msg_end);
1855 if (!*msg_cur) {
1856 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1857 goto malformed_unlock;
1858 }
1859
1860 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001861 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001862 stktable_data_cast(data_ptr, std_t_frqp) = data;
1863 }
1864 break;
1865 }
1866
1867 /* array is fully decoded
1868 * proceed next data_type.
1869 */
1870 continue;
1871 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001872 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001873 if (!*msg_cur) {
1874 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001875 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001876 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001877
Willy Tarreau1e82a142019-01-29 11:08:06 +01001878 switch (stktable_data_types[data_type].std_type) {
1879 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001880 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001881 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001882 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001883 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001884
Willy Tarreau1e82a142019-01-29 11:08:06 +01001885 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001886 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001887 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001888 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001889 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001890
Willy Tarreau1e82a142019-01-29 11:08:06 +01001891 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001892 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001893 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001894 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001895 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001896
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001897 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001898 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001899
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001900 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001901 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001902 so we don't need to update the update the freq_ctr
Emeric Brun90a9b672021-06-22 16:09:55 +02001903 using its internal lock.
1904 */
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001905
Willy Tarreau1e82a142019-01-29 11:08:06 +01001906 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001907 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001908 if (!*msg_cur) {
1909 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001910 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001911 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001912
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001913 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001914 if (!*msg_cur) {
1915 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001916 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001917 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001918
1919 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001920 if (data_ptr && !ignore)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001921 stktable_data_cast(data_ptr, std_t_frqp) = data;
1922 break;
1923 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001924 case STD_T_DICT: {
1925 struct buffer *chunk;
1926 size_t data_len, value_len;
1927 unsigned int id;
1928 struct dict_entry *de;
1929 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001930 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001931
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001932 if (!decoded_int) {
1933 /* No entry. */
1934 break;
1935 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001936 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001937 if (*msg_cur + data_len > msg_end) {
1938 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1939 NULL, p, *msg_cur);
1940 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1941 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001942 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001943 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001944
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001945 /* Compute the end of the current data, <msg_end> being at the end of
1946 * the entire message.
1947 */
1948 end = *msg_cur + data_len;
1949 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001950 if (!*msg_cur || !id) {
1951 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1952 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001953 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001954 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001955
1956 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001957 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001958 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001959 if (id > dc->max_entries) {
1960 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1961 NULL, p, NULL, &id);
1962 goto malformed_unlock;
1963 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001964 /* IDs sent over the network are numbered from 1. */
1965 de = dc->rx[id - 1].de;
1966 }
1967 else {
1968 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001969 value_len = intdecode(msg_cur, end);
1970 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01001971 unlikely(value_len + 1 >= chunk->size)) {
1972 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1973 NULL, p, *msg_cur, &value_len);
1974 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1975 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001976 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001977 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001978
1979 chunk_memcpy(chunk, *msg_cur, value_len);
1980 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001981 *msg_cur += value_len;
1982
Thayne McCombs92149f92020-11-20 01:28:26 -07001983 de = dict_insert(&server_key_dict, chunk->area);
1984 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001985 dc->rx[id - 1].de = de;
1986 }
1987 if (de) {
1988 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001989 if (data_ptr && !ignore) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001990 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001991 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07001992 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001993 }
1994 break;
1995 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001996 }
1997 }
1998 /* Force new expiration */
1999 ts->expire = tick_add(now_ms, expire);
2000
2001 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2002 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002003 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002004 return 1;
2005
2006 ignore_msg:
2007 /* skip consumed message */
2008 co_skip(si_oc(si), totl);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002009 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002010 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01002011
2012 malformed_unlock:
2013 /* malformed message */
2014 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2015 stktable_touch_remote(st->table, ts, 1);
2016 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002017 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002018 return 0;
2019
2020 malformed_free_newts:
2021 /* malformed message */
2022 stksess_free(st->table, newts);
2023 malformed_exit:
2024 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002025 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002026 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002027}
2028
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01002029/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002030 * Function used to parse a stick-table update acknowledgement message after it
2031 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
2032 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2033 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2034 * was encountered.
2035 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2036 */
2037static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
2038 char **msg_cur, char *msg_end)
2039{
2040 /* ack message */
2041 uint32_t table_id ;
2042 uint32_t update;
2043 struct shared_table *st;
2044
Emeric Brunb0d60be2021-03-04 10:27:10 +01002045 /* ignore ack during teaching process */
2046 if (p->flags & PEER_F_TEACH_PROCESS)
2047 return 1;
2048
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002049 table_id = intdecode(msg_cur, msg_end);
2050 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
2051 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002052
2053 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
2054 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002055 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2056 return 0;
2057 }
2058
2059 memcpy(&update, *msg_cur, sizeof(update));
2060 update = ntohl(update);
2061
2062 for (st = p->tables; st; st = st->next) {
2063 if (st->local_id == table_id) {
2064 st->update = update;
2065 break;
2066 }
2067 }
2068
2069 return 1;
2070}
2071
2072/*
2073 * Function used to parse a stick-table switch message after it has been received
2074 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2075 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2076 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2077 * was encountered.
2078 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2079 */
2080static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
2081 char **msg_cur, char *msg_end)
2082{
2083 struct shared_table *st;
2084 int table_id;
2085
2086 table_id = intdecode(msg_cur, msg_end);
2087 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002088 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002089 /* malformed message */
2090 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2091 return 0;
2092 }
2093
2094 p->remote_table = NULL;
2095 for (st = p->tables; st; st = st->next) {
2096 if (st->remote_id == table_id) {
2097 p->remote_table = st;
2098 break;
2099 }
2100 }
2101
2102 return 1;
2103}
2104
2105/*
2106 * Function used to parse a stick-table definition message after it has been received
2107 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2108 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2109 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2110 * was encountered.
2111 * <totl> is the length of the stick-table update message computed upon receipt.
2112 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2113 */
2114static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
2115 char **msg_cur, char *msg_end, int totl)
2116{
Christopher Faulet86e1c332021-12-20 17:09:39 +01002117 struct stream_interface *si = cs_si(appctx->owner);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002118 int table_id_len;
2119 struct shared_table *st;
2120 int table_type;
2121 int table_keylen;
2122 int table_id;
2123 uint64_t table_data;
2124
2125 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002126 if (!*msg_cur) {
2127 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002128 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002129 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002130
2131 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002132 if (!*msg_cur) {
2133 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002134 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002135 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002136
2137 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002138 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
2139 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002140 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002141 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002142
2143 for (st = p->tables; st; st = st->next) {
2144 /* Reset IDs */
2145 if (st->remote_id == table_id)
2146 st->remote_id = 0;
2147
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01002148 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
2149 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002150 p->remote_table = st;
2151 }
2152
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002153 if (!p->remote_table) {
2154 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002155 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002156 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002157
2158 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002159 if (*msg_cur >= msg_end) {
2160 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002161 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002162 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002163
2164 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002165 if (!*msg_cur) {
2166 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002167 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002168 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002169
2170 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002171 if (!*msg_cur) {
2172 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002173 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002174 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002175
2176 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002177 if (!*msg_cur) {
2178 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002179 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002180 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002181
Emeric Brun530ba382020-06-02 11:17:42 +02002182 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002183 || p->remote_table->table->key_size != table_keylen) {
2184 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002185 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002186 goto ignore_msg;
2187 }
2188
Ilya Shipitsin01881082021-08-07 14:41:56 +05002189 /* Check if there there is the additional expire data */
Emeric Brun90a9b672021-06-22 16:09:55 +02002190 intdecode(msg_cur, msg_end);
2191 if (*msg_cur) {
2192 uint64_t data_type;
2193 uint64_t type;
2194
2195 /* This define contains the expire data so we consider
2196 * it also contain all data_types parameters.
2197 */
2198 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2199 if (table_data & (1ULL << data_type)) {
2200 if (stktable_data_types[data_type].is_array) {
2201 /* This should be an array
2202 * so we parse the data_type prefix
2203 * because we must have parameters.
2204 */
2205 type = intdecode(msg_cur, msg_end);
2206 if (!*msg_cur) {
2207 p->remote_table = NULL;
2208 TRACE_PROTO("missing meta data for array", PEERS_EV_DEFMSG, NULL, p);
2209 goto ignore_msg;
2210 }
2211
2212 /* check if the data_type match the current from the bitfield */
2213 if (type != data_type) {
2214 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002215 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002216 goto ignore_msg;
2217 }
2218
2219 /* decode the nbelem of the array */
2220 p->remote_table->remote_data_nbelem[type] = intdecode(msg_cur, msg_end);
2221 if (!*msg_cur) {
2222 p->remote_table = NULL;
2223 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2224 goto ignore_msg;
2225 }
2226
2227 /* if it is an array of frqp, we must also have the period to decode */
2228 if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2229 intdecode(msg_cur, msg_end);
2230 if (!*msg_cur) {
2231 p->remote_table = NULL;
2232 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2233 goto ignore_msg;
2234 }
2235 }
2236 }
2237 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2238 /* This should be a std freq counter data_type
2239 * so we parse the data_type prefix
2240 * because we must have parameters.
2241 */
2242 type = intdecode(msg_cur, msg_end);
2243 if (!*msg_cur) {
2244 p->remote_table = NULL;
2245 TRACE_PROTO("missing meta data for frqp", PEERS_EV_DEFMSG, NULL, p);
2246 goto ignore_msg;
2247 }
2248
2249 /* check if the data_type match the current from the bitfield */
2250 if (type != data_type) {
2251 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002252 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002253 goto ignore_msg;
2254 }
2255
2256 /* decode the period */
2257 intdecode(msg_cur, msg_end);
2258 if (!*msg_cur) {
2259 p->remote_table = NULL;
2260 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2261 goto ignore_msg;
2262 }
2263 }
2264 }
2265 }
2266 }
2267 else {
2268 uint64_t data_type;
2269
2270 /* There is not additional data but
2271 * array size parameter is mandatory to parse array
2272 * so we consider an error if an array data_type is define
2273 * but there is no additional data.
2274 */
2275 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2276 if (table_data & (1ULL << data_type)) {
2277 if (stktable_data_types[data_type].is_array) {
2278 p->remote_table = NULL;
2279 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2280 goto ignore_msg;
2281 }
2282 }
2283 }
2284 }
2285
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002286 p->remote_table->remote_data = table_data;
2287 p->remote_table->remote_id = table_id;
2288 return 1;
2289
2290 ignore_msg:
2291 co_skip(si_oc(si), totl);
2292 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01002293
2294 malformed_exit:
2295 /* malformed message */
2296 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2297 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002298}
2299
2300/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002301 * Receive a stick-table message or pre-parse any other message.
2302 * The message's header will be sent into <msg_head> which must be at least
2303 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
2304 * The first two bytes are always read, and the rest is only read if the
2305 * first bytes indicate a stick-table message. If the message is a stick-table
2306 * message, the varint is decoded and the equivalent number of bytes will be
2307 * copied into the trash at trash.area. <totl> is incremented by the number of
2308 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002309 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
2310 * -1 if there was an error updating the appctx state st0 accordingly.
2311 */
2312static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2313 uint32_t *msg_len, int *totl)
2314{
2315 int reql;
Christopher Faulet86e1c332021-12-20 17:09:39 +01002316 struct stream_interface *si = cs_si(appctx->owner);
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002317 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002318
2319 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
2320 if (reql <= 0) /* closed or EOL not found */
2321 goto incomplete;
2322
2323 *totl += reql;
2324
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002325 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002326 return 1;
2327
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002328 /* This is a stick-table message, let's go on */
2329
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002330 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002331 msg_head += *totl;
2332 msg_head_sz -= *totl;
2333 reql = co_data(si_oc(si)) - *totl;
2334 if (reql > msg_head_sz)
2335 reql = msg_head_sz;
2336
2337 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002338 if (reql <= 0) /* closed */
2339 goto incomplete;
2340
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002341 cur = msg_head;
2342 *msg_len = intdecode(&cur, cur + reql);
2343 if (!cur) {
2344 /* the number is truncated, did we read enough ? */
2345 if (reql < msg_head_sz)
2346 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002347
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002348 /* malformed message */
2349 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2350 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2351 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002352 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002353 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002354
2355 /* Read message content */
2356 if (*msg_len) {
2357 if (*msg_len > trash.size) {
2358 /* Status code is not success, abort */
2359 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2360 return -1;
2361 }
2362
2363 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
2364 if (reql <= 0) /* closed */
2365 goto incomplete;
2366 *totl += reql;
2367 }
2368
2369 return 1;
2370
2371 incomplete:
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002372 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
2373 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002374 appctx->st0 = PEER_SESS_ST_END;
2375 return -1;
2376 }
2377
2378 return 0;
2379}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002380
2381/*
2382 * Treat the awaited message with <msg_head> as header.*
2383 * Return 1 if succeeded, 0 if not.
2384 */
2385static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2386 char **msg_cur, char *msg_end, int msg_len, int totl)
2387{
Christopher Faulet86e1c332021-12-20 17:09:39 +01002388 struct stream_interface *si = cs_si(appctx->owner);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002389 struct stream *s = si_strm(si);
2390 struct peers *peers = strm_fe(s)->parent;
2391
2392 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2393 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2394 struct shared_table *st;
2395 /* Reset message: remote need resync */
2396
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002397 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2398 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002399 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002400 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002401 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002402 st->flags = 0;
2403 }
2404
2405 /* reset teaching flags to 0 */
2406 peer->flags &= PEER_TEACH_RESET;
2407
2408 /* flag to start to teach lesson */
2409 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002410 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002411 }
2412 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002413 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2414 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002415 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2416 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2417 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2418 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002419 if (peer->local)
2420 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2421 else
2422 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002423 }
2424 peer->confirm++;
2425 }
2426 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002427 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2428 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002429 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2430 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2431 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2432
Emeric Brunccdfbae2021-04-28 12:59:35 +02002433 if (peer->local)
2434 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2435 else
2436 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002437 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002438 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002439 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2440 }
2441 peer->confirm++;
2442 }
2443 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2444 struct shared_table *st;
2445
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002446 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2447 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002448 /* If stopping state */
2449 if (stopping) {
2450 /* Close session, push resync no more needed */
2451 peer->flags |= PEER_F_TEACH_COMPLETE;
2452 appctx->st0 = PEER_SESS_ST_END;
2453 return 0;
2454 }
2455 for (st = peer->tables; st; st = st->next) {
2456 st->update = st->last_pushed = st->teaching_origin;
2457 st->flags = 0;
2458 }
2459
2460 /* reset teaching flags to 0 */
2461 peer->flags &= PEER_TEACH_RESET;
2462 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002463 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002464 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2465 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002466 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002467 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002468 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002469 }
2470 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2471 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2472 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2473 return 0;
2474 }
2475 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2476 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2477 return 0;
2478 }
2479 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2480 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2481 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2482 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2483 int update, expire;
2484
2485 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2486 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2487 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2488 msg_cur, msg_end, msg_len, totl))
2489 return 0;
2490
2491 }
2492 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2493 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2494 return 0;
2495 }
2496 }
2497 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2498 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2499 return 0;
2500 }
2501
2502 return 1;
2503}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002504
2505
2506/*
2507 * Send any message to <peer> peer.
2508 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002509 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002510 * to a peer state change (from the peer I/O handler point of view).
2511 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002512static inline int peer_send_msgs(struct appctx *appctx,
2513 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002514{
2515 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002516
2517 /* Need to request a resync */
2518 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2519 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2520 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2521
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002522 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002523 if (repl <= 0)
2524 return repl;
2525
2526 peers->flags |= PEERS_F_RESYNC_PROCESS;
2527 }
2528
2529 /* Nothing to read, now we start to write */
2530 if (peer->tables) {
2531 struct shared_table *st;
2532 struct shared_table *last_local_table;
2533
2534 last_local_table = peer->last_local_table;
2535 if (!last_local_table)
2536 last_local_table = peer->tables;
2537 st = last_local_table->next;
2538
2539 while (1) {
2540 if (!st)
2541 st = peer->tables;
2542
2543 /* It remains some updates to ack */
2544 if (st->last_get != st->last_acked) {
2545 repl = peer_send_ackmsg(st, appctx);
2546 if (repl <= 0)
2547 return repl;
2548
2549 st->last_acked = st->last_get;
2550 }
2551
2552 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2553 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2554 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002555 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002556
2557 repl = peer_send_teach_process_msgs(appctx, peer, st);
2558 if (repl <= 0) {
2559 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2560 return repl;
2561 }
2562 }
2563 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2564 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002565 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002566 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2567 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2568 if (repl <= 0)
2569 return repl;
2570 }
2571
2572 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2573 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2574 if (repl <= 0)
2575 return repl;
2576 }
2577 }
2578
2579 if (st == last_local_table)
2580 break;
2581 st = st->next;
2582 }
2583 }
2584
2585 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002586 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002587 if (repl <= 0)
2588 return repl;
2589
2590 /* flag finished message sent */
2591 peer->flags |= PEER_F_TEACH_FINISHED;
2592 }
2593
2594 /* Confirm finished or partial messages */
2595 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002596 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002597 if (repl <= 0)
2598 return repl;
2599
2600 peer->confirm--;
2601 }
2602
2603 return 1;
2604}
2605
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002606/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002607 * Read and parse a first line of a "hello" peer protocol message.
2608 * Returns 0 if could not read a line, -1 if there was a read error or
2609 * the line is malformed, 1 if succeeded.
2610 */
2611static inline int peer_getline_version(struct appctx *appctx,
2612 unsigned int *maj_ver, unsigned int *min_ver)
2613{
2614 int reql;
2615
2616 reql = peer_getline(appctx);
2617 if (!reql)
2618 return 0;
2619
2620 if (reql < 0)
2621 return -1;
2622
2623 /* test protocol */
2624 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2625 appctx->st0 = PEER_SESS_ST_EXIT;
2626 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2627 return -1;
2628 }
2629 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2630 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2631 appctx->st0 = PEER_SESS_ST_EXIT;
2632 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2633 return -1;
2634 }
2635
2636 return 1;
2637}
2638
2639/*
2640 * Read and parse a second line of a "hello" peer protocol message.
2641 * Returns 0 if could not read a line, -1 if there was a read error or
2642 * the line is malformed, 1 if succeeded.
2643 */
2644static inline int peer_getline_host(struct appctx *appctx)
2645{
2646 int reql;
2647
2648 reql = peer_getline(appctx);
2649 if (!reql)
2650 return 0;
2651
2652 if (reql < 0)
2653 return -1;
2654
2655 /* test hostname match */
2656 if (strcmp(localpeer, trash.area) != 0) {
2657 appctx->st0 = PEER_SESS_ST_EXIT;
2658 appctx->st1 = PEER_SESS_SC_ERRHOST;
2659 return -1;
2660 }
2661
2662 return 1;
2663}
2664
2665/*
2666 * Read and parse a last line of a "hello" peer protocol message.
2667 * Returns 0 if could not read a character, -1 if there was a read error or
2668 * the line is malformed, 1 if succeeded.
2669 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2670 */
2671static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2672{
2673 char *p;
2674 int reql;
2675 struct peer *peer;
Christopher Faulet86e1c332021-12-20 17:09:39 +01002676 struct stream_interface *si = cs_si(appctx->owner);
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002677 struct stream *s = si_strm(si);
2678 struct peers *peers = strm_fe(s)->parent;
2679
2680 reql = peer_getline(appctx);
2681 if (!reql)
2682 return 0;
2683
2684 if (reql < 0)
2685 return -1;
2686
2687 /* parse line "<peer name> <pid> <relative_pid>" */
2688 p = strchr(trash.area, ' ');
2689 if (!p) {
2690 appctx->st0 = PEER_SESS_ST_EXIT;
2691 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2692 return -1;
2693 }
2694 *p = 0;
2695
2696 /* lookup known peer */
2697 for (peer = peers->remote; peer; peer = peer->next) {
2698 if (strcmp(peer->id, trash.area) == 0)
2699 break;
2700 }
2701
2702 /* if unknown peer */
2703 if (!peer) {
2704 appctx->st0 = PEER_SESS_ST_EXIT;
2705 appctx->st1 = PEER_SESS_SC_ERRPEER;
2706 return -1;
2707 }
2708 *curpeer = peer;
2709
2710 return 1;
2711}
2712
2713/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002714 * Init <peer> peer after having accepted it at peer protocol level.
2715 */
2716static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2717{
2718 struct shared_table *st;
2719
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002720 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002721 /* Register status code */
2722 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002723 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002724
2725 /* Awake main task */
2726 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2727
2728 /* Init confirm counter */
2729 peer->confirm = 0;
2730
2731 /* Init cursors */
2732 for (st = peer->tables; st ; st = st->next) {
2733 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002734 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2735 /* if st->update appears to be in future it means
2736 * that the last acked value is very old and we
2737 * remain unconnected a too long time to use this
2738 * acknowlegement as a reset.
2739 * We should update the protocol to be able to
2740 * signal the remote peer that it needs a full resync.
2741 * Here a partial fix consist to set st->update at
2742 * the max past value
2743 */
2744 if ((int)(st->table->localupdate - st->update) < 0)
2745 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002746 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002747 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002748 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2749 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002750 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002751 }
2752
2753 /* reset teaching and learning flags to 0 */
2754 peer->flags &= PEER_TEACH_RESET;
2755 peer->flags &= PEER_LEARN_RESET;
2756
2757 /* if current peer is local */
2758 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002759 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002760 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2761 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2762 /* assign local peer for a lesson, consider lesson already requested */
2763 peer->flags |= PEER_F_LEARN_ASSIGN;
2764 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002765 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002766 }
2767
2768 }
2769 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2770 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2771 /* assign peer for a lesson */
2772 peer->flags |= PEER_F_LEARN_ASSIGN;
2773 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002774 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002775 }
2776}
2777
2778/*
2779 * Init <peer> peer after having connected it at peer protocol level.
2780 */
2781static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2782{
2783 struct shared_table *st;
2784
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002785 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002786 /* Init cursors */
2787 for (st = peer->tables; st ; st = st->next) {
2788 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002789 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2790 /* if st->update appears to be in future it means
2791 * that the last acked value is very old and we
2792 * remain unconnected a too long time to use this
2793 * acknowlegement as a reset.
2794 * We should update the protocol to be able to
2795 * signal the remote peer that it needs a full resync.
2796 * Here a partial fix consist to set st->update at
2797 * the max past value.
2798 */
2799 if ((int)(st->table->localupdate - st->update) < 0)
2800 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002801 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002802 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002803 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2804 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002805 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002806 }
2807
2808 /* Init confirm counter */
2809 peer->confirm = 0;
2810
2811 /* reset teaching and learning flags to 0 */
2812 peer->flags &= PEER_TEACH_RESET;
2813 peer->flags &= PEER_LEARN_RESET;
2814
2815 /* If current peer is local */
2816 if (peer->local) {
2817 /* flag to start to teach lesson */
2818 peer->flags |= PEER_F_TEACH_PROCESS;
2819 }
2820 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2821 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2822 /* If peer is remote and resync from remote is needed,
2823 and no peer currently assigned */
2824
2825 /* assign peer for a lesson */
2826 peer->flags |= PEER_F_LEARN_ASSIGN;
2827 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002828 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002829 }
2830}
2831
2832/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002833 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002834 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002835static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002836{
Christopher Faulet86e1c332021-12-20 17:09:39 +01002837 struct stream_interface *si = cs_si(appctx->owner);
Willy Tarreau87b09662015-04-03 00:22:06 +02002838 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002839 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002840 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002841 int reql = 0;
2842 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002843 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002844 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002845
Joseph Herlant82b2f542018-11-15 12:19:14 -08002846 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002847 if (si_ic(si)->buf.size == 0) {
2848 si_rx_room_blk(si);
2849 goto out;
2850 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002851
Emeric Brun2b920a12010-09-23 18:30:22 +02002852 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002853 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002854switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002855 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002857 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002858 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002859 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002860 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002861 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002862 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002863 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002864 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2865 if (reql <= 0) {
2866 if (!reql)
2867 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002868 goto switchstate;
2869 }
2870
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002871 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002872 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002873 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002874 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002875 reql = peer_getline_host(appctx);
2876 if (reql <= 0) {
2877 if (!reql)
2878 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002879 goto switchstate;
2880 }
2881
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002882 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002883 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002884 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002885 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002886 reql = peer_getline_last(appctx, &curpeer);
2887 if (reql <= 0) {
2888 if (!reql)
2889 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002890 goto switchstate;
2891 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002892
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002893 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002894 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002895 if (curpeer->local) {
2896 /* Local connection, reply a retry */
2897 appctx->st0 = PEER_SESS_ST_EXIT;
2898 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2899 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002900 }
Emeric Brun80527f52017-06-19 17:46:37 +02002901
2902 /* we're killing a connection, we must apply a random delay before
2903 * retrying otherwise the other end will do the same and we can loop
2904 * for a while.
2905 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002906 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002907 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002908 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002909 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002910 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002911 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2912 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2913 curpeer->flags |= PEER_F_DWNGRD;
2914 }
2915 else {
2916 curpeer->flags &= ~PEER_F_DWNGRD;
2917 }
2918 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002919 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002920 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002921 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002922 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002923 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002924 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002925 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002926 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002927 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002928 if (!curpeer) {
2929 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002930 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002931 if (curpeer->appctx != appctx) {
2932 appctx->st0 = PEER_SESS_ST_END;
2933 goto switchstate;
2934 }
2935 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002936
2937 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002938 if (repl <= 0) {
2939 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002940 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002941 goto switchstate;
2942 }
2943
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002944 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002945
Emeric Brun2b920a12010-09-23 18:30:22 +02002946 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002947 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002948 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002949 goto switchstate;
2950 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002951 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002952 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002953 if (!curpeer) {
2954 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002955 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002956 if (curpeer->appctx != appctx) {
2957 appctx->st0 = PEER_SESS_ST_END;
2958 goto switchstate;
2959 }
2960 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002961
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002962 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002963 if (repl <= 0) {
2964 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002965 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002966 goto switchstate;
2967 }
2968
2969 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002970 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002971 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002972 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002973 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002974 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002975 if (!curpeer) {
2976 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002977 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002978 if (curpeer->appctx != appctx) {
2979 appctx->st0 = PEER_SESS_ST_END;
2980 goto switchstate;
2981 }
2982 }
2983
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002984 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002985 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002986
Frédéric Lécaillece025572019-01-21 13:38:06 +01002987 reql = peer_getline(appctx);
2988 if (!reql)
2989 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002990
Frédéric Lécaillece025572019-01-21 13:38:06 +01002991 if (reql < 0)
2992 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002993
2994 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002995 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002996 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02002997
2998 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002999 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02003000
3001 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003002 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01003003 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02003004 }
3005 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02003006 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
3007 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02003008 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003009 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02003010 goto switchstate;
3011 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003012 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003013 appctx->st0 = PEER_SESS_ST_WAITMSG;
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_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003017 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003018 char *msg_cur = trash.area;
3019 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01003020 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02003021 int totl = 0;
3022
Willy Tarreau2d372c22018-11-05 17:12:27 +01003023 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003024 if (!curpeer) {
3025 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003026 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003027 if (curpeer->appctx != appctx) {
3028 appctx->st0 = PEER_SESS_ST_END;
3029 goto switchstate;
3030 }
3031 }
3032
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003033 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
3034 if (reql <= 0) {
3035 if (reql == -1)
3036 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003037 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003038 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01003039
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003040 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01003041 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02003042 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003043
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003044 curpeer->flags |= PEER_F_ALIVE;
3045
Emeric Brun2b920a12010-09-23 18:30:22 +02003046 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003047 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02003048 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02003049 goto switchstate;
3050
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003051send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003052 if (curpeer->flags & PEER_F_HEARTBEAT) {
3053 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003054 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003055 if (repl <= 0) {
3056 if (repl == -1)
3057 goto out;
3058 goto switchstate;
3059 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01003060 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003061 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003062 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003063 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01003064 if (repl <= 0) {
3065 if (repl == -1)
3066 goto out;
3067 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02003068 }
3069
Emeric Brun2b920a12010-09-23 18:30:22 +02003070 /* noting more to do */
3071 goto out;
3072 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003073 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01003074 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003075 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003076 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003077 if (peer_send_status_errormsg(appctx) == -1)
3078 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003079 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003080 goto switchstate;
3081 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003082 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003083 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003084 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003085 if (peer_send_error_size_limitmsg(appctx) == -1)
3086 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003087 appctx->st0 = PEER_SESS_ST_END;
3088 goto switchstate;
3089 }
3090 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003091 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
3092 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003093 if (curpeer)
3094 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003095 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003096 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003097 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003098 if (peer_send_error_protomsg(appctx) == -1) {
3099 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003100 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003101 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003102 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003103 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003104 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003105 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003106 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003107 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003108 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003109 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003110 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003111 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003112 curpeer = NULL;
3113 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02003114 si_shutw(si);
3115 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003116 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02003117 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003118 }
3119 }
3120 }
3121out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003122 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02003123
3124 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003125 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02003126 return;
3127}
3128
Willy Tarreau30576452015-04-13 13:50:30 +02003129static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003130 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003131 .name = "<PEER>", /* used for logging */
3132 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07003133 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003134};
Emeric Brun2b920a12010-09-23 18:30:22 +02003135
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003136
Emeric Brun2b920a12010-09-23 18:30:22 +02003137/*
3138 * Use this function to force a close of a peer session
3139 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003140static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003141{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003142 struct appctx *appctx = peer->appctx;
3143
Frédéric Lécaille5df11902017-06-13 16:39:57 +02003144 /* Note that the peer sessions which have just been created
3145 * (->st0 == PEER_SESS_ST_CONNECT) must not
3146 * be shutdown, if not, the TCP session will never be closed
3147 * and stay in CLOSE_WAIT state after having been closed by
3148 * the remote side.
3149 */
3150 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003151 return;
3152
Willy Tarreau81bc3b02016-10-31 17:37:39 +01003153 if (appctx->applet != &peer_applet)
3154 return;
3155
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003156 __peer_session_deinit(peer);
3157
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003158 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01003159 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003160}
3161
Willy Tarreau91d96282015-03-13 15:47:26 +01003162/* Pre-configures a peers frontend to accept incoming connections */
3163void peers_setup_frontend(struct proxy *fe)
3164{
3165 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02003166 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02003167 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01003168 fe->maxconn = 0;
3169 fe->conn_retries = CONN_RETRIES;
3170 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01003171 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01003172 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01003173 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
3174}
3175
Emeric Brun2b920a12010-09-23 18:30:22 +02003176/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01003177 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02003178 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003179static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003180{
Willy Tarreau04b92862017-09-15 11:01:04 +02003181 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003182 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02003183 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01003184 struct conn_stream *cs;
Willy Tarreau87b09662015-04-03 00:22:06 +02003185 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02003186
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01003187 peer->new_conn++;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003188 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003189 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003190 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003191 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003192 s = NULL;
3193
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003194 cs = cs_new();
3195 if (!cs) {
3196 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02003197 goto out_close;
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003198 }
3199
3200 appctx = appctx_new(&peer_applet, cs);
3201 if (!appctx)
3202 goto out_free_cs;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003203
3204 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003205 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003206
Willy Tarreau04b92862017-09-15 11:01:04 +02003207 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02003208 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003209 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02003210 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003211 }
3212
Christopher Faulet13a35e52021-12-20 15:34:16 +01003213 if ((s = stream_new(sess, cs, &BUF_NULL)) == NULL) {
3214 ha_alert("Failed to initialize stream in peer_session_create().\n");
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003215 goto out_free_sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01003216 }
3217
Willy Tarreau6e2979c2015-04-27 13:21:15 +02003218 /* applet is waiting for data */
Christopher Faulet56489e22021-12-23 13:35:56 +01003219 si_cant_get(cs_si(s->csf));
Willy Tarreau6e2979c2015-04-27 13:21:15 +02003220 appctx_wakeup(appctx);
3221
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02003222 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02003223 s->target = peer_session_target(peer, s);
Christopher Faulet56489e22021-12-23 13:35:56 +01003224 if (!sockaddr_alloc(&(cs_si(s->csb)->dst), &peer->addr, sizeof(peer->addr)))
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02003225 goto out_free_strm;
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003226
3227 cs_attach_endp(cs, &appctx->obj_type, appctx);
Willy Tarreau02efeda2019-07-18 17:21:24 +02003228 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Christopher Faulet56489e22021-12-23 13:35:56 +01003229 cs_si(s->csb)->flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003230
Emeric Brun2b920a12010-09-23 18:30:22 +02003231 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003232 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02003233
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003234 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01003235
Emeric Brunb3971ab2015-05-12 18:49:09 +02003236 peer->appctx = appctx;
Willy Tarreau4781b152021-04-06 13:53:36 +02003237 _HA_ATOMIC_INC(&active_peers);
Willy Tarreau9df94c22016-10-31 18:42:52 +01003238 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003239
3240 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02003241 out_free_strm:
Willy Tarreau2b718102021-04-21 07:32:39 +02003242 LIST_DELETE(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003243 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02003244 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02003245 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02003246 out_free_appctx:
3247 appctx_free(appctx);
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003248 out_free_cs:
3249 cs_free(cs);
Emeric Brun2b920a12010-09-23 18:30:22 +02003250 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01003251 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003252}
3253
3254/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003255 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01003256 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
3257 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02003258 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003259struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02003260{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003261 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003262 struct peer *ps;
3263 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02003264
3265 task->expire = TICK_ETERNITY;
3266
Emeric Brunb3971ab2015-05-12 18:49:09 +02003267 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003268 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003269 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003270 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02003271 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003272 return NULL;
3273 }
3274
Emeric Brun80527f52017-06-19 17:46:37 +02003275 /* Acquire lock for all peers of the section */
3276 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003277 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003278
Emeric Brun2b920a12010-09-23 18:30:22 +02003279 if (!stopping) {
3280 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02003281
Emeric Brun2c4ab412021-04-21 16:06:35 +02003282 /* resync timeout set to TICK_ETERNITY means we just start
3283 * a new process and timer was not initialized.
3284 * We must arm this timer to switch to a request to a remote
3285 * node if incoming connection from old local process never
3286 * comes.
3287 */
3288 if (peers->resync_timeout == TICK_ETERNITY)
3289 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3290
Emeric Brunb3971ab2015-05-12 18:49:09 +02003291 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
3292 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
3293 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003294 /* Resync from local peer needed
3295 no peer was assigned for the lesson
3296 and no old local peer found
3297 or resync timeout expire */
3298
3299 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003300 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003301 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003302
3303 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003304 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02003305 }
3306
3307 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003308 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003309 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003310 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003311 if (!ps->appctx) {
3312 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003313 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003314 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003315 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003316 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003317 tick_is_expired(ps->reconnect, now_ms))) {
3318 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003319 * or previous peer connection established with success
3320 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003321 * and reconnection timer is expired */
3322
3323 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003324 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003325 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003326 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003327 /* If previous session failed during connection
3328 * but reconnection timer is not expired */
3329
3330 /* reschedule task for reconnect */
3331 task->expire = tick_first(task->expire, ps->reconnect);
3332 }
3333 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003334 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003335 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003336 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003337 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3338 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003339 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3340 /* Resync from a remote is needed
3341 * and no peer was assigned for lesson
3342 * and current peer may be up2date */
3343
3344 /* assign peer for the lesson */
3345 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003346 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003347 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003348
Willy Tarreau9df94c22016-10-31 18:42:52 +01003349 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003350 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003351 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003352 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003353 int update_to_push = 0;
3354
Emeric Brunb3971ab2015-05-12 18:49:09 +02003355 /* Awake session if there is data to push */
3356 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003357 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003358 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003359 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003360 /* There is no need to send a heartbeat message
3361 * when some updates must be pushed. The remote
3362 * peer will consider <ps> peer as alive when it will
3363 * receive these updates.
3364 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003365 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003366 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003367 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003368 /* We are going to send updates, let's ensure we will
3369 * come back to send heartbeat messages or to reconnect.
3370 */
3371 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003372 appctx_wakeup(ps->appctx);
3373 break;
3374 }
3375 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003376 /* When there are updates to send we do not reconnect
3377 * and do not send heartbeat message either.
3378 */
3379 if (!update_to_push) {
3380 if (tick_is_expired(ps->reconnect, now_ms)) {
3381 if (ps->flags & PEER_F_ALIVE) {
3382 /* This peer was alive during a 'reconnect' period.
3383 * Flag it as not alive again for the next period.
3384 */
3385 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003386 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003387 }
3388 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003389 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003390 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003391 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003392 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003393 }
3394 }
3395 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3396 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3397 ps->flags |= PEER_F_HEARTBEAT;
3398 appctx_wakeup(ps->appctx);
3399 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003400 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003401 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003402 }
3403 /* else do nothing */
3404 } /* SUCCESSCODE */
3405 } /* !ps->peer->local */
3406 } /* for */
3407
3408 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003409 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3410 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3411 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003412 /* Resync from remote peer needed
3413 * no peer was assigned for the lesson
3414 * and resync timeout expire */
3415
3416 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003417 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003418 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003419 }
3420
Emeric Brunb3971ab2015-05-12 18:49:09 +02003421 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003422 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003423 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3424 if (!tick_is_expired(peers->resync_timeout, now_ms))
3425 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003426 }
3427 } /* !stopping */
3428 else {
3429 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003430 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003431 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003432 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003433 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003434 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003435 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003436
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003437 /* disconnect all connected peers to process a local sync
3438 * this must be done only the first time we are switching
3439 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003440 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003441 for (ps = peers->remote; ps; ps = ps->next) {
3442 /* we're killing a connection, we must apply a random delay before
3443 * retrying otherwise the other end will do the same and we can loop
3444 * for a while.
3445 */
3446 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3447 if (ps->appctx) {
3448 peer_session_forceshutdown(ps);
3449 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003450 }
3451 }
3452 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003453
Emeric Brunb3971ab2015-05-12 18:49:09 +02003454 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003455 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003456 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003457 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003458 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003459 peers->flags &= ~PEERS_F_DONOTSTOP;
3460 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003461 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003462 }
3463 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003464 else if (!ps->appctx) {
3465 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003466 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003467 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3468 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3469 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003470 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003471 * or previous peer connection was successfully established
3472 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02003473 * or during previous connect, peer replies a try again statuscode */
3474
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003475 /* connect to the local peer if we must push a local sync */
3476 if (peers->flags & PEERS_F_DONOTSTOP) {
3477 peer_session_create(peers, ps);
3478 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003479 }
3480 else {
3481 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003482 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003483 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003484 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003485 peers->flags &= ~PEERS_F_DONOTSTOP;
3486 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003487 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003488 }
3489 }
3490 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003491 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003492 /* current peer connection is active and established
3493 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003494 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003495 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003496 appctx_wakeup(ps->appctx);
3497 break;
3498 }
3499 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003500 }
3501 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003502
3503 /* Release lock for all peers of the section */
3504 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003505 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003506
Emeric Brun2b920a12010-09-23 18:30:22 +02003507 /* Wakeup for re-connect */
3508 return task;
3509}
3510
Emeric Brunb3971ab2015-05-12 18:49:09 +02003511
Emeric Brun2b920a12010-09-23 18:30:22 +02003512/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003513 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003514 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003515int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003516{
Emeric Brun2b920a12010-09-23 18:30:22 +02003517 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003518
Emeric Brun2b920a12010-09-23 18:30:22 +02003519 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003520 peers->peers_fe->maxconn += 3;
3521 }
3522
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003523 peers->sync_task = task_new_anywhere();
Willy Tarreaud9443442018-10-15 11:18:03 +02003524 if (!peers->sync_task)
3525 return 0;
3526
Emeric Brunb3971ab2015-05-12 18:49:09 +02003527 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003528 peers->sync_task->context = (void *)peers;
3529 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3530 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003531 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003532}
3533
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003534/*
3535 * Allocate a cache a dictionary entries used upon transmission.
3536 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003537static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003538{
3539 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003540 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003541
3542 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003543 entries = calloc(max_entries, sizeof *entries);
3544 if (!d || !entries)
3545 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003546
3547 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003548 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003549 d->cached_entries = EB_ROOT_UNIQUE;
3550 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003551
3552 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003553
3554 err:
3555 free(d);
3556 free(entries);
3557 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003558}
3559
3560/*
3561 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3562 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003563 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003564 * Must be deallocated calling free_dcache().
3565 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003566static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003567{
3568 struct dcache_tx *dc_tx;
3569 struct dcache *dc;
3570 struct dcache_rx *dc_rx;
3571
3572 dc = calloc(1, sizeof *dc);
3573 dc_tx = new_dcache_tx(max_entries);
3574 dc_rx = calloc(max_entries, sizeof *dc_rx);
3575 if (!dc || !dc_tx || !dc_rx)
3576 goto err;
3577
3578 dc->tx = dc_tx;
3579 dc->rx = dc_rx;
3580 dc->max_entries = max_entries;
3581
3582 return dc;
3583
3584 err:
3585 free(dc);
3586 free(dc_tx);
3587 free(dc_rx);
3588 return NULL;
3589}
3590
3591/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003592 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3593 * entries used upon transmission.
3594 * Return the entry if found, NULL if not.
3595 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003596static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3597 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003598{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003599 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003600}
3601
3602/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003603 * Flush <dc> cache.
3604 * Always succeeds.
3605 */
3606static inline void flush_dcache(struct peer *peer)
3607{
3608 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003609 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003610
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003611 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003612 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003613 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003614 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3615 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003616 }
3617 dc->tx->prev_lookup = NULL;
3618 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003619
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003620 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003621}
3622
3623/*
3624 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3625 * with information provided by <i> dictionary cache entry (especially the value
3626 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003627 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003628 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003629static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003630{
3631 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003632 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003633
3634 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003635
3636 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3637 o = dc_tx->prev_lookup;
3638 } else {
3639 o = dcache_tx_lookup_value(dc_tx, i);
3640 if (o) {
3641 /* Save it */
3642 dc_tx->prev_lookup = o;
3643 }
3644 }
3645
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003646 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003647 /* Copy the ID. */
3648 i->id = o - dc->tx->entries;
3649 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003650 }
3651
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003652 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003653 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003654
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003655 ebpt_delete(o);
3656 o->key = i->entry.key;
3657 ebpt_insert(&dc_tx->cached_entries, o);
3658 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003659
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003660 /* Update the index for the next entry to put in cache */
3661 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003662
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003663 return o;
3664}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003665
3666/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003667 * Allocate a dictionary cache for each peer of <peers> section.
3668 * Return 1 if succeeded, 0 if not.
3669 */
3670int peers_alloc_dcache(struct peers *peers)
3671{
3672 struct peer *p;
3673
3674 for (p = peers->remote; p; p = p->next) {
3675 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3676 if (!p->dcache)
3677 return 0;
3678 }
3679
3680 return 1;
3681}
3682
3683/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003684 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003685 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003686 */
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003687int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003688{
3689 struct shared_table *st;
3690 struct peer * curpeer;
3691 int id = 0;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003692 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003693
3694 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003695 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003696 if (!st) {
3697 retval = 1;
3698 break;
3699 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003700 st->table = table;
3701 st->next = curpeer->tables;
3702 if (curpeer->tables)
3703 id = curpeer->tables->local_id;
3704 st->local_id = id + 1;
3705
Emeric Brun2cc201f2021-04-23 12:21:26 +02003706 /* If peer is local we inc table
3707 * refcnt to protect against flush
3708 * until this process pushed all
3709 * table content to the new one
3710 */
3711 if (curpeer->local)
3712 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003713 curpeer->tables = st;
3714 }
3715
3716 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003717
3718 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003719}
3720
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003721/*
3722 * Parse the "show peers" command arguments.
3723 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3724 * error message.
3725 */
3726static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3727{
3728 appctx->ctx.cfgpeers.target = NULL;
3729
Willy Tarreau49962b52021-02-12 16:56:22 +01003730 if (strcmp(args[2], "dict") == 0) {
3731 /* show the dictionaries (large dump) */
3732 appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT;
3733 args++;
3734 } else if (strcmp(args[2], "-") == 0)
3735 args++; // allows to show a section called "dict"
3736
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003737 if (*args[2]) {
3738 struct peers *p;
3739
3740 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003741 if (strcmp(p->id, args[2]) == 0) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003742 appctx->ctx.cfgpeers.target = p;
3743 break;
3744 }
3745 }
3746
Willy Tarreau9d008692019-08-09 11:21:01 +02003747 if (!p)
3748 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003749 }
3750
3751 return 0;
3752}
3753
3754/*
3755 * This function dumps the peer state information of <peers> "peers" section.
3756 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3757 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3758 */
3759static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3760{
3761 struct tm tm;
3762
3763 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003764 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 +02003765 peers,
3766 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3767 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003768 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003769 peers->resync_timeout ?
3770 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3771 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003772 TICKS_TO_MS(1000)) : "<NEVER>",
3773 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003774
3775 if (ci_putchk(si_ic(si), msg) == -1) {
3776 si_rx_room_blk(si);
3777 return 0;
3778 }
3779
3780 return 1;
3781}
3782
3783/*
3784 * This function dumps <peer> state information.
3785 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3786 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3787 */
Willy Tarreau49962b52021-02-12 16:56:22 +01003788static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer, int flags)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003789{
3790 struct connection *conn;
3791 char pn[INET6_ADDRSTRLEN];
3792 struct stream_interface *peer_si;
3793 struct stream *peer_s;
3794 struct appctx *appctx;
3795 struct shared_table *st;
3796
3797 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003798 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003799 peer, peer->id,
3800 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003801 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003802 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003803 statuscode_str(peer->statuscode));
3804
3805 chunk_appendf(msg, " last_hdshk=%s\n",
3806 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3807 TICKS_TO_MS(1000)) : "<NEVER>");
3808
3809 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003810 peer->reconnect ?
3811 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3812 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003813 TICKS_TO_MS(1000)) : "<NEVER>");
3814
3815 chunk_appendf(msg, " heartbeat=%s",
3816 peer->heartbeat ?
3817 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3818 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3819 TICKS_TO_MS(1000)) : "<NEVER>");
3820
3821 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 +01003822 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003823 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003824
3825 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3826
3827 appctx = peer->appctx;
3828 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003829 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003830
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003831 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3832 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003833
Christopher Faulet86e1c332021-12-20 17:09:39 +01003834 peer_si = cs_si(peer->appctx->owner);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003835 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003836 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003837
3838 peer_s = si_strm(peer_si);
3839 if (!peer_s)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003840 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003841
3842 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3843
3844 conn = objt_conn(strm_orig(peer_s));
3845 if (conn)
3846 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3847
Willy Tarreau3ca14902019-07-17 14:53:15 +02003848 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 +02003849 case AF_INET:
3850 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003851 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003852 break;
3853 case AF_UNIX:
3854 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3855 break;
3856 }
3857
Willy Tarreau3ca14902019-07-17 14:53:15 +02003858 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 +02003859 case AF_INET:
3860 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003861 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003862 break;
3863 case AF_UNIX:
3864 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3865 break;
3866 }
3867
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003868 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003869 if (peer->remote_table)
3870 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3871 peer->remote_table,
3872 peer->remote_table->table->id,
3873 peer->remote_table->local_id,
3874 peer->remote_table->remote_id);
3875
3876 if (peer->last_local_table)
3877 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3878 peer->last_local_table,
3879 peer->last_local_table->table->id,
3880 peer->last_local_table->local_id,
3881 peer->last_local_table->remote_id);
3882
3883 if (peer->tables) {
3884 chunk_appendf(&trash, "\n shared tables:");
3885 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003886 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003887 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003888 struct dcache *dcache;
3889
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003890 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003891 dcache = peer->dcache;
3892
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003893 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3894 "flags=0x%x remote_data=0x%llx",
3895 st, st->local_id, st->remote_id,
3896 st->flags, (unsigned long long)st->remote_data);
3897 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3898 " teaching_origin=%u update=%u",
3899 st->last_acked, st->last_pushed, st->last_get,
3900 st->teaching_origin, st->update);
3901 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003902 " commitupdate=%u refcnt=%u",
3903 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003904 if (flags & PEERS_SHOW_F_DICT) {
3905 chunk_appendf(&trash, "\n TX dictionary cache:");
3906 count = 0;
3907 for (i = 0; i < dcache->max_entries; i++) {
3908 struct ebpt_node *node;
3909 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003910
Willy Tarreau49962b52021-02-12 16:56:22 +01003911 node = &dcache->tx->entries[i];
3912 if (!node->key)
3913 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003914
Willy Tarreau49962b52021-02-12 16:56:22 +01003915 if (!count++)
3916 chunk_appendf(&trash, "\n ");
3917 de = node->key;
3918 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3919 count &= 0x3;
3920 }
3921 chunk_appendf(&trash, "\n RX dictionary cache:");
3922 count = 0;
3923 for (i = 0; i < dcache->max_entries; i++) {
3924 if (!count++)
3925 chunk_appendf(&trash, "\n ");
3926 chunk_appendf(&trash, " %3u -> %s", i,
3927 dcache->rx[i].de ?
3928 (char *)dcache->rx[i].de->value.key : "-");
3929 count &= 0x3;
3930 }
3931 } else {
3932 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003933 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003934 }
3935 }
3936
3937 end:
3938 chunk_appendf(&trash, "\n");
3939 if (ci_putchk(si_ic(si), msg) == -1) {
3940 si_rx_room_blk(si);
3941 return 0;
3942 }
3943
3944 return 1;
3945}
3946
3947/*
3948 * This function dumps all the peers of "peers" section.
3949 * Returns 0 if the output buffer is full and needs to be called
3950 * again, non-zero if not. It proceeds in an isolated thread, so
3951 * there is no thread safety issue here.
3952 */
3953static int cli_io_handler_show_peers(struct appctx *appctx)
3954{
3955 int show_all;
3956 int ret = 0, first_peers = 1;
Christopher Faulet86e1c332021-12-20 17:09:39 +01003957 struct stream_interface *si = cs_si(appctx->owner);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003958
3959 thread_isolate();
3960
3961 show_all = !appctx->ctx.cfgpeers.target;
3962
3963 chunk_reset(&trash);
3964
3965 while (appctx->st2 != STAT_ST_FIN) {
3966 switch (appctx->st2) {
3967 case STAT_ST_INIT:
3968 if (show_all)
3969 appctx->ctx.cfgpeers.peers = cfg_peers;
3970 else
3971 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3972
3973 appctx->st2 = STAT_ST_LIST;
3974 /* fall through */
3975
3976 case STAT_ST_LIST:
3977 if (!appctx->ctx.cfgpeers.peers) {
3978 /* No more peers list. */
3979 appctx->st2 = STAT_ST_END;
3980 }
3981 else {
3982 if (!first_peers)
3983 chunk_appendf(&trash, "\n");
3984 else
3985 first_peers = 0;
3986 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3987 goto out;
3988
3989 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3990 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3991 appctx->st2 = STAT_ST_INFO;
3992 }
3993 break;
3994
3995 case STAT_ST_INFO:
3996 if (!appctx->ctx.cfgpeers.peer) {
3997 /* End of peer list */
3998 if (show_all)
3999 appctx->st2 = STAT_ST_LIST;
4000 else
4001 appctx->st2 = STAT_ST_END;
4002 }
4003 else {
Willy Tarreau49962b52021-02-12 16:56:22 +01004004 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer, appctx->ctx.cfgpeers.flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004005 goto out;
4006
4007 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
4008 }
4009 break;
4010
4011 case STAT_ST_END:
4012 appctx->st2 = STAT_ST_FIN;
4013 break;
4014 }
4015 }
4016 ret = 1;
4017 out:
4018 thread_release();
4019 return ret;
4020}
4021
4022/*
4023 * CLI keywords.
4024 */
4025static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004026 { { "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 +02004027 {},
4028}};
4029
4030/* Register cli keywords */
4031INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);