blob: 425949039ef7182f7cf0a93183f57ed773f4e56e [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
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020027#include <common/hathreads.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020028
29#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <types/listener.h>
31#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020033#include <types/stats.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020034
35#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020036#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020037#include <proto/channel.h>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020038#include <proto/cli.h>
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +020039#include <proto/dict.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020040#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010041#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020042#include <proto/log.h>
43#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020044#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020045#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020046#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020047#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020048#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010049#include <proto/signal.h>
50#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020051#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020052#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020053
54
55/*******************************/
56/* Current peer learning state */
57/*******************************/
58
59/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020060/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020061/******************************/
Emeric Brunf8716e82021-04-28 12:59:35 +020062#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
63#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
64#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
65#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
66#define PEERS_F_RESYNC_LOCALTIMEOUT 0x00000010 /* Timeout waiting for a full resync from a local node */
67#define PEERS_F_RESYNC_REMOTETIMEOUT 0x00000020 /* Timeout waiting for a full resync from a remote node */
68#define PEERS_F_RESYNC_LOCALABORT 0x00000040 /* Session aborted learning from a local node */
69#define PEERS_F_RESYNC_REMOTEABORT 0x00000080 /* Session aborted learning from a remote node */
70#define PEERS_F_RESYNC_LOCALFINISHED 0x00000100 /* A local node teach us and was fully up to date */
71#define PEERS_F_RESYNC_REMOTEFINISHED 0x00000200 /* A remote node teach us and was fully up to date */
72#define PEERS_F_RESYNC_LOCALPARTIAL 0x00000400 /* A local node teach us but was partially up to date */
73#define PEERS_F_RESYNC_REMOTEPARTIAL 0x00000800 /* A remote node teach us but was partially up to date */
74#define PEERS_F_RESYNC_LOCALASSIGN 0x00001000 /* A local node was assigned for a full resync */
75#define PEERS_F_RESYNC_REMOTEASSIGN 0x00002000 /* A remote node was assigned for a full resync */
76#define PEERS_F_RESYNC_REQUESTED 0x00004000 /* A resync was explicitly requested */
77#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
78 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020079
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010080#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
81#define PEERS_RESYNC_FROMLOCAL 0x00000000
82#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
83#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020084
85/***********************************/
86/* Current shared table sync state */
87/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010088#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
89#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020090
91/******************************/
92/* Remote peer teaching state */
93/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010094#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
95#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
96#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
97#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
98#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
99#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
100#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
101#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 +0200102
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100103#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
104#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +0200105
Frédéric Lécaille54bff832019-03-26 10:25:20 +0100106#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
107#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100108#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
109
Emeric Brunb3971ab2015-05-12 18:49:09 +0200110/*****************************/
111/* Sync message class */
112/*****************************/
113enum {
114 PEER_MSG_CLASS_CONTROL = 0,
115 PEER_MSG_CLASS_ERROR,
116 PEER_MSG_CLASS_STICKTABLE = 10,
117 PEER_MSG_CLASS_RESERVED = 255,
118};
119
120/*****************************/
121/* control message types */
122/*****************************/
123enum {
124 PEER_MSG_CTRL_RESYNCREQ = 0,
125 PEER_MSG_CTRL_RESYNCFINISHED,
126 PEER_MSG_CTRL_RESYNCPARTIAL,
127 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100128 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200129};
130
131/*****************************/
132/* error message types */
133/*****************************/
134enum {
135 PEER_MSG_ERR_PROTOCOL = 0,
136 PEER_MSG_ERR_SIZELIMIT,
137};
138
Emeric Brunc329cd02020-06-02 11:17:42 +0200139/* network key types;
140 * network types were directly and mistakenly
141 * mapped on sample types, to keep backward
142 * compatiblitiy we keep those values but
143 * we now use a internal/network mapping
144 * to avoid further mistakes adding or
145 * modifying internals types
146 */
147enum {
148 PEER_KT_ANY = 0, /* any type */
149 PEER_KT_RESV1, /* UNUSED */
150 PEER_KT_SINT, /* signed 64bits integer type */
151 PEER_KT_RESV3, /* UNUSED */
152 PEER_KT_IPV4, /* ipv4 type */
153 PEER_KT_IPV6, /* ipv6 type */
154 PEER_KT_STR, /* char string type */
155 PEER_KT_BIN, /* buffer type */
156 PEER_KT_TYPES /* number of types, must always be last */
157};
158
159/* Map used to retrieve network type from internal type
160 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
161 */
162static int peer_net_key_type[SMP_TYPES] = {
163 [SMP_T_SINT] = PEER_KT_SINT,
164 [SMP_T_IPV4] = PEER_KT_IPV4,
165 [SMP_T_IPV6] = PEER_KT_IPV6,
166 [SMP_T_STR] = PEER_KT_STR,
167 [SMP_T_BIN] = PEER_KT_BIN,
168};
169
170/* Map used to retrieve internal type from external type
171 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
172 */
173static int peer_int_key_type[PEER_KT_TYPES] = {
174 [PEER_KT_SINT] = SMP_T_SINT,
175 [PEER_KT_IPV4] = SMP_T_IPV4,
176 [PEER_KT_IPV6] = SMP_T_IPV6,
177 [PEER_KT_STR] = SMP_T_STR,
178 [PEER_KT_BIN] = SMP_T_BIN,
179};
180
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100181/*
182 * Parameters used by functions to build peer protocol messages. */
183struct peer_prep_params {
184 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100185 struct peer *peer;
186 } hello;
187 struct {
188 unsigned int st1;
189 } error_status;
190 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100191 struct stksess *stksess;
192 struct shared_table *shared_table;
193 unsigned int updateid;
194 int use_identifier;
195 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200196 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100197 } updt;
198 struct {
199 struct shared_table *shared_table;
200 } swtch;
201 struct {
202 struct shared_table *shared_table;
203 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100204 struct {
205 unsigned char head[2];
206 } control;
207 struct {
208 unsigned char head[2];
209 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100210};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200211
212/*******************************/
213/* stick table sync mesg types */
214/* Note: ids >= 128 contains */
215/* id message cotains data */
216/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200217#define PEER_MSG_STKT_UPDATE 0x80
218#define PEER_MSG_STKT_INCUPDATE 0x81
219#define PEER_MSG_STKT_DEFINE 0x82
220#define PEER_MSG_STKT_SWITCH 0x83
221#define PEER_MSG_STKT_ACK 0x84
222#define PEER_MSG_STKT_UPDATE_TIMED 0x85
223#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200224/* All the stick-table message identifiers abova have the #7 bit set */
225#define PEER_MSG_STKT_BIT 7
226#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200227
Frédéric Lécaille39143342019-05-24 14:32:27 +0200228/* The maximum length of an encoded data length. */
229#define PEER_MSG_ENC_LENGTH_MAXLEN 5
230
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200231/* Minimum 64-bits value encoded with 2 bytes */
232#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
233/* 3 bytes */
234#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
235/* 4 bytes */
236#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
237/* 5 bytes */
238#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
239/* 6 bytes */
240#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
241/* 7 bytes */
242#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
243/* 8 bytes */
244#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
245/* 9 bytes */
246#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
247/* 10 bytes */
248#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
249
250/* #7 bit used to detect the last byte to be encoded */
251#define PEER_ENC_STOP_BIT 7
252/* The byte minimum value with #7 bit set */
253#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
254/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
255#define PEER_ENC_2BYTES_MIN_BITS 4
256
Frédéric Lécaille39143342019-05-24 14:32:27 +0200257#define PEER_MSG_HEADER_LEN 2
258
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200259#define PEER_STKT_CACHE_MAX_ENTRIES 128
260
Emeric Brun2b920a12010-09-23 18:30:22 +0200261/**********************************/
262/* Peer Session IO handler states */
263/**********************************/
264
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100265enum {
266 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
267 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
268 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
269 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100270 /* after this point, data were possibly exchanged */
271 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
272 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
273 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
274 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
275 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200276 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
277 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100278 PEER_SESS_ST_END, /* Killed session */
279};
Emeric Brun2b920a12010-09-23 18:30:22 +0200280
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100281/***************************************************/
282/* Peer Session status code - part of the protocol */
283/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200284
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100285#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
286#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200287
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100288#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200289
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100290#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200291
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100292#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
293#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
294#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
295#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200296
297#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200298#define PEER_MAJOR_VER 2
299#define PEER_MINOR_VER 1
300#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200301
Willy Tarreau6254a922019-01-29 17:45:23 +0100302static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200303struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200304static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200305
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200306static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
307 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200308static inline void flush_dcache(struct peer *peer);
309
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200310static const char *statuscode_str(int statuscode)
311{
312 switch (statuscode) {
313 case PEER_SESS_SC_CONNECTCODE:
314 return "CONN";
315 case PEER_SESS_SC_CONNECTEDCODE:
316 return "HSHK";
317 case PEER_SESS_SC_SUCCESSCODE:
318 return "ESTA";
319 case PEER_SESS_SC_TRYAGAIN:
320 return "RETR";
321 case PEER_SESS_SC_ERRPROTO:
322 return "PROT";
323 case PEER_SESS_SC_ERRVERSION:
324 return "VERS";
325 case PEER_SESS_SC_ERRHOST:
326 return "NAME";
327 case PEER_SESS_SC_ERRPEER:
328 return "UNKN";
329 default:
330 return "NONE";
331 }
332}
333
Emeric Brun18928af2017-03-29 16:32:53 +0200334/* This function encode an uint64 to 'dynamic' length format.
335 The encoded value is written at address *str, and the
336 caller must assure that size after *str is large enought.
337 At return, the *str is set at the next Byte after then
338 encoded integer. The function returns then length of the
339 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200340int intencode(uint64_t i, char **str) {
341 int idx = 0;
342 unsigned char *msg;
343
Emeric Brunb3971ab2015-05-12 18:49:09 +0200344 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200345 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200346 msg[0] = (unsigned char)i;
347 *str = (char *)&msg[idx+1];
348 return (idx+1);
349 }
350
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200351 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
352 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
353 while (i >= PEER_ENC_STOP_BYTE) {
354 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
355 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200356 }
357 msg[++idx] = (unsigned char)i;
358 *str = (char *)&msg[idx+1];
359 return (idx+1);
360}
361
362
363/* This function returns the decoded integer or 0
364 if decode failed
365 *str point on the beginning of the integer to decode
366 at the end of decoding *str point on the end of the
367 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200368uint64_t intdecode(char **str, char *end)
369{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200370 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200371 uint64_t i;
372 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200373
374 if (!*str)
375 return 0;
376
377 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200378 if (msg >= (unsigned char *)end)
379 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200380
Emeric Brun18928af2017-03-29 16:32:53 +0200381 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200382 if (i >= PEER_ENC_2BYTES_MIN) {
383 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200384 do {
385 if (msg >= (unsigned char *)end)
386 goto fail;
387 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200388 shift += PEER_ENC_STOP_BIT;
389 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200390 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100391 *str = (char *)msg;
392 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200393
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100394 fail:
395 *str = NULL;
396 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200397}
Emeric Brun2b920a12010-09-23 18:30:22 +0200398
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100399/*
400 * Build a "hello" peer protocol message.
401 * Return the number of written bytes written to build this messages if succeeded,
402 * 0 if not.
403 */
404static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
405{
406 int min_ver, ret;
407 struct peer *peer;
408
409 peer = p->hello.peer;
410 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
411 /* Prepare headers */
412 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
413 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
414 if (ret >= size)
415 return 0;
416
417 return ret;
418}
419
420/*
421 * Build a "handshake succeeded" status message.
422 * Return the number of written bytes written to build this messages if succeeded,
423 * 0 if not.
424 */
425static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
426{
427 int ret;
428
429 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
430 if (ret >= size)
431 return 0;
432
433 return ret;
434}
435
436/*
437 * Build an error status message.
438 * Return the number of written bytes written to build this messages if succeeded,
439 * 0 if not.
440 */
441static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
442{
443 int ret;
444 unsigned int st1;
445
446 st1 = p->error_status.st1;
447 ret = snprintf(msg, size, "%d\n", st1);
448 if (ret >= size)
449 return 0;
450
451 return ret;
452}
453
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200454/* Set the stick-table UPDATE message type byte at <msg_type> address,
455 * depending on <use_identifier> and <use_timed> boolean parameters.
456 * Always successful.
457 */
458static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
459{
460 if (use_timed) {
461 if (use_identifier)
462 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
463 else
464 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
465 }
466 else {
467 if (use_identifier)
468 *msg_type = PEER_MSG_STKT_UPDATE;
469 else
470 *msg_type = PEER_MSG_STKT_INCUPDATE;
471 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200472}
Emeric Brun2b920a12010-09-23 18:30:22 +0200473/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200474 * This prepare the data update message on the stick session <ts>, <st> is the considered
475 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800476 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200477 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
478 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200479 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100480static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200481{
482 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200483 unsigned short datalen;
484 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200485 unsigned int data_type;
486 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100487 struct stksess *ts;
488 struct shared_table *st;
489 unsigned int updateid;
490 int use_identifier;
491 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200492 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100493
494 ts = p->updt.stksess;
495 st = p->updt.shared_table;
496 updateid = p->updt.updateid;
497 use_identifier = p->updt.use_identifier;
498 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200499 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200500
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200501 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200502
Emeric Brun2b920a12010-09-23 18:30:22 +0200503 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200504
505 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200506 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200507 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200508 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200509
510 /* encode update identifier if needed */
511 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200512 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200513 memcpy(cursor, &netinteger, sizeof(netinteger));
514 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200515 }
516
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200517 if (use_timed) {
518 netinteger = htonl(tick_remain(now_ms, ts->expire));
519 memcpy(cursor, &netinteger, sizeof(netinteger));
520 cursor += sizeof(netinteger);
521 }
522
Emeric Brunb3971ab2015-05-12 18:49:09 +0200523 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200524 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200525 int stlen = strlen((char *)ts->key.key);
526
Emeric Brunb3971ab2015-05-12 18:49:09 +0200527 intencode(stlen, &cursor);
528 memcpy(cursor, ts->key.key, stlen);
529 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200530 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200531 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200532 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200533 memcpy(cursor, &netinteger, sizeof(netinteger));
534 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200535 }
536 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200537 memcpy(cursor, ts->key.key, st->table->key_size);
538 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200539 }
540
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100541 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200542 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200543 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200544
Emeric Brun94900952015-06-11 18:25:54 +0200545 data_ptr = stktable_data_ptr(st->table, ts, data_type);
546 if (data_ptr) {
547 switch (stktable_data_types[data_type].std_type) {
548 case STD_T_SINT: {
549 int data;
550
551 data = stktable_data_cast(data_ptr, std_t_sint);
552 intencode(data, &cursor);
553 break;
554 }
555 case STD_T_UINT: {
556 unsigned int data;
557
558 data = stktable_data_cast(data_ptr, std_t_uint);
559 intencode(data, &cursor);
560 break;
561 }
562 case STD_T_ULL: {
563 unsigned long long data;
564
565 data = stktable_data_cast(data_ptr, std_t_ull);
566 intencode(data, &cursor);
567 break;
568 }
569 case STD_T_FRQP: {
570 struct freq_ctr_period *frqp;
571
572 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
573 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
574 intencode(frqp->curr_ctr, &cursor);
575 intencode(frqp->prev_ctr, &cursor);
576 break;
577 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200578 case STD_T_DICT: {
579 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200580 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200581 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200582 char *beg, *end;
583 size_t value_len, data_len;
584 struct dcache *dc;
585
586 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailled9203ca2019-11-13 17:50:34 +0100587 if (!de) {
588 /* No entry */
589 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200590 break;
Frédéric Lécailled9203ca2019-11-13 17:50:34 +0100591 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200592
593 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200594 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200595 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200596 if (cached_de == &cde.entry) {
597 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
598 break;
599 /* Encode the length of the remaining data -> 1 */
600 intencode(1, &cursor);
601 /* Encode the cache entry ID */
602 intencode(cde.id + 1, &cursor);
603 }
604 else {
605 /* Leave enough room to encode the remaining data length. */
606 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
607 /* Encode the dictionary entry key */
608 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200609 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200610 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200611 intencode(value_len, &end);
612 /* Copy the data */
613 memcpy(end, de->value.key, value_len);
614 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200615 /* Encode the length of the data */
616 data_len = end - beg;
617 intencode(data_len, &cursor);
618 memmove(cursor, beg, data_len);
619 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200620 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200621 break;
622 }
Emeric Brun94900952015-06-11 18:25:54 +0200623 }
624 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200625 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100626 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200627
628 /* Compute datalen */
629 datalen = (cursor - datamsg);
630
631 /* prepare message header */
632 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200633 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200634 cursor = &msg[2];
635 intencode(datalen, &cursor);
636
637 /* move data after header */
638 memmove(cursor, datamsg, datalen);
639
640 /* return header size + data_len */
641 return (cursor - msg) + datalen;
642}
643
644/*
645 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800646 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200647 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
648 * check size)
649 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100650static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200651{
652 int len;
653 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200654 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200655 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200656 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200657 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100658 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200659
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100660 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200661 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200662
663 /* Encode data */
664
665 /* encode local id */
666 intencode(st->local_id, &cursor);
667
668 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100669 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200670 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100671 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200672 cursor += len;
673
674 /* encode table type */
675
Emeric Brunc329cd02020-06-02 11:17:42 +0200676 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200677
678 /* encode table key size */
679 intencode(st->table->key_size, &cursor);
680
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200681 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200682 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200683 /* encode available known data types in table */
684 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
685 if (st->table->data_ofs[data_type]) {
686 switch (stktable_data_types[data_type].std_type) {
687 case STD_T_SINT:
688 case STD_T_UINT:
689 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200690 case STD_T_DICT:
Emeric Bruncb4d8c02021-07-01 18:54:05 +0200691 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200692 break;
Emeric Brun94900952015-06-11 18:25:54 +0200693 case STD_T_FRQP:
Emeric Bruncb4d8c02021-07-01 18:54:05 +0200694 data |= 1ULL << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200695 intencode(data_type, &chunkq);
696 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200697 break;
698 }
699 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200700 }
701 intencode(data, &cursor);
702
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200703 /* Encode stick-table entries duration. */
704 intencode(st->table->expire, &cursor);
705
706 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200707 chunk->data = chunkq - chunkp;
708 memcpy(cursor, chunk->area, chunk->data);
709 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200710 }
711
Emeric Brunb3971ab2015-05-12 18:49:09 +0200712 /* Compute datalen */
713 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200714
Emeric Brunb3971ab2015-05-12 18:49:09 +0200715 /* prepare message header */
716 msg[0] = PEER_MSG_CLASS_STICKTABLE;
717 msg[1] = PEER_MSG_STKT_DEFINE;
718 cursor = &msg[2];
719 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200720
Emeric Brunb3971ab2015-05-12 18:49:09 +0200721 /* move data after header */
722 memmove(cursor, datamsg, datalen);
723
724 /* return header size + data_len */
725 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200726}
727
Emeric Brunb3971ab2015-05-12 18:49:09 +0200728/*
729 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
730 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800731 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200732 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
733 * check size)
734 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100735static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200736{
737 unsigned short datalen;
738 char *cursor, *datamsg;
739 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100740 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200741
Frédéric Lécaille39143342019-05-24 14:32:27 +0200742 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200743
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100744 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200745 intencode(st->remote_id, &cursor);
746 netinteger = htonl(st->last_get);
747 memcpy(cursor, &netinteger, sizeof(netinteger));
748 cursor += sizeof(netinteger);
749
750 /* Compute datalen */
751 datalen = (cursor - datamsg);
752
753 /* prepare message header */
754 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200755 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200756 cursor = &msg[2];
757 intencode(datalen, &cursor);
758
759 /* move data after header */
760 memmove(cursor, datamsg, datalen);
761
762 /* return header size + data_len */
763 return (cursor - msg) + datalen;
764}
Emeric Brun2b920a12010-09-23 18:30:22 +0200765
766/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200767 * Function to deinit connected peer
768 */
769void __peer_session_deinit(struct peer *peer)
770{
771 struct stream_interface *si;
772 struct stream *s;
773 struct peers *peers;
774
775 if (!peer->appctx)
776 return;
777
778 si = peer->appctx->owner;
779 if (!si)
780 return;
781
782 s = si_strm(si);
783 if (!s)
784 return;
785
786 peers = strm_fe(s)->parent;
787 if (!peers)
788 return;
789
790 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
791 HA_ATOMIC_SUB(&connected_peers, 1);
792
793 HA_ATOMIC_SUB(&active_peers, 1);
794
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200795 flush_dcache(peer);
796
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200797 /* Re-init current table pointers to force announcement on re-connect */
798 peer->remote_table = peer->last_local_table = NULL;
799 peer->appctx = NULL;
800 if (peer->flags & PEER_F_LEARN_ASSIGN) {
801 /* unassign current peer for learning */
802 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
803 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
804
Emeric Brunf8716e82021-04-28 12:59:35 +0200805 if (peer->local)
806 peers->flags |= PEERS_F_RESYNC_LOCALABORT;
807 else
808 peers->flags |= PEERS_F_RESYNC_REMOTEABORT;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200809 /* reschedule a resync */
810 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
811 }
812 /* reset teaching and learning flags to 0 */
813 peer->flags &= PEER_TEACH_RESET;
814 peer->flags &= PEER_LEARN_RESET;
815 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
816}
817
818/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200819 * Callback to release a session with a peer
820 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200821static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200822{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200823 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200824
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100825 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100826 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200827 return;
828
829 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200830 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100831 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200832 if (peer->appctx == appctx)
833 __peer_session_deinit(peer);
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +0200834 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100835 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200836 }
837}
838
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200839/* Retrieve the major and minor versions of peers protocol
840 * announced by a remote peer. <str> is a null-terminated
841 * string with the following format: "<maj_ver>.<min_ver>".
842 */
843static int peer_get_version(const char *str,
844 unsigned int *maj_ver, unsigned int *min_ver)
845{
846 unsigned int majv, minv;
847 const char *pos, *saved;
848 const char *end;
849
850 saved = pos = str;
851 end = str + strlen(str);
852
853 majv = read_uint(&pos, end);
854 if (saved == pos || *pos++ != '.')
855 return -1;
856
857 saved = pos;
858 minv = read_uint(&pos, end);
859 if (saved == pos || pos != end)
860 return -1;
861
862 *maj_ver = majv;
863 *min_ver = minv;
864
865 return 0;
866}
Emeric Brun2b920a12010-09-23 18:30:22 +0200867
868/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100869 * Parse a line terminated by an optional '\r' character, followed by a mandatory
870 * '\n' character.
871 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
872 * a line could not be read because the communication channel is closed.
873 */
874static inline int peer_getline(struct appctx *appctx)
875{
876 int n;
877 struct stream_interface *si = appctx->owner;
878
879 n = co_getline(si_oc(si), trash.area, trash.size);
880 if (!n)
881 return 0;
882
883 if (n < 0 || trash.area[n - 1] != '\n') {
884 appctx->st0 = PEER_SESS_ST_END;
885 return -1;
886 }
887
888 if (n > 1 && (trash.area[n - 2] == '\r'))
889 trash.area[n - 2] = 0;
890 else
891 trash.area[n - 1] = 0;
892
893 co_skip(si_oc(si), n);
894
895 return n;
896}
897
898/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100899 * Send a message after having called <peer_prepare_msg> to build it.
900 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
901 * Returns -1 if there was not enough room left to send the message,
902 * any other negative returned value must be considered as an error with an appcxt st0
903 * returned value equal to PEER_SESS_ST_END.
904 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100905static inline int peer_send_msg(struct appctx *appctx,
906 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
907 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100908{
909 int ret, msglen;
910 struct stream_interface *si = appctx->owner;
911
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100912 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100913 if (!msglen) {
914 /* internal error: message does not fit in trash */
915 appctx->st0 = PEER_SESS_ST_END;
916 return 0;
917 }
918
919 /* message to buffer */
920 ret = ci_putblk(si_ic(si), trash.area, msglen);
921 if (ret <= 0) {
922 if (ret == -1) {
923 /* No more write possible */
924 si_rx_room_blk(si);
925 return -1;
926 }
927 appctx->st0 = PEER_SESS_ST_END;
928 }
929
930 return ret;
931}
932
933/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100934 * Send a hello message.
935 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
936 * Returns -1 if there was not enough room left to send the message,
937 * any other negative returned value must be considered as an error with an appcxt st0
938 * returned value equal to PEER_SESS_ST_END.
939 */
940static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
941{
942 struct peer_prep_params p = {
943 .hello.peer = peer,
944 };
945
946 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
947}
948
949/*
950 * Send a success peer handshake status message.
951 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
952 * Returns -1 if there was not enough room left to send the message,
953 * any other negative returned value must be considered as an error with an appcxt st0
954 * returned value equal to PEER_SESS_ST_END.
955 */
956static inline int peer_send_status_successmsg(struct appctx *appctx)
957{
958 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
959}
960
961/*
962 * Send a peer handshake status error message.
963 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
964 * Returns -1 if there was not enough room left to send the message,
965 * any other negative returned value must be considered as an error with an appcxt st0
966 * returned value equal to PEER_SESS_ST_END.
967 */
968static inline int peer_send_status_errormsg(struct appctx *appctx)
969{
970 struct peer_prep_params p = {
971 .error_status.st1 = appctx->st1,
972 };
973
974 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
975}
976
977/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100978 * Send a stick-table switch message.
979 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
980 * Returns -1 if there was not enough room left to send the message,
981 * any other negative returned value must be considered as an error with an appcxt st0
982 * returned value equal to PEER_SESS_ST_END.
983 */
984static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
985{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100986 struct peer_prep_params p = {
987 .swtch.shared_table = st,
988 };
989
990 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100991}
992
993/*
994 * Send a stick-table update acknowledgement message.
995 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
996 * Returns -1 if there was not enough room left to send the message,
997 * any other negative returned value must be considered as an error with an appcxt st0
998 * returned value equal to PEER_SESS_ST_END.
999 */
1000static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
1001{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001002 struct peer_prep_params p = {
1003 .ack.shared_table = st,
1004 };
1005
1006 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001007}
1008
1009/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001010 * Send a stick-table update message.
1011 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1012 * Returns -1 if there was not enough room left to send the message,
1013 * any other negative returned value must be considered as an error with an appcxt st0
1014 * returned value equal to PEER_SESS_ST_END.
1015 */
1016static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1017 unsigned int updateid, int use_identifier, int use_timed)
1018{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001019 struct peer_prep_params p = {
1020 .updt.stksess = ts,
1021 .updt.shared_table = st,
1022 .updt.updateid = updateid,
1023 .updt.use_identifier = use_identifier,
1024 .updt.use_timed = use_timed,
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001025 .updt.peer = appctx->ctx.peers.ptr,
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001026 };
1027
1028 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001029}
1030
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001031/*
1032 * Build a peer protocol control class message.
1033 * Returns the number of written bytes used to build the message if succeeded,
1034 * 0 if not.
1035 */
1036static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1037{
1038 if (size < sizeof p->control.head)
1039 return 0;
1040
1041 msg[0] = p->control.head[0];
1042 msg[1] = p->control.head[1];
1043
1044 return 2;
1045}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001046
1047/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001048 * Send a stick-table synchronization request message.
1049 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1050 * Returns -1 if there was not enough room left to send the message,
1051 * any other negative returned value must be considered as an error with an appctx st0
1052 * returned value equal to PEER_SESS_ST_END.
1053 */
1054static inline int peer_send_resync_reqmsg(struct appctx *appctx)
1055{
1056 struct peer_prep_params p = {
1057 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1058 };
1059
1060 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1061}
1062
1063/*
1064 * Send a stick-table synchronization confirmation message.
1065 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1066 * Returns -1 if there was not enough room left to send the message,
1067 * any other negative returned value must be considered as an error with an appctx st0
1068 * returned value equal to PEER_SESS_ST_END.
1069 */
1070static inline int peer_send_resync_confirmsg(struct appctx *appctx)
1071{
1072 struct peer_prep_params p = {
1073 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1074 };
1075
1076 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1077}
1078
1079/*
1080 * Send a stick-table synchronization finished message.
1081 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1082 * Returns -1 if there was not enough room left to send the message,
1083 * any other negative returned value must be considered as an error with an appctx st0
1084 * returned value equal to PEER_SESS_ST_END.
1085 */
Emeric Brundeda2a62020-03-16 10:51:01 +01001086static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001087{
1088 struct peer_prep_params p = {
1089 .control.head = { PEER_MSG_CLASS_CONTROL, },
1090 };
1091
Emeric Brundeda2a62020-03-16 10:51:01 +01001092 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001093 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1094
1095 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1096}
1097
1098/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001099 * Send a heartbeat message.
1100 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1101 * Returns -1 if there was not enough room left to send the message,
1102 * any other negative returned value must be considered as an error with an appctx st0
1103 * returned value equal to PEER_SESS_ST_END.
1104 */
1105static inline int peer_send_heartbeatmsg(struct appctx *appctx)
1106{
1107 struct peer_prep_params p = {
1108 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1109 };
1110
1111 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1112}
1113
1114/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001115 * Build a peer protocol error class message.
1116 * Returns the number of written bytes used to build the message if succeeded,
1117 * 0 if not.
1118 */
1119static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1120{
1121 if (size < sizeof p->error.head)
1122 return 0;
1123
1124 msg[0] = p->error.head[0];
1125 msg[1] = p->error.head[1];
1126
1127 return 2;
1128}
1129
1130/*
1131 * Send a "size limit reached" error message.
1132 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1133 * Returns -1 if there was not enough room left to send the message,
1134 * any other negative returned value must be considered as an error with an appctx st0
1135 * returned value equal to PEER_SESS_ST_END.
1136 */
1137static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1138{
1139 struct peer_prep_params p = {
1140 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1141 };
1142
1143 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1144}
1145
1146/*
1147 * Send a "peer protocol" error message.
1148 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1149 * Returns -1 if there was not enough room left to send the message,
1150 * any other negative returned value must be considered as an error with an appctx st0
1151 * returned value equal to PEER_SESS_ST_END.
1152 */
1153static inline int peer_send_error_protomsg(struct appctx *appctx)
1154{
1155 struct peer_prep_params p = {
1156 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1157 };
1158
1159 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1160}
1161
1162/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001163 * Function used to lookup for recent stick-table updates associated with
1164 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1165 */
1166static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1167{
1168 struct eb32_node *eb;
1169
1170 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1171 if (!eb) {
1172 eb = eb32_first(&st->table->updates);
Emeric Brun129d8fa2021-04-28 11:48:15 +02001173 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001174 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1175 return NULL;
1176 }
1177 }
1178
Emeric Brun129d8fa2021-04-28 11:48:15 +02001179 /* if distance between the last pushed and the retrieved key
1180 * is greater than the distance last_pushed and the local_update
1181 * this means we are beyond localupdate.
1182 */
1183 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001184 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1185 return NULL;
1186 }
1187
1188 return eb32_entry(eb, struct stksess, upd);
1189}
1190
1191/*
1192 * Function used to lookup for recent stick-table updates associated with
1193 * <st> shared stick-table during teach state 1 step.
1194 */
1195static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1196{
1197 struct eb32_node *eb;
1198
1199 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1200 if (!eb) {
1201 st->flags |= SHTABLE_F_TEACH_STAGE1;
1202 eb = eb32_first(&st->table->updates);
1203 if (eb)
1204 st->last_pushed = eb->key - 1;
1205 return NULL;
1206 }
1207
1208 return eb32_entry(eb, struct stksess, upd);
1209}
1210
1211/*
1212 * Function used to lookup for recent stick-table updates associated with
1213 * <st> shared stick-table during teach state 2 step.
1214 */
1215static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1216{
1217 struct eb32_node *eb;
1218
1219 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1220 if (!eb || eb->key > st->teaching_origin) {
1221 st->flags |= SHTABLE_F_TEACH_STAGE2;
1222 return NULL;
1223 }
1224
1225 return eb32_entry(eb, struct stksess, upd);
1226}
1227
1228/*
1229 * Generic function to emit update messages for <st> stick-table when a lesson must
1230 * be taught to the peer <p>.
1231 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1232 * this function, 0 if not.
1233 *
1234 * This function temporary unlock/lock <st> when it sends stick-table updates or
1235 * when decrementing its refcount in case of any error when it sends this updates.
1236 *
1237 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1238 * Returns -1 if there was not enough room left to send the message,
1239 * any other negative returned value must be considered as an error with an appcxt st0
1240 * returned value equal to PEER_SESS_ST_END.
1241 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1242 * unlocked if not already locked when entering this function.
1243 */
1244static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1245 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1246 struct shared_table *st, int locked)
1247{
1248 int ret, new_pushed, use_timed;
1249
1250 ret = 1;
1251 use_timed = 0;
1252 if (st != p->last_local_table) {
1253 ret = peer_send_switchmsg(st, appctx);
1254 if (ret <= 0)
1255 return ret;
1256
1257 p->last_local_table = st;
1258 }
1259
1260 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1261 use_timed = !(p->flags & PEER_F_DWNGRD);
1262
1263 /* We force new pushed to 1 to force identifier in update message */
1264 new_pushed = 1;
1265
1266 if (!locked)
1267 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1268
1269 while (1) {
1270 struct stksess *ts;
1271 unsigned updateid;
1272
1273 /* push local updates */
1274 ts = peer_stksess_lookup(st);
1275 if (!ts)
1276 break;
1277
1278 updateid = ts->upd.key;
1279 ts->ref_cnt++;
1280 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1281
1282 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1283 if (ret <= 0) {
1284 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1285 ts->ref_cnt--;
1286 if (!locked)
1287 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1288 return ret;
1289 }
1290
1291 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1292 ts->ref_cnt--;
1293 st->last_pushed = updateid;
1294
1295 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1296 (int)(st->last_pushed - st->table->commitupdate) > 0)
1297 st->table->commitupdate = st->last_pushed;
1298
1299 /* identifier may not needed in next update message */
1300 new_pushed = 0;
1301 }
1302
1303 out:
1304 if (!locked)
1305 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1306 return 1;
1307}
1308
1309/*
1310 * Function to emit update messages for <st> stick-table when a lesson must
1311 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1312 *
1313 * Note that <st> shared stick-table is locked when calling this function.
1314 *
1315 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1316 * Returns -1 if there was not enough room left to send the message,
1317 * any other negative returned value must be considered as an error with an appcxt st0
1318 * returned value equal to PEER_SESS_ST_END.
1319 */
1320static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1321 struct shared_table *st)
1322{
1323 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1324}
1325
1326/*
1327 * Function to emit update messages for <st> stick-table when a lesson must
1328 * be taught to the peer <p> during teach state 1 step.
1329 *
1330 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1331 * Returns -1 if there was not enough room left to send the message,
1332 * any other negative returned value must be considered as an error with an appcxt st0
1333 * returned value equal to PEER_SESS_ST_END.
1334 */
1335static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1336 struct shared_table *st)
1337{
1338 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1339}
1340
1341/*
1342 * Function to emit update messages for <st> stick-table when a lesson must
1343 * be taught to the peer <p> during teach state 1 step.
1344 *
1345 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1346 * Returns -1 if there was not enough room left to send the message,
1347 * any other negative returned value must be considered as an error with an appcxt st0
1348 * returned value equal to PEER_SESS_ST_END.
1349 */
1350static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1351 struct shared_table *st)
1352{
1353 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1354}
1355
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001356
1357/*
1358 * Function used to parse a stick-table update message after it has been received
1359 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1360 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1361 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1362 * was encountered.
1363 * <exp> must be set if the stick-table entry expires.
1364 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1365 * messages, in this case the stick-table udpate message is received with a stick-table
1366 * update ID.
1367 * <totl> is the length of the stick-table update message computed upon receipt.
1368 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001369static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1370 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001371{
1372 struct stream_interface *si = appctx->owner;
1373 struct shared_table *st = p->remote_table;
1374 struct stksess *ts, *newts;
1375 uint32_t update;
1376 int expire;
1377 unsigned int data_type;
1378 void *data_ptr;
1379
1380 /* Here we have data message */
1381 if (!st)
1382 goto ignore_msg;
1383
1384 expire = MS_TO_TICKS(st->table->expire);
1385
1386 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001387 if (msg_len < sizeof(update))
1388 goto malformed_exit;
1389
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001390 memcpy(&update, *msg_cur, sizeof(update));
1391 *msg_cur += sizeof(update);
1392 st->last_get = htonl(update);
1393 }
1394 else {
1395 st->last_get++;
1396 }
1397
1398 if (exp) {
1399 size_t expire_sz = sizeof expire;
1400
Willy Tarreau1e82a142019-01-29 11:08:06 +01001401 if (*msg_cur + expire_sz > msg_end)
1402 goto malformed_exit;
1403
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001404 memcpy(&expire, *msg_cur, expire_sz);
1405 *msg_cur += expire_sz;
1406 expire = ntohl(expire);
1407 }
1408
1409 newts = stksess_new(st->table, NULL);
1410 if (!newts)
1411 goto ignore_msg;
1412
1413 if (st->table->type == SMP_T_STR) {
1414 unsigned int to_read, to_store;
1415
1416 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001417 if (!*msg_cur)
1418 goto malformed_free_newts;
1419
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001420 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001421 if (*msg_cur + to_store > msg_end)
1422 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001423
1424 memcpy(newts->key.key, *msg_cur, to_store);
1425 newts->key.key[to_store] = 0;
1426 *msg_cur += to_read;
1427 }
1428 else if (st->table->type == SMP_T_SINT) {
1429 unsigned int netinteger;
1430
Willy Tarreau1e82a142019-01-29 11:08:06 +01001431 if (*msg_cur + sizeof(netinteger) > msg_end)
1432 goto malformed_free_newts;
1433
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001434 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1435 netinteger = ntohl(netinteger);
1436 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1437 *msg_cur += sizeof(netinteger);
1438 }
1439 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001440 if (*msg_cur + st->table->key_size > msg_end)
1441 goto malformed_free_newts;
1442
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001443 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1444 *msg_cur += st->table->key_size;
1445 }
1446
1447 /* lookup for existing entry */
1448 ts = stktable_set_entry(st->table, newts);
1449 if (ts != newts) {
1450 stksess_free(st->table, newts);
1451 newts = NULL;
1452 }
1453
1454 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1455
1456 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001457 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001458
Emeric Bruncb4d8c02021-07-01 18:54:05 +02001459 if (!((1ULL << data_type) & st->remote_data))
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001460 continue;
1461
Willy Tarreau1e82a142019-01-29 11:08:06 +01001462 decoded_int = intdecode(msg_cur, msg_end);
1463 if (!*msg_cur)
1464 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001465
Willy Tarreau1e82a142019-01-29 11:08:06 +01001466 switch (stktable_data_types[data_type].std_type) {
1467 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001468 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1469 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001470 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001471 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001472
Willy Tarreau1e82a142019-01-29 11:08:06 +01001473 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001474 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1475 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001476 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001477 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001478
Willy Tarreau1e82a142019-01-29 11:08:06 +01001479 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001480 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1481 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001482 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001483 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001484
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001485 case STD_T_FRQP: {
1486 struct freq_ctr_period data;
1487
1488 /* First bit is reserved for the freq_ctr_period lock
1489 Note: here we're still protected by the stksess lock
1490 so we don't need to update the update the freq_ctr_period
1491 using its internal lock */
1492
Willy Tarreau1e82a142019-01-29 11:08:06 +01001493 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001494 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001495 if (!*msg_cur)
1496 goto malformed_unlock;
1497
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001498 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001499 if (!*msg_cur)
1500 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001501
1502 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1503 if (data_ptr)
1504 stktable_data_cast(data_ptr, std_t_frqp) = data;
1505 break;
1506 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001507 case STD_T_DICT: {
1508 struct buffer *chunk;
1509 size_t data_len, value_len;
1510 unsigned int id;
1511 struct dict_entry *de;
1512 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001513 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001514
Frédéric Lécailled9203ca2019-11-13 17:50:34 +01001515 if (!decoded_int) {
1516 /* No entry. */
1517 break;
1518 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001519 data_len = decoded_int;
1520 if (*msg_cur + data_len > msg_end)
1521 goto malformed_unlock;
1522
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001523 /* Compute the end of the current data, <msg_end> being at the end of
1524 * the entire message.
1525 */
1526 end = *msg_cur + data_len;
1527 id = intdecode(msg_cur, end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001528 if (!*msg_cur || !id)
1529 goto malformed_unlock;
1530
1531 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001532 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001533 /* Dictionary entry key without value. */
Frédéric Lécaille506fe342020-11-12 19:53:11 +01001534 if (id > dc->max_entries) {
1535 goto malformed_unlock;
1536 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001537 /* IDs sent over the network are numbered from 1. */
1538 de = dc->rx[id - 1].de;
1539 }
1540 else {
1541 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001542 value_len = intdecode(msg_cur, end);
1543 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001544 unlikely(value_len + 1 >= chunk->size))
1545 goto malformed_unlock;
1546
1547 chunk_memcpy(chunk, *msg_cur, value_len);
1548 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001549 *msg_cur += value_len;
1550
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001551 de = dict_insert(&server_name_dict, chunk->area);
1552 dc->rx[id - 1].de = de;
1553 }
1554 if (de) {
1555 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1556 if (data_ptr)
1557 stktable_data_cast(data_ptr, std_t_dict) = de;
1558 }
1559 break;
1560 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001561 }
1562 }
1563 /* Force new expiration */
1564 ts->expire = tick_add(now_ms, expire);
1565
1566 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1567 stktable_touch_remote(st->table, ts, 1);
1568 return 1;
1569
1570 ignore_msg:
1571 /* skip consumed message */
1572 co_skip(si_oc(si), totl);
1573 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001574
1575 malformed_unlock:
1576 /* malformed message */
1577 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1578 stktable_touch_remote(st->table, ts, 1);
1579 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1580 return 0;
1581
1582 malformed_free_newts:
1583 /* malformed message */
1584 stksess_free(st->table, newts);
1585 malformed_exit:
1586 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1587 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001588}
1589
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001590/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001591 * Function used to parse a stick-table update acknowledgement message after it
1592 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1593 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1594 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1595 * was encountered.
1596 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1597 */
1598static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1599 char **msg_cur, char *msg_end)
1600{
1601 /* ack message */
1602 uint32_t table_id ;
1603 uint32_t update;
1604 struct shared_table *st;
1605
Emeric Brun5347ccd2021-03-04 10:27:10 +01001606 /* ignore ack during teaching process */
1607 if (p->flags & PEER_F_TEACH_PROCESS)
1608 return 1;
1609
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001610 table_id = intdecode(msg_cur, msg_end);
1611 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1612 /* malformed message */
1613 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1614 return 0;
1615 }
1616
1617 memcpy(&update, *msg_cur, sizeof(update));
1618 update = ntohl(update);
1619
1620 for (st = p->tables; st; st = st->next) {
1621 if (st->local_id == table_id) {
1622 st->update = update;
1623 break;
1624 }
1625 }
1626
1627 return 1;
1628}
1629
1630/*
1631 * Function used to parse a stick-table switch message after it has been received
1632 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1633 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1634 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1635 * was encountered.
1636 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1637 */
1638static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1639 char **msg_cur, char *msg_end)
1640{
1641 struct shared_table *st;
1642 int table_id;
1643
1644 table_id = intdecode(msg_cur, msg_end);
1645 if (!*msg_cur) {
1646 /* malformed message */
1647 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1648 return 0;
1649 }
1650
1651 p->remote_table = NULL;
1652 for (st = p->tables; st; st = st->next) {
1653 if (st->remote_id == table_id) {
1654 p->remote_table = st;
1655 break;
1656 }
1657 }
1658
1659 return 1;
1660}
1661
1662/*
1663 * Function used to parse a stick-table definition message after it has been received
1664 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1665 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1666 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1667 * was encountered.
1668 * <totl> is the length of the stick-table update message computed upon receipt.
1669 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1670 */
1671static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1672 char **msg_cur, char *msg_end, int totl)
1673{
1674 struct stream_interface *si = appctx->owner;
1675 int table_id_len;
1676 struct shared_table *st;
1677 int table_type;
1678 int table_keylen;
1679 int table_id;
1680 uint64_t table_data;
1681
1682 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001683 if (!*msg_cur)
1684 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001685
1686 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001687 if (!*msg_cur)
1688 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001689
1690 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001691 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1692 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001693
1694 for (st = p->tables; st; st = st->next) {
1695 /* Reset IDs */
1696 if (st->remote_id == table_id)
1697 st->remote_id = 0;
1698
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001699 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1700 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001701 p->remote_table = st;
1702 }
1703
1704 if (!p->remote_table)
1705 goto ignore_msg;
1706
1707 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001708 if (*msg_cur >= msg_end)
1709 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001710
1711 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001712 if (!*msg_cur)
1713 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001714
1715 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001716 if (!*msg_cur)
1717 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001718
1719 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001720 if (!*msg_cur)
1721 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001722
Emeric Brunc329cd02020-06-02 11:17:42 +02001723 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001724 || p->remote_table->table->key_size != table_keylen) {
1725 p->remote_table = NULL;
1726 goto ignore_msg;
1727 }
1728
1729 p->remote_table->remote_data = table_data;
1730 p->remote_table->remote_id = table_id;
1731 return 1;
1732
1733 ignore_msg:
1734 co_skip(si_oc(si), totl);
1735 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001736
1737 malformed_exit:
1738 /* malformed message */
1739 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1740 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001741}
1742
1743/*
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001744 * Receive a stick-table message or pre-parse any other message.
1745 * The message's header will be sent into <msg_head> which must be at least
1746 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
1747 * The first two bytes are always read, and the rest is only read if the
1748 * first bytes indicate a stick-table message. If the message is a stick-table
1749 * message, the varint is decoded and the equivalent number of bytes will be
1750 * copied into the trash at trash.area. <totl> is incremented by the number of
1751 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001752 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1753 * -1 if there was an error updating the appctx state st0 accordingly.
1754 */
1755static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1756 uint32_t *msg_len, int *totl)
1757{
1758 int reql;
1759 struct stream_interface *si = appctx->owner;
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001760 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001761
1762 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1763 if (reql <= 0) /* closed or EOL not found */
1764 goto incomplete;
1765
1766 *totl += reql;
1767
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02001768 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001769 return 1;
1770
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001771 /* This is a stick-table message, let's go on */
1772
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001773 /* Read and Decode message length */
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001774 msg_head += *totl;
1775 msg_head_sz -= *totl;
1776 reql = co_data(si_oc(si)) - *totl;
1777 if (reql > msg_head_sz)
1778 reql = msg_head_sz;
1779
1780 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001781 if (reql <= 0) /* closed */
1782 goto incomplete;
1783
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001784 cur = msg_head;
1785 *msg_len = intdecode(&cur, cur + reql);
1786 if (!cur) {
1787 /* the number is truncated, did we read enough ? */
1788 if (reql < msg_head_sz)
1789 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001790
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001791 /* malformed message */
1792 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1793 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001794 }
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001795 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001796
1797 /* Read message content */
1798 if (*msg_len) {
1799 if (*msg_len > trash.size) {
1800 /* Status code is not success, abort */
1801 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1802 return -1;
1803 }
1804
1805 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1806 if (reql <= 0) /* closed */
1807 goto incomplete;
1808 *totl += reql;
1809 }
1810
1811 return 1;
1812
1813 incomplete:
Willy Tarreau3660fae2020-11-26 17:06:04 +01001814 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1815 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001816 appctx->st0 = PEER_SESS_ST_END;
1817 return -1;
1818 }
1819
1820 return 0;
1821}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001822
1823/*
1824 * Treat the awaited message with <msg_head> as header.*
1825 * Return 1 if succeeded, 0 if not.
1826 */
1827static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1828 char **msg_cur, char *msg_end, int msg_len, int totl)
1829{
1830 struct stream_interface *si = appctx->owner;
1831 struct stream *s = si_strm(si);
1832 struct peers *peers = strm_fe(s)->parent;
1833
1834 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1835 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1836 struct shared_table *st;
1837 /* Reset message: remote need resync */
1838
1839 /* prepare tables fot a global push */
1840 for (st = peer->tables; st; st = st->next) {
Emeric Brunf4ea7a82021-04-28 09:49:33 +02001841 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001842 st->flags = 0;
1843 }
1844
1845 /* reset teaching flags to 0 */
1846 peer->flags &= PEER_TEACH_RESET;
1847
1848 /* flag to start to teach lesson */
1849 peer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunf8716e82021-04-28 12:59:35 +02001850 peers->flags |= PEERS_F_RESYNC_REQUESTED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001851 }
1852 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1853 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1854 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1855 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1856 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Emeric Brunf8716e82021-04-28 12:59:35 +02001857 if (peer->local)
1858 peers->flags |= PEERS_F_RESYNC_LOCALFINISHED;
1859 else
1860 peers->flags |= PEERS_F_RESYNC_REMOTEFINISHED;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001861 }
1862 peer->confirm++;
1863 }
1864 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1865 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1866 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1867 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1868
Emeric Brunf8716e82021-04-28 12:59:35 +02001869 if (peer->local)
1870 peers->flags |= PEERS_F_RESYNC_LOCALPARTIAL;
1871 else
1872 peers->flags |= PEERS_F_RESYNC_REMOTEPARTIAL;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001873 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001874 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001875 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1876 }
1877 peer->confirm++;
1878 }
1879 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1880 struct shared_table *st;
1881
1882 /* If stopping state */
1883 if (stopping) {
1884 /* Close session, push resync no more needed */
1885 peer->flags |= PEER_F_TEACH_COMPLETE;
1886 appctx->st0 = PEER_SESS_ST_END;
1887 return 0;
1888 }
1889 for (st = peer->tables; st; st = st->next) {
1890 st->update = st->last_pushed = st->teaching_origin;
1891 st->flags = 0;
1892 }
1893
1894 /* reset teaching flags to 0 */
1895 peer->flags &= PEER_TEACH_RESET;
1896 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001897 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001898 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille5d072702019-11-06 11:51:26 +01001899 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001900 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001901 }
1902 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1903 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1904 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1905 return 0;
1906 }
1907 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1908 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1909 return 0;
1910 }
1911 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1912 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1913 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1914 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1915 int update, expire;
1916
1917 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1918 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1919 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1920 msg_cur, msg_end, msg_len, totl))
1921 return 0;
1922
1923 }
1924 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1925 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1926 return 0;
1927 }
1928 }
1929 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1930 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1931 return 0;
1932 }
1933
1934 return 1;
1935}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001936
1937
1938/*
1939 * Send any message to <peer> peer.
1940 * Returns 1 if succeeded, or -1 or 0 if failed.
1941 * -1 means an internal error occured, 0 is for a peer protocol error leading
1942 * to a peer state change (from the peer I/O handler point of view).
1943 */
1944static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1945{
1946 int repl;
1947 struct stream_interface *si = appctx->owner;
1948 struct stream *s = si_strm(si);
1949 struct peers *peers = strm_fe(s)->parent;
1950
1951 /* Need to request a resync */
1952 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1953 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1954 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1955
1956 repl = peer_send_resync_reqmsg(appctx);
1957 if (repl <= 0)
1958 return repl;
1959
1960 peers->flags |= PEERS_F_RESYNC_PROCESS;
1961 }
1962
1963 /* Nothing to read, now we start to write */
1964 if (peer->tables) {
1965 struct shared_table *st;
1966 struct shared_table *last_local_table;
1967
1968 last_local_table = peer->last_local_table;
1969 if (!last_local_table)
1970 last_local_table = peer->tables;
1971 st = last_local_table->next;
1972
1973 while (1) {
1974 if (!st)
1975 st = peer->tables;
1976
1977 /* It remains some updates to ack */
1978 if (st->last_get != st->last_acked) {
1979 repl = peer_send_ackmsg(st, appctx);
1980 if (repl <= 0)
1981 return repl;
1982
1983 st->last_acked = st->last_get;
1984 }
1985
1986 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1987 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1988 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun129d8fa2021-04-28 11:48:15 +02001989 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001990
1991 repl = peer_send_teach_process_msgs(appctx, peer, st);
1992 if (repl <= 0) {
1993 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1994 return repl;
1995 }
1996 }
1997 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1998 }
Emeric Brun2f96bae2021-04-22 18:13:13 +02001999 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002000 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
2001 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
2002 if (repl <= 0)
2003 return repl;
2004 }
2005
2006 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
2007 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
2008 if (repl <= 0)
2009 return repl;
2010 }
2011 }
2012
2013 if (st == last_local_table)
2014 break;
2015 st = st->next;
2016 }
2017 }
2018
2019 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Emeric Brundeda2a62020-03-16 10:51:01 +01002020 repl = peer_send_resync_finishedmsg(appctx, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002021 if (repl <= 0)
2022 return repl;
2023
2024 /* flag finished message sent */
2025 peer->flags |= PEER_F_TEACH_FINISHED;
2026 }
2027
2028 /* Confirm finished or partial messages */
2029 while (peer->confirm) {
2030 repl = peer_send_resync_confirmsg(appctx);
2031 if (repl <= 0)
2032 return repl;
2033
2034 peer->confirm--;
2035 }
2036
2037 return 1;
2038}
2039
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002040/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002041 * Read and parse a first line of a "hello" peer protocol message.
2042 * Returns 0 if could not read a line, -1 if there was a read error or
2043 * the line is malformed, 1 if succeeded.
2044 */
2045static inline int peer_getline_version(struct appctx *appctx,
2046 unsigned int *maj_ver, unsigned int *min_ver)
2047{
2048 int reql;
2049
2050 reql = peer_getline(appctx);
2051 if (!reql)
2052 return 0;
2053
2054 if (reql < 0)
2055 return -1;
2056
2057 /* test protocol */
2058 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2059 appctx->st0 = PEER_SESS_ST_EXIT;
2060 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2061 return -1;
2062 }
2063 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2064 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2065 appctx->st0 = PEER_SESS_ST_EXIT;
2066 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2067 return -1;
2068 }
2069
2070 return 1;
2071}
2072
2073/*
2074 * Read and parse a second line of a "hello" peer protocol message.
2075 * Returns 0 if could not read a line, -1 if there was a read error or
2076 * the line is malformed, 1 if succeeded.
2077 */
2078static inline int peer_getline_host(struct appctx *appctx)
2079{
2080 int reql;
2081
2082 reql = peer_getline(appctx);
2083 if (!reql)
2084 return 0;
2085
2086 if (reql < 0)
2087 return -1;
2088
2089 /* test hostname match */
2090 if (strcmp(localpeer, trash.area) != 0) {
2091 appctx->st0 = PEER_SESS_ST_EXIT;
2092 appctx->st1 = PEER_SESS_SC_ERRHOST;
2093 return -1;
2094 }
2095
2096 return 1;
2097}
2098
2099/*
2100 * Read and parse a last line of a "hello" peer protocol message.
2101 * Returns 0 if could not read a character, -1 if there was a read error or
2102 * the line is malformed, 1 if succeeded.
2103 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2104 */
2105static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2106{
2107 char *p;
2108 int reql;
2109 struct peer *peer;
2110 struct stream_interface *si = appctx->owner;
2111 struct stream *s = si_strm(si);
2112 struct peers *peers = strm_fe(s)->parent;
2113
2114 reql = peer_getline(appctx);
2115 if (!reql)
2116 return 0;
2117
2118 if (reql < 0)
2119 return -1;
2120
2121 /* parse line "<peer name> <pid> <relative_pid>" */
2122 p = strchr(trash.area, ' ');
2123 if (!p) {
2124 appctx->st0 = PEER_SESS_ST_EXIT;
2125 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2126 return -1;
2127 }
2128 *p = 0;
2129
2130 /* lookup known peer */
2131 for (peer = peers->remote; peer; peer = peer->next) {
2132 if (strcmp(peer->id, trash.area) == 0)
2133 break;
2134 }
2135
2136 /* if unknown peer */
2137 if (!peer) {
2138 appctx->st0 = PEER_SESS_ST_EXIT;
2139 appctx->st1 = PEER_SESS_SC_ERRPEER;
2140 return -1;
2141 }
2142 *curpeer = peer;
2143
2144 return 1;
2145}
2146
2147/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002148 * Init <peer> peer after having accepted it at peer protocol level.
2149 */
2150static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2151{
2152 struct shared_table *st;
2153
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002154 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002155 /* Register status code */
2156 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
2157
2158 /* Awake main task */
2159 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2160
2161 /* Init confirm counter */
2162 peer->confirm = 0;
2163
2164 /* Init cursors */
2165 for (st = peer->tables; st ; st = st->next) {
2166 st->last_get = st->last_acked = 0;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002167 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2168 /* if st->update appears to be in future it means
2169 * that the last acked value is very old and we
2170 * remain unconnected a too long time to use this
2171 * acknowlegement as a reset.
2172 * We should update the protocol to be able to
2173 * signal the remote peer that it needs a full resync.
2174 * Here a partial fix consist to set st->update at
2175 * the max past value
2176 */
2177 if ((int)(st->table->localupdate - st->update) < 0)
2178 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002179 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun9a2847b2021-04-20 14:43:46 +02002180 st->flags = 0;
Emeric Brun053ecea2021-02-23 17:08:08 +01002181 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2182 st->table->commitupdate = st->last_pushed;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002183 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002184 }
2185
2186 /* reset teaching and learning flags to 0 */
2187 peer->flags &= PEER_TEACH_RESET;
2188 peer->flags &= PEER_LEARN_RESET;
2189
2190 /* if current peer is local */
2191 if (peer->local) {
2192 /* if current host need resyncfrom local and no process assined */
2193 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2194 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2195 /* assign local peer for a lesson, consider lesson already requested */
2196 peer->flags |= PEER_F_LEARN_ASSIGN;
2197 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunf8716e82021-04-28 12:59:35 +02002198 peers->flags |= PEERS_F_RESYNC_LOCALASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002199 }
2200
2201 }
2202 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2203 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2204 /* assign peer for a lesson */
2205 peer->flags |= PEER_F_LEARN_ASSIGN;
2206 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunf8716e82021-04-28 12:59:35 +02002207 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002208 }
2209}
2210
2211/*
2212 * Init <peer> peer after having connected it at peer protocol level.
2213 */
2214static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2215{
2216 struct shared_table *st;
2217
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002218 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002219 /* Init cursors */
2220 for (st = peer->tables; st ; st = st->next) {
2221 st->last_get = st->last_acked = 0;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002222 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2223 /* if st->update appears to be in future it means
2224 * that the last acked value is very old and we
2225 * remain unconnected a too long time to use this
2226 * acknowlegement as a reset.
2227 * We should update the protocol to be able to
2228 * signal the remote peer that it needs a full resync.
2229 * Here a partial fix consist to set st->update at
2230 * the max past value.
2231 */
2232 if ((int)(st->table->localupdate - st->update) < 0)
2233 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002234 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun9a2847b2021-04-20 14:43:46 +02002235 st->flags = 0;
Emeric Brun053ecea2021-02-23 17:08:08 +01002236 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2237 st->table->commitupdate = st->last_pushed;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002238 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002239 }
2240
2241 /* Init confirm counter */
2242 peer->confirm = 0;
2243
2244 /* reset teaching and learning flags to 0 */
2245 peer->flags &= PEER_TEACH_RESET;
2246 peer->flags &= PEER_LEARN_RESET;
2247
2248 /* If current peer is local */
2249 if (peer->local) {
2250 /* flag to start to teach lesson */
2251 peer->flags |= PEER_F_TEACH_PROCESS;
2252 }
2253 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2254 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2255 /* If peer is remote and resync from remote is needed,
2256 and no peer currently assigned */
2257
2258 /* assign peer for a lesson */
2259 peer->flags |= PEER_F_LEARN_ASSIGN;
2260 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunf8716e82021-04-28 12:59:35 +02002261 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002262 }
2263}
2264
2265/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002266 * IO Handler to handle message exchance with a peer
2267 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002268static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002269{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002270 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002271 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002272 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002273 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002274 int reql = 0;
2275 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002276 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002277 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002278
Joseph Herlant82b2f542018-11-15 12:19:14 -08002279 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002280 if (si_ic(si)->buf.size == 0) {
2281 si_rx_room_blk(si);
2282 goto out;
2283 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002284
Emeric Brun2b920a12010-09-23 18:30:22 +02002285 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002286 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002287switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002288 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002289 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002290 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002291 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002292 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002293 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002294 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002295 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002296 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002297 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2298 if (reql <= 0) {
2299 if (!reql)
2300 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002301 goto switchstate;
2302 }
2303
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002304 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002305 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002306 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002307 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002308 reql = peer_getline_host(appctx);
2309 if (reql <= 0) {
2310 if (!reql)
2311 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002312 goto switchstate;
2313 }
2314
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002315 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002316 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002317 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002318 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002319 reql = peer_getline_last(appctx, &curpeer);
2320 if (reql <= 0) {
2321 if (!reql)
2322 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002323 goto switchstate;
2324 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002325
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002326 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002327 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002328 if (curpeer->local) {
2329 /* Local connection, reply a retry */
2330 appctx->st0 = PEER_SESS_ST_EXIT;
2331 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2332 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002333 }
Emeric Brun80527f52017-06-19 17:46:37 +02002334
2335 /* we're killing a connection, we must apply a random delay before
2336 * retrying otherwise the other end will do the same and we can loop
2337 * for a while.
2338 */
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002339 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002340 peer_session_forceshutdown(curpeer);
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002341 curpeer->heartbeat = TICK_ETERNITY;
Emeric Brun2b920a12010-09-23 18:30:22 +02002342 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002343 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2344 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2345 curpeer->flags |= PEER_F_DWNGRD;
2346 }
2347 else {
2348 curpeer->flags &= ~PEER_F_DWNGRD;
2349 }
2350 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002351 curpeer->appctx = appctx;
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002352 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002353 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002354 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002355 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002356 /* fall through */
2357 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002358 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002359 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002360 if (!curpeer) {
2361 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002362 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002363 if (curpeer->appctx != appctx) {
2364 appctx->st0 = PEER_SESS_ST_END;
2365 goto switchstate;
2366 }
2367 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002368
2369 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002370 if (repl <= 0) {
2371 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002372 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002373 goto switchstate;
2374 }
2375
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002376 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002377
Emeric Brun2b920a12010-09-23 18:30:22 +02002378 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002379 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002380 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002381 goto switchstate;
2382 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002383 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002384 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002385 if (!curpeer) {
2386 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002387 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002388 if (curpeer->appctx != appctx) {
2389 appctx->st0 = PEER_SESS_ST_END;
2390 goto switchstate;
2391 }
2392 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002393
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002394 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002395 if (repl <= 0) {
2396 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002397 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002398 goto switchstate;
2399 }
2400
2401 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002402 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002403 /* fall through */
2404 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002405 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002406 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002407 if (!curpeer) {
2408 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002409 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002410 if (curpeer->appctx != appctx) {
2411 appctx->st0 = PEER_SESS_ST_END;
2412 goto switchstate;
2413 }
2414 }
2415
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002416 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002417 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002418
Frédéric Lécaillece025572019-01-21 13:38:06 +01002419 reql = peer_getline(appctx);
2420 if (!reql)
2421 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002422
Frédéric Lécaillece025572019-01-21 13:38:06 +01002423 if (reql < 0)
2424 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002425
2426 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002427 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002428
2429 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002430 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002431
2432 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002433 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002434 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002435 }
2436 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002437 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2438 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002439 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002440 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002441 goto switchstate;
2442 }
Olivier Houcharded879892019-03-08 18:53:43 +01002443 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002444 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002445 /* fall through */
2446 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002447 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002448 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002449 char *msg_cur = trash.area;
2450 char *msg_end = trash.area;
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01002451 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02002452 int totl = 0;
2453
Willy Tarreau2d372c22018-11-05 17:12:27 +01002454 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002455 if (!curpeer) {
2456 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002457 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002458 if (curpeer->appctx != appctx) {
2459 appctx->st0 = PEER_SESS_ST_END;
2460 goto switchstate;
2461 }
2462 }
2463
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002464 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2465 if (reql <= 0) {
2466 if (reql == -1)
2467 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002468 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002469 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002470
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002471 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002472 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002473 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002474
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002475 curpeer->flags |= PEER_F_ALIVE;
2476
Emeric Brun2b920a12010-09-23 18:30:22 +02002477 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002478 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002479 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002480 goto switchstate;
2481
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002482send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002483 if (curpeer->flags & PEER_F_HEARTBEAT) {
2484 curpeer->flags &= ~PEER_F_HEARTBEAT;
2485 repl = peer_send_heartbeatmsg(appctx);
2486 if (repl <= 0) {
2487 if (repl == -1)
2488 goto out;
2489 goto switchstate;
2490 }
Frédéric Lécaille5d072702019-11-06 11:51:26 +01002491 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002492 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002493 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002494 repl = peer_send_msgs(appctx, curpeer);
2495 if (repl <= 0) {
2496 if (repl == -1)
2497 goto out;
2498 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002499 }
2500
Emeric Brun2b920a12010-09-23 18:30:22 +02002501 /* noting more to do */
2502 goto out;
2503 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002504 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002505 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002506 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002507 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002508 if (peer_send_status_errormsg(appctx) == -1)
2509 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002510 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002511 goto switchstate;
2512 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002513 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002514 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002515 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002516 if (peer_send_error_size_limitmsg(appctx) == -1)
2517 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002518 appctx->st0 = PEER_SESS_ST_END;
2519 goto switchstate;
2520 }
2521 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécaille9a88a832019-11-07 15:22:33 +01002522 if (curpeer)
2523 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002524 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002525 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002526 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002527 if (peer_send_error_protomsg(appctx) == -1)
2528 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002529 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002530 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002531 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002532 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002533 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002534 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002535 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002536 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002537 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002538 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002539 curpeer = NULL;
2540 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002541 si_shutw(si);
2542 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002543 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002544 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002545 }
2546 }
2547 }
2548out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002549 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002550
2551 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002552 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002553 return;
2554}
2555
Willy Tarreau30576452015-04-13 13:50:30 +02002556static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002557 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002558 .name = "<PEER>", /* used for logging */
2559 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002560 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002561};
Emeric Brun2b920a12010-09-23 18:30:22 +02002562
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002563
Emeric Brun2b920a12010-09-23 18:30:22 +02002564/*
2565 * Use this function to force a close of a peer session
2566 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002567static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002568{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002569 struct appctx *appctx = peer->appctx;
2570
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002571 /* Note that the peer sessions which have just been created
2572 * (->st0 == PEER_SESS_ST_CONNECT) must not
2573 * be shutdown, if not, the TCP session will never be closed
2574 * and stay in CLOSE_WAIT state after having been closed by
2575 * the remote side.
2576 */
2577 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002578 return;
2579
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002580 if (appctx->applet != &peer_applet)
2581 return;
2582
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002583 __peer_session_deinit(peer);
2584
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002585 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002586 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002587}
2588
Willy Tarreau91d96282015-03-13 15:47:26 +01002589/* Pre-configures a peers frontend to accept incoming connections */
2590void peers_setup_frontend(struct proxy *fe)
2591{
2592 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002593 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002594 fe->maxconn = 0;
2595 fe->conn_retries = CONN_RETRIES;
2596 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002597 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002598 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002599 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002600 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002601}
2602
Emeric Brun2b920a12010-09-23 18:30:22 +02002603/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002604 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002605 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002606static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002607{
Willy Tarreau04b92862017-09-15 11:01:04 +02002608 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002609 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002610 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002611 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002612 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002613 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002614
Frédéric Lécaille54152712021-01-18 15:14:39 +01002615 peer->new_conn++;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002616 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002617 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002618 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002619 s = NULL;
2620
Emeric Brun1138fd02017-06-19 12:38:55 +02002621 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002622 if (!appctx)
2623 goto out_close;
2624
2625 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002626 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002627
Willy Tarreau04b92862017-09-15 11:01:04 +02002628 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002629 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002630 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002631 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002632 }
2633
Willy Tarreau87787ac2017-08-28 16:22:54 +02002634 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002635 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002636 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002637 }
2638
Willy Tarreau342bfb12015-04-05 01:35:34 +02002639 /* The tasks below are normally what is supposed to be done by
2640 * fe->accept().
2641 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002642 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002643
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002644 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002645 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002646 appctx_wakeup(appctx);
2647
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002648 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002649 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002650 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002651
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002652 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002653 * pre-initialized connection in si->conn.
2654 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002655 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002656 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002657
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002658 if (unlikely((cs = cs_new(conn)) == NULL))
2659 goto out_free_conn;
2660
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002661 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002662 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2663
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002664 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002665 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2666 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002667 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002668
Emeric Brun2b920a12010-09-23 18:30:22 +02002669 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002670 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002671
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002672 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002673
Emeric Brunb3971ab2015-05-12 18:49:09 +02002674 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002675 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002676 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002677 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002678
2679 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002680out_free_cs:
2681 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002682 out_free_conn:
2683 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002684 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002685 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002686 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002687 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002688 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002689 out_free_appctx:
2690 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002691 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002692 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002693}
2694
2695/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002696 * Task processing function to manage re-connect, peer session
2697 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002698 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002699static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002700{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002701 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002702 struct peer *ps;
2703 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002704
2705 task->expire = TICK_ETERNITY;
2706
Emeric Brunb3971ab2015-05-12 18:49:09 +02002707 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002708 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002709 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002710 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002711 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002712 return NULL;
2713 }
2714
Emeric Brun80527f52017-06-19 17:46:37 +02002715 /* Acquire lock for all peers of the section */
2716 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002717 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002718
Emeric Brun2b920a12010-09-23 18:30:22 +02002719 if (!stopping) {
2720 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002721
Emeric Bruna9cbe952021-04-21 16:06:35 +02002722 /* resync timeout set to TICK_ETERNITY means we just start
2723 * a new process and timer was not initialized.
2724 * We must arm this timer to switch to a request to a remote
2725 * node if incoming connection from old local process never
2726 * comes.
2727 */
2728 if (peers->resync_timeout == TICK_ETERNITY)
2729 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
2730
Emeric Brunb3971ab2015-05-12 18:49:09 +02002731 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2732 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2733 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002734 /* Resync from local peer needed
2735 no peer was assigned for the lesson
2736 and no old local peer found
2737 or resync timeout expire */
2738
2739 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002740 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brunf8716e82021-04-28 12:59:35 +02002741 peers->flags |= PEERS_F_RESYNC_LOCALTIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02002742
2743 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002744 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002745 }
2746
2747 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002748 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002749 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002750 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002751 if (!ps->appctx) {
2752 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002753 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002754 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002755 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002756 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002757 tick_is_expired(ps->reconnect, now_ms))) {
2758 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002759 * or previous peer connection established with success
2760 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002761 * and reconnection timer is expired */
2762
2763 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002764 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002765 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002766 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002767 /* If previous session failed during connection
2768 * but reconnection timer is not expired */
2769
2770 /* reschedule task for reconnect */
2771 task->expire = tick_first(task->expire, ps->reconnect);
2772 }
2773 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002774 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002775 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002776 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002777 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2778 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002779 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2780 /* Resync from a remote is needed
2781 * and no peer was assigned for lesson
2782 * and current peer may be up2date */
2783
2784 /* assign peer for the lesson */
2785 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002786 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brunf8716e82021-04-28 12:59:35 +02002787 peers->flags |= PEERS_F_RESYNC_REMOTEASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002788
Willy Tarreau9df94c22016-10-31 18:42:52 +01002789 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002790 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002791 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002792 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002793 int update_to_push = 0;
2794
Emeric Brunb3971ab2015-05-12 18:49:09 +02002795 /* Awake session if there is data to push */
2796 for (st = ps->tables; st ; st = st->next) {
Emeric Brun129d8fa2021-04-28 11:48:15 +02002797 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002798 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002799 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002800 /* There is no need to send a heartbeat message
2801 * when some updates must be pushed. The remote
2802 * peer will consider <ps> peer as alive when it will
2803 * receive these updates.
2804 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002805 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002806 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002807 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002808 /* We are going to send updates, let's ensure we will
2809 * come back to send heartbeat messages or to reconnect.
2810 */
2811 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002812 appctx_wakeup(ps->appctx);
2813 break;
2814 }
2815 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002816 /* When there are updates to send we do not reconnect
2817 * and do not send heartbeat message either.
2818 */
2819 if (!update_to_push) {
2820 if (tick_is_expired(ps->reconnect, now_ms)) {
2821 if (ps->flags & PEER_F_ALIVE) {
2822 /* This peer was alive during a 'reconnect' period.
2823 * Flag it as not alive again for the next period.
2824 */
2825 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002826 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002827 }
2828 else {
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002829 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002830 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002831 peer_session_forceshutdown(ps);
Frédéric Lécaille9a88a832019-11-07 15:22:33 +01002832 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002833 }
2834 }
2835 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2836 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2837 ps->flags |= PEER_F_HEARTBEAT;
2838 appctx_wakeup(ps->appctx);
2839 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002840 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002841 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002842 }
2843 /* else do nothing */
2844 } /* SUCCESSCODE */
2845 } /* !ps->peer->local */
2846 } /* for */
2847
2848 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002849 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2850 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2851 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002852 /* Resync from remote peer needed
2853 * no peer was assigned for the lesson
2854 * and resync timeout expire */
2855
2856 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002857 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brunf8716e82021-04-28 12:59:35 +02002858 peers->flags |= PEERS_F_RESYNC_REMOTETIMEOUT;
Emeric Brun2b920a12010-09-23 18:30:22 +02002859 }
2860
Emeric Brunb3971ab2015-05-12 18:49:09 +02002861 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002862 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002863 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2864 if (!tick_is_expired(peers->resync_timeout, now_ms))
2865 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002866 }
2867 } /* !stopping */
2868 else {
2869 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002870 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002871 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002872 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002873 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002874 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002875 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02002876
Emeric Brun051c52b2021-04-22 18:20:37 +02002877 /* disconnect all connected peers to process a local sync
2878 * this must be done only the first time we are switching
2879 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02002880 */
Emeric Brun051c52b2021-04-22 18:20:37 +02002881 for (ps = peers->remote; ps; ps = ps->next) {
2882 /* we're killing a connection, we must apply a random delay before
2883 * retrying otherwise the other end will do the same and we can loop
2884 * for a while.
2885 */
2886 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
2887 if (ps->appctx) {
2888 peer_session_forceshutdown(ps);
2889 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002890 }
2891 }
2892 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002893
Emeric Brunb3971ab2015-05-12 18:49:09 +02002894 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002895 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002896 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002897 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002898 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002899 peers->flags &= ~PEERS_F_DONOTSTOP;
2900 for (st = ps->tables; st ; st = st->next)
Emeric Brun692e71d2021-04-23 17:02:22 +02002901 _HA_ATOMIC_SUB(&st->table->refcnt, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002902 }
2903 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002904 else if (!ps->appctx) {
2905 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002906 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002907 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2908 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2909 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002910 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002911 * or previous peer connection was successfully established
2912 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002913 * or during previous connect, peer replies a try again statuscode */
2914
Emeric Brun051c52b2021-04-22 18:20:37 +02002915 /* connect to the local peer if we must push a local sync */
2916 if (peers->flags & PEERS_F_DONOTSTOP) {
2917 peer_session_create(peers, ps);
2918 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002919 }
2920 else {
2921 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002922 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002923 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002924 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002925 peers->flags &= ~PEERS_F_DONOTSTOP;
2926 for (st = ps->tables; st ; st = st->next)
Emeric Brun692e71d2021-04-23 17:02:22 +02002927 _HA_ATOMIC_SUB(&st->table->refcnt, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002928 }
2929 }
2930 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002931 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002932 /* current peer connection is active and established
2933 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002934 for (st = ps->tables; st ; st = st->next) {
Emeric Brun129d8fa2021-04-28 11:48:15 +02002935 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002936 appctx_wakeup(ps->appctx);
2937 break;
2938 }
2939 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002940 }
2941 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002942
2943 /* Release lock for all peers of the section */
2944 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002945 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002946
Emeric Brun2b920a12010-09-23 18:30:22 +02002947 /* Wakeup for re-connect */
2948 return task;
2949}
2950
Emeric Brunb3971ab2015-05-12 18:49:09 +02002951
Emeric Brun2b920a12010-09-23 18:30:22 +02002952/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002953 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002954 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002955int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002956{
Emeric Brun2b920a12010-09-23 18:30:22 +02002957 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002958
Emeric Brun2b920a12010-09-23 18:30:22 +02002959 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002960 peers->peers_fe->maxconn += 3;
2961 }
2962
Emeric Brunc60def82017-09-27 14:59:38 +02002963 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002964 if (!peers->sync_task)
2965 return 0;
2966
Emeric Brunb3971ab2015-05-12 18:49:09 +02002967 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002968 peers->sync_task->context = (void *)peers;
2969 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2970 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002971 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002972}
2973
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002974/*
2975 * Allocate a cache a dictionary entries used upon transmission.
2976 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002977static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002978{
2979 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002980 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002981
2982 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002983 entries = calloc(max_entries, sizeof *entries);
2984 if (!d || !entries)
2985 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002986
2987 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002988 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002989 d->cached_entries = EB_ROOT_UNIQUE;
2990 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002991
2992 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002993
2994 err:
2995 free(d);
2996 free(entries);
2997 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002998}
2999
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003000static void free_dcache_tx(struct dcache_tx *dc)
3001{
3002 free(dc->entries);
3003 dc->entries = NULL;
3004 free(dc);
3005}
3006
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003007/*
3008 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
3009 * as maximum of entries.
3010 * Return the dictionay cache if succeeded, NULL if not.
3011 * Must be deallocated calling free_dcache().
3012 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003013static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003014{
3015 struct dcache_tx *dc_tx;
3016 struct dcache *dc;
3017 struct dcache_rx *dc_rx;
3018
3019 dc = calloc(1, sizeof *dc);
3020 dc_tx = new_dcache_tx(max_entries);
3021 dc_rx = calloc(max_entries, sizeof *dc_rx);
3022 if (!dc || !dc_tx || !dc_rx)
3023 goto err;
3024
3025 dc->tx = dc_tx;
3026 dc->rx = dc_rx;
3027 dc->max_entries = max_entries;
3028
3029 return dc;
3030
3031 err:
3032 free(dc);
3033 free(dc_tx);
3034 free(dc_rx);
3035 return NULL;
3036}
3037
3038/*
3039 * Deallocate a cache of dictionary entries.
3040 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003041static inline void free_dcache(struct dcache *dc)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003042{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003043 free_dcache_tx(dc->tx);
3044 dc->tx = NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003045 free(dc->rx); dc->rx = NULL;
3046 free(dc);
3047}
3048
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003049
3050/*
3051 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3052 * entries used upon transmission.
3053 * Return the entry if found, NULL if not.
3054 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003055static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3056 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003057{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003058 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003059}
3060
3061/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003062 * Flush <dc> cache.
3063 * Always succeeds.
3064 */
3065static inline void flush_dcache(struct peer *peer)
3066{
3067 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003068 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003069
Frédéric Lécailled31dc822020-11-12 21:01:54 +01003070 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003071 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailled31dc822020-11-12 21:01:54 +01003072 dc->tx->entries[i].key = NULL;
3073 }
3074 dc->tx->prev_lookup = NULL;
3075 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003076
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003077 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003078}
3079
3080/*
3081 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3082 * with information provided by <i> dictionary cache entry (especially the value
3083 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003084 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003085 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003086static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003087{
3088 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003089 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003090
3091 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003092
3093 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3094 o = dc_tx->prev_lookup;
3095 } else {
3096 o = dcache_tx_lookup_value(dc_tx, i);
3097 if (o) {
3098 /* Save it */
3099 dc_tx->prev_lookup = o;
3100 }
3101 }
3102
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003103 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003104 /* Copy the ID. */
3105 i->id = o - dc->tx->entries;
3106 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003107 }
3108
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003109 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003110 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003111
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003112 ebpt_delete(o);
3113 o->key = i->entry.key;
3114 ebpt_insert(&dc_tx->cached_entries, o);
3115 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003116
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003117 /* Update the index for the next entry to put in cache */
3118 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003119
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003120 return o;
3121}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003122
3123/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003124 * Allocate a dictionary cache for each peer of <peers> section.
3125 * Return 1 if succeeded, 0 if not.
3126 */
3127int peers_alloc_dcache(struct peers *peers)
3128{
3129 struct peer *p;
3130
3131 for (p = peers->remote; p; p = p->next) {
3132 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3133 if (!p->dcache)
3134 return 0;
3135 }
3136
3137 return 1;
3138}
3139
3140/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003141 * Function used to register a table for sync on a group of peers
Remi Tricot-Le Breton99191412021-05-12 17:39:04 +02003142 * Returns 0 in case of success.
Emeric Brunb3971ab2015-05-12 18:49:09 +02003143 */
Remi Tricot-Le Breton99191412021-05-12 17:39:04 +02003144int peers_register_table(struct peers *peers, struct stktable *table)
Emeric Brunb3971ab2015-05-12 18:49:09 +02003145{
3146 struct shared_table *st;
3147 struct peer * curpeer;
3148 int id = 0;
Remi Tricot-Le Breton99191412021-05-12 17:39:04 +02003149 int retval = 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02003150
3151 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003152 st = calloc(1,sizeof(*st));
Remi Tricot-Le Breton99191412021-05-12 17:39:04 +02003153 if (!st) {
3154 retval = 1;
3155 break;
3156 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02003157 st->table = table;
3158 st->next = curpeer->tables;
3159 if (curpeer->tables)
3160 id = curpeer->tables->local_id;
3161 st->local_id = id + 1;
3162
Emeric Brun692e71d2021-04-23 17:02:22 +02003163 /* If peer is local we inc table
3164 * refcnt to protect against flush
3165 * until this process pushed all
3166 * table content to the new one
3167 */
3168 if (curpeer->local)
3169 _HA_ATOMIC_ADD(&st->table->refcnt, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003170 curpeer->tables = st;
3171 }
3172
3173 table->sync_task = peers->sync_task;
Remi Tricot-Le Breton99191412021-05-12 17:39:04 +02003174
3175 return retval;
Emeric Brun2b920a12010-09-23 18:30:22 +02003176}
3177
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003178/*
3179 * Parse the "show peers" command arguments.
3180 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3181 * error message.
3182 */
3183static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3184{
3185 appctx->ctx.cfgpeers.target = NULL;
3186
3187 if (*args[2]) {
3188 struct peers *p;
3189
3190 for (p = cfg_peers; p; p = p->next) {
3191 if (!strcmp(p->id, args[2])) {
3192 appctx->ctx.cfgpeers.target = p;
3193 break;
3194 }
3195 }
3196
3197 if (!p) {
3198 appctx->ctx.cli.severity = LOG_ERR;
3199 appctx->ctx.cli.msg = "No such peers\n";
3200 appctx->st0 = CLI_ST_PRINT;
3201 return 1;
3202 }
3203 }
3204
3205 return 0;
3206}
3207
3208/*
3209 * This function dumps the peer state information of <peers> "peers" section.
3210 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3211 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3212 */
3213static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3214{
3215 struct tm tm;
3216
3217 get_localtime(peers->last_change, &tm);
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003218 chunk_appendf(msg, "%p: [%02d/%s/%04d:%02d:%02d:%02d] id=%s state=%d flags=0x%x resync_timeout=%s task_calls=%u\n",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003219 peers,
3220 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3221 tm.tm_hour, tm.tm_min, tm.tm_sec,
3222 peers->id, peers->state, peers->flags,
3223 peers->resync_timeout ?
3224 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3225 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003226 TICKS_TO_MS(1000)) : "<NEVER>",
3227 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003228
3229 if (ci_putchk(si_ic(si), msg) == -1) {
3230 si_rx_room_blk(si);
3231 return 0;
3232 }
3233
3234 return 1;
3235}
3236
3237/*
3238 * This function dumps <peer> state information.
3239 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3240 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3241 */
3242static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
3243{
3244 struct connection *conn;
3245 char pn[INET6_ADDRSTRLEN];
3246 struct stream_interface *peer_si;
3247 struct stream *peer_s;
3248 struct appctx *appctx;
3249 struct shared_table *st;
3250
3251 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille85e72122020-10-05 12:33:07 +02003252 chunk_appendf(msg, " %p: id=%s(%s,%s) addr=%s:%d last_status=%s reconnect=%s confirm=%u tx_hbt=%u rx_hbt=%u no_hbt=%u new_conn=%u proto_err=%u\n",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003253 peer, peer->id,
3254 peer->local ? "local" : "remote",
Frédéric Lécaille85e72122020-10-05 12:33:07 +02003255 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003256 pn, get_host_port(&peer->addr),
3257 statuscode_str(peer->statuscode),
3258 peer->reconnect ?
3259 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3260 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
3261 TICKS_TO_MS(1000)) : "<NEVER>",
Frédéric Lécaille9a88a832019-11-07 15:22:33 +01003262 peer->confirm, peer->tx_hbt, peer->rx_hbt,
3263 peer->no_hbt, peer->new_conn, peer->proto_err);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003264
3265 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3266
3267 appctx = peer->appctx;
3268 if (!appctx)
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003269 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003270
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003271 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3272 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003273
3274 peer_si = peer->appctx->owner;
3275 if (!peer_si)
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003276 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003277
3278 peer_s = si_strm(peer_si);
3279 if (!peer_s)
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003280 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003281
3282 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3283
3284 conn = objt_conn(strm_orig(peer_s));
3285 if (conn)
3286 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3287
3288 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3289 case AF_INET:
3290 case AF_INET6:
3291 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(&conn->addr.from));
3292 break;
3293 case AF_UNIX:
3294 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3295 break;
3296 }
3297
3298 if (conn)
3299 conn_get_to_addr(conn);
3300
3301 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3302 case AF_INET:
3303 case AF_INET6:
3304 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(&conn->addr.to));
3305 break;
3306 case AF_UNIX:
3307 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3308 break;
3309 }
3310
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003311 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003312 if (peer->remote_table)
3313 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3314 peer->remote_table,
3315 peer->remote_table->table->id,
3316 peer->remote_table->local_id,
3317 peer->remote_table->remote_id);
3318
3319 if (peer->last_local_table)
3320 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3321 peer->last_local_table,
3322 peer->last_local_table->table->id,
3323 peer->last_local_table->local_id,
3324 peer->last_local_table->remote_id);
3325
3326 if (peer->tables) {
3327 chunk_appendf(&trash, "\n shared tables:");
3328 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003329 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003330 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003331 struct dcache *dcache;
3332
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003333 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003334 dcache = peer->dcache;
3335
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003336 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3337 "flags=0x%x remote_data=0x%llx",
3338 st, st->local_id, st->remote_id,
3339 st->flags, (unsigned long long)st->remote_data);
3340 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3341 " teaching_origin=%u update=%u",
3342 st->last_acked, st->last_pushed, st->last_get,
3343 st->teaching_origin, st->update);
3344 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun692e71d2021-04-23 17:02:22 +02003345 " commitupdate=%u refcnt=%u",
3346 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003347 chunk_appendf(&trash, "\n TX dictionary cache:");
3348 count = 0;
3349 for (i = 0; i < dcache->max_entries; i++) {
3350 struct ebpt_node *node;
3351 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003352
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003353 node = &dcache->tx->entries[i];
3354 if (!node->key)
3355 break;
3356
3357 if (!count++)
3358 chunk_appendf(&trash, "\n ");
3359 de = node->key;
3360 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3361 count &= 0x3;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003362 }
3363 chunk_appendf(&trash, "\n RX dictionary cache:");
3364 count = 0;
3365 for (i = 0; i < dcache->max_entries; i++) {
3366 if (!count++)
3367 chunk_appendf(&trash, "\n ");
3368 chunk_appendf(&trash, " %3u -> %s", i,
3369 dcache->rx[i].de ?
3370 (char *)dcache->rx[i].de->value.key : "-");
3371 count &= 0x3;
3372 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003373 }
3374 }
3375
3376 end:
3377 chunk_appendf(&trash, "\n");
3378 if (ci_putchk(si_ic(si), msg) == -1) {
3379 si_rx_room_blk(si);
3380 return 0;
3381 }
3382
3383 return 1;
3384}
3385
3386/*
3387 * This function dumps all the peers of "peers" section.
3388 * Returns 0 if the output buffer is full and needs to be called
3389 * again, non-zero if not. It proceeds in an isolated thread, so
3390 * there is no thread safety issue here.
3391 */
3392static int cli_io_handler_show_peers(struct appctx *appctx)
3393{
3394 int show_all;
3395 int ret = 0, first_peers = 1;
3396 struct stream_interface *si = appctx->owner;
3397
3398 thread_isolate();
3399
3400 show_all = !appctx->ctx.cfgpeers.target;
3401
3402 chunk_reset(&trash);
3403
3404 while (appctx->st2 != STAT_ST_FIN) {
3405 switch (appctx->st2) {
3406 case STAT_ST_INIT:
3407 if (show_all)
3408 appctx->ctx.cfgpeers.peers = cfg_peers;
3409 else
3410 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3411
3412 appctx->st2 = STAT_ST_LIST;
3413 /* fall through */
3414
3415 case STAT_ST_LIST:
3416 if (!appctx->ctx.cfgpeers.peers) {
3417 /* No more peers list. */
3418 appctx->st2 = STAT_ST_END;
3419 }
3420 else {
3421 if (!first_peers)
3422 chunk_appendf(&trash, "\n");
3423 else
3424 first_peers = 0;
3425 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3426 goto out;
3427
3428 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3429 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3430 appctx->st2 = STAT_ST_INFO;
3431 }
3432 break;
3433
3434 case STAT_ST_INFO:
3435 if (!appctx->ctx.cfgpeers.peer) {
3436 /* End of peer list */
3437 if (show_all)
3438 appctx->st2 = STAT_ST_LIST;
3439 else
3440 appctx->st2 = STAT_ST_END;
3441 }
3442 else {
3443 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
3444 goto out;
3445
3446 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3447 }
3448 break;
3449
3450 case STAT_ST_END:
3451 appctx->st2 = STAT_ST_FIN;
3452 break;
3453 }
3454 }
3455 ret = 1;
3456 out:
3457 thread_release();
3458 return ret;
3459}
3460
3461/*
3462 * CLI keywords.
3463 */
3464static struct cli_kw_list cli_kws = {{ }, {
3465 { { "show", "peers", NULL }, "show peers [peers section]: dump some information about all the peers or this peers section", cli_parse_show_peers, cli_io_handler_show_peers, },
3466 {},
3467}};
3468
3469/* Register cli keywords */
3470INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3471