blob: 5f71d1e2781ae639562db6219ed6c35ca9537865 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/applet.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020025#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020027#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020029#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020030#include <haproxy/frontend.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020032#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020033#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020035#include <haproxy/session-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020036#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stick_table.h>
39#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020040#include <haproxy/stream_interface.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/task.h>
42#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020043#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020044#include <haproxy/tools.h>
Frédéric Lécailled8659352020-11-10 16:18:03 +010045#include <haproxy/trace.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020046
Emeric Brun2b920a12010-09-23 18:30:22 +020047
48/*******************************/
49/* Current peer learning state */
50/*******************************/
51
52/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020053/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020054/******************************/
Emeric Brunccdfbae2021-04-28 12:59:35 +020055#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
56#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
57#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
58#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
59#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
60#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
61#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
62#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
63#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
64#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
65#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
66#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
67#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
68#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
69#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
70#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
71 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020072
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010073#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
74#define PEERS_RESYNC_FROMLOCAL 0x00000000
75#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
76#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020077
78/***********************************/
79/* Current shared table sync state */
80/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010081#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
82#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020083
84/******************************/
85/* Remote peer teaching state */
86/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010087#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
88#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
89#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
90#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
91#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
92#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
93#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
94#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020095
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010096#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
97#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020098
Frédéric Lécaille54bff832019-03-26 10:25:20 +010099#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
100#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100101#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
102
Willy Tarreau49962b52021-02-12 16:56:22 +0100103/* flags for "show peers" */
104#define PEERS_SHOW_F_DICT 0x00000001 /* also show the contents of the dictionary */
105
Emeric Brunb3971ab2015-05-12 18:49:09 +0200106/*****************************/
107/* Sync message class */
108/*****************************/
109enum {
110 PEER_MSG_CLASS_CONTROL = 0,
111 PEER_MSG_CLASS_ERROR,
112 PEER_MSG_CLASS_STICKTABLE = 10,
113 PEER_MSG_CLASS_RESERVED = 255,
114};
115
116/*****************************/
117/* control message types */
118/*****************************/
119enum {
120 PEER_MSG_CTRL_RESYNCREQ = 0,
121 PEER_MSG_CTRL_RESYNCFINISHED,
122 PEER_MSG_CTRL_RESYNCPARTIAL,
123 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100124 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200125};
126
127/*****************************/
128/* error message types */
129/*****************************/
130enum {
131 PEER_MSG_ERR_PROTOCOL = 0,
132 PEER_MSG_ERR_SIZELIMIT,
133};
134
Emeric Brun530ba382020-06-02 11:17:42 +0200135/* network key types;
136 * network types were directly and mistakenly
137 * mapped on sample types, to keep backward
138 * compatiblitiy we keep those values but
139 * we now use a internal/network mapping
140 * to avoid further mistakes adding or
141 * modifying internals types
142 */
143enum {
144 PEER_KT_ANY = 0, /* any type */
145 PEER_KT_RESV1, /* UNUSED */
146 PEER_KT_SINT, /* signed 64bits integer type */
147 PEER_KT_RESV3, /* UNUSED */
148 PEER_KT_IPV4, /* ipv4 type */
149 PEER_KT_IPV6, /* ipv6 type */
150 PEER_KT_STR, /* char string type */
151 PEER_KT_BIN, /* buffer type */
152 PEER_KT_TYPES /* number of types, must always be last */
153};
154
155/* Map used to retrieve network type from internal type
156 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
157 */
158static int peer_net_key_type[SMP_TYPES] = {
159 [SMP_T_SINT] = PEER_KT_SINT,
160 [SMP_T_IPV4] = PEER_KT_IPV4,
161 [SMP_T_IPV6] = PEER_KT_IPV6,
162 [SMP_T_STR] = PEER_KT_STR,
163 [SMP_T_BIN] = PEER_KT_BIN,
164};
165
166/* Map used to retrieve internal type from external type
167 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
168 */
169static int peer_int_key_type[PEER_KT_TYPES] = {
170 [PEER_KT_SINT] = SMP_T_SINT,
171 [PEER_KT_IPV4] = SMP_T_IPV4,
172 [PEER_KT_IPV6] = SMP_T_IPV6,
173 [PEER_KT_STR] = SMP_T_STR,
174 [PEER_KT_BIN] = SMP_T_BIN,
175};
176
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100177/*
178 * Parameters used by functions to build peer protocol messages. */
179struct peer_prep_params {
180 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100181 struct peer *peer;
182 } hello;
183 struct {
184 unsigned int st1;
185 } error_status;
186 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100187 struct stksess *stksess;
188 struct shared_table *shared_table;
189 unsigned int updateid;
190 int use_identifier;
191 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200192 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100193 } updt;
194 struct {
195 struct shared_table *shared_table;
196 } swtch;
197 struct {
198 struct shared_table *shared_table;
199 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100200 struct {
201 unsigned char head[2];
202 } control;
203 struct {
204 unsigned char head[2];
205 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100206};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200207
208/*******************************/
209/* stick table sync mesg types */
210/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500211/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200212/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200213#define PEER_MSG_STKT_UPDATE 0x80
214#define PEER_MSG_STKT_INCUPDATE 0x81
215#define PEER_MSG_STKT_DEFINE 0x82
216#define PEER_MSG_STKT_SWITCH 0x83
217#define PEER_MSG_STKT_ACK 0x84
218#define PEER_MSG_STKT_UPDATE_TIMED 0x85
219#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200220/* All the stick-table message identifiers abova have the #7 bit set */
221#define PEER_MSG_STKT_BIT 7
222#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200223
Frédéric Lécaille39143342019-05-24 14:32:27 +0200224/* The maximum length of an encoded data length. */
225#define PEER_MSG_ENC_LENGTH_MAXLEN 5
226
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200227/* Minimum 64-bits value encoded with 2 bytes */
228#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
229/* 3 bytes */
230#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
231/* 4 bytes */
232#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
233/* 5 bytes */
234#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
235/* 6 bytes */
236#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
237/* 7 bytes */
238#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
239/* 8 bytes */
240#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
241/* 9 bytes */
242#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
243/* 10 bytes */
244#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
245
246/* #7 bit used to detect the last byte to be encoded */
247#define PEER_ENC_STOP_BIT 7
248/* The byte minimum value with #7 bit set */
249#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
250/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
251#define PEER_ENC_2BYTES_MIN_BITS 4
252
Frédéric Lécaille39143342019-05-24 14:32:27 +0200253#define PEER_MSG_HEADER_LEN 2
254
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200255#define PEER_STKT_CACHE_MAX_ENTRIES 128
256
Emeric Brun2b920a12010-09-23 18:30:22 +0200257/**********************************/
258/* Peer Session IO handler states */
259/**********************************/
260
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100261enum {
262 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
263 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
264 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
265 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100266 /* after this point, data were possibly exchanged */
267 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
268 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
269 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
270 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
271 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200272 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
273 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100274 PEER_SESS_ST_END, /* Killed session */
275};
Emeric Brun2b920a12010-09-23 18:30:22 +0200276
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100277/***************************************************/
278/* Peer Session status code - part of the protocol */
279/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200280
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100281#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
282#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200283
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100284#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200285
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100286#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200287
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100288#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
289#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
290#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
291#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200292
293#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200294#define PEER_MAJOR_VER 2
295#define PEER_MINOR_VER 1
296#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200297
Willy Tarreau6254a922019-01-29 17:45:23 +0100298static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200299struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200300static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200301
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200302static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
303 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200304static inline void flush_dcache(struct peer *peer);
305
Frédéric Lécailled8659352020-11-10 16:18:03 +0100306/* trace source and events */
307static void peers_trace(enum trace_level level, uint64_t mask,
308 const struct trace_source *src,
309 const struct ist where, const struct ist func,
310 const void *a1, const void *a2, const void *a3, const void *a4);
311
312static const struct trace_event peers_trace_events[] = {
313#define PEERS_EV_UPDTMSG (1 << 0)
314 { .mask = PEERS_EV_UPDTMSG, .name = "updtmsg", .desc = "update message received" },
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100315#define PEERS_EV_ACKMSG (1 << 1)
316 { .mask = PEERS_EV_ACKMSG, .name = "ackmsg", .desc = "ack message received" },
317#define PEERS_EV_SWTCMSG (1 << 2)
318 { .mask = PEERS_EV_SWTCMSG, .name = "swtcmsg", .desc = "switch message received" },
319#define PEERS_EV_DEFMSG (1 << 3)
320 { .mask = PEERS_EV_DEFMSG, .name = "defmsg", .desc = "definition message received" },
321#define PEERS_EV_CTRLMSG (1 << 4)
322 { .mask = PEERS_EV_CTRLMSG, .name = "ctrlmsg", .desc = "control message sent/received" },
323#define PEERS_EV_SESSREL (1 << 5)
324 { .mask = PEERS_EV_SESSREL, .name = "sessrl", .desc = "peer session releasing" },
325#define PEERS_EV_PROTOERR (1 << 6)
326 { .mask = PEERS_EV_PROTOERR, .name = "protoerr", .desc = "protocol error" },
Frédéric Lécailled8659352020-11-10 16:18:03 +0100327};
328
329static const struct name_desc peers_trace_lockon_args[4] = {
330 /* arg1 */ { /* already used by the connection */ },
331 /* arg2 */ { .name="peers", .desc="Peers protocol" },
332 /* arg3 */ { },
333 /* arg4 */ { }
334};
335
336static const struct name_desc peers_trace_decoding[] = {
337#define PEERS_VERB_CLEAN 1
338 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
339 { /* end */ }
340};
341
342
343struct trace_source trace_peers = {
344 .name = IST("peers"),
345 .desc = "Peers protocol",
346 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
347 .default_cb = peers_trace,
348 .known_events = peers_trace_events,
349 .lockon_args = peers_trace_lockon_args,
350 .decoding = peers_trace_decoding,
351 .report_events = ~0, /* report everything by default */
352};
353
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100354/* Return peer control message types as strings (only for debugging purpose). */
355static inline char *ctrl_msg_type_str(unsigned int type)
356{
357 switch (type) {
358 case PEER_MSG_CTRL_RESYNCREQ:
359 return "RESYNCREQ";
360 case PEER_MSG_CTRL_RESYNCFINISHED:
361 return "RESYNCFINISHED";
362 case PEER_MSG_CTRL_RESYNCPARTIAL:
363 return "RESYNCPARTIAL";
364 case PEER_MSG_CTRL_RESYNCCONFIRM:
365 return "RESYNCCONFIRM";
366 case PEER_MSG_CTRL_HEARTBEAT:
367 return "HEARTBEAT";
368 default:
369 return "???";
370 }
371}
372
Frédéric Lécailled8659352020-11-10 16:18:03 +0100373#define TRACE_SOURCE &trace_peers
374INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
375
376static void peers_trace(enum trace_level level, uint64_t mask,
377 const struct trace_source *src,
378 const struct ist where, const struct ist func,
379 const void *a1, const void *a2, const void *a3, const void *a4)
380{
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100381 if (mask & (PEERS_EV_UPDTMSG|PEERS_EV_ACKMSG|PEERS_EV_SWTCMSG)) {
Frédéric Lécailled8659352020-11-10 16:18:03 +0100382 if (a2) {
383 const struct peer *peer = a2;
384
385 chunk_appendf(&trace_buf, " peer=%s", peer->id);
386 }
387 if (a3) {
388 const char *p = a3;
389
390 chunk_appendf(&trace_buf, " @%p", p);
391 }
392 if (a4) {
393 const size_t *val = a4;
394
395 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*val);
396 }
397 }
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100398
399 if (mask & PEERS_EV_DEFMSG) {
400 if (a2) {
401 const struct peer *peer = a2;
402
403 chunk_appendf(&trace_buf, " peer=%s", peer->id);
404 }
405 if (a3) {
406 const char *p = a3;
407
408 chunk_appendf(&trace_buf, " @%p", p);
409 }
410 if (a4) {
411 const int *val = a4;
412
413 chunk_appendf(&trace_buf, " %d", *val);
414 }
415 }
416
417 if (mask & PEERS_EV_CTRLMSG) {
418 if (a2) {
419 const unsigned char *ctrl_msg_type = a2;
420
421 chunk_appendf(&trace_buf, " %s", ctrl_msg_type_str(*ctrl_msg_type));
422
423 }
424 if (a3) {
425 const char *local_peer = a3;
426
427 chunk_appendf(&trace_buf, " %s", local_peer);
428 }
429
430 if (a4) {
431 const char *remote_peer = a4;
432
433 chunk_appendf(&trace_buf, " -> %s", remote_peer);
434 }
435 }
436
437 if (mask & (PEERS_EV_SESSREL|PEERS_EV_PROTOERR)) {
438 if (a2) {
439 const struct peer *peer = a2;
440 struct peers *peers = NULL;
441
Frédéric Lécaille4b1a05f2021-01-17 13:08:39 +0100442 if (peer && peer->appctx) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100443 struct stream_interface *si;
444
445 si = peer->appctx->owner;
446 if (si) {
447 struct stream *s = si_strm(si);
448
449 peers = strm_fe(s)->parent;
450 }
451 }
452
453 if (peers)
454 chunk_appendf(&trace_buf, " %s", peers->local->id);
455 if (peer)
456 chunk_appendf(&trace_buf, " -> %s", peer->id);
457 }
458
459 if (a3) {
460 const int *prev_state = a3;
461
462 chunk_appendf(&trace_buf, " prev_state=%d\n", *prev_state);
463 }
464 }
Frédéric Lécailled8659352020-11-10 16:18:03 +0100465}
466
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200467static const char *statuscode_str(int statuscode)
468{
469 switch (statuscode) {
470 case PEER_SESS_SC_CONNECTCODE:
471 return "CONN";
472 case PEER_SESS_SC_CONNECTEDCODE:
473 return "HSHK";
474 case PEER_SESS_SC_SUCCESSCODE:
475 return "ESTA";
476 case PEER_SESS_SC_TRYAGAIN:
477 return "RETR";
478 case PEER_SESS_SC_ERRPROTO:
479 return "PROT";
480 case PEER_SESS_SC_ERRVERSION:
481 return "VERS";
482 case PEER_SESS_SC_ERRHOST:
483 return "NAME";
484 case PEER_SESS_SC_ERRPEER:
485 return "UNKN";
486 default:
487 return "NONE";
488 }
489}
490
Emeric Brun18928af2017-03-29 16:32:53 +0200491/* This function encode an uint64 to 'dynamic' length format.
492 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500493 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200494 At return, the *str is set at the next Byte after then
495 encoded integer. The function returns then length of the
496 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200497int intencode(uint64_t i, char **str) {
498 int idx = 0;
499 unsigned char *msg;
500
Emeric Brunb3971ab2015-05-12 18:49:09 +0200501 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200502 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200503 msg[0] = (unsigned char)i;
504 *str = (char *)&msg[idx+1];
505 return (idx+1);
506 }
507
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200508 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
509 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
510 while (i >= PEER_ENC_STOP_BYTE) {
511 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
512 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200513 }
514 msg[++idx] = (unsigned char)i;
515 *str = (char *)&msg[idx+1];
516 return (idx+1);
517}
518
519
Emeric Brun5ea07d92021-06-30 13:21:58 +0200520/* This function returns a decoded 64bits unsigned integer
521 * from a varint
522 *
523 * Calling:
524 * - *str must point on the first byte of the buffer to decode.
525 * - end must point on the next byte after the end of the buffer
526 * we are authorized to parse (buf + buflen)
527 *
528 * At return:
529 *
530 * On success *str will point at the byte following
531 * the fully decoded integer into the buffer. and
532 * the decoded value is returned.
533 *
534 * If end is reached before the integer was fully decoded,
535 * *str is set to NULL and the caller have to check this
536 * to know there is a decoding error. In this case
537 * the returned integer is also forced to 0
538 */
Emeric Brun18928af2017-03-29 16:32:53 +0200539uint64_t intdecode(char **str, char *end)
540{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200541 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200542 uint64_t i;
543 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200544
545 if (!*str)
546 return 0;
547
548 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200549 if (msg >= (unsigned char *)end)
550 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200551
Emeric Brun18928af2017-03-29 16:32:53 +0200552 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200553 if (i >= PEER_ENC_2BYTES_MIN) {
554 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200555 do {
556 if (msg >= (unsigned char *)end)
557 goto fail;
558 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200559 shift += PEER_ENC_STOP_BIT;
560 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200561 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100562 *str = (char *)msg;
563 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200564
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100565 fail:
566 *str = NULL;
567 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200568}
Emeric Brun2b920a12010-09-23 18:30:22 +0200569
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100570/*
571 * Build a "hello" peer protocol message.
572 * Return the number of written bytes written to build this messages if succeeded,
573 * 0 if not.
574 */
575static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
576{
577 int min_ver, ret;
578 struct peer *peer;
579
580 peer = p->hello.peer;
581 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
582 /* Prepare headers */
583 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
Willy Tarreaue8422bf2021-06-15 09:08:18 +0200584 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), 1);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100585 if (ret >= size)
586 return 0;
587
588 return ret;
589}
590
591/*
592 * Build a "handshake succeeded" status message.
593 * Return the number of written bytes written to build this messages if succeeded,
594 * 0 if not.
595 */
596static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
597{
598 int ret;
599
600 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
601 if (ret >= size)
602 return 0;
603
604 return ret;
605}
606
607/*
608 * Build an error status message.
609 * Return the number of written bytes written to build this messages if succeeded,
610 * 0 if not.
611 */
612static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
613{
614 int ret;
615 unsigned int st1;
616
617 st1 = p->error_status.st1;
618 ret = snprintf(msg, size, "%d\n", st1);
619 if (ret >= size)
620 return 0;
621
622 return ret;
623}
624
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200625/* Set the stick-table UPDATE message type byte at <msg_type> address,
626 * depending on <use_identifier> and <use_timed> boolean parameters.
627 * Always successful.
628 */
629static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
630{
631 if (use_timed) {
632 if (use_identifier)
633 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
634 else
635 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
636 }
637 else {
638 if (use_identifier)
639 *msg_type = PEER_MSG_STKT_UPDATE;
640 else
641 *msg_type = PEER_MSG_STKT_INCUPDATE;
642 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200643}
Emeric Brun2b920a12010-09-23 18:30:22 +0200644/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200645 * This prepare the data update message on the stick session <ts>, <st> is the considered
646 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800647 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200648 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
649 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200650 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100651static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200652{
653 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200654 unsigned short datalen;
655 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200656 unsigned int data_type;
657 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100658 struct stksess *ts;
659 struct shared_table *st;
660 unsigned int updateid;
661 int use_identifier;
662 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200663 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100664
665 ts = p->updt.stksess;
666 st = p->updt.shared_table;
667 updateid = p->updt.updateid;
668 use_identifier = p->updt.use_identifier;
669 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200670 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200671
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200672 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200673
Emeric Brun2b920a12010-09-23 18:30:22 +0200674 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200675
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500676 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200677 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200678 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200679 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200680
681 /* encode update identifier if needed */
682 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200683 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200684 memcpy(cursor, &netinteger, sizeof(netinteger));
685 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200686 }
687
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200688 if (use_timed) {
689 netinteger = htonl(tick_remain(now_ms, ts->expire));
690 memcpy(cursor, &netinteger, sizeof(netinteger));
691 cursor += sizeof(netinteger);
692 }
693
Emeric Brunb3971ab2015-05-12 18:49:09 +0200694 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200695 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200696 int stlen = strlen((char *)ts->key.key);
697
Emeric Brunb3971ab2015-05-12 18:49:09 +0200698 intencode(stlen, &cursor);
699 memcpy(cursor, ts->key.key, stlen);
700 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200701 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200702 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100703 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200704 memcpy(cursor, &netinteger, sizeof(netinteger));
705 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200706 }
707 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200708 memcpy(cursor, ts->key.key, st->table->key_size);
709 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200710 }
711
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100712 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200713 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200714 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200715
Emeric Brun94900952015-06-11 18:25:54 +0200716 data_ptr = stktable_data_ptr(st->table, ts, data_type);
717 if (data_ptr) {
718 switch (stktable_data_types[data_type].std_type) {
719 case STD_T_SINT: {
720 int data;
721
722 data = stktable_data_cast(data_ptr, std_t_sint);
723 intencode(data, &cursor);
724 break;
725 }
726 case STD_T_UINT: {
727 unsigned int data;
728
729 data = stktable_data_cast(data_ptr, std_t_uint);
730 intencode(data, &cursor);
731 break;
732 }
733 case STD_T_ULL: {
734 unsigned long long data;
735
736 data = stktable_data_cast(data_ptr, std_t_ull);
737 intencode(data, &cursor);
738 break;
739 }
740 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +0200741 struct freq_ctr *frqp;
Emeric Brun94900952015-06-11 18:25:54 +0200742
743 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
744 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
745 intencode(frqp->curr_ctr, &cursor);
746 intencode(frqp->prev_ctr, &cursor);
747 break;
748 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200749 case STD_T_DICT: {
750 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200751 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200752 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200753 char *beg, *end;
754 size_t value_len, data_len;
755 struct dcache *dc;
756
757 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100758 if (!de) {
759 /* No entry */
760 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200761 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100762 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200763
764 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200765 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200766 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200767 if (cached_de == &cde.entry) {
768 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
769 break;
770 /* Encode the length of the remaining data -> 1 */
771 intencode(1, &cursor);
772 /* Encode the cache entry ID */
773 intencode(cde.id + 1, &cursor);
774 }
775 else {
776 /* Leave enough room to encode the remaining data length. */
777 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
778 /* Encode the dictionary entry key */
779 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200780 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200781 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200782 intencode(value_len, &end);
783 /* Copy the data */
784 memcpy(end, de->value.key, value_len);
785 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200786 /* Encode the length of the data */
787 data_len = end - beg;
788 intencode(data_len, &cursor);
789 memmove(cursor, beg, data_len);
790 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200791 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200792 break;
793 }
Emeric Brun94900952015-06-11 18:25:54 +0200794 }
795 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200796 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100797 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200798
799 /* Compute datalen */
800 datalen = (cursor - datamsg);
801
802 /* prepare message header */
803 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200804 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200805 cursor = &msg[2];
806 intencode(datalen, &cursor);
807
808 /* move data after header */
809 memmove(cursor, datamsg, datalen);
810
811 /* return header size + data_len */
812 return (cursor - msg) + datalen;
813}
814
815/*
816 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800817 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200818 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
819 * check size)
820 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100821static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200822{
823 int len;
824 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200825 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200826 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200827 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200828 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100829 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200830
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100831 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200832 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200833
834 /* Encode data */
835
836 /* encode local id */
837 intencode(st->local_id, &cursor);
838
839 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100840 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200841 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100842 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200843 cursor += len;
844
845 /* encode table type */
846
Emeric Brun530ba382020-06-02 11:17:42 +0200847 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200848
849 /* encode table key size */
850 intencode(st->table->key_size, &cursor);
851
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200852 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200854 /* encode available known data types in table */
855 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
856 if (st->table->data_ofs[data_type]) {
857 switch (stktable_data_types[data_type].std_type) {
858 case STD_T_SINT:
859 case STD_T_UINT:
860 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200861 case STD_T_DICT:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200862 data |= 1 << data_type;
863 break;
Emeric Brun94900952015-06-11 18:25:54 +0200864 case STD_T_FRQP:
865 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200866 intencode(data_type, &chunkq);
867 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200868 break;
869 }
870 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200871 }
872 intencode(data, &cursor);
873
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200874 /* Encode stick-table entries duration. */
875 intencode(st->table->expire, &cursor);
876
877 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200878 chunk->data = chunkq - chunkp;
879 memcpy(cursor, chunk->area, chunk->data);
880 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200881 }
882
Emeric Brunb3971ab2015-05-12 18:49:09 +0200883 /* Compute datalen */
884 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200885
Emeric Brunb3971ab2015-05-12 18:49:09 +0200886 /* prepare message header */
887 msg[0] = PEER_MSG_CLASS_STICKTABLE;
888 msg[1] = PEER_MSG_STKT_DEFINE;
889 cursor = &msg[2];
890 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200891
Emeric Brunb3971ab2015-05-12 18:49:09 +0200892 /* move data after header */
893 memmove(cursor, datamsg, datalen);
894
895 /* return header size + data_len */
896 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200897}
898
Emeric Brunb3971ab2015-05-12 18:49:09 +0200899/*
900 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
901 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800902 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200903 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
904 * check size)
905 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100906static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200907{
908 unsigned short datalen;
909 char *cursor, *datamsg;
910 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100911 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200912
Frédéric Lécaille39143342019-05-24 14:32:27 +0200913 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200914
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100915 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200916 intencode(st->remote_id, &cursor);
917 netinteger = htonl(st->last_get);
918 memcpy(cursor, &netinteger, sizeof(netinteger));
919 cursor += sizeof(netinteger);
920
921 /* Compute datalen */
922 datalen = (cursor - datamsg);
923
924 /* prepare message header */
925 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200926 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200927 cursor = &msg[2];
928 intencode(datalen, &cursor);
929
930 /* move data after header */
931 memmove(cursor, datamsg, datalen);
932
933 /* return header size + data_len */
934 return (cursor - msg) + datalen;
935}
Emeric Brun2b920a12010-09-23 18:30:22 +0200936
937/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200938 * Function to deinit connected peer
939 */
940void __peer_session_deinit(struct peer *peer)
941{
942 struct stream_interface *si;
943 struct stream *s;
944 struct peers *peers;
945
946 if (!peer->appctx)
947 return;
948
949 si = peer->appctx->owner;
950 if (!si)
951 return;
952
953 s = si_strm(si);
954 if (!s)
955 return;
956
957 peers = strm_fe(s)->parent;
958 if (!peers)
959 return;
960
961 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +0200962 HA_ATOMIC_DEC(&connected_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200963
Willy Tarreau4781b152021-04-06 13:53:36 +0200964 HA_ATOMIC_DEC(&active_peers);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200965
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200966 flush_dcache(peer);
967
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200968 /* Re-init current table pointers to force announcement on re-connect */
969 peer->remote_table = peer->last_local_table = NULL;
970 peer->appctx = NULL;
971 if (peer->flags & PEER_F_LEARN_ASSIGN) {
972 /* unassign current peer for learning */
973 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
974 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
975
Emeric Brunccdfbae2021-04-28 12:59:35 +0200976 if (peer->local)
977 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
978 else
979 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200980 /* reschedule a resync */
981 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
982 }
983 /* reset teaching and learning flags to 0 */
984 peer->flags &= PEER_TEACH_RESET;
985 peer->flags &= PEER_LEARN_RESET;
986 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
987}
988
989/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200990 * Callback to release a session with a peer
991 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200992static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200993{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200994 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200995
Frédéric Lécailleda2b0842021-01-15 16:21:28 +0100996 TRACE_PROTO("releasing peer session", PEERS_EV_SESSREL, NULL, peer);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100997 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100998 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200999 return;
1000
1001 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001002 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001003 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +02001004 if (peer->appctx == appctx)
1005 __peer_session_deinit(peer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02001006 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001007 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001008 }
1009}
1010
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001011/* Retrieve the major and minor versions of peers protocol
1012 * announced by a remote peer. <str> is a null-terminated
1013 * string with the following format: "<maj_ver>.<min_ver>".
1014 */
1015static int peer_get_version(const char *str,
1016 unsigned int *maj_ver, unsigned int *min_ver)
1017{
1018 unsigned int majv, minv;
1019 const char *pos, *saved;
1020 const char *end;
1021
1022 saved = pos = str;
1023 end = str + strlen(str);
1024
1025 majv = read_uint(&pos, end);
1026 if (saved == pos || *pos++ != '.')
1027 return -1;
1028
1029 saved = pos;
1030 minv = read_uint(&pos, end);
1031 if (saved == pos || pos != end)
1032 return -1;
1033
1034 *maj_ver = majv;
1035 *min_ver = minv;
1036
1037 return 0;
1038}
Emeric Brun2b920a12010-09-23 18:30:22 +02001039
1040/*
Frédéric Lécaillece025572019-01-21 13:38:06 +01001041 * Parse a line terminated by an optional '\r' character, followed by a mandatory
1042 * '\n' character.
1043 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
1044 * a line could not be read because the communication channel is closed.
1045 */
1046static inline int peer_getline(struct appctx *appctx)
1047{
1048 int n;
1049 struct stream_interface *si = appctx->owner;
1050
1051 n = co_getline(si_oc(si), trash.area, trash.size);
1052 if (!n)
1053 return 0;
1054
1055 if (n < 0 || trash.area[n - 1] != '\n') {
1056 appctx->st0 = PEER_SESS_ST_END;
1057 return -1;
1058 }
1059
1060 if (n > 1 && (trash.area[n - 2] == '\r'))
1061 trash.area[n - 2] = 0;
1062 else
1063 trash.area[n - 1] = 0;
1064
1065 co_skip(si_oc(si), n);
1066
1067 return n;
1068}
1069
1070/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001071 * Send a message after having called <peer_prepare_msg> to build it.
1072 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1073 * Returns -1 if there was not enough room left to send the message,
1074 * any other negative returned value must be considered as an error with an appcxt st0
1075 * returned value equal to PEER_SESS_ST_END.
1076 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001077static inline int peer_send_msg(struct appctx *appctx,
1078 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
1079 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001080{
1081 int ret, msglen;
1082 struct stream_interface *si = appctx->owner;
1083
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001084 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001085 if (!msglen) {
1086 /* internal error: message does not fit in trash */
1087 appctx->st0 = PEER_SESS_ST_END;
1088 return 0;
1089 }
1090
1091 /* message to buffer */
1092 ret = ci_putblk(si_ic(si), trash.area, msglen);
1093 if (ret <= 0) {
1094 if (ret == -1) {
1095 /* No more write possible */
1096 si_rx_room_blk(si);
1097 return -1;
1098 }
1099 appctx->st0 = PEER_SESS_ST_END;
1100 }
1101
1102 return ret;
1103}
1104
1105/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001106 * Send a hello message.
1107 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1108 * Returns -1 if there was not enough room left to send the message,
1109 * any other negative returned value must be considered as an error with an appcxt st0
1110 * returned value equal to PEER_SESS_ST_END.
1111 */
1112static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
1113{
1114 struct peer_prep_params p = {
1115 .hello.peer = peer,
1116 };
1117
1118 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
1119}
1120
1121/*
1122 * Send a success peer handshake status message.
1123 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1124 * Returns -1 if there was not enough room left to send the message,
1125 * any other negative returned value must be considered as an error with an appcxt st0
1126 * returned value equal to PEER_SESS_ST_END.
1127 */
1128static inline int peer_send_status_successmsg(struct appctx *appctx)
1129{
1130 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
1131}
1132
1133/*
1134 * Send a peer handshake status error message.
1135 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1136 * Returns -1 if there was not enough room left to send the message,
1137 * any other negative returned value must be considered as an error with an appcxt st0
1138 * returned value equal to PEER_SESS_ST_END.
1139 */
1140static inline int peer_send_status_errormsg(struct appctx *appctx)
1141{
1142 struct peer_prep_params p = {
1143 .error_status.st1 = appctx->st1,
1144 };
1145
1146 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
1147}
1148
1149/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001150 * Send a stick-table switch message.
1151 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1152 * Returns -1 if there was not enough room left to send the message,
1153 * any other negative returned value must be considered as an error with an appcxt st0
1154 * returned value equal to PEER_SESS_ST_END.
1155 */
1156static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
1157{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001158 struct peer_prep_params p = {
1159 .swtch.shared_table = st,
1160 };
1161
1162 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001163}
1164
1165/*
1166 * Send a stick-table update acknowledgement message.
1167 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1168 * Returns -1 if there was not enough room left to send the message,
1169 * any other negative returned value must be considered as an error with an appcxt st0
1170 * returned value equal to PEER_SESS_ST_END.
1171 */
1172static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1173{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001174 struct peer_prep_params p = {
1175 .ack.shared_table = st,
1176 };
1177
1178 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001179}
1180
1181/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001182 * Send a stick-table update message.
1183 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1184 * Returns -1 if there was not enough room left to send the message,
1185 * any other negative returned value must be considered as an error with an appcxt st0
1186 * returned value equal to PEER_SESS_ST_END.
1187 */
1188static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1189 unsigned int updateid, int use_identifier, int use_timed)
1190{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001191 struct peer_prep_params p = {
Willy Tarreaua898f0c2020-07-03 19:09:29 +02001192 .updt = {
1193 .stksess = ts,
1194 .shared_table = st,
1195 .updateid = updateid,
1196 .use_identifier = use_identifier,
1197 .use_timed = use_timed,
1198 .peer = appctx->ctx.peers.ptr,
1199 },
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001200 };
1201
1202 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001203}
1204
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001205/*
1206 * Build a peer protocol control class message.
1207 * Returns the number of written bytes used to build the message if succeeded,
1208 * 0 if not.
1209 */
1210static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1211{
1212 if (size < sizeof p->control.head)
1213 return 0;
1214
1215 msg[0] = p->control.head[0];
1216 msg[1] = p->control.head[1];
1217
1218 return 2;
1219}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001220
1221/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001222 * Send a stick-table synchronization request message.
1223 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1224 * Returns -1 if there was not enough room left to send the message,
1225 * any other negative returned value must be considered as an error with an appctx st0
1226 * returned value equal to PEER_SESS_ST_END.
1227 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001228static inline int peer_send_resync_reqmsg(struct appctx *appctx,
1229 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001230{
1231 struct peer_prep_params p = {
1232 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1233 };
1234
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001235 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1236 NULL, &p.control.head[1], peers->local->id, peer->id);
1237
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001238 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1239}
1240
1241/*
1242 * Send a stick-table synchronization confirmation message.
1243 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1244 * Returns -1 if there was not enough room left to send the message,
1245 * any other negative returned value must be considered as an error with an appctx st0
1246 * returned value equal to PEER_SESS_ST_END.
1247 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001248static inline int peer_send_resync_confirmsg(struct appctx *appctx,
1249 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001250{
1251 struct peer_prep_params p = {
1252 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1253 };
1254
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001255 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1256 NULL, &p.control.head[1], peers->local->id, peer->id);
1257
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001258 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1259}
1260
1261/*
1262 * Send a stick-table synchronization finished message.
1263 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1264 * Returns -1 if there was not enough room left to send the message,
1265 * any other negative returned value must be considered as an error with an appctx st0
1266 * returned value equal to PEER_SESS_ST_END.
1267 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001268static inline int peer_send_resync_finishedmsg(struct appctx *appctx,
1269 struct peer *peer, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001270{
1271 struct peer_prep_params p = {
1272 .control.head = { PEER_MSG_CLASS_CONTROL, },
1273 };
1274
Emeric Brun70de43b2020-03-16 10:51:01 +01001275 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001276 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1277
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001278 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1279 NULL, &p.control.head[1], peers->local->id, peer->id);
1280
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001281 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1282}
1283
1284/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001285 * Send a heartbeat message.
1286 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1287 * Returns -1 if there was not enough room left to send the message,
1288 * any other negative returned value must be considered as an error with an appctx st0
1289 * returned value equal to PEER_SESS_ST_END.
1290 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001291static inline int peer_send_heartbeatmsg(struct appctx *appctx,
1292 struct peer *peer, struct peers *peers)
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001293{
1294 struct peer_prep_params p = {
1295 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1296 };
1297
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001298 TRACE_PROTO("send control message", PEERS_EV_CTRLMSG,
1299 NULL, &p.control.head[1], peers->local->id, peer->id);
1300
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001301 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1302}
1303
1304/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001305 * Build a peer protocol error class message.
1306 * Returns the number of written bytes used to build the message if succeeded,
1307 * 0 if not.
1308 */
1309static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1310{
1311 if (size < sizeof p->error.head)
1312 return 0;
1313
1314 msg[0] = p->error.head[0];
1315 msg[1] = p->error.head[1];
1316
1317 return 2;
1318}
1319
1320/*
1321 * Send a "size limit reached" error message.
1322 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1323 * Returns -1 if there was not enough room left to send the message,
1324 * any other negative returned value must be considered as an error with an appctx st0
1325 * returned value equal to PEER_SESS_ST_END.
1326 */
1327static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1328{
1329 struct peer_prep_params p = {
1330 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1331 };
1332
1333 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1334}
1335
1336/*
1337 * Send a "peer protocol" error message.
1338 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1339 * Returns -1 if there was not enough room left to send the message,
1340 * any other negative returned value must be considered as an error with an appctx st0
1341 * returned value equal to PEER_SESS_ST_END.
1342 */
1343static inline int peer_send_error_protomsg(struct appctx *appctx)
1344{
1345 struct peer_prep_params p = {
1346 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1347 };
1348
1349 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1350}
1351
1352/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001353 * Function used to lookup for recent stick-table updates associated with
1354 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1355 */
1356static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1357{
1358 struct eb32_node *eb;
1359
1360 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1361 if (!eb) {
1362 eb = eb32_first(&st->table->updates);
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001363 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001364 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1365 return NULL;
1366 }
1367 }
1368
Emeric Brun8e7a13e2021-04-28 11:48:15 +02001369 /* if distance between the last pushed and the retrieved key
1370 * is greater than the distance last_pushed and the local_update
1371 * this means we are beyond localupdate.
1372 */
1373 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001374 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1375 return NULL;
1376 }
1377
1378 return eb32_entry(eb, struct stksess, upd);
1379}
1380
1381/*
1382 * Function used to lookup for recent stick-table updates associated with
1383 * <st> shared stick-table during teach state 1 step.
1384 */
1385static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1386{
1387 struct eb32_node *eb;
1388
1389 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1390 if (!eb) {
1391 st->flags |= SHTABLE_F_TEACH_STAGE1;
1392 eb = eb32_first(&st->table->updates);
1393 if (eb)
1394 st->last_pushed = eb->key - 1;
1395 return NULL;
1396 }
1397
1398 return eb32_entry(eb, struct stksess, upd);
1399}
1400
1401/*
1402 * Function used to lookup for recent stick-table updates associated with
1403 * <st> shared stick-table during teach state 2 step.
1404 */
1405static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1406{
1407 struct eb32_node *eb;
1408
1409 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1410 if (!eb || eb->key > st->teaching_origin) {
1411 st->flags |= SHTABLE_F_TEACH_STAGE2;
1412 return NULL;
1413 }
1414
1415 return eb32_entry(eb, struct stksess, upd);
1416}
1417
1418/*
1419 * Generic function to emit update messages for <st> stick-table when a lesson must
1420 * be taught to the peer <p>.
1421 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1422 * this function, 0 if not.
1423 *
1424 * This function temporary unlock/lock <st> when it sends stick-table updates or
1425 * when decrementing its refcount in case of any error when it sends this updates.
1426 *
1427 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1428 * Returns -1 if there was not enough room left to send the message,
1429 * any other negative returned value must be considered as an error with an appcxt st0
1430 * returned value equal to PEER_SESS_ST_END.
1431 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1432 * unlocked if not already locked when entering this function.
1433 */
1434static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1435 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1436 struct shared_table *st, int locked)
1437{
1438 int ret, new_pushed, use_timed;
1439
1440 ret = 1;
1441 use_timed = 0;
1442 if (st != p->last_local_table) {
1443 ret = peer_send_switchmsg(st, appctx);
1444 if (ret <= 0)
1445 return ret;
1446
1447 p->last_local_table = st;
1448 }
1449
1450 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1451 use_timed = !(p->flags & PEER_F_DWNGRD);
1452
1453 /* We force new pushed to 1 to force identifier in update message */
1454 new_pushed = 1;
1455
1456 if (!locked)
1457 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1458
1459 while (1) {
1460 struct stksess *ts;
1461 unsigned updateid;
1462
1463 /* push local updates */
1464 ts = peer_stksess_lookup(st);
1465 if (!ts)
1466 break;
1467
1468 updateid = ts->upd.key;
1469 ts->ref_cnt++;
1470 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1471
1472 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1473 if (ret <= 0) {
1474 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1475 ts->ref_cnt--;
1476 if (!locked)
1477 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1478 return ret;
1479 }
1480
1481 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1482 ts->ref_cnt--;
1483 st->last_pushed = updateid;
1484
1485 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1486 (int)(st->last_pushed - st->table->commitupdate) > 0)
1487 st->table->commitupdate = st->last_pushed;
1488
1489 /* identifier may not needed in next update message */
1490 new_pushed = 0;
1491 }
1492
1493 out:
1494 if (!locked)
1495 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1496 return 1;
1497}
1498
1499/*
1500 * Function to emit update messages for <st> stick-table when a lesson must
1501 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1502 *
1503 * Note that <st> shared stick-table is locked when calling this function.
1504 *
1505 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1506 * Returns -1 if there was not enough room left to send the message,
1507 * any other negative returned value must be considered as an error with an appcxt st0
1508 * returned value equal to PEER_SESS_ST_END.
1509 */
1510static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1511 struct shared_table *st)
1512{
1513 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1514}
1515
1516/*
1517 * Function to emit update messages for <st> stick-table when a lesson must
1518 * be taught to the peer <p> during teach state 1 step.
1519 *
1520 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1521 * Returns -1 if there was not enough room left to send the message,
1522 * any other negative returned value must be considered as an error with an appcxt st0
1523 * returned value equal to PEER_SESS_ST_END.
1524 */
1525static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1526 struct shared_table *st)
1527{
1528 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1529}
1530
1531/*
1532 * Function to emit update messages for <st> stick-table when a lesson must
1533 * be taught to the peer <p> during teach state 1 step.
1534 *
1535 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1536 * Returns -1 if there was not enough room left to send the message,
1537 * any other negative returned value must be considered as an error with an appcxt st0
1538 * returned value equal to PEER_SESS_ST_END.
1539 */
1540static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1541 struct shared_table *st)
1542{
1543 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1544}
1545
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001546
1547/*
1548 * Function used to parse a stick-table update message after it has been received
1549 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1550 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1551 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1552 * was encountered.
1553 * <exp> must be set if the stick-table entry expires.
1554 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001555 * 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 +01001556 * update ID.
1557 * <totl> is the length of the stick-table update message computed upon receipt.
1558 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001559static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1560 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001561{
1562 struct stream_interface *si = appctx->owner;
1563 struct shared_table *st = p->remote_table;
1564 struct stksess *ts, *newts;
1565 uint32_t update;
1566 int expire;
1567 unsigned int data_type;
1568 void *data_ptr;
1569
Frédéric Lécailled8659352020-11-10 16:18:03 +01001570 TRACE_ENTER(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001571 /* Here we have data message */
1572 if (!st)
1573 goto ignore_msg;
1574
1575 expire = MS_TO_TICKS(st->table->expire);
1576
1577 if (updt) {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001578 if (msg_len < sizeof(update)) {
1579 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001580 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001581 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001582
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001583 memcpy(&update, *msg_cur, sizeof(update));
1584 *msg_cur += sizeof(update);
1585 st->last_get = htonl(update);
1586 }
1587 else {
1588 st->last_get++;
1589 }
1590
1591 if (exp) {
1592 size_t expire_sz = sizeof expire;
1593
Frédéric Lécailled8659352020-11-10 16:18:03 +01001594 if (*msg_cur + expire_sz > msg_end) {
1595 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1596 NULL, p, *msg_cur);
1597 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1598 NULL, p, msg_end, &expire_sz);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001599 goto malformed_exit;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001600 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001601
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001602 memcpy(&expire, *msg_cur, expire_sz);
1603 *msg_cur += expire_sz;
1604 expire = ntohl(expire);
1605 }
1606
1607 newts = stksess_new(st->table, NULL);
1608 if (!newts)
1609 goto ignore_msg;
1610
1611 if (st->table->type == SMP_T_STR) {
1612 unsigned int to_read, to_store;
1613
1614 to_read = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001615 if (!*msg_cur) {
1616 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001617 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001618 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001619
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001620 to_store = MIN(to_read, st->table->key_size - 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001621 if (*msg_cur + to_store > msg_end) {
1622 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1623 NULL, p, *msg_cur);
1624 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1625 NULL, p, msg_end, &to_store);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001626 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001627 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001628
1629 memcpy(newts->key.key, *msg_cur, to_store);
1630 newts->key.key[to_store] = 0;
1631 *msg_cur += to_read;
1632 }
1633 else if (st->table->type == SMP_T_SINT) {
1634 unsigned int netinteger;
1635
Frédéric Lécailled8659352020-11-10 16:18:03 +01001636 if (*msg_cur + sizeof(netinteger) > msg_end) {
1637 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1638 NULL, p, *msg_cur);
1639 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1640 NULL, p, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001641 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001642 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001643
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001644 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1645 netinteger = ntohl(netinteger);
1646 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1647 *msg_cur += sizeof(netinteger);
1648 }
1649 else {
Frédéric Lécailled8659352020-11-10 16:18:03 +01001650 if (*msg_cur + st->table->key_size > msg_end) {
1651 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1652 NULL, p, *msg_cur);
1653 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1654 NULL, p, msg_end, &st->table->key_size);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001655 goto malformed_free_newts;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001656 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001657
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001658 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1659 *msg_cur += st->table->key_size;
1660 }
1661
1662 /* lookup for existing entry */
1663 ts = stktable_set_entry(st->table, newts);
1664 if (ts != newts) {
1665 stksess_free(st->table, newts);
1666 newts = NULL;
1667 }
1668
1669 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1670
1671 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001672 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001673
1674 if (!((1 << data_type) & st->remote_data))
1675 continue;
1676
Willy Tarreau1e82a142019-01-29 11:08:06 +01001677 decoded_int = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001678 if (!*msg_cur) {
1679 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001680 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001681 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001682
Willy Tarreau1e82a142019-01-29 11:08:06 +01001683 switch (stktable_data_types[data_type].std_type) {
1684 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001685 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1686 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001687 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001688 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001689
Willy Tarreau1e82a142019-01-29 11:08:06 +01001690 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001691 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1692 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001693 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001694 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001695
Willy Tarreau1e82a142019-01-29 11:08:06 +01001696 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001697 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1698 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001699 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001700 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001701
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001702 case STD_T_FRQP: {
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001703 struct freq_ctr data;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001704
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001705 /* First bit is reserved for the freq_ctr lock
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001706 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02001707 so we don't need to update the update the freq_ctr
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001708 using its internal lock */
1709
Willy Tarreau1e82a142019-01-29 11:08:06 +01001710 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001711 data.curr_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001712 if (!*msg_cur) {
1713 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001714 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001715 }
Willy Tarreau1e82a142019-01-29 11:08:06 +01001716
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001717 data.prev_ctr = intdecode(msg_cur, msg_end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001718 if (!*msg_cur) {
1719 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG, NULL, p);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001720 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001721 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001722
1723 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1724 if (data_ptr)
1725 stktable_data_cast(data_ptr, std_t_frqp) = data;
1726 break;
1727 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001728 case STD_T_DICT: {
1729 struct buffer *chunk;
1730 size_t data_len, value_len;
1731 unsigned int id;
1732 struct dict_entry *de;
1733 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001734 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001735
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001736 if (!decoded_int) {
1737 /* No entry. */
1738 break;
1739 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001740 data_len = decoded_int;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001741 if (*msg_cur + data_len > msg_end) {
1742 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1743 NULL, p, *msg_cur);
1744 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1745 NULL, p, msg_end, &data_len);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001746 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001747 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001748
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001749 /* Compute the end of the current data, <msg_end> being at the end of
1750 * the entire message.
1751 */
1752 end = *msg_cur + data_len;
1753 id = intdecode(msg_cur, end);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001754 if (!*msg_cur || !id) {
1755 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1756 NULL, p, *msg_cur, &id);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001757 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001758 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001759
1760 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001761 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001762 /* Dictionary entry key without value. */
Frédéric Lécaillef9e51be2020-11-12 19:53:11 +01001763 if (id > dc->max_entries) {
1764 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1765 NULL, p, NULL, &id);
1766 goto malformed_unlock;
1767 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001768 /* IDs sent over the network are numbered from 1. */
1769 de = dc->rx[id - 1].de;
1770 }
1771 else {
1772 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001773 value_len = intdecode(msg_cur, end);
1774 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécailled8659352020-11-10 16:18:03 +01001775 unlikely(value_len + 1 >= chunk->size)) {
1776 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1777 NULL, p, *msg_cur, &value_len);
1778 TRACE_PROTO("malformed message", PEERS_EV_UPDTMSG,
1779 NULL, p, end, &chunk->size);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001780 goto malformed_unlock;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001781 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001782
1783 chunk_memcpy(chunk, *msg_cur, value_len);
1784 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001785 *msg_cur += value_len;
1786
Thayne McCombs92149f92020-11-20 01:28:26 -07001787 de = dict_insert(&server_key_dict, chunk->area);
1788 dict_entry_unref(&server_key_dict, dc->rx[id - 1].de);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001789 dc->rx[id - 1].de = de;
1790 }
1791 if (de) {
1792 data_ptr = stktable_data_ptr(st->table, ts, data_type);
Thayne McCombs92149f92020-11-20 01:28:26 -07001793 if (data_ptr) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001794 HA_ATOMIC_INC(&de->refcount);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001795 stktable_data_cast(data_ptr, std_t_dict) = de;
Thayne McCombs92149f92020-11-20 01:28:26 -07001796 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001797 }
1798 break;
1799 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001800 }
1801 }
1802 /* Force new expiration */
1803 ts->expire = tick_add(now_ms, expire);
1804
1805 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1806 stktable_touch_remote(st->table, ts, 1);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001807 TRACE_LEAVE(PEERS_EV_UPDTMSG, NULL, p);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001808 return 1;
1809
1810 ignore_msg:
1811 /* skip consumed message */
1812 co_skip(si_oc(si), totl);
Frédéric Lécailled8659352020-11-10 16:18:03 +01001813 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001814 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001815
1816 malformed_unlock:
1817 /* malformed message */
1818 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1819 stktable_touch_remote(st->table, ts, 1);
1820 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001821 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001822 return 0;
1823
1824 malformed_free_newts:
1825 /* malformed message */
1826 stksess_free(st->table, newts);
1827 malformed_exit:
1828 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Frédéric Lécailled8659352020-11-10 16:18:03 +01001829 TRACE_DEVEL("leaving in error", PEERS_EV_UPDTMSG);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001830 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001831}
1832
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001833/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001834 * Function used to parse a stick-table update acknowledgement message after it
1835 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1836 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1837 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1838 * was encountered.
1839 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1840 */
1841static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1842 char **msg_cur, char *msg_end)
1843{
1844 /* ack message */
1845 uint32_t table_id ;
1846 uint32_t update;
1847 struct shared_table *st;
1848
Emeric Brunb0d60be2021-03-04 10:27:10 +01001849 /* ignore ack during teaching process */
1850 if (p->flags & PEER_F_TEACH_PROCESS)
1851 return 1;
1852
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001853 table_id = intdecode(msg_cur, msg_end);
1854 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1855 /* malformed message */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001856
1857 TRACE_PROTO("malformed message", PEERS_EV_ACKMSG,
1858 NULL, p, *msg_cur);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001859 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1860 return 0;
1861 }
1862
1863 memcpy(&update, *msg_cur, sizeof(update));
1864 update = ntohl(update);
1865
1866 for (st = p->tables; st; st = st->next) {
1867 if (st->local_id == table_id) {
1868 st->update = update;
1869 break;
1870 }
1871 }
1872
1873 return 1;
1874}
1875
1876/*
1877 * Function used to parse a stick-table switch message after it has been received
1878 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1879 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1880 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1881 * was encountered.
1882 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1883 */
1884static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1885 char **msg_cur, char *msg_end)
1886{
1887 struct shared_table *st;
1888 int table_id;
1889
1890 table_id = intdecode(msg_cur, msg_end);
1891 if (!*msg_cur) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001892 TRACE_PROTO("malformed message", PEERS_EV_SWTCMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001893 /* malformed message */
1894 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1895 return 0;
1896 }
1897
1898 p->remote_table = NULL;
1899 for (st = p->tables; st; st = st->next) {
1900 if (st->remote_id == table_id) {
1901 p->remote_table = st;
1902 break;
1903 }
1904 }
1905
1906 return 1;
1907}
1908
1909/*
1910 * Function used to parse a stick-table definition message after it has been received
1911 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1912 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1913 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1914 * was encountered.
1915 * <totl> is the length of the stick-table update message computed upon receipt.
1916 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1917 */
1918static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1919 char **msg_cur, char *msg_end, int totl)
1920{
1921 struct stream_interface *si = appctx->owner;
1922 int table_id_len;
1923 struct shared_table *st;
1924 int table_type;
1925 int table_keylen;
1926 int table_id;
1927 uint64_t table_data;
1928
1929 table_id = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001930 if (!*msg_cur) {
1931 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001932 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001933 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001934
1935 table_id_len = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001936 if (!*msg_cur) {
1937 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001938 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001939 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001940
1941 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001942 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1943 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p, *msg_cur, &table_id_len);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001944 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001945 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001946
1947 for (st = p->tables; st; st = st->next) {
1948 /* Reset IDs */
1949 if (st->remote_id == table_id)
1950 st->remote_id = 0;
1951
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001952 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1953 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001954 p->remote_table = st;
1955 }
1956
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001957 if (!p->remote_table) {
1958 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001959 goto ignore_msg;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001960 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001961
1962 *msg_cur += table_id_len;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001963 if (*msg_cur >= msg_end) {
1964 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001965 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001966 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001967
1968 table_type = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001969 if (!*msg_cur) {
1970 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001971 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001972 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001973
1974 table_keylen = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001975 if (!*msg_cur) {
1976 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001977 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001978 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001979
1980 table_data = intdecode(msg_cur, msg_end);
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001981 if (!*msg_cur) {
1982 TRACE_PROTO("malformed message", PEERS_EV_DEFMSG, NULL, p);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001983 goto malformed_exit;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001984 }
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001985
Emeric Brun530ba382020-06-02 11:17:42 +02001986 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001987 || p->remote_table->table->key_size != table_keylen) {
1988 p->remote_table = NULL;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01001989 TRACE_PROTO("ignored message", PEERS_EV_DEFMSG, NULL, p);
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001990 goto ignore_msg;
1991 }
1992
1993 p->remote_table->remote_data = table_data;
1994 p->remote_table->remote_id = table_id;
1995 return 1;
1996
1997 ignore_msg:
1998 co_skip(si_oc(si), totl);
1999 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01002000
2001 malformed_exit:
2002 /* malformed message */
2003 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2004 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002005}
2006
2007/*
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002008 * Receive a stick-table message or pre-parse any other message.
2009 * The message's header will be sent into <msg_head> which must be at least
2010 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
2011 * The first two bytes are always read, and the rest is only read if the
2012 * first bytes indicate a stick-table message. If the message is a stick-table
2013 * message, the varint is decoded and the equivalent number of bytes will be
2014 * copied into the trash at trash.area. <totl> is incremented by the number of
2015 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002016 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
2017 * -1 if there was an error updating the appctx state st0 accordingly.
2018 */
2019static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
2020 uint32_t *msg_len, int *totl)
2021{
2022 int reql;
2023 struct stream_interface *si = appctx->owner;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002024 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002025
2026 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
2027 if (reql <= 0) /* closed or EOL not found */
2028 goto incomplete;
2029
2030 *totl += reql;
2031
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02002032 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002033 return 1;
2034
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002035 /* This is a stick-table message, let's go on */
2036
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002037 /* Read and Decode message length */
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002038 msg_head += *totl;
2039 msg_head_sz -= *totl;
2040 reql = co_data(si_oc(si)) - *totl;
2041 if (reql > msg_head_sz)
2042 reql = msg_head_sz;
2043
2044 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002045 if (reql <= 0) /* closed */
2046 goto incomplete;
2047
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002048 cur = msg_head;
2049 *msg_len = intdecode(&cur, cur + reql);
2050 if (!cur) {
2051 /* the number is truncated, did we read enough ? */
2052 if (reql < msg_head_sz)
2053 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002054
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002055 /* malformed message */
2056 TRACE_PROTO("malformed message: too large length encoding", PEERS_EV_UPDTMSG);
2057 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2058 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002059 }
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002060 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002061
2062 /* Read message content */
2063 if (*msg_len) {
2064 if (*msg_len > trash.size) {
2065 /* Status code is not success, abort */
2066 appctx->st0 = PEER_SESS_ST_ERRSIZE;
2067 return -1;
2068 }
2069
2070 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
2071 if (reql <= 0) /* closed */
2072 goto incomplete;
2073 *totl += reql;
2074 }
2075
2076 return 1;
2077
2078 incomplete:
Willy Tarreau345ebcf2020-11-26 17:06:04 +01002079 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
2080 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002081 appctx->st0 = PEER_SESS_ST_END;
2082 return -1;
2083 }
2084
2085 return 0;
2086}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002087
2088/*
2089 * Treat the awaited message with <msg_head> as header.*
2090 * Return 1 if succeeded, 0 if not.
2091 */
2092static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
2093 char **msg_cur, char *msg_end, int msg_len, int totl)
2094{
2095 struct stream_interface *si = appctx->owner;
2096 struct stream *s = si_strm(si);
2097 struct peers *peers = strm_fe(s)->parent;
2098
2099 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
2100 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
2101 struct shared_table *st;
2102 /* Reset message: remote need resync */
2103
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002104 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2105 NULL, &msg_head[1], peers->local->id, peer->id);
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002106 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002107 for (st = peer->tables; st; st = st->next) {
Emeric Brun437e48a2021-04-28 09:49:33 +02002108 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002109 st->flags = 0;
2110 }
2111
2112 /* reset teaching flags to 0 */
2113 peer->flags &= PEER_TEACH_RESET;
2114
2115 /* flag to start to teach lesson */
2116 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002117 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002118 }
2119 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002120 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2121 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002122 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2123 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2124 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2125 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002126 if (peer->local)
2127 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
2128 else
2129 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002130 }
2131 peer->confirm++;
2132 }
2133 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002134 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2135 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002136 if (peer->flags & PEER_F_LEARN_ASSIGN) {
2137 peer->flags &= ~PEER_F_LEARN_ASSIGN;
2138 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2139
Emeric Brunccdfbae2021-04-28 12:59:35 +02002140 if (peer->local)
2141 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
2142 else
2143 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002144 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002145 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002146 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2147 }
2148 peer->confirm++;
2149 }
2150 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
2151 struct shared_table *st;
2152
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002153 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2154 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002155 /* If stopping state */
2156 if (stopping) {
2157 /* Close session, push resync no more needed */
2158 peer->flags |= PEER_F_TEACH_COMPLETE;
2159 appctx->st0 = PEER_SESS_ST_END;
2160 return 0;
2161 }
2162 for (st = peer->tables; st; st = st->next) {
2163 st->update = st->last_pushed = st->teaching_origin;
2164 st->flags = 0;
2165 }
2166
2167 /* reset teaching flags to 0 */
2168 peer->flags &= PEER_TEACH_RESET;
2169 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002170 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002171 TRACE_PROTO("received control message", PEERS_EV_CTRLMSG,
2172 NULL, &msg_head[1], peers->local->id, peer->id);
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002173 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002174 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002175 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002176 }
2177 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
2178 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
2179 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
2180 return 0;
2181 }
2182 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
2183 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
2184 return 0;
2185 }
2186 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
2187 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
2188 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
2189 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
2190 int update, expire;
2191
2192 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
2193 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2194 if (!peer_treat_updatemsg(appctx, peer, update, expire,
2195 msg_cur, msg_end, msg_len, totl))
2196 return 0;
2197
2198 }
2199 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
2200 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
2201 return 0;
2202 }
2203 }
2204 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2205 appctx->st0 = PEER_SESS_ST_ERRPROTO;
2206 return 0;
2207 }
2208
2209 return 1;
2210}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002211
2212
2213/*
2214 * Send any message to <peer> peer.
2215 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002216 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002217 * to a peer state change (from the peer I/O handler point of view).
2218 */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002219static inline int peer_send_msgs(struct appctx *appctx,
2220 struct peer *peer, struct peers *peers)
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002221{
2222 int repl;
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002223
2224 /* Need to request a resync */
2225 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
2226 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2227 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
2228
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002229 repl = peer_send_resync_reqmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002230 if (repl <= 0)
2231 return repl;
2232
2233 peers->flags |= PEERS_F_RESYNC_PROCESS;
2234 }
2235
2236 /* Nothing to read, now we start to write */
2237 if (peer->tables) {
2238 struct shared_table *st;
2239 struct shared_table *last_local_table;
2240
2241 last_local_table = peer->last_local_table;
2242 if (!last_local_table)
2243 last_local_table = peer->tables;
2244 st = last_local_table->next;
2245
2246 while (1) {
2247 if (!st)
2248 st = peer->tables;
2249
2250 /* It remains some updates to ack */
2251 if (st->last_get != st->last_acked) {
2252 repl = peer_send_ackmsg(st, appctx);
2253 if (repl <= 0)
2254 return repl;
2255
2256 st->last_acked = st->last_get;
2257 }
2258
2259 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
2260 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2261 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun8e7a13e2021-04-28 11:48:15 +02002262 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002263
2264 repl = peer_send_teach_process_msgs(appctx, peer, st);
2265 if (repl <= 0) {
2266 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2267 return repl;
2268 }
2269 }
2270 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
2271 }
Emeric Brun1675ada2021-04-22 18:13:13 +02002272 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002273 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2274 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2275 if (repl <= 0)
2276 return repl;
2277 }
2278
2279 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2280 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2281 if (repl <= 0)
2282 return repl;
2283 }
2284 }
2285
2286 if (st == last_local_table)
2287 break;
2288 st = st->next;
2289 }
2290 }
2291
2292 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002293 repl = peer_send_resync_finishedmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002294 if (repl <= 0)
2295 return repl;
2296
2297 /* flag finished message sent */
2298 peer->flags |= PEER_F_TEACH_FINISHED;
2299 }
2300
2301 /* Confirm finished or partial messages */
2302 while (peer->confirm) {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002303 repl = peer_send_resync_confirmsg(appctx, peer, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002304 if (repl <= 0)
2305 return repl;
2306
2307 peer->confirm--;
2308 }
2309
2310 return 1;
2311}
2312
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002313/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002314 * Read and parse a first line of a "hello" peer protocol message.
2315 * Returns 0 if could not read a line, -1 if there was a read error or
2316 * the line is malformed, 1 if succeeded.
2317 */
2318static inline int peer_getline_version(struct appctx *appctx,
2319 unsigned int *maj_ver, unsigned int *min_ver)
2320{
2321 int reql;
2322
2323 reql = peer_getline(appctx);
2324 if (!reql)
2325 return 0;
2326
2327 if (reql < 0)
2328 return -1;
2329
2330 /* test protocol */
2331 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2332 appctx->st0 = PEER_SESS_ST_EXIT;
2333 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2334 return -1;
2335 }
2336 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2337 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2338 appctx->st0 = PEER_SESS_ST_EXIT;
2339 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2340 return -1;
2341 }
2342
2343 return 1;
2344}
2345
2346/*
2347 * Read and parse a second line of a "hello" peer protocol message.
2348 * Returns 0 if could not read a line, -1 if there was a read error or
2349 * the line is malformed, 1 if succeeded.
2350 */
2351static inline int peer_getline_host(struct appctx *appctx)
2352{
2353 int reql;
2354
2355 reql = peer_getline(appctx);
2356 if (!reql)
2357 return 0;
2358
2359 if (reql < 0)
2360 return -1;
2361
2362 /* test hostname match */
2363 if (strcmp(localpeer, trash.area) != 0) {
2364 appctx->st0 = PEER_SESS_ST_EXIT;
2365 appctx->st1 = PEER_SESS_SC_ERRHOST;
2366 return -1;
2367 }
2368
2369 return 1;
2370}
2371
2372/*
2373 * Read and parse a last line of a "hello" peer protocol message.
2374 * Returns 0 if could not read a character, -1 if there was a read error or
2375 * the line is malformed, 1 if succeeded.
2376 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2377 */
2378static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2379{
2380 char *p;
2381 int reql;
2382 struct peer *peer;
2383 struct stream_interface *si = appctx->owner;
2384 struct stream *s = si_strm(si);
2385 struct peers *peers = strm_fe(s)->parent;
2386
2387 reql = peer_getline(appctx);
2388 if (!reql)
2389 return 0;
2390
2391 if (reql < 0)
2392 return -1;
2393
2394 /* parse line "<peer name> <pid> <relative_pid>" */
2395 p = strchr(trash.area, ' ');
2396 if (!p) {
2397 appctx->st0 = PEER_SESS_ST_EXIT;
2398 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2399 return -1;
2400 }
2401 *p = 0;
2402
2403 /* lookup known peer */
2404 for (peer = peers->remote; peer; peer = peer->next) {
2405 if (strcmp(peer->id, trash.area) == 0)
2406 break;
2407 }
2408
2409 /* if unknown peer */
2410 if (!peer) {
2411 appctx->st0 = PEER_SESS_ST_EXIT;
2412 appctx->st1 = PEER_SESS_SC_ERRPEER;
2413 return -1;
2414 }
2415 *curpeer = peer;
2416
2417 return 1;
2418}
2419
2420/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002421 * Init <peer> peer after having accepted it at peer protocol level.
2422 */
2423static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2424{
2425 struct shared_table *st;
2426
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002427 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002428 /* Register status code */
2429 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002430 peer->last_hdshk = now_ms;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002431
2432 /* Awake main task */
2433 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2434
2435 /* Init confirm counter */
2436 peer->confirm = 0;
2437
2438 /* Init cursors */
2439 for (st = peer->tables; st ; st = st->next) {
2440 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002441 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2442 /* if st->update appears to be in future it means
2443 * that the last acked value is very old and we
2444 * remain unconnected a too long time to use this
2445 * acknowlegement as a reset.
2446 * We should update the protocol to be able to
2447 * signal the remote peer that it needs a full resync.
2448 * Here a partial fix consist to set st->update at
2449 * the max past value
2450 */
2451 if ((int)(st->table->localupdate - st->update) < 0)
2452 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002453 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002454 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002455 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2456 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002457 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002458 }
2459
2460 /* reset teaching and learning flags to 0 */
2461 peer->flags &= PEER_TEACH_RESET;
2462 peer->flags &= PEER_LEARN_RESET;
2463
2464 /* if current peer is local */
2465 if (peer->local) {
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002466 /* if current host need resyncfrom local and no process assigned */
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002467 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2468 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2469 /* assign local peer for a lesson, consider lesson already requested */
2470 peer->flags |= PEER_F_LEARN_ASSIGN;
2471 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunccdfbae2021-04-28 12:59:35 +02002472 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002473 }
2474
2475 }
2476 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2477 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2478 /* assign peer for a lesson */
2479 peer->flags |= PEER_F_LEARN_ASSIGN;
2480 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002481 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002482 }
2483}
2484
2485/*
2486 * Init <peer> peer after having connected it at peer protocol level.
2487 */
2488static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2489{
2490 struct shared_table *st;
2491
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002492 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002493 /* Init cursors */
2494 for (st = peer->tables; st ; st = st->next) {
2495 st->last_get = st->last_acked = 0;
Emeric Brund9729da2021-02-23 16:50:53 +01002496 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2497 /* if st->update appears to be in future it means
2498 * that the last acked value is very old and we
2499 * remain unconnected a too long time to use this
2500 * acknowlegement as a reset.
2501 * We should update the protocol to be able to
2502 * signal the remote peer that it needs a full resync.
2503 * Here a partial fix consist to set st->update at
2504 * the max past value.
2505 */
2506 if ((int)(st->table->localupdate - st->update) < 0)
2507 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002508 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun1a6b43e2021-04-20 14:43:46 +02002509 st->flags = 0;
Emeric Bruncc9cce92021-02-23 17:08:08 +01002510 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2511 st->table->commitupdate = st->last_pushed;
Emeric Brund9729da2021-02-23 16:50:53 +01002512 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002513 }
2514
2515 /* Init confirm counter */
2516 peer->confirm = 0;
2517
2518 /* reset teaching and learning flags to 0 */
2519 peer->flags &= PEER_TEACH_RESET;
2520 peer->flags &= PEER_LEARN_RESET;
2521
2522 /* If current peer is local */
2523 if (peer->local) {
2524 /* flag to start to teach lesson */
2525 peer->flags |= PEER_F_TEACH_PROCESS;
2526 }
2527 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2528 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2529 /* If peer is remote and resync from remote is needed,
2530 and no peer currently assigned */
2531
2532 /* assign peer for a lesson */
2533 peer->flags |= PEER_F_LEARN_ASSIGN;
2534 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002535 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002536 }
2537}
2538
2539/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05002540 * IO Handler to handle message exchange with a peer
Emeric Brun2b920a12010-09-23 18:30:22 +02002541 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002542static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002543{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002544 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002545 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002546 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002547 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002548 int reql = 0;
2549 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002550 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002551 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002552
Joseph Herlant82b2f542018-11-15 12:19:14 -08002553 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002554 if (si_ic(si)->buf.size == 0) {
2555 si_rx_room_blk(si);
2556 goto out;
2557 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002558
Emeric Brun2b920a12010-09-23 18:30:22 +02002559 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002560 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002561switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002562 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002563 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002564 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002565 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002566 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002567 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002568 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002569 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002570 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002571 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2572 if (reql <= 0) {
2573 if (!reql)
2574 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002575 goto switchstate;
2576 }
2577
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002578 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002579 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002580 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002581 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002582 reql = peer_getline_host(appctx);
2583 if (reql <= 0) {
2584 if (!reql)
2585 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002586 goto switchstate;
2587 }
2588
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002589 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002590 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002591 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002592 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002593 reql = peer_getline_last(appctx, &curpeer);
2594 if (reql <= 0) {
2595 if (!reql)
2596 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002597 goto switchstate;
2598 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002599
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002600 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002601 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002602 if (curpeer->local) {
2603 /* Local connection, reply a retry */
2604 appctx->st0 = PEER_SESS_ST_EXIT;
2605 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2606 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002607 }
Emeric Brun80527f52017-06-19 17:46:37 +02002608
2609 /* we're killing a connection, we must apply a random delay before
2610 * retrying otherwise the other end will do the same and we can loop
2611 * for a while.
2612 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002613 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002614 peer_session_forceshutdown(curpeer);
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002615 curpeer->heartbeat = TICK_ETERNITY;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002616 curpeer->coll++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002617 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002618 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2619 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2620 curpeer->flags |= PEER_F_DWNGRD;
2621 }
2622 else {
2623 curpeer->flags &= ~PEER_F_DWNGRD;
2624 }
2625 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002626 curpeer->appctx = appctx;
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002627 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002628 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002629 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002630 _HA_ATOMIC_INC(&active_peers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002631 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002632 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002633 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002634 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002635 if (!curpeer) {
2636 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002637 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002638 if (curpeer->appctx != appctx) {
2639 appctx->st0 = PEER_SESS_ST_END;
2640 goto switchstate;
2641 }
2642 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002643
2644 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002645 if (repl <= 0) {
2646 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002647 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002648 goto switchstate;
2649 }
2650
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002651 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002652
Emeric Brun2b920a12010-09-23 18:30:22 +02002653 /* switch to waiting message state */
Willy Tarreau4781b152021-04-06 13:53:36 +02002654 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002655 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002656 goto switchstate;
2657 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002658 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002659 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002660 if (!curpeer) {
2661 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002662 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002663 if (curpeer->appctx != appctx) {
2664 appctx->st0 = PEER_SESS_ST_END;
2665 goto switchstate;
2666 }
2667 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002668
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002669 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002670 if (repl <= 0) {
2671 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002672 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002673 goto switchstate;
2674 }
2675
2676 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002677 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002678 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002679 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002680 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002681 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002682 if (!curpeer) {
2683 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002684 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002685 if (curpeer->appctx != appctx) {
2686 appctx->st0 = PEER_SESS_ST_END;
2687 goto switchstate;
2688 }
2689 }
2690
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002691 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002692 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002693
Frédéric Lécaillece025572019-01-21 13:38:06 +01002694 reql = peer_getline(appctx);
2695 if (!reql)
2696 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002697
Frédéric Lécaillece025572019-01-21 13:38:06 +01002698 if (reql < 0)
2699 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002700
2701 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002702 curpeer->statuscode = atoi(trash.area);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002703 curpeer->last_hdshk = now_ms;
Emeric Brun2b920a12010-09-23 18:30:22 +02002704
2705 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002706 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002707
2708 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002709 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002710 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002711 }
2712 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002713 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2714 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002715 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002716 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002717 goto switchstate;
2718 }
Willy Tarreau4781b152021-04-06 13:53:36 +02002719 _HA_ATOMIC_INC(&connected_peers);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002720 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002721 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002722 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002723 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002724 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 char *msg_cur = trash.area;
2726 char *msg_end = trash.area;
Willy Tarreau1dfd4f12020-11-13 14:10:20 +01002727 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02002728 int totl = 0;
2729
Willy Tarreau2d372c22018-11-05 17:12:27 +01002730 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002731 if (!curpeer) {
2732 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002733 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002734 if (curpeer->appctx != appctx) {
2735 appctx->st0 = PEER_SESS_ST_END;
2736 goto switchstate;
2737 }
2738 }
2739
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002740 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2741 if (reql <= 0) {
2742 if (reql == -1)
2743 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002744 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002745 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002746
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002747 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002748 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002749 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002750
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002751 curpeer->flags |= PEER_F_ALIVE;
2752
Emeric Brun2b920a12010-09-23 18:30:22 +02002753 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002754 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002755 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002756 goto switchstate;
2757
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002758send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002759 if (curpeer->flags & PEER_F_HEARTBEAT) {
2760 curpeer->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002761 repl = peer_send_heartbeatmsg(appctx, curpeer, curpeers);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002762 if (repl <= 0) {
2763 if (repl == -1)
2764 goto out;
2765 goto switchstate;
2766 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002767 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002768 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002769 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002770 repl = peer_send_msgs(appctx, curpeer, curpeers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002771 if (repl <= 0) {
2772 if (repl == -1)
2773 goto out;
2774 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002775 }
2776
Emeric Brun2b920a12010-09-23 18:30:22 +02002777 /* noting more to do */
2778 goto out;
2779 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002780 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002781 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002782 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002783 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002784 if (peer_send_status_errormsg(appctx) == -1)
2785 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002786 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002787 goto switchstate;
2788 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002789 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002790 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002791 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002792 if (peer_send_error_size_limitmsg(appctx) == -1)
2793 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002794 appctx->st0 = PEER_SESS_ST_END;
2795 goto switchstate;
2796 }
2797 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002798 TRACE_PROTO("protocol error", PEERS_EV_PROTOERR,
2799 NULL, curpeer, &prev_state);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002800 if (curpeer)
2801 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002802 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002803 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002804 prev_state = appctx->st0;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002805 if (peer_send_error_protomsg(appctx) == -1) {
2806 TRACE_PROTO("could not send error message", PEERS_EV_PROTOERR);
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002807 goto out;
Frédéric Lécailleda2b0842021-01-15 16:21:28 +01002808 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002809 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002810 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002811 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02002812 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002813 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002814 if (prev_state == PEER_SESS_ST_WAITMSG)
Willy Tarreau4781b152021-04-06 13:53:36 +02002815 _HA_ATOMIC_DEC(&connected_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002816 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002817 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002818 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002819 curpeer = NULL;
2820 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002821 si_shutw(si);
2822 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002823 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002824 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002825 }
2826 }
2827 }
2828out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002829 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002830
2831 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002832 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002833 return;
2834}
2835
Willy Tarreau30576452015-04-13 13:50:30 +02002836static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002837 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002838 .name = "<PEER>", /* used for logging */
2839 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002840 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002841};
Emeric Brun2b920a12010-09-23 18:30:22 +02002842
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002843
Emeric Brun2b920a12010-09-23 18:30:22 +02002844/*
2845 * Use this function to force a close of a peer session
2846 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002847static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002848{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002849 struct appctx *appctx = peer->appctx;
2850
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002851 /* Note that the peer sessions which have just been created
2852 * (->st0 == PEER_SESS_ST_CONNECT) must not
2853 * be shutdown, if not, the TCP session will never be closed
2854 * and stay in CLOSE_WAIT state after having been closed by
2855 * the remote side.
2856 */
2857 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002858 return;
2859
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002860 if (appctx->applet != &peer_applet)
2861 return;
2862
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002863 __peer_session_deinit(peer);
2864
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002865 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002866 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002867}
2868
Willy Tarreau91d96282015-03-13 15:47:26 +01002869/* Pre-configures a peers frontend to accept incoming connections */
2870void peers_setup_frontend(struct proxy *fe)
2871{
2872 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002873 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaua389c9e2020-10-07 17:49:42 +02002874 fe->mode = PR_MODE_PEERS;
Willy Tarreau91d96282015-03-13 15:47:26 +01002875 fe->maxconn = 0;
2876 fe->conn_retries = CONN_RETRIES;
2877 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002878 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002879 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002880 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
2881}
2882
Emeric Brun2b920a12010-09-23 18:30:22 +02002883/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002884 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002885 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002886static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002887{
Willy Tarreau04b92862017-09-15 11:01:04 +02002888 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002889 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002890 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002891 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02002892
Frédéric Lécaille2b0ba542021-01-18 15:14:39 +01002893 peer->new_conn++;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002894 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02002895 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002896 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02002897 peer->last_hdshk = now_ms;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002898 s = NULL;
2899
Emeric Brun1138fd02017-06-19 12:38:55 +02002900 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002901 if (!appctx)
2902 goto out_close;
2903
2904 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002905 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002906
Willy Tarreau04b92862017-09-15 11:01:04 +02002907 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002908 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002909 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002910 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002911 }
2912
Christopher Faulet26256f82020-09-14 11:40:13 +02002913 if ((s = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002914 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002915 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002916 }
2917
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002918 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002919 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002920 appctx_wakeup(appctx);
2921
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002922 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002923 s->target = peer_session_target(peer, s);
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002924 if (!sockaddr_alloc(&s->target_addr, &peer->addr, sizeof(peer->addr)))
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002925 goto out_free_strm;
Willy Tarreau02efeda2019-07-18 17:21:24 +02002926 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Willy Tarreaudbd02672017-12-06 17:39:53 +01002927 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002928
Emeric Brun2b920a12010-09-23 18:30:22 +02002929 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002930 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002931
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002932 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002933
Emeric Brunb3971ab2015-05-12 18:49:09 +02002934 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002935 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau4781b152021-04-06 13:53:36 +02002936 _HA_ATOMIC_INC(&active_peers);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002937 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002938
2939 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02002940 out_free_strm:
Willy Tarreau2b718102021-04-21 07:32:39 +02002941 LIST_DELETE(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002942 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002943 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002944 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002945 out_free_appctx:
2946 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002947 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002948 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002949}
2950
2951/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002952 * Task processing function to manage re-connect, peer session
Willy Tarreauf6c88422021-01-29 12:38:42 +01002953 * tasks wakeup on local update and heartbeat. Let's keep it exported so that it
2954 * resolves in stack traces and "show tasks".
Emeric Brun2b920a12010-09-23 18:30:22 +02002955 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002956struct task *process_peer_sync(struct task * task, void *context, unsigned int state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002957{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002958 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002959 struct peer *ps;
2960 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002961
2962 task->expire = TICK_ETERNITY;
2963
Emeric Brunb3971ab2015-05-12 18:49:09 +02002964 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002965 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002966 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002967 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002968 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002969 return NULL;
2970 }
2971
Emeric Brun80527f52017-06-19 17:46:37 +02002972 /* Acquire lock for all peers of the section */
2973 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002974 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002975
Emeric Brun2b920a12010-09-23 18:30:22 +02002976 if (!stopping) {
2977 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002978
Emeric Brun2c4ab412021-04-21 16:06:35 +02002979 /* resync timeout set to TICK_ETERNITY means we just start
2980 * a new process and timer was not initialized.
2981 * We must arm this timer to switch to a request to a remote
2982 * node if incoming connection from old local process never
2983 * comes.
2984 */
2985 if (peers->resync_timeout == TICK_ETERNITY)
2986 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
2987
Emeric Brunb3971ab2015-05-12 18:49:09 +02002988 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2989 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2990 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002991 /* Resync from local peer needed
2992 no peer was assigned for the lesson
2993 and no old local peer found
2994 or resync timeout expire */
2995
2996 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002997 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunccdfbae2021-04-28 12:59:35 +02002998 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02002999
3000 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003001 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02003002 }
3003
3004 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003005 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003006 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003007 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003008 if (!ps->appctx) {
3009 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003010 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003011 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003012 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003013 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003014 tick_is_expired(ps->reconnect, now_ms))) {
3015 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003016 * or previous peer connection established with success
3017 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02003018 * and reconnection timer is expired */
3019
3020 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003021 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02003022 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02003023 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003024 /* If previous session failed during connection
3025 * but reconnection timer is not expired */
3026
3027 /* reschedule task for reconnect */
3028 task->expire = tick_first(task->expire, ps->reconnect);
3029 }
3030 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01003031 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003032 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003033 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003034 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3035 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02003036 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
3037 /* Resync from a remote is needed
3038 * and no peer was assigned for lesson
3039 * and current peer may be up2date */
3040
3041 /* assign peer for the lesson */
3042 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003043 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003044 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02003045
Willy Tarreau9df94c22016-10-31 18:42:52 +01003046 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02003047 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02003048 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003049 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003050 int update_to_push = 0;
3051
Emeric Brunb3971ab2015-05-12 18:49:09 +02003052 /* Awake session if there is data to push */
3053 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003054 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003055 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003056 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003057 /* There is no need to send a heartbeat message
3058 * when some updates must be pushed. The remote
3059 * peer will consider <ps> peer as alive when it will
3060 * receive these updates.
3061 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003062 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003063 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003064 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003065 /* We are going to send updates, let's ensure we will
3066 * come back to send heartbeat messages or to reconnect.
3067 */
3068 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003069 appctx_wakeup(ps->appctx);
3070 break;
3071 }
3072 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003073 /* When there are updates to send we do not reconnect
3074 * and do not send heartbeat message either.
3075 */
3076 if (!update_to_push) {
3077 if (tick_is_expired(ps->reconnect, now_ms)) {
3078 if (ps->flags & PEER_F_ALIVE) {
3079 /* This peer was alive during a 'reconnect' period.
3080 * Flag it as not alive again for the next period.
3081 */
3082 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01003083 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003084 }
3085 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01003086 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillebaeb9192020-10-14 11:50:26 +02003087 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02003088 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003089 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01003090 }
3091 }
3092 else if (tick_is_expired(ps->heartbeat, now_ms)) {
3093 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
3094 ps->flags |= PEER_F_HEARTBEAT;
3095 appctx_wakeup(ps->appctx);
3096 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01003097 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01003098 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003099 }
3100 /* else do nothing */
3101 } /* SUCCESSCODE */
3102 } /* !ps->peer->local */
3103 } /* for */
3104
3105 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003106 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
3107 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
3108 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003109 /* Resync from remote peer needed
3110 * no peer was assigned for the lesson
3111 * and resync timeout expire */
3112
3113 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003114 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunccdfbae2021-04-28 12:59:35 +02003115 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02003116 }
3117
Emeric Brunb3971ab2015-05-12 18:49:09 +02003118 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003119 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02003120 /* reschedule task to resync timeout if not expired, to ended resync if needed */
3121 if (!tick_is_expired(peers->resync_timeout, now_ms))
3122 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02003123 }
3124 } /* !stopping */
3125 else {
3126 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01003127 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08003128 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003129 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003130 /* add DO NOT STOP flag if not present */
Willy Tarreau4781b152021-04-06 13:53:36 +02003131 _HA_ATOMIC_INC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003132 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02003133
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003134 /* disconnect all connected peers to process a local sync
3135 * this must be done only the first time we are switching
3136 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02003137 */
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003138 for (ps = peers->remote; ps; ps = ps->next) {
3139 /* we're killing a connection, we must apply a random delay before
3140 * retrying otherwise the other end will do the same and we can loop
3141 * for a while.
3142 */
3143 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
3144 if (ps->appctx) {
3145 peer_session_forceshutdown(ps);
3146 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003147 }
3148 }
3149 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003150
Emeric Brunb3971ab2015-05-12 18:49:09 +02003151 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02003152 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003153 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003154 /* resync of new process was complete, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003155 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003156 peers->flags &= ~PEERS_F_DONOTSTOP;
3157 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003158 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003159 }
3160 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01003161 else if (!ps->appctx) {
3162 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02003163 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01003164 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
3165 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
3166 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003167 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01003168 * or previous peer connection was successfully established
3169 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02003170 * or during previous connect, peer replies a try again statuscode */
3171
Emeric Bruncbfe5eb2021-04-22 18:20:37 +02003172 /* connect to the local peer if we must push a local sync */
3173 if (peers->flags & PEERS_F_DONOTSTOP) {
3174 peer_session_create(peers, ps);
3175 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003176 }
3177 else {
3178 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003179 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003180 /* unable to resync new process, current process can die now */
Willy Tarreau4781b152021-04-06 13:53:36 +02003181 _HA_ATOMIC_DEC(&jobs);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003182 peers->flags &= ~PEERS_F_DONOTSTOP;
3183 for (st = ps->tables; st ; st = st->next)
Emeric Brun2cc201f2021-04-23 12:21:26 +02003184 HA_ATOMIC_DEC(&st->table->refcnt);
Emeric Brun2b920a12010-09-23 18:30:22 +02003185 }
3186 }
3187 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003188 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01003189 /* current peer connection is active and established
3190 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02003191 for (st = ps->tables; st ; st = st->next) {
Emeric Brun8e7a13e2021-04-28 11:48:15 +02003192 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02003193 appctx_wakeup(ps->appctx);
3194 break;
3195 }
3196 }
Emeric Brun2b920a12010-09-23 18:30:22 +02003197 }
3198 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02003199
3200 /* Release lock for all peers of the section */
3201 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003202 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02003203
Emeric Brun2b920a12010-09-23 18:30:22 +02003204 /* Wakeup for re-connect */
3205 return task;
3206}
3207
Emeric Brunb3971ab2015-05-12 18:49:09 +02003208
Emeric Brun2b920a12010-09-23 18:30:22 +02003209/*
Willy Tarreaud9443442018-10-15 11:18:03 +02003210 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02003211 */
Willy Tarreaud9443442018-10-15 11:18:03 +02003212int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02003213{
Emeric Brun2b920a12010-09-23 18:30:22 +02003214 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02003215
Emeric Brun2b920a12010-09-23 18:30:22 +02003216 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02003217 peers->peers_fe->maxconn += 3;
3218 }
3219
Emeric Brunc60def82017-09-27 14:59:38 +02003220 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02003221 if (!peers->sync_task)
3222 return 0;
3223
Emeric Brunb3971ab2015-05-12 18:49:09 +02003224 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003225 peers->sync_task->context = (void *)peers;
3226 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
3227 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02003228 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003229}
3230
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003231/*
3232 * Allocate a cache a dictionary entries used upon transmission.
3233 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003234static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003235{
3236 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003237 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003238
3239 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003240 entries = calloc(max_entries, sizeof *entries);
3241 if (!d || !entries)
3242 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003243
3244 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003245 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003246 d->cached_entries = EB_ROOT_UNIQUE;
3247 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003248
3249 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003250
3251 err:
3252 free(d);
3253 free(entries);
3254 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003255}
3256
3257/*
3258 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3259 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05003260 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003261 * Must be deallocated calling free_dcache().
3262 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003263static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003264{
3265 struct dcache_tx *dc_tx;
3266 struct dcache *dc;
3267 struct dcache_rx *dc_rx;
3268
3269 dc = calloc(1, sizeof *dc);
3270 dc_tx = new_dcache_tx(max_entries);
3271 dc_rx = calloc(max_entries, sizeof *dc_rx);
3272 if (!dc || !dc_tx || !dc_rx)
3273 goto err;
3274
3275 dc->tx = dc_tx;
3276 dc->rx = dc_rx;
3277 dc->max_entries = max_entries;
3278
3279 return dc;
3280
3281 err:
3282 free(dc);
3283 free(dc_tx);
3284 free(dc_rx);
3285 return NULL;
3286}
3287
3288/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003289 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3290 * entries used upon transmission.
3291 * Return the entry if found, NULL if not.
3292 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003293static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3294 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003295{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003296 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003297}
3298
3299/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003300 * Flush <dc> cache.
3301 * Always succeeds.
3302 */
3303static inline void flush_dcache(struct peer *peer)
3304{
3305 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003306 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003307
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003308 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003309 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003310 dc->tx->entries[i].key = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -07003311 dict_entry_unref(&server_key_dict, dc->rx[i].de);
3312 dc->rx[i].de = NULL;
Frédéric Lécailleea875e62020-11-12 21:01:54 +01003313 }
3314 dc->tx->prev_lookup = NULL;
3315 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003316
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003317 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003318}
3319
3320/*
3321 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3322 * with information provided by <i> dictionary cache entry (especially the value
3323 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003324 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003325 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003326static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003327{
3328 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003329 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003330
3331 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003332
3333 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3334 o = dc_tx->prev_lookup;
3335 } else {
3336 o = dcache_tx_lookup_value(dc_tx, i);
3337 if (o) {
3338 /* Save it */
3339 dc_tx->prev_lookup = o;
3340 }
3341 }
3342
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003343 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003344 /* Copy the ID. */
3345 i->id = o - dc->tx->entries;
3346 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003347 }
3348
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003349 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003350 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003351
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003352 ebpt_delete(o);
3353 o->key = i->entry.key;
3354 ebpt_insert(&dc_tx->cached_entries, o);
3355 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003356
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003357 /* Update the index for the next entry to put in cache */
3358 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003359
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003360 return o;
3361}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003362
3363/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003364 * Allocate a dictionary cache for each peer of <peers> section.
3365 * Return 1 if succeeded, 0 if not.
3366 */
3367int peers_alloc_dcache(struct peers *peers)
3368{
3369 struct peer *p;
3370
3371 for (p = peers->remote; p; p = p->next) {
3372 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3373 if (!p->dcache)
3374 return 0;
3375 }
3376
3377 return 1;
3378}
3379
3380/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003381 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003382 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003383 */
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003384int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003385{
3386 struct shared_table *st;
3387 struct peer * curpeer;
3388 int id = 0;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003389 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003390
3391 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003392 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003393 if (!st) {
3394 retval = 1;
3395 break;
3396 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003397 st->table = table;
3398 st->next = curpeer->tables;
3399 if (curpeer->tables)
3400 id = curpeer->tables->local_id;
3401 st->local_id = id + 1;
3402
Emeric Brun2cc201f2021-04-23 12:21:26 +02003403 /* If peer is local we inc table
3404 * refcnt to protect against flush
3405 * until this process pushed all
3406 * table content to the new one
3407 */
3408 if (curpeer->local)
3409 HA_ATOMIC_INC(&st->table->refcnt);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003410 curpeer->tables = st;
3411 }
3412
3413 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +02003414
3415 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003416}
3417
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003418/*
3419 * Parse the "show peers" command arguments.
3420 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3421 * error message.
3422 */
3423static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3424{
3425 appctx->ctx.cfgpeers.target = NULL;
3426
Willy Tarreau49962b52021-02-12 16:56:22 +01003427 if (strcmp(args[2], "dict") == 0) {
3428 /* show the dictionaries (large dump) */
3429 appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT;
3430 args++;
3431 } else if (strcmp(args[2], "-") == 0)
3432 args++; // allows to show a section called "dict"
3433
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003434 if (*args[2]) {
3435 struct peers *p;
3436
3437 for (p = cfg_peers; p; p = p->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003438 if (strcmp(p->id, args[2]) == 0) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003439 appctx->ctx.cfgpeers.target = p;
3440 break;
3441 }
3442 }
3443
Willy Tarreau9d008692019-08-09 11:21:01 +02003444 if (!p)
3445 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003446 }
3447
3448 return 0;
3449}
3450
3451/*
3452 * This function dumps the peer state information of <peers> "peers" section.
3453 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3454 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3455 */
3456static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3457{
3458 struct tm tm;
3459
3460 get_localtime(peers->last_change, &tm);
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003461 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 +02003462 peers,
3463 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3464 tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau1ad64ac2020-09-24 08:48:08 +02003465 peers->id, peers->disabled, peers->flags,
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003466 peers->resync_timeout ?
3467 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3468 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003469 TICKS_TO_MS(1000)) : "<NEVER>",
3470 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003471
3472 if (ci_putchk(si_ic(si), msg) == -1) {
3473 si_rx_room_blk(si);
3474 return 0;
3475 }
3476
3477 return 1;
3478}
3479
3480/*
3481 * This function dumps <peer> state information.
3482 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3483 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3484 */
Willy Tarreau49962b52021-02-12 16:56:22 +01003485static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer, int flags)
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003486{
3487 struct connection *conn;
3488 char pn[INET6_ADDRSTRLEN];
3489 struct stream_interface *peer_si;
3490 struct stream *peer_s;
3491 struct appctx *appctx;
3492 struct shared_table *st;
3493
3494 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003495 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003496 peer, peer->id,
3497 peer->local ? "local" : "remote",
Frédéric Lécaillee7e2b212020-10-05 12:33:07 +02003498 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003499 pn, get_host_port(&peer->addr),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003500 statuscode_str(peer->statuscode));
3501
3502 chunk_appendf(msg, " last_hdshk=%s\n",
3503 peer->last_hdshk ? human_time(TICKS_TO_MS(now_ms - peer->last_hdshk),
3504 TICKS_TO_MS(1000)) : "<NEVER>");
3505
3506 chunk_appendf(msg, " reconnect=%s",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003507 peer->reconnect ?
3508 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3509 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003510 TICKS_TO_MS(1000)) : "<NEVER>");
3511
3512 chunk_appendf(msg, " heartbeat=%s",
3513 peer->heartbeat ?
3514 tick_is_expired(peer->heartbeat, now_ms) ? "<PAST>" :
3515 human_time(TICKS_TO_MS(peer->heartbeat - now_ms),
3516 TICKS_TO_MS(1000)) : "<NEVER>");
3517
3518 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 +01003519 peer->confirm, peer->tx_hbt, peer->rx_hbt,
Frédéric Lécaille3fc0fe02020-10-08 09:46:24 +02003520 peer->no_hbt, peer->new_conn, peer->proto_err, peer->coll);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003521
3522 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3523
3524 appctx = peer->appctx;
3525 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003526 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003527
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003528 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3529 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003530
3531 peer_si = peer->appctx->owner;
3532 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003533 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003534
3535 peer_s = si_strm(peer_si);
3536 if (!peer_s)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003537 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003538
3539 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3540
3541 conn = objt_conn(strm_orig(peer_s));
3542 if (conn)
3543 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3544
Willy Tarreau3ca14902019-07-17 14:53:15 +02003545 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 +02003546 case AF_INET:
3547 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003548 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003549 break;
3550 case AF_UNIX:
3551 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3552 break;
3553 }
3554
Willy Tarreau3ca14902019-07-17 14:53:15 +02003555 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 +02003556 case AF_INET:
3557 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003558 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003559 break;
3560 case AF_UNIX:
3561 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3562 break;
3563 }
3564
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003565 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003566 if (peer->remote_table)
3567 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3568 peer->remote_table,
3569 peer->remote_table->table->id,
3570 peer->remote_table->local_id,
3571 peer->remote_table->remote_id);
3572
3573 if (peer->last_local_table)
3574 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3575 peer->last_local_table,
3576 peer->last_local_table->table->id,
3577 peer->last_local_table->local_id,
3578 peer->last_local_table->remote_id);
3579
3580 if (peer->tables) {
3581 chunk_appendf(&trash, "\n shared tables:");
3582 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003583 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003584 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003585 struct dcache *dcache;
3586
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003587 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003588 dcache = peer->dcache;
3589
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003590 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3591 "flags=0x%x remote_data=0x%llx",
3592 st, st->local_id, st->remote_id,
3593 st->flags, (unsigned long long)st->remote_data);
3594 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3595 " teaching_origin=%u update=%u",
3596 st->last_acked, st->last_pushed, st->last_get,
3597 st->teaching_origin, st->update);
3598 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun2cc201f2021-04-23 12:21:26 +02003599 " commitupdate=%u refcnt=%u",
3600 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Willy Tarreau49962b52021-02-12 16:56:22 +01003601 if (flags & PEERS_SHOW_F_DICT) {
3602 chunk_appendf(&trash, "\n TX dictionary cache:");
3603 count = 0;
3604 for (i = 0; i < dcache->max_entries; i++) {
3605 struct ebpt_node *node;
3606 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003607
Willy Tarreau49962b52021-02-12 16:56:22 +01003608 node = &dcache->tx->entries[i];
3609 if (!node->key)
3610 break;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003611
Willy Tarreau49962b52021-02-12 16:56:22 +01003612 if (!count++)
3613 chunk_appendf(&trash, "\n ");
3614 de = node->key;
3615 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3616 count &= 0x3;
3617 }
3618 chunk_appendf(&trash, "\n RX dictionary cache:");
3619 count = 0;
3620 for (i = 0; i < dcache->max_entries; i++) {
3621 if (!count++)
3622 chunk_appendf(&trash, "\n ");
3623 chunk_appendf(&trash, " %3u -> %s", i,
3624 dcache->rx[i].de ?
3625 (char *)dcache->rx[i].de->value.key : "-");
3626 count &= 0x3;
3627 }
3628 } else {
3629 chunk_appendf(&trash, "\n Dictionary cache not dumped (use \"show peers dict\")");
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003630 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003631 }
3632 }
3633
3634 end:
3635 chunk_appendf(&trash, "\n");
3636 if (ci_putchk(si_ic(si), msg) == -1) {
3637 si_rx_room_blk(si);
3638 return 0;
3639 }
3640
3641 return 1;
3642}
3643
3644/*
3645 * This function dumps all the peers of "peers" section.
3646 * Returns 0 if the output buffer is full and needs to be called
3647 * again, non-zero if not. It proceeds in an isolated thread, so
3648 * there is no thread safety issue here.
3649 */
3650static int cli_io_handler_show_peers(struct appctx *appctx)
3651{
3652 int show_all;
3653 int ret = 0, first_peers = 1;
3654 struct stream_interface *si = appctx->owner;
3655
3656 thread_isolate();
3657
3658 show_all = !appctx->ctx.cfgpeers.target;
3659
3660 chunk_reset(&trash);
3661
3662 while (appctx->st2 != STAT_ST_FIN) {
3663 switch (appctx->st2) {
3664 case STAT_ST_INIT:
3665 if (show_all)
3666 appctx->ctx.cfgpeers.peers = cfg_peers;
3667 else
3668 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3669
3670 appctx->st2 = STAT_ST_LIST;
3671 /* fall through */
3672
3673 case STAT_ST_LIST:
3674 if (!appctx->ctx.cfgpeers.peers) {
3675 /* No more peers list. */
3676 appctx->st2 = STAT_ST_END;
3677 }
3678 else {
3679 if (!first_peers)
3680 chunk_appendf(&trash, "\n");
3681 else
3682 first_peers = 0;
3683 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3684 goto out;
3685
3686 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3687 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3688 appctx->st2 = STAT_ST_INFO;
3689 }
3690 break;
3691
3692 case STAT_ST_INFO:
3693 if (!appctx->ctx.cfgpeers.peer) {
3694 /* End of peer list */
3695 if (show_all)
3696 appctx->st2 = STAT_ST_LIST;
3697 else
3698 appctx->st2 = STAT_ST_END;
3699 }
3700 else {
Willy Tarreau49962b52021-02-12 16:56:22 +01003701 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer, appctx->ctx.cfgpeers.flags))
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003702 goto out;
3703
3704 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3705 }
3706 break;
3707
3708 case STAT_ST_END:
3709 appctx->st2 = STAT_ST_FIN;
3710 break;
3711 }
3712 }
3713 ret = 1;
3714 out:
3715 thread_release();
3716 return ret;
3717}
3718
3719/*
3720 * CLI keywords.
3721 */
3722static struct cli_kw_list cli_kws = {{ }, {
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003723 { { "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 +02003724 {},
3725}};
3726
3727/* Register cli keywords */
3728INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3729