blob: 2cb2433545258942bde19c7066b4f99c13ab8a91 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau8db34cc2021-10-06 17:53:19 +020023#include <import/eb32tree.h>
24#include <import/ebmbtree.h>
25#include <import/ebpttree.h>
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/applet.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020029#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010031#include <haproxy/conn_stream.h>
32#include <haproxy/cs_utils.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020033#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020034#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020036#include <haproxy/frontend.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020037#include <haproxy/net_helper.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020038#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020039#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020040#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020041#include <haproxy/session-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020042#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020043#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020044#include <haproxy/stick_table.h>
45#include <haproxy/stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/task.h>
47#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020048#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020049#include <haproxy/tools.h>
Frédéric Lécailled8659352020-11-10 16:18:03 +010050#include <haproxy/trace.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020051
Emeric Brun2b920a12010-09-23 18:30:22 +020052
53/*******************************/
54/* Current peer learning state */
55/*******************************/
56
57/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020058/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020059/******************************/
Emeric Brunccdfbae2021-04-28 12:59:35 +020060#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
61#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
62#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
63#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
64#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
65#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
66#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
67#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
68#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
69#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
70#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
71#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
72#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
73#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
74#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
75#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
76 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020077
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010078#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
79#define PEERS_RESYNC_FROMLOCAL 0x00000000
80#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
81#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020082
83/***********************************/
84/* Current shared table sync state */
85/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010086#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
87#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020088
89/******************************/
90/* Remote peer teaching state */
91/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010092#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
93#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
94#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
95#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
96#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
97#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
98#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
99#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +0200100
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100101#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
102#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +0200103
Frédéric Lécaille54bff832019-03-26 10:25:20 +0100104#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
105#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100106#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
107
Willy Tarreau49962b52021-02-12 16:56:22 +0100108/* flags for "show peers" */
109#define PEERS_SHOW_F_DICT 0x00000001 /* also show the contents of the dictionary */
110
Emeric Brunb3971ab2015-05-12 18:49:09 +0200111/*****************************/
112/* Sync message class */
113/*****************************/
114enum {
115 PEER_MSG_CLASS_CONTROL = 0,
116 PEER_MSG_CLASS_ERROR,
117 PEER_MSG_CLASS_STICKTABLE = 10,
118 PEER_MSG_CLASS_RESERVED = 255,
119};
120
121/*****************************/
122/* control message types */
123/*****************************/
124enum {
125 PEER_MSG_CTRL_RESYNCREQ = 0,
126 PEER_MSG_CTRL_RESYNCFINISHED,
127 PEER_MSG_CTRL_RESYNCPARTIAL,
128 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100129 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200130};
131
132/*****************************/
133/* error message types */
134/*****************************/
135enum {
136 PEER_MSG_ERR_PROTOCOL = 0,
137 PEER_MSG_ERR_SIZELIMIT,
138};
139
Emeric Brun530ba382020-06-02 11:17:42 +0200140/* network key types;
141 * network types were directly and mistakenly
142 * mapped on sample types, to keep backward
143 * compatiblitiy we keep those values but
144 * we now use a internal/network mapping
145 * to avoid further mistakes adding or
146 * modifying internals types
147 */
148enum {
149 PEER_KT_ANY = 0, /* any type */
150 PEER_KT_RESV1, /* UNUSED */
151 PEER_KT_SINT, /* signed 64bits integer type */
152 PEER_KT_RESV3, /* UNUSED */
153 PEER_KT_IPV4, /* ipv4 type */
154 PEER_KT_IPV6, /* ipv6 type */
155 PEER_KT_STR, /* char string type */
156 PEER_KT_BIN, /* buffer type */
157 PEER_KT_TYPES /* number of types, must always be last */
158};
159
160/* Map used to retrieve network type from internal type
161 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
162 */
163static int peer_net_key_type[SMP_TYPES] = {
164 [SMP_T_SINT] = PEER_KT_SINT,
165 [SMP_T_IPV4] = PEER_KT_IPV4,
166 [SMP_T_IPV6] = PEER_KT_IPV6,
167 [SMP_T_STR] = PEER_KT_STR,
168 [SMP_T_BIN] = PEER_KT_BIN,
169};
170
171/* Map used to retrieve internal type from external type
172 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
173 */
174static int peer_int_key_type[PEER_KT_TYPES] = {
175 [PEER_KT_SINT] = SMP_T_SINT,
176 [PEER_KT_IPV4] = SMP_T_IPV4,
177 [PEER_KT_IPV6] = SMP_T_IPV6,
178 [PEER_KT_STR] = SMP_T_STR,
179 [PEER_KT_BIN] = SMP_T_BIN,
180};
181
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100182/*
183 * Parameters used by functions to build peer protocol messages. */
184struct peer_prep_params {
185 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100186 struct peer *peer;
187 } hello;
188 struct {
189 unsigned int st1;
190 } error_status;
191 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100192 struct stksess *stksess;
193 struct shared_table *shared_table;
194 unsigned int updateid;
195 int use_identifier;
196 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200197 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100198 } updt;
199 struct {
200 struct shared_table *shared_table;
201 } swtch;
202 struct {
203 struct shared_table *shared_table;
204 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100205 struct {
206 unsigned char head[2];
207 } control;
208 struct {
209 unsigned char head[2];
210 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100211};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200212
213/*******************************/
214/* stick table sync mesg types */
215/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500216/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200217/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200218#define PEER_MSG_STKT_UPDATE 0x80
219#define PEER_MSG_STKT_INCUPDATE 0x81
220#define PEER_MSG_STKT_DEFINE 0x82
221#define PEER_MSG_STKT_SWITCH 0x83
222#define PEER_MSG_STKT_ACK 0x84
223#define PEER_MSG_STKT_UPDATE_TIMED 0x85
224#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200225/* All the stick-table message identifiers abova have the #7 bit set */
226#define PEER_MSG_STKT_BIT 7
227#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200228
Frédéric Lécaille39143342019-05-24 14:32:27 +0200229/* The maximum length of an encoded data length. */
230#define PEER_MSG_ENC_LENGTH_MAXLEN 5
231
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200232/* Minimum 64-bits value encoded with 2 bytes */
233#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
234/* 3 bytes */
235#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
236/* 4 bytes */
237#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
238/* 5 bytes */
239#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
240/* 6 bytes */
241#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
242/* 7 bytes */
243#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
244/* 8 bytes */
245#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
246/* 9 bytes */
247#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
248/* 10 bytes */
249#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
250
251/* #7 bit used to detect the last byte to be encoded */
252#define PEER_ENC_STOP_BIT 7
253/* The byte minimum value with #7 bit set */
254#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
255/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
256#define PEER_ENC_2BYTES_MIN_BITS 4
257
Frédéric Lécaille39143342019-05-24 14:32:27 +0200258#define PEER_MSG_HEADER_LEN 2
259
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200260#define PEER_STKT_CACHE_MAX_ENTRIES 128
261
Emeric Brun2b920a12010-09-23 18:30:22 +0200262/**********************************/
263/* Peer Session IO handler states */
264/**********************************/
265
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100266enum {
267 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
268 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
269 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
270 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100271 /* after this point, data were possibly exchanged */
272 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
273 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
274 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
275 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
276 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200277 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
278 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100279 PEER_SESS_ST_END, /* Killed session */
280};
Emeric Brun2b920a12010-09-23 18:30:22 +0200281
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100282/***************************************************/
283/* Peer Session status code - part of the protocol */
284/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200285
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100286#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
287#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200288
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100289#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200290
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100291#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200292
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100293#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
294#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
295#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
296#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200297
298#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200299#define PEER_MAJOR_VER 2
300#define PEER_MINOR_VER 1
301#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200302
Willy Tarreau6254a922019-01-29 17:45:23 +0100303static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200304struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200305static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200306
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200307static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
308 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200309static inline void flush_dcache(struct peer *peer);
310
Frédéric Lécailled8659352020-11-10 16:18:03 +0100311/* trace source and events */
312static void peers_trace(enum trace_level level, uint64_t mask,
313 const struct trace_source *src,
314 const struct ist where, const struct ist func,
315 const void *a1, const void *a2, const void *a3, const void *a4);
316
317static const struct trace_event peers_trace_events[] = {
318#define PEERS_EV_UPDTMSG (1 << 0)
319 { .mask = PEERS_EV_UPDTMSG, .name = "updtmsg", .desc = "update message received" },
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100320#define PEERS_EV_ACKMSG (1 << 1)
321 { .mask = PEERS_EV_ACKMSG, .name = "ackmsg", .desc = "ack message received" },
322#define PEERS_EV_SWTCMSG (1 << 2)
323 { .mask = PEERS_EV_SWTCMSG, .name = "swtcmsg", .desc = "switch message received" },
324#define PEERS_EV_DEFMSG (1 << 3)
325 { .mask = PEERS_EV_DEFMSG, .name = "defmsg", .desc = "definition message received" },
326#define PEERS_EV_CTRLMSG (1 << 4)
327 { .mask = PEERS_EV_CTRLMSG, .name = "ctrlmsg", .desc = "control message sent/received" },
328#define PEERS_EV_SESSREL (1 << 5)
329 { .mask = PEERS_EV_SESSREL, .name = "sessrl", .desc = "peer session releasing" },
330#define PEERS_EV_PROTOERR (1 << 6)
331 { .mask = PEERS_EV_PROTOERR, .name = "protoerr", .desc = "protocol error" },
Frédéric Lécailled8659352020-11-10 16:18:03 +0100332};
333
334static const struct name_desc peers_trace_lockon_args[4] = {
335 /* arg1 */ { /* already used by the connection */ },
336 /* arg2 */ { .name="peers", .desc="Peers protocol" },
337 /* arg3 */ { },
338 /* arg4 */ { }
339};
340
341static const struct name_desc peers_trace_decoding[] = {
342#define PEERS_VERB_CLEAN 1
343 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
344 { /* end */ }
345};
346
347
348struct trace_source trace_peers = {
349 .name = IST("peers"),
350 .desc = "Peers protocol",
351 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
352 .default_cb = peers_trace,
353 .known_events = peers_trace_events,
354 .lockon_args = peers_trace_lockon_args,
355 .decoding = peers_trace_decoding,
356 .report_events = ~0, /* report everything by default */
357};
358
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100359/* Return peer control message types as strings (only for debugging purpose). */
360static inline char *ctrl_msg_type_str(unsigned int type)
361{
362 switch (type) {
363 case PEER_MSG_CTRL_RESYNCREQ:
364 return "RESYNCREQ";
365 case PEER_MSG_CTRL_RESYNCFINISHED:
366 return "RESYNCFINISHED";
367 case PEER_MSG_CTRL_RESYNCPARTIAL:
368 return "RESYNCPARTIAL";
369 case PEER_MSG_CTRL_RESYNCCONFIRM:
370 return "RESYNCCONFIRM";
371 case PEER_MSG_CTRL_HEARTBEAT:
372 return "HEARTBEAT";
373 default:
374 return "???";
375 }
376}
377
Frédéric Lécailled8659352020-11-10 16:18:03 +0100378#define TRACE_SOURCE &trace_peers
379INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
380
381static void peers_trace(enum trace_level level, uint64_t mask,
382 const struct trace_source *src,
383 const struct ist where, const struct ist func,
384 const void *a1, const void *a2, const void *a3, const void *a4)
385{
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100386 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécailled8659352020-11-10 16:18:03 +0100387 if (a2) {
388 const struct peer *peer = a2;
389
390 chunk_appendf(&trace_buf, " peer=%s", peer->id);
391 }
392 if (a3) {
393 const char *p = a3;
394
395 chunk_appendf(&trace_buf, " @%p", p);
396 }
397 if (a4) {
398 const size_t *val = a4;
399
400 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
401 }
402 }
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100403
404 if (mask & PEERS_EV_DEFMSG) {
405 if (a2) {
406 const struct peer *peer = a2;
407
408 chunk_appendf(&trace_buf, " peer=%s", peer->id);
409 }
410 if (a3) {
411 const char *p = a3;
412
413 chunk_appendf(&trace_buf, " @%p", p);
414 }
415 if (a4) {
416 const int *val = a4;
417
418 chunk_appendf(&trace_buf, " %d", *val);
419 }
420 }
421
422 if (mask & PEERS_EV_CTRLMSG) {
423 if (a2) {
424 const unsigned char *ctrl_msg_type = a2;
425
426 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
427
428 }
429 if (a3) {
430 const char *local_peer = a3;
431
432 chunk_appendf(&trace_buf, " %s", local_peer);
433 }
434
435 if (a4) {
436 const char *remote_peer = a4;
437
438 chunk_appendf(&trace_buf, " -> %s", remote_peer);
439 }
440 }
441
442 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
443 if (a2) {
444 const struct peer *peer = a2;
445 struct peers *peers = NULL;
446
Christopher Fauletd9e6b352021-11-15 09:40:57 +0100447 if (peer->appctx) {
Christopher Faulet908628c2022-03-25 16:43:49 +0100448 struct stream *s = __cs_strm(peer->appctx->owner);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100449
Christopher Faulet908628c2022-03-25 16:43:49 +0100450 peers = strm_fe(s)->parent;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100451 }
452
453 if (peers)
454 chunk_appendf(&trace_buf, " %s", peers->local->id);
Christopher Fauletd9e6b352021-11-15 09:40:57 +0100455 chunk_appendf(&trace_buf, " -> %s", peer->id);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100456 }
457
458 if (a3) {
459 const int *prev_state = a3;
460
461 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
462 }
463 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100464}
465
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200466static const char *statuscode_str(int statuscode)
467{
468 switch (statuscode) {
469 case PEER_SESS_SC_CONNECTCODE:
470 return "CONN";
471 case PEER_SESS_SC_CONNECTEDCODE:
472 return "HSHK";
473 case PEER_SESS_SC_SUCCESSCODE:
474 return "ESTA";
475 case PEER_SESS_SC_TRYAGAIN:
476 return "RETR";
477 case PEER_SESS_SC_ERRPROTO:
478 return "PROT";
479 case PEER_SESS_SC_ERRVERSION:
480 return "VERS";
481 case PEER_SESS_SC_ERRHOST:
482 return "NAME";
483 case PEER_SESS_SC_ERRPEER:
484 return "UNKN";
485 default:
486 return "NONE";
487 }
488}
489
Emeric Brun18928af2017-03-29 16:32:53 +0200490/* This function encode an uint64 to 'dynamic' length format.
491 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500492 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200493 At return, the *str is set at the next Byte after then
494 encoded integer. The function returns then length of the
495 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200496int intencode(uint64_t i, char **str) {
497 int idx = 0;
498 unsigned char *msg;
499
Emeric Brunb3971ab2015-05-12 18:49:09 +0200500 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200501 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200502 msg[0] = (unsigned char)i;
503 *str = (char *)&msg[idx+1];
504 return (idx+1);
505 }
506
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200507 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
508 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
509 while (i >= PEER_ENC_STOP_BYTE) {
510 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
511 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200512 }
513 msg[++idx] = (unsigned char)i;
514 *str = (char *)&msg[idx+1];
515 return (idx+1);
516}
517
518
Emeric Brun5ea07d92021-06-30 13:21:58 +0200519/* This function returns a decoded 64bits unsigned integer
520 * from a varint
521 *
522 * Calling:
523 * - *str must point on the first byte of the buffer to decode.
524 * - end must point on the next byte after the end of the buffer
525 * we are authorized to parse (buf + buflen)
526 *
527 * At return:
528 *
529 * On success *str will point at the byte following
530 * the fully decoded integer into the buffer. and
531 * the decoded value is returned.
532 *
533 * If end is reached before the integer was fully decoded,
534 * *str is set to NULL and the caller have to check this
535 * to know there is a decoding error. In this case
536 * the returned integer is also forced to 0
537 */
Emeric Brun18928af2017-03-29 16:32:53 +0200538uint64_t intdecode(char **str, char *end)
539{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200540 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200541 uint64_t i;
542 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200543
544 if (!*str)
545 return 0;
546
547 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200548 if (msg >= (unsigned char *)end)
549 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200550
Emeric Brun18928af2017-03-29 16:32:53 +0200551 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200552 if (i >= PEER_ENC_2BYTES_MIN) {
553 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200554 do {
555 if (msg >= (unsigned char *)end)
556 goto fail;
557 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200558 shift += PEER_ENC_STOP_BIT;
559 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200560 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100561 *str = (char *)msg;
562 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200563
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100564 fail:
565 *str = NULL;
566 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200567}
Emeric Brun2b920a12010-09-23 18:30:22 +0200568
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100569/*
570 * Build a "hello" peer protocol message.
571 * Return the number of written bytes written to build this messages if succeeded,
572 * 0 if not.
573 */
574static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
575{
576 int min_ver, ret;
577 struct peer *peer;
578
579 peer = p->hello.peer;
580 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
581 /* Prepare headers */
Willy Tarreau2645b342022-04-12 08:28:18 +0200582 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %d.%d\n%s\n%s %d %d\n",
583 (int)PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), (int)1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100584 if (ret >= size)
585 return 0;
586
587 return ret;
588}
589
590/*
591 * Build a "handshake succeeded" 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_successmsg(char *msg, size_t size, struct peer_prep_params *p)
596{
597 int ret;
598
Willy Tarreau2645b342022-04-12 08:28:18 +0200599 ret = snprintf(msg, size, "%d\n", (int)PEER_SESS_SC_SUCCESSCODE);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100600 if (ret >= size)
601 return 0;
602
603 return ret;
604}
605
606/*
607 * Build an error status message.
608 * Return the number of written bytes written to build this messages if succeeded,
609 * 0 if not.
610 */
611static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
612{
613 int ret;
614 unsigned int st1;
615
616 st1 = p->error_status.st1;
617 ret = snprintf(msg, size, "%d\n", st1);
618 if (ret >= size)
619 return 0;
620
621 return ret;
622}
623
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200624/* Set the stick-table UPDATE message type byte at <msg_type> address,
625 * depending on <use_identifier> and <use_timed> boolean parameters.
626 * Always successful.
627 */
628static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
629{
630 if (use_timed) {
631 if (use_identifier)
632 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
633 else
634 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
635 }
636 else {
637 if (use_identifier)
638 *msg_type = PEER_MSG_STKT_UPDATE;
639 else
640 *msg_type = PEER_MSG_STKT_INCUPDATE;
641 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200642}
Emeric Brun2b920a12010-09-23 18:30:22 +0200643/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200644 * This prepare the data update message on the stick session <ts>, <st> is the considered
645 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800646 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200647 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
648 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200649 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100650static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200651{
652 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200653 unsigned short datalen;
654 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200655 unsigned int data_type;
656 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100657 struct stksess *ts;
658 struct shared_table *st;
659 unsigned int updateid;
660 int use_identifier;
661 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200662 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100663
664 ts = p->updt.stksess;
665 st = p->updt.shared_table;
666 updateid = p->updt.updateid;
667 use_identifier = p->updt.use_identifier;
668 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200669 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200670
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200671 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200672
Emeric Brun2b920a12010-09-23 18:30:22 +0200673 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200674
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500675 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200676 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200677 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200678 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200679
680 /* encode update identifier if needed */
681 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200682 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200683 memcpy(cursor, &netinteger, sizeof(netinteger));
684 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200685 }
686
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200687 if (use_timed) {
688 netinteger = htonl(tick_remain(now_ms, ts->expire));
689 memcpy(cursor, &netinteger, sizeof(netinteger));
690 cursor += sizeof(netinteger);
691 }
692
Emeric Brunb3971ab2015-05-12 18:49:09 +0200693 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200694 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200695 int stlen = strlen((char *)ts->key.key);
696
Emeric Brunb3971ab2015-05-12 18:49:09 +0200697 intencode(stlen, &cursor);
698 memcpy(cursor, ts->key.key, stlen);
699 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200700 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200701 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100702 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200703 memcpy(cursor, &netinteger, sizeof(netinteger));
704 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200705 }
706 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200707 memcpy(cursor, ts->key.key, st->table->key_size);
708 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200709 }
710
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100711 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200712 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200713 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200714
Emeric Brun94900952015-06-11 18:25:54 +0200715 data_ptr = stktable_data_ptr(st->table, ts, data_type);
716 if (data_ptr) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200717 /* in case of array all elements use
718 * the same std_type and they are linearly
719 * encoded.
720 */
721 if (stktable_data_types[data_type].is_array) {
722 unsigned int idx = 0;
723
724 switch (stktable_data_types[data_type].std_type) {
725 case STD_T_SINT: {
726 int data;
727
728 do {
729 data = stktable_data_cast(data_ptr, std_t_sint);
730 intencode(data, &cursor);
731
732 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
733 } while(data_ptr);
734 break;
735 }
736 case STD_T_UINT: {
737 unsigned int data;
738
739 do {
740 data = stktable_data_cast(data_ptr, std_t_uint);
741 intencode(data, &cursor);
742
743 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
744 } while(data_ptr);
745 break;
746 }
747 case STD_T_ULL: {
748 unsigned long long data;
749
750 do {
751 data = stktable_data_cast(data_ptr, std_t_ull);
752 intencode(data, &cursor);
753
754 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
755 } while(data_ptr);
756 break;
757 }
758 case STD_T_FRQP: {
759 struct freq_ctr *frqp;
760
761 do {
762 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
763 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
764 intencode(frqp->curr_ctr, &cursor);
765 intencode(frqp->prev_ctr, &cursor);
766
767 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
768 } while(data_ptr);
769 break;
770 }
771 }
772
773 /* array elements fully encoded
774 * proceed next data_type.
775 */
776 continue;
777 }
Emeric Brun94900952015-06-11 18:25:54 +0200778 switch (stktable_data_types[data_type].std_type) {
779 case STD_T_SINT: {
780 int data;
781
782 data = stktable_data_cast(data_ptr, std_t_sint);
783 intencode(data, &cursor);
784 break;
785 }
786 case STD_T_UINT: {
787 unsigned int data;
788
789 data = stktable_data_cast(data_ptr, std_t_uint);
790 intencode(data, &cursor);
791 break;
792 }
793 case STD_T_ULL: {
794 unsigned long long data;
795
796 data = stktable_data_cast(data_ptr, std_t_ull);
797 intencode(data, &cursor);
798 break;
799 }
800 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200801 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200802
803 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
804 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
805 intencode(frqp->curr_ctr, &cursor);
806 intencode(frqp->prev_ctr, &cursor);
807 break;
808 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200809 case STD_T_DICT: {
810 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200811 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200812 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200813 char *beg, *end;
814 size_t value_len, data_len;
815 struct dcache *dc;
816
817 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100818 if (!de) {
819 /* No entry */
820 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200821 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100822 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200823
824 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200825 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200826 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200827 if (cached_de == &cde.entry) {
828 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
829 break;
830 /* Encode the length of the remaining data -> 1 */
831 intencode(1, &cursor);
832 /* Encode the cache entry ID */
833 intencode(cde.id + 1, &cursor);
834 }
835 else {
836 /* Leave enough room to encode the remaining data length. */
837 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
838 /* Encode the dictionary entry key */
839 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200840 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200841 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200842 intencode(value_len, &end);
843 /* Copy the data */
844 memcpy(end, de->value.key, value_len);
845 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200846 /* Encode the length of the data */
847 data_len = end - beg;
848 intencode(data_len, &cursor);
849 memmove(cursor, beg, data_len);
850 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200851 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200852 break;
853 }
Emeric Brun94900952015-06-11 18:25:54 +0200854 }
855 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200856 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100857 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200858
859 /* Compute datalen */
860 datalen = (cursor - datamsg);
861
862 /* prepare message header */
863 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200864 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200865 cursor = &msg[2];
866 intencode(datalen, &cursor);
867
868 /* move data after header */
869 memmove(cursor, datamsg, datalen);
870
871 /* return header size + data_len */
872 return (cursor - msg) + datalen;
873}
874
875/*
876 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800877 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200878 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
879 * check size)
880 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100881static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200882{
883 int len;
884 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200885 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200886 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200887 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200888 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100889 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100891 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200892 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200893
894 /* Encode data */
895
896 /* encode local id */
897 intencode(st->local_id, &cursor);
898
899 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100900 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200901 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100902 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200903 cursor += len;
904
905 /* encode table type */
906
Emeric Brun530ba382020-06-02 11:17:42 +0200907 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200908
909 /* encode table key size */
910 intencode(st->table->key_size, &cursor);
911
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200912 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200913 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200914 /* encode available known data types in table */
915 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
916 if (st->table->data_ofs[data_type]) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200917 /* stored data types parameters are all linearly encoded
918 * at the end of the 'table definition' message.
919 *
920 * Currently only array data_types and and data_types
921 * using freq_counter base type have parameters:
922 *
923 * - array has always at least one parameter set to the
924 * number of elements.
925 *
926 * - array of base-type freq_counters has an additional
927 * parameter set to the period used to compute those
928 * freq_counters.
929 *
930 * - simple freq counter has a parameter set to the period
931 * used to compute
932 *
933 * A set of parameter for a datatype MUST BE prefixed
934 * by the data-type id itself:
935 * This is useless because the data_types are ordered and
936 * the data_type bitfield already gives the information of
937 * stored types, but it was designed this way when the
938 * push of period parameter was added for freq counters
939 * and we don't want to break the compatibility.
940 *
941 */
942 if (stktable_data_types[data_type].is_array) {
943 /* This is an array type so we first encode
944 * the data_type itself to prefix parameters
945 */
946 intencode(data_type, &chunkq);
947
948 /* We encode the first parameter which is
949 * the number of elements of this array
950 */
951 intencode(st->table->data_nbelem[data_type], &chunkq);
952
Ilya Shipitsin01881082021-08-07 14:41:56 +0500953 /* for array of freq counters, there is an additional
Emeric Brun90a9b672021-06-22 16:09:55 +0200954 * period parameter to encode
955 */
956 if (stktable_data_types[data_type].std_type == STD_T_FRQP)
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200957 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200958 }
Emeric Brun90a9b672021-06-22 16:09:55 +0200959 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
960 /* this datatype is a simple freq counter not part
961 * of an array. We encode the data_type itself
962 * to prefix the 'period' parameter
963 */
964 intencode(data_type, &chunkq);
965 intencode(st->table->data_arg[data_type].u, &chunkq);
966 }
967 /* set the bit corresponding to stored data type */
968 data |= 1ULL << data_type;
Emeric Brun94900952015-06-11 18:25:54 +0200969 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200970 }
971 intencode(data, &cursor);
972
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200973 /* Encode stick-table entries duration. */
974 intencode(st->table->expire, &cursor);
975
976 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200977 chunk->data = chunkq - chunkp;
978 memcpy(cursor, chunk->area, chunk->data);
979 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200980 }
981
Emeric Brunb3971ab2015-05-12 18:49:09 +0200982 /* Compute datalen */
983 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200984
Emeric Brunb3971ab2015-05-12 18:49:09 +0200985 /* prepare message header */
986 msg[0] = PEER_MSG_CLASS_STICKTABLE;
987 msg[1] = PEER_MSG_STKT_DEFINE;
988 cursor = &msg[2];
989 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200990
Emeric Brunb3971ab2015-05-12 18:49:09 +0200991 /* move data after header */
992 memmove(cursor, datamsg, datalen);
993
994 /* return header size + data_len */
995 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200996}
997
Emeric Brunb3971ab2015-05-12 18:49:09 +0200998/*
999 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
1000 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -08001001 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +02001002 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
1003 * check size)
1004 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001005static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001006{
1007 unsigned short datalen;
1008 char *cursor, *datamsg;
1009 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001010 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001011
Frédéric Lécaille39143342019-05-24 14:32:27 +02001012 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001013
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001014 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001015 intencode(st->remote_id, &cursor);
1016 netinteger = htonl(st->last_get);
1017 memcpy(cursor, &netinteger, sizeof(netinteger));
1018 cursor += sizeof(netinteger);
1019
1020 /* Compute datalen */
1021 datalen = (cursor - datamsg);
1022
1023 /* prepare message header */
1024 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +02001025 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001026 cursor = &msg[2];
1027 intencode(datalen, &cursor);
1028
1029 /* move data after header */
1030 memmove(cursor, datamsg, datalen);
1031
1032 /* return header size + data_len */
1033 return (cursor - msg) + datalen;
1034}
Emeric Brun2b920a12010-09-23 18:30:22 +02001035
1036/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001037 * Function to deinit connected peer
1038 */
1039void __peer_session_deinit(struct peer *peer)
1040{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001041 struct stream *s;
1042 struct peers *peers;
1043
1044 if (!peer->appctx)
1045 return;
1046
Christopher Faulet908628c2022-03-25 16:43:49 +01001047 s = __cs_strm(peer->appctx->owner);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001048
1049 peers = strm_fe(s)->parent;
1050 if (!peers)
1051 return;
1052
1053 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02001054 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001055
Willy Tarreau4781b152021-04-06 13:53:36 +02001056 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001057
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001058 flush_dcache(peer);
1059
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001060 /* Re-init current table pointers to force announcement on re-connect */
1061 peer->remote_table = peer->last_local_table = NULL;
1062 peer->appctx = NULL;
1063 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1064 /* unassign current peer for learning */
1065 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
1066 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1067
Emeric Brunccdfbae2021-04-28 12:59:35 +02001068 if (peer->local)
1069 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
1070 else
1071 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001072 /* reschedule a resync */
1073 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1074 }
1075 /* reset teaching and learning flags to 0 */
1076 peer->flags &= PEER_TEACH_RESET;
1077 peer->flags &= PEER_LEARN_RESET;
1078 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1079}
1080
1081/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001082 * Callback to release a session with a peer
1083 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001084static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001085{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001086 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +02001087
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001088 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001090 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +02001091 return;
1092
1093 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001094 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001095 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001096 if (peer->appctx == appctx)
1097 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02001098 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001099 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001100 }
1101}
1102
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001103/* Retrieve the major and minor versions of peers protocol
1104 * announced by a remote peer. <str> is a null-terminated
1105 * string with the following format: "<maj_ver>.<min_ver>".
1106 */
1107static int peer_get_version(const char *str,
1108 unsigned int *maj_ver, unsigned int *min_ver)
1109{
1110 unsigned int majv, minv;
1111 const char *pos, *saved;
1112 const char *end;
1113
1114 saved = pos = str;
1115 end = str + strlen(str);
1116
1117 majv = read_uint(&pos, end);
1118 if (saved == pos || *pos++ != '.')
1119 return -1;
1120
1121 saved = pos;
1122 minv = read_uint(&pos, end);
1123 if (saved == pos || pos != end)
1124 return -1;
1125
1126 *maj_ver = majv;
1127 *min_ver = minv;
1128
1129 return 0;
1130}
Emeric Brun2b920a12010-09-23 18:30:22 +02001131
1132/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001133 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1134 * '\n' character.
1135 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1136 * a line could not be read because the communication channel is closed.
1137 */
1138static inline int peer_getline(struct appctx *appctx)
1139{
Christopher Faulet908628c2022-03-25 16:43:49 +01001140 struct conn_stream *cs = appctx->owner;
Frédéric Lécaillece025572019-01-21 13:38:06 +01001141 int n;
Frédéric Lécaillece025572019-01-21 13:38:06 +01001142
Christopher Faulet908628c2022-03-25 16:43:49 +01001143 n = co_getline(cs_oc(cs), trash.area, trash.size);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001144 if (!n)
1145 return 0;
1146
1147 if (n < 0 || trash.area[n - 1] != '\n') {
1148 appctx->st0 = PEER_SESS_ST_END;
1149 return -1;
1150 }
1151
1152 if (n > 1 && (trash.area[n - 2] == '\r'))
1153 trash.area[n - 2] = 0;
1154 else
1155 trash.area[n - 1] = 0;
1156
Christopher Faulet908628c2022-03-25 16:43:49 +01001157 co_skip(cs_oc(cs), n);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001158
1159 return n;
1160}
1161
1162/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001163 * Send a message after having called <peer_prepare_msg> to build it.
1164 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1165 * Returns -1 if there was not enough room left to send the message,
1166 * any other negative returned value must be considered as an error with an appcxt st0
1167 * returned value equal to PEER_SESS_ST_END.
1168 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001169static inline int peer_send_msg(struct appctx *appctx,
1170 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1171 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001172{
1173 int ret, msglen;
Christopher Faulet908628c2022-03-25 16:43:49 +01001174 struct conn_stream *cs = appctx->owner;
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001175
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001176 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001177 if (!msglen) {
1178 /* internal error: message does not fit in trash */
1179 appctx->st0 = PEER_SESS_ST_END;
1180 return 0;
1181 }
1182
1183 /* message to buffer */
Christopher Faulet908628c2022-03-25 16:43:49 +01001184 ret = ci_putblk(cs_ic(cs), trash.area, msglen);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001185 if (ret <= 0) {
1186 if (ret == -1) {
1187 /* No more write possible */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001188 cs_rx_room_blk(cs);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001189 return -1;
1190 }
1191 appctx->st0 = PEER_SESS_ST_END;
1192 }
1193
1194 return ret;
1195}
1196
1197/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001198 * Send a hello message.
1199 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1200 * Returns -1 if there was not enough room left to send the message,
1201 * any other negative returned value must be considered as an error with an appcxt st0
1202 * returned value equal to PEER_SESS_ST_END.
1203 */
1204static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1205{
1206 struct peer_prep_params p = {
1207 .hello.peer = peer,
1208 };
1209
1210 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1211}
1212
1213/*
1214 * Send a success peer handshake status message.
1215 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1216 * Returns -1 if there was not enough room left to send the message,
1217 * any other negative returned value must be considered as an error with an appcxt st0
1218 * returned value equal to PEER_SESS_ST_END.
1219 */
1220static inline int peer_send_status_successmsg(struct appctx *appctx)
1221{
1222 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1223}
1224
1225/*
1226 * Send a peer handshake status error message.
1227 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1228 * Returns -1 if there was not enough room left to send the message,
1229 * any other negative returned value must be considered as an error with an appcxt st0
1230 * returned value equal to PEER_SESS_ST_END.
1231 */
1232static inline int peer_send_status_errormsg(struct appctx *appctx)
1233{
1234 struct peer_prep_params p = {
1235 .error_status.st1 = appctx->st1,
1236 };
1237
1238 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1239}
1240
1241/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001242 * Send a stick-table switch message.
1243 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1244 * Returns -1 if there was not enough room left to send the message,
1245 * any other negative returned value must be considered as an error with an appcxt st0
1246 * returned value equal to PEER_SESS_ST_END.
1247 */
1248static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1249{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001250 struct peer_prep_params p = {
1251 .swtch.shared_table = st,
1252 };
1253
1254 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001255}
1256
1257/*
1258 * Send a stick-table update acknowledgement message.
1259 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1260 * Returns -1 if there was not enough room left to send the message,
1261 * any other negative returned value must be considered as an error with an appcxt st0
1262 * returned value equal to PEER_SESS_ST_END.
1263 */
1264static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1265{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001266 struct peer_prep_params p = {
1267 .ack.shared_table = st,
1268 };
1269
1270 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001271}
1272
1273/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001274 * Send a stick-table update message.
1275 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1276 * Returns -1 if there was not enough room left to send the message,
1277 * any other negative returned value must be considered as an error with an appcxt st0
1278 * returned value equal to PEER_SESS_ST_END.
1279 */
1280static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1281 unsigned int updateid, int use_identifier, int use_timed)
1282{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001283 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001284 .updt = {
1285 .stksess = ts,
1286 .shared_table = st,
1287 .updateid = updateid,
1288 .use_identifier = use_identifier,
1289 .use_timed = use_timed,
1290 .peer = appctx->ctx.peers.ptr,
1291 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001292 };
1293
1294 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001295}
1296
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001297/*
1298 * Build a peer protocol control class message.
1299 * Returns the number of written bytes used to build the message if succeeded,
1300 * 0 if not.
1301 */
1302static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1303{
1304 if (size < sizeof p->control.head)
1305 return 0;
1306
1307 msg[0] = p->control.head[0];
1308 msg[1] = p->control.head[1];
1309
1310 return 2;
1311}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001312
1313/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001314 * Send a stick-table synchronization request message.
1315 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1316 * Returns -1 if there was not enough room left to send the message,
1317 * any other negative returned value must be considered as an error with an appctx st0
1318 * returned value equal to PEER_SESS_ST_END.
1319 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001320static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1321 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001322{
1323 struct peer_prep_params p = {
1324 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1325 };
1326
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001327 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1328 NULL, &p.control.head[1], peers->local->id, peer->id);
1329
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001330 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1331}
1332
1333/*
1334 * Send a stick-table synchronization confirmation message.
1335 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1336 * Returns -1 if there was not enough room left to send the message,
1337 * any other negative returned value must be considered as an error with an appctx st0
1338 * returned value equal to PEER_SESS_ST_END.
1339 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001340static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1341 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001342{
1343 struct peer_prep_params p = {
1344 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1345 };
1346
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001347 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1348 NULL, &p.control.head[1], peers->local->id, peer->id);
1349
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001350 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1351}
1352
1353/*
1354 * Send a stick-table synchronization finished message.
1355 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1356 * Returns -1 if there was not enough room left to send the message,
1357 * any other negative returned value must be considered as an error with an appctx st0
1358 * returned value equal to PEER_SESS_ST_END.
1359 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001360static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1361 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001362{
1363 struct peer_prep_params p = {
1364 .control.head = { PEER_MSG_CLASS_CONTROL, },
1365 };
1366
Emeric Brun70de43b2020-03-16 10:51:01 +01001367 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001368 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1369
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001370 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1371 NULL, &p.control.head[1], peers->local->id, peer->id);
1372
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001373 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1374}
1375
1376/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001377 * Send a heartbeat message.
1378 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1379 * Returns -1 if there was not enough room left to send the message,
1380 * any other negative returned value must be considered as an error with an appctx st0
1381 * returned value equal to PEER_SESS_ST_END.
1382 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001383static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1384 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001385{
1386 struct peer_prep_params p = {
1387 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1388 };
1389
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001390 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1391 NULL, &p.control.head[1], peers->local->id, peer->id);
1392
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001393 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1394}
1395
1396/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001397 * Build a peer protocol error class message.
1398 * Returns the number of written bytes used to build the message if succeeded,
1399 * 0 if not.
1400 */
1401static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1402{
1403 if (size < sizeof p->error.head)
1404 return 0;
1405
1406 msg[0] = p->error.head[0];
1407 msg[1] = p->error.head[1];
1408
1409 return 2;
1410}
1411
1412/*
1413 * Send a "size limit reached" error message.
1414 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1415 * Returns -1 if there was not enough room left to send the message,
1416 * any other negative returned value must be considered as an error with an appctx st0
1417 * returned value equal to PEER_SESS_ST_END.
1418 */
1419static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1420{
1421 struct peer_prep_params p = {
1422 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1423 };
1424
1425 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1426}
1427
1428/*
1429 * Send a "peer protocol" error message.
1430 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1431 * Returns -1 if there was not enough room left to send the message,
1432 * any other negative returned value must be considered as an error with an appctx st0
1433 * returned value equal to PEER_SESS_ST_END.
1434 */
1435static inline int peer_send_error_protomsg(struct appctx *appctx)
1436{
1437 struct peer_prep_params p = {
1438 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1439 };
1440
1441 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1442}
1443
1444/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001445 * Function used to lookup for recent stick-table updates associated with
1446 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1447 */
1448static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1449{
1450 struct eb32_node *eb;
1451
1452 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1453 if (!eb) {
1454 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001455 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001456 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1457 return NULL;
1458 }
1459 }
1460
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001461 /* if distance between the last pushed and the retrieved key
1462 * is greater than the distance last_pushed and the local_update
1463 * this means we are beyond localupdate.
1464 */
1465 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001466 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1467 return NULL;
1468 }
1469
1470 return eb32_entry(eb, struct stksess, upd);
1471}
1472
1473/*
1474 * Function used to lookup for recent stick-table updates associated with
1475 * <st> shared stick-table during teach state 1 step.
1476 */
1477static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1478{
1479 struct eb32_node *eb;
1480
1481 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1482 if (!eb) {
1483 st->flags |= SHTABLE_F_TEACH_STAGE1;
1484 eb = eb32_first(&st->table->updates);
1485 if (eb)
1486 st->last_pushed = eb->key - 1;
1487 return NULL;
1488 }
1489
1490 return eb32_entry(eb, struct stksess, upd);
1491}
1492
1493/*
1494 * Function used to lookup for recent stick-table updates associated with
1495 * <st> shared stick-table during teach state 2 step.
1496 */
1497static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1498{
1499 struct eb32_node *eb;
1500
1501 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1502 if (!eb || eb->key > st->teaching_origin) {
1503 st->flags |= SHTABLE_F_TEACH_STAGE2;
1504 return NULL;
1505 }
1506
1507 return eb32_entry(eb, struct stksess, upd);
1508}
1509
1510/*
1511 * Generic function to emit update messages for <st> stick-table when a lesson must
1512 * be taught to the peer <p>.
1513 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1514 * this function, 0 if not.
1515 *
1516 * This function temporary unlock/lock <st> when it sends stick-table updates or
1517 * when decrementing its refcount in case of any error when it sends this updates.
1518 *
1519 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1520 * Returns -1 if there was not enough room left to send the message,
1521 * any other negative returned value must be considered as an error with an appcxt st0
1522 * returned value equal to PEER_SESS_ST_END.
1523 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1524 * unlocked if not already locked when entering this function.
1525 */
1526static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1527 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1528 struct shared_table *st, int locked)
1529{
1530 int ret, new_pushed, use_timed;
1531
1532 ret = 1;
1533 use_timed = 0;
1534 if (st != p->last_local_table) {
1535 ret = peer_send_switchmsg(st, appctx);
1536 if (ret <= 0)
1537 return ret;
1538
1539 p->last_local_table = st;
1540 }
1541
1542 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1543 use_timed = !(p->flags & PEER_F_DWNGRD);
1544
1545 /* We force new pushed to 1 to force identifier in update message */
1546 new_pushed = 1;
1547
1548 if (!locked)
1549 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1550
1551 while (1) {
1552 struct stksess *ts;
1553 unsigned updateid;
1554
1555 /* push local updates */
1556 ts = peer_stksess_lookup(st);
1557 if (!ts)
1558 break;
1559
1560 updateid = ts->upd.key;
1561 ts->ref_cnt++;
1562 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1563
1564 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1565 if (ret <= 0) {
1566 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1567 ts->ref_cnt--;
1568 if (!locked)
1569 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1570 return ret;
1571 }
1572
1573 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1574 ts->ref_cnt--;
1575 st->last_pushed = updateid;
1576
1577 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1578 (int)(st->last_pushed - st->table->commitupdate) > 0)
1579 st->table->commitupdate = st->last_pushed;
1580
1581 /* identifier may not needed in next update message */
1582 new_pushed = 0;
1583 }
1584
1585 out:
1586 if (!locked)
1587 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1588 return 1;
1589}
1590
1591/*
1592 * Function to emit update messages for <st> stick-table when a lesson must
1593 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1594 *
1595 * Note that <st> shared stick-table is locked when calling this function.
1596 *
1597 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1598 * Returns -1 if there was not enough room left to send the message,
1599 * any other negative returned value must be considered as an error with an appcxt st0
1600 * returned value equal to PEER_SESS_ST_END.
1601 */
1602static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1603 struct shared_table *st)
1604{
1605 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1606}
1607
1608/*
1609 * Function to emit update messages for <st> stick-table when a lesson must
1610 * be taught to the peer <p> during teach state 1 step.
1611 *
1612 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1613 * Returns -1 if there was not enough room left to send the message,
1614 * any other negative returned value must be considered as an error with an appcxt st0
1615 * returned value equal to PEER_SESS_ST_END.
1616 */
1617static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1618 struct shared_table *st)
1619{
1620 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1621}
1622
1623/*
1624 * Function to emit update messages for <st> stick-table when a lesson must
1625 * be taught to the peer <p> during teach state 1 step.
1626 *
1627 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1628 * Returns -1 if there was not enough room left to send the message,
1629 * any other negative returned value must be considered as an error with an appcxt st0
1630 * returned value equal to PEER_SESS_ST_END.
1631 */
1632static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1633 struct shared_table *st)
1634{
1635 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1636}
1637
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001638
1639/*
1640 * Function used to parse a stick-table update message after it has been received
1641 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1642 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1643 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1644 * was encountered.
1645 * <exp> must be set if the stick-table entry expires.
1646 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001647 * 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 +01001648 * update ID.
1649 * <totl> is the length of the stick-table update message computed upon receipt.
1650 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001651static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1652 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001653{
Christopher Faulet908628c2022-03-25 16:43:49 +01001654 struct conn_stream *cs = appctx->owner;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001655 struct shared_table *st = p->remote_table;
1656 struct stksess *ts, *newts;
1657 uint32_t update;
1658 int expire;
1659 unsigned int data_type;
1660 void *data_ptr;
1661
Frédéric Lécailled8659352020-11-10 16:18:03 +01001662 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001663 /* Here we have data message */
1664 if (!st)
1665 goto ignore_msg;
1666
1667 expire = MS_TO_TICKS(st->table->expire);
1668
1669 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001670 if (msg_len < sizeof(update)) {
1671 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001672 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001673 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001674
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001675 memcpy(&update, *msg_cur, sizeof(update));
1676 *msg_cur += sizeof(update);
1677 st->last_get = htonl(update);
1678 }
1679 else {
1680 st->last_get++;
1681 }
1682
1683 if (exp) {
1684 size_t expire_sz = sizeof expire;
1685
Frédéric Lécailled8659352020-11-10 16:18:03 +01001686 if (*msg_cur + expire_sz > msg_end) {
1687 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1688 NULL, p, *msg_cur);
1689 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1690 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001691 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001692 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001693
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001694 memcpy(&expire, *msg_cur, expire_sz);
1695 *msg_cur += expire_sz;
1696 expire = ntohl(expire);
1697 }
1698
1699 newts = stksess_new(st->table, NULL);
1700 if (!newts)
1701 goto ignore_msg;
1702
1703 if (st->table->type == SMP_T_STR) {
1704 unsigned int to_read, to_store;
1705
1706 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001707 if (!*msg_cur) {
1708 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001709 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001710 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001711
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001712 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001713 if (*msg_cur + to_store > msg_end) {
1714 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1715 NULL, p, *msg_cur);
1716 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1717 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001718 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001719 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001720
1721 memcpy(newts->key.key, *msg_cur, to_store);
1722 newts->key.key[to_store] = 0;
1723 *msg_cur += to_read;
1724 }
1725 else if (st->table->type == SMP_T_SINT) {
1726 unsigned int netinteger;
1727
Frédéric Lécailled8659352020-11-10 16:18:03 +01001728 if (*msg_cur + sizeof(netinteger) > msg_end) {
1729 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1730 NULL, p, *msg_cur);
1731 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1732 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001733 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001734 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001735
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001736 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1737 netinteger = ntohl(netinteger);
1738 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1739 *msg_cur += sizeof(netinteger);
1740 }
1741 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001742 if (*msg_cur + st->table->key_size > msg_end) {
1743 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1744 NULL, p, *msg_cur);
1745 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1746 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001747 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001748 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001749
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001750 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1751 *msg_cur += st->table->key_size;
1752 }
1753
1754 /* lookup for existing entry */
1755 ts = stktable_set_entry(st->table, newts);
1756 if (ts != newts) {
1757 stksess_free(st->table, newts);
1758 newts = NULL;
1759 }
1760
1761 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1762
1763 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001764 uint64_t decoded_int;
Emeric Brun90a9b672021-06-22 16:09:55 +02001765 unsigned int idx;
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001766 int ignore;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001767
Emeric Brun08b0f672021-07-01 18:54:05 +02001768 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001769 continue;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001770
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001771 ignore = stktable_data_types[data_type].is_local;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001772
Emeric Brun90a9b672021-06-22 16:09:55 +02001773 if (stktable_data_types[data_type].is_array) {
1774 /* in case of array all elements
1775 * use the same std_type and they
1776 * are linearly encoded.
1777 * The number of elements was provided
1778 * by table definition message
1779 */
1780 switch (stktable_data_types[data_type].std_type) {
1781 case STD_T_SINT:
1782 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1783 decoded_int = intdecode(msg_cur, msg_end);
1784 if (!*msg_cur) {
1785 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1786 goto malformed_unlock;
1787 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001788
Emeric Brun90a9b672021-06-22 16:09:55 +02001789 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001790 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001791 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
1792 }
1793 break;
1794 case STD_T_UINT:
1795 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1796 decoded_int = intdecode(msg_cur, msg_end);
1797 if (!*msg_cur) {
1798 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1799 goto malformed_unlock;
1800 }
1801
1802 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001803 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001804 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
1805 }
1806 break;
1807 case STD_T_ULL:
1808 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1809 decoded_int = intdecode(msg_cur, msg_end);
1810 if (!*msg_cur) {
1811 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1812 goto malformed_unlock;
1813 }
1814
1815 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001816 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001817 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
1818 }
1819 break;
1820 case STD_T_FRQP:
1821 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1822 struct freq_ctr data;
1823
1824 /* First bit is reserved for the freq_ctr lock
1825 * Note: here we're still protected by the stksess lock
1826 * so we don't need to update the update the freq_ctr
1827 * using its internal lock.
1828 */
1829
1830 decoded_int = intdecode(msg_cur, msg_end);
1831 if (!*msg_cur) {
1832 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1833 goto malformed_unlock;
1834 }
1835
1836 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
1837 data.curr_ctr = intdecode(msg_cur, msg_end);
1838 if (!*msg_cur) {
1839 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1840 goto malformed_unlock;
1841 }
1842
1843 data.prev_ctr = intdecode(msg_cur, msg_end);
1844 if (!*msg_cur) {
1845 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1846 goto malformed_unlock;
1847 }
1848
1849 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001850 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001851 stktable_data_cast(data_ptr, std_t_frqp) = data;
1852 }
1853 break;
1854 }
1855
1856 /* array is fully decoded
1857 * proceed next data_type.
1858 */
1859 continue;
1860 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001861 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001862 if (!*msg_cur) {
1863 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001864 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001865 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001866
Willy Tarreau1e82a142019-01-29 11:08:06 +01001867 switch (stktable_data_types[data_type].std_type) {
1868 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001869 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001870 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001871 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001872 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001873
Willy Tarreau1e82a142019-01-29 11:08:06 +01001874 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001875 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001876 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001877 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001878 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001879
Willy Tarreau1e82a142019-01-29 11:08:06 +01001880 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001881 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001882 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001883 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001884 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001885
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001886 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001887 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001888
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001889 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001890 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001891 so we don't need to update the update the freq_ctr
Emeric Brun90a9b672021-06-22 16:09:55 +02001892 using its internal lock.
1893 */
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001894
Willy Tarreau1e82a142019-01-29 11:08:06 +01001895 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001896 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001897 if (!*msg_cur) {
1898 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001899 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001900 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001901
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001902 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001903 if (!*msg_cur) {
1904 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001905 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001906 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001907
1908 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001909 if (data_ptr && !ignore)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001910 stktable_data_cast(data_ptr, std_t_frqp) = data;
1911 break;
1912 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001913 case STD_T_DICT: {
1914 struct buffer *chunk;
1915 size_t data_len, value_len;
1916 unsigned int id;
1917 struct dict_entry *de;
1918 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001919 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001920
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001921 if (!decoded_int) {
1922 /* No entry. */
1923 break;
1924 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001925 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001926 if (*msg_cur + data_len > msg_end) {
1927 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1928 NULL, p, *msg_cur);
1929 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1930 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001931 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001932 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001933
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001934 /* Compute the end of the current data, <msg_end> being at the end of
1935 * the entire message.
1936 */
1937 end = *msg_cur + data_len;
1938 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001939 if (!*msg_cur || !id) {
1940 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1941 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001942 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001943 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001944
1945 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001946 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001947 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001948 if (id > dc->max_entries) {
1949 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1950 NULL, p, NULL, &id);
1951 goto malformed_unlock;
1952 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001953 /* IDs sent over the network are numbered from 1. */
1954 de = dc->rx[id - 1].de;
1955 }
1956 else {
1957 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001958 value_len = intdecode(msg_cur, end);
1959 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01001960 unlikely(value_len + 1 >= chunk->size)) {
1961 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1962 NULL, p, *msg_cur, &value_len);
1963 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1964 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001965 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001966 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001967
1968 chunk_memcpy(chunk, *msg_cur, value_len);
1969 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001970 *msg_cur += value_len;
1971
Thayne McCombs92149f92020-11-20 01:28:26 -07001972 de = dict_insert(&server_key_dict, chunk->area);
1973 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001974 dc->rx[id - 1].de = de;
1975 }
1976 if (de) {
1977 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001978 if (data_ptr && !ignore) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001979 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001980 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07001981 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001982 }
1983 break;
1984 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001985 }
1986 }
1987 /* Force new expiration */
1988 ts->expire = tick_add(now_ms, expire);
1989
1990 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1991 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001992 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001993 return 1;
1994
1995 ignore_msg:
1996 /* skip consumed message */
Christopher Faulet908628c2022-03-25 16:43:49 +01001997 co_skip(cs_oc(cs), totl);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001998 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001999 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01002000
2001 malformed_unlock:
2002 /* malformed message */
2003 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2004 stktable_touch_remote(st->table, ts, 1);
2005 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002006 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002007 return 0;
2008
2009 malformed_free_newts:
2010 /* malformed message */
2011 stksess_free(st->table, newts);
2012 malformed_exit:
2013 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002014 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002015 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002016}
2017
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01002018/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002019 * Function used to parse a stick-table update acknowledgement message after it
2020 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
2021 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2022 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2023 * was encountered.
2024 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2025 */
2026static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
2027 char **msg_cur, char *msg_end)
2028{
2029 /* ack message */
2030 uint32_t table_id ;
2031 uint32_t update;
2032 struct shared_table *st;
2033
Emeric Brunb0d60be2021-03-04 10:27:10 +01002034 /* ignore ack during teaching process */
2035 if (p->flags & PEER_F_TEACH_PROCESS)
2036 return 1;
2037
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002038 table_id = intdecode(msg_cur, msg_end);
2039 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
2040 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002041
2042 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
2043 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002044 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2045 return 0;
2046 }
2047
2048 memcpy(&update, *msg_cur, sizeof(update));
2049 update = ntohl(update);
2050
2051 for (st = p->tables; st; st = st->next) {
2052 if (st->local_id == table_id) {
2053 st->update = update;
2054 break;
2055 }
2056 }
2057
2058 return 1;
2059}
2060
2061/*
2062 * Function used to parse a stick-table switch message after it has been received
2063 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2064 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2065 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2066 * was encountered.
2067 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2068 */
2069static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
2070 char **msg_cur, char *msg_end)
2071{
2072 struct shared_table *st;
2073 int table_id;
2074
2075 table_id = intdecode(msg_cur, msg_end);
2076 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002077 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002078 /* malformed message */
2079 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2080 return 0;
2081 }
2082
2083 p->remote_table = NULL;
2084 for (st = p->tables; st; st = st->next) {
2085 if (st->remote_id == table_id) {
2086 p->remote_table = st;
2087 break;
2088 }
2089 }
2090
2091 return 1;
2092}
2093
2094/*
2095 * Function used to parse a stick-table definition message after it has been received
2096 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2097 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2098 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2099 * was encountered.
2100 * <totl> is the length of the stick-table update message computed upon receipt.
2101 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2102 */
2103static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
2104 char **msg_cur, char *msg_end, int totl)
2105{
Christopher Faulet908628c2022-03-25 16:43:49 +01002106 struct conn_stream *cs = appctx->owner;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002107 int table_id_len;
2108 struct shared_table *st;
2109 int table_type;
2110 int table_keylen;
2111 int table_id;
2112 uint64_t table_data;
2113
2114 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002115 if (!*msg_cur) {
2116 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002117 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002118 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002119
2120 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002121 if (!*msg_cur) {
2122 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002123 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002124 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002125
2126 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002127 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
2128 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002129 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002130 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002131
2132 for (st = p->tables; st; st = st->next) {
2133 /* Reset IDs */
2134 if (st->remote_id == table_id)
2135 st->remote_id = 0;
2136
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01002137 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
2138 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002139 p->remote_table = st;
2140 }
2141
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002142 if (!p->remote_table) {
2143 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002144 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002145 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002146
2147 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002148 if (*msg_cur >= msg_end) {
2149 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002150 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002151 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002152
2153 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002154 if (!*msg_cur) {
2155 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002156 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002157 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002158
2159 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002160 if (!*msg_cur) {
2161 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002162 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002163 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002164
2165 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002166 if (!*msg_cur) {
2167 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002168 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002169 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002170
Emeric Brun530ba382020-06-02 11:17:42 +02002171 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002172 || p->remote_table->table->key_size != table_keylen) {
2173 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002174 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002175 goto ignore_msg;
2176 }
2177
Ilya Shipitsin01881082021-08-07 14:41:56 +05002178 /* Check if there there is the additional expire data */
Emeric Brun90a9b672021-06-22 16:09:55 +02002179 intdecode(msg_cur, msg_end);
2180 if (*msg_cur) {
2181 uint64_t data_type;
2182 uint64_t type;
2183
2184 /* This define contains the expire data so we consider
2185 * it also contain all data_types parameters.
2186 */
2187 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2188 if (table_data & (1ULL << data_type)) {
2189 if (stktable_data_types[data_type].is_array) {
2190 /* This should be an array
2191 * so we parse the data_type prefix
2192 * because we must have parameters.
2193 */
2194 type = intdecode(msg_cur, msg_end);
2195 if (!*msg_cur) {
2196 p->remote_table = NULL;
2197 TRACE_PROTO("missing meta data for array", PEERS_EV_DEFMSG, NULL, p);
2198 goto ignore_msg;
2199 }
2200
2201 /* check if the data_type match the current from the bitfield */
2202 if (type != data_type) {
2203 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002204 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002205 goto ignore_msg;
2206 }
2207
2208 /* decode the nbelem of the array */
2209 p->remote_table->remote_data_nbelem[type] = intdecode(msg_cur, msg_end);
2210 if (!*msg_cur) {
2211 p->remote_table = NULL;
2212 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2213 goto ignore_msg;
2214 }
2215
2216 /* if it is an array of frqp, we must also have the period to decode */
2217 if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2218 intdecode(msg_cur, msg_end);
2219 if (!*msg_cur) {
2220 p->remote_table = NULL;
2221 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2222 goto ignore_msg;
2223 }
2224 }
2225 }
2226 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2227 /* This should be a std freq counter data_type
2228 * so we parse the data_type prefix
2229 * because we must have parameters.
2230 */
2231 type = intdecode(msg_cur, msg_end);
2232 if (!*msg_cur) {
2233 p->remote_table = NULL;
2234 TRACE_PROTO("missing meta data for frqp", PEERS_EV_DEFMSG, NULL, p);
2235 goto ignore_msg;
2236 }
2237
2238 /* check if the data_type match the current from the bitfield */
2239 if (type != data_type) {
2240 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002241 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002242 goto ignore_msg;
2243 }
2244
2245 /* decode the period */
2246 intdecode(msg_cur, msg_end);
2247 if (!*msg_cur) {
2248 p->remote_table = NULL;
2249 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2250 goto ignore_msg;
2251 }
2252 }
2253 }
2254 }
2255 }
2256 else {
2257 uint64_t data_type;
2258
2259 /* There is not additional data but
2260 * array size parameter is mandatory to parse array
2261 * so we consider an error if an array data_type is define
2262 * but there is no additional data.
2263 */
2264 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2265 if (table_data & (1ULL << data_type)) {
2266 if (stktable_data_types[data_type].is_array) {
2267 p->remote_table = NULL;
2268 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2269 goto ignore_msg;
2270 }
2271 }
2272 }
2273 }
2274
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002275 p->remote_table->remote_data = table_data;
2276 p->remote_table->remote_id = table_id;
2277 return 1;
2278
2279 ignore_msg:
Christopher Faulet908628c2022-03-25 16:43:49 +01002280 co_skip(cs_oc(cs), totl);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002281 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01002282
2283 malformed_exit:
2284 /* malformed message */
2285 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2286 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002287}
2288
2289/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002290 * Receive a stick-table message or pre-parse any other message.
2291 * The message's header will be sent into <msg_head> which must be at least
2292 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
2293 * The first two bytes are always read, and the rest is only read if the
2294 * first bytes indicate a stick-table message. If the message is a stick-table
2295 * message, the varint is decoded and the equivalent number of bytes will be
2296 * copied into the trash at trash.area. <totl> is incremented by the number of
2297 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002298 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
2299 * -1 if there was an error updating the appctx state st0 accordingly.
2300 */
2301static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2302 uint32_t *msg_len, int *totl)
2303{
2304 int reql;
Christopher Faulet908628c2022-03-25 16:43:49 +01002305 struct conn_stream *cs = appctx->owner;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002306 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002307
Christopher Faulet908628c2022-03-25 16:43:49 +01002308 reql = co_getblk(cs_oc(cs), msg_head, 2 * sizeof(char), *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002309 if (reql <= 0) /* closed or EOL not found */
2310 goto incomplete;
2311
2312 *totl += reql;
2313
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002314 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002315 return 1;
2316
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002317 /* This is a stick-table message, let's go on */
2318
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002319 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002320 msg_head += *totl;
2321 msg_head_sz -= *totl;
Christopher Faulet908628c2022-03-25 16:43:49 +01002322 reql = co_data(cs_oc(cs)) - *totl;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002323 if (reql > msg_head_sz)
2324 reql = msg_head_sz;
2325
Christopher Faulet908628c2022-03-25 16:43:49 +01002326 reql = co_getblk(cs_oc(cs), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002327 if (reql <= 0) /* closed */
2328 goto incomplete;
2329
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002330 cur = msg_head;
2331 *msg_len = intdecode(&cur, cur + reql);
2332 if (!cur) {
2333 /* the number is truncated, did we read enough ? */
2334 if (reql < msg_head_sz)
2335 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002336
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002337 /* malformed message */
2338 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2339 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2340 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002341 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002342 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002343
2344 /* Read message content */
2345 if (*msg_len) {
2346 if (*msg_len > trash.size) {
2347 /* Status code is not success, abort */
2348 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2349 return -1;
2350 }
2351
Christopher Faulet908628c2022-03-25 16:43:49 +01002352 reql = co_getblk(cs_oc(cs), trash.area, *msg_len, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002353 if (reql <= 0) /* closed */
2354 goto incomplete;
2355 *totl += reql;
2356 }
2357
2358 return 1;
2359
2360 incomplete:
Christopher Faulet908628c2022-03-25 16:43:49 +01002361 if (reql < 0 || (cs_oc(cs)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002362 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002363 appctx->st0 = PEER_SESS_ST_END;
2364 return -1;
2365 }
2366
2367 return 0;
2368}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002369
2370/*
2371 * Treat the awaited message with <msg_head> as header.*
2372 * Return 1 if succeeded, 0 if not.
2373 */
2374static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2375 char **msg_cur, char *msg_end, int msg_len, int totl)
2376{
Christopher Faulet908628c2022-03-25 16:43:49 +01002377 struct stream *s = __cs_strm(appctx->owner);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002378 struct peers *peers = strm_fe(s)->parent;
2379
2380 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2381 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2382 struct shared_table *st;
2383 /* Reset message: remote need resync */
2384
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002385 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2386 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002387 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002388 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002389 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002390 st->flags = 0;
2391 }
2392
2393 /* reset teaching flags to 0 */
2394 peer->flags &= PEER_TEACH_RESET;
2395
2396 /* flag to start to teach lesson */
2397 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002398 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002399 }
2400 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002401 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2402 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002403 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2404 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2405 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2406 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002407 if (peer->local)
2408 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2409 else
2410 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002411 }
2412 peer->confirm++;
2413 }
2414 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002415 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2416 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002417 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2418 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2419 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2420
Emeric Brunccdfbae2021-04-28 12:59:35 +02002421 if (peer->local)
2422 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2423 else
2424 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002425 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002426 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002427 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2428 }
2429 peer->confirm++;
2430 }
2431 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2432 struct shared_table *st;
2433
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002434 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2435 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002436 /* If stopping state */
2437 if (stopping) {
2438 /* Close session, push resync no more needed */
2439 peer->flags |= PEER_F_TEACH_COMPLETE;
2440 appctx->st0 = PEER_SESS_ST_END;
2441 return 0;
2442 }
2443 for (st = peer->tables; st; st = st->next) {
2444 st->update = st->last_pushed = st->teaching_origin;
2445 st->flags = 0;
2446 }
2447
2448 /* reset teaching flags to 0 */
2449 peer->flags &= PEER_TEACH_RESET;
2450 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002451 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002452 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2453 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002454 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002455 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002456 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002457 }
2458 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2459 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2460 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2461 return 0;
2462 }
2463 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2464 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2465 return 0;
2466 }
2467 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2468 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2469 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2470 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2471 int update, expire;
2472
2473 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2474 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2475 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2476 msg_cur, msg_end, msg_len, totl))
2477 return 0;
2478
2479 }
2480 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2481 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2482 return 0;
2483 }
2484 }
2485 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2486 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2487 return 0;
2488 }
2489
2490 return 1;
2491}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002492
2493
2494/*
2495 * Send any message to <peer> peer.
2496 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002497 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002498 * to a peer state change (from the peer I/O handler point of view).
2499 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002500static inline int peer_send_msgs(struct appctx *appctx,
2501 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002502{
2503 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002504
2505 /* Need to request a resync */
2506 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2507 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2508 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2509
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002510 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002511 if (repl <= 0)
2512 return repl;
2513
2514 peers->flags |= PEERS_F_RESYNC_PROCESS;
2515 }
2516
2517 /* Nothing to read, now we start to write */
2518 if (peer->tables) {
2519 struct shared_table *st;
2520 struct shared_table *last_local_table;
2521
2522 last_local_table = peer->last_local_table;
2523 if (!last_local_table)
2524 last_local_table = peer->tables;
2525 st = last_local_table->next;
2526
2527 while (1) {
2528 if (!st)
2529 st = peer->tables;
2530
2531 /* It remains some updates to ack */
2532 if (st->last_get != st->last_acked) {
2533 repl = peer_send_ackmsg(st, appctx);
2534 if (repl <= 0)
2535 return repl;
2536
2537 st->last_acked = st->last_get;
2538 }
2539
2540 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2541 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2542 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002543 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002544
2545 repl = peer_send_teach_process_msgs(appctx, peer, st);
2546 if (repl <= 0) {
2547 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2548 return repl;
2549 }
2550 }
2551 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2552 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002553 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002554 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2555 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2556 if (repl <= 0)
2557 return repl;
2558 }
2559
2560 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2561 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2562 if (repl <= 0)
2563 return repl;
2564 }
2565 }
2566
2567 if (st == last_local_table)
2568 break;
2569 st = st->next;
2570 }
2571 }
2572
2573 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002574 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002575 if (repl <= 0)
2576 return repl;
2577
2578 /* flag finished message sent */
2579 peer->flags |= PEER_F_TEACH_FINISHED;
2580 }
2581
2582 /* Confirm finished or partial messages */
2583 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002584 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002585 if (repl <= 0)
2586 return repl;
2587
2588 peer->confirm--;
2589 }
2590
2591 return 1;
2592}
2593
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002594/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002595 * Read and parse a first line of a "hello" peer protocol message.
2596 * Returns 0 if could not read a line, -1 if there was a read error or
2597 * the line is malformed, 1 if succeeded.
2598 */
2599static inline int peer_getline_version(struct appctx *appctx,
2600 unsigned int *maj_ver, unsigned int *min_ver)
2601{
2602 int reql;
2603
2604 reql = peer_getline(appctx);
2605 if (!reql)
2606 return 0;
2607
2608 if (reql < 0)
2609 return -1;
2610
2611 /* test protocol */
2612 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2613 appctx->st0 = PEER_SESS_ST_EXIT;
2614 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2615 return -1;
2616 }
2617 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2618 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2619 appctx->st0 = PEER_SESS_ST_EXIT;
2620 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2621 return -1;
2622 }
2623
2624 return 1;
2625}
2626
2627/*
2628 * Read and parse a second line of a "hello" peer protocol message.
2629 * Returns 0 if could not read a line, -1 if there was a read error or
2630 * the line is malformed, 1 if succeeded.
2631 */
2632static inline int peer_getline_host(struct appctx *appctx)
2633{
2634 int reql;
2635
2636 reql = peer_getline(appctx);
2637 if (!reql)
2638 return 0;
2639
2640 if (reql < 0)
2641 return -1;
2642
2643 /* test hostname match */
2644 if (strcmp(localpeer, trash.area) != 0) {
2645 appctx->st0 = PEER_SESS_ST_EXIT;
2646 appctx->st1 = PEER_SESS_SC_ERRHOST;
2647 return -1;
2648 }
2649
2650 return 1;
2651}
2652
2653/*
2654 * Read and parse a last line of a "hello" peer protocol message.
2655 * Returns 0 if could not read a character, -1 if there was a read error or
2656 * the line is malformed, 1 if succeeded.
2657 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2658 */
2659static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2660{
2661 char *p;
2662 int reql;
2663 struct peer *peer;
Christopher Faulet908628c2022-03-25 16:43:49 +01002664 struct stream *s = __cs_strm(appctx->owner);
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002665 struct peers *peers = strm_fe(s)->parent;
2666
2667 reql = peer_getline(appctx);
2668 if (!reql)
2669 return 0;
2670
2671 if (reql < 0)
2672 return -1;
2673
2674 /* parse line "<peer name> <pid> <relative_pid>" */
2675 p = strchr(trash.area, ' ');
2676 if (!p) {
2677 appctx->st0 = PEER_SESS_ST_EXIT;
2678 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2679 return -1;
2680 }
2681 *p = 0;
2682
2683 /* lookup known peer */
2684 for (peer = peers->remote; peer; peer = peer->next) {
2685 if (strcmp(peer->id, trash.area) == 0)
2686 break;
2687 }
2688
2689 /* if unknown peer */
2690 if (!peer) {
2691 appctx->st0 = PEER_SESS_ST_EXIT;
2692 appctx->st1 = PEER_SESS_SC_ERRPEER;
2693 return -1;
2694 }
2695 *curpeer = peer;
2696
2697 return 1;
2698}
2699
2700/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002701 * Init <peer> peer after having accepted it at peer protocol level.
2702 */
2703static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2704{
2705 struct shared_table *st;
2706
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002707 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002708 /* Register status code */
2709 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002710 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002711
2712 /* Awake main task */
2713 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2714
2715 /* Init confirm counter */
2716 peer->confirm = 0;
2717
2718 /* Init cursors */
2719 for (st = peer->tables; st ; st = st->next) {
2720 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002721 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2722 /* if st->update appears to be in future it means
2723 * that the last acked value is very old and we
2724 * remain unconnected a too long time to use this
2725 * acknowlegement as a reset.
2726 * We should update the protocol to be able to
2727 * signal the remote peer that it needs a full resync.
2728 * Here a partial fix consist to set st->update at
2729 * the max past value
2730 */
2731 if ((int)(st->table->localupdate - st->update) < 0)
2732 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002733 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002734 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002735 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2736 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002737 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002738 }
2739
2740 /* reset teaching and learning flags to 0 */
2741 peer->flags &= PEER_TEACH_RESET;
2742 peer->flags &= PEER_LEARN_RESET;
2743
2744 /* if current peer is local */
2745 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002746 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002747 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2748 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2749 /* assign local peer for a lesson, consider lesson already requested */
2750 peer->flags |= PEER_F_LEARN_ASSIGN;
2751 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002752 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002753 }
2754
2755 }
2756 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2757 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2758 /* assign peer for a lesson */
2759 peer->flags |= PEER_F_LEARN_ASSIGN;
2760 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002761 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002762 }
2763}
2764
2765/*
2766 * Init <peer> peer after having connected it at peer protocol level.
2767 */
2768static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2769{
2770 struct shared_table *st;
2771
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002772 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002773 /* Init cursors */
2774 for (st = peer->tables; st ; st = st->next) {
2775 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002776 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2777 /* if st->update appears to be in future it means
2778 * that the last acked value is very old and we
2779 * remain unconnected a too long time to use this
2780 * acknowlegement as a reset.
2781 * We should update the protocol to be able to
2782 * signal the remote peer that it needs a full resync.
2783 * Here a partial fix consist to set st->update at
2784 * the max past value.
2785 */
2786 if ((int)(st->table->localupdate - st->update) < 0)
2787 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002788 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002789 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002790 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2791 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002792 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002793 }
2794
2795 /* Init confirm counter */
2796 peer->confirm = 0;
2797
2798 /* reset teaching and learning flags to 0 */
2799 peer->flags &= PEER_TEACH_RESET;
2800 peer->flags &= PEER_LEARN_RESET;
2801
2802 /* If current peer is local */
2803 if (peer->local) {
2804 /* flag to start to teach lesson */
2805 peer->flags |= PEER_F_TEACH_PROCESS;
2806 }
2807 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2808 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2809 /* If peer is remote and resync from remote is needed,
2810 and no peer currently assigned */
2811
2812 /* assign peer for a lesson */
2813 peer->flags |= PEER_F_LEARN_ASSIGN;
2814 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002815 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002816 }
2817}
2818
2819/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002820 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002821 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002822static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002823{
Christopher Faulet908628c2022-03-25 16:43:49 +01002824 struct conn_stream *cs = appctx->owner;
2825 struct stream *s = __cs_strm(cs);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002826 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002827 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002828 int reql = 0;
2829 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002830 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002831 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002832
Joseph Herlant82b2f542018-11-15 12:19:14 -08002833 /* Check if the input buffer is available. */
Christopher Faulet908628c2022-03-25 16:43:49 +01002834 if (cs_ib(cs)->size == 0) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02002835 cs_rx_room_blk(cs);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002836 goto out;
2837 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002838
Emeric Brun2b920a12010-09-23 18:30:22 +02002839 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002840 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002841switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002842 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002843 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002844 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002845 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002846 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002847 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002848 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002849 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002850 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002851 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2852 if (reql <= 0) {
2853 if (!reql)
2854 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002855 goto switchstate;
2856 }
2857
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002858 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002859 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002860 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002861 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002862 reql = peer_getline_host(appctx);
2863 if (reql <= 0) {
2864 if (!reql)
2865 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002866 goto switchstate;
2867 }
2868
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002869 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002870 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002871 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002872 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002873 reql = peer_getline_last(appctx, &curpeer);
2874 if (reql <= 0) {
2875 if (!reql)
2876 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002877 goto switchstate;
2878 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002879
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002880 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002881 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002882 if (curpeer->local) {
2883 /* Local connection, reply a retry */
2884 appctx->st0 = PEER_SESS_ST_EXIT;
2885 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2886 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002887 }
Emeric Brun80527f52017-06-19 17:46:37 +02002888
2889 /* we're killing a connection, we must apply a random delay before
2890 * retrying otherwise the other end will do the same and we can loop
2891 * for a while.
2892 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002893 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002894 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002895 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002896 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002897 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002898 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2899 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2900 curpeer->flags |= PEER_F_DWNGRD;
2901 }
2902 else {
2903 curpeer->flags &= ~PEER_F_DWNGRD;
2904 }
2905 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002906 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002907 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002908 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002909 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002910 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002911 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002912 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002913 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002914 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002915 if (!curpeer) {
2916 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002917 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002918 if (curpeer->appctx != appctx) {
2919 appctx->st0 = PEER_SESS_ST_END;
2920 goto switchstate;
2921 }
2922 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002923
2924 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002925 if (repl <= 0) {
2926 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002927 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002928 goto switchstate;
2929 }
2930
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002931 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002932
Emeric Brun2b920a12010-09-23 18:30:22 +02002933 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002934 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002935 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002936 goto switchstate;
2937 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002938 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002939 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002940 if (!curpeer) {
2941 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002942 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002943 if (curpeer->appctx != appctx) {
2944 appctx->st0 = PEER_SESS_ST_END;
2945 goto switchstate;
2946 }
2947 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002948
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002949 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002950 if (repl <= 0) {
2951 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002952 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002953 goto switchstate;
2954 }
2955
2956 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002957 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002958 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002959 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002960 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002961 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002962 if (!curpeer) {
2963 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002964 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002965 if (curpeer->appctx != appctx) {
2966 appctx->st0 = PEER_SESS_ST_END;
2967 goto switchstate;
2968 }
2969 }
2970
Christopher Faulet908628c2022-03-25 16:43:49 +01002971 if (cs_ic(cs)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002972 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002973
Frédéric Lécaillece025572019-01-21 13:38:06 +01002974 reql = peer_getline(appctx);
2975 if (!reql)
2976 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002977
Frédéric Lécaillece025572019-01-21 13:38:06 +01002978 if (reql < 0)
2979 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002980
2981 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002982 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002983 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02002984
2985 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002986 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002987
2988 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002989 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002990 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002991 }
2992 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002993 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2994 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002995 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002996 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002997 goto switchstate;
2998 }
Willy Tarreau4781b152021-04-06 13:53:36 +02002999 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003000 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02003001 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003002 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003003 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003004 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 char *msg_cur = trash.area;
3006 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01003007 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02003008 int totl = 0;
3009
Willy Tarreau2d372c22018-11-05 17:12:27 +01003010 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003011 if (!curpeer) {
3012 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003013 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003014 if (curpeer->appctx != appctx) {
3015 appctx->st0 = PEER_SESS_ST_END;
3016 goto switchstate;
3017 }
3018 }
3019
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003020 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
3021 if (reql <= 0) {
3022 if (reql == -1)
3023 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003024 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003025 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01003026
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003027 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01003028 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02003029 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003030
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003031 curpeer->flags |= PEER_F_ALIVE;
3032
Emeric Brun2b920a12010-09-23 18:30:22 +02003033 /* skip consumed message */
Christopher Faulet908628c2022-03-25 16:43:49 +01003034 co_skip(cs_oc(cs), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02003035 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02003036 goto switchstate;
3037
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003038send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003039 if (curpeer->flags & PEER_F_HEARTBEAT) {
3040 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003041 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003042 if (repl <= 0) {
3043 if (repl == -1)
3044 goto out;
3045 goto switchstate;
3046 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01003047 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003048 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003049 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003050 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01003051 if (repl <= 0) {
3052 if (repl == -1)
3053 goto out;
3054 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02003055 }
3056
Emeric Brun2b920a12010-09-23 18:30:22 +02003057 /* noting more to do */
3058 goto out;
3059 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003060 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01003061 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003062 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003063 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003064 if (peer_send_status_errormsg(appctx) == -1)
3065 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003066 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003067 goto switchstate;
3068 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003069 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003070 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003071 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003072 if (peer_send_error_size_limitmsg(appctx) == -1)
3073 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003074 appctx->st0 = PEER_SESS_ST_END;
3075 goto switchstate;
3076 }
3077 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003078 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
3079 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003080 if (curpeer)
3081 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003082 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003083 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003084 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003085 if (peer_send_error_protomsg(appctx) == -1) {
3086 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003087 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003088 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003089 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003090 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003091 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003092 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003093 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003094 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003095 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003096 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003097 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003098 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003099 curpeer = NULL;
3100 }
Christopher Fauletda098e62022-03-31 17:44:45 +02003101 cs_shutw(cs);
3102 cs_shutr(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +01003103 cs_ic(cs)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02003104 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003105 }
3106 }
3107 }
3108out:
Christopher Faulet908628c2022-03-25 16:43:49 +01003109 cs_oc(cs)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02003110
3111 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003112 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02003113 return;
3114}
3115
Willy Tarreau30576452015-04-13 13:50:30 +02003116static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003117 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003118 .name = "<PEER>", /* used for logging */
3119 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07003120 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003121};
Emeric Brun2b920a12010-09-23 18:30:22 +02003122
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003123
Emeric Brun2b920a12010-09-23 18:30:22 +02003124/*
3125 * Use this function to force a close of a peer session
3126 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003127static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003128{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003129 struct appctx *appctx = peer->appctx;
3130
Frédéric Lécaille5df11902017-06-13 16:39:57 +02003131 /* Note that the peer sessions which have just been created
3132 * (->st0 == PEER_SESS_ST_CONNECT) must not
3133 * be shutdown, if not, the TCP session will never be closed
3134 * and stay in CLOSE_WAIT state after having been closed by
3135 * the remote side.
3136 */
3137 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003138 return;
3139
Willy Tarreau81bc3b02016-10-31 17:37:39 +01003140 if (appctx->applet != &peer_applet)
3141 return;
3142
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003143 __peer_session_deinit(peer);
3144
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003145 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01003146 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003147}
3148
Willy Tarreau91d96282015-03-13 15:47:26 +01003149/* Pre-configures a peers frontend to accept incoming connections */
3150void peers_setup_frontend(struct proxy *fe)
3151{
3152 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02003153 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02003154 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01003155 fe->maxconn = 0;
3156 fe->conn_retries = CONN_RETRIES;
3157 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01003158 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01003159 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01003160 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
3161}
3162
Emeric Brun2b920a12010-09-23 18:30:22 +02003163/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01003164 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02003165 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003166static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003167{
Willy Tarreau04b92862017-09-15 11:01:04 +02003168 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003169 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02003170 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01003171 struct conn_stream *cs;
Willy Tarreau87b09662015-04-03 00:22:06 +02003172 struct stream *s;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003173 struct sockaddr_storage *addr = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003174
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01003175 peer->new_conn++;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003176 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003177 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003178 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003179 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003180 s = NULL;
3181
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01003182 appctx = appctx_new(&peer_applet, NULL);
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003183 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003184 goto out_close;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003185
3186 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003187 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003188
Willy Tarreau04b92862017-09-15 11:01:04 +02003189 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02003190 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003191 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02003192 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003193 }
3194
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003195 if (!sockaddr_alloc(&addr, &peer->addr, sizeof(peer->addr)))
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003196 goto out_free_sess;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003197
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01003198 cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL);
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003199 if (!cs) {
3200 ha_alert("Failed to initialize stream in peer_session_create().\n");
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003201 goto out_free_addr;
Christopher Faulet13a35e52021-12-20 15:34:16 +01003202 }
3203
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003204 s = DISGUISE(cs_strm(cs));
3205
Willy Tarreau6e2979c2015-04-27 13:21:15 +02003206 /* applet is waiting for data */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003207 cs_cant_get(s->csf);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02003208 appctx_wakeup(appctx);
3209
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02003210 /* initiate an outgoing connection */
Christopher Faulet8da67aa2022-03-29 17:53:09 +02003211 s->csb->dst = addr;
Christopher Faulet8abe7122022-03-30 15:10:18 +02003212 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003213 s->flags = SF_ASSIGNED|SF_ADDR_SET;
3214 s->target = peer_session_target(peer, s);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003215
Emeric Brun2b920a12010-09-23 18:30:22 +02003216 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003217 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02003218
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003219 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01003220
Emeric Brunb3971ab2015-05-12 18:49:09 +02003221 peer->appctx = appctx;
Willy Tarreau4781b152021-04-06 13:53:36 +02003222 _HA_ATOMIC_INC(&active_peers);
Willy Tarreau9df94c22016-10-31 18:42:52 +01003223 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003224
3225 /* Error unrolling */
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003226 out_free_addr:
3227 sockaddr_free(&addr);
Willy Tarreau15b5e142015-04-04 14:38:25 +02003228 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02003229 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02003230 out_free_appctx:
3231 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003232 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01003233 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003234}
3235
3236/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003237 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01003238 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
3239 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02003240 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003241struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02003242{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003243 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003244 struct peer *ps;
3245 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02003246
3247 task->expire = TICK_ETERNITY;
3248
Emeric Brunb3971ab2015-05-12 18:49:09 +02003249 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003250 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003251 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003252 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02003253 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003254 return NULL;
3255 }
3256
Emeric Brun80527f52017-06-19 17:46:37 +02003257 /* Acquire lock for all peers of the section */
3258 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003259 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003260
Emeric Brun2b920a12010-09-23 18:30:22 +02003261 if (!stopping) {
3262 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02003263
Emeric Brun2c4ab412021-04-21 16:06:35 +02003264 /* resync timeout set to TICK_ETERNITY means we just start
3265 * a new process and timer was not initialized.
3266 * We must arm this timer to switch to a request to a remote
3267 * node if incoming connection from old local process never
3268 * comes.
3269 */
3270 if (peers->resync_timeout == TICK_ETERNITY)
3271 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3272
Emeric Brunb3971ab2015-05-12 18:49:09 +02003273 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
3274 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
3275 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003276 /* Resync from local peer needed
3277 no peer was assigned for the lesson
3278 and no old local peer found
3279 or resync timeout expire */
3280
3281 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003282 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003283 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003284
3285 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003286 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02003287 }
3288
3289 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003290 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003291 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003292 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003293 if (!ps->appctx) {
3294 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003295 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003296 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003297 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003298 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003299 tick_is_expired(ps->reconnect, now_ms))) {
3300 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003301 * or previous peer connection established with success
3302 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003303 * and reconnection timer is expired */
3304
3305 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003306 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003307 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003308 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003309 /* If previous session failed during connection
3310 * but reconnection timer is not expired */
3311
3312 /* reschedule task for reconnect */
3313 task->expire = tick_first(task->expire, ps->reconnect);
3314 }
3315 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003316 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003317 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003318 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003319 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3320 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003321 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3322 /* Resync from a remote is needed
3323 * and no peer was assigned for lesson
3324 * and current peer may be up2date */
3325
3326 /* assign peer for the lesson */
3327 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003328 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003329 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003330
Willy Tarreau9df94c22016-10-31 18:42:52 +01003331 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003332 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003333 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003334 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003335 int update_to_push = 0;
3336
Emeric Brunb3971ab2015-05-12 18:49:09 +02003337 /* Awake session if there is data to push */
3338 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003339 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003340 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003341 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003342 /* There is no need to send a heartbeat message
3343 * when some updates must be pushed. The remote
3344 * peer will consider <ps> peer as alive when it will
3345 * receive these updates.
3346 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003347 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003348 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003349 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003350 /* We are going to send updates, let's ensure we will
3351 * come back to send heartbeat messages or to reconnect.
3352 */
3353 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003354 appctx_wakeup(ps->appctx);
3355 break;
3356 }
3357 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003358 /* When there are updates to send we do not reconnect
3359 * and do not send heartbeat message either.
3360 */
3361 if (!update_to_push) {
3362 if (tick_is_expired(ps->reconnect, now_ms)) {
3363 if (ps->flags & PEER_F_ALIVE) {
3364 /* This peer was alive during a 'reconnect' period.
3365 * Flag it as not alive again for the next period.
3366 */
3367 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003368 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003369 }
3370 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003371 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003372 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003373 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003374 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003375 }
3376 }
3377 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3378 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3379 ps->flags |= PEER_F_HEARTBEAT;
3380 appctx_wakeup(ps->appctx);
3381 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003382 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003383 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003384 }
3385 /* else do nothing */
3386 } /* SUCCESSCODE */
3387 } /* !ps->peer->local */
3388 } /* for */
3389
3390 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003391 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3392 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3393 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003394 /* Resync from remote peer needed
3395 * no peer was assigned for the lesson
3396 * and resync timeout expire */
3397
3398 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003399 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003400 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003401 }
3402
Emeric Brunb3971ab2015-05-12 18:49:09 +02003403 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003404 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003405 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3406 if (!tick_is_expired(peers->resync_timeout, now_ms))
3407 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003408 }
3409 } /* !stopping */
3410 else {
3411 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003412 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003413 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003414 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003415 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003416 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003417 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003418
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003419 /* disconnect all connected peers to process a local sync
3420 * this must be done only the first time we are switching
3421 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003422 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003423 for (ps = peers->remote; ps; ps = ps->next) {
3424 /* we're killing a connection, we must apply a random delay before
3425 * retrying otherwise the other end will do the same and we can loop
3426 * for a while.
3427 */
3428 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3429 if (ps->appctx) {
3430 peer_session_forceshutdown(ps);
3431 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003432 }
3433 }
3434 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003435
Emeric Brunb3971ab2015-05-12 18:49:09 +02003436 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003437 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003438 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003439 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003440 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003441 peers->flags &= ~PEERS_F_DONOTSTOP;
3442 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003443 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003444 }
3445 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003446 else if (!ps->appctx) {
3447 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003448 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003449 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3450 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3451 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003452 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003453 * or previous peer connection was successfully established
3454 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02003455 * or during previous connect, peer replies a try again statuscode */
3456
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003457 /* connect to the local peer if we must push a local sync */
3458 if (peers->flags & PEERS_F_DONOTSTOP) {
3459 peer_session_create(peers, ps);
3460 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003461 }
3462 else {
3463 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003464 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003465 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003466 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003467 peers->flags &= ~PEERS_F_DONOTSTOP;
3468 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003469 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003470 }
3471 }
3472 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003473 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003474 /* current peer connection is active and established
3475 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003476 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003477 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003478 appctx_wakeup(ps->appctx);
3479 break;
3480 }
3481 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003482 }
3483 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003484
3485 /* Release lock for all peers of the section */
3486 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003487 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003488
Emeric Brun2b920a12010-09-23 18:30:22 +02003489 /* Wakeup for re-connect */
3490 return task;
3491}
3492
Emeric Brunb3971ab2015-05-12 18:49:09 +02003493
Emeric Brun2b920a12010-09-23 18:30:22 +02003494/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003495 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003496 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003497int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003498{
Emeric Brun2b920a12010-09-23 18:30:22 +02003499 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003500
Emeric Brun2b920a12010-09-23 18:30:22 +02003501 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003502 peers->peers_fe->maxconn += 3;
3503 }
3504
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003505 peers->sync_task = task_new_anywhere();
Willy Tarreaud9443442018-10-15 11:18:03 +02003506 if (!peers->sync_task)
3507 return 0;
3508
Emeric Brunb3971ab2015-05-12 18:49:09 +02003509 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003510 peers->sync_task->context = (void *)peers;
3511 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3512 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003513 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003514}
3515
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003516/*
3517 * Allocate a cache a dictionary entries used upon transmission.
3518 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003519static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003520{
3521 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003522 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003523
3524 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003525 entries = calloc(max_entries, sizeof *entries);
3526 if (!d || !entries)
3527 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003528
3529 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003530 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003531 d->cached_entries = EB_ROOT_UNIQUE;
3532 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003533
3534 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003535
3536 err:
3537 free(d);
3538 free(entries);
3539 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003540}
3541
3542/*
3543 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3544 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003545 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003546 * Must be deallocated calling free_dcache().
3547 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003548static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003549{
3550 struct dcache_tx *dc_tx;
3551 struct dcache *dc;
3552 struct dcache_rx *dc_rx;
3553
3554 dc = calloc(1, sizeof *dc);
3555 dc_tx = new_dcache_tx(max_entries);
3556 dc_rx = calloc(max_entries, sizeof *dc_rx);
3557 if (!dc || !dc_tx || !dc_rx)
3558 goto err;
3559
3560 dc->tx = dc_tx;
3561 dc->rx = dc_rx;
3562 dc->max_entries = max_entries;
3563
3564 return dc;
3565
3566 err:
3567 free(dc);
3568 free(dc_tx);
3569 free(dc_rx);
3570 return NULL;
3571}
3572
3573/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003574 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3575 * entries used upon transmission.
3576 * Return the entry if found, NULL if not.
3577 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003578static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3579 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003580{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003581 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003582}
3583
3584/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003585 * Flush <dc> cache.
3586 * Always succeeds.
3587 */
3588static inline void flush_dcache(struct peer *peer)
3589{
3590 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003591 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003592
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003593 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003594 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003595 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003596 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3597 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003598 }
3599 dc->tx->prev_lookup = NULL;
3600 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003601
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003602 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003603}
3604
3605/*
3606 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3607 * with information provided by <i> dictionary cache entry (especially the value
3608 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003609 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003610 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003611static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003612{
3613 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003614 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003615
3616 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003617
3618 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3619 o = dc_tx->prev_lookup;
3620 } else {
3621 o = dcache_tx_lookup_value(dc_tx, i);
3622 if (o) {
3623 /* Save it */
3624 dc_tx->prev_lookup = o;
3625 }
3626 }
3627
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003628 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003629 /* Copy the ID. */
3630 i->id = o - dc->tx->entries;
3631 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003632 }
3633
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003634 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003635 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003636
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003637 ebpt_delete(o);
3638 o->key = i->entry.key;
3639 ebpt_insert(&dc_tx->cached_entries, o);
3640 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003641
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003642 /* Update the index for the next entry to put in cache */
3643 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003644
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003645 return o;
3646}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003647
3648/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003649 * Allocate a dictionary cache for each peer of <peers> section.
3650 * Return 1 if succeeded, 0 if not.
3651 */
3652int peers_alloc_dcache(struct peers *peers)
3653{
3654 struct peer *p;
3655
3656 for (p = peers->remote; p; p = p->next) {
3657 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3658 if (!p->dcache)
3659 return 0;
3660 }
3661
3662 return 1;
3663}
3664
3665/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003666 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003667 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003668 */
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003669int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003670{
3671 struct shared_table *st;
3672 struct peer * curpeer;
3673 int id = 0;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003674 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003675
3676 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003677 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003678 if (!st) {
3679 retval = 1;
3680 break;
3681 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003682 st->table = table;
3683 st->next = curpeer->tables;
3684 if (curpeer->tables)
3685 id = curpeer->tables->local_id;
3686 st->local_id = id + 1;
3687
Emeric Brun2cc201f2021-04-23 12:21:26 +02003688 /* If peer is local we inc table
3689 * refcnt to protect against flush
3690 * until this process pushed all
3691 * table content to the new one
3692 */
3693 if (curpeer->local)
3694 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003695 curpeer->tables = st;
3696 }
3697
3698 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003699
3700 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003701}
3702
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003703/*
3704 * Parse the "show peers" command arguments.
3705 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3706 * error message.
3707 */
3708static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3709{
3710 appctx->ctx.cfgpeers.target = NULL;
3711
Willy Tarreau49962b52021-02-12 16:56:22 +01003712 if (strcmp(args[2], "dict") == 0) {
3713 /* show the dictionaries (large dump) */
3714 appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT;
3715 args++;
3716 } else if (strcmp(args[2], "-") == 0)
3717 args++; // allows to show a section called "dict"
3718
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003719 if (*args[2]) {
3720 struct peers *p;
3721
3722 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003723 if (strcmp(p->id, args[2]) == 0) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003724 appctx->ctx.cfgpeers.target = p;
3725 break;
3726 }
3727 }
3728
Willy Tarreau9d008692019-08-09 11:21:01 +02003729 if (!p)
3730 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003731 }
3732
3733 return 0;
3734}
3735
3736/*
3737 * This function dumps the peer state information of <peers> "peers" section.
3738 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3739 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3740 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003741static int peers_dump_head(struct buffer *msg, struct conn_stream *cs, struct peers *peers)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003742{
3743 struct tm tm;
3744
3745 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003746 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 +02003747 peers,
3748 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3749 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003750 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003751 peers->resync_timeout ?
3752 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3753 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003754 TICKS_TO_MS(1000)) : "<NEVER>",
3755 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003756
Christopher Faulet908628c2022-03-25 16:43:49 +01003757 if (ci_putchk(cs_ic(cs), msg) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003758 cs_rx_room_blk(cs);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003759 return 0;
3760 }
3761
3762 return 1;
3763}
3764
3765/*
3766 * This function dumps <peer> state information.
3767 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3768 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3769 */
Christopher Faulet908628c2022-03-25 16:43:49 +01003770static int peers_dump_peer(struct buffer *msg, struct conn_stream *cs, struct peer *peer, int flags)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003771{
3772 struct connection *conn;
3773 char pn[INET6_ADDRSTRLEN];
Christopher Faulet908628c2022-03-25 16:43:49 +01003774 struct conn_stream *peer_cs;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003775 struct stream *peer_s;
3776 struct appctx *appctx;
3777 struct shared_table *st;
3778
3779 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003780 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003781 peer, peer->id,
3782 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003783 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003784 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003785 statuscode_str(peer->statuscode));
3786
3787 chunk_appendf(msg, " last_hdshk=%s\n",
3788 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3789 TICKS_TO_MS(1000)) : "<NEVER>");
3790
3791 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003792 peer->reconnect ?
3793 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3794 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003795 TICKS_TO_MS(1000)) : "<NEVER>");
3796
3797 chunk_appendf(msg, " heartbeat=%s",
3798 peer->heartbeat ?
3799 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3800 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3801 TICKS_TO_MS(1000)) : "<NEVER>");
3802
3803 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 +01003804 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003805 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003806
3807 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3808
3809 appctx = peer->appctx;
3810 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003811 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003812
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003813 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3814 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003815
Christopher Faulet908628c2022-03-25 16:43:49 +01003816 peer_cs = peer->appctx->owner;
3817 peer_s = __cs_strm(peer_cs);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003818
Christopher Faulet62e75742022-03-31 09:16:34 +02003819 chunk_appendf(&trash, " state=%s", cs_state_str(cs_opposite(peer_cs)->state));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003820
3821 conn = objt_conn(strm_orig(peer_s));
3822 if (conn)
3823 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3824
Willy Tarreau3ca14902019-07-17 14:53:15 +02003825 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 +02003826 case AF_INET:
3827 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003828 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003829 break;
3830 case AF_UNIX:
3831 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3832 break;
3833 }
3834
Willy Tarreau3ca14902019-07-17 14:53:15 +02003835 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 +02003836 case AF_INET:
3837 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003838 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003839 break;
3840 case AF_UNIX:
3841 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3842 break;
3843 }
3844
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003845 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003846 if (peer->remote_table)
3847 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3848 peer->remote_table,
3849 peer->remote_table->table->id,
3850 peer->remote_table->local_id,
3851 peer->remote_table->remote_id);
3852
3853 if (peer->last_local_table)
3854 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3855 peer->last_local_table,
3856 peer->last_local_table->table->id,
3857 peer->last_local_table->local_id,
3858 peer->last_local_table->remote_id);
3859
3860 if (peer->tables) {
3861 chunk_appendf(&trash, "\n shared tables:");
3862 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003863 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003864 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003865 struct dcache *dcache;
3866
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003867 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003868 dcache = peer->dcache;
3869
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003870 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3871 "flags=0x%x remote_data=0x%llx",
3872 st, st->local_id, st->remote_id,
3873 st->flags, (unsigned long long)st->remote_data);
3874 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3875 " teaching_origin=%u update=%u",
3876 st->last_acked, st->last_pushed, st->last_get,
3877 st->teaching_origin, st->update);
3878 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003879 " commitupdate=%u refcnt=%u",
3880 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003881 if (flags & PEERS_SHOW_F_DICT) {
3882 chunk_appendf(&trash, "\n TX dictionary cache:");
3883 count = 0;
3884 for (i = 0; i < dcache->max_entries; i++) {
3885 struct ebpt_node *node;
3886 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003887
Willy Tarreau49962b52021-02-12 16:56:22 +01003888 node = &dcache->tx->entries[i];
3889 if (!node->key)
3890 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003891
Willy Tarreau49962b52021-02-12 16:56:22 +01003892 if (!count++)
3893 chunk_appendf(&trash, "\n ");
3894 de = node->key;
3895 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3896 count &= 0x3;
3897 }
3898 chunk_appendf(&trash, "\n RX dictionary cache:");
3899 count = 0;
3900 for (i = 0; i < dcache->max_entries; i++) {
3901 if (!count++)
3902 chunk_appendf(&trash, "\n ");
3903 chunk_appendf(&trash, " %3u -> %s", i,
3904 dcache->rx[i].de ?
3905 (char *)dcache->rx[i].de->value.key : "-");
3906 count &= 0x3;
3907 }
3908 } else {
3909 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003910 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003911 }
3912 }
3913
3914 end:
3915 chunk_appendf(&trash, "\n");
Christopher Faulet908628c2022-03-25 16:43:49 +01003916 if (ci_putchk(cs_ic(cs), msg) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02003917 cs_rx_room_blk(cs);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003918 return 0;
3919 }
3920
3921 return 1;
3922}
3923
3924/*
3925 * This function dumps all the peers of "peers" section.
3926 * Returns 0 if the output buffer is full and needs to be called
3927 * again, non-zero if not. It proceeds in an isolated thread, so
3928 * there is no thread safety issue here.
3929 */
3930static int cli_io_handler_show_peers(struct appctx *appctx)
3931{
3932 int show_all;
3933 int ret = 0, first_peers = 1;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003934
3935 thread_isolate();
3936
3937 show_all = !appctx->ctx.cfgpeers.target;
3938
3939 chunk_reset(&trash);
3940
3941 while (appctx->st2 != STAT_ST_FIN) {
3942 switch (appctx->st2) {
3943 case STAT_ST_INIT:
3944 if (show_all)
3945 appctx->ctx.cfgpeers.peers = cfg_peers;
3946 else
3947 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3948
3949 appctx->st2 = STAT_ST_LIST;
3950 /* fall through */
3951
3952 case STAT_ST_LIST:
3953 if (!appctx->ctx.cfgpeers.peers) {
3954 /* No more peers list. */
3955 appctx->st2 = STAT_ST_END;
3956 }
3957 else {
3958 if (!first_peers)
3959 chunk_appendf(&trash, "\n");
3960 else
3961 first_peers = 0;
Christopher Faulet908628c2022-03-25 16:43:49 +01003962 if (!peers_dump_head(&trash, appctx->owner, appctx->ctx.cfgpeers.peers))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003963 goto out;
3964
3965 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3966 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3967 appctx->st2 = STAT_ST_INFO;
3968 }
3969 break;
3970
3971 case STAT_ST_INFO:
3972 if (!appctx->ctx.cfgpeers.peer) {
3973 /* End of peer list */
3974 if (show_all)
3975 appctx->st2 = STAT_ST_LIST;
3976 else
3977 appctx->st2 = STAT_ST_END;
3978 }
3979 else {
Christopher Faulet908628c2022-03-25 16:43:49 +01003980 if (!peers_dump_peer(&trash, appctx->owner, appctx->ctx.cfgpeers.peer, appctx->ctx.cfgpeers.flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003981 goto out;
3982
3983 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3984 }
3985 break;
3986
3987 case STAT_ST_END:
3988 appctx->st2 = STAT_ST_FIN;
3989 break;
3990 }
3991 }
3992 ret = 1;
3993 out:
3994 thread_release();
3995 return ret;
3996}
3997
3998/*
3999 * CLI keywords.
4000 */
4001static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004002 { { "show", "peers", NULL }, "show peers [dict|-] [section] : dump some information about all the peers or this peers section", cli_parse_show_peers, cli_io_handler_show_peers, },
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02004003 {},
4004}};
4005
4006/* Register cli keywords */
4007INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);