blob: 3df4999f6793efc848ef82a9cccc38d29787a788 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
Willy Tarreau8db34cc2021-10-06 17:53:19 +020022#include <import/eb32tree.h>
23#include <import/ebmbtree.h>
24#include <import/ebpttree.h>
25
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/applet.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020028#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020029#include <haproxy/cli.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020030#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020033#include <haproxy/frontend.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020034#include <haproxy/net_helper.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020035#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020036#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020037#include <haproxy/proxy.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020038#include <haproxy/sc_strm.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020039#include <haproxy/session-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020040#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020041#include <haproxy/stats-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020042#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020043#include <haproxy/stick_table.h>
44#include <haproxy/stream.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020045#include <haproxy/task.h>
46#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020047#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020048#include <haproxy/tools.h>
Frédéric Lécailled8659352020-11-10 16:18:03 +010049#include <haproxy/trace.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
Emeric Brun2b920a12010-09-23 18:30:22 +020051
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Emeric Brunccdfbae2021-04-28 12:59:35 +020059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
64#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
65#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
66#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
67#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
68#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
69#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
70#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
71#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
72#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
73#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
74#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
75 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010077#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
78#define PEERS_RESYNC_FROMLOCAL 0x00000000
79#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
80#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020081
82/***********************************/
83/* Current shared table sync state */
84/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010085#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
86#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020087
88/******************************/
89/* Remote peer teaching state */
90/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010091#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
92#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
93#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
94#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
95#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
96#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
97#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
98#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020099
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100100#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
101#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +0200102
Frédéric Lécaille54bff832019-03-26 10:25:20 +0100103#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
104#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Christopher Fauletab4b0942022-07-26 19:14:36 +0200105#define PEER_LOCAL_RECONNECT_TIMEOUT 500 /* 500ms */
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 Faulet387e7972022-05-12 14:47:52 +0200447 if (peer->appctx)
448 peers = peer->peers;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100449
450 if (peers)
451 chunk_appendf(&trace_buf, " %s", peers->local->id);
Christopher Fauletd9e6b352021-11-15 09:40:57 +0100452 chunk_appendf(&trace_buf, " -> %s", peer->id);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100453 }
454
455 if (a3) {
456 const int *prev_state = a3;
457
458 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
459 }
460 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100461}
462
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200463static const char *statuscode_str(int statuscode)
464{
465 switch (statuscode) {
466 case PEER_SESS_SC_CONNECTCODE:
467 return "CONN";
468 case PEER_SESS_SC_CONNECTEDCODE:
469 return "HSHK";
470 case PEER_SESS_SC_SUCCESSCODE:
471 return "ESTA";
472 case PEER_SESS_SC_TRYAGAIN:
473 return "RETR";
474 case PEER_SESS_SC_ERRPROTO:
475 return "PROT";
476 case PEER_SESS_SC_ERRVERSION:
477 return "VERS";
478 case PEER_SESS_SC_ERRHOST:
479 return "NAME";
480 case PEER_SESS_SC_ERRPEER:
481 return "UNKN";
482 default:
483 return "NONE";
484 }
485}
486
Emeric Brun18928af2017-03-29 16:32:53 +0200487/* This function encode an uint64 to 'dynamic' length format.
488 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500489 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200490 At return, the *str is set at the next Byte after then
491 encoded integer. The function returns then length of the
492 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200493int intencode(uint64_t i, char **str) {
494 int idx = 0;
495 unsigned char *msg;
496
Emeric Brunb3971ab2015-05-12 18:49:09 +0200497 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200498 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200499 msg[0] = (unsigned char)i;
500 *str = (char *)&msg[idx+1];
501 return (idx+1);
502 }
503
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200504 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
505 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
506 while (i >= PEER_ENC_STOP_BYTE) {
507 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
508 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200509 }
510 msg[++idx] = (unsigned char)i;
511 *str = (char *)&msg[idx+1];
512 return (idx+1);
513}
514
515
Emeric Brun5ea07d92021-06-30 13:21:58 +0200516/* This function returns a decoded 64bits unsigned integer
517 * from a varint
518 *
519 * Calling:
520 * - *str must point on the first byte of the buffer to decode.
521 * - end must point on the next byte after the end of the buffer
522 * we are authorized to parse (buf + buflen)
523 *
524 * At return:
525 *
526 * On success *str will point at the byte following
527 * the fully decoded integer into the buffer. and
528 * the decoded value is returned.
529 *
530 * If end is reached before the integer was fully decoded,
531 * *str is set to NULL and the caller have to check this
532 * to know there is a decoding error. In this case
533 * the returned integer is also forced to 0
534 */
Emeric Brun18928af2017-03-29 16:32:53 +0200535uint64_t intdecode(char **str, char *end)
536{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200537 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200538 uint64_t i;
539 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200540
541 if (!*str)
542 return 0;
543
544 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200545 if (msg >= (unsigned char *)end)
546 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200547
Emeric Brun18928af2017-03-29 16:32:53 +0200548 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200549 if (i >= PEER_ENC_2BYTES_MIN) {
550 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200551 do {
552 if (msg >= (unsigned char *)end)
553 goto fail;
554 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200555 shift += PEER_ENC_STOP_BIT;
556 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200557 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100558 *str = (char *)msg;
559 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200560
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100561 fail:
562 *str = NULL;
563 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200564}
Emeric Brun2b920a12010-09-23 18:30:22 +0200565
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100566/*
567 * Build a "hello" peer protocol message.
568 * Return the number of written bytes written to build this messages if succeeded,
569 * 0 if not.
570 */
571static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
572{
573 int min_ver, ret;
574 struct peer *peer;
575
576 peer = p->hello.peer;
577 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
578 /* Prepare headers */
Willy Tarreau2645b342022-04-12 08:28:18 +0200579 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %d.%d\n%s\n%s %d %d\n",
580 (int)PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), (int)1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100581 if (ret >= size)
582 return 0;
583
584 return ret;
585}
586
587/*
588 * Build a "handshake succeeded" status message.
589 * Return the number of written bytes written to build this messages if succeeded,
590 * 0 if not.
591 */
592static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
593{
594 int ret;
595
Willy Tarreau2645b342022-04-12 08:28:18 +0200596 ret = snprintf(msg, size, "%d\n", (int)PEER_SESS_SC_SUCCESSCODE);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100597 if (ret >= size)
598 return 0;
599
600 return ret;
601}
602
603/*
604 * Build an error status message.
605 * Return the number of written bytes written to build this messages if succeeded,
606 * 0 if not.
607 */
608static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
609{
610 int ret;
611 unsigned int st1;
612
613 st1 = p->error_status.st1;
614 ret = snprintf(msg, size, "%d\n", st1);
615 if (ret >= size)
616 return 0;
617
618 return ret;
619}
620
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200621/* Set the stick-table UPDATE message type byte at <msg_type> address,
622 * depending on <use_identifier> and <use_timed> boolean parameters.
623 * Always successful.
624 */
625static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
626{
627 if (use_timed) {
628 if (use_identifier)
629 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
630 else
631 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
632 }
633 else {
634 if (use_identifier)
635 *msg_type = PEER_MSG_STKT_UPDATE;
636 else
637 *msg_type = PEER_MSG_STKT_INCUPDATE;
638 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200639}
Emeric Brun2b920a12010-09-23 18:30:22 +0200640/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200641 * This prepare the data update message on the stick session <ts>, <st> is the considered
642 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800643 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200644 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
645 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200646 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100647static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200648{
649 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200650 unsigned short datalen;
651 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200652 unsigned int data_type;
653 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100654 struct stksess *ts;
655 struct shared_table *st;
656 unsigned int updateid;
657 int use_identifier;
658 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200659 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100660
661 ts = p->updt.stksess;
662 st = p->updt.shared_table;
663 updateid = p->updt.updateid;
664 use_identifier = p->updt.use_identifier;
665 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200666 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200667
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200668 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200669
Emeric Brun2b920a12010-09-23 18:30:22 +0200670 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200671
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500672 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200673 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200674 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200675 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200676
677 /* encode update identifier if needed */
678 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200679 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200680 memcpy(cursor, &netinteger, sizeof(netinteger));
681 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200682 }
683
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200684 if (use_timed) {
685 netinteger = htonl(tick_remain(now_ms, ts->expire));
686 memcpy(cursor, &netinteger, sizeof(netinteger));
687 cursor += sizeof(netinteger);
688 }
689
Emeric Brunb3971ab2015-05-12 18:49:09 +0200690 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200691 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200692 int stlen = strlen((char *)ts->key.key);
693
Emeric Brunb3971ab2015-05-12 18:49:09 +0200694 intencode(stlen, &cursor);
695 memcpy(cursor, ts->key.key, stlen);
696 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200697 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200698 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100699 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200700 memcpy(cursor, &netinteger, sizeof(netinteger));
701 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200702 }
703 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200704 memcpy(cursor, ts->key.key, st->table->key_size);
705 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200706 }
707
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100708 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200709 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200710 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200711
Emeric Brun94900952015-06-11 18:25:54 +0200712 data_ptr = stktable_data_ptr(st->table, ts, data_type);
713 if (data_ptr) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200714 /* in case of array all elements use
715 * the same std_type and they are linearly
716 * encoded.
717 */
718 if (stktable_data_types[data_type].is_array) {
719 unsigned int idx = 0;
720
721 switch (stktable_data_types[data_type].std_type) {
722 case STD_T_SINT: {
723 int data;
724
725 do {
726 data = stktable_data_cast(data_ptr, std_t_sint);
727 intencode(data, &cursor);
728
729 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
730 } while(data_ptr);
731 break;
732 }
733 case STD_T_UINT: {
734 unsigned int data;
735
736 do {
737 data = stktable_data_cast(data_ptr, std_t_uint);
738 intencode(data, &cursor);
739
740 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
741 } while(data_ptr);
742 break;
743 }
744 case STD_T_ULL: {
745 unsigned long long data;
746
747 do {
748 data = stktable_data_cast(data_ptr, std_t_ull);
749 intencode(data, &cursor);
750
751 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
752 } while(data_ptr);
753 break;
754 }
755 case STD_T_FRQP: {
756 struct freq_ctr *frqp;
757
758 do {
759 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
760 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
761 intencode(frqp->curr_ctr, &cursor);
762 intencode(frqp->prev_ctr, &cursor);
763
764 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, ++idx);
765 } while(data_ptr);
766 break;
767 }
768 }
769
770 /* array elements fully encoded
771 * proceed next data_type.
772 */
773 continue;
774 }
Emeric Brun94900952015-06-11 18:25:54 +0200775 switch (stktable_data_types[data_type].std_type) {
776 case STD_T_SINT: {
777 int data;
778
779 data = stktable_data_cast(data_ptr, std_t_sint);
780 intencode(data, &cursor);
781 break;
782 }
783 case STD_T_UINT: {
784 unsigned int data;
785
786 data = stktable_data_cast(data_ptr, std_t_uint);
787 intencode(data, &cursor);
788 break;
789 }
790 case STD_T_ULL: {
791 unsigned long long data;
792
793 data = stktable_data_cast(data_ptr, std_t_ull);
794 intencode(data, &cursor);
795 break;
796 }
797 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200798 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200799
800 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
801 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
802 intencode(frqp->curr_ctr, &cursor);
803 intencode(frqp->prev_ctr, &cursor);
804 break;
805 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200806 case STD_T_DICT: {
807 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200808 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200809 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200810 char *beg, *end;
811 size_t value_len, data_len;
812 struct dcache *dc;
813
814 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100815 if (!de) {
816 /* No entry */
817 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200818 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100819 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200820
821 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200822 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200823 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200824 if (cached_de == &cde.entry) {
825 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
826 break;
827 /* Encode the length of the remaining data -> 1 */
828 intencode(1, &cursor);
829 /* Encode the cache entry ID */
830 intencode(cde.id + 1, &cursor);
831 }
832 else {
833 /* Leave enough room to encode the remaining data length. */
834 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
835 /* Encode the dictionary entry key */
836 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200837 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200838 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200839 intencode(value_len, &end);
840 /* Copy the data */
841 memcpy(end, de->value.key, value_len);
842 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200843 /* Encode the length of the data */
844 data_len = end - beg;
845 intencode(data_len, &cursor);
846 memmove(cursor, beg, data_len);
847 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200848 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200849 break;
850 }
Emeric Brun94900952015-06-11 18:25:54 +0200851 }
852 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200853 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100854 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200855
856 /* Compute datalen */
857 datalen = (cursor - datamsg);
858
859 /* prepare message header */
860 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200861 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200862 cursor = &msg[2];
863 intencode(datalen, &cursor);
864
865 /* move data after header */
866 memmove(cursor, datamsg, datalen);
867
868 /* return header size + data_len */
869 return (cursor - msg) + datalen;
870}
871
872/*
873 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800874 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200875 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
876 * check size)
877 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100878static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200879{
880 int len;
881 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200882 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200883 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200884 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200885 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100886 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200887
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100888 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200889 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200890
891 /* Encode data */
892
893 /* encode local id */
894 intencode(st->local_id, &cursor);
895
896 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100897 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200898 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100899 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200900 cursor += len;
901
902 /* encode table type */
903
Emeric Brun530ba382020-06-02 11:17:42 +0200904 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200905
906 /* encode table key size */
907 intencode(st->table->key_size, &cursor);
908
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200909 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200910 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200911 /* encode available known data types in table */
912 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
913 if (st->table->data_ofs[data_type]) {
Emeric Brun90a9b672021-06-22 16:09:55 +0200914 /* stored data types parameters are all linearly encoded
915 * at the end of the 'table definition' message.
916 *
917 * Currently only array data_types and and data_types
918 * using freq_counter base type have parameters:
919 *
920 * - array has always at least one parameter set to the
921 * number of elements.
922 *
923 * - array of base-type freq_counters has an additional
924 * parameter set to the period used to compute those
925 * freq_counters.
926 *
927 * - simple freq counter has a parameter set to the period
928 * used to compute
929 *
930 * A set of parameter for a datatype MUST BE prefixed
931 * by the data-type id itself:
932 * This is useless because the data_types are ordered and
933 * the data_type bitfield already gives the information of
934 * stored types, but it was designed this way when the
935 * push of period parameter was added for freq counters
936 * and we don't want to break the compatibility.
937 *
938 */
939 if (stktable_data_types[data_type].is_array) {
940 /* This is an array type so we first encode
941 * the data_type itself to prefix parameters
942 */
943 intencode(data_type, &chunkq);
944
945 /* We encode the first parameter which is
946 * the number of elements of this array
947 */
948 intencode(st->table->data_nbelem[data_type], &chunkq);
949
Ilya Shipitsin01881082021-08-07 14:41:56 +0500950 /* for array of freq counters, there is an additional
Emeric Brun90a9b672021-06-22 16:09:55 +0200951 * period parameter to encode
952 */
953 if (stktable_data_types[data_type].std_type == STD_T_FRQP)
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200954 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200955 }
Emeric Brun90a9b672021-06-22 16:09:55 +0200956 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
957 /* this datatype is a simple freq counter not part
958 * of an array. We encode the data_type itself
959 * to prefix the 'period' parameter
960 */
961 intencode(data_type, &chunkq);
962 intencode(st->table->data_arg[data_type].u, &chunkq);
963 }
964 /* set the bit corresponding to stored data type */
965 data |= 1ULL << data_type;
Emeric Brun94900952015-06-11 18:25:54 +0200966 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200967 }
968 intencode(data, &cursor);
969
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200970 /* Encode stick-table entries duration. */
971 intencode(st->table->expire, &cursor);
972
973 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200974 chunk->data = chunkq - chunkp;
975 memcpy(cursor, chunk->area, chunk->data);
976 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200977 }
978
Emeric Brunb3971ab2015-05-12 18:49:09 +0200979 /* Compute datalen */
980 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200981
Emeric Brunb3971ab2015-05-12 18:49:09 +0200982 /* prepare message header */
983 msg[0] = PEER_MSG_CLASS_STICKTABLE;
984 msg[1] = PEER_MSG_STKT_DEFINE;
985 cursor = &msg[2];
986 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200987
Emeric Brunb3971ab2015-05-12 18:49:09 +0200988 /* move data after header */
989 memmove(cursor, datamsg, datalen);
990
991 /* return header size + data_len */
992 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200993}
994
Emeric Brunb3971ab2015-05-12 18:49:09 +0200995/*
996 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
997 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800998 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200999 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
1000 * check size)
1001 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001002static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001003{
1004 unsigned short datalen;
1005 char *cursor, *datamsg;
1006 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001007 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001008
Frédéric Lécaille39143342019-05-24 14:32:27 +02001009 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001010
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001011 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001012 intencode(st->remote_id, &cursor);
1013 netinteger = htonl(st->last_get);
1014 memcpy(cursor, &netinteger, sizeof(netinteger));
1015 cursor += sizeof(netinteger);
1016
1017 /* Compute datalen */
1018 datalen = (cursor - datamsg);
1019
1020 /* prepare message header */
1021 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +02001022 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001023 cursor = &msg[2];
1024 intencode(datalen, &cursor);
1025
1026 /* move data after header */
1027 memmove(cursor, datamsg, datalen);
1028
1029 /* return header size + data_len */
1030 return (cursor - msg) + datalen;
1031}
Emeric Brun2b920a12010-09-23 18:30:22 +02001032
1033/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001034 * Function to deinit connected peer
1035 */
1036void __peer_session_deinit(struct peer *peer)
1037{
Christopher Faulet387e7972022-05-12 14:47:52 +02001038 struct peers *peers = peer->peers;
Maciej Zdebd01be2a2022-05-16 17:26:20 +02001039 int thr;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001040
Christopher Faulet387e7972022-05-12 14:47:52 +02001041 if (!peers || !peer->appctx)
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001042 return;
1043
Willy Tarreau29ffe262022-06-15 14:31:38 +02001044 thr = peer->appctx->t->tid;
Maciej Zdebd01be2a2022-05-16 17:26:20 +02001045 HA_ATOMIC_DEC(&peers->applet_count[thr]);
1046
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001047 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02001048 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001049
Willy Tarreau4781b152021-04-06 13:53:36 +02001050 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001051
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001052 flush_dcache(peer);
1053
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001054 /* Re-init current table pointers to force announcement on re-connect */
1055 peer->remote_table = peer->last_local_table = NULL;
1056 peer->appctx = NULL;
1057 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1058 /* unassign current peer for learning */
1059 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
1060 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1061
Emeric Brunccdfbae2021-04-28 12:59:35 +02001062 if (peer->local)
1063 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
1064 else
1065 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001066 /* reschedule a resync */
1067 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1068 }
1069 /* reset teaching and learning flags to 0 */
1070 peer->flags &= PEER_TEACH_RESET;
1071 peer->flags &= PEER_LEARN_RESET;
1072 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1073}
1074
Christopher Faulet6712dc62022-05-12 15:36:11 +02001075static int peer_session_init(struct appctx *appctx)
1076{
1077 struct peer *peer = appctx->svcctx;
1078 struct stream *s;
1079 struct sockaddr_storage *addr = NULL;
1080
1081 if (!sockaddr_alloc(&addr, &peer->addr, sizeof(peer->addr)))
1082 goto out_error;
1083
1084 if (appctx_finalize_startup(appctx, peer->peers->peers_fe, &BUF_NULL) == -1)
1085 goto out_free_addr;
1086
1087 s = appctx_strm(appctx);
1088 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001089 applet_need_more_data(appctx);
Christopher Faulet6712dc62022-05-12 15:36:11 +02001090 appctx_wakeup(appctx);
1091
1092 /* initiate an outgoing connection */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001093 s->scb->dst = addr;
Willy Tarreaucb041662022-05-17 19:44:42 +02001094 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulet6712dc62022-05-12 15:36:11 +02001095 s->flags = SF_ASSIGNED;
1096 s->target = peer_session_target(peer, s);
1097
1098 s->do_log = NULL;
1099 s->uniq_id = 0;
1100
1101 s->res.flags |= CF_READ_DONTWAIT;
1102
1103 _HA_ATOMIC_INC(&active_peers);
1104 return 0;
1105
1106 out_free_addr:
1107 sockaddr_free(&addr);
1108 out_error:
1109 return -1;
1110}
1111
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001112/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001113 * Callback to release a session with a peer
1114 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001115static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001116{
Willy Tarreau455caef2022-05-05 20:16:16 +02001117 struct peer *peer = appctx->svcctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02001118
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001119 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau455caef2022-05-05 20:16:16 +02001120 /* appctx->svcctx is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001121 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +02001122 return;
1123
1124 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001125 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001126 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001127 if (peer->appctx == appctx)
1128 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02001129 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001130 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001131 }
1132}
1133
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001134/* Retrieve the major and minor versions of peers protocol
1135 * announced by a remote peer. <str> is a null-terminated
1136 * string with the following format: "<maj_ver>.<min_ver>".
1137 */
1138static int peer_get_version(const char *str,
1139 unsigned int *maj_ver, unsigned int *min_ver)
1140{
1141 unsigned int majv, minv;
1142 const char *pos, *saved;
1143 const char *end;
1144
1145 saved = pos = str;
1146 end = str + strlen(str);
1147
1148 majv = read_uint(&pos, end);
1149 if (saved == pos || *pos++ != '.')
1150 return -1;
1151
1152 saved = pos;
1153 minv = read_uint(&pos, end);
1154 if (saved == pos || pos != end)
1155 return -1;
1156
1157 *maj_ver = majv;
1158 *min_ver = minv;
1159
1160 return 0;
1161}
Emeric Brun2b920a12010-09-23 18:30:22 +02001162
1163/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001164 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1165 * '\n' character.
1166 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1167 * a line could not be read because the communication channel is closed.
1168 */
1169static inline int peer_getline(struct appctx *appctx)
1170{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001171 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001172 int n;
Frédéric Lécaillece025572019-01-21 13:38:06 +01001173
Willy Tarreauda304902022-05-27 10:44:04 +02001174 n = co_getline(sc_oc(sc), trash.area, trash.size);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001175 if (!n)
1176 return 0;
1177
1178 if (n < 0 || trash.area[n - 1] != '\n') {
1179 appctx->st0 = PEER_SESS_ST_END;
1180 return -1;
1181 }
1182
1183 if (n > 1 && (trash.area[n - 2] == '\r'))
1184 trash.area[n - 2] = 0;
1185 else
1186 trash.area[n - 1] = 0;
1187
Willy Tarreauda304902022-05-27 10:44:04 +02001188 co_skip(sc_oc(sc), n);
Frédéric Lécaillece025572019-01-21 13:38:06 +01001189
1190 return n;
1191}
1192
1193/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001194 * Send a message after having called <peer_prepare_msg> to build it.
1195 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1196 * Returns -1 if there was not enough room left to send the message,
1197 * any other negative returned value must be considered as an error with an appcxt st0
1198 * returned value equal to PEER_SESS_ST_END.
1199 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001200static inline int peer_send_msg(struct appctx *appctx,
1201 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1202 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001203{
1204 int ret, msglen;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001205 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001206
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001207 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001208 if (!msglen) {
1209 /* internal error: message does not fit in trash */
1210 appctx->st0 = PEER_SESS_ST_END;
1211 return 0;
1212 }
1213
1214 /* message to buffer */
Willy Tarreauda304902022-05-27 10:44:04 +02001215 ret = ci_putblk(sc_ic(sc), trash.area, msglen);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001216 if (ret <= 0) {
1217 if (ret == -1) {
1218 /* No more write possible */
Willy Tarreauda304902022-05-27 10:44:04 +02001219 sc_need_room(sc);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001220 return -1;
1221 }
1222 appctx->st0 = PEER_SESS_ST_END;
1223 }
1224
1225 return ret;
1226}
1227
1228/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001229 * Send a hello message.
1230 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1231 * Returns -1 if there was not enough room left to send the message,
1232 * any other negative returned value must be considered as an error with an appcxt st0
1233 * returned value equal to PEER_SESS_ST_END.
1234 */
1235static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1236{
1237 struct peer_prep_params p = {
1238 .hello.peer = peer,
1239 };
1240
1241 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1242}
1243
1244/*
1245 * Send a success peer handshake status message.
1246 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1247 * Returns -1 if there was not enough room left to send the message,
1248 * any other negative returned value must be considered as an error with an appcxt st0
1249 * returned value equal to PEER_SESS_ST_END.
1250 */
1251static inline int peer_send_status_successmsg(struct appctx *appctx)
1252{
1253 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1254}
1255
1256/*
1257 * Send a peer handshake status error message.
1258 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1259 * Returns -1 if there was not enough room left to send the message,
1260 * any other negative returned value must be considered as an error with an appcxt st0
1261 * returned value equal to PEER_SESS_ST_END.
1262 */
1263static inline int peer_send_status_errormsg(struct appctx *appctx)
1264{
1265 struct peer_prep_params p = {
1266 .error_status.st1 = appctx->st1,
1267 };
1268
1269 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1270}
1271
1272/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001273 * Send a stick-table switch message.
1274 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1275 * Returns -1 if there was not enough room left to send the message,
1276 * any other negative returned value must be considered as an error with an appcxt st0
1277 * returned value equal to PEER_SESS_ST_END.
1278 */
1279static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1280{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001281 struct peer_prep_params p = {
1282 .swtch.shared_table = st,
1283 };
1284
1285 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001286}
1287
1288/*
1289 * Send a stick-table update acknowledgement message.
1290 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1291 * Returns -1 if there was not enough room left to send the message,
1292 * any other negative returned value must be considered as an error with an appcxt st0
1293 * returned value equal to PEER_SESS_ST_END.
1294 */
1295static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1296{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001297 struct peer_prep_params p = {
1298 .ack.shared_table = st,
1299 };
1300
1301 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001302}
1303
1304/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001305 * Send a stick-table update message.
1306 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1307 * Returns -1 if there was not enough room left to send the message,
1308 * any other negative returned value must be considered as an error with an appcxt st0
1309 * returned value equal to PEER_SESS_ST_END.
1310 */
1311static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1312 unsigned int updateid, int use_identifier, int use_timed)
1313{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001314 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001315 .updt = {
1316 .stksess = ts,
1317 .shared_table = st,
1318 .updateid = updateid,
1319 .use_identifier = use_identifier,
1320 .use_timed = use_timed,
Willy Tarreau455caef2022-05-05 20:16:16 +02001321 .peer = appctx->svcctx,
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001322 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001323 };
1324
1325 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001326}
1327
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001328/*
1329 * Build a peer protocol control class message.
1330 * Returns the number of written bytes used to build the message if succeeded,
1331 * 0 if not.
1332 */
1333static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1334{
1335 if (size < sizeof p->control.head)
1336 return 0;
1337
1338 msg[0] = p->control.head[0];
1339 msg[1] = p->control.head[1];
1340
1341 return 2;
1342}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001343
1344/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001345 * Send a stick-table synchronization request message.
1346 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1347 * Returns -1 if there was not enough room left to send the message,
1348 * any other negative returned value must be considered as an error with an appctx st0
1349 * returned value equal to PEER_SESS_ST_END.
1350 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001351static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1352 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001353{
1354 struct peer_prep_params p = {
1355 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1356 };
1357
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001358 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1359 NULL, &p.control.head[1], peers->local->id, peer->id);
1360
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001361 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1362}
1363
1364/*
1365 * Send a stick-table synchronization confirmation message.
1366 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1367 * Returns -1 if there was not enough room left to send the message,
1368 * any other negative returned value must be considered as an error with an appctx st0
1369 * returned value equal to PEER_SESS_ST_END.
1370 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001371static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1372 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001373{
1374 struct peer_prep_params p = {
1375 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1376 };
1377
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001378 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1379 NULL, &p.control.head[1], peers->local->id, peer->id);
1380
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001381 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1382}
1383
1384/*
1385 * Send a stick-table synchronization finished message.
1386 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1387 * Returns -1 if there was not enough room left to send the message,
1388 * any other negative returned value must be considered as an error with an appctx st0
1389 * returned value equal to PEER_SESS_ST_END.
1390 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001391static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1392 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001393{
1394 struct peer_prep_params p = {
1395 .control.head = { PEER_MSG_CLASS_CONTROL, },
1396 };
1397
Emeric Brun70de43b2020-03-16 10:51:01 +01001398 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001399 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1400
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001401 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1402 NULL, &p.control.head[1], peers->local->id, peer->id);
1403
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001404 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1405}
1406
1407/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001408 * Send a heartbeat message.
1409 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1410 * Returns -1 if there was not enough room left to send the message,
1411 * any other negative returned value must be considered as an error with an appctx st0
1412 * returned value equal to PEER_SESS_ST_END.
1413 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001414static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1415 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001416{
1417 struct peer_prep_params p = {
1418 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1419 };
1420
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001421 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1422 NULL, &p.control.head[1], peers->local->id, peer->id);
1423
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001424 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1425}
1426
1427/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001428 * Build a peer protocol error class message.
1429 * Returns the number of written bytes used to build the message if succeeded,
1430 * 0 if not.
1431 */
1432static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1433{
1434 if (size < sizeof p->error.head)
1435 return 0;
1436
1437 msg[0] = p->error.head[0];
1438 msg[1] = p->error.head[1];
1439
1440 return 2;
1441}
1442
1443/*
1444 * Send a "size limit reached" error message.
1445 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1446 * Returns -1 if there was not enough room left to send the message,
1447 * any other negative returned value must be considered as an error with an appctx st0
1448 * returned value equal to PEER_SESS_ST_END.
1449 */
1450static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1451{
1452 struct peer_prep_params p = {
1453 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1454 };
1455
1456 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1457}
1458
1459/*
1460 * Send a "peer protocol" error message.
1461 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1462 * Returns -1 if there was not enough room left to send the message,
1463 * any other negative returned value must be considered as an error with an appctx st0
1464 * returned value equal to PEER_SESS_ST_END.
1465 */
1466static inline int peer_send_error_protomsg(struct appctx *appctx)
1467{
1468 struct peer_prep_params p = {
1469 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1470 };
1471
1472 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1473}
1474
1475/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001476 * Function used to lookup for recent stick-table updates associated with
1477 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1478 */
1479static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1480{
1481 struct eb32_node *eb;
1482
1483 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1484 if (!eb) {
1485 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001486 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001487 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1488 return NULL;
1489 }
1490 }
1491
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001492 /* if distance between the last pushed and the retrieved key
1493 * is greater than the distance last_pushed and the local_update
1494 * this means we are beyond localupdate.
1495 */
1496 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001497 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1498 return NULL;
1499 }
1500
1501 return eb32_entry(eb, struct stksess, upd);
1502}
1503
1504/*
1505 * Function used to lookup for recent stick-table updates associated with
1506 * <st> shared stick-table during teach state 1 step.
1507 */
1508static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1509{
1510 struct eb32_node *eb;
1511
1512 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1513 if (!eb) {
1514 st->flags |= SHTABLE_F_TEACH_STAGE1;
1515 eb = eb32_first(&st->table->updates);
1516 if (eb)
1517 st->last_pushed = eb->key - 1;
1518 return NULL;
1519 }
1520
1521 return eb32_entry(eb, struct stksess, upd);
1522}
1523
1524/*
1525 * Function used to lookup for recent stick-table updates associated with
1526 * <st> shared stick-table during teach state 2 step.
1527 */
1528static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1529{
1530 struct eb32_node *eb;
1531
1532 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1533 if (!eb || eb->key > st->teaching_origin) {
1534 st->flags |= SHTABLE_F_TEACH_STAGE2;
1535 return NULL;
1536 }
1537
1538 return eb32_entry(eb, struct stksess, upd);
1539}
1540
1541/*
1542 * Generic function to emit update messages for <st> stick-table when a lesson must
1543 * be taught to the peer <p>.
1544 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1545 * this function, 0 if not.
1546 *
1547 * This function temporary unlock/lock <st> when it sends stick-table updates or
1548 * when decrementing its refcount in case of any error when it sends this updates.
1549 *
1550 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1551 * Returns -1 if there was not enough room left to send the message,
1552 * any other negative returned value must be considered as an error with an appcxt st0
1553 * returned value equal to PEER_SESS_ST_END.
1554 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1555 * unlocked if not already locked when entering this function.
1556 */
1557static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1558 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1559 struct shared_table *st, int locked)
1560{
1561 int ret, new_pushed, use_timed;
1562
1563 ret = 1;
1564 use_timed = 0;
1565 if (st != p->last_local_table) {
1566 ret = peer_send_switchmsg(st, appctx);
1567 if (ret <= 0)
1568 return ret;
1569
1570 p->last_local_table = st;
1571 }
1572
1573 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1574 use_timed = !(p->flags & PEER_F_DWNGRD);
1575
1576 /* We force new pushed to 1 to force identifier in update message */
1577 new_pushed = 1;
1578
1579 if (!locked)
1580 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1581
1582 while (1) {
1583 struct stksess *ts;
1584 unsigned updateid;
1585
1586 /* push local updates */
1587 ts = peer_stksess_lookup(st);
1588 if (!ts)
1589 break;
1590
1591 updateid = ts->upd.key;
1592 ts->ref_cnt++;
1593 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1594
1595 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1596 if (ret <= 0) {
1597 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1598 ts->ref_cnt--;
1599 if (!locked)
1600 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1601 return ret;
1602 }
1603
1604 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1605 ts->ref_cnt--;
1606 st->last_pushed = updateid;
1607
1608 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1609 (int)(st->last_pushed - st->table->commitupdate) > 0)
1610 st->table->commitupdate = st->last_pushed;
1611
1612 /* identifier may not needed in next update message */
1613 new_pushed = 0;
1614 }
1615
1616 out:
1617 if (!locked)
1618 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1619 return 1;
1620}
1621
1622/*
1623 * Function to emit update messages for <st> stick-table when a lesson must
1624 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1625 *
1626 * Note that <st> shared stick-table is locked when calling this function.
1627 *
1628 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1629 * Returns -1 if there was not enough room left to send the message,
1630 * any other negative returned value must be considered as an error with an appcxt st0
1631 * returned value equal to PEER_SESS_ST_END.
1632 */
1633static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1634 struct shared_table *st)
1635{
1636 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1637}
1638
1639/*
1640 * Function to emit update messages for <st> stick-table when a lesson must
1641 * be taught to the peer <p> during teach state 1 step.
1642 *
1643 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1644 * Returns -1 if there was not enough room left to send the message,
1645 * any other negative returned value must be considered as an error with an appcxt st0
1646 * returned value equal to PEER_SESS_ST_END.
1647 */
1648static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1649 struct shared_table *st)
1650{
1651 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1652}
1653
1654/*
1655 * Function to emit update messages for <st> stick-table when a lesson must
1656 * be taught to the peer <p> during teach state 1 step.
1657 *
1658 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1659 * Returns -1 if there was not enough room left to send the message,
1660 * any other negative returned value must be considered as an error with an appcxt st0
1661 * returned value equal to PEER_SESS_ST_END.
1662 */
1663static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1664 struct shared_table *st)
1665{
1666 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1667}
1668
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001669
1670/*
1671 * Function used to parse a stick-table update message after it has been received
1672 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1673 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1674 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1675 * was encountered.
1676 * <exp> must be set if the stick-table entry expires.
1677 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001678 * 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 +01001679 * update ID.
1680 * <totl> is the length of the stick-table update message computed upon receipt.
1681 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001682static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1683 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001684{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001685 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001686 struct shared_table *st = p->remote_table;
1687 struct stksess *ts, *newts;
1688 uint32_t update;
1689 int expire;
1690 unsigned int data_type;
1691 void *data_ptr;
1692
Frédéric Lécailled8659352020-11-10 16:18:03 +01001693 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001694 /* Here we have data message */
1695 if (!st)
1696 goto ignore_msg;
1697
1698 expire = MS_TO_TICKS(st->table->expire);
1699
1700 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001701 if (msg_len < sizeof(update)) {
1702 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001703 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001704 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001705
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001706 memcpy(&update, *msg_cur, sizeof(update));
1707 *msg_cur += sizeof(update);
1708 st->last_get = htonl(update);
1709 }
1710 else {
1711 st->last_get++;
1712 }
1713
1714 if (exp) {
1715 size_t expire_sz = sizeof expire;
1716
Frédéric Lécailled8659352020-11-10 16:18:03 +01001717 if (*msg_cur + expire_sz > msg_end) {
1718 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1719 NULL, p, *msg_cur);
1720 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1721 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001722 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001723 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001724
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001725 memcpy(&expire, *msg_cur, expire_sz);
1726 *msg_cur += expire_sz;
1727 expire = ntohl(expire);
1728 }
1729
1730 newts = stksess_new(st->table, NULL);
1731 if (!newts)
1732 goto ignore_msg;
1733
1734 if (st->table->type == SMP_T_STR) {
1735 unsigned int to_read, to_store;
1736
1737 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001738 if (!*msg_cur) {
1739 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001740 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001741 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001742
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001743 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001744 if (*msg_cur + to_store > msg_end) {
1745 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1746 NULL, p, *msg_cur);
1747 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1748 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001749 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001750 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001751
1752 memcpy(newts->key.key, *msg_cur, to_store);
1753 newts->key.key[to_store] = 0;
1754 *msg_cur += to_read;
1755 }
1756 else if (st->table->type == SMP_T_SINT) {
1757 unsigned int netinteger;
1758
Frédéric Lécailled8659352020-11-10 16:18:03 +01001759 if (*msg_cur + sizeof(netinteger) > msg_end) {
1760 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1761 NULL, p, *msg_cur);
1762 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1763 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001764 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001765 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001766
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001767 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1768 netinteger = ntohl(netinteger);
1769 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1770 *msg_cur += sizeof(netinteger);
1771 }
1772 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001773 if (*msg_cur + st->table->key_size > msg_end) {
1774 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1775 NULL, p, *msg_cur);
1776 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1777 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001778 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001779 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001780
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001781 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1782 *msg_cur += st->table->key_size;
1783 }
1784
1785 /* lookup for existing entry */
1786 ts = stktable_set_entry(st->table, newts);
1787 if (ts != newts) {
1788 stksess_free(st->table, newts);
1789 newts = NULL;
1790 }
1791
1792 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1793
1794 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001795 uint64_t decoded_int;
Emeric Brun90a9b672021-06-22 16:09:55 +02001796 unsigned int idx;
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001797 int ignore;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001798
Emeric Brun08b0f672021-07-01 18:54:05 +02001799 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001800 continue;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001801
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001802 ignore = stktable_data_types[data_type].is_local;
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001803
Emeric Brun90a9b672021-06-22 16:09:55 +02001804 if (stktable_data_types[data_type].is_array) {
1805 /* in case of array all elements
1806 * use the same std_type and they
1807 * are linearly encoded.
1808 * The number of elements was provided
1809 * by table definition message
1810 */
1811 switch (stktable_data_types[data_type].std_type) {
1812 case STD_T_SINT:
1813 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1814 decoded_int = intdecode(msg_cur, msg_end);
1815 if (!*msg_cur) {
1816 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1817 goto malformed_unlock;
1818 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001819
Emeric Brun90a9b672021-06-22 16:09:55 +02001820 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001821 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001822 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
1823 }
1824 break;
1825 case STD_T_UINT:
1826 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1827 decoded_int = intdecode(msg_cur, msg_end);
1828 if (!*msg_cur) {
1829 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1830 goto malformed_unlock;
1831 }
1832
1833 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001834 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001835 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
1836 }
1837 break;
1838 case STD_T_ULL:
1839 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1840 decoded_int = intdecode(msg_cur, msg_end);
1841 if (!*msg_cur) {
1842 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1843 goto malformed_unlock;
1844 }
1845
1846 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001847 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001848 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
1849 }
1850 break;
1851 case STD_T_FRQP:
1852 for (idx = 0; idx < st->remote_data_nbelem[data_type]; idx++) {
1853 struct freq_ctr data;
1854
1855 /* First bit is reserved for the freq_ctr lock
1856 * Note: here we're still protected by the stksess lock
1857 * so we don't need to update the update the freq_ctr
1858 * using its internal lock.
1859 */
1860
1861 decoded_int = intdecode(msg_cur, msg_end);
1862 if (!*msg_cur) {
1863 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1864 goto malformed_unlock;
1865 }
1866
1867 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
1868 data.curr_ctr = intdecode(msg_cur, msg_end);
1869 if (!*msg_cur) {
1870 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1871 goto malformed_unlock;
1872 }
1873
1874 data.prev_ctr = intdecode(msg_cur, msg_end);
1875 if (!*msg_cur) {
1876 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
1877 goto malformed_unlock;
1878 }
1879
1880 data_ptr = stktable_data_ptr_idx(st->table, ts, data_type, idx);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001881 if (data_ptr && !ignore)
Emeric Brun90a9b672021-06-22 16:09:55 +02001882 stktable_data_cast(data_ptr, std_t_frqp) = data;
1883 }
1884 break;
1885 }
1886
1887 /* array is fully decoded
1888 * proceed next data_type.
1889 */
1890 continue;
1891 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001892 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001893 if (!*msg_cur) {
1894 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001895 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001896 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001897
Willy Tarreau1e82a142019-01-29 11:08:06 +01001898 switch (stktable_data_types[data_type].std_type) {
1899 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001900 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001901 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001902 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001903 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001904
Willy Tarreau1e82a142019-01-29 11:08:06 +01001905 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001906 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001907 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001908 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001909 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001910
Willy Tarreau1e82a142019-01-29 11:08:06 +01001911 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001912 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001913 if (data_ptr && !ignore)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001914 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001915 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001916
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001917 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001918 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001919
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001920 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001921 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001922 so we don't need to update the update the freq_ctr
Emeric Brun90a9b672021-06-22 16:09:55 +02001923 using its internal lock.
1924 */
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001925
Willy Tarreau1e82a142019-01-29 11:08:06 +01001926 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001927 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001928 if (!*msg_cur) {
1929 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001930 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001931 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001932
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001933 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001934 if (!*msg_cur) {
1935 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001936 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001937 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001938
1939 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01001940 if (data_ptr && !ignore)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001941 stktable_data_cast(data_ptr, std_t_frqp) = data;
1942 break;
1943 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001944 case STD_T_DICT: {
1945 struct buffer *chunk;
1946 size_t data_len, value_len;
1947 unsigned int id;
1948 struct dict_entry *de;
1949 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001950 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001951
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001952 if (!decoded_int) {
1953 /* No entry. */
1954 break;
1955 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001956 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001957 if (*msg_cur + data_len > msg_end) {
1958 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1959 NULL, p, *msg_cur);
1960 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1961 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001962 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001963 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001964
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001965 /* Compute the end of the current data, <msg_end> being at the end of
1966 * the entire message.
1967 */
1968 end = *msg_cur + data_len;
1969 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001970 if (!*msg_cur || !id) {
1971 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1972 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001973 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001974 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001975
1976 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001977 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001978 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001979 if (id > dc->max_entries) {
1980 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1981 NULL, p, NULL, &id);
1982 goto malformed_unlock;
1983 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001984 /* IDs sent over the network are numbered from 1. */
1985 de = dc->rx[id - 1].de;
1986 }
1987 else {
1988 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001989 value_len = intdecode(msg_cur, end);
1990 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01001991 unlikely(value_len + 1 >= chunk->size)) {
1992 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1993 NULL, p, *msg_cur, &value_len);
1994 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1995 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001996 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001997 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001998
1999 chunk_memcpy(chunk, *msg_cur, value_len);
2000 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02002001 *msg_cur += value_len;
2002
Thayne McCombs92149f92020-11-20 01:28:26 -07002003 de = dict_insert(&server_key_dict, chunk->area);
2004 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002005 dc->rx[id - 1].de = de;
2006 }
2007 if (de) {
2008 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Willy Tarreaub4ff6f42021-12-24 13:38:49 +01002009 if (data_ptr && !ignore) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002010 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002011 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07002012 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002013 }
2014 break;
2015 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002016 }
2017 }
2018 /* Force new expiration */
2019 ts->expire = tick_add(now_ms, expire);
2020
2021 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2022 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002023 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002024 return 1;
2025
2026 ignore_msg:
2027 /* skip consumed message */
Willy Tarreauda304902022-05-27 10:44:04 +02002028 co_skip(sc_oc(sc), totl);
Frédéric Lécailled8659352020-11-10 16:18:03 +01002029 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002030 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01002031
2032 malformed_unlock:
2033 /* malformed message */
2034 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2035 stktable_touch_remote(st->table, ts, 1);
2036 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002037 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002038 return 0;
2039
2040 malformed_free_newts:
2041 /* malformed message */
2042 stksess_free(st->table, newts);
2043 malformed_exit:
2044 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01002045 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01002046 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01002047}
2048
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01002049/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002050 * Function used to parse a stick-table update acknowledgement message after it
2051 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
2052 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2053 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2054 * was encountered.
2055 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2056 */
2057static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
2058 char **msg_cur, char *msg_end)
2059{
2060 /* ack message */
2061 uint32_t table_id ;
2062 uint32_t update;
2063 struct shared_table *st;
2064
Emeric Brunb0d60be2021-03-04 10:27:10 +01002065 /* ignore ack during teaching process */
2066 if (p->flags & PEER_F_TEACH_PROCESS)
2067 return 1;
2068
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002069 table_id = intdecode(msg_cur, msg_end);
2070 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
2071 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002072
2073 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
2074 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002075 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2076 return 0;
2077 }
2078
2079 memcpy(&update, *msg_cur, sizeof(update));
2080 update = ntohl(update);
2081
2082 for (st = p->tables; st; st = st->next) {
2083 if (st->local_id == table_id) {
2084 st->update = update;
2085 break;
2086 }
2087 }
2088
2089 return 1;
2090}
2091
2092/*
2093 * Function used to parse a stick-table switch message after it has been received
2094 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2095 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2096 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2097 * was encountered.
2098 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2099 */
2100static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
2101 char **msg_cur, char *msg_end)
2102{
2103 struct shared_table *st;
2104 int table_id;
2105
2106 table_id = intdecode(msg_cur, msg_end);
2107 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002108 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002109 /* malformed message */
2110 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2111 return 0;
2112 }
2113
2114 p->remote_table = NULL;
2115 for (st = p->tables; st; st = st->next) {
2116 if (st->remote_id == table_id) {
2117 p->remote_table = st;
2118 break;
2119 }
2120 }
2121
2122 return 1;
2123}
2124
2125/*
2126 * Function used to parse a stick-table definition message after it has been received
2127 * by <p> peer with <msg_cur> as address of the pointer to the position in the
2128 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
2129 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
2130 * was encountered.
2131 * <totl> is the length of the stick-table update message computed upon receipt.
2132 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
2133 */
2134static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
2135 char **msg_cur, char *msg_end, int totl)
2136{
Willy Tarreauc12b3212022-05-27 11:08:15 +02002137 struct stconn *sc = appctx_sc(appctx);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002138 int table_id_len;
2139 struct shared_table *st;
2140 int table_type;
2141 int table_keylen;
2142 int table_id;
2143 uint64_t table_data;
2144
2145 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002146 if (!*msg_cur) {
2147 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002148 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002149 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002150
2151 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002152 if (!*msg_cur) {
2153 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002154 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002155 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002156
2157 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002158 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
2159 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002160 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002161 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002162
2163 for (st = p->tables; st; st = st->next) {
2164 /* Reset IDs */
2165 if (st->remote_id == table_id)
2166 st->remote_id = 0;
2167
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01002168 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
2169 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002170 p->remote_table = st;
2171 }
2172
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002173 if (!p->remote_table) {
2174 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002175 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002176 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002177
2178 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002179 if (*msg_cur >= msg_end) {
2180 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002181 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002182 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002183
2184 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002185 if (!*msg_cur) {
2186 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002187 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002188 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002189
2190 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002191 if (!*msg_cur) {
2192 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002193 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002194 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002195
2196 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002197 if (!*msg_cur) {
2198 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01002199 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002200 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002201
Emeric Brun530ba382020-06-02 11:17:42 +02002202 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002203 || p->remote_table->table->key_size != table_keylen) {
2204 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002205 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002206 goto ignore_msg;
2207 }
2208
Ilya Shipitsin01881082021-08-07 14:41:56 +05002209 /* Check if there there is the additional expire data */
Emeric Brun90a9b672021-06-22 16:09:55 +02002210 intdecode(msg_cur, msg_end);
2211 if (*msg_cur) {
2212 uint64_t data_type;
2213 uint64_t type;
2214
2215 /* This define contains the expire data so we consider
2216 * it also contain all data_types parameters.
2217 */
2218 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2219 if (table_data & (1ULL << data_type)) {
2220 if (stktable_data_types[data_type].is_array) {
2221 /* This should be an array
2222 * so we parse the data_type prefix
2223 * because we must have parameters.
2224 */
2225 type = intdecode(msg_cur, msg_end);
2226 if (!*msg_cur) {
2227 p->remote_table = NULL;
2228 TRACE_PROTO("missing meta data for array", PEERS_EV_DEFMSG, NULL, p);
2229 goto ignore_msg;
2230 }
2231
2232 /* check if the data_type match the current from the bitfield */
2233 if (type != data_type) {
2234 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002235 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002236 goto ignore_msg;
2237 }
2238
2239 /* decode the nbelem of the array */
2240 p->remote_table->remote_data_nbelem[type] = intdecode(msg_cur, msg_end);
2241 if (!*msg_cur) {
2242 p->remote_table = NULL;
2243 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2244 goto ignore_msg;
2245 }
2246
2247 /* if it is an array of frqp, we must also have the period to decode */
2248 if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2249 intdecode(msg_cur, msg_end);
2250 if (!*msg_cur) {
2251 p->remote_table = NULL;
2252 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2253 goto ignore_msg;
2254 }
2255 }
2256 }
2257 else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
2258 /* This should be a std freq counter data_type
2259 * so we parse the data_type prefix
2260 * because we must have parameters.
2261 */
2262 type = intdecode(msg_cur, msg_end);
2263 if (!*msg_cur) {
2264 p->remote_table = NULL;
2265 TRACE_PROTO("missing meta data for frqp", PEERS_EV_DEFMSG, NULL, p);
2266 goto ignore_msg;
2267 }
2268
2269 /* check if the data_type match the current from the bitfield */
2270 if (type != data_type) {
2271 p->remote_table = NULL;
Ilya Shipitsin01881082021-08-07 14:41:56 +05002272 TRACE_PROTO("meta data mismatch type", PEERS_EV_DEFMSG, NULL, p);
Emeric Brun90a9b672021-06-22 16:09:55 +02002273 goto ignore_msg;
2274 }
2275
2276 /* decode the period */
2277 intdecode(msg_cur, msg_end);
2278 if (!*msg_cur) {
2279 p->remote_table = NULL;
2280 TRACE_PROTO("missing period for frqp", PEERS_EV_DEFMSG, NULL, p);
2281 goto ignore_msg;
2282 }
2283 }
2284 }
2285 }
2286 }
2287 else {
2288 uint64_t data_type;
2289
2290 /* There is not additional data but
2291 * array size parameter is mandatory to parse array
2292 * so we consider an error if an array data_type is define
2293 * but there is no additional data.
2294 */
2295 for (data_type = 0; data_type < STKTABLE_DATA_TYPES; data_type++) {
2296 if (table_data & (1ULL << data_type)) {
2297 if (stktable_data_types[data_type].is_array) {
2298 p->remote_table = NULL;
2299 TRACE_PROTO("missing array size meta data for array", PEERS_EV_DEFMSG, NULL, p);
2300 goto ignore_msg;
2301 }
2302 }
2303 }
2304 }
2305
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002306 p->remote_table->remote_data = table_data;
2307 p->remote_table->remote_id = table_id;
2308 return 1;
2309
2310 ignore_msg:
Willy Tarreauda304902022-05-27 10:44:04 +02002311 co_skip(sc_oc(sc), totl);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002312 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01002313
2314 malformed_exit:
2315 /* malformed message */
2316 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2317 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002318}
2319
2320/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002321 * Receive a stick-table message or pre-parse any other message.
2322 * The message's header will be sent into <msg_head> which must be at least
2323 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
2324 * The first two bytes are always read, and the rest is only read if the
2325 * first bytes indicate a stick-table message. If the message is a stick-table
2326 * message, the varint is decoded and the equivalent number of bytes will be
2327 * copied into the trash at trash.area. <totl> is incremented by the number of
2328 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002329 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
2330 * -1 if there was an error updating the appctx state st0 accordingly.
2331 */
2332static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2333 uint32_t *msg_len, int *totl)
2334{
2335 int reql;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002336 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002337 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002338
Willy Tarreauda304902022-05-27 10:44:04 +02002339 reql = co_getblk(sc_oc(sc), msg_head, 2 * sizeof(char), *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002340 if (reql <= 0) /* closed or EOL not found */
2341 goto incomplete;
2342
2343 *totl += reql;
2344
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002345 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002346 return 1;
2347
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002348 /* This is a stick-table message, let's go on */
2349
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002350 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002351 msg_head += *totl;
2352 msg_head_sz -= *totl;
Willy Tarreauda304902022-05-27 10:44:04 +02002353 reql = co_data(sc_oc(sc)) - *totl;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002354 if (reql > msg_head_sz)
2355 reql = msg_head_sz;
2356
Willy Tarreauda304902022-05-27 10:44:04 +02002357 reql = co_getblk(sc_oc(sc), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002358 if (reql <= 0) /* closed */
2359 goto incomplete;
2360
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002361 cur = msg_head;
2362 *msg_len = intdecode(&cur, cur + reql);
2363 if (!cur) {
2364 /* the number is truncated, did we read enough ? */
2365 if (reql < msg_head_sz)
2366 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002367
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002368 /* malformed message */
2369 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2370 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2371 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002372 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002373 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002374
2375 /* Read message content */
2376 if (*msg_len) {
2377 if (*msg_len > trash.size) {
2378 /* Status code is not success, abort */
2379 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2380 return -1;
2381 }
2382
Willy Tarreauda304902022-05-27 10:44:04 +02002383 reql = co_getblk(sc_oc(sc), trash.area, *msg_len, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002384 if (reql <= 0) /* closed */
2385 goto incomplete;
2386 *totl += reql;
2387 }
2388
2389 return 1;
2390
2391 incomplete:
Willy Tarreauda304902022-05-27 10:44:04 +02002392 if (reql < 0 || (sc_oc(sc)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002393 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002394 appctx->st0 = PEER_SESS_ST_END;
2395 return -1;
2396 }
2397
2398 return 0;
2399}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002400
2401/*
2402 * Treat the awaited message with <msg_head> as header.*
2403 * Return 1 if succeeded, 0 if not.
2404 */
2405static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2406 char **msg_cur, char *msg_end, int msg_len, int totl)
2407{
Christopher Faulet387e7972022-05-12 14:47:52 +02002408 struct peers *peers = peer->peers;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002409
2410 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2411 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2412 struct shared_table *st;
2413 /* Reset message: remote need resync */
2414
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);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002417 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002418 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002419 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002420 st->flags = 0;
2421 }
2422
2423 /* reset teaching flags to 0 */
2424 peer->flags &= PEER_TEACH_RESET;
2425
2426 /* flag to start to teach lesson */
2427 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002428 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002429 }
2430 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002431 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2432 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002433 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2434 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2435 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2436 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002437 if (peer->local)
2438 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2439 else
2440 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002441 }
2442 peer->confirm++;
2443 }
2444 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002445 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2446 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002447 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2448 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2449 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2450
Emeric Brunccdfbae2021-04-28 12:59:35 +02002451 if (peer->local)
2452 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2453 else
2454 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002455 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002456 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002457 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2458 }
2459 peer->confirm++;
2460 }
2461 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2462 struct shared_table *st;
2463
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002464 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2465 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002466 /* If stopping state */
2467 if (stopping) {
2468 /* Close session, push resync no more needed */
2469 peer->flags |= PEER_F_TEACH_COMPLETE;
2470 appctx->st0 = PEER_SESS_ST_END;
2471 return 0;
2472 }
2473 for (st = peer->tables; st; st = st->next) {
2474 st->update = st->last_pushed = st->teaching_origin;
2475 st->flags = 0;
2476 }
2477
2478 /* reset teaching flags to 0 */
2479 peer->flags &= PEER_TEACH_RESET;
2480 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002481 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002482 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2483 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002484 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002485 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002486 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002487 }
2488 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2489 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2490 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2491 return 0;
2492 }
2493 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2494 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2495 return 0;
2496 }
2497 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2498 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2499 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2500 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2501 int update, expire;
2502
2503 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2504 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2505 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2506 msg_cur, msg_end, msg_len, totl))
2507 return 0;
2508
2509 }
2510 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2511 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2512 return 0;
2513 }
2514 }
2515 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2516 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2517 return 0;
2518 }
2519
2520 return 1;
2521}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002522
2523
2524/*
2525 * Send any message to <peer> peer.
2526 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002527 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002528 * to a peer state change (from the peer I/O handler point of view).
2529 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002530static inline int peer_send_msgs(struct appctx *appctx,
2531 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002532{
2533 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002534
2535 /* Need to request a resync */
2536 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2537 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2538 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2539
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002540 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002541 if (repl <= 0)
2542 return repl;
2543
2544 peers->flags |= PEERS_F_RESYNC_PROCESS;
2545 }
2546
2547 /* Nothing to read, now we start to write */
2548 if (peer->tables) {
2549 struct shared_table *st;
2550 struct shared_table *last_local_table;
2551
2552 last_local_table = peer->last_local_table;
2553 if (!last_local_table)
2554 last_local_table = peer->tables;
2555 st = last_local_table->next;
2556
2557 while (1) {
2558 if (!st)
2559 st = peer->tables;
2560
2561 /* It remains some updates to ack */
2562 if (st->last_get != st->last_acked) {
2563 repl = peer_send_ackmsg(st, appctx);
2564 if (repl <= 0)
2565 return repl;
2566
2567 st->last_acked = st->last_get;
2568 }
2569
2570 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2571 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2572 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002573 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002574
2575 repl = peer_send_teach_process_msgs(appctx, peer, st);
2576 if (repl <= 0) {
2577 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2578 return repl;
2579 }
2580 }
2581 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2582 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002583 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002584 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2585 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2586 if (repl <= 0)
2587 return repl;
2588 }
2589
2590 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2591 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2592 if (repl <= 0)
2593 return repl;
2594 }
2595 }
2596
2597 if (st == last_local_table)
2598 break;
2599 st = st->next;
2600 }
2601 }
2602
2603 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002604 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002605 if (repl <= 0)
2606 return repl;
2607
2608 /* flag finished message sent */
2609 peer->flags |= PEER_F_TEACH_FINISHED;
2610 }
2611
2612 /* Confirm finished or partial messages */
2613 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002614 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002615 if (repl <= 0)
2616 return repl;
2617
2618 peer->confirm--;
2619 }
2620
2621 return 1;
2622}
2623
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002624/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002625 * Read and parse a first line of a "hello" peer protocol message.
2626 * Returns 0 if could not read a line, -1 if there was a read error or
2627 * the line is malformed, 1 if succeeded.
2628 */
2629static inline int peer_getline_version(struct appctx *appctx,
2630 unsigned int *maj_ver, unsigned int *min_ver)
2631{
2632 int reql;
2633
2634 reql = peer_getline(appctx);
2635 if (!reql)
2636 return 0;
2637
2638 if (reql < 0)
2639 return -1;
2640
2641 /* test protocol */
2642 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2643 appctx->st0 = PEER_SESS_ST_EXIT;
2644 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2645 return -1;
2646 }
2647 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2648 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2649 appctx->st0 = PEER_SESS_ST_EXIT;
2650 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2651 return -1;
2652 }
2653
2654 return 1;
2655}
2656
2657/*
2658 * Read and parse a second line of a "hello" peer protocol message.
2659 * Returns 0 if could not read a line, -1 if there was a read error or
2660 * the line is malformed, 1 if succeeded.
2661 */
2662static inline int peer_getline_host(struct appctx *appctx)
2663{
2664 int reql;
2665
2666 reql = peer_getline(appctx);
2667 if (!reql)
2668 return 0;
2669
2670 if (reql < 0)
2671 return -1;
2672
2673 /* test hostname match */
2674 if (strcmp(localpeer, trash.area) != 0) {
2675 appctx->st0 = PEER_SESS_ST_EXIT;
2676 appctx->st1 = PEER_SESS_SC_ERRHOST;
2677 return -1;
2678 }
2679
2680 return 1;
2681}
2682
2683/*
2684 * Read and parse a last line of a "hello" peer protocol message.
2685 * Returns 0 if could not read a character, -1 if there was a read error or
2686 * the line is malformed, 1 if succeeded.
2687 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2688 */
2689static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2690{
2691 char *p;
2692 int reql;
2693 struct peer *peer;
Willy Tarreau0698c802022-05-11 14:09:57 +02002694 struct stream *s = appctx_strm(appctx);
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002695 struct peers *peers = strm_fe(s)->parent;
2696
2697 reql = peer_getline(appctx);
2698 if (!reql)
2699 return 0;
2700
2701 if (reql < 0)
2702 return -1;
2703
2704 /* parse line "<peer name> <pid> <relative_pid>" */
2705 p = strchr(trash.area, ' ');
2706 if (!p) {
2707 appctx->st0 = PEER_SESS_ST_EXIT;
2708 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2709 return -1;
2710 }
2711 *p = 0;
2712
2713 /* lookup known peer */
2714 for (peer = peers->remote; peer; peer = peer->next) {
2715 if (strcmp(peer->id, trash.area) == 0)
2716 break;
2717 }
2718
2719 /* if unknown peer */
2720 if (!peer) {
2721 appctx->st0 = PEER_SESS_ST_EXIT;
2722 appctx->st1 = PEER_SESS_SC_ERRPEER;
2723 return -1;
2724 }
2725 *curpeer = peer;
2726
2727 return 1;
2728}
2729
2730/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002731 * Init <peer> peer after having accepted it at peer protocol level.
2732 */
2733static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2734{
2735 struct shared_table *st;
2736
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002737 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002738 /* Register status code */
2739 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002740 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002741
2742 /* Awake main task */
2743 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2744
2745 /* Init confirm counter */
2746 peer->confirm = 0;
2747
2748 /* Init cursors */
2749 for (st = peer->tables; st ; st = st->next) {
2750 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002751 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2752 /* if st->update appears to be in future it means
2753 * that the last acked value is very old and we
2754 * remain unconnected a too long time to use this
2755 * acknowlegement as a reset.
2756 * We should update the protocol to be able to
2757 * signal the remote peer that it needs a full resync.
2758 * Here a partial fix consist to set st->update at
2759 * the max past value
2760 */
2761 if ((int)(st->table->localupdate - st->update) < 0)
2762 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002763 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002764 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002765 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2766 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002767 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002768 }
2769
2770 /* reset teaching and learning flags to 0 */
2771 peer->flags &= PEER_TEACH_RESET;
2772 peer->flags &= PEER_LEARN_RESET;
2773
2774 /* if current peer is local */
2775 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002776 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002777 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2778 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2779 /* assign local peer for a lesson, consider lesson already requested */
2780 peer->flags |= PEER_F_LEARN_ASSIGN;
2781 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002782 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002783 }
2784
2785 }
2786 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2787 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2788 /* assign peer for a lesson */
2789 peer->flags |= PEER_F_LEARN_ASSIGN;
2790 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002791 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002792 }
2793}
2794
2795/*
2796 * Init <peer> peer after having connected it at peer protocol level.
2797 */
2798static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2799{
2800 struct shared_table *st;
2801
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002802 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002803 /* Init cursors */
2804 for (st = peer->tables; st ; st = st->next) {
2805 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002806 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2807 /* if st->update appears to be in future it means
2808 * that the last acked value is very old and we
2809 * remain unconnected a too long time to use this
2810 * acknowlegement as a reset.
2811 * We should update the protocol to be able to
2812 * signal the remote peer that it needs a full resync.
2813 * Here a partial fix consist to set st->update at
2814 * the max past value.
2815 */
2816 if ((int)(st->table->localupdate - st->update) < 0)
2817 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002818 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002819 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002820 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2821 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002822 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002823 }
2824
2825 /* Init confirm counter */
2826 peer->confirm = 0;
2827
2828 /* reset teaching and learning flags to 0 */
2829 peer->flags &= PEER_TEACH_RESET;
2830 peer->flags &= PEER_LEARN_RESET;
2831
2832 /* If current peer is local */
2833 if (peer->local) {
2834 /* flag to start to teach lesson */
2835 peer->flags |= PEER_F_TEACH_PROCESS;
2836 }
2837 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2838 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2839 /* If peer is remote and resync from remote is needed,
2840 and no peer currently assigned */
2841
2842 /* assign peer for a lesson */
2843 peer->flags |= PEER_F_LEARN_ASSIGN;
2844 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002845 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002846 }
2847}
2848
2849/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002850 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002851 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002852static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002853{
Willy Tarreauc12b3212022-05-27 11:08:15 +02002854 struct stconn *sc = appctx_sc(appctx);
Willy Tarreauda304902022-05-27 10:44:04 +02002855 struct stream *s = __sc_strm(sc);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002856 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002857 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002858 int reql = 0;
2859 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002860 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002861 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002862
Joseph Herlant82b2f542018-11-15 12:19:14 -08002863 /* Check if the input buffer is available. */
Willy Tarreauda304902022-05-27 10:44:04 +02002864 if (sc_ib(sc)->size == 0) {
2865 sc_need_room(sc);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002866 goto out;
2867 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002868
Emeric Brun2b920a12010-09-23 18:30:22 +02002869 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002870 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002871switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002872 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002873 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002874 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002875 prev_state = appctx->st0;
Willy Tarreau455caef2022-05-05 20:16:16 +02002876 appctx->svcctx = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002877 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002878 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002879 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002880 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002881 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2882 if (reql <= 0) {
2883 if (!reql)
2884 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002885 goto switchstate;
2886 }
2887
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002888 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002889 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002890 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002891 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002892 reql = peer_getline_host(appctx);
2893 if (reql <= 0) {
2894 if (!reql)
2895 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002896 goto switchstate;
2897 }
2898
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002899 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002900 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002901 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002902 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002903 reql = peer_getline_last(appctx, &curpeer);
2904 if (reql <= 0) {
2905 if (!reql)
2906 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002907 goto switchstate;
2908 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002909
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002910 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002911 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002912 if (curpeer->local) {
2913 /* Local connection, reply a retry */
2914 appctx->st0 = PEER_SESS_ST_EXIT;
2915 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2916 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002917 }
Emeric Brun80527f52017-06-19 17:46:37 +02002918
2919 /* we're killing a connection, we must apply a random delay before
2920 * retrying otherwise the other end will do the same and we can loop
2921 * for a while.
2922 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002923 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002924 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002925 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002926 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002927 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002928 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2929 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2930 curpeer->flags |= PEER_F_DWNGRD;
2931 }
2932 else {
2933 curpeer->flags &= ~PEER_F_DWNGRD;
2934 }
2935 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002936 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002937 curpeer->flags |= PEER_F_ALIVE;
Willy Tarreau455caef2022-05-05 20:16:16 +02002938 appctx->svcctx = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002939 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002940 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002941 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002942 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002943 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002944 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002945 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02002946 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002947 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002948 if (curpeer->appctx != appctx) {
2949 appctx->st0 = PEER_SESS_ST_END;
2950 goto switchstate;
2951 }
2952 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002953
2954 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002955 if (repl <= 0) {
2956 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002957 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002958 goto switchstate;
2959 }
2960
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002961 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002962
Emeric Brun2b920a12010-09-23 18:30:22 +02002963 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002964 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002965 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002966 goto switchstate;
2967 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002968 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002969 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002970 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02002971 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002972 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002973 if (curpeer->appctx != appctx) {
2974 appctx->st0 = PEER_SESS_ST_END;
2975 goto switchstate;
2976 }
2977 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002978
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002979 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002980 if (repl <= 0) {
2981 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002982 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002983 goto switchstate;
2984 }
2985
2986 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002987 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002988 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002989 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002990 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002991 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002992 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02002993 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002994 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002995 if (curpeer->appctx != appctx) {
2996 appctx->st0 = PEER_SESS_ST_END;
2997 goto switchstate;
2998 }
2999 }
3000
Christopher Faulet642170a2022-07-27 10:49:31 +02003001 if (sc_ic(sc)->flags & CF_WROTE_DATA)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003002 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02003003
Frédéric Lécaillece025572019-01-21 13:38:06 +01003004 reql = peer_getline(appctx);
3005 if (!reql)
3006 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003007
Frédéric Lécaillece025572019-01-21 13:38:06 +01003008 if (reql < 0)
3009 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02003010
3011 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003012 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003013 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003014
3015 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02003016 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02003017
3018 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003019 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01003020 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02003021 }
3022 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02003023 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
3024 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02003025 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003026 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02003027 goto switchstate;
3028 }
Willy Tarreau4781b152021-04-06 13:53:36 +02003029 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003030 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02003031 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003032 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003033 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003034 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003035 char *msg_cur = trash.area;
3036 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01003037 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02003038 int totl = 0;
3039
Willy Tarreau2d372c22018-11-05 17:12:27 +01003040 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003041 if (!curpeer) {
Willy Tarreau455caef2022-05-05 20:16:16 +02003042 curpeer = appctx->svcctx;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003043 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003044 if (curpeer->appctx != appctx) {
3045 appctx->st0 = PEER_SESS_ST_END;
3046 goto switchstate;
3047 }
3048 }
3049
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003050 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
3051 if (reql <= 0) {
3052 if (reql == -1)
3053 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003054 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003055 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01003056
Frédéric Lécaille95203f22019-01-23 19:38:11 +01003057 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01003058 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02003059 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003060
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003061 curpeer->flags |= PEER_F_ALIVE;
3062
Emeric Brun2b920a12010-09-23 18:30:22 +02003063 /* skip consumed message */
Willy Tarreauda304902022-05-27 10:44:04 +02003064 co_skip(sc_oc(sc), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02003065 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02003066 goto switchstate;
3067
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003068send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003069 if (curpeer->flags & PEER_F_HEARTBEAT) {
3070 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003071 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003072 if (repl <= 0) {
3073 if (repl == -1)
3074 goto out;
3075 goto switchstate;
3076 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01003077 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003078 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01003079 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003080 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01003081 if (repl <= 0) {
3082 if (repl == -1)
3083 goto out;
3084 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02003085 }
3086
Emeric Brun2b920a12010-09-23 18:30:22 +02003087 /* noting more to do */
3088 goto out;
3089 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003090 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01003091 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003092 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003093 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003094 if (peer_send_status_errormsg(appctx) == -1)
3095 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003096 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003097 goto switchstate;
3098 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003099 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003100 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003101 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003102 if (peer_send_error_size_limitmsg(appctx) == -1)
3103 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003104 appctx->st0 = PEER_SESS_ST_END;
3105 goto switchstate;
3106 }
3107 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003108 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
3109 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003110 if (curpeer)
3111 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003112 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003113 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003114 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003115 if (peer_send_error_protomsg(appctx) == -1) {
3116 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01003117 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01003118 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003119 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01003120 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003121 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02003122 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003123 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01003124 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02003125 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003126 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02003127 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003128 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003129 curpeer = NULL;
3130 }
Willy Tarreauda304902022-05-27 10:44:04 +02003131 sc_shutw(sc);
3132 sc_shutr(sc);
3133 sc_ic(sc)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02003134 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02003135 }
3136 }
3137 }
3138out:
Willy Tarreauda304902022-05-27 10:44:04 +02003139 sc_oc(sc)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02003140
3141 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003142 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02003143 return;
3144}
3145
Willy Tarreau30576452015-04-13 13:50:30 +02003146static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003147 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003148 .name = "<PEER>", /* used for logging */
3149 .fct = peer_io_handler,
Christopher Faulet6712dc62022-05-12 15:36:11 +02003150 .init = peer_session_init,
Aman Gupta9a13e842012-04-02 18:57:53 -07003151 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01003152};
Emeric Brun2b920a12010-09-23 18:30:22 +02003153
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003154
Emeric Brun2b920a12010-09-23 18:30:22 +02003155/*
3156 * Use this function to force a close of a peer session
3157 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003158static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003159{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003160 struct appctx *appctx = peer->appctx;
3161
Frédéric Lécaille5df11902017-06-13 16:39:57 +02003162 /* Note that the peer sessions which have just been created
3163 * (->st0 == PEER_SESS_ST_CONNECT) must not
3164 * be shutdown, if not, the TCP session will never be closed
3165 * and stay in CLOSE_WAIT state after having been closed by
3166 * the remote side.
3167 */
3168 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003169 return;
3170
Willy Tarreau81bc3b02016-10-31 17:37:39 +01003171 if (appctx->applet != &peer_applet)
3172 return;
3173
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003174 __peer_session_deinit(peer);
3175
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003176 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01003177 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003178}
3179
Willy Tarreau91d96282015-03-13 15:47:26 +01003180/* Pre-configures a peers frontend to accept incoming connections */
3181void peers_setup_frontend(struct proxy *fe)
3182{
3183 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02003184 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02003185 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01003186 fe->maxconn = 0;
3187 fe->conn_retries = CONN_RETRIES;
3188 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01003189 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01003190 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01003191 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
3192}
3193
Emeric Brun2b920a12010-09-23 18:30:22 +02003194/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01003195 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02003196 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003197static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02003198{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003199 struct appctx *appctx;
Maciej Zdeb34e40852022-05-16 17:27:28 +02003200 unsigned int thr = 0;
3201 int idx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003202
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01003203 peer->new_conn++;
Christopher Fauletab4b0942022-07-26 19:14:36 +02003204 peer->reconnect = tick_add(now_ms, (stopping ? MS_TO_TICKS(PEER_LOCAL_RECONNECT_TIMEOUT) : MS_TO_TICKS(PEER_RECONNECT_TIMEOUT)));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003205 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003206 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003207 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003208
Maciej Zdeb34e40852022-05-16 17:27:28 +02003209 for (idx = 0; idx < global.nbthread; idx++)
3210 thr = peers->applet_count[idx] < peers->applet_count[thr] ? idx : thr;
3211 appctx = appctx_new_on(&peer_applet, NULL, thr);
Christopher Faulet2479e5f2022-01-19 14:50:11 +01003212 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003213 goto out_close;
Willy Tarreau455caef2022-05-05 20:16:16 +02003214 appctx->svcctx = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02003215
Christopher Faulet6712dc62022-05-12 15:36:11 +02003216 appctx->st0 = PEER_SESS_ST_CONNECT;
3217 peer->appctx = appctx;
Christopher Fauleta9e8b392022-03-23 11:01:09 +01003218
Maciej Zdebd01be2a2022-05-16 17:26:20 +02003219 HA_ATOMIC_INC(&peers->applet_count[thr]);
Maciej Zdeb34e40852022-05-16 17:27:28 +02003220 appctx_wakeup(appctx);
Willy Tarreau9df94c22016-10-31 18:42:52 +01003221 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02003222
Emeric Brun2b920a12010-09-23 18:30:22 +02003223 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01003224 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02003225}
3226
3227/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003228 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01003229 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
3230 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02003231 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003232struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02003233{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003234 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003235 struct peer *ps;
3236 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02003237
3238 task->expire = TICK_ETERNITY;
3239
Emeric Brunb3971ab2015-05-12 18:49:09 +02003240 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003241 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003242 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02003243 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02003244 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02003245 return NULL;
3246 }
3247
Emeric Brun80527f52017-06-19 17:46:37 +02003248 /* Acquire lock for all peers of the section */
3249 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003250 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003251
Emeric Brun2b920a12010-09-23 18:30:22 +02003252 if (!stopping) {
3253 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02003254
Emeric Brun2c4ab412021-04-21 16:06:35 +02003255 /* resync timeout set to TICK_ETERNITY means we just start
3256 * a new process and timer was not initialized.
3257 * We must arm this timer to switch to a request to a remote
3258 * node if incoming connection from old local process never
3259 * comes.
3260 */
3261 if (peers->resync_timeout == TICK_ETERNITY)
3262 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3263
Emeric Brunb3971ab2015-05-12 18:49:09 +02003264 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
3265 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
3266 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003267 /* Resync from local peer needed
3268 no peer was assigned for the lesson
3269 and no old local peer found
3270 or resync timeout expire */
3271
3272 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003273 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003274 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003275
3276 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003277 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02003278 }
3279
3280 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003281 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003282 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003283 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003284 if (!ps->appctx) {
3285 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003286 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003287 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003288 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003289 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003290 tick_is_expired(ps->reconnect, now_ms))) {
3291 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003292 * or previous peer connection established with success
3293 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003294 * and reconnection timer is expired */
3295
3296 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003297 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003298 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003299 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003300 /* If previous session failed during connection
3301 * but reconnection timer is not expired */
3302
3303 /* reschedule task for reconnect */
3304 task->expire = tick_first(task->expire, ps->reconnect);
3305 }
3306 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003307 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003308 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003309 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003310 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3311 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003312 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3313 /* Resync from a remote is needed
3314 * and no peer was assigned for lesson
3315 * and current peer may be up2date */
3316
3317 /* assign peer for the lesson */
3318 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003319 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003320 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003321
Willy Tarreau9df94c22016-10-31 18:42:52 +01003322 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003323 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003324 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003325 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003326 int update_to_push = 0;
3327
Emeric Brunb3971ab2015-05-12 18:49:09 +02003328 /* Awake session if there is data to push */
3329 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003330 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003331 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003332 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003333 /* There is no need to send a heartbeat message
3334 * when some updates must be pushed. The remote
3335 * peer will consider <ps> peer as alive when it will
3336 * receive these updates.
3337 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003338 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003339 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003340 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003341 /* We are going to send updates, let's ensure we will
3342 * come back to send heartbeat messages or to reconnect.
3343 */
3344 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003345 appctx_wakeup(ps->appctx);
3346 break;
3347 }
3348 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003349 /* When there are updates to send we do not reconnect
3350 * and do not send heartbeat message either.
3351 */
3352 if (!update_to_push) {
3353 if (tick_is_expired(ps->reconnect, now_ms)) {
3354 if (ps->flags & PEER_F_ALIVE) {
3355 /* This peer was alive during a 'reconnect' period.
3356 * Flag it as not alive again for the next period.
3357 */
3358 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003359 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003360 }
3361 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003362 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003363 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003364 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003365 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003366 }
3367 }
3368 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3369 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3370 ps->flags |= PEER_F_HEARTBEAT;
3371 appctx_wakeup(ps->appctx);
3372 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003373 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003374 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003375 }
3376 /* else do nothing */
3377 } /* SUCCESSCODE */
3378 } /* !ps->peer->local */
3379 } /* for */
3380
3381 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003382 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3383 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3384 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003385 /* Resync from remote peer needed
3386 * no peer was assigned for the lesson
3387 * and resync timeout expire */
3388
3389 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003390 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003391 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003392 }
3393
Emeric Brunb3971ab2015-05-12 18:49:09 +02003394 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003395 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003396 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3397 if (!tick_is_expired(peers->resync_timeout, now_ms))
3398 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003399 }
3400 } /* !stopping */
3401 else {
3402 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003403 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003404 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003405 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003406 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003407 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003408 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003409
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003410 /* disconnect all connected peers to process a local sync
3411 * this must be done only the first time we are switching
3412 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003413 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003414 for (ps = peers->remote; ps; ps = ps->next) {
3415 /* we're killing a connection, we must apply a random delay before
3416 * retrying otherwise the other end will do the same and we can loop
3417 * for a while.
3418 */
3419 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3420 if (ps->appctx) {
3421 peer_session_forceshutdown(ps);
3422 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003423 }
Christopher Faulet160fff62022-07-26 19:19:18 +02003424
3425 /* Set resync timeout for the local peer and request a immediate reconnect */
3426 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
3427 peers->local->reconnect = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02003428 }
3429 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003430
Emeric Brunb3971ab2015-05-12 18:49:09 +02003431 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003432 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003433 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003434 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003435 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003436 peers->flags &= ~PEERS_F_DONOTSTOP;
3437 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003438 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003439 }
3440 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003441 else if (!ps->appctx) {
3442 /* If there's no active peer connection */
Christopher Faulet160fff62022-07-26 19:19:18 +02003443 if (!tick_is_expired(peers->resync_timeout, now_ms) &&
3444 (ps->statuscode == 0 ||
3445 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3446 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3447 ps->statuscode == PEER_SESS_SC_TRYAGAIN)) {
3448 /* The resync timeout is not expired and
3449 * connection never tried
3450 * or previous peer connection was successfully established
3451 * or previous tcp connect succeeded but init state incomplete
3452 * or during previous connect, peer replies a try again statuscode */
Emeric Brun2b920a12010-09-23 18:30:22 +02003453
Christopher Faulet160fff62022-07-26 19:19:18 +02003454 if (!tick_is_expired(ps->reconnect, now_ms)) {
3455 /* reconnection timer is not expired. reschedule task for reconnect */
3456 task->expire = tick_first(task->expire, ps->reconnect);
3457 }
3458 else {
3459 /* connect to the local peer if we must push a local sync */
3460 if (peers->flags & PEERS_F_DONOTSTOP) {
3461 peer_session_create(peers, ps);
3462 }
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003463 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003464 }
3465 else {
3466 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003467 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003468 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003469 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003470 peers->flags &= ~PEERS_F_DONOTSTOP;
3471 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003472 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003473 }
3474 }
3475 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003476 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003477 /* current peer connection is active and established
3478 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003479 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003480 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003481 appctx_wakeup(ps->appctx);
3482 break;
3483 }
3484 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003485 }
3486 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003487
3488 /* Release lock for all peers of the section */
3489 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003490 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003491
Emeric Brun2b920a12010-09-23 18:30:22 +02003492 /* Wakeup for re-connect */
3493 return task;
3494}
3495
Emeric Brunb3971ab2015-05-12 18:49:09 +02003496
Emeric Brun2b920a12010-09-23 18:30:22 +02003497/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003498 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003499 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003500int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003501{
Emeric Brun2b920a12010-09-23 18:30:22 +02003502 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003503
Emeric Brun2b920a12010-09-23 18:30:22 +02003504 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003505 peers->peers_fe->maxconn += 3;
3506 }
3507
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003508 peers->sync_task = task_new_anywhere();
Willy Tarreaud9443442018-10-15 11:18:03 +02003509 if (!peers->sync_task)
3510 return 0;
3511
Maciej Zdebd01be2a2022-05-16 17:26:20 +02003512 memset(peers->applet_count, 0, sizeof(peers->applet_count));
Emeric Brunb3971ab2015-05-12 18:49:09 +02003513 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003514 peers->sync_task->context = (void *)peers;
3515 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3516 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003517 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003518}
3519
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003520/*
3521 * Allocate a cache a dictionary entries used upon transmission.
3522 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003523static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003524{
3525 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003526 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003527
3528 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003529 entries = calloc(max_entries, sizeof *entries);
3530 if (!d || !entries)
3531 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003532
3533 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003534 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003535 d->cached_entries = EB_ROOT_UNIQUE;
3536 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003537
3538 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003539
3540 err:
3541 free(d);
3542 free(entries);
3543 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003544}
3545
3546/*
3547 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3548 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003549 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003550 * Must be deallocated calling free_dcache().
3551 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003552static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003553{
3554 struct dcache_tx *dc_tx;
3555 struct dcache *dc;
3556 struct dcache_rx *dc_rx;
3557
3558 dc = calloc(1, sizeof *dc);
3559 dc_tx = new_dcache_tx(max_entries);
3560 dc_rx = calloc(max_entries, sizeof *dc_rx);
3561 if (!dc || !dc_tx || !dc_rx)
3562 goto err;
3563
3564 dc->tx = dc_tx;
3565 dc->rx = dc_rx;
3566 dc->max_entries = max_entries;
3567
3568 return dc;
3569
3570 err:
3571 free(dc);
3572 free(dc_tx);
3573 free(dc_rx);
3574 return NULL;
3575}
3576
3577/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003578 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3579 * entries used upon transmission.
3580 * Return the entry if found, NULL if not.
3581 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003582static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3583 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003584{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003585 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003586}
3587
3588/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003589 * Flush <dc> cache.
3590 * Always succeeds.
3591 */
3592static inline void flush_dcache(struct peer *peer)
3593{
3594 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003595 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003596
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003597 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003598 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003599 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003600 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3601 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003602 }
3603 dc->tx->prev_lookup = NULL;
3604 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003605
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003606 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003607}
3608
3609/*
3610 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3611 * with information provided by <i> dictionary cache entry (especially the value
3612 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003613 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003614 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003615static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003616{
3617 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003618 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003619
3620 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003621
3622 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3623 o = dc_tx->prev_lookup;
3624 } else {
3625 o = dcache_tx_lookup_value(dc_tx, i);
3626 if (o) {
3627 /* Save it */
3628 dc_tx->prev_lookup = o;
3629 }
3630 }
3631
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003632 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003633 /* Copy the ID. */
3634 i->id = o - dc->tx->entries;
3635 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003636 }
3637
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003638 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003639 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003640
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003641 ebpt_delete(o);
3642 o->key = i->entry.key;
3643 ebpt_insert(&dc_tx->cached_entries, o);
3644 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003645
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003646 /* Update the index for the next entry to put in cache */
3647 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003648
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003649 return o;
3650}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003651
3652/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003653 * Allocate a dictionary cache for each peer of <peers> section.
3654 * Return 1 if succeeded, 0 if not.
3655 */
3656int peers_alloc_dcache(struct peers *peers)
3657{
3658 struct peer *p;
3659
3660 for (p = peers->remote; p; p = p->next) {
3661 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3662 if (!p->dcache)
3663 return 0;
3664 }
3665
3666 return 1;
3667}
3668
3669/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003670 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003671 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003672 */
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003673int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003674{
3675 struct shared_table *st;
3676 struct peer * curpeer;
3677 int id = 0;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003678 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003679
3680 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003681 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003682 if (!st) {
3683 retval = 1;
3684 break;
3685 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003686 st->table = table;
3687 st->next = curpeer->tables;
3688 if (curpeer->tables)
3689 id = curpeer->tables->local_id;
3690 st->local_id = id + 1;
3691
Emeric Brun2cc201f2021-04-23 12:21:26 +02003692 /* If peer is local we inc table
3693 * refcnt to protect against flush
3694 * until this process pushed all
3695 * table content to the new one
3696 */
3697 if (curpeer->local)
3698 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003699 curpeer->tables = st;
3700 }
3701
3702 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003703
3704 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003705}
3706
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003707/* context used by a "show peers" command */
3708struct show_peers_ctx {
Willy Tarreau3a31e372022-05-03 14:58:47 +02003709 void *target; /* if non-null, dump only this section and stop */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003710 struct peers *peers; /* "peers" section being currently dumped. */
3711 struct peer *peer; /* "peer" being currently dumped. */
3712 int flags; /* non-zero if "dict" dump requested */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003713 enum {
Willy Tarreauce9123c2022-05-03 15:04:25 +02003714 STATE_HEAD = 0, /* dump the section's header */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003715 STATE_PEER, /* dump the whole peer */
3716 STATE_DONE, /* finished */
3717 } state; /* parser's state */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003718};
3719
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003720/*
3721 * Parse the "show peers" command arguments.
3722 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3723 * error message.
3724 */
3725static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3726{
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003727 struct show_peers_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003728
Willy Tarreau49962b52021-02-12 16:56:22 +01003729 if (strcmp(args[2], "dict") == 0) {
3730 /* show the dictionaries (large dump) */
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003731 ctx->flags |= PEERS_SHOW_F_DICT;
Willy Tarreau49962b52021-02-12 16:56:22 +01003732 args++;
3733 } else if (strcmp(args[2], "-") == 0)
3734 args++; // allows to show a section called "dict"
3735
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003736 if (*args[2]) {
3737 struct peers *p;
3738
3739 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003740 if (strcmp(p->id, args[2]) == 0) {
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003741 ctx->target = p;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003742 break;
3743 }
3744 }
3745
Willy Tarreau9d008692019-08-09 11:21:01 +02003746 if (!p)
3747 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003748 }
3749
Willy Tarreauce9123c2022-05-03 15:04:25 +02003750 /* where to start from */
3751 ctx->peers = ctx->target ? ctx->target : cfg_peers;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003752 return 0;
3753}
3754
3755/*
3756 * This function dumps the peer state information of <peers> "peers" section.
3757 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3758 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3759 */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003760static int peers_dump_head(struct buffer *msg, struct appctx *appctx, struct peers *peers)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003761{
3762 struct tm tm;
3763
3764 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003765 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 +02003766 peers,
3767 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3768 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003769 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003770 peers->resync_timeout ?
3771 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3772 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003773 TICKS_TO_MS(1000)) : "<NEVER>",
3774 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003775
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003776 if (applet_putchk(appctx, msg) == -1)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003777 return 0;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003778
3779 return 1;
3780}
3781
3782/*
3783 * This function dumps <peer> state information.
3784 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3785 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3786 */
Willy Tarreau50e77b22022-05-31 08:55:54 +02003787static int peers_dump_peer(struct buffer *msg, struct appctx *appctx, struct peer *peer, int flags)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003788{
3789 struct connection *conn;
3790 char pn[INET6_ADDRSTRLEN];
Willy Tarreau4596fe22022-05-17 19:07:51 +02003791 struct stconn *peer_cs;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003792 struct stream *peer_s;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003793 struct shared_table *st;
3794
3795 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003796 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003797 peer, peer->id,
3798 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003799 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003800 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003801 statuscode_str(peer->statuscode));
3802
3803 chunk_appendf(msg, " last_hdshk=%s\n",
3804 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3805 TICKS_TO_MS(1000)) : "<NEVER>");
3806
3807 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003808 peer->reconnect ?
3809 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3810 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003811 TICKS_TO_MS(1000)) : "<NEVER>");
3812
3813 chunk_appendf(msg, " heartbeat=%s",
3814 peer->heartbeat ?
3815 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3816 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3817 TICKS_TO_MS(1000)) : "<NEVER>");
3818
3819 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 +01003820 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003821 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003822
3823 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3824
Willy Tarreaufc505992022-05-31 08:53:25 +02003825 if (!peer->appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003826 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003827
Willy Tarreaufc505992022-05-31 08:53:25 +02003828 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u",
3829 peer->appctx, peer->appctx->st0, peer->appctx->st1,
3830 peer->appctx->t ? peer->appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003831
Willy Tarreauc12b3212022-05-27 11:08:15 +02003832 peer_cs = appctx_sc(peer->appctx);
Willy Tarreauea27f482022-05-18 16:10:52 +02003833 peer_s = __sc_strm(peer_cs);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003834
Willy Tarreau74568cf2022-05-27 09:03:30 +02003835 chunk_appendf(&trash, " state=%s", sc_state_str(sc_opposite(peer_cs)->state));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003836
3837 conn = objt_conn(strm_orig(peer_s));
3838 if (conn)
3839 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3840
Willy Tarreau3ca14902019-07-17 14:53:15 +02003841 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 +02003842 case AF_INET:
3843 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003844 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003845 break;
3846 case AF_UNIX:
3847 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3848 break;
3849 }
3850
Willy Tarreau3ca14902019-07-17 14:53:15 +02003851 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 +02003852 case AF_INET:
3853 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003854 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003855 break;
3856 case AF_UNIX:
3857 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3858 break;
3859 }
3860
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003861 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003862 if (peer->remote_table)
3863 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3864 peer->remote_table,
3865 peer->remote_table->table->id,
3866 peer->remote_table->local_id,
3867 peer->remote_table->remote_id);
3868
3869 if (peer->last_local_table)
3870 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3871 peer->last_local_table,
3872 peer->last_local_table->table->id,
3873 peer->last_local_table->local_id,
3874 peer->last_local_table->remote_id);
3875
3876 if (peer->tables) {
3877 chunk_appendf(&trash, "\n shared tables:");
3878 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003879 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003880 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003881 struct dcache *dcache;
3882
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003883 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003884 dcache = peer->dcache;
3885
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003886 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3887 "flags=0x%x remote_data=0x%llx",
3888 st, st->local_id, st->remote_id,
3889 st->flags, (unsigned long long)st->remote_data);
3890 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3891 " teaching_origin=%u update=%u",
3892 st->last_acked, st->last_pushed, st->last_get,
3893 st->teaching_origin, st->update);
3894 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003895 " commitupdate=%u refcnt=%u",
3896 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003897 if (flags & PEERS_SHOW_F_DICT) {
3898 chunk_appendf(&trash, "\n TX dictionary cache:");
3899 count = 0;
3900 for (i = 0; i < dcache->max_entries; i++) {
3901 struct ebpt_node *node;
3902 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003903
Willy Tarreau49962b52021-02-12 16:56:22 +01003904 node = &dcache->tx->entries[i];
3905 if (!node->key)
3906 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003907
Willy Tarreau49962b52021-02-12 16:56:22 +01003908 if (!count++)
3909 chunk_appendf(&trash, "\n ");
3910 de = node->key;
3911 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3912 count &= 0x3;
3913 }
3914 chunk_appendf(&trash, "\n RX dictionary cache:");
3915 count = 0;
3916 for (i = 0; i < dcache->max_entries; i++) {
3917 if (!count++)
3918 chunk_appendf(&trash, "\n ");
3919 chunk_appendf(&trash, " %3u -> %s", i,
3920 dcache->rx[i].de ?
3921 (char *)dcache->rx[i].de->value.key : "-");
3922 count &= 0x3;
3923 }
3924 } else {
3925 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003926 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003927 }
3928 }
3929
3930 end:
3931 chunk_appendf(&trash, "\n");
Willy Tarreau50e77b22022-05-31 08:55:54 +02003932 if (applet_putchk(appctx, msg) == -1)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003933 return 0;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003934
3935 return 1;
3936}
3937
3938/*
3939 * This function dumps all the peers of "peers" section.
3940 * Returns 0 if the output buffer is full and needs to be called
3941 * again, non-zero if not. It proceeds in an isolated thread, so
3942 * there is no thread safety issue here.
3943 */
3944static int cli_io_handler_show_peers(struct appctx *appctx)
3945{
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003946 struct show_peers_ctx *ctx = appctx->svcctx;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003947 int ret = 0, first_peers = 1;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003948
3949 thread_isolate();
3950
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003951 chunk_reset(&trash);
3952
Willy Tarreau3a31e372022-05-03 14:58:47 +02003953 while (ctx->state != STATE_DONE) {
3954 switch (ctx->state) {
Willy Tarreau3a31e372022-05-03 14:58:47 +02003955 case STATE_HEAD:
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003956 if (!ctx->peers) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003957 /* No more peers list. */
Willy Tarreau3a31e372022-05-03 14:58:47 +02003958 ctx->state = STATE_DONE;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003959 }
3960 else {
3961 if (!first_peers)
3962 chunk_appendf(&trash, "\n");
3963 else
3964 first_peers = 0;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003965 if (!peers_dump_head(&trash, appctx, ctx->peers))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003966 goto out;
3967
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003968 ctx->peer = ctx->peers->remote;
3969 ctx->peers = ctx->peers->next;
Willy Tarreau3a31e372022-05-03 14:58:47 +02003970 ctx->state = STATE_PEER;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003971 }
3972 break;
3973
Willy Tarreau3a31e372022-05-03 14:58:47 +02003974 case STATE_PEER:
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003975 if (!ctx->peer) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003976 /* End of peer list */
Willy Tarreauce9123c2022-05-03 15:04:25 +02003977 if (!ctx->target)
3978 ctx->state = STATE_HEAD; // next one
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003979 else
Willy Tarreau3a31e372022-05-03 14:58:47 +02003980 ctx->state = STATE_DONE;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003981 }
3982 else {
Willy Tarreau50e77b22022-05-31 08:55:54 +02003983 if (!peers_dump_peer(&trash, appctx, ctx->peer, ctx->flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003984 goto out;
3985
Willy Tarreaucb8bf172022-05-03 14:26:31 +02003986 ctx->peer = ctx->peer->next;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003987 }
Willy Tarreau3a31e372022-05-03 14:58:47 +02003988 break;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003989
Willy Tarreau3a31e372022-05-03 14:58:47 +02003990 default:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003991 break;
3992 }
3993 }
3994 ret = 1;
3995 out:
3996 thread_release();
3997 return ret;
3998}
3999
4000/*
4001 * CLI keywords.
4002 */
4003static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004004 { { "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 +02004005 {},
4006}};
4007
4008/* Register cli keywords */
4009INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);