blob: fe50172a534c41f82ea8d712862f99053da6a11f [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/applet.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020025#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020027#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020029#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020030#include <haproxy/frontend.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020032#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020033#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020035#include <haproxy/session-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020036#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stick_table.h>
39#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020040#include <haproxy/stream_interface.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/task.h>
42#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020043#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020044#include <haproxy/tools.h>
Frédéric Lécaille94571512020-11-10 16:18:03 +010045#include <haproxy/trace.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020046
Emeric Brun2b920a12010-09-23 18:30:22 +020047
48/*******************************/
49/* Current peer learning state */
50/*******************************/
51
52/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020053/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020054/******************************/
Emeric Brune1f55742021-04-28 12:59:35 +020055#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
56#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
57#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
58#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
59#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
60#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
61#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
62#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
63#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
64#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
65#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
66#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
67#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
68#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
69#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
70#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
71 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020072
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010073#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
74#define PEERS_RESYNC_FROMLOCAL 0x00000000
75#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
76#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020077
78/***********************************/
79/* Current shared table sync state */
80/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010081#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
82#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020083
84/******************************/
85/* Remote peer teaching state */
86/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010087#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
88#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
89#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
90#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
91#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
92#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
93#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
94#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020095
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010096#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
97#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020098
Frédéric Lécaille54bff832019-03-26 10:25:20 +010099#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
100#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100101#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
102
Emeric Brunb3971ab2015-05-12 18:49:09 +0200103/*****************************/
104/* Sync message class */
105/*****************************/
106enum {
107 PEER_MSG_CLASS_CONTROL = 0,
108 PEER_MSG_CLASS_ERROR,
109 PEER_MSG_CLASS_STICKTABLE = 10,
110 PEER_MSG_CLASS_RESERVED = 255,
111};
112
113/*****************************/
114/* control message types */
115/*****************************/
116enum {
117 PEER_MSG_CTRL_RESYNCREQ = 0,
118 PEER_MSG_CTRL_RESYNCFINISHED,
119 PEER_MSG_CTRL_RESYNCPARTIAL,
120 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100121 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200122};
123
124/*****************************/
125/* error message types */
126/*****************************/
127enum {
128 PEER_MSG_ERR_PROTOCOL = 0,
129 PEER_MSG_ERR_SIZELIMIT,
130};
131
Emeric Brun530ba382020-06-02 11:17:42 +0200132/* network key types;
133 * network types were directly and mistakenly
134 * mapped on sample types, to keep backward
135 * compatiblitiy we keep those values but
136 * we now use a internal/network mapping
137 * to avoid further mistakes adding or
138 * modifying internals types
139 */
140enum {
141 PEER_KT_ANY = 0, /* any type */
142 PEER_KT_RESV1, /* UNUSED */
143 PEER_KT_SINT, /* signed 64bits integer type */
144 PEER_KT_RESV3, /* UNUSED */
145 PEER_KT_IPV4, /* ipv4 type */
146 PEER_KT_IPV6, /* ipv6 type */
147 PEER_KT_STR, /* char string type */
148 PEER_KT_BIN, /* buffer type */
149 PEER_KT_TYPES /* number of types, must always be last */
150};
151
152/* Map used to retrieve network type from internal type
153 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
154 */
155static int peer_net_key_type[SMP_TYPES] = {
156 [SMP_T_SINT] = PEER_KT_SINT,
157 [SMP_T_IPV4] = PEER_KT_IPV4,
158 [SMP_T_IPV6] = PEER_KT_IPV6,
159 [SMP_T_STR] = PEER_KT_STR,
160 [SMP_T_BIN] = PEER_KT_BIN,
161};
162
163/* Map used to retrieve internal type from external type
164 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
165 */
166static int peer_int_key_type[PEER_KT_TYPES] = {
167 [PEER_KT_SINT] = SMP_T_SINT,
168 [PEER_KT_IPV4] = SMP_T_IPV4,
169 [PEER_KT_IPV6] = SMP_T_IPV6,
170 [PEER_KT_STR] = SMP_T_STR,
171 [PEER_KT_BIN] = SMP_T_BIN,
172};
173
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100174/*
175 * Parameters used by functions to build peer protocol messages. */
176struct peer_prep_params {
177 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100178 struct peer *peer;
179 } hello;
180 struct {
181 unsigned int st1;
182 } error_status;
183 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100184 struct stksess *stksess;
185 struct shared_table *shared_table;
186 unsigned int updateid;
187 int use_identifier;
188 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200189 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100190 } updt;
191 struct {
192 struct shared_table *shared_table;
193 } swtch;
194 struct {
195 struct shared_table *shared_table;
196 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100197 struct {
198 unsigned char head[2];
199 } control;
200 struct {
201 unsigned char head[2];
202 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100203};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200204
205/*******************************/
206/* stick table sync mesg types */
207/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500208/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200209/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200210#define PEER_MSG_STKT_UPDATE 0x80
211#define PEER_MSG_STKT_INCUPDATE 0x81
212#define PEER_MSG_STKT_DEFINE 0x82
213#define PEER_MSG_STKT_SWITCH 0x83
214#define PEER_MSG_STKT_ACK 0x84
215#define PEER_MSG_STKT_UPDATE_TIMED 0x85
216#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200217/* All the stick-table message identifiers abova have the #7 bit set */
218#define PEER_MSG_STKT_BIT 7
219#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200220
Frédéric Lécaille39143342019-05-24 14:32:27 +0200221/* The maximum length of an encoded data length. */
222#define PEER_MSG_ENC_LENGTH_MAXLEN 5
223
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200224/* Minimum 64-bits value encoded with 2 bytes */
225#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
226/* 3 bytes */
227#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
228/* 4 bytes */
229#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
230/* 5 bytes */
231#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
232/* 6 bytes */
233#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
234/* 7 bytes */
235#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
236/* 8 bytes */
237#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
238/* 9 bytes */
239#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
240/* 10 bytes */
241#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
242
243/* #7 bit used to detect the last byte to be encoded */
244#define PEER_ENC_STOP_BIT 7
245/* The byte minimum value with #7 bit set */
246#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
247/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
248#define PEER_ENC_2BYTES_MIN_BITS 4
249
Frédéric Lécaille39143342019-05-24 14:32:27 +0200250#define PEER_MSG_HEADER_LEN 2
251
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200252#define PEER_STKT_CACHE_MAX_ENTRIES 128
253
Emeric Brun2b920a12010-09-23 18:30:22 +0200254/**********************************/
255/* Peer Session IO handler states */
256/**********************************/
257
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100258enum {
259 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
260 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
261 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
262 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100263 /* after this point, data were possibly exchanged */
264 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
265 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
266 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
267 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
268 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200269 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
270 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100271 PEER_SESS_ST_END, /* Killed session */
272};
Emeric Brun2b920a12010-09-23 18:30:22 +0200273
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100274/***************************************************/
275/* Peer Session status code - part of the protocol */
276/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200277
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100278#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
279#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200280
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100281#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200282
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100283#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200284
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100285#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
286#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
287#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
288#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200289
290#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200291#define PEER_MAJOR_VER 2
292#define PEER_MINOR_VER 1
293#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200294
Willy Tarreau6254a922019-01-29 17:45:23 +0100295static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200296struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200297static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200298
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200299static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
300 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200301static inline void flush_dcache(struct peer *peer);
302
Frédéric Lécaille94571512020-11-10 16:18:03 +0100303/* trace source and events */
304static void peers_trace(enum trace_level level, uint64_t mask,
305 const struct trace_source *src,
306 const struct ist where, const struct ist func,
307 const void *a1, const void *a2, const void *a3, const void *a4);
308
309static const struct trace_event peers_trace_events[] = {
310#define PEERS_EV_UPDTMSG (1 << 0)
311 { .mask = PEERS_EV_UPDTMSG, .name = "updtmsg", .desc = "update message received" },
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +0100312#define PEERS_EV_ACKMSG (1 << 1)
313 { .mask = PEERS_EV_ACKMSG, .name = "ackmsg", .desc = "ack message received" },
314#define PEERS_EV_SWTCMSG (1 << 2)
315 { .mask = PEERS_EV_SWTCMSG, .name = "swtcmsg", .desc = "switch message received" },
316#define PEERS_EV_DEFMSG (1 << 3)
317 { .mask = PEERS_EV_DEFMSG, .name = "defmsg", .desc = "definition message received" },
318#define PEERS_EV_CTRLMSG (1 << 4)
319 { .mask = PEERS_EV_CTRLMSG, .name = "ctrlmsg", .desc = "control message sent/received" },
320#define PEERS_EV_SESSREL (1 << 5)
321 { .mask = PEERS_EV_SESSREL, .name = "sessrl", .desc = "peer session releasing" },
322#define PEERS_EV_PROTOERR (1 << 6)
323 { .mask = PEERS_EV_PROTOERR, .name = "protoerr", .desc = "protocol error" },
Frédéric Lécaille94571512020-11-10 16:18:03 +0100324};
325
326static const struct name_desc peers_trace_lockon_args[4] = {
327 /* arg1 */ { /* already used by the connection */ },
328 /* arg2 */ { .name="peers", .desc="Peers protocol" },
329 /* arg3 */ { },
330 /* arg4 */ { }
331};
332
333static const struct name_desc peers_trace_decoding[] = {
334#define PEERS_VERB_CLEAN 1
335 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
336 { /* end */ }
337};
338
339
340struct trace_source trace_peers = {
341 .name = IST("peers"),
342 .desc = "Peers protocol",
343 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
344 .default_cb = peers_trace,
345 .known_events = peers_trace_events,
346 .lockon_args = peers_trace_lockon_args,
347 .decoding = peers_trace_decoding,
348 .report_events = ~0, /* report everything by default */
349};
350
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +0100351/* Return peer control message types as strings (only for debugging purpose). */
352static inline char *ctrl_msg_type_str(unsigned int type)
353{
354 switch (type) {
355 case PEER_MSG_CTRL_RESYNCREQ:
356 return "RESYNCREQ";
357 case PEER_MSG_CTRL_RESYNCFINISHED:
358 return "RESYNCFINISHED";
359 case PEER_MSG_CTRL_RESYNCPARTIAL:
360 return "RESYNCPARTIAL";
361 case PEER_MSG_CTRL_RESYNCCONFIRM:
362 return "RESYNCCONFIRM";
363 case PEER_MSG_CTRL_HEARTBEAT:
364 return "HEARTBEAT";
365 default:
366 return "???";
367 }
368}
369
Frédéric Lécaille94571512020-11-10 16:18:03 +0100370#define TRACE_SOURCE &trace_peers
371INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
372
373static void peers_trace(enum trace_level level, uint64_t mask,
374 const struct trace_source *src,
375 const struct ist where, const struct ist func,
376 const void *a1, const void *a2, const void *a3, const void *a4)
377{
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +0100378 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécaille94571512020-11-10 16:18:03 +0100379 if (a2) {
380 const struct peer *peer = a2;
381
382 chunk_appendf(&trace_buf, " peer=%s", peer->id);
383 }
384 if (a3) {
385 const char *p = a3;
386
387 chunk_appendf(&trace_buf, " @%p", p);
388 }
389 if (a4) {
390 const size_t *val = a4;
391
392 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
393 }
394 }
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +0100395
396 if (mask & PEERS_EV_DEFMSG) {
397 if (a2) {
398 const struct peer *peer = a2;
399
400 chunk_appendf(&trace_buf, " peer=%s", peer->id);
401 }
402 if (a3) {
403 const char *p = a3;
404
405 chunk_appendf(&trace_buf, " @%p", p);
406 }
407 if (a4) {
408 const int *val = a4;
409
410 chunk_appendf(&trace_buf, " %d", *val);
411 }
412 }
413
414 if (mask & PEERS_EV_CTRLMSG) {
415 if (a2) {
416 const unsigned char *ctrl_msg_type = a2;
417
418 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
419
420 }
421 if (a3) {
422 const char *local_peer = a3;
423
424 chunk_appendf(&trace_buf, " %s", local_peer);
425 }
426
427 if (a4) {
428 const char *remote_peer = a4;
429
430 chunk_appendf(&trace_buf, " -> %s", remote_peer);
431 }
432 }
433
434 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
435 if (a2) {
436 const struct peer *peer = a2;
437 struct peers *peers = NULL;
438
Frédéric Lécaille707a1f52021-01-17 13:08:39 +0100439 if (peer && peer->appctx) {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +0100440 struct stream_interface *si;
441
442 si = peer->appctx->owner;
443 if (si) {
444 struct stream *s = si_strm(si);
445
446 peers = strm_fe(s)->parent;
447 }
448 }
449
450 if (peers)
451 chunk_appendf(&trace_buf, " %s", peers->local->id);
452 if (peer)
453 chunk_appendf(&trace_buf, " -> %s", peer->id);
454 }
455
456 if (a3) {
457 const int *prev_state = a3;
458
459 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
460 }
461 }
Frédéric Lécaille94571512020-11-10 16:18:03 +0100462}
463
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200464static const char *statuscode_str(int statuscode)
465{
466 switch (statuscode) {
467 case PEER_SESS_SC_CONNECTCODE:
468 return "CONN";
469 case PEER_SESS_SC_CONNECTEDCODE:
470 return "HSHK";
471 case PEER_SESS_SC_SUCCESSCODE:
472 return "ESTA";
473 case PEER_SESS_SC_TRYAGAIN:
474 return "RETR";
475 case PEER_SESS_SC_ERRPROTO:
476 return "PROT";
477 case PEER_SESS_SC_ERRVERSION:
478 return "VERS";
479 case PEER_SESS_SC_ERRHOST:
480 return "NAME";
481 case PEER_SESS_SC_ERRPEER:
482 return "UNKN";
483 default:
484 return "NONE";
485 }
486}
487
Emeric Brun18928af2017-03-29 16:32:53 +0200488/* This function encode an uint64 to 'dynamic' length format.
489 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500490 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200491 At return, the *str is set at the next Byte after then
492 encoded integer. The function returns then length of the
493 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200494int intencode(uint64_t i, char **str) {
495 int idx = 0;
496 unsigned char *msg;
497
Emeric Brunb3971ab2015-05-12 18:49:09 +0200498 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200499 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200500 msg[0] = (unsigned char)i;
501 *str = (char *)&msg[idx+1];
502 return (idx+1);
503 }
504
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200505 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
506 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
507 while (i >= PEER_ENC_STOP_BYTE) {
508 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
509 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200510 }
511 msg[++idx] = (unsigned char)i;
512 *str = (char *)&msg[idx+1];
513 return (idx+1);
514}
515
516
517/* This function returns the decoded integer or 0
518 if decode failed
519 *str point on the beginning of the integer to decode
520 at the end of decoding *str point on the end of the
521 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200522uint64_t intdecode(char **str, char *end)
523{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200524 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200525 uint64_t i;
526 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200527
528 if (!*str)
529 return 0;
530
531 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200532 if (msg >= (unsigned char *)end)
533 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200534
Emeric Brun18928af2017-03-29 16:32:53 +0200535 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200536 if (i >= PEER_ENC_2BYTES_MIN) {
537 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200538 do {
539 if (msg >= (unsigned char *)end)
540 goto fail;
541 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200542 shift += PEER_ENC_STOP_BIT;
543 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200544 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100545 *str = (char *)msg;
546 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200547
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100548 fail:
549 *str = NULL;
550 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200551}
Emeric Brun2b920a12010-09-23 18:30:22 +0200552
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100553/*
554 * Build a "hello" peer protocol message.
555 * Return the number of written bytes written to build this messages if succeeded,
556 * 0 if not.
557 */
558static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
559{
560 int min_ver, ret;
561 struct peer *peer;
562
563 peer = p->hello.peer;
564 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
565 /* Prepare headers */
566 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
567 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
568 if (ret >= size)
569 return 0;
570
571 return ret;
572}
573
574/*
575 * Build a "handshake succeeded" status message.
576 * Return the number of written bytes written to build this messages if succeeded,
577 * 0 if not.
578 */
579static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
580{
581 int ret;
582
583 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
584 if (ret >= size)
585 return 0;
586
587 return ret;
588}
589
590/*
591 * Build an error status message.
592 * Return the number of written bytes written to build this messages if succeeded,
593 * 0 if not.
594 */
595static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
596{
597 int ret;
598 unsigned int st1;
599
600 st1 = p->error_status.st1;
601 ret = snprintf(msg, size, "%d\n", st1);
602 if (ret >= size)
603 return 0;
604
605 return ret;
606}
607
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200608/* Set the stick-table UPDATE message type byte at <msg_type> address,
609 * depending on <use_identifier> and <use_timed> boolean parameters.
610 * Always successful.
611 */
612static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
613{
614 if (use_timed) {
615 if (use_identifier)
616 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
617 else
618 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
619 }
620 else {
621 if (use_identifier)
622 *msg_type = PEER_MSG_STKT_UPDATE;
623 else
624 *msg_type = PEER_MSG_STKT_INCUPDATE;
625 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200626}
Emeric Brun2b920a12010-09-23 18:30:22 +0200627/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200628 * This prepare the data update message on the stick session <ts>, <st> is the considered
629 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800630 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200631 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
632 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200633 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100634static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200635{
636 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200637 unsigned short datalen;
638 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200639 unsigned int data_type;
640 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100641 struct stksess *ts;
642 struct shared_table *st;
643 unsigned int updateid;
644 int use_identifier;
645 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200646 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100647
648 ts = p->updt.stksess;
649 st = p->updt.shared_table;
650 updateid = p->updt.updateid;
651 use_identifier = p->updt.use_identifier;
652 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200653 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200654
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200655 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200656
Emeric Brun2b920a12010-09-23 18:30:22 +0200657 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200658
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500659 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200660 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200661 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200662 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200663
664 /* encode update identifier if needed */
665 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200666 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200667 memcpy(cursor, &netinteger, sizeof(netinteger));
668 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200669 }
670
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200671 if (use_timed) {
672 netinteger = htonl(tick_remain(now_ms, ts->expire));
673 memcpy(cursor, &netinteger, sizeof(netinteger));
674 cursor += sizeof(netinteger);
675 }
676
Emeric Brunb3971ab2015-05-12 18:49:09 +0200677 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200678 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200679 int stlen = strlen((char *)ts->key.key);
680
Emeric Brunb3971ab2015-05-12 18:49:09 +0200681 intencode(stlen, &cursor);
682 memcpy(cursor, ts->key.key, stlen);
683 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200684 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200685 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100686 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200687 memcpy(cursor, &netinteger, sizeof(netinteger));
688 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200689 }
690 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200691 memcpy(cursor, ts->key.key, st->table->key_size);
692 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200693 }
694
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100695 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200696 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200697 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200698
Emeric Brun94900952015-06-11 18:25:54 +0200699 data_ptr = stktable_data_ptr(st->table, ts, data_type);
700 if (data_ptr) {
701 switch (stktable_data_types[data_type].std_type) {
702 case STD_T_SINT: {
703 int data;
704
705 data = stktable_data_cast(data_ptr, std_t_sint);
706 intencode(data, &cursor);
707 break;
708 }
709 case STD_T_UINT: {
710 unsigned int data;
711
712 data = stktable_data_cast(data_ptr, std_t_uint);
713 intencode(data, &cursor);
714 break;
715 }
716 case STD_T_ULL: {
717 unsigned long long data;
718
719 data = stktable_data_cast(data_ptr, std_t_ull);
720 intencode(data, &cursor);
721 break;
722 }
723 case STD_T_FRQP: {
724 struct freq_ctr_period *frqp;
725
726 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
727 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
728 intencode(frqp->curr_ctr, &cursor);
729 intencode(frqp->prev_ctr, &cursor);
730 break;
731 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200732 case STD_T_DICT: {
733 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200734 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200735 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200736 char *beg, *end;
737 size_t value_len, data_len;
738 struct dcache *dc;
739
740 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100741 if (!de) {
742 /* No entry */
743 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200744 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100745 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200746
747 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200748 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200749 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200750 if (cached_de == &cde.entry) {
751 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
752 break;
753 /* Encode the length of the remaining data -> 1 */
754 intencode(1, &cursor);
755 /* Encode the cache entry ID */
756 intencode(cde.id + 1, &cursor);
757 }
758 else {
759 /* Leave enough room to encode the remaining data length. */
760 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
761 /* Encode the dictionary entry key */
762 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200763 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200764 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200765 intencode(value_len, &end);
766 /* Copy the data */
767 memcpy(end, de->value.key, value_len);
768 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200769 /* Encode the length of the data */
770 data_len = end - beg;
771 intencode(data_len, &cursor);
772 memmove(cursor, beg, data_len);
773 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200774 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200775 break;
776 }
Emeric Brun94900952015-06-11 18:25:54 +0200777 }
778 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200779 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100780 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200781
782 /* Compute datalen */
783 datalen = (cursor - datamsg);
784
785 /* prepare message header */
786 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200787 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200788 cursor = &msg[2];
789 intencode(datalen, &cursor);
790
791 /* move data after header */
792 memmove(cursor, datamsg, datalen);
793
794 /* return header size + data_len */
795 return (cursor - msg) + datalen;
796}
797
798/*
799 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800800 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200801 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
802 * check size)
803 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100804static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200805{
806 int len;
807 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200808 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200809 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200810 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200811 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100812 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200813
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100814 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200815 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200816
817 /* Encode data */
818
819 /* encode local id */
820 intencode(st->local_id, &cursor);
821
822 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100823 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200824 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100825 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200826 cursor += len;
827
828 /* encode table type */
829
Emeric Brun530ba382020-06-02 11:17:42 +0200830 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200831
832 /* encode table key size */
833 intencode(st->table->key_size, &cursor);
834
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200835 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200836 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200837 /* encode available known data types in table */
838 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
839 if (st->table->data_ofs[data_type]) {
840 switch (stktable_data_types[data_type].std_type) {
841 case STD_T_SINT:
842 case STD_T_UINT:
843 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200844 case STD_T_DICT:
Emeric Bruncecd1d32021-07-01 18:54:05 +0200845 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200846 break;
Emeric Brun94900952015-06-11 18:25:54 +0200847 case STD_T_FRQP:
Emeric Bruncecd1d32021-07-01 18:54:05 +0200848 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200849 intencode(data_type, &chunkq);
850 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200851 break;
852 }
853 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200854 }
855 intencode(data, &cursor);
856
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200857 /* Encode stick-table entries duration. */
858 intencode(st->table->expire, &cursor);
859
860 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200861 chunk->data = chunkq - chunkp;
862 memcpy(cursor, chunk->area, chunk->data);
863 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200864 }
865
Emeric Brunb3971ab2015-05-12 18:49:09 +0200866 /* Compute datalen */
867 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200868
Emeric Brunb3971ab2015-05-12 18:49:09 +0200869 /* prepare message header */
870 msg[0] = PEER_MSG_CLASS_STICKTABLE;
871 msg[1] = PEER_MSG_STKT_DEFINE;
872 cursor = &msg[2];
873 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200874
Emeric Brunb3971ab2015-05-12 18:49:09 +0200875 /* move data after header */
876 memmove(cursor, datamsg, datalen);
877
878 /* return header size + data_len */
879 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200880}
881
Emeric Brunb3971ab2015-05-12 18:49:09 +0200882/*
883 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
884 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800885 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
887 * check size)
888 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100889static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890{
891 unsigned short datalen;
892 char *cursor, *datamsg;
893 uint32_t netinteger;
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écaille39143342019-05-24 14:32:27 +0200896 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200897
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100898 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200899 intencode(st->remote_id, &cursor);
900 netinteger = htonl(st->last_get);
901 memcpy(cursor, &netinteger, sizeof(netinteger));
902 cursor += sizeof(netinteger);
903
904 /* Compute datalen */
905 datalen = (cursor - datamsg);
906
907 /* prepare message header */
908 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200909 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200910 cursor = &msg[2];
911 intencode(datalen, &cursor);
912
913 /* move data after header */
914 memmove(cursor, datamsg, datalen);
915
916 /* return header size + data_len */
917 return (cursor - msg) + datalen;
918}
Emeric Brun2b920a12010-09-23 18:30:22 +0200919
920/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200921 * Function to deinit connected peer
922 */
923void __peer_session_deinit(struct peer *peer)
924{
925 struct stream_interface *si;
926 struct stream *s;
927 struct peers *peers;
928
929 if (!peer->appctx)
930 return;
931
932 si = peer->appctx->owner;
933 if (!si)
934 return;
935
936 s = si_strm(si);
937 if (!s)
938 return;
939
940 peers = strm_fe(s)->parent;
941 if (!peers)
942 return;
943
944 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
945 HA_ATOMIC_SUB(&connected_peers, 1);
946
947 HA_ATOMIC_SUB(&active_peers, 1);
948
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200949 flush_dcache(peer);
950
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200951 /* Re-init current table pointers to force announcement on re-connect */
952 peer->remote_table = peer->last_local_table = NULL;
953 peer->appctx = NULL;
954 if (peer->flags & PEER_F_LEARN_ASSIGN) {
955 /* unassign current peer for learning */
956 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
957 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
958
Emeric Brune1f55742021-04-28 12:59:35 +0200959 if (peer->local)
960 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
961 else
962 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200963 /* reschedule a resync */
964 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
965 }
966 /* reset teaching and learning flags to 0 */
967 peer->flags &= PEER_TEACH_RESET;
968 peer->flags &= PEER_LEARN_RESET;
969 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
970}
971
972/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200973 * Callback to release a session with a peer
974 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200975static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200976{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200977 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200978
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +0100979 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100980 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100981 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200982 return;
983
984 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200985 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100986 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200987 if (peer->appctx == appctx)
988 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +0200989 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100990 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200991 }
992}
993
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200994/* Retrieve the major and minor versions of peers protocol
995 * announced by a remote peer. <str> is a null-terminated
996 * string with the following format: "<maj_ver>.<min_ver>".
997 */
998static int peer_get_version(const char *str,
999 unsigned int *maj_ver, unsigned int *min_ver)
1000{
1001 unsigned int majv, minv;
1002 const char *pos, *saved;
1003 const char *end;
1004
1005 saved = pos = str;
1006 end = str + strlen(str);
1007
1008 majv = read_uint(&pos, end);
1009 if (saved == pos || *pos++ != '.')
1010 return -1;
1011
1012 saved = pos;
1013 minv = read_uint(&pos, end);
1014 if (saved == pos || pos != end)
1015 return -1;
1016
1017 *maj_ver = majv;
1018 *min_ver = minv;
1019
1020 return 0;
1021}
Emeric Brun2b920a12010-09-23 18:30:22 +02001022
1023/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001024 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1025 * '\n' character.
1026 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1027 * a line could not be read because the communication channel is closed.
1028 */
1029static inline int peer_getline(struct appctx *appctx)
1030{
1031 int n;
1032 struct stream_interface *si = appctx->owner;
1033
1034 n = co_getline(si_oc(si), trash.area, trash.size);
1035 if (!n)
1036 return 0;
1037
1038 if (n < 0 || trash.area[n - 1] != '\n') {
1039 appctx->st0 = PEER_SESS_ST_END;
1040 return -1;
1041 }
1042
1043 if (n > 1 && (trash.area[n - 2] == '\r'))
1044 trash.area[n - 2] = 0;
1045 else
1046 trash.area[n - 1] = 0;
1047
1048 co_skip(si_oc(si), n);
1049
1050 return n;
1051}
1052
1053/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001054 * Send a message after having called <peer_prepare_msg> to build it.
1055 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1056 * Returns -1 if there was not enough room left to send the message,
1057 * any other negative returned value must be considered as an error with an appcxt st0
1058 * returned value equal to PEER_SESS_ST_END.
1059 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001060static inline int peer_send_msg(struct appctx *appctx,
1061 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1062 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001063{
1064 int ret, msglen;
1065 struct stream_interface *si = appctx->owner;
1066
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001067 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001068 if (!msglen) {
1069 /* internal error: message does not fit in trash */
1070 appctx->st0 = PEER_SESS_ST_END;
1071 return 0;
1072 }
1073
1074 /* message to buffer */
1075 ret = ci_putblk(si_ic(si), trash.area, msglen);
1076 if (ret <= 0) {
1077 if (ret == -1) {
1078 /* No more write possible */
1079 si_rx_room_blk(si);
1080 return -1;
1081 }
1082 appctx->st0 = PEER_SESS_ST_END;
1083 }
1084
1085 return ret;
1086}
1087
1088/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001089 * Send a hello message.
1090 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1091 * Returns -1 if there was not enough room left to send the message,
1092 * any other negative returned value must be considered as an error with an appcxt st0
1093 * returned value equal to PEER_SESS_ST_END.
1094 */
1095static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1096{
1097 struct peer_prep_params p = {
1098 .hello.peer = peer,
1099 };
1100
1101 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1102}
1103
1104/*
1105 * Send a success peer handshake status message.
1106 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1107 * Returns -1 if there was not enough room left to send the message,
1108 * any other negative returned value must be considered as an error with an appcxt st0
1109 * returned value equal to PEER_SESS_ST_END.
1110 */
1111static inline int peer_send_status_successmsg(struct appctx *appctx)
1112{
1113 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1114}
1115
1116/*
1117 * Send a peer handshake status error message.
1118 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1119 * Returns -1 if there was not enough room left to send the message,
1120 * any other negative returned value must be considered as an error with an appcxt st0
1121 * returned value equal to PEER_SESS_ST_END.
1122 */
1123static inline int peer_send_status_errormsg(struct appctx *appctx)
1124{
1125 struct peer_prep_params p = {
1126 .error_status.st1 = appctx->st1,
1127 };
1128
1129 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1130}
1131
1132/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001133 * Send a stick-table switch message.
1134 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1135 * Returns -1 if there was not enough room left to send the message,
1136 * any other negative returned value must be considered as an error with an appcxt st0
1137 * returned value equal to PEER_SESS_ST_END.
1138 */
1139static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1140{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001141 struct peer_prep_params p = {
1142 .swtch.shared_table = st,
1143 };
1144
1145 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001146}
1147
1148/*
1149 * Send a stick-table update acknowledgement message.
1150 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1151 * Returns -1 if there was not enough room left to send the message,
1152 * any other negative returned value must be considered as an error with an appcxt st0
1153 * returned value equal to PEER_SESS_ST_END.
1154 */
1155static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1156{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001157 struct peer_prep_params p = {
1158 .ack.shared_table = st,
1159 };
1160
1161 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001162}
1163
1164/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001165 * Send a stick-table update message.
1166 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1167 * Returns -1 if there was not enough room left to send the message,
1168 * any other negative returned value must be considered as an error with an appcxt st0
1169 * returned value equal to PEER_SESS_ST_END.
1170 */
1171static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1172 unsigned int updateid, int use_identifier, int use_timed)
1173{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001174 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001175 .updt = {
1176 .stksess = ts,
1177 .shared_table = st,
1178 .updateid = updateid,
1179 .use_identifier = use_identifier,
1180 .use_timed = use_timed,
1181 .peer = appctx->ctx.peers.ptr,
1182 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001183 };
1184
1185 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001186}
1187
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001188/*
1189 * Build a peer protocol control class message.
1190 * Returns the number of written bytes used to build the message if succeeded,
1191 * 0 if not.
1192 */
1193static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1194{
1195 if (size < sizeof p->control.head)
1196 return 0;
1197
1198 msg[0] = p->control.head[0];
1199 msg[1] = p->control.head[1];
1200
1201 return 2;
1202}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001203
1204/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001205 * Send a stick-table synchronization request message.
1206 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1207 * Returns -1 if there was not enough room left to send the message,
1208 * any other negative returned value must be considered as an error with an appctx st0
1209 * returned value equal to PEER_SESS_ST_END.
1210 */
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001211static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1212 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001213{
1214 struct peer_prep_params p = {
1215 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1216 };
1217
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001218 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1219 NULL, &p.control.head[1], peers->local->id, peer->id);
1220
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001221 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1222}
1223
1224/*
1225 * Send a stick-table synchronization confirmation message.
1226 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1227 * Returns -1 if there was not enough room left to send the message,
1228 * any other negative returned value must be considered as an error with an appctx st0
1229 * returned value equal to PEER_SESS_ST_END.
1230 */
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001231static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1232 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001233{
1234 struct peer_prep_params p = {
1235 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1236 };
1237
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001238 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1239 NULL, &p.control.head[1], peers->local->id, peer->id);
1240
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001241 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1242}
1243
1244/*
1245 * Send a stick-table synchronization finished message.
1246 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1247 * Returns -1 if there was not enough room left to send the message,
1248 * any other negative returned value must be considered as an error with an appctx st0
1249 * returned value equal to PEER_SESS_ST_END.
1250 */
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001251static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1252 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001253{
1254 struct peer_prep_params p = {
1255 .control.head = { PEER_MSG_CLASS_CONTROL, },
1256 };
1257
Emeric Brun70de43b2020-03-16 10:51:01 +01001258 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001259 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1260
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001261 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1262 NULL, &p.control.head[1], peers->local->id, peer->id);
1263
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001264 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1265}
1266
1267/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001268 * Send a heartbeat message.
1269 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1270 * Returns -1 if there was not enough room left to send the message,
1271 * any other negative returned value must be considered as an error with an appctx st0
1272 * returned value equal to PEER_SESS_ST_END.
1273 */
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001274static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1275 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001276{
1277 struct peer_prep_params p = {
1278 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1279 };
1280
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001281 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1282 NULL, &p.control.head[1], peers->local->id, peer->id);
1283
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001284 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1285}
1286
1287/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001288 * Build a peer protocol error class message.
1289 * Returns the number of written bytes used to build the message if succeeded,
1290 * 0 if not.
1291 */
1292static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1293{
1294 if (size < sizeof p->error.head)
1295 return 0;
1296
1297 msg[0] = p->error.head[0];
1298 msg[1] = p->error.head[1];
1299
1300 return 2;
1301}
1302
1303/*
1304 * Send a "size limit reached" error message.
1305 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1306 * Returns -1 if there was not enough room left to send the message,
1307 * any other negative returned value must be considered as an error with an appctx st0
1308 * returned value equal to PEER_SESS_ST_END.
1309 */
1310static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1311{
1312 struct peer_prep_params p = {
1313 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1314 };
1315
1316 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1317}
1318
1319/*
1320 * Send a "peer protocol" error message.
1321 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1322 * Returns -1 if there was not enough room left to send the message,
1323 * any other negative returned value must be considered as an error with an appctx st0
1324 * returned value equal to PEER_SESS_ST_END.
1325 */
1326static inline int peer_send_error_protomsg(struct appctx *appctx)
1327{
1328 struct peer_prep_params p = {
1329 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1330 };
1331
1332 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1333}
1334
1335/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001336 * Function used to lookup for recent stick-table updates associated with
1337 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1338 */
1339static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1340{
1341 struct eb32_node *eb;
1342
1343 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1344 if (!eb) {
1345 eb = eb32_first(&st->table->updates);
Emeric Brund9e4ee62021-04-28 11:48:15 +02001346 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001347 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1348 return NULL;
1349 }
1350 }
1351
Emeric Brund9e4ee62021-04-28 11:48:15 +02001352 /* if distance between the last pushed and the retrieved key
1353 * is greater than the distance last_pushed and the local_update
1354 * this means we are beyond localupdate.
1355 */
1356 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001357 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1358 return NULL;
1359 }
1360
1361 return eb32_entry(eb, struct stksess, upd);
1362}
1363
1364/*
1365 * Function used to lookup for recent stick-table updates associated with
1366 * <st> shared stick-table during teach state 1 step.
1367 */
1368static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1369{
1370 struct eb32_node *eb;
1371
1372 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1373 if (!eb) {
1374 st->flags |= SHTABLE_F_TEACH_STAGE1;
1375 eb = eb32_first(&st->table->updates);
1376 if (eb)
1377 st->last_pushed = eb->key - 1;
1378 return NULL;
1379 }
1380
1381 return eb32_entry(eb, struct stksess, upd);
1382}
1383
1384/*
1385 * Function used to lookup for recent stick-table updates associated with
1386 * <st> shared stick-table during teach state 2 step.
1387 */
1388static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1389{
1390 struct eb32_node *eb;
1391
1392 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1393 if (!eb || eb->key > st->teaching_origin) {
1394 st->flags |= SHTABLE_F_TEACH_STAGE2;
1395 return NULL;
1396 }
1397
1398 return eb32_entry(eb, struct stksess, upd);
1399}
1400
1401/*
1402 * Generic function to emit update messages for <st> stick-table when a lesson must
1403 * be taught to the peer <p>.
1404 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1405 * this function, 0 if not.
1406 *
1407 * This function temporary unlock/lock <st> when it sends stick-table updates or
1408 * when decrementing its refcount in case of any error when it sends this updates.
1409 *
1410 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1411 * Returns -1 if there was not enough room left to send the message,
1412 * any other negative returned value must be considered as an error with an appcxt st0
1413 * returned value equal to PEER_SESS_ST_END.
1414 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1415 * unlocked if not already locked when entering this function.
1416 */
1417static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1418 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1419 struct shared_table *st, int locked)
1420{
1421 int ret, new_pushed, use_timed;
1422
1423 ret = 1;
1424 use_timed = 0;
1425 if (st != p->last_local_table) {
1426 ret = peer_send_switchmsg(st, appctx);
1427 if (ret <= 0)
1428 return ret;
1429
1430 p->last_local_table = st;
1431 }
1432
1433 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1434 use_timed = !(p->flags & PEER_F_DWNGRD);
1435
1436 /* We force new pushed to 1 to force identifier in update message */
1437 new_pushed = 1;
1438
1439 if (!locked)
1440 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1441
1442 while (1) {
1443 struct stksess *ts;
1444 unsigned updateid;
1445
1446 /* push local updates */
1447 ts = peer_stksess_lookup(st);
1448 if (!ts)
1449 break;
1450
1451 updateid = ts->upd.key;
1452 ts->ref_cnt++;
1453 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1454
1455 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1456 if (ret <= 0) {
1457 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1458 ts->ref_cnt--;
1459 if (!locked)
1460 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1461 return ret;
1462 }
1463
1464 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1465 ts->ref_cnt--;
1466 st->last_pushed = updateid;
1467
1468 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1469 (int)(st->last_pushed - st->table->commitupdate) > 0)
1470 st->table->commitupdate = st->last_pushed;
1471
1472 /* identifier may not needed in next update message */
1473 new_pushed = 0;
1474 }
1475
1476 out:
1477 if (!locked)
1478 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1479 return 1;
1480}
1481
1482/*
1483 * Function to emit update messages for <st> stick-table when a lesson must
1484 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1485 *
1486 * Note that <st> shared stick-table is locked when calling this function.
1487 *
1488 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1489 * Returns -1 if there was not enough room left to send the message,
1490 * any other negative returned value must be considered as an error with an appcxt st0
1491 * returned value equal to PEER_SESS_ST_END.
1492 */
1493static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1494 struct shared_table *st)
1495{
1496 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1497}
1498
1499/*
1500 * Function to emit update messages for <st> stick-table when a lesson must
1501 * be taught to the peer <p> during teach state 1 step.
1502 *
1503 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1504 * Returns -1 if there was not enough room left to send the message,
1505 * any other negative returned value must be considered as an error with an appcxt st0
1506 * returned value equal to PEER_SESS_ST_END.
1507 */
1508static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1509 struct shared_table *st)
1510{
1511 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1512}
1513
1514/*
1515 * Function to emit update messages for <st> stick-table when a lesson must
1516 * be taught to the peer <p> during teach state 1 step.
1517 *
1518 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1519 * Returns -1 if there was not enough room left to send the message,
1520 * any other negative returned value must be considered as an error with an appcxt st0
1521 * returned value equal to PEER_SESS_ST_END.
1522 */
1523static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1524 struct shared_table *st)
1525{
1526 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1527}
1528
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001529
1530/*
1531 * Function used to parse a stick-table update message after it has been received
1532 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1533 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1534 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1535 * was encountered.
1536 * <exp> must be set if the stick-table entry expires.
1537 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001538 * 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 +01001539 * update ID.
1540 * <totl> is the length of the stick-table update message computed upon receipt.
1541 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001542static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1543 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001544{
1545 struct stream_interface *si = appctx->owner;
1546 struct shared_table *st = p->remote_table;
1547 struct stksess *ts, *newts;
1548 uint32_t update;
1549 int expire;
1550 unsigned int data_type;
1551 void *data_ptr;
1552
Frédéric Lécaille94571512020-11-10 16:18:03 +01001553 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001554 /* Here we have data message */
1555 if (!st)
1556 goto ignore_msg;
1557
1558 expire = MS_TO_TICKS(st->table->expire);
1559
1560 if (updt) {
Frédéric Lécaille94571512020-11-10 16:18:03 +01001561 if (msg_len < sizeof(update)) {
1562 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001563 goto malformed_exit;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001564 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001565
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001566 memcpy(&update, *msg_cur, sizeof(update));
1567 *msg_cur += sizeof(update);
1568 st->last_get = htonl(update);
1569 }
1570 else {
1571 st->last_get++;
1572 }
1573
1574 if (exp) {
1575 size_t expire_sz = sizeof expire;
1576
Frédéric Lécaille94571512020-11-10 16:18:03 +01001577 if (*msg_cur + expire_sz > msg_end) {
1578 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1579 NULL, p, *msg_cur);
1580 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1581 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001582 goto malformed_exit;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001583 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001584
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001585 memcpy(&expire, *msg_cur, expire_sz);
1586 *msg_cur += expire_sz;
1587 expire = ntohl(expire);
1588 }
1589
1590 newts = stksess_new(st->table, NULL);
1591 if (!newts)
1592 goto ignore_msg;
1593
1594 if (st->table->type == SMP_T_STR) {
1595 unsigned int to_read, to_store;
1596
1597 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001598 if (!*msg_cur) {
1599 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001600 goto malformed_free_newts;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001601 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001602
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001603 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001604 if (*msg_cur + to_store > msg_end) {
1605 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1606 NULL, p, *msg_cur);
1607 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1608 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001609 goto malformed_free_newts;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001610 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001611
1612 memcpy(newts->key.key, *msg_cur, to_store);
1613 newts->key.key[to_store] = 0;
1614 *msg_cur += to_read;
1615 }
1616 else if (st->table->type == SMP_T_SINT) {
1617 unsigned int netinteger;
1618
Frédéric Lécaille94571512020-11-10 16:18:03 +01001619 if (*msg_cur + sizeof(netinteger) > msg_end) {
1620 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1621 NULL, p, *msg_cur);
1622 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1623 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001624 goto malformed_free_newts;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001625 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001626
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001627 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1628 netinteger = ntohl(netinteger);
1629 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1630 *msg_cur += sizeof(netinteger);
1631 }
1632 else {
Frédéric Lécaille94571512020-11-10 16:18:03 +01001633 if (*msg_cur + st->table->key_size > msg_end) {
1634 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1635 NULL, p, *msg_cur);
1636 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1637 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001638 goto malformed_free_newts;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001639 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001640
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001641 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1642 *msg_cur += st->table->key_size;
1643 }
1644
1645 /* lookup for existing entry */
1646 ts = stktable_set_entry(st->table, newts);
1647 if (ts != newts) {
1648 stksess_free(st->table, newts);
1649 newts = NULL;
1650 }
1651
1652 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1653
1654 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001655 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001656
Emeric Bruncecd1d32021-07-01 18:54:05 +02001657 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001658 continue;
1659
Willy Tarreau1e82a142019-01-29 11:08:06 +01001660 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001661 if (!*msg_cur) {
1662 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001663 goto malformed_unlock;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001664 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001665
Willy Tarreau1e82a142019-01-29 11:08:06 +01001666 switch (stktable_data_types[data_type].std_type) {
1667 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001668 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1669 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001670 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001671 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001672
Willy Tarreau1e82a142019-01-29 11:08:06 +01001673 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001674 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1675 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001676 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001677 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001678
Willy Tarreau1e82a142019-01-29 11:08:06 +01001679 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001680 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1681 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001682 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001683 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001684
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001685 case STD_T_FRQP: {
1686 struct freq_ctr_period data;
1687
1688 /* First bit is reserved for the freq_ctr_period lock
1689 Note: here we're still protected by the stksess lock
1690 so we don't need to update the update the freq_ctr_period
1691 using its internal lock */
1692
Willy Tarreau1e82a142019-01-29 11:08:06 +01001693 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001694 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001695 if (!*msg_cur) {
1696 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001697 goto malformed_unlock;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001698 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001699
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001700 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001701 if (!*msg_cur) {
1702 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001703 goto malformed_unlock;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001704 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001705
1706 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1707 if (data_ptr)
1708 stktable_data_cast(data_ptr, std_t_frqp) = data;
1709 break;
1710 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001711 case STD_T_DICT: {
1712 struct buffer *chunk;
1713 size_t data_len, value_len;
1714 unsigned int id;
1715 struct dict_entry *de;
1716 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001717 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001718
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001719 if (!decoded_int) {
1720 /* No entry. */
1721 break;
1722 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001723 data_len = decoded_int;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001724 if (*msg_cur + data_len > msg_end) {
1725 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1726 NULL, p, *msg_cur);
1727 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1728 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001729 goto malformed_unlock;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001730 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001731
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001732 /* Compute the end of the current data, <msg_end> being at the end of
1733 * the entire message.
1734 */
1735 end = *msg_cur + data_len;
1736 id = intdecode(msg_cur, end);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001737 if (!*msg_cur || !id) {
1738 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1739 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001740 goto malformed_unlock;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001741 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001742
1743 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001744 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001745 /* Dictionary entry key without value. */
Frédéric Lécaillecd24d5c2020-11-12 19:53:11 +01001746 if (id > dc->max_entries) {
Frédéric Lécaille94571512020-11-10 16:18:03 +01001747 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1748 NULL, p, NULL, &id);
Frédéric Lécaillecd24d5c2020-11-12 19:53:11 +01001749 goto malformed_unlock;
1750 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001751 /* IDs sent over the network are numbered from 1. */
1752 de = dc->rx[id - 1].de;
1753 }
1754 else {
1755 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001756 value_len = intdecode(msg_cur, end);
1757 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécaille94571512020-11-10 16:18:03 +01001758 unlikely(value_len + 1 >= chunk->size)) {
1759 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1760 NULL, p, *msg_cur, &value_len);
1761 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1762 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001763 goto malformed_unlock;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001764 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001765
1766 chunk_memcpy(chunk, *msg_cur, value_len);
1767 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001768 *msg_cur += value_len;
1769
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001770 de = dict_insert(&server_name_dict, chunk->area);
1771 dc->rx[id - 1].de = de;
1772 }
1773 if (de) {
1774 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1775 if (data_ptr)
1776 stktable_data_cast(data_ptr, std_t_dict) = de;
1777 }
1778 break;
1779 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001780 }
1781 }
1782 /* Force new expiration */
1783 ts->expire = tick_add(now_ms, expire);
1784
1785 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1786 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001787 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001788 return 1;
1789
1790 ignore_msg:
1791 /* skip consumed message */
1792 co_skip(si_oc(si), totl);
Frédéric Lécaille94571512020-11-10 16:18:03 +01001793 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001794 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001795
1796 malformed_unlock:
1797 /* malformed message */
1798 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1799 stktable_touch_remote(st->table, ts, 1);
1800 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001801 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001802 return 0;
1803
1804 malformed_free_newts:
1805 /* malformed message */
1806 stksess_free(st->table, newts);
1807 malformed_exit:
1808 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécaille94571512020-11-10 16:18:03 +01001809 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001810 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001811}
1812
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001813/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001814 * Function used to parse a stick-table update acknowledgement message after it
1815 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1816 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1817 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1818 * was encountered.
1819 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1820 */
1821static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1822 char **msg_cur, char *msg_end)
1823{
1824 /* ack message */
1825 uint32_t table_id ;
1826 uint32_t update;
1827 struct shared_table *st;
1828
Emeric Brun39555d62021-03-04 10:27:10 +01001829 /* ignore ack during teaching process */
1830 if (p->flags & PEER_F_TEACH_PROCESS)
1831 return 1;
1832
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001833 table_id = intdecode(msg_cur, msg_end);
1834 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1835 /* malformed message */
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001836
1837 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
1838 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001839 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1840 return 0;
1841 }
1842
1843 memcpy(&update, *msg_cur, sizeof(update));
1844 update = ntohl(update);
1845
1846 for (st = p->tables; st; st = st->next) {
1847 if (st->local_id == table_id) {
1848 st->update = update;
1849 break;
1850 }
1851 }
1852
1853 return 1;
1854}
1855
1856/*
1857 * Function used to parse a stick-table switch message after it has been received
1858 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1859 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1860 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1861 * was encountered.
1862 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1863 */
1864static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1865 char **msg_cur, char *msg_end)
1866{
1867 struct shared_table *st;
1868 int table_id;
1869
1870 table_id = intdecode(msg_cur, msg_end);
1871 if (!*msg_cur) {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001872 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001873 /* malformed message */
1874 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1875 return 0;
1876 }
1877
1878 p->remote_table = NULL;
1879 for (st = p->tables; st; st = st->next) {
1880 if (st->remote_id == table_id) {
1881 p->remote_table = st;
1882 break;
1883 }
1884 }
1885
1886 return 1;
1887}
1888
1889/*
1890 * Function used to parse a stick-table definition message after it has been received
1891 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1892 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1893 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1894 * was encountered.
1895 * <totl> is the length of the stick-table update message computed upon receipt.
1896 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1897 */
1898static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1899 char **msg_cur, char *msg_end, int totl)
1900{
1901 struct stream_interface *si = appctx->owner;
1902 int table_id_len;
1903 struct shared_table *st;
1904 int table_type;
1905 int table_keylen;
1906 int table_id;
1907 uint64_t table_data;
1908
1909 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001910 if (!*msg_cur) {
1911 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001912 goto malformed_exit;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001913 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001914
1915 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001916 if (!*msg_cur) {
1917 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001918 goto malformed_exit;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001919 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001920
1921 p->remote_table = NULL;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001922 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1923 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001924 goto malformed_exit;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001925 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001926
1927 for (st = p->tables; st; st = st->next) {
1928 /* Reset IDs */
1929 if (st->remote_id == table_id)
1930 st->remote_id = 0;
1931
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001932 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1933 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001934 p->remote_table = st;
1935 }
1936
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001937 if (!p->remote_table) {
1938 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001939 goto ignore_msg;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001940 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001941
1942 *msg_cur += table_id_len;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001943 if (*msg_cur >= msg_end) {
1944 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001945 goto malformed_exit;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001946 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001947
1948 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001949 if (!*msg_cur) {
1950 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001951 goto malformed_exit;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001952 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001953
1954 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001955 if (!*msg_cur) {
1956 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001957 goto malformed_exit;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001958 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001959
1960 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001961 if (!*msg_cur) {
1962 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001963 goto malformed_exit;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001964 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001965
Emeric Brun530ba382020-06-02 11:17:42 +02001966 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001967 || p->remote_table->table->key_size != table_keylen) {
1968 p->remote_table = NULL;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01001969 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001970 goto ignore_msg;
1971 }
1972
1973 p->remote_table->remote_data = table_data;
1974 p->remote_table->remote_id = table_id;
1975 return 1;
1976
1977 ignore_msg:
1978 co_skip(si_oc(si), totl);
1979 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001980
1981 malformed_exit:
1982 /* malformed message */
1983 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1984 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001985}
1986
1987/*
Willy Tarreaud4913912020-11-13 14:10:20 +01001988 * Receive a stick-table message or pre-parse any other message.
1989 * The message's header will be sent into <msg_head> which must be at least
1990 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
1991 * The first two bytes are always read, and the rest is only read if the
1992 * first bytes indicate a stick-table message. If the message is a stick-table
1993 * message, the varint is decoded and the equivalent number of bytes will be
1994 * copied into the trash at trash.area. <totl> is incremented by the number of
1995 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001996 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1997 * -1 if there was an error updating the appctx state st0 accordingly.
1998 */
1999static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2000 uint32_t *msg_len, int *totl)
2001{
2002 int reql;
2003 struct stream_interface *si = appctx->owner;
Willy Tarreaud4913912020-11-13 14:10:20 +01002004 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002005
2006 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
2007 if (reql <= 0) /* closed or EOL not found */
2008 goto incomplete;
2009
2010 *totl += reql;
2011
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002012 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002013 return 1;
2014
Willy Tarreaud4913912020-11-13 14:10:20 +01002015 /* This is a stick-table message, let's go on */
2016
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002017 /* Read and Decode message length */
Willy Tarreaud4913912020-11-13 14:10:20 +01002018 msg_head += *totl;
2019 msg_head_sz -= *totl;
2020 reql = co_data(si_oc(si)) - *totl;
2021 if (reql > msg_head_sz)
2022 reql = msg_head_sz;
2023
2024 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002025 if (reql <= 0) /* closed */
2026 goto incomplete;
2027
Willy Tarreaud4913912020-11-13 14:10:20 +01002028 cur = msg_head;
2029 *msg_len = intdecode(&cur, cur + reql);
2030 if (!cur) {
2031 /* the number is truncated, did we read enough ? */
2032 if (reql < msg_head_sz)
2033 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002034
Willy Tarreaud4913912020-11-13 14:10:20 +01002035 /* malformed message */
Frédéric Lécaille94571512020-11-10 16:18:03 +01002036 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
Willy Tarreaud4913912020-11-13 14:10:20 +01002037 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2038 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002039 }
Willy Tarreaud4913912020-11-13 14:10:20 +01002040 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002041
2042 /* Read message content */
2043 if (*msg_len) {
2044 if (*msg_len > trash.size) {
2045 /* Status code is not success, abort */
2046 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2047 return -1;
2048 }
2049
2050 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
2051 if (reql <= 0) /* closed */
2052 goto incomplete;
2053 *totl += reql;
2054 }
2055
2056 return 1;
2057
2058 incomplete:
Willy Tarreau46af74a2020-11-26 17:06:04 +01002059 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
2060 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002061 appctx->st0 = PEER_SESS_ST_END;
2062 return -1;
2063 }
2064
2065 return 0;
2066}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002067
2068/*
2069 * Treat the awaited message with <msg_head> as header.*
2070 * Return 1 if succeeded, 0 if not.
2071 */
2072static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2073 char **msg_cur, char *msg_end, int msg_len, int totl)
2074{
2075 struct stream_interface *si = appctx->owner;
2076 struct stream *s = si_strm(si);
2077 struct peers *peers = strm_fe(s)->parent;
2078
2079 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2080 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2081 struct shared_table *st;
2082 /* Reset message: remote need resync */
2083
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002084 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2085 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002086 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002087 for (st = peer->tables; st; st = st->next) {
Emeric Bruneb2f8e02021-04-28 09:49:33 +02002088 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002089 st->flags = 0;
2090 }
2091
2092 /* reset teaching flags to 0 */
2093 peer->flags &= PEER_TEACH_RESET;
2094
2095 /* flag to start to teach lesson */
2096 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brune1f55742021-04-28 12:59:35 +02002097 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002098 }
2099 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002100 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2101 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002102 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2103 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2104 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2105 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brune1f55742021-04-28 12:59:35 +02002106 if (peer->local)
2107 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2108 else
2109 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002110 }
2111 peer->confirm++;
2112 }
2113 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002114 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2115 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002116 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2117 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2118 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2119
Emeric Brune1f55742021-04-28 12:59:35 +02002120 if (peer->local)
2121 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2122 else
2123 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002124 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002125 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002126 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2127 }
2128 peer->confirm++;
2129 }
2130 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2131 struct shared_table *st;
2132
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002133 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2134 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002135 /* If stopping state */
2136 if (stopping) {
2137 /* Close session, push resync no more needed */
2138 peer->flags |= PEER_F_TEACH_COMPLETE;
2139 appctx->st0 = PEER_SESS_ST_END;
2140 return 0;
2141 }
2142 for (st = peer->tables; st; st = st->next) {
2143 st->update = st->last_pushed = st->teaching_origin;
2144 st->flags = 0;
2145 }
2146
2147 /* reset teaching flags to 0 */
2148 peer->flags &= PEER_TEACH_RESET;
2149 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002150 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002151 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2152 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002153 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002154 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002155 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002156 }
2157 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2158 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2159 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2160 return 0;
2161 }
2162 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2163 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2164 return 0;
2165 }
2166 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2167 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2168 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2169 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2170 int update, expire;
2171
2172 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2173 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2174 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2175 msg_cur, msg_end, msg_len, totl))
2176 return 0;
2177
2178 }
2179 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2180 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2181 return 0;
2182 }
2183 }
2184 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2185 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2186 return 0;
2187 }
2188
2189 return 1;
2190}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002191
2192
2193/*
2194 * Send any message to <peer> peer.
2195 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002196 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002197 * to a peer state change (from the peer I/O handler point of view).
2198 */
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002199static inline int peer_send_msgs(struct appctx *appctx,
2200 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002201{
2202 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002203
2204 /* Need to request a resync */
2205 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2206 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2207 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2208
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002209 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002210 if (repl <= 0)
2211 return repl;
2212
2213 peers->flags |= PEERS_F_RESYNC_PROCESS;
2214 }
2215
2216 /* Nothing to read, now we start to write */
2217 if (peer->tables) {
2218 struct shared_table *st;
2219 struct shared_table *last_local_table;
2220
2221 last_local_table = peer->last_local_table;
2222 if (!last_local_table)
2223 last_local_table = peer->tables;
2224 st = last_local_table->next;
2225
2226 while (1) {
2227 if (!st)
2228 st = peer->tables;
2229
2230 /* It remains some updates to ack */
2231 if (st->last_get != st->last_acked) {
2232 repl = peer_send_ackmsg(st, appctx);
2233 if (repl <= 0)
2234 return repl;
2235
2236 st->last_acked = st->last_get;
2237 }
2238
2239 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2240 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2241 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brund9e4ee62021-04-28 11:48:15 +02002242 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002243
2244 repl = peer_send_teach_process_msgs(appctx, peer, st);
2245 if (repl <= 0) {
2246 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2247 return repl;
2248 }
2249 }
2250 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2251 }
Emeric Brun6ffb9bc2021-04-22 18:13:13 +02002252 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002253 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2254 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2255 if (repl <= 0)
2256 return repl;
2257 }
2258
2259 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2260 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2261 if (repl <= 0)
2262 return repl;
2263 }
2264 }
2265
2266 if (st == last_local_table)
2267 break;
2268 st = st->next;
2269 }
2270 }
2271
2272 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002273 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002274 if (repl <= 0)
2275 return repl;
2276
2277 /* flag finished message sent */
2278 peer->flags |= PEER_F_TEACH_FINISHED;
2279 }
2280
2281 /* Confirm finished or partial messages */
2282 while (peer->confirm) {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002283 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002284 if (repl <= 0)
2285 return repl;
2286
2287 peer->confirm--;
2288 }
2289
2290 return 1;
2291}
2292
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002293/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002294 * Read and parse a first line of a "hello" peer protocol message.
2295 * Returns 0 if could not read a line, -1 if there was a read error or
2296 * the line is malformed, 1 if succeeded.
2297 */
2298static inline int peer_getline_version(struct appctx *appctx,
2299 unsigned int *maj_ver, unsigned int *min_ver)
2300{
2301 int reql;
2302
2303 reql = peer_getline(appctx);
2304 if (!reql)
2305 return 0;
2306
2307 if (reql < 0)
2308 return -1;
2309
2310 /* test protocol */
2311 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2312 appctx->st0 = PEER_SESS_ST_EXIT;
2313 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2314 return -1;
2315 }
2316 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2317 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2318 appctx->st0 = PEER_SESS_ST_EXIT;
2319 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2320 return -1;
2321 }
2322
2323 return 1;
2324}
2325
2326/*
2327 * Read and parse a second line of a "hello" peer protocol message.
2328 * Returns 0 if could not read a line, -1 if there was a read error or
2329 * the line is malformed, 1 if succeeded.
2330 */
2331static inline int peer_getline_host(struct appctx *appctx)
2332{
2333 int reql;
2334
2335 reql = peer_getline(appctx);
2336 if (!reql)
2337 return 0;
2338
2339 if (reql < 0)
2340 return -1;
2341
2342 /* test hostname match */
2343 if (strcmp(localpeer, trash.area) != 0) {
2344 appctx->st0 = PEER_SESS_ST_EXIT;
2345 appctx->st1 = PEER_SESS_SC_ERRHOST;
2346 return -1;
2347 }
2348
2349 return 1;
2350}
2351
2352/*
2353 * Read and parse a last line of a "hello" peer protocol message.
2354 * Returns 0 if could not read a character, -1 if there was a read error or
2355 * the line is malformed, 1 if succeeded.
2356 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2357 */
2358static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2359{
2360 char *p;
2361 int reql;
2362 struct peer *peer;
2363 struct stream_interface *si = appctx->owner;
2364 struct stream *s = si_strm(si);
2365 struct peers *peers = strm_fe(s)->parent;
2366
2367 reql = peer_getline(appctx);
2368 if (!reql)
2369 return 0;
2370
2371 if (reql < 0)
2372 return -1;
2373
2374 /* parse line "<peer name> <pid> <relative_pid>" */
2375 p = strchr(trash.area, ' ');
2376 if (!p) {
2377 appctx->st0 = PEER_SESS_ST_EXIT;
2378 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2379 return -1;
2380 }
2381 *p = 0;
2382
2383 /* lookup known peer */
2384 for (peer = peers->remote; peer; peer = peer->next) {
2385 if (strcmp(peer->id, trash.area) == 0)
2386 break;
2387 }
2388
2389 /* if unknown peer */
2390 if (!peer) {
2391 appctx->st0 = PEER_SESS_ST_EXIT;
2392 appctx->st1 = PEER_SESS_SC_ERRPEER;
2393 return -1;
2394 }
2395 *curpeer = peer;
2396
2397 return 1;
2398}
2399
2400/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002401 * Init <peer> peer after having accepted it at peer protocol level.
2402 */
2403static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2404{
2405 struct shared_table *st;
2406
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002407 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002408 /* Register status code */
2409 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002410 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002411
2412 /* Awake main task */
2413 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2414
2415 /* Init confirm counter */
2416 peer->confirm = 0;
2417
2418 /* Init cursors */
2419 for (st = peer->tables; st ; st = st->next) {
2420 st->last_get = st->last_acked = 0;
Emeric Brun21680f62021-02-23 16:50:53 +01002421 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2422 /* if st->update appears to be in future it means
2423 * that the last acked value is very old and we
2424 * remain unconnected a too long time to use this
2425 * acknowlegement as a reset.
2426 * We should update the protocol to be able to
2427 * signal the remote peer that it needs a full resync.
2428 * Here a partial fix consist to set st->update at
2429 * the max past value
2430 */
2431 if ((int)(st->table->localupdate - st->update) < 0)
2432 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002433 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun5c880772021-04-20 14:43:46 +02002434 st->flags = 0;
Emeric Bruncd0a30e2021-02-23 17:08:08 +01002435 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2436 st->table->commitupdate = st->last_pushed;
Emeric Brun21680f62021-02-23 16:50:53 +01002437 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002438 }
2439
2440 /* reset teaching and learning flags to 0 */
2441 peer->flags &= PEER_TEACH_RESET;
2442 peer->flags &= PEER_LEARN_RESET;
2443
2444 /* if current peer is local */
2445 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002446 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002447 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2448 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2449 /* assign local peer for a lesson, consider lesson already requested */
2450 peer->flags |= PEER_F_LEARN_ASSIGN;
2451 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brune1f55742021-04-28 12:59:35 +02002452 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002453 }
2454
2455 }
2456 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2457 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2458 /* assign peer for a lesson */
2459 peer->flags |= PEER_F_LEARN_ASSIGN;
2460 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brune1f55742021-04-28 12:59:35 +02002461 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002462 }
2463}
2464
2465/*
2466 * Init <peer> peer after having connected it at peer protocol level.
2467 */
2468static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2469{
2470 struct shared_table *st;
2471
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002472 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002473 /* Init cursors */
2474 for (st = peer->tables; st ; st = st->next) {
2475 st->last_get = st->last_acked = 0;
Emeric Brun21680f62021-02-23 16:50:53 +01002476 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2477 /* if st->update appears to be in future it means
2478 * that the last acked value is very old and we
2479 * remain unconnected a too long time to use this
2480 * acknowlegement as a reset.
2481 * We should update the protocol to be able to
2482 * signal the remote peer that it needs a full resync.
2483 * Here a partial fix consist to set st->update at
2484 * the max past value.
2485 */
2486 if ((int)(st->table->localupdate - st->update) < 0)
2487 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002488 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun5c880772021-04-20 14:43:46 +02002489 st->flags = 0;
Emeric Bruncd0a30e2021-02-23 17:08:08 +01002490 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2491 st->table->commitupdate = st->last_pushed;
Emeric Brun21680f62021-02-23 16:50:53 +01002492 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002493 }
2494
2495 /* Init confirm counter */
2496 peer->confirm = 0;
2497
2498 /* reset teaching and learning flags to 0 */
2499 peer->flags &= PEER_TEACH_RESET;
2500 peer->flags &= PEER_LEARN_RESET;
2501
2502 /* If current peer is local */
2503 if (peer->local) {
2504 /* flag to start to teach lesson */
2505 peer->flags |= PEER_F_TEACH_PROCESS;
2506 }
2507 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2508 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2509 /* If peer is remote and resync from remote is needed,
2510 and no peer currently assigned */
2511
2512 /* assign peer for a lesson */
2513 peer->flags |= PEER_F_LEARN_ASSIGN;
2514 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brune1f55742021-04-28 12:59:35 +02002515 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002516 }
2517}
2518
2519/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002520 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002521 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002522static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002523{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002524 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002525 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002526 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002527 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002528 int reql = 0;
2529 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002530 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002531 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002532
Joseph Herlant82b2f542018-11-15 12:19:14 -08002533 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002534 if (si_ic(si)->buf.size == 0) {
2535 si_rx_room_blk(si);
2536 goto out;
2537 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002538
Emeric Brun2b920a12010-09-23 18:30:22 +02002539 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002540 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002541switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002542 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002543 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002544 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002545 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002546 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002547 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002548 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002549 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002550 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002551 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2552 if (reql <= 0) {
2553 if (!reql)
2554 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002555 goto switchstate;
2556 }
2557
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002558 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002559 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002560 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002561 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002562 reql = peer_getline_host(appctx);
2563 if (reql <= 0) {
2564 if (!reql)
2565 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002566 goto switchstate;
2567 }
2568
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002569 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002570 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002571 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002572 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002573 reql = peer_getline_last(appctx, &curpeer);
2574 if (reql <= 0) {
2575 if (!reql)
2576 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002577 goto switchstate;
2578 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002579
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002580 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002581 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002582 if (curpeer->local) {
2583 /* Local connection, reply a retry */
2584 appctx->st0 = PEER_SESS_ST_EXIT;
2585 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2586 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002587 }
Emeric Brun80527f52017-06-19 17:46:37 +02002588
2589 /* we're killing a connection, we must apply a random delay before
2590 * retrying otherwise the other end will do the same and we can loop
2591 * for a while.
2592 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002593 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002594 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002595 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002596 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002597 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002598 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2599 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2600 curpeer->flags |= PEER_F_DWNGRD;
2601 }
2602 else {
2603 curpeer->flags &= ~PEER_F_DWNGRD;
2604 }
2605 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002606 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002607 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002608 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002609 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002610 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002611 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002612 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002613 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002614 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002615 if (!curpeer) {
2616 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002617 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002618 if (curpeer->appctx != appctx) {
2619 appctx->st0 = PEER_SESS_ST_END;
2620 goto switchstate;
2621 }
2622 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002623
2624 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002625 if (repl <= 0) {
2626 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002627 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002628 goto switchstate;
2629 }
2630
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002631 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002632
Emeric Brun2b920a12010-09-23 18:30:22 +02002633 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002634 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002635 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002636 goto switchstate;
2637 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002638 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002639 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002640 if (!curpeer) {
2641 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002642 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002643 if (curpeer->appctx != appctx) {
2644 appctx->st0 = PEER_SESS_ST_END;
2645 goto switchstate;
2646 }
2647 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002648
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002649 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002650 if (repl <= 0) {
2651 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002652 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002653 goto switchstate;
2654 }
2655
2656 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002657 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002658 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002659 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002660 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002661 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002662 if (!curpeer) {
2663 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002664 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002665 if (curpeer->appctx != appctx) {
2666 appctx->st0 = PEER_SESS_ST_END;
2667 goto switchstate;
2668 }
2669 }
2670
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002671 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002672 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002673
Frédéric Lécaillece025572019-01-21 13:38:06 +01002674 reql = peer_getline(appctx);
2675 if (!reql)
2676 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002677
Frédéric Lécaillece025572019-01-21 13:38:06 +01002678 if (reql < 0)
2679 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002680
2681 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002682 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002683 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02002684
2685 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002686 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002687
2688 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002689 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002690 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002691 }
2692 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002693 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2694 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002695 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002696 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002697 goto switchstate;
2698 }
Olivier Houcharded879892019-03-08 18:53:43 +01002699 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002700 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002701 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002702 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002703 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002704 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002705 char *msg_cur = trash.area;
2706 char *msg_end = trash.area;
Willy Tarreaud4913912020-11-13 14:10:20 +01002707 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02002708 int totl = 0;
2709
Willy Tarreau2d372c22018-11-05 17:12:27 +01002710 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002711 if (!curpeer) {
2712 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002713 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002714 if (curpeer->appctx != appctx) {
2715 appctx->st0 = PEER_SESS_ST_END;
2716 goto switchstate;
2717 }
2718 }
2719
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002720 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2721 if (reql <= 0) {
2722 if (reql == -1)
2723 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002724 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002725 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002726
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002727 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002728 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002729 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002730
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002731 curpeer->flags |= PEER_F_ALIVE;
2732
Emeric Brun2b920a12010-09-23 18:30:22 +02002733 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002734 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002735 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002736 goto switchstate;
2737
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002738send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002739 if (curpeer->flags & PEER_F_HEARTBEAT) {
2740 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002741 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002742 if (repl <= 0) {
2743 if (repl == -1)
2744 goto out;
2745 goto switchstate;
2746 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002747 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002748 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002749 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002750 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002751 if (repl <= 0) {
2752 if (repl == -1)
2753 goto out;
2754 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002755 }
2756
Emeric Brun2b920a12010-09-23 18:30:22 +02002757 /* noting more to do */
2758 goto out;
2759 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002760 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002761 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002762 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002763 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002764 if (peer_send_status_errormsg(appctx) == -1)
2765 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002766 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002767 goto switchstate;
2768 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002769 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002770 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002771 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002772 if (peer_send_error_size_limitmsg(appctx) == -1)
2773 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002774 appctx->st0 = PEER_SESS_ST_END;
2775 goto switchstate;
2776 }
2777 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002778 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
2779 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002780 if (curpeer)
2781 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002782 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002783 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002784 prev_state = appctx->st0;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002785 if (peer_send_error_protomsg(appctx) == -1) {
2786 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002787 goto out;
Frédéric Lécaille1cc0d9a2021-01-15 16:21:28 +01002788 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002789 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002790 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002791 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002792 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002793 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002794 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002795 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002796 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002797 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002798 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002799 curpeer = NULL;
2800 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002801 si_shutw(si);
2802 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002803 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002804 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002805 }
2806 }
2807 }
2808out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002809 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002810
2811 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002812 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002813 return;
2814}
2815
Willy Tarreau30576452015-04-13 13:50:30 +02002816static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002817 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002818 .name = "<PEER>", /* used for logging */
2819 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002820 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002821};
Emeric Brun2b920a12010-09-23 18:30:22 +02002822
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002823
Emeric Brun2b920a12010-09-23 18:30:22 +02002824/*
2825 * Use this function to force a close of a peer session
2826 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002827static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002828{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002829 struct appctx *appctx = peer->appctx;
2830
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002831 /* Note that the peer sessions which have just been created
2832 * (->st0 == PEER_SESS_ST_CONNECT) must not
2833 * be shutdown, if not, the TCP session will never be closed
2834 * and stay in CLOSE_WAIT state after having been closed by
2835 * the remote side.
2836 */
2837 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002838 return;
2839
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002840 if (appctx->applet != &peer_applet)
2841 return;
2842
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002843 __peer_session_deinit(peer);
2844
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002845 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002846 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002847}
2848
Willy Tarreau91d96282015-03-13 15:47:26 +01002849/* Pre-configures a peers frontend to accept incoming connections */
2850void peers_setup_frontend(struct proxy *fe)
2851{
2852 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002853 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002854 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01002855 fe->maxconn = 0;
2856 fe->conn_retries = CONN_RETRIES;
2857 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002858 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002859 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002860 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002861 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002862}
2863
Emeric Brun2b920a12010-09-23 18:30:22 +02002864/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002865 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002866 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002867static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002868{
Willy Tarreau04b92862017-09-15 11:01:04 +02002869 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002870 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002871 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002872 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02002873
Frédéric Lécaille219587f2021-01-18 15:14:39 +01002874 peer->new_conn++;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002875 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002876 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002877 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002878 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002879 s = NULL;
2880
Emeric Brun1138fd02017-06-19 12:38:55 +02002881 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002882 if (!appctx)
2883 goto out_close;
2884
2885 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002886 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002887
Willy Tarreau04b92862017-09-15 11:01:04 +02002888 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002889 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002890 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002891 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002892 }
2893
Willy Tarreau87787ac2017-08-28 16:22:54 +02002894 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002895 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002896 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002897 }
2898
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002899 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002900 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002901 appctx_wakeup(appctx);
2902
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002903 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002904 s->target = peer_session_target(peer, s);
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002905 if (!sockaddr_alloc(&s->target_addr, &peer->addr, sizeof(peer->addr)))
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002906 goto out_free_strm;
Willy Tarreau02efeda2019-07-18 17:21:24 +02002907 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Willy Tarreaudbd02672017-12-06 17:39:53 +01002908 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002909
Emeric Brun2b920a12010-09-23 18:30:22 +02002910 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002911 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002912
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002913 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002914
Emeric Brunb3971ab2015-05-12 18:49:09 +02002915 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002916 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002917 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002918 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002919
2920 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02002921 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002922 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002923 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002924 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002925 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002926 out_free_appctx:
2927 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002928 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002929 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002930}
2931
2932/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002933 * Task processing function to manage re-connect, peer session
2934 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002935 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002936static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002937{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002938 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002939 struct peer *ps;
2940 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002941
2942 task->expire = TICK_ETERNITY;
2943
Emeric Brunb3971ab2015-05-12 18:49:09 +02002944 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002945 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002946 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002947 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002948 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002949 return NULL;
2950 }
2951
Emeric Brun80527f52017-06-19 17:46:37 +02002952 /* Acquire lock for all peers of the section */
2953 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002954 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002955
Emeric Brun2b920a12010-09-23 18:30:22 +02002956 if (!stopping) {
2957 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002958
Emeric Brun13eca442021-04-21 16:06:35 +02002959 /* resync timeout set to TICK_ETERNITY means we just start
2960 * a new process and timer was not initialized.
2961 * We must arm this timer to switch to a request to a remote
2962 * node if incoming connection from old local process never
2963 * comes.
2964 */
2965 if (peers->resync_timeout == TICK_ETERNITY)
2966 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
2967
Emeric Brunb3971ab2015-05-12 18:49:09 +02002968 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2969 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2970 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002971 /* Resync from local peer needed
2972 no peer was assigned for the lesson
2973 and no old local peer found
2974 or resync timeout expire */
2975
2976 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002977 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brune1f55742021-04-28 12:59:35 +02002978 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02002979
2980 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002981 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002982 }
2983
2984 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002985 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002986 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002987 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002988 if (!ps->appctx) {
2989 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002990 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002991 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002992 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002993 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002994 tick_is_expired(ps->reconnect, now_ms))) {
2995 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002996 * or previous peer connection established with success
2997 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002998 * and reconnection timer is expired */
2999
3000 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003001 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003002 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003003 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003004 /* If previous session failed during connection
3005 * but reconnection timer is not expired */
3006
3007 /* reschedule task for reconnect */
3008 task->expire = tick_first(task->expire, ps->reconnect);
3009 }
3010 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003011 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003012 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003013 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003014 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3015 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003016 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3017 /* Resync from a remote is needed
3018 * and no peer was assigned for lesson
3019 * and current peer may be up2date */
3020
3021 /* assign peer for the lesson */
3022 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003023 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brune1f55742021-04-28 12:59:35 +02003024 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003025
Willy Tarreau9df94c22016-10-31 18:42:52 +01003026 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003027 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003028 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003029 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003030 int update_to_push = 0;
3031
Emeric Brunb3971ab2015-05-12 18:49:09 +02003032 /* Awake session if there is data to push */
3033 for (st = ps->tables; st ; st = st->next) {
Emeric Brund9e4ee62021-04-28 11:48:15 +02003034 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003035 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003036 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003037 /* There is no need to send a heartbeat message
3038 * when some updates must be pushed. The remote
3039 * peer will consider <ps> peer as alive when it will
3040 * receive these updates.
3041 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003042 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003043 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003044 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003045 /* We are going to send updates, let's ensure we will
3046 * come back to send heartbeat messages or to reconnect.
3047 */
3048 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003049 appctx_wakeup(ps->appctx);
3050 break;
3051 }
3052 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003053 /* When there are updates to send we do not reconnect
3054 * and do not send heartbeat message either.
3055 */
3056 if (!update_to_push) {
3057 if (tick_is_expired(ps->reconnect, now_ms)) {
3058 if (ps->flags & PEER_F_ALIVE) {
3059 /* This peer was alive during a 'reconnect' period.
3060 * Flag it as not alive again for the next period.
3061 */
3062 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003063 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003064 }
3065 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003066 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003067 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003068 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003069 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003070 }
3071 }
3072 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3073 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3074 ps->flags |= PEER_F_HEARTBEAT;
3075 appctx_wakeup(ps->appctx);
3076 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003077 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003078 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003079 }
3080 /* else do nothing */
3081 } /* SUCCESSCODE */
3082 } /* !ps->peer->local */
3083 } /* for */
3084
3085 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003086 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3087 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3088 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003089 /* Resync from remote peer needed
3090 * no peer was assigned for the lesson
3091 * and resync timeout expire */
3092
3093 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003094 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brune1f55742021-04-28 12:59:35 +02003095 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003096 }
3097
Emeric Brunb3971ab2015-05-12 18:49:09 +02003098 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003099 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003100 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3101 if (!tick_is_expired(peers->resync_timeout, now_ms))
3102 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003103 }
3104 } /* !stopping */
3105 else {
3106 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003107 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003108 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003109 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003110 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01003111 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003112 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003113
Emeric Brun1efabf12021-04-22 18:20:37 +02003114 /* disconnect all connected peers to process a local sync
3115 * this must be done only the first time we are switching
3116 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003117 */
Emeric Brun1efabf12021-04-22 18:20:37 +02003118 for (ps = peers->remote; ps; ps = ps->next) {
3119 /* we're killing a connection, we must apply a random delay before
3120 * retrying otherwise the other end will do the same and we can loop
3121 * for a while.
3122 */
3123 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3124 if (ps->appctx) {
3125 peer_session_forceshutdown(ps);
3126 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003127 }
3128 }
3129 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003130
Emeric Brunb3971ab2015-05-12 18:49:09 +02003131 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003132 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003133 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003134 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01003135 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003136 peers->flags &= ~PEERS_F_DONOTSTOP;
3137 for (st = ps->tables; st ; st = st->next)
Emeric Brun167b7832021-04-23 17:02:22 +02003138 _HA_ATOMIC_SUB(&st->table->refcnt, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02003139 }
3140 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003141 else if (!ps->appctx) {
3142 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003143 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003144 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3145 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3146 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003147 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003148 * or previous peer connection was successfully established
3149 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02003150 * or during previous connect, peer replies a try again statuscode */
3151
Emeric Brun1efabf12021-04-22 18:20:37 +02003152 /* connect to the local peer if we must push a local sync */
3153 if (peers->flags & PEERS_F_DONOTSTOP) {
3154 peer_session_create(peers, ps);
3155 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003156 }
3157 else {
3158 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003159 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003160 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01003161 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003162 peers->flags &= ~PEERS_F_DONOTSTOP;
3163 for (st = ps->tables; st ; st = st->next)
Emeric Brun167b7832021-04-23 17:02:22 +02003164 _HA_ATOMIC_SUB(&st->table->refcnt, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02003165 }
3166 }
3167 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003168 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003169 /* current peer connection is active and established
3170 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003171 for (st = ps->tables; st ; st = st->next) {
Emeric Brund9e4ee62021-04-28 11:48:15 +02003172 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003173 appctx_wakeup(ps->appctx);
3174 break;
3175 }
3176 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003177 }
3178 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003179
3180 /* Release lock for all peers of the section */
3181 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003182 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003183
Emeric Brun2b920a12010-09-23 18:30:22 +02003184 /* Wakeup for re-connect */
3185 return task;
3186}
3187
Emeric Brunb3971ab2015-05-12 18:49:09 +02003188
Emeric Brun2b920a12010-09-23 18:30:22 +02003189/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003190 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003191 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003192int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003193{
Emeric Brun2b920a12010-09-23 18:30:22 +02003194 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003195
Emeric Brun2b920a12010-09-23 18:30:22 +02003196 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003197 peers->peers_fe->maxconn += 3;
3198 }
3199
Emeric Brunc60def82017-09-27 14:59:38 +02003200 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02003201 if (!peers->sync_task)
3202 return 0;
3203
Emeric Brunb3971ab2015-05-12 18:49:09 +02003204 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003205 peers->sync_task->context = (void *)peers;
3206 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3207 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003208 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003209}
3210
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003211/*
3212 * Allocate a cache a dictionary entries used upon transmission.
3213 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003214static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003215{
3216 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003217 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003218
3219 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003220 entries = calloc(max_entries, sizeof *entries);
3221 if (!d || !entries)
3222 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003223
3224 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003225 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003226 d->cached_entries = EB_ROOT_UNIQUE;
3227 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003228
3229 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003230
3231 err:
3232 free(d);
3233 free(entries);
3234 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003235}
3236
3237/*
3238 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3239 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003240 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003241 * Must be deallocated calling free_dcache().
3242 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003243static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003244{
3245 struct dcache_tx *dc_tx;
3246 struct dcache *dc;
3247 struct dcache_rx *dc_rx;
3248
3249 dc = calloc(1, sizeof *dc);
3250 dc_tx = new_dcache_tx(max_entries);
3251 dc_rx = calloc(max_entries, sizeof *dc_rx);
3252 if (!dc || !dc_tx || !dc_rx)
3253 goto err;
3254
3255 dc->tx = dc_tx;
3256 dc->rx = dc_rx;
3257 dc->max_entries = max_entries;
3258
3259 return dc;
3260
3261 err:
3262 free(dc);
3263 free(dc_tx);
3264 free(dc_rx);
3265 return NULL;
3266}
3267
3268/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003269 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3270 * entries used upon transmission.
3271 * Return the entry if found, NULL if not.
3272 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003273static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3274 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003275{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003276 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003277}
3278
3279/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003280 * Flush <dc> cache.
3281 * Always succeeds.
3282 */
3283static inline void flush_dcache(struct peer *peer)
3284{
3285 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003286 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003287
Frédéric Lécaille9650ee92020-11-12 21:01:54 +01003288 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003289 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécaille9650ee92020-11-12 21:01:54 +01003290 dc->tx->entries[i].key = NULL;
3291 }
3292 dc->tx->prev_lookup = NULL;
3293 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003294
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003295 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003296}
3297
3298/*
3299 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3300 * with information provided by <i> dictionary cache entry (especially the value
3301 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003302 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003303 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003304static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003305{
3306 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003307 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003308
3309 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003310
3311 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3312 o = dc_tx->prev_lookup;
3313 } else {
3314 o = dcache_tx_lookup_value(dc_tx, i);
3315 if (o) {
3316 /* Save it */
3317 dc_tx->prev_lookup = o;
3318 }
3319 }
3320
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003321 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003322 /* Copy the ID. */
3323 i->id = o - dc->tx->entries;
3324 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003325 }
3326
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003327 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003328 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003329
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003330 ebpt_delete(o);
3331 o->key = i->entry.key;
3332 ebpt_insert(&dc_tx->cached_entries, o);
3333 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003334
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003335 /* Update the index for the next entry to put in cache */
3336 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003337
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003338 return o;
3339}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003340
3341/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003342 * Allocate a dictionary cache for each peer of <peers> section.
3343 * Return 1 if succeeded, 0 if not.
3344 */
3345int peers_alloc_dcache(struct peers *peers)
3346{
3347 struct peer *p;
3348
3349 for (p = peers->remote; p; p = p->next) {
3350 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3351 if (!p->dcache)
3352 return 0;
3353 }
3354
3355 return 1;
3356}
3357
3358/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003359 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Bretoncff2df92021-05-12 17:39:04 +02003360 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003361 */
Remi Tricot-Le Bretoncff2df92021-05-12 17:39:04 +02003362int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003363{
3364 struct shared_table *st;
3365 struct peer * curpeer;
3366 int id = 0;
Remi Tricot-Le Bretoncff2df92021-05-12 17:39:04 +02003367 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003368
3369 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003370 st = calloc(1,sizeof(*st));
Remi Tricot-Le Bretoncff2df92021-05-12 17:39:04 +02003371 if (!st) {
3372 retval = 1;
3373 break;
3374 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003375 st->table = table;
3376 st->next = curpeer->tables;
3377 if (curpeer->tables)
3378 id = curpeer->tables->local_id;
3379 st->local_id = id + 1;
3380
Emeric Brun167b7832021-04-23 17:02:22 +02003381 /* If peer is local we inc table
3382 * refcnt to protect against flush
3383 * until this process pushed all
3384 * table content to the new one
3385 */
3386 if (curpeer->local)
3387 _HA_ATOMIC_ADD(&st->table->refcnt, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003388 curpeer->tables = st;
3389 }
3390
3391 table->sync_task = peers->sync_task;
Remi Tricot-Le Bretoncff2df92021-05-12 17:39:04 +02003392
3393 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003394}
3395
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003396/*
3397 * Parse the "show peers" command arguments.
3398 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3399 * error message.
3400 */
3401static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3402{
3403 appctx->ctx.cfgpeers.target = NULL;
3404
3405 if (*args[2]) {
3406 struct peers *p;
3407
3408 for (p = cfg_peers; p; p = p->next) {
3409 if (!strcmp(p->id, args[2])) {
3410 appctx->ctx.cfgpeers.target = p;
3411 break;
3412 }
3413 }
3414
Willy Tarreau9d008692019-08-09 11:21:01 +02003415 if (!p)
3416 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003417 }
3418
3419 return 0;
3420}
3421
3422/*
3423 * This function dumps the peer state information of <peers> "peers" section.
3424 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3425 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3426 */
3427static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3428{
3429 struct tm tm;
3430
3431 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003432 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 +02003433 peers,
3434 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3435 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003436 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003437 peers->resync_timeout ?
3438 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3439 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003440 TICKS_TO_MS(1000)) : "<NEVER>",
3441 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003442
3443 if (ci_putchk(si_ic(si), msg) == -1) {
3444 si_rx_room_blk(si);
3445 return 0;
3446 }
3447
3448 return 1;
3449}
3450
3451/*
3452 * This function dumps <peer> state information.
3453 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3454 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3455 */
3456static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
3457{
3458 struct connection *conn;
3459 char pn[INET6_ADDRSTRLEN];
3460 struct stream_interface *peer_si;
3461 struct stream *peer_s;
3462 struct appctx *appctx;
3463 struct shared_table *st;
3464
3465 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003466 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003467 peer, peer->id,
3468 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003469 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003470 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003471 statuscode_str(peer->statuscode));
3472
3473 chunk_appendf(msg, " last_hdshk=%s\n",
3474 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3475 TICKS_TO_MS(1000)) : "<NEVER>");
3476
3477 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003478 peer->reconnect ?
3479 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3480 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003481 TICKS_TO_MS(1000)) : "<NEVER>");
3482
3483 chunk_appendf(msg, " heartbeat=%s",
3484 peer->heartbeat ?
3485 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3486 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3487 TICKS_TO_MS(1000)) : "<NEVER>");
3488
3489 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 +01003490 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003491 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003492
3493 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3494
3495 appctx = peer->appctx;
3496 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003497 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003498
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003499 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3500 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003501
3502 peer_si = peer->appctx->owner;
3503 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003504 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003505
3506 peer_s = si_strm(peer_si);
3507 if (!peer_s)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003508 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003509
3510 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3511
3512 conn = objt_conn(strm_orig(peer_s));
3513 if (conn)
3514 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3515
Willy Tarreau3ca14902019-07-17 14:53:15 +02003516 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 +02003517 case AF_INET:
3518 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003519 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003520 break;
3521 case AF_UNIX:
3522 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3523 break;
3524 }
3525
Willy Tarreau3ca14902019-07-17 14:53:15 +02003526 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 +02003527 case AF_INET:
3528 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003529 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003530 break;
3531 case AF_UNIX:
3532 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3533 break;
3534 }
3535
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003536 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003537 if (peer->remote_table)
3538 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3539 peer->remote_table,
3540 peer->remote_table->table->id,
3541 peer->remote_table->local_id,
3542 peer->remote_table->remote_id);
3543
3544 if (peer->last_local_table)
3545 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3546 peer->last_local_table,
3547 peer->last_local_table->table->id,
3548 peer->last_local_table->local_id,
3549 peer->last_local_table->remote_id);
3550
3551 if (peer->tables) {
3552 chunk_appendf(&trash, "\n shared tables:");
3553 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003554 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003555 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003556 struct dcache *dcache;
3557
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003558 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003559 dcache = peer->dcache;
3560
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003561 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3562 "flags=0x%x remote_data=0x%llx",
3563 st, st->local_id, st->remote_id,
3564 st->flags, (unsigned long long)st->remote_data);
3565 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3566 " teaching_origin=%u update=%u",
3567 st->last_acked, st->last_pushed, st->last_get,
3568 st->teaching_origin, st->update);
3569 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun167b7832021-04-23 17:02:22 +02003570 " commitupdate=%u refcnt=%u",
3571 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003572 chunk_appendf(&trash, "\n TX dictionary cache:");
3573 count = 0;
3574 for (i = 0; i < dcache->max_entries; i++) {
3575 struct ebpt_node *node;
3576 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003577
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003578 node = &dcache->tx->entries[i];
3579 if (!node->key)
3580 break;
3581
3582 if (!count++)
3583 chunk_appendf(&trash, "\n ");
3584 de = node->key;
3585 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3586 count &= 0x3;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003587 }
3588 chunk_appendf(&trash, "\n RX dictionary cache:");
3589 count = 0;
3590 for (i = 0; i < dcache->max_entries; i++) {
3591 if (!count++)
3592 chunk_appendf(&trash, "\n ");
3593 chunk_appendf(&trash, " %3u -> %s", i,
3594 dcache->rx[i].de ?
3595 (char *)dcache->rx[i].de->value.key : "-");
3596 count &= 0x3;
3597 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003598 }
3599 }
3600
3601 end:
3602 chunk_appendf(&trash, "\n");
3603 if (ci_putchk(si_ic(si), msg) == -1) {
3604 si_rx_room_blk(si);
3605 return 0;
3606 }
3607
3608 return 1;
3609}
3610
3611/*
3612 * This function dumps all the peers of "peers" section.
3613 * Returns 0 if the output buffer is full and needs to be called
3614 * again, non-zero if not. It proceeds in an isolated thread, so
3615 * there is no thread safety issue here.
3616 */
3617static int cli_io_handler_show_peers(struct appctx *appctx)
3618{
3619 int show_all;
3620 int ret = 0, first_peers = 1;
3621 struct stream_interface *si = appctx->owner;
3622
3623 thread_isolate();
3624
3625 show_all = !appctx->ctx.cfgpeers.target;
3626
3627 chunk_reset(&trash);
3628
3629 while (appctx->st2 != STAT_ST_FIN) {
3630 switch (appctx->st2) {
3631 case STAT_ST_INIT:
3632 if (show_all)
3633 appctx->ctx.cfgpeers.peers = cfg_peers;
3634 else
3635 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3636
3637 appctx->st2 = STAT_ST_LIST;
3638 /* fall through */
3639
3640 case STAT_ST_LIST:
3641 if (!appctx->ctx.cfgpeers.peers) {
3642 /* No more peers list. */
3643 appctx->st2 = STAT_ST_END;
3644 }
3645 else {
3646 if (!first_peers)
3647 chunk_appendf(&trash, "\n");
3648 else
3649 first_peers = 0;
3650 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3651 goto out;
3652
3653 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3654 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3655 appctx->st2 = STAT_ST_INFO;
3656 }
3657 break;
3658
3659 case STAT_ST_INFO:
3660 if (!appctx->ctx.cfgpeers.peer) {
3661 /* End of peer list */
3662 if (show_all)
3663 appctx->st2 = STAT_ST_LIST;
3664 else
3665 appctx->st2 = STAT_ST_END;
3666 }
3667 else {
3668 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
3669 goto out;
3670
3671 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3672 }
3673 break;
3674
3675 case STAT_ST_END:
3676 appctx->st2 = STAT_ST_FIN;
3677 break;
3678 }
3679 }
3680 ret = 1;
3681 out:
3682 thread_release();
3683 return ret;
3684}
3685
3686/*
3687 * CLI keywords.
3688 */
3689static struct cli_kw_list cli_kws = {{ }, {
3690 { { "show", "peers", NULL }, "show peers [peers section]: dump some information about all the peers or this peers section", cli_parse_show_peers, cli_io_handler_show_peers, },
3691 {},
3692}};
3693
3694/* Register cli keywords */
3695INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3696