blob: 5a382f863a709af36ecca02577a0367eb76bc253 [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
Frédéric Lécaille4b1a05f2021-01-17 13:08:39 +0100446 if (peer && peer->appctx) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100447 struct stream_interface *si;
448
449 si = peer->appctx->owner;
450 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);
459 if (peer)
460 chunk_appendf(&trace_buf, " -> %s", peer->id);
461 }
462
463 if (a3) {
464 const int *prev_state = a3;
465
466 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
467 }
468 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100469}
470
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200471static const char *statuscode_str(int statuscode)
472{
473 switch (statuscode) {
474 case PEER_SESS_SC_CONNECTCODE:
475 return "CONN";
476 case PEER_SESS_SC_CONNECTEDCODE:
477 return "HSHK";
478 case PEER_SESS_SC_SUCCESSCODE:
479 return "ESTA";
480 case PEER_SESS_SC_TRYAGAIN:
481 return "RETR";
482 case PEER_SESS_SC_ERRPROTO:
483 return "PROT";
484 case PEER_SESS_SC_ERRVERSION:
485 return "VERS";
486 case PEER_SESS_SC_ERRHOST:
487 return "NAME";
488 case PEER_SESS_SC_ERRPEER:
489 return "UNKN";
490 default:
491 return "NONE";
492 }
493}
494
Emeric Brun18928af2017-03-29 16:32:53 +0200495/* This function encode an uint64 to 'dynamic' length format.
496 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500497 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200498 At return, the *str is set at the next Byte after then
499 encoded integer. The function returns then length of the
500 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200501int intencode(uint64_t i, char **str) {
502 int idx = 0;
503 unsigned char *msg;
504
Emeric Brunb3971ab2015-05-12 18:49:09 +0200505 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200506 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200507 msg[0] = (unsigned char)i;
508 *str = (char *)&msg[idx+1];
509 return (idx+1);
510 }
511
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200512 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
513 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
514 while (i >= PEER_ENC_STOP_BYTE) {
515 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
516 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200517 }
518 msg[++idx] = (unsigned char)i;
519 *str = (char *)&msg[idx+1];
520 return (idx+1);
521}
522
523
Emeric Brun5ea07d92021-06-30 13:21:58 +0200524/* This function returns a decoded 64bits unsigned integer
525 * from a varint
526 *
527 * Calling:
528 * - *str must point on the first byte of the buffer to decode.
529 * - end must point on the next byte after the end of the buffer
530 * we are authorized to parse (buf + buflen)
531 *
532 * At return:
533 *
534 * On success *str will point at the byte following
535 * the fully decoded integer into the buffer. and
536 * the decoded value is returned.
537 *
538 * If end is reached before the integer was fully decoded,
539 * *str is set to NULL and the caller have to check this
540 * to know there is a decoding error. In this case
541 * the returned integer is also forced to 0
542 */
Emeric Brun18928af2017-03-29 16:32:53 +0200543uint64_t intdecode(char **str, char *end)
544{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200545 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200546 uint64_t i;
547 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200548
549 if (!*str)
550 return 0;
551
552 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200553 if (msg >= (unsigned char *)end)
554 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200555
Emeric Brun18928af2017-03-29 16:32:53 +0200556 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200557 if (i >= PEER_ENC_2BYTES_MIN) {
558 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200559 do {
560 if (msg >= (unsigned char *)end)
561 goto fail;
562 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200563 shift += PEER_ENC_STOP_BIT;
564 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200565 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100566 *str = (char *)msg;
567 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200568
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100569 fail:
570 *str = NULL;
571 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200572}
Emeric Brun2b920a12010-09-23 18:30:22 +0200573
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100574/*
575 * Build a "hello" peer protocol message.
576 * Return the number of written bytes written to build this messages if succeeded,
577 * 0 if not.
578 */
579static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
580{
581 int min_ver, ret;
582 struct peer *peer;
583
584 peer = p->hello.peer;
585 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
586 /* Prepare headers */
587 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200588 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), 1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100589 if (ret >= size)
590 return 0;
591
592 return ret;
593}
594
595/*
596 * Build a "handshake succeeded" status message.
597 * Return the number of written bytes written to build this messages if succeeded,
598 * 0 if not.
599 */
600static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
601{
602 int ret;
603
604 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
605 if (ret >= size)
606 return 0;
607
608 return ret;
609}
610
611/*
612 * Build an error status message.
613 * Return the number of written bytes written to build this messages if succeeded,
614 * 0 if not.
615 */
616static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
617{
618 int ret;
619 unsigned int st1;
620
621 st1 = p->error_status.st1;
622 ret = snprintf(msg, size, "%d\n", st1);
623 if (ret >= size)
624 return 0;
625
626 return ret;
627}
628
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200629/* Set the stick-table UPDATE message type byte at <msg_type> address,
630 * depending on <use_identifier> and <use_timed> boolean parameters.
631 * Always successful.
632 */
633static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
634{
635 if (use_timed) {
636 if (use_identifier)
637 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
638 else
639 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
640 }
641 else {
642 if (use_identifier)
643 *msg_type = PEER_MSG_STKT_UPDATE;
644 else
645 *msg_type = PEER_MSG_STKT_INCUPDATE;
646 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200647}
Emeric Brun2b920a12010-09-23 18:30:22 +0200648/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200649 * This prepare the data update message on the stick session <ts>, <st> is the considered
650 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800651 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200652 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
653 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200654 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100655static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200656{
657 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200658 unsigned short datalen;
659 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200660 unsigned int data_type;
661 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100662 struct stksess *ts;
663 struct shared_table *st;
664 unsigned int updateid;
665 int use_identifier;
666 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200667 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100668
669 ts = p->updt.stksess;
670 st = p->updt.shared_table;
671 updateid = p->updt.updateid;
672 use_identifier = p->updt.use_identifier;
673 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200674 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200675
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200676 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200677
Emeric Brun2b920a12010-09-23 18:30:22 +0200678 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200679
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500680 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200681 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200682 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200683 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200684
685 /* encode update identifier if needed */
686 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200687 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200688 memcpy(cursor, &netinteger, sizeof(netinteger));
689 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200690 }
691
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200692 if (use_timed) {
693 netinteger = htonl(tick_remain(now_ms, ts->expire));
694 memcpy(cursor, &netinteger, sizeof(netinteger));
695 cursor += sizeof(netinteger);
696 }
697
Emeric Brunb3971ab2015-05-12 18:49:09 +0200698 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200699 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200700 int stlen = strlen((char *)ts->key.key);
701
Emeric Brunb3971ab2015-05-12 18:49:09 +0200702 intencode(stlen, &cursor);
703 memcpy(cursor, ts->key.key, stlen);
704 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200705 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200706 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100707 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200708 memcpy(cursor, &netinteger, sizeof(netinteger));
709 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200710 }
711 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200712 memcpy(cursor, ts->key.key, st->table->key_size);
713 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200714 }
715
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100716 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200717 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200718 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200719
Emeric Brun94900952015-06-11 18:25:54 +0200720 data_ptr = stktable_data_ptr(st->table, ts, data_type);
721 if (data_ptr) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200722 /* in case of array all elements use
723 * the same std_type and they are linearly
724 * encoded.
725 */
726 if (stktable_data_types[data_type].is_array) {
727 unsigned int idx = 0;
728
729 switch (stktable_data_types[data_type].std_type) {
730 case STD_T_SINT: {
731 int data;
732
733 do {
734 data = stktable_data_cast(data_ptr, std_t_sint);
735 intencode(data, &cursor);
736
737 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
738 } while(data_ptr);
739 break;
740 }
741 case STD_T_UINT: {
742 unsigned int data;
743
744 do {
745 data = stktable_data_cast(data_ptr, std_t_uint);
746 intencode(data, &cursor);
747
748 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
749 } while(data_ptr);
750 break;
751 }
752 case STD_T_ULL: {
753 unsigned long long data;
754
755 do {
756 data = stktable_data_cast(data_ptr, std_t_ull);
757 intencode(data, &cursor);
758
759 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
760 } while(data_ptr);
761 break;
762 }
763 case STD_T_FRQP: {
764 struct freq_ctr *frqp;
765
766 do {
767 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
768 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
769 intencode(frqp->curr_ctr, &cursor);
770 intencode(frqp->prev_ctr, &cursor);
771
772 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
773 } while(data_ptr);
774 break;
775 }
776 }
777
778 /* array elements fully encoded
779 * proceed next data_type.
780 */
781 continue;
782 }
Emeric Brun94900952015-06-11 18:25:54 +0200783 switch (stktable_data_types[data_type].std_type) {
784 case STD_T_SINT: {
785 int data;
786
787 data = stktable_data_cast(data_ptr, std_t_sint);
788 intencode(data, &cursor);
789 break;
790 }
791 case STD_T_UINT: {
792 unsigned int data;
793
794 data = stktable_data_cast(data_ptr, std_t_uint);
795 intencode(data, &cursor);
796 break;
797 }
798 case STD_T_ULL: {
799 unsigned long long data;
800
801 data = stktable_data_cast(data_ptr, std_t_ull);
802 intencode(data, &cursor);
803 break;
804 }
805 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200806 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200807
808 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
809 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
810 intencode(frqp->curr_ctr, &cursor);
811 intencode(frqp->prev_ctr, &cursor);
812 break;
813 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200814 case STD_T_DICT: {
815 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200816 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200817 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200818 char *beg, *end;
819 size_t value_len, data_len;
820 struct dcache *dc;
821
822 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100823 if (!de) {
824 /* No entry */
825 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200826 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100827 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200828
829 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200830 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200831 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200832 if (cached_de == &cde.entry) {
833 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
834 break;
835 /* Encode the length of the remaining data -> 1 */
836 intencode(1, &cursor);
837 /* Encode the cache entry ID */
838 intencode(cde.id + 1, &cursor);
839 }
840 else {
841 /* Leave enough room to encode the remaining data length. */
842 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
843 /* Encode the dictionary entry key */
844 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200845 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200846 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200847 intencode(value_len, &end);
848 /* Copy the data */
849 memcpy(end, de->value.key, value_len);
850 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200851 /* Encode the length of the data */
852 data_len = end - beg;
853 intencode(data_len, &cursor);
854 memmove(cursor, beg, data_len);
855 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200856 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200857 break;
858 }
Emeric Brun94900952015-06-11 18:25:54 +0200859 }
860 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200861 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100862 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200863
864 /* Compute datalen */
865 datalen = (cursor - datamsg);
866
867 /* prepare message header */
868 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200869 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200870 cursor = &msg[2];
871 intencode(datalen, &cursor);
872
873 /* move data after header */
874 memmove(cursor, datamsg, datalen);
875
876 /* return header size + data_len */
877 return (cursor - msg) + datalen;
878}
879
880/*
881 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800882 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200883 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
884 * check size)
885 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100886static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200887{
888 int len;
889 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200890 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200891 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200892 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200893 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100894 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200895
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100896 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200897 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200898
899 /* Encode data */
900
901 /* encode local id */
902 intencode(st->local_id, &cursor);
903
904 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100905 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200906 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100907 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200908 cursor += len;
909
910 /* encode table type */
911
Emeric Brun530ba382020-06-02 11:17:42 +0200912 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200913
914 /* encode table key size */
915 intencode(st->table->key_size, &cursor);
916
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200917 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200918 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200919 /* encode available known data types in table */
920 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
921 if (st->table->data_ofs[data_type]) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200922 /* stored data types parameters are all linearly encoded
923 * at the end of the 'table definition' message.
924 *
925 * Currently only array data_types and and data_types
926 * using freq_counter base type have parameters:
927 *
928 * - array has always at least one parameter set to the
929 * number of elements.
930 *
931 * - array of base-type freq_counters has an additional
932 * parameter set to the period used to compute those
933 * freq_counters.
934 *
935 * - simple freq counter has a parameter set to the period
936 * used to compute
937 *
938 * A set of parameter for a datatype MUST BE prefixed
939 * by the data-type id itself:
940 * This is useless because the data_types are ordered and
941 * the data_type bitfield already gives the information of
942 * stored types, but it was designed this way when the
943 * push of period parameter was added for freq counters
944 * and we don't want to break the compatibility.
945 *
946 */
947 if (stktable_data_types[data_type].is_array) {
948 /* This is an array type so we first encode
949 * the data_type itself to prefix parameters
950 */
951 intencode(data_type, &chunkq);
952
953 /* We encode the first parameter which is
954 * the number of elements of this array
955 */
956 intencode(st->table->data_nbelem[data_type], &chunkq);
957
Ilya Shipitsin01881082021-08-07 14:41:56 +0500958 /* for array of freq counters, there is an additional
Emeric Brun90a9b672021-06-22 16:09:55 +0200959 * period parameter to encode
960 */
961 if (stktable_data_types[data_type].std_type == STD_T_FRQP)
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200962 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200963 }
Emeric Brun90a9b672021-06-22 16:09:55 +0200964 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
965 /* this datatype is a simple freq counter not part
966 * of an array. We encode the data_type itself
967 * to prefix the 'period' parameter
968 */
969 intencode(data_type, &chunkq);
970 intencode(st->table->data_arg[data_type].u, &chunkq);
971 }
972 /* set the bit corresponding to stored data type */
973 data |= 1ULL << data_type;
Emeric Brun94900952015-06-11 18:25:54 +0200974 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200975 }
976 intencode(data, &cursor);
977
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200978 /* Encode stick-table entries duration. */
979 intencode(st->table->expire, &cursor);
980
981 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200982 chunk->data = chunkq - chunkp;
983 memcpy(cursor, chunk->area, chunk->data);
984 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200985 }
986
Emeric Brunb3971ab2015-05-12 18:49:09 +0200987 /* Compute datalen */
988 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200989
Emeric Brunb3971ab2015-05-12 18:49:09 +0200990 /* prepare message header */
991 msg[0] = PEER_MSG_CLASS_STICKTABLE;
992 msg[1] = PEER_MSG_STKT_DEFINE;
993 cursor = &msg[2];
994 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200995
Emeric Brunb3971ab2015-05-12 18:49:09 +0200996 /* move data after header */
997 memmove(cursor, datamsg, datalen);
998
999 /* return header size + data_len */
1000 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +02001001}
1002
Emeric Brunb3971ab2015-05-12 18:49:09 +02001003/*
1004 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
1005 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -08001006 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +02001007 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
1008 * check size)
1009 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001010static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001011{
1012 unsigned short datalen;
1013 char *cursor, *datamsg;
1014 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001015 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001016
Frédéric Lécaille39143342019-05-24 14:32:27 +02001017 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001018
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001019 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001020 intencode(st->remote_id, &cursor);
1021 netinteger = htonl(st->last_get);
1022 memcpy(cursor, &netinteger, sizeof(netinteger));
1023 cursor += sizeof(netinteger);
1024
1025 /* Compute datalen */
1026 datalen = (cursor - datamsg);
1027
1028 /* prepare message header */
1029 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +02001030 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001031 cursor = &msg[2];
1032 intencode(datalen, &cursor);
1033
1034 /* move data after header */
1035 memmove(cursor, datamsg, datalen);
1036
1037 /* return header size + data_len */
1038 return (cursor - msg) + datalen;
1039}
Emeric Brun2b920a12010-09-23 18:30:22 +02001040
1041/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001042 * Function to deinit connected peer
1043 */
1044void __peer_session_deinit(struct peer *peer)
1045{
1046 struct stream_interface *si;
1047 struct stream *s;
1048 struct peers *peers;
1049
1050 if (!peer->appctx)
1051 return;
1052
1053 si = peer->appctx->owner;
1054 if (!si)
1055 return;
1056
1057 s = si_strm(si);
1058 if (!s)
1059 return;
1060
1061 peers = strm_fe(s)->parent;
1062 if (!peers)
1063 return;
1064
1065 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02001066 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001067
Willy Tarreau4781b152021-04-06 13:53:36 +02001068 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001069
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001070 flush_dcache(peer);
1071
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001072 /* Re-init current table pointers to force announcement on re-connect */
1073 peer->remote_table = peer->last_local_table = NULL;
1074 peer->appctx = NULL;
1075 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1076 /* unassign current peer for learning */
1077 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
1078 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1079
Emeric Brunccdfbae2021-04-28 12:59:35 +02001080 if (peer->local)
1081 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
1082 else
1083 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001084 /* reschedule a resync */
1085 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1086 }
1087 /* reset teaching and learning flags to 0 */
1088 peer->flags &= PEER_TEACH_RESET;
1089 peer->flags &= PEER_LEARN_RESET;
1090 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1091}
1092
1093/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001094 * Callback to release a session with a peer
1095 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001096static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001097{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001098 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001099
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001100 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001101 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001102 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +02001103 return;
1104
1105 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001106 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001107 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001108 if (peer->appctx == appctx)
1109 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02001110 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001111 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001112 }
1113}
1114
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001115/* Retrieve the major and minor versions of peers protocol
1116 * announced by a remote peer. <str> is a null-terminated
1117 * string with the following format: "<maj_ver>.<min_ver>".
1118 */
1119static int peer_get_version(const char *str,
1120 unsigned int *maj_ver, unsigned int *min_ver)
1121{
1122 unsigned int majv, minv;
1123 const char *pos, *saved;
1124 const char *end;
1125
1126 saved = pos = str;
1127 end = str + strlen(str);
1128
1129 majv = read_uint(&pos, end);
1130 if (saved == pos || *pos++ != '.')
1131 return -1;
1132
1133 saved = pos;
1134 minv = read_uint(&pos, end);
1135 if (saved == pos || pos != end)
1136 return -1;
1137
1138 *maj_ver = majv;
1139 *min_ver = minv;
1140
1141 return 0;
1142}
Emeric Brun2b920a12010-09-23 18:30:22 +02001143
1144/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001145 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1146 * '\n' character.
1147 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1148 * a line could not be read because the communication channel is closed.
1149 */
1150static inline int peer_getline(struct appctx *appctx)
1151{
1152 int n;
1153 struct stream_interface *si = appctx->owner;
1154
1155 n = co_getline(si_oc(si), trash.area, trash.size);
1156 if (!n)
1157 return 0;
1158
1159 if (n < 0 || trash.area[n - 1] != '\n') {
1160 appctx->st0 = PEER_SESS_ST_END;
1161 return -1;
1162 }
1163
1164 if (n > 1 && (trash.area[n - 2] == '\r'))
1165 trash.area[n - 2] = 0;
1166 else
1167 trash.area[n - 1] = 0;
1168
1169 co_skip(si_oc(si), n);
1170
1171 return n;
1172}
1173
1174/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001175 * Send a message after having called <peer_prepare_msg> to build it.
1176 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1177 * Returns -1 if there was not enough room left to send the message,
1178 * any other negative returned value must be considered as an error with an appcxt st0
1179 * returned value equal to PEER_SESS_ST_END.
1180 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001181static inline int peer_send_msg(struct appctx *appctx,
1182 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1183 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001184{
1185 int ret, msglen;
1186 struct stream_interface *si = appctx->owner;
1187
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001188 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001189 if (!msglen) {
1190 /* internal error: message does not fit in trash */
1191 appctx->st0 = PEER_SESS_ST_END;
1192 return 0;
1193 }
1194
1195 /* message to buffer */
1196 ret = ci_putblk(si_ic(si), trash.area, msglen);
1197 if (ret <= 0) {
1198 if (ret == -1) {
1199 /* No more write possible */
1200 si_rx_room_blk(si);
1201 return -1;
1202 }
1203 appctx->st0 = PEER_SESS_ST_END;
1204 }
1205
1206 return ret;
1207}
1208
1209/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001210 * Send a hello message.
1211 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1212 * Returns -1 if there was not enough room left to send the message,
1213 * any other negative returned value must be considered as an error with an appcxt st0
1214 * returned value equal to PEER_SESS_ST_END.
1215 */
1216static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1217{
1218 struct peer_prep_params p = {
1219 .hello.peer = peer,
1220 };
1221
1222 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1223}
1224
1225/*
1226 * Send a success peer handshake status message.
1227 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1228 * Returns -1 if there was not enough room left to send the message,
1229 * any other negative returned value must be considered as an error with an appcxt st0
1230 * returned value equal to PEER_SESS_ST_END.
1231 */
1232static inline int peer_send_status_successmsg(struct appctx *appctx)
1233{
1234 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1235}
1236
1237/*
1238 * Send a peer handshake status error message.
1239 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1240 * Returns -1 if there was not enough room left to send the message,
1241 * any other negative returned value must be considered as an error with an appcxt st0
1242 * returned value equal to PEER_SESS_ST_END.
1243 */
1244static inline int peer_send_status_errormsg(struct appctx *appctx)
1245{
1246 struct peer_prep_params p = {
1247 .error_status.st1 = appctx->st1,
1248 };
1249
1250 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1251}
1252
1253/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001254 * Send a stick-table switch message.
1255 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1256 * Returns -1 if there was not enough room left to send the message,
1257 * any other negative returned value must be considered as an error with an appcxt st0
1258 * returned value equal to PEER_SESS_ST_END.
1259 */
1260static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1261{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001262 struct peer_prep_params p = {
1263 .swtch.shared_table = st,
1264 };
1265
1266 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001267}
1268
1269/*
1270 * Send a stick-table update acknowledgement message.
1271 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1272 * Returns -1 if there was not enough room left to send the message,
1273 * any other negative returned value must be considered as an error with an appcxt st0
1274 * returned value equal to PEER_SESS_ST_END.
1275 */
1276static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1277{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001278 struct peer_prep_params p = {
1279 .ack.shared_table = st,
1280 };
1281
1282 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001283}
1284
1285/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001286 * Send a stick-table update message.
1287 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1288 * Returns -1 if there was not enough room left to send the message,
1289 * any other negative returned value must be considered as an error with an appcxt st0
1290 * returned value equal to PEER_SESS_ST_END.
1291 */
1292static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1293 unsigned int updateid, int use_identifier, int use_timed)
1294{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001295 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001296 .updt = {
1297 .stksess = ts,
1298 .shared_table = st,
1299 .updateid = updateid,
1300 .use_identifier = use_identifier,
1301 .use_timed = use_timed,
1302 .peer = appctx->ctx.peers.ptr,
1303 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001304 };
1305
1306 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001307}
1308
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001309/*
1310 * Build a peer protocol control class message.
1311 * Returns the number of written bytes used to build the message if succeeded,
1312 * 0 if not.
1313 */
1314static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1315{
1316 if (size < sizeof p->control.head)
1317 return 0;
1318
1319 msg[0] = p->control.head[0];
1320 msg[1] = p->control.head[1];
1321
1322 return 2;
1323}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001324
1325/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001326 * Send a stick-table synchronization request message.
1327 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1328 * Returns -1 if there was not enough room left to send the message,
1329 * any other negative returned value must be considered as an error with an appctx st0
1330 * returned value equal to PEER_SESS_ST_END.
1331 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001332static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1333 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001334{
1335 struct peer_prep_params p = {
1336 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1337 };
1338
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001339 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1340 NULL, &p.control.head[1], peers->local->id, peer->id);
1341
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001342 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1343}
1344
1345/*
1346 * Send a stick-table synchronization confirmation message.
1347 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1348 * Returns -1 if there was not enough room left to send the message,
1349 * any other negative returned value must be considered as an error with an appctx st0
1350 * returned value equal to PEER_SESS_ST_END.
1351 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001352static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1353 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001354{
1355 struct peer_prep_params p = {
1356 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1357 };
1358
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001359 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1360 NULL, &p.control.head[1], peers->local->id, peer->id);
1361
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001362 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1363}
1364
1365/*
1366 * Send a stick-table synchronization finished message.
1367 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1368 * Returns -1 if there was not enough room left to send the message,
1369 * any other negative returned value must be considered as an error with an appctx st0
1370 * returned value equal to PEER_SESS_ST_END.
1371 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001372static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1373 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001374{
1375 struct peer_prep_params p = {
1376 .control.head = { PEER_MSG_CLASS_CONTROL, },
1377 };
1378
Emeric Brun70de43b2020-03-16 10:51:01 +01001379 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001380 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1381
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001382 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1383 NULL, &p.control.head[1], peers->local->id, peer->id);
1384
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001385 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1386}
1387
1388/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001389 * Send a heartbeat message.
1390 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1391 * Returns -1 if there was not enough room left to send the message,
1392 * any other negative returned value must be considered as an error with an appctx st0
1393 * returned value equal to PEER_SESS_ST_END.
1394 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001395static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1396 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001397{
1398 struct peer_prep_params p = {
1399 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1400 };
1401
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001402 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1403 NULL, &p.control.head[1], peers->local->id, peer->id);
1404
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001405 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1406}
1407
1408/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001409 * Build a peer protocol error class message.
1410 * Returns the number of written bytes used to build the message if succeeded,
1411 * 0 if not.
1412 */
1413static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1414{
1415 if (size < sizeof p->error.head)
1416 return 0;
1417
1418 msg[0] = p->error.head[0];
1419 msg[1] = p->error.head[1];
1420
1421 return 2;
1422}
1423
1424/*
1425 * Send a "size limit reached" error message.
1426 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1427 * Returns -1 if there was not enough room left to send the message,
1428 * any other negative returned value must be considered as an error with an appctx st0
1429 * returned value equal to PEER_SESS_ST_END.
1430 */
1431static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1432{
1433 struct peer_prep_params p = {
1434 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1435 };
1436
1437 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1438}
1439
1440/*
1441 * Send a "peer protocol" error message.
1442 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1443 * Returns -1 if there was not enough room left to send the message,
1444 * any other negative returned value must be considered as an error with an appctx st0
1445 * returned value equal to PEER_SESS_ST_END.
1446 */
1447static inline int peer_send_error_protomsg(struct appctx *appctx)
1448{
1449 struct peer_prep_params p = {
1450 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1451 };
1452
1453 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1454}
1455
1456/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001457 * Function used to lookup for recent stick-table updates associated with
1458 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1459 */
1460static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1461{
1462 struct eb32_node *eb;
1463
1464 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1465 if (!eb) {
1466 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001467 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001468 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1469 return NULL;
1470 }
1471 }
1472
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001473 /* if distance between the last pushed and the retrieved key
1474 * is greater than the distance last_pushed and the local_update
1475 * this means we are beyond localupdate.
1476 */
1477 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001478 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1479 return NULL;
1480 }
1481
1482 return eb32_entry(eb, struct stksess, upd);
1483}
1484
1485/*
1486 * Function used to lookup for recent stick-table updates associated with
1487 * <st> shared stick-table during teach state 1 step.
1488 */
1489static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1490{
1491 struct eb32_node *eb;
1492
1493 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1494 if (!eb) {
1495 st->flags |= SHTABLE_F_TEACH_STAGE1;
1496 eb = eb32_first(&st->table->updates);
1497 if (eb)
1498 st->last_pushed = eb->key - 1;
1499 return NULL;
1500 }
1501
1502 return eb32_entry(eb, struct stksess, upd);
1503}
1504
1505/*
1506 * Function used to lookup for recent stick-table updates associated with
1507 * <st> shared stick-table during teach state 2 step.
1508 */
1509static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1510{
1511 struct eb32_node *eb;
1512
1513 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1514 if (!eb || eb->key > st->teaching_origin) {
1515 st->flags |= SHTABLE_F_TEACH_STAGE2;
1516 return NULL;
1517 }
1518
1519 return eb32_entry(eb, struct stksess, upd);
1520}
1521
1522/*
1523 * Generic function to emit update messages for <st> stick-table when a lesson must
1524 * be taught to the peer <p>.
1525 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1526 * this function, 0 if not.
1527 *
1528 * This function temporary unlock/lock <st> when it sends stick-table updates or
1529 * when decrementing its refcount in case of any error when it sends this updates.
1530 *
1531 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1532 * Returns -1 if there was not enough room left to send the message,
1533 * any other negative returned value must be considered as an error with an appcxt st0
1534 * returned value equal to PEER_SESS_ST_END.
1535 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1536 * unlocked if not already locked when entering this function.
1537 */
1538static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1539 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1540 struct shared_table *st, int locked)
1541{
1542 int ret, new_pushed, use_timed;
1543
1544 ret = 1;
1545 use_timed = 0;
1546 if (st != p->last_local_table) {
1547 ret = peer_send_switchmsg(st, appctx);
1548 if (ret <= 0)
1549 return ret;
1550
1551 p->last_local_table = st;
1552 }
1553
1554 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1555 use_timed = !(p->flags & PEER_F_DWNGRD);
1556
1557 /* We force new pushed to 1 to force identifier in update message */
1558 new_pushed = 1;
1559
1560 if (!locked)
1561 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1562
1563 while (1) {
1564 struct stksess *ts;
1565 unsigned updateid;
1566
1567 /* push local updates */
1568 ts = peer_stksess_lookup(st);
1569 if (!ts)
1570 break;
1571
1572 updateid = ts->upd.key;
1573 ts->ref_cnt++;
1574 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1575
1576 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1577 if (ret <= 0) {
1578 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1579 ts->ref_cnt--;
1580 if (!locked)
1581 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1582 return ret;
1583 }
1584
1585 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1586 ts->ref_cnt--;
1587 st->last_pushed = updateid;
1588
1589 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1590 (int)(st->last_pushed - st->table->commitupdate) > 0)
1591 st->table->commitupdate = st->last_pushed;
1592
1593 /* identifier may not needed in next update message */
1594 new_pushed = 0;
1595 }
1596
1597 out:
1598 if (!locked)
1599 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1600 return 1;
1601}
1602
1603/*
1604 * Function to emit update messages for <st> stick-table when a lesson must
1605 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1606 *
1607 * Note that <st> shared stick-table is locked when calling this function.
1608 *
1609 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1610 * Returns -1 if there was not enough room left to send the message,
1611 * any other negative returned value must be considered as an error with an appcxt st0
1612 * returned value equal to PEER_SESS_ST_END.
1613 */
1614static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1615 struct shared_table *st)
1616{
1617 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1618}
1619
1620/*
1621 * Function to emit update messages for <st> stick-table when a lesson must
1622 * be taught to the peer <p> during teach state 1 step.
1623 *
1624 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1625 * Returns -1 if there was not enough room left to send the message,
1626 * any other negative returned value must be considered as an error with an appcxt st0
1627 * returned value equal to PEER_SESS_ST_END.
1628 */
1629static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1630 struct shared_table *st)
1631{
1632 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1633}
1634
1635/*
1636 * Function to emit update messages for <st> stick-table when a lesson must
1637 * be taught to the peer <p> during teach state 1 step.
1638 *
1639 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1640 * Returns -1 if there was not enough room left to send the message,
1641 * any other negative returned value must be considered as an error with an appcxt st0
1642 * returned value equal to PEER_SESS_ST_END.
1643 */
1644static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1645 struct shared_table *st)
1646{
1647 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1648}
1649
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001650
1651/*
1652 * Function used to parse a stick-table update message after it has been received
1653 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1654 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1655 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1656 * was encountered.
1657 * <exp> must be set if the stick-table entry expires.
1658 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001659 * 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 +01001660 * update ID.
1661 * <totl> is the length of the stick-table update message computed upon receipt.
1662 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001663static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1664 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001665{
1666 struct stream_interface *si = appctx->owner;
1667 struct shared_table *st = p->remote_table;
1668 struct stksess *ts, *newts;
1669 uint32_t update;
1670 int expire;
1671 unsigned int data_type;
1672 void *data_ptr;
1673
Frédéric Lécailled8659352020-11-10 16:18:03 +01001674 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001675 /* Here we have data message */
1676 if (!st)
1677 goto ignore_msg;
1678
1679 expire = MS_TO_TICKS(st->table->expire);
1680
1681 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001682 if (msg_len < sizeof(update)) {
1683 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001684 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001685 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001686
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001687 memcpy(&update, *msg_cur, sizeof(update));
1688 *msg_cur += sizeof(update);
1689 st->last_get = htonl(update);
1690 }
1691 else {
1692 st->last_get++;
1693 }
1694
1695 if (exp) {
1696 size_t expire_sz = sizeof expire;
1697
Frédéric Lécailled8659352020-11-10 16:18:03 +01001698 if (*msg_cur + expire_sz > msg_end) {
1699 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1700 NULL, p, *msg_cur);
1701 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1702 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001703 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001704 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001705
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001706 memcpy(&expire, *msg_cur, expire_sz);
1707 *msg_cur += expire_sz;
1708 expire = ntohl(expire);
1709 }
1710
1711 newts = stksess_new(st->table, NULL);
1712 if (!newts)
1713 goto ignore_msg;
1714
1715 if (st->table->type == SMP_T_STR) {
1716 unsigned int to_read, to_store;
1717
1718 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001719 if (!*msg_cur) {
1720 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001721 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001722 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001723
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001724 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001725 if (*msg_cur + to_store > msg_end) {
1726 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1727 NULL, p, *msg_cur);
1728 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1729 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001730 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001731 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001732
1733 memcpy(newts->key.key, *msg_cur, to_store);
1734 newts->key.key[to_store] = 0;
1735 *msg_cur += to_read;
1736 }
1737 else if (st->table->type == SMP_T_SINT) {
1738 unsigned int netinteger;
1739
Frédéric Lécailled8659352020-11-10 16:18:03 +01001740 if (*msg_cur + sizeof(netinteger) > msg_end) {
1741 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1742 NULL, p, *msg_cur);
1743 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1744 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001745 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001746 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001747
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001748 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1749 netinteger = ntohl(netinteger);
1750 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1751 *msg_cur += sizeof(netinteger);
1752 }
1753 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001754 if (*msg_cur + st->table->key_size > msg_end) {
1755 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1756 NULL, p, *msg_cur);
1757 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1758 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001759 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001760 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001761
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001762 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1763 *msg_cur += st->table->key_size;
1764 }
1765
1766 /* lookup for existing entry */
1767 ts = stktable_set_entry(st->table, newts);
1768 if (ts != newts) {
1769 stksess_free(st->table, newts);
1770 newts = NULL;
1771 }
1772
1773 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1774
1775 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001776 uint64_t decoded_int;
Emeric Brun90a9b672021-06-22 16:09:55 +02001777 unsigned int idx;
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;
Emeric Brun90a9b672021-06-22 16:09:55 +02001781 if (stktable_data_types[data_type].is_array) {
1782 /* in case of array all elements
1783 * use the same std_type and they
1784 * are linearly encoded.
1785 * The number of elements was provided
1786 * by table definition message
1787 */
1788 switch (stktable_data_types[data_type].std_type) {
1789 case STD_T_SINT:
1790 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1791 decoded_int = intdecode(msg_cur, msg_end);
1792 if (!*msg_cur) {
1793 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1794 goto malformed_unlock;
1795 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001796
Emeric Brun90a9b672021-06-22 16:09:55 +02001797 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
1798 if (data_ptr)
1799 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
1800 }
1801 break;
1802 case STD_T_UINT:
1803 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1804 decoded_int = intdecode(msg_cur, msg_end);
1805 if (!*msg_cur) {
1806 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1807 goto malformed_unlock;
1808 }
1809
1810 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
1811 if (data_ptr)
1812 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
1813 }
1814 break;
1815 case STD_T_ULL:
1816 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1817 decoded_int = intdecode(msg_cur, msg_end);
1818 if (!*msg_cur) {
1819 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1820 goto malformed_unlock;
1821 }
1822
1823 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
1824 if (data_ptr)
1825 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
1826 }
1827 break;
1828 case STD_T_FRQP:
1829 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1830 struct freq_ctr data;
1831
1832 /* First bit is reserved for the freq_ctr lock
1833 * Note: here we're still protected by the stksess lock
1834 * so we don't need to update the update the freq_ctr
1835 * using its internal lock.
1836 */
1837
1838 decoded_int = intdecode(msg_cur, msg_end);
1839 if (!*msg_cur) {
1840 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1841 goto malformed_unlock;
1842 }
1843
1844 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
1845 data.curr_ctr = intdecode(msg_cur, msg_end);
1846 if (!*msg_cur) {
1847 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1848 goto malformed_unlock;
1849 }
1850
1851 data.prev_ctr = intdecode(msg_cur, msg_end);
1852 if (!*msg_cur) {
1853 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1854 goto malformed_unlock;
1855 }
1856
1857 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
1858 if (data_ptr)
1859 stktable_data_cast(data_ptr, std_t_frqp) = data;
1860 }
1861 break;
1862 }
1863
1864 /* array is fully decoded
1865 * proceed next data_type.
1866 */
1867 continue;
1868 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001869 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001870 if (!*msg_cur) {
1871 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001872 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001873 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001874
Willy Tarreau1e82a142019-01-29 11:08:06 +01001875 switch (stktable_data_types[data_type].std_type) {
1876 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001877 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1878 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001879 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001880 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001881
Willy Tarreau1e82a142019-01-29 11:08:06 +01001882 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001883 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1884 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001885 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001886 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001887
Willy Tarreau1e82a142019-01-29 11:08:06 +01001888 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001889 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1890 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001891 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001892 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001893
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001894 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001895 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001896
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001897 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001898 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001899 so we don't need to update the update the freq_ctr
Emeric Brun90a9b672021-06-22 16:09:55 +02001900 using its internal lock.
1901 */
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001902
Willy Tarreau1e82a142019-01-29 11:08:06 +01001903 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001904 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001905 if (!*msg_cur) {
1906 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001907 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001908 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001909
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001910 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001911 if (!*msg_cur) {
1912 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001913 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001914 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001915
1916 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1917 if (data_ptr)
1918 stktable_data_cast(data_ptr, std_t_frqp) = data;
1919 break;
1920 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001921 case STD_T_DICT: {
1922 struct buffer *chunk;
1923 size_t data_len, value_len;
1924 unsigned int id;
1925 struct dict_entry *de;
1926 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001927 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001928
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001929 if (!decoded_int) {
1930 /* No entry. */
1931 break;
1932 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001933 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001934 if (*msg_cur + data_len > msg_end) {
1935 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1936 NULL, p, *msg_cur);
1937 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1938 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001939 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001940 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001941
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001942 /* Compute the end of the current data, <msg_end> being at the end of
1943 * the entire message.
1944 */
1945 end = *msg_cur + data_len;
1946 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001947 if (!*msg_cur || !id) {
1948 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1949 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001950 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001951 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001952
1953 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001954 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001955 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001956 if (id > dc->max_entries) {
1957 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1958 NULL, p, NULL, &id);
1959 goto malformed_unlock;
1960 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001961 /* IDs sent over the network are numbered from 1. */
1962 de = dc->rx[id - 1].de;
1963 }
1964 else {
1965 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001966 value_len = intdecode(msg_cur, end);
1967 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01001968 unlikely(value_len + 1 >= chunk->size)) {
1969 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1970 NULL, p, *msg_cur, &value_len);
1971 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1972 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001973 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001974 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001975
1976 chunk_memcpy(chunk, *msg_cur, value_len);
1977 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001978 *msg_cur += value_len;
1979
Thayne McCombs92149f92020-11-20 01:28:26 -07001980 de = dict_insert(&server_key_dict, chunk->area);
1981 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001982 dc->rx[id - 1].de = de;
1983 }
1984 if (de) {
1985 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Thayne McCombs92149f92020-11-20 01:28:26 -07001986 if (data_ptr) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001987 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001988 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07001989 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001990 }
1991 break;
1992 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001993 }
1994 }
1995 /* Force new expiration */
1996 ts->expire = tick_add(now_ms, expire);
1997
1998 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1999 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002000 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002001 return 1;
2002
2003 ignore_msg:
2004 /* skip consumed message */
2005 co_skip(si_oc(si), totl);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002006 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002007 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01002008
2009 malformed_unlock:
2010 /* malformed message */
2011 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2012 stktable_touch_remote(st->table, ts, 1);
2013 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002014 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002015 return 0;
2016
2017 malformed_free_newts:
2018 /* malformed message */
2019 stksess_free(st->table, newts);
2020 malformed_exit:
2021 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002022 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002023 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002024}
2025
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01002026/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002027 * Function used to parse a stick-table update acknowledgement message after it
2028 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
2029 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2030 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2031 * was encountered.
2032 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2033 */
2034static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
2035 char **msg_cur, char *msg_end)
2036{
2037 /* ack message */
2038 uint32_t table_id ;
2039 uint32_t update;
2040 struct shared_table *st;
2041
Emeric Brunb0d60be2021-03-04 10:27:10 +01002042 /* ignore ack during teaching process */
2043 if (p->flags & PEER_F_TEACH_PROCESS)
2044 return 1;
2045
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002046 table_id = intdecode(msg_cur, msg_end);
2047 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
2048 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002049
2050 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
2051 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002052 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2053 return 0;
2054 }
2055
2056 memcpy(&update, *msg_cur, sizeof(update));
2057 update = ntohl(update);
2058
2059 for (st = p->tables; st; st = st->next) {
2060 if (st->local_id == table_id) {
2061 st->update = update;
2062 break;
2063 }
2064 }
2065
2066 return 1;
2067}
2068
2069/*
2070 * Function used to parse a stick-table switch message after it has been received
2071 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2072 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2073 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2074 * was encountered.
2075 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2076 */
2077static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
2078 char **msg_cur, char *msg_end)
2079{
2080 struct shared_table *st;
2081 int table_id;
2082
2083 table_id = intdecode(msg_cur, msg_end);
2084 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002085 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002086 /* malformed message */
2087 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2088 return 0;
2089 }
2090
2091 p->remote_table = NULL;
2092 for (st = p->tables; st; st = st->next) {
2093 if (st->remote_id == table_id) {
2094 p->remote_table = st;
2095 break;
2096 }
2097 }
2098
2099 return 1;
2100}
2101
2102/*
2103 * Function used to parse a stick-table definition message after it has been received
2104 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2105 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2106 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2107 * was encountered.
2108 * <totl> is the length of the stick-table update message computed upon receipt.
2109 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2110 */
2111static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
2112 char **msg_cur, char *msg_end, int totl)
2113{
2114 struct stream_interface *si = appctx->owner;
2115 int table_id_len;
2116 struct shared_table *st;
2117 int table_type;
2118 int table_keylen;
2119 int table_id;
2120 uint64_t table_data;
2121
2122 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002123 if (!*msg_cur) {
2124 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002125 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002126 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002127
2128 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002129 if (!*msg_cur) {
2130 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002131 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002132 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002133
2134 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002135 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
2136 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002137 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002138 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002139
2140 for (st = p->tables; st; st = st->next) {
2141 /* Reset IDs */
2142 if (st->remote_id == table_id)
2143 st->remote_id = 0;
2144
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01002145 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
2146 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002147 p->remote_table = st;
2148 }
2149
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002150 if (!p->remote_table) {
2151 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002152 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002153 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002154
2155 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002156 if (*msg_cur >= msg_end) {
2157 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002158 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002159 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002160
2161 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002162 if (!*msg_cur) {
2163 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002164 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002165 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002166
2167 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002168 if (!*msg_cur) {
2169 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002170 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002171 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002172
2173 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002174 if (!*msg_cur) {
2175 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002176 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002177 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002178
Emeric Brun530ba382020-06-02 11:17:42 +02002179 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002180 || p->remote_table->table->key_size != table_keylen) {
2181 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002182 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002183 goto ignore_msg;
2184 }
2185
Ilya Shipitsin01881082021-08-07 14:41:56 +05002186 /* Check if there there is the additional expire data */
Emeric Brun90a9b672021-06-22 16:09:55 +02002187 intdecode(msg_cur, msg_end);
2188 if (*msg_cur) {
2189 uint64_t data_type;
2190 uint64_t type;
2191
2192 /* This define contains the expire data so we consider
2193 * it also contain all data_types parameters.
2194 */
2195 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2196 if (table_data & (1ULL << data_type)) {
2197 if (stktable_data_types[data_type].is_array) {
2198 /* This should be an array
2199 * so we parse the data_type prefix
2200 * because we must have parameters.
2201 */
2202 type = intdecode(msg_cur, msg_end);
2203 if (!*msg_cur) {
2204 p->remote_table = NULL;
2205 TRACE_PROTO("missing meta data for array", PEERS_EV_DEFMSG, NULL, p);
2206 goto ignore_msg;
2207 }
2208
2209 /* check if the data_type match the current from the bitfield */
2210 if (type != data_type) {
2211 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002212 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002213 goto ignore_msg;
2214 }
2215
2216 /* decode the nbelem of the array */
2217 p->remote_table->remote_data_nbelem[type] = intdecode(msg_cur, msg_end);
2218 if (!*msg_cur) {
2219 p->remote_table = NULL;
2220 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2221 goto ignore_msg;
2222 }
2223
2224 /* if it is an array of frqp, we must also have the period to decode */
2225 if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2226 intdecode(msg_cur, msg_end);
2227 if (!*msg_cur) {
2228 p->remote_table = NULL;
2229 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2230 goto ignore_msg;
2231 }
2232 }
2233 }
2234 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2235 /* This should be a std freq counter data_type
2236 * so we parse the data_type prefix
2237 * because we must have parameters.
2238 */
2239 type = intdecode(msg_cur, msg_end);
2240 if (!*msg_cur) {
2241 p->remote_table = NULL;
2242 TRACE_PROTO("missing meta data for frqp", PEERS_EV_DEFMSG, NULL, p);
2243 goto ignore_msg;
2244 }
2245
2246 /* check if the data_type match the current from the bitfield */
2247 if (type != data_type) {
2248 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002249 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002250 goto ignore_msg;
2251 }
2252
2253 /* decode the period */
2254 intdecode(msg_cur, msg_end);
2255 if (!*msg_cur) {
2256 p->remote_table = NULL;
2257 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2258 goto ignore_msg;
2259 }
2260 }
2261 }
2262 }
2263 }
2264 else {
2265 uint64_t data_type;
2266
2267 /* There is not additional data but
2268 * array size parameter is mandatory to parse array
2269 * so we consider an error if an array data_type is define
2270 * but there is no additional data.
2271 */
2272 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2273 if (table_data & (1ULL << data_type)) {
2274 if (stktable_data_types[data_type].is_array) {
2275 p->remote_table = NULL;
2276 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2277 goto ignore_msg;
2278 }
2279 }
2280 }
2281 }
2282
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002283 p->remote_table->remote_data = table_data;
2284 p->remote_table->remote_id = table_id;
2285 return 1;
2286
2287 ignore_msg:
2288 co_skip(si_oc(si), totl);
2289 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01002290
2291 malformed_exit:
2292 /* malformed message */
2293 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2294 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002295}
2296
2297/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002298 * Receive a stick-table message or pre-parse any other message.
2299 * The message's header will be sent into <msg_head> which must be at least
2300 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
2301 * The first two bytes are always read, and the rest is only read if the
2302 * first bytes indicate a stick-table message. If the message is a stick-table
2303 * message, the varint is decoded and the equivalent number of bytes will be
2304 * copied into the trash at trash.area. <totl> is incremented by the number of
2305 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002306 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
2307 * -1 if there was an error updating the appctx state st0 accordingly.
2308 */
2309static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2310 uint32_t *msg_len, int *totl)
2311{
2312 int reql;
2313 struct stream_interface *si = appctx->owner;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002314 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002315
2316 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
2317 if (reql <= 0) /* closed or EOL not found */
2318 goto incomplete;
2319
2320 *totl += reql;
2321
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002322 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002323 return 1;
2324
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002325 /* This is a stick-table message, let's go on */
2326
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002327 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002328 msg_head += *totl;
2329 msg_head_sz -= *totl;
2330 reql = co_data(si_oc(si)) - *totl;
2331 if (reql > msg_head_sz)
2332 reql = msg_head_sz;
2333
2334 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002335 if (reql <= 0) /* closed */
2336 goto incomplete;
2337
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002338 cur = msg_head;
2339 *msg_len = intdecode(&cur, cur + reql);
2340 if (!cur) {
2341 /* the number is truncated, did we read enough ? */
2342 if (reql < msg_head_sz)
2343 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002344
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002345 /* malformed message */
2346 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2347 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2348 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002349 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002350 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002351
2352 /* Read message content */
2353 if (*msg_len) {
2354 if (*msg_len > trash.size) {
2355 /* Status code is not success, abort */
2356 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2357 return -1;
2358 }
2359
2360 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
2361 if (reql <= 0) /* closed */
2362 goto incomplete;
2363 *totl += reql;
2364 }
2365
2366 return 1;
2367
2368 incomplete:
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002369 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
2370 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002371 appctx->st0 = PEER_SESS_ST_END;
2372 return -1;
2373 }
2374
2375 return 0;
2376}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002377
2378/*
2379 * Treat the awaited message with <msg_head> as header.*
2380 * Return 1 if succeeded, 0 if not.
2381 */
2382static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2383 char **msg_cur, char *msg_end, int msg_len, int totl)
2384{
2385 struct stream_interface *si = appctx->owner;
2386 struct stream *s = si_strm(si);
2387 struct peers *peers = strm_fe(s)->parent;
2388
2389 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2390 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2391 struct shared_table *st;
2392 /* Reset message: remote need resync */
2393
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002394 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2395 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002396 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002397 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002398 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002399 st->flags = 0;
2400 }
2401
2402 /* reset teaching flags to 0 */
2403 peer->flags &= PEER_TEACH_RESET;
2404
2405 /* flag to start to teach lesson */
2406 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002407 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002408 }
2409 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002410 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2411 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002412 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2413 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2414 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2415 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002416 if (peer->local)
2417 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2418 else
2419 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002420 }
2421 peer->confirm++;
2422 }
2423 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002424 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2425 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002426 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2427 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2428 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2429
Emeric Brunccdfbae2021-04-28 12:59:35 +02002430 if (peer->local)
2431 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2432 else
2433 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002434 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002435 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002436 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2437 }
2438 peer->confirm++;
2439 }
2440 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2441 struct shared_table *st;
2442
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002443 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2444 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002445 /* If stopping state */
2446 if (stopping) {
2447 /* Close session, push resync no more needed */
2448 peer->flags |= PEER_F_TEACH_COMPLETE;
2449 appctx->st0 = PEER_SESS_ST_END;
2450 return 0;
2451 }
2452 for (st = peer->tables; st; st = st->next) {
2453 st->update = st->last_pushed = st->teaching_origin;
2454 st->flags = 0;
2455 }
2456
2457 /* reset teaching flags to 0 */
2458 peer->flags &= PEER_TEACH_RESET;
2459 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002460 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002461 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2462 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002463 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002464 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002465 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002466 }
2467 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2468 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2469 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2470 return 0;
2471 }
2472 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2473 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2474 return 0;
2475 }
2476 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2477 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2478 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2479 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2480 int update, expire;
2481
2482 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2483 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2484 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2485 msg_cur, msg_end, msg_len, totl))
2486 return 0;
2487
2488 }
2489 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2490 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2491 return 0;
2492 }
2493 }
2494 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2495 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2496 return 0;
2497 }
2498
2499 return 1;
2500}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002501
2502
2503/*
2504 * Send any message to <peer> peer.
2505 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002506 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002507 * to a peer state change (from the peer I/O handler point of view).
2508 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002509static inline int peer_send_msgs(struct appctx *appctx,
2510 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002511{
2512 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002513
2514 /* Need to request a resync */
2515 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2516 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2517 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2518
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002519 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002520 if (repl <= 0)
2521 return repl;
2522
2523 peers->flags |= PEERS_F_RESYNC_PROCESS;
2524 }
2525
2526 /* Nothing to read, now we start to write */
2527 if (peer->tables) {
2528 struct shared_table *st;
2529 struct shared_table *last_local_table;
2530
2531 last_local_table = peer->last_local_table;
2532 if (!last_local_table)
2533 last_local_table = peer->tables;
2534 st = last_local_table->next;
2535
2536 while (1) {
2537 if (!st)
2538 st = peer->tables;
2539
2540 /* It remains some updates to ack */
2541 if (st->last_get != st->last_acked) {
2542 repl = peer_send_ackmsg(st, appctx);
2543 if (repl <= 0)
2544 return repl;
2545
2546 st->last_acked = st->last_get;
2547 }
2548
2549 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2550 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2551 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002552 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002553
2554 repl = peer_send_teach_process_msgs(appctx, peer, st);
2555 if (repl <= 0) {
2556 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2557 return repl;
2558 }
2559 }
2560 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2561 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002562 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002563 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2564 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2565 if (repl <= 0)
2566 return repl;
2567 }
2568
2569 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2570 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2571 if (repl <= 0)
2572 return repl;
2573 }
2574 }
2575
2576 if (st == last_local_table)
2577 break;
2578 st = st->next;
2579 }
2580 }
2581
2582 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002583 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002584 if (repl <= 0)
2585 return repl;
2586
2587 /* flag finished message sent */
2588 peer->flags |= PEER_F_TEACH_FINISHED;
2589 }
2590
2591 /* Confirm finished or partial messages */
2592 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002593 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002594 if (repl <= 0)
2595 return repl;
2596
2597 peer->confirm--;
2598 }
2599
2600 return 1;
2601}
2602
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002603/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002604 * Read and parse a first line of a "hello" peer protocol message.
2605 * Returns 0 if could not read a line, -1 if there was a read error or
2606 * the line is malformed, 1 if succeeded.
2607 */
2608static inline int peer_getline_version(struct appctx *appctx,
2609 unsigned int *maj_ver, unsigned int *min_ver)
2610{
2611 int reql;
2612
2613 reql = peer_getline(appctx);
2614 if (!reql)
2615 return 0;
2616
2617 if (reql < 0)
2618 return -1;
2619
2620 /* test protocol */
2621 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2622 appctx->st0 = PEER_SESS_ST_EXIT;
2623 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2624 return -1;
2625 }
2626 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2627 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2628 appctx->st0 = PEER_SESS_ST_EXIT;
2629 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2630 return -1;
2631 }
2632
2633 return 1;
2634}
2635
2636/*
2637 * Read and parse a second line of a "hello" peer protocol message.
2638 * Returns 0 if could not read a line, -1 if there was a read error or
2639 * the line is malformed, 1 if succeeded.
2640 */
2641static inline int peer_getline_host(struct appctx *appctx)
2642{
2643 int reql;
2644
2645 reql = peer_getline(appctx);
2646 if (!reql)
2647 return 0;
2648
2649 if (reql < 0)
2650 return -1;
2651
2652 /* test hostname match */
2653 if (strcmp(localpeer, trash.area) != 0) {
2654 appctx->st0 = PEER_SESS_ST_EXIT;
2655 appctx->st1 = PEER_SESS_SC_ERRHOST;
2656 return -1;
2657 }
2658
2659 return 1;
2660}
2661
2662/*
2663 * Read and parse a last line of a "hello" peer protocol message.
2664 * Returns 0 if could not read a character, -1 if there was a read error or
2665 * the line is malformed, 1 if succeeded.
2666 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2667 */
2668static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2669{
2670 char *p;
2671 int reql;
2672 struct peer *peer;
2673 struct stream_interface *si = appctx->owner;
2674 struct stream *s = si_strm(si);
2675 struct peers *peers = strm_fe(s)->parent;
2676
2677 reql = peer_getline(appctx);
2678 if (!reql)
2679 return 0;
2680
2681 if (reql < 0)
2682 return -1;
2683
2684 /* parse line "<peer name> <pid> <relative_pid>" */
2685 p = strchr(trash.area, ' ');
2686 if (!p) {
2687 appctx->st0 = PEER_SESS_ST_EXIT;
2688 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2689 return -1;
2690 }
2691 *p = 0;
2692
2693 /* lookup known peer */
2694 for (peer = peers->remote; peer; peer = peer->next) {
2695 if (strcmp(peer->id, trash.area) == 0)
2696 break;
2697 }
2698
2699 /* if unknown peer */
2700 if (!peer) {
2701 appctx->st0 = PEER_SESS_ST_EXIT;
2702 appctx->st1 = PEER_SESS_SC_ERRPEER;
2703 return -1;
2704 }
2705 *curpeer = peer;
2706
2707 return 1;
2708}
2709
2710/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002711 * Init <peer> peer after having accepted it at peer protocol level.
2712 */
2713static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2714{
2715 struct shared_table *st;
2716
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002717 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002718 /* Register status code */
2719 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002720 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002721
2722 /* Awake main task */
2723 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2724
2725 /* Init confirm counter */
2726 peer->confirm = 0;
2727
2728 /* Init cursors */
2729 for (st = peer->tables; st ; st = st->next) {
2730 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002731 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2732 /* if st->update appears to be in future it means
2733 * that the last acked value is very old and we
2734 * remain unconnected a too long time to use this
2735 * acknowlegement as a reset.
2736 * We should update the protocol to be able to
2737 * signal the remote peer that it needs a full resync.
2738 * Here a partial fix consist to set st->update at
2739 * the max past value
2740 */
2741 if ((int)(st->table->localupdate - st->update) < 0)
2742 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002743 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002744 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002745 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2746 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002747 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002748 }
2749
2750 /* reset teaching and learning flags to 0 */
2751 peer->flags &= PEER_TEACH_RESET;
2752 peer->flags &= PEER_LEARN_RESET;
2753
2754 /* if current peer is local */
2755 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002756 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002757 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2758 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2759 /* assign local peer for a lesson, consider lesson already requested */
2760 peer->flags |= PEER_F_LEARN_ASSIGN;
2761 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002762 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002763 }
2764
2765 }
2766 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2767 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2768 /* assign peer for a lesson */
2769 peer->flags |= PEER_F_LEARN_ASSIGN;
2770 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002771 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002772 }
2773}
2774
2775/*
2776 * Init <peer> peer after having connected it at peer protocol level.
2777 */
2778static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2779{
2780 struct shared_table *st;
2781
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002782 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002783 /* Init cursors */
2784 for (st = peer->tables; st ; st = st->next) {
2785 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002786 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2787 /* if st->update appears to be in future it means
2788 * that the last acked value is very old and we
2789 * remain unconnected a too long time to use this
2790 * acknowlegement as a reset.
2791 * We should update the protocol to be able to
2792 * signal the remote peer that it needs a full resync.
2793 * Here a partial fix consist to set st->update at
2794 * the max past value.
2795 */
2796 if ((int)(st->table->localupdate - st->update) < 0)
2797 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002798 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002799 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002800 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2801 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002802 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002803 }
2804
2805 /* Init confirm counter */
2806 peer->confirm = 0;
2807
2808 /* reset teaching and learning flags to 0 */
2809 peer->flags &= PEER_TEACH_RESET;
2810 peer->flags &= PEER_LEARN_RESET;
2811
2812 /* If current peer is local */
2813 if (peer->local) {
2814 /* flag to start to teach lesson */
2815 peer->flags |= PEER_F_TEACH_PROCESS;
2816 }
2817 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2818 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2819 /* If peer is remote and resync from remote is needed,
2820 and no peer currently assigned */
2821
2822 /* assign peer for a lesson */
2823 peer->flags |= PEER_F_LEARN_ASSIGN;
2824 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002825 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002826 }
2827}
2828
2829/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002830 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002831 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002832static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002833{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002834 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002835 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002836 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002837 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002838 int reql = 0;
2839 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002840 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002841 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002842
Joseph Herlant82b2f542018-11-15 12:19:14 -08002843 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002844 if (si_ic(si)->buf.size == 0) {
2845 si_rx_room_blk(si);
2846 goto out;
2847 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002848
Emeric Brun2b920a12010-09-23 18:30:22 +02002849 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002850 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002851switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002852 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002853 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002854 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002855 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002857 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002858 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002859 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002860 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002861 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2862 if (reql <= 0) {
2863 if (!reql)
2864 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002865 goto switchstate;
2866 }
2867
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002868 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002869 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002870 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002871 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002872 reql = peer_getline_host(appctx);
2873 if (reql <= 0) {
2874 if (!reql)
2875 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002876 goto switchstate;
2877 }
2878
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002879 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002880 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002881 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002882 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002883 reql = peer_getline_last(appctx, &curpeer);
2884 if (reql <= 0) {
2885 if (!reql)
2886 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002887 goto switchstate;
2888 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002889
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002890 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002891 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002892 if (curpeer->local) {
2893 /* Local connection, reply a retry */
2894 appctx->st0 = PEER_SESS_ST_EXIT;
2895 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2896 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002897 }
Emeric Brun80527f52017-06-19 17:46:37 +02002898
2899 /* we're killing a connection, we must apply a random delay before
2900 * retrying otherwise the other end will do the same and we can loop
2901 * for a while.
2902 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002903 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002904 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002905 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002906 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002907 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002908 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2909 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2910 curpeer->flags |= PEER_F_DWNGRD;
2911 }
2912 else {
2913 curpeer->flags &= ~PEER_F_DWNGRD;
2914 }
2915 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002916 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002917 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002918 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002919 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002920 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002921 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002922 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002923 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002924 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002925 if (!curpeer) {
2926 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002927 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002928 if (curpeer->appctx != appctx) {
2929 appctx->st0 = PEER_SESS_ST_END;
2930 goto switchstate;
2931 }
2932 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002933
2934 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002935 if (repl <= 0) {
2936 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002937 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002938 goto switchstate;
2939 }
2940
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002941 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002942
Emeric Brun2b920a12010-09-23 18:30:22 +02002943 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002944 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002945 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002946 goto switchstate;
2947 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002948 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002949 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002950 if (!curpeer) {
2951 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002952 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002953 if (curpeer->appctx != appctx) {
2954 appctx->st0 = PEER_SESS_ST_END;
2955 goto switchstate;
2956 }
2957 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002958
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002959 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002960 if (repl <= 0) {
2961 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002962 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002963 goto switchstate;
2964 }
2965
2966 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002967 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002968 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002969 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002970 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002971 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002972 if (!curpeer) {
2973 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002974 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002975 if (curpeer->appctx != appctx) {
2976 appctx->st0 = PEER_SESS_ST_END;
2977 goto switchstate;
2978 }
2979 }
2980
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002981 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002982 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002983
Frédéric Lécaillece025572019-01-21 13:38:06 +01002984 reql = peer_getline(appctx);
2985 if (!reql)
2986 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002987
Frédéric Lécaillece025572019-01-21 13:38:06 +01002988 if (reql < 0)
2989 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002990
2991 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002992 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002993 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02002994
2995 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002996 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002997
2998 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002999 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01003000 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02003001 }
3002 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02003003 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
3004 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02003005 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003006 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02003007 goto switchstate;
3008 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003009 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003010 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02003011 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003012 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003013 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003014 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003015 char *msg_cur = trash.area;
3016 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01003017 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02003018 int totl = 0;
3019
Willy Tarreau2d372c22018-11-05 17:12:27 +01003020 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003021 if (!curpeer) {
3022 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003023 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003024 if (curpeer->appctx != appctx) {
3025 appctx->st0 = PEER_SESS_ST_END;
3026 goto switchstate;
3027 }
3028 }
3029
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003030 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
3031 if (reql <= 0) {
3032 if (reql == -1)
3033 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003034 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003035 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01003036
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003037 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01003038 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02003039 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003040
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003041 curpeer->flags |= PEER_F_ALIVE;
3042
Emeric Brun2b920a12010-09-23 18:30:22 +02003043 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003044 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02003045 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02003046 goto switchstate;
3047
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003048send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003049 if (curpeer->flags & PEER_F_HEARTBEAT) {
3050 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003051 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003052 if (repl <= 0) {
3053 if (repl == -1)
3054 goto out;
3055 goto switchstate;
3056 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01003057 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003058 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003059 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003060 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01003061 if (repl <= 0) {
3062 if (repl == -1)
3063 goto out;
3064 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02003065 }
3066
Emeric Brun2b920a12010-09-23 18:30:22 +02003067 /* noting more to do */
3068 goto out;
3069 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003070 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01003071 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003072 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003073 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003074 if (peer_send_status_errormsg(appctx) == -1)
3075 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003076 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003077 goto switchstate;
3078 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003079 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003080 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003081 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003082 if (peer_send_error_size_limitmsg(appctx) == -1)
3083 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003084 appctx->st0 = PEER_SESS_ST_END;
3085 goto switchstate;
3086 }
3087 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003088 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
3089 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003090 if (curpeer)
3091 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003092 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003093 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003094 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003095 if (peer_send_error_protomsg(appctx) == -1) {
3096 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003097 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003098 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003099 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003100 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003101 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003102 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003103 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003104 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003105 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003106 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003107 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003108 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003109 curpeer = NULL;
3110 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02003111 si_shutw(si);
3112 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003113 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02003114 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003115 }
3116 }
3117 }
3118out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003119 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02003120
3121 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003122 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02003123 return;
3124}
3125
Willy Tarreau30576452015-04-13 13:50:30 +02003126static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003127 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003128 .name = "<PEER>", /* used for logging */
3129 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07003130 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003131};
Emeric Brun2b920a12010-09-23 18:30:22 +02003132
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003133
Emeric Brun2b920a12010-09-23 18:30:22 +02003134/*
3135 * Use this function to force a close of a peer session
3136 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003137static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003138{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003139 struct appctx *appctx = peer->appctx;
3140
Frédéric Lécaille5df11902017-06-13 16:39:57 +02003141 /* Note that the peer sessions which have just been created
3142 * (->st0 == PEER_SESS_ST_CONNECT) must not
3143 * be shutdown, if not, the TCP session will never be closed
3144 * and stay in CLOSE_WAIT state after having been closed by
3145 * the remote side.
3146 */
3147 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 return;
3149
Willy Tarreau81bc3b02016-10-31 17:37:39 +01003150 if (appctx->applet != &peer_applet)
3151 return;
3152
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003153 __peer_session_deinit(peer);
3154
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003155 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01003156 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003157}
3158
Willy Tarreau91d96282015-03-13 15:47:26 +01003159/* Pre-configures a peers frontend to accept incoming connections */
3160void peers_setup_frontend(struct proxy *fe)
3161{
3162 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02003163 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02003164 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01003165 fe->maxconn = 0;
3166 fe->conn_retries = CONN_RETRIES;
3167 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01003168 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01003169 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01003170 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
3171}
3172
Emeric Brun2b920a12010-09-23 18:30:22 +02003173/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01003174 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02003175 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003176static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003177{
Willy Tarreau04b92862017-09-15 11:01:04 +02003178 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003179 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02003180 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02003181 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02003182
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01003183 peer->new_conn++;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003184 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003185 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003186 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003187 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003188 s = NULL;
3189
Willy Tarreaue6124462021-09-13 10:07:38 +02003190 appctx = appctx_new(&peer_applet);
Willy Tarreaud990baf2015-04-05 00:32:03 +02003191 if (!appctx)
3192 goto out_close;
3193
3194 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003195 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003196
Willy Tarreau04b92862017-09-15 11:01:04 +02003197 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02003198 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003199 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02003200 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003201 }
3202
Christopher Faulet26256f82020-09-14 11:40:13 +02003203 if ((s = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003204 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02003205 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02003206 }
3207
Willy Tarreau6e2979c2015-04-27 13:21:15 +02003208 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003209 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02003210 appctx_wakeup(appctx);
3211
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02003212 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02003213 s->target = peer_session_target(peer, s);
Willy Tarreau9b7587a2020-10-15 07:32:10 +02003214 if (!sockaddr_alloc(&s->target_addr, &peer->addr, sizeof(peer->addr)))
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02003215 goto out_free_strm;
Willy Tarreau02efeda2019-07-18 17:21:24 +02003216 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Willy Tarreaudbd02672017-12-06 17:39:53 +01003217 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003218
Emeric Brun2b920a12010-09-23 18:30:22 +02003219 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003220 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02003221
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003222 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01003223
Emeric Brunb3971ab2015-05-12 18:49:09 +02003224 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02003225 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau4781b152021-04-06 13:53:36 +02003226 _HA_ATOMIC_INC(&active_peers);
Willy Tarreau9df94c22016-10-31 18:42:52 +01003227 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003228
3229 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02003230 out_free_strm:
Willy Tarreau2b718102021-04-21 07:32:39 +02003231 LIST_DELETE(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003232 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02003233 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02003234 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02003235 out_free_appctx:
3236 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003237 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01003238 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003239}
3240
3241/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003242 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01003243 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
3244 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02003245 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003246struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02003247{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003248 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003249 struct peer *ps;
3250 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02003251
3252 task->expire = TICK_ETERNITY;
3253
Emeric Brunb3971ab2015-05-12 18:49:09 +02003254 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003255 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003256 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003257 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02003258 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003259 return NULL;
3260 }
3261
Emeric Brun80527f52017-06-19 17:46:37 +02003262 /* Acquire lock for all peers of the section */
3263 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003264 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003265
Emeric Brun2b920a12010-09-23 18:30:22 +02003266 if (!stopping) {
3267 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02003268
Emeric Brun2c4ab412021-04-21 16:06:35 +02003269 /* resync timeout set to TICK_ETERNITY means we just start
3270 * a new process and timer was not initialized.
3271 * We must arm this timer to switch to a request to a remote
3272 * node if incoming connection from old local process never
3273 * comes.
3274 */
3275 if (peers->resync_timeout == TICK_ETERNITY)
3276 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3277
Emeric Brunb3971ab2015-05-12 18:49:09 +02003278 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
3279 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
3280 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003281 /* Resync from local peer needed
3282 no peer was assigned for the lesson
3283 and no old local peer found
3284 or resync timeout expire */
3285
3286 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003287 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003288 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003289
3290 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003291 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02003292 }
3293
3294 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003295 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003296 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003297 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003298 if (!ps->appctx) {
3299 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003300 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003301 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003302 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003303 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003304 tick_is_expired(ps->reconnect, now_ms))) {
3305 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003306 * or previous peer connection established with success
3307 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003308 * and reconnection timer is expired */
3309
3310 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003311 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003312 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003313 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003314 /* If previous session failed during connection
3315 * but reconnection timer is not expired */
3316
3317 /* reschedule task for reconnect */
3318 task->expire = tick_first(task->expire, ps->reconnect);
3319 }
3320 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003321 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003322 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003323 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003324 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3325 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003326 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3327 /* Resync from a remote is needed
3328 * and no peer was assigned for lesson
3329 * and current peer may be up2date */
3330
3331 /* assign peer for the lesson */
3332 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003333 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003334 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003335
Willy Tarreau9df94c22016-10-31 18:42:52 +01003336 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003337 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003338 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003339 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003340 int update_to_push = 0;
3341
Emeric Brunb3971ab2015-05-12 18:49:09 +02003342 /* Awake session if there is data to push */
3343 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003344 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003345 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003346 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003347 /* There is no need to send a heartbeat message
3348 * when some updates must be pushed. The remote
3349 * peer will consider <ps> peer as alive when it will
3350 * receive these updates.
3351 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003352 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003353 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003354 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003355 /* We are going to send updates, let's ensure we will
3356 * come back to send heartbeat messages or to reconnect.
3357 */
3358 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003359 appctx_wakeup(ps->appctx);
3360 break;
3361 }
3362 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003363 /* When there are updates to send we do not reconnect
3364 * and do not send heartbeat message either.
3365 */
3366 if (!update_to_push) {
3367 if (tick_is_expired(ps->reconnect, now_ms)) {
3368 if (ps->flags & PEER_F_ALIVE) {
3369 /* This peer was alive during a 'reconnect' period.
3370 * Flag it as not alive again for the next period.
3371 */
3372 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003373 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003374 }
3375 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003376 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003377 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003378 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003379 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003380 }
3381 }
3382 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3383 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3384 ps->flags |= PEER_F_HEARTBEAT;
3385 appctx_wakeup(ps->appctx);
3386 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003387 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003388 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003389 }
3390 /* else do nothing */
3391 } /* SUCCESSCODE */
3392 } /* !ps->peer->local */
3393 } /* for */
3394
3395 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003396 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3397 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3398 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003399 /* Resync from remote peer needed
3400 * no peer was assigned for the lesson
3401 * and resync timeout expire */
3402
3403 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003404 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003405 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003406 }
3407
Emeric Brunb3971ab2015-05-12 18:49:09 +02003408 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003409 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003410 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3411 if (!tick_is_expired(peers->resync_timeout, now_ms))
3412 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003413 }
3414 } /* !stopping */
3415 else {
3416 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003417 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003418 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003419 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003420 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003421 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003422 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003423
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003424 /* disconnect all connected peers to process a local sync
3425 * this must be done only the first time we are switching
3426 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003427 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003428 for (ps = peers->remote; ps; ps = ps->next) {
3429 /* we're killing a connection, we must apply a random delay before
3430 * retrying otherwise the other end will do the same and we can loop
3431 * for a while.
3432 */
3433 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3434 if (ps->appctx) {
3435 peer_session_forceshutdown(ps);
3436 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003437 }
3438 }
3439 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003440
Emeric Brunb3971ab2015-05-12 18:49:09 +02003441 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003442 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003443 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003444 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003445 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003446 peers->flags &= ~PEERS_F_DONOTSTOP;
3447 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003448 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003449 }
3450 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003451 else if (!ps->appctx) {
3452 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003453 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003454 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3455 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3456 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003457 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003458 * or previous peer connection was successfully established
3459 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02003460 * or during previous connect, peer replies a try again statuscode */
3461
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003462 /* connect to the local peer if we must push a local sync */
3463 if (peers->flags & PEERS_F_DONOTSTOP) {
3464 peer_session_create(peers, ps);
3465 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003466 }
3467 else {
3468 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003469 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003470 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003471 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003472 peers->flags &= ~PEERS_F_DONOTSTOP;
3473 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003474 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003475 }
3476 }
3477 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003478 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003479 /* current peer connection is active and established
3480 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003481 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003482 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003483 appctx_wakeup(ps->appctx);
3484 break;
3485 }
3486 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003487 }
3488 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003489
3490 /* Release lock for all peers of the section */
3491 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003492 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003493
Emeric Brun2b920a12010-09-23 18:30:22 +02003494 /* Wakeup for re-connect */
3495 return task;
3496}
3497
Emeric Brunb3971ab2015-05-12 18:49:09 +02003498
Emeric Brun2b920a12010-09-23 18:30:22 +02003499/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003500 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003501 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003502int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003503{
Emeric Brun2b920a12010-09-23 18:30:22 +02003504 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003505
Emeric Brun2b920a12010-09-23 18:30:22 +02003506 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003507 peers->peers_fe->maxconn += 3;
3508 }
3509
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003510 peers->sync_task = task_new_anywhere();
Willy Tarreaud9443442018-10-15 11:18:03 +02003511 if (!peers->sync_task)
3512 return 0;
3513
Emeric Brunb3971ab2015-05-12 18:49:09 +02003514 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003515 peers->sync_task->context = (void *)peers;
3516 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3517 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003518 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003519}
3520
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003521/*
3522 * Allocate a cache a dictionary entries used upon transmission.
3523 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003524static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003525{
3526 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003527 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003528
3529 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003530 entries = calloc(max_entries, sizeof *entries);
3531 if (!d || !entries)
3532 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003533
3534 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003535 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003536 d->cached_entries = EB_ROOT_UNIQUE;
3537 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003538
3539 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003540
3541 err:
3542 free(d);
3543 free(entries);
3544 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003545}
3546
3547/*
3548 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3549 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003550 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003551 * Must be deallocated calling free_dcache().
3552 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003553static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003554{
3555 struct dcache_tx *dc_tx;
3556 struct dcache *dc;
3557 struct dcache_rx *dc_rx;
3558
3559 dc = calloc(1, sizeof *dc);
3560 dc_tx = new_dcache_tx(max_entries);
3561 dc_rx = calloc(max_entries, sizeof *dc_rx);
3562 if (!dc || !dc_tx || !dc_rx)
3563 goto err;
3564
3565 dc->tx = dc_tx;
3566 dc->rx = dc_rx;
3567 dc->max_entries = max_entries;
3568
3569 return dc;
3570
3571 err:
3572 free(dc);
3573 free(dc_tx);
3574 free(dc_rx);
3575 return NULL;
3576}
3577
3578/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003579 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3580 * entries used upon transmission.
3581 * Return the entry if found, NULL if not.
3582 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003583static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3584 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003585{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003586 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003587}
3588
3589/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003590 * Flush <dc> cache.
3591 * Always succeeds.
3592 */
3593static inline void flush_dcache(struct peer *peer)
3594{
3595 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003596 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003597
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003598 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003599 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003600 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003601 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3602 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003603 }
3604 dc->tx->prev_lookup = NULL;
3605 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003606
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003607 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003608}
3609
3610/*
3611 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3612 * with information provided by <i> dictionary cache entry (especially the value
3613 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003614 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003615 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003616static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003617{
3618 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003619 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003620
3621 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003622
3623 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3624 o = dc_tx->prev_lookup;
3625 } else {
3626 o = dcache_tx_lookup_value(dc_tx, i);
3627 if (o) {
3628 /* Save it */
3629 dc_tx->prev_lookup = o;
3630 }
3631 }
3632
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003633 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003634 /* Copy the ID. */
3635 i->id = o - dc->tx->entries;
3636 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003637 }
3638
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003639 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003640 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003641
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003642 ebpt_delete(o);
3643 o->key = i->entry.key;
3644 ebpt_insert(&dc_tx->cached_entries, o);
3645 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003646
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003647 /* Update the index for the next entry to put in cache */
3648 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003649
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003650 return o;
3651}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003652
3653/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003654 * Allocate a dictionary cache for each peer of <peers> section.
3655 * Return 1 if succeeded, 0 if not.
3656 */
3657int peers_alloc_dcache(struct peers *peers)
3658{
3659 struct peer *p;
3660
3661 for (p = peers->remote; p; p = p->next) {
3662 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3663 if (!p->dcache)
3664 return 0;
3665 }
3666
3667 return 1;
3668}
3669
3670/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003671 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003672 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003673 */
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003674int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003675{
3676 struct shared_table *st;
3677 struct peer * curpeer;
3678 int id = 0;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003679 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003680
3681 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003682 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003683 if (!st) {
3684 retval = 1;
3685 break;
3686 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003687 st->table = table;
3688 st->next = curpeer->tables;
3689 if (curpeer->tables)
3690 id = curpeer->tables->local_id;
3691 st->local_id = id + 1;
3692
Emeric Brun2cc201f2021-04-23 12:21:26 +02003693 /* If peer is local we inc table
3694 * refcnt to protect against flush
3695 * until this process pushed all
3696 * table content to the new one
3697 */
3698 if (curpeer->local)
3699 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003700 curpeer->tables = st;
3701 }
3702
3703 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003704
3705 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003706}
3707
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003708/*
3709 * Parse the "show peers" command arguments.
3710 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3711 * error message.
3712 */
3713static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3714{
3715 appctx->ctx.cfgpeers.target = NULL;
3716
Willy Tarreau49962b52021-02-12 16:56:22 +01003717 if (strcmp(args[2], "dict") == 0) {
3718 /* show the dictionaries (large dump) */
3719 appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT;
3720 args++;
3721 } else if (strcmp(args[2], "-") == 0)
3722 args++; // allows to show a section called "dict"
3723
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003724 if (*args[2]) {
3725 struct peers *p;
3726
3727 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003728 if (strcmp(p->id, args[2]) == 0) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003729 appctx->ctx.cfgpeers.target = p;
3730 break;
3731 }
3732 }
3733
Willy Tarreau9d008692019-08-09 11:21:01 +02003734 if (!p)
3735 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003736 }
3737
3738 return 0;
3739}
3740
3741/*
3742 * This function dumps the peer state information of <peers> "peers" section.
3743 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3744 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3745 */
3746static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3747{
3748 struct tm tm;
3749
3750 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003751 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 +02003752 peers,
3753 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3754 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003755 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003756 peers->resync_timeout ?
3757 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3758 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003759 TICKS_TO_MS(1000)) : "<NEVER>",
3760 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003761
3762 if (ci_putchk(si_ic(si), msg) == -1) {
3763 si_rx_room_blk(si);
3764 return 0;
3765 }
3766
3767 return 1;
3768}
3769
3770/*
3771 * This function dumps <peer> state information.
3772 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3773 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3774 */
Willy Tarreau49962b52021-02-12 16:56:22 +01003775static 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 +02003776{
3777 struct connection *conn;
3778 char pn[INET6_ADDRSTRLEN];
3779 struct stream_interface *peer_si;
3780 struct stream *peer_s;
3781 struct appctx *appctx;
3782 struct shared_table *st;
3783
3784 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003785 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003786 peer, peer->id,
3787 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003788 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003789 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003790 statuscode_str(peer->statuscode));
3791
3792 chunk_appendf(msg, " last_hdshk=%s\n",
3793 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3794 TICKS_TO_MS(1000)) : "<NEVER>");
3795
3796 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003797 peer->reconnect ?
3798 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3799 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003800 TICKS_TO_MS(1000)) : "<NEVER>");
3801
3802 chunk_appendf(msg, " heartbeat=%s",
3803 peer->heartbeat ?
3804 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3805 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3806 TICKS_TO_MS(1000)) : "<NEVER>");
3807
3808 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 +01003809 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003810 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003811
3812 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3813
3814 appctx = peer->appctx;
3815 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003816 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003817
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003818 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3819 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003820
3821 peer_si = peer->appctx->owner;
3822 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003823 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003824
3825 peer_s = si_strm(peer_si);
3826 if (!peer_s)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003827 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003828
3829 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3830
3831 conn = objt_conn(strm_orig(peer_s));
3832 if (conn)
3833 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3834
Willy Tarreau3ca14902019-07-17 14:53:15 +02003835 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 +02003836 case AF_INET:
3837 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003838 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003839 break;
3840 case AF_UNIX:
3841 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3842 break;
3843 }
3844
Willy Tarreau3ca14902019-07-17 14:53:15 +02003845 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 +02003846 case AF_INET:
3847 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003848 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003849 break;
3850 case AF_UNIX:
3851 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3852 break;
3853 }
3854
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003855 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003856 if (peer->remote_table)
3857 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3858 peer->remote_table,
3859 peer->remote_table->table->id,
3860 peer->remote_table->local_id,
3861 peer->remote_table->remote_id);
3862
3863 if (peer->last_local_table)
3864 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3865 peer->last_local_table,
3866 peer->last_local_table->table->id,
3867 peer->last_local_table->local_id,
3868 peer->last_local_table->remote_id);
3869
3870 if (peer->tables) {
3871 chunk_appendf(&trash, "\n shared tables:");
3872 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003873 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003874 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003875 struct dcache *dcache;
3876
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003877 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003878 dcache = peer->dcache;
3879
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003880 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3881 "flags=0x%x remote_data=0x%llx",
3882 st, st->local_id, st->remote_id,
3883 st->flags, (unsigned long long)st->remote_data);
3884 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3885 " teaching_origin=%u update=%u",
3886 st->last_acked, st->last_pushed, st->last_get,
3887 st->teaching_origin, st->update);
3888 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003889 " commitupdate=%u refcnt=%u",
3890 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003891 if (flags & PEERS_SHOW_F_DICT) {
3892 chunk_appendf(&trash, "\n TX dictionary cache:");
3893 count = 0;
3894 for (i = 0; i < dcache->max_entries; i++) {
3895 struct ebpt_node *node;
3896 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003897
Willy Tarreau49962b52021-02-12 16:56:22 +01003898 node = &dcache->tx->entries[i];
3899 if (!node->key)
3900 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003901
Willy Tarreau49962b52021-02-12 16:56:22 +01003902 if (!count++)
3903 chunk_appendf(&trash, "\n ");
3904 de = node->key;
3905 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3906 count &= 0x3;
3907 }
3908 chunk_appendf(&trash, "\n RX dictionary cache:");
3909 count = 0;
3910 for (i = 0; i < dcache->max_entries; i++) {
3911 if (!count++)
3912 chunk_appendf(&trash, "\n ");
3913 chunk_appendf(&trash, " %3u -> %s", i,
3914 dcache->rx[i].de ?
3915 (char *)dcache->rx[i].de->value.key : "-");
3916 count &= 0x3;
3917 }
3918 } else {
3919 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003920 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003921 }
3922 }
3923
3924 end:
3925 chunk_appendf(&trash, "\n");
3926 if (ci_putchk(si_ic(si), msg) == -1) {
3927 si_rx_room_blk(si);
3928 return 0;
3929 }
3930
3931 return 1;
3932}
3933
3934/*
3935 * This function dumps all the peers of "peers" section.
3936 * Returns 0 if the output buffer is full and needs to be called
3937 * again, non-zero if not. It proceeds in an isolated thread, so
3938 * there is no thread safety issue here.
3939 */
3940static int cli_io_handler_show_peers(struct appctx *appctx)
3941{
3942 int show_all;
3943 int ret = 0, first_peers = 1;
3944 struct stream_interface *si = appctx->owner;
3945
3946 thread_isolate();
3947
3948 show_all = !appctx->ctx.cfgpeers.target;
3949
3950 chunk_reset(&trash);
3951
3952 while (appctx->st2 != STAT_ST_FIN) {
3953 switch (appctx->st2) {
3954 case STAT_ST_INIT:
3955 if (show_all)
3956 appctx->ctx.cfgpeers.peers = cfg_peers;
3957 else
3958 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3959
3960 appctx->st2 = STAT_ST_LIST;
3961 /* fall through */
3962
3963 case STAT_ST_LIST:
3964 if (!appctx->ctx.cfgpeers.peers) {
3965 /* No more peers list. */
3966 appctx->st2 = STAT_ST_END;
3967 }
3968 else {
3969 if (!first_peers)
3970 chunk_appendf(&trash, "\n");
3971 else
3972 first_peers = 0;
3973 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3974 goto out;
3975
3976 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3977 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3978 appctx->st2 = STAT_ST_INFO;
3979 }
3980 break;
3981
3982 case STAT_ST_INFO:
3983 if (!appctx->ctx.cfgpeers.peer) {
3984 /* End of peer list */
3985 if (show_all)
3986 appctx->st2 = STAT_ST_LIST;
3987 else
3988 appctx->st2 = STAT_ST_END;
3989 }
3990 else {
Willy Tarreau49962b52021-02-12 16:56:22 +01003991 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer, appctx->ctx.cfgpeers.flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003992 goto out;
3993
3994 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3995 }
3996 break;
3997
3998 case STAT_ST_END:
3999 appctx->st2 = STAT_ST_FIN;
4000 break;
4001 }
4002 }
4003 ret = 1;
4004 out:
4005 thread_release();
4006 return ret;
4007}
4008
4009/*
4010 * CLI keywords.
4011 */
4012static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004013 { { "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 +02004014 {},
4015}};
4016
4017/* Register cli keywords */
4018INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4019