blob: c6634d9b6675a809bc30764818d24f550bdb2cba [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/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010062#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_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
67 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020068
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010069#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
70#define PEERS_RESYNC_FROMLOCAL 0x00000000
71#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
72#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020073
74/***********************************/
75/* Current shared table sync state */
76/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010077#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
78#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020079
80/******************************/
81/* Remote peer teaching state */
82/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010083#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
84#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
85#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
86#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
87#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
88#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
89#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
90#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 +020091
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010092#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
93#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020094
Frédéric Lécaille54bff832019-03-26 10:25:20 +010095#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
96#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010097#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
98
Emeric Brunb3971ab2015-05-12 18:49:09 +020099/*****************************/
100/* Sync message class */
101/*****************************/
102enum {
103 PEER_MSG_CLASS_CONTROL = 0,
104 PEER_MSG_CLASS_ERROR,
105 PEER_MSG_CLASS_STICKTABLE = 10,
106 PEER_MSG_CLASS_RESERVED = 255,
107};
108
109/*****************************/
110/* control message types */
111/*****************************/
112enum {
113 PEER_MSG_CTRL_RESYNCREQ = 0,
114 PEER_MSG_CTRL_RESYNCFINISHED,
115 PEER_MSG_CTRL_RESYNCPARTIAL,
116 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100117 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200118};
119
120/*****************************/
121/* error message types */
122/*****************************/
123enum {
124 PEER_MSG_ERR_PROTOCOL = 0,
125 PEER_MSG_ERR_SIZELIMIT,
126};
127
Emeric Brunc329cd02020-06-02 11:17:42 +0200128/* network key types;
129 * network types were directly and mistakenly
130 * mapped on sample types, to keep backward
131 * compatiblitiy we keep those values but
132 * we now use a internal/network mapping
133 * to avoid further mistakes adding or
134 * modifying internals types
135 */
136enum {
137 PEER_KT_ANY = 0, /* any type */
138 PEER_KT_RESV1, /* UNUSED */
139 PEER_KT_SINT, /* signed 64bits integer type */
140 PEER_KT_RESV3, /* UNUSED */
141 PEER_KT_IPV4, /* ipv4 type */
142 PEER_KT_IPV6, /* ipv6 type */
143 PEER_KT_STR, /* char string type */
144 PEER_KT_BIN, /* buffer type */
145 PEER_KT_TYPES /* number of types, must always be last */
146};
147
148/* Map used to retrieve network type from internal type
149 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
150 */
151static int peer_net_key_type[SMP_TYPES] = {
152 [SMP_T_SINT] = PEER_KT_SINT,
153 [SMP_T_IPV4] = PEER_KT_IPV4,
154 [SMP_T_IPV6] = PEER_KT_IPV6,
155 [SMP_T_STR] = PEER_KT_STR,
156 [SMP_T_BIN] = PEER_KT_BIN,
157};
158
159/* Map used to retrieve internal type from external type
160 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
161 */
162static int peer_int_key_type[PEER_KT_TYPES] = {
163 [PEER_KT_SINT] = SMP_T_SINT,
164 [PEER_KT_IPV4] = SMP_T_IPV4,
165 [PEER_KT_IPV6] = SMP_T_IPV6,
166 [PEER_KT_STR] = SMP_T_STR,
167 [PEER_KT_BIN] = SMP_T_BIN,
168};
169
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100170/*
171 * Parameters used by functions to build peer protocol messages. */
172struct peer_prep_params {
173 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100174 struct peer *peer;
175 } hello;
176 struct {
177 unsigned int st1;
178 } error_status;
179 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100180 struct stksess *stksess;
181 struct shared_table *shared_table;
182 unsigned int updateid;
183 int use_identifier;
184 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200185 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100186 } updt;
187 struct {
188 struct shared_table *shared_table;
189 } swtch;
190 struct {
191 struct shared_table *shared_table;
192 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100193 struct {
194 unsigned char head[2];
195 } control;
196 struct {
197 unsigned char head[2];
198 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100199};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200200
201/*******************************/
202/* stick table sync mesg types */
203/* Note: ids >= 128 contains */
204/* id message cotains data */
205/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200206#define PEER_MSG_STKT_UPDATE 0x80
207#define PEER_MSG_STKT_INCUPDATE 0x81
208#define PEER_MSG_STKT_DEFINE 0x82
209#define PEER_MSG_STKT_SWITCH 0x83
210#define PEER_MSG_STKT_ACK 0x84
211#define PEER_MSG_STKT_UPDATE_TIMED 0x85
212#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200213/* All the stick-table message identifiers abova have the #7 bit set */
214#define PEER_MSG_STKT_BIT 7
215#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200216
Frédéric Lécaille39143342019-05-24 14:32:27 +0200217/* The maximum length of an encoded data length. */
218#define PEER_MSG_ENC_LENGTH_MAXLEN 5
219
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200220/* Minimum 64-bits value encoded with 2 bytes */
221#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
222/* 3 bytes */
223#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
224/* 4 bytes */
225#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
226/* 5 bytes */
227#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
228/* 6 bytes */
229#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
230/* 7 bytes */
231#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
232/* 8 bytes */
233#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
234/* 9 bytes */
235#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
236/* 10 bytes */
237#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
238
239/* #7 bit used to detect the last byte to be encoded */
240#define PEER_ENC_STOP_BIT 7
241/* The byte minimum value with #7 bit set */
242#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
243/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
244#define PEER_ENC_2BYTES_MIN_BITS 4
245
Frédéric Lécaille39143342019-05-24 14:32:27 +0200246#define PEER_MSG_HEADER_LEN 2
247
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200248#define PEER_STKT_CACHE_MAX_ENTRIES 128
249
Emeric Brun2b920a12010-09-23 18:30:22 +0200250/**********************************/
251/* Peer Session IO handler states */
252/**********************************/
253
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100254enum {
255 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
256 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
257 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
258 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100259 /* after this point, data were possibly exchanged */
260 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
261 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
262 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
263 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
264 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200265 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
266 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100267 PEER_SESS_ST_END, /* Killed session */
268};
Emeric Brun2b920a12010-09-23 18:30:22 +0200269
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100270/***************************************************/
271/* Peer Session status code - part of the protocol */
272/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200273
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100274#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
275#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200276
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100277#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200278
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100279#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200280
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100281#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
282#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
283#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
284#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200285
286#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200287#define PEER_MAJOR_VER 2
288#define PEER_MINOR_VER 1
289#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200290
Willy Tarreau6254a922019-01-29 17:45:23 +0100291static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200292struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200293static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200294
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200295static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
296 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200297static inline void flush_dcache(struct peer *peer);
298
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200299static const char *statuscode_str(int statuscode)
300{
301 switch (statuscode) {
302 case PEER_SESS_SC_CONNECTCODE:
303 return "CONN";
304 case PEER_SESS_SC_CONNECTEDCODE:
305 return "HSHK";
306 case PEER_SESS_SC_SUCCESSCODE:
307 return "ESTA";
308 case PEER_SESS_SC_TRYAGAIN:
309 return "RETR";
310 case PEER_SESS_SC_ERRPROTO:
311 return "PROT";
312 case PEER_SESS_SC_ERRVERSION:
313 return "VERS";
314 case PEER_SESS_SC_ERRHOST:
315 return "NAME";
316 case PEER_SESS_SC_ERRPEER:
317 return "UNKN";
318 default:
319 return "NONE";
320 }
321}
322
Emeric Brun18928af2017-03-29 16:32:53 +0200323/* This function encode an uint64 to 'dynamic' length format.
324 The encoded value is written at address *str, and the
325 caller must assure that size after *str is large enought.
326 At return, the *str is set at the next Byte after then
327 encoded integer. The function returns then length of the
328 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200329int intencode(uint64_t i, char **str) {
330 int idx = 0;
331 unsigned char *msg;
332
Emeric Brunb3971ab2015-05-12 18:49:09 +0200333 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200334 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200335 msg[0] = (unsigned char)i;
336 *str = (char *)&msg[idx+1];
337 return (idx+1);
338 }
339
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200340 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
341 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
342 while (i >= PEER_ENC_STOP_BYTE) {
343 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
344 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200345 }
346 msg[++idx] = (unsigned char)i;
347 *str = (char *)&msg[idx+1];
348 return (idx+1);
349}
350
351
352/* This function returns the decoded integer or 0
353 if decode failed
354 *str point on the beginning of the integer to decode
355 at the end of decoding *str point on the end of the
356 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200357uint64_t intdecode(char **str, char *end)
358{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200359 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200360 uint64_t i;
361 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200362
363 if (!*str)
364 return 0;
365
366 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200367 if (msg >= (unsigned char *)end)
368 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200369
Emeric Brun18928af2017-03-29 16:32:53 +0200370 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200371 if (i >= PEER_ENC_2BYTES_MIN) {
372 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200373 do {
374 if (msg >= (unsigned char *)end)
375 goto fail;
376 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200377 shift += PEER_ENC_STOP_BIT;
378 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200379 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100380 *str = (char *)msg;
381 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200382
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100383 fail:
384 *str = NULL;
385 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200386}
Emeric Brun2b920a12010-09-23 18:30:22 +0200387
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100388/*
389 * Build a "hello" peer protocol message.
390 * Return the number of written bytes written to build this messages if succeeded,
391 * 0 if not.
392 */
393static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
394{
395 int min_ver, ret;
396 struct peer *peer;
397
398 peer = p->hello.peer;
399 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
400 /* Prepare headers */
401 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
402 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
403 if (ret >= size)
404 return 0;
405
406 return ret;
407}
408
409/*
410 * Build a "handshake succeeded" status message.
411 * Return the number of written bytes written to build this messages if succeeded,
412 * 0 if not.
413 */
414static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
415{
416 int ret;
417
418 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
419 if (ret >= size)
420 return 0;
421
422 return ret;
423}
424
425/*
426 * Build an error status message.
427 * Return the number of written bytes written to build this messages if succeeded,
428 * 0 if not.
429 */
430static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
431{
432 int ret;
433 unsigned int st1;
434
435 st1 = p->error_status.st1;
436 ret = snprintf(msg, size, "%d\n", st1);
437 if (ret >= size)
438 return 0;
439
440 return ret;
441}
442
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200443/* Set the stick-table UPDATE message type byte at <msg_type> address,
444 * depending on <use_identifier> and <use_timed> boolean parameters.
445 * Always successful.
446 */
447static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
448{
449 if (use_timed) {
450 if (use_identifier)
451 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
452 else
453 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
454 }
455 else {
456 if (use_identifier)
457 *msg_type = PEER_MSG_STKT_UPDATE;
458 else
459 *msg_type = PEER_MSG_STKT_INCUPDATE;
460 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200461}
Emeric Brun2b920a12010-09-23 18:30:22 +0200462/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200463 * This prepare the data update message on the stick session <ts>, <st> is the considered
464 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800465 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200466 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
467 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200468 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100469static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200470{
471 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200472 unsigned short datalen;
473 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200474 unsigned int data_type;
475 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100476 struct stksess *ts;
477 struct shared_table *st;
478 unsigned int updateid;
479 int use_identifier;
480 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200481 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100482
483 ts = p->updt.stksess;
484 st = p->updt.shared_table;
485 updateid = p->updt.updateid;
486 use_identifier = p->updt.use_identifier;
487 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200488 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200489
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200490 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200491
Emeric Brun2b920a12010-09-23 18:30:22 +0200492 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200493
494 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200495 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200496 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200497 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200498
499 /* encode update identifier if needed */
500 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200501 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200502 memcpy(cursor, &netinteger, sizeof(netinteger));
503 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200504 }
505
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200506 if (use_timed) {
507 netinteger = htonl(tick_remain(now_ms, ts->expire));
508 memcpy(cursor, &netinteger, sizeof(netinteger));
509 cursor += sizeof(netinteger);
510 }
511
Emeric Brunb3971ab2015-05-12 18:49:09 +0200512 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200513 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200514 int stlen = strlen((char *)ts->key.key);
515
Emeric Brunb3971ab2015-05-12 18:49:09 +0200516 intencode(stlen, &cursor);
517 memcpy(cursor, ts->key.key, stlen);
518 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200519 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200520 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200521 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200522 memcpy(cursor, &netinteger, sizeof(netinteger));
523 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200524 }
525 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 memcpy(cursor, ts->key.key, st->table->key_size);
527 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200528 }
529
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100530 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200532 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200533
Emeric Brun94900952015-06-11 18:25:54 +0200534 data_ptr = stktable_data_ptr(st->table, ts, data_type);
535 if (data_ptr) {
536 switch (stktable_data_types[data_type].std_type) {
537 case STD_T_SINT: {
538 int data;
539
540 data = stktable_data_cast(data_ptr, std_t_sint);
541 intencode(data, &cursor);
542 break;
543 }
544 case STD_T_UINT: {
545 unsigned int data;
546
547 data = stktable_data_cast(data_ptr, std_t_uint);
548 intencode(data, &cursor);
549 break;
550 }
551 case STD_T_ULL: {
552 unsigned long long data;
553
554 data = stktable_data_cast(data_ptr, std_t_ull);
555 intencode(data, &cursor);
556 break;
557 }
558 case STD_T_FRQP: {
559 struct freq_ctr_period *frqp;
560
561 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
562 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
563 intencode(frqp->curr_ctr, &cursor);
564 intencode(frqp->prev_ctr, &cursor);
565 break;
566 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200567 case STD_T_DICT: {
568 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200569 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200570 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200571 char *beg, *end;
572 size_t value_len, data_len;
573 struct dcache *dc;
574
575 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailled9203ca2019-11-13 17:50:34 +0100576 if (!de) {
577 /* No entry */
578 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200579 break;
Frédéric Lécailled9203ca2019-11-13 17:50:34 +0100580 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200581
582 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200583 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200584 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200585 if (cached_de == &cde.entry) {
586 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
587 break;
588 /* Encode the length of the remaining data -> 1 */
589 intencode(1, &cursor);
590 /* Encode the cache entry ID */
591 intencode(cde.id + 1, &cursor);
592 }
593 else {
594 /* Leave enough room to encode the remaining data length. */
595 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
596 /* Encode the dictionary entry key */
597 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200598 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200599 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200600 intencode(value_len, &end);
601 /* Copy the data */
602 memcpy(end, de->value.key, value_len);
603 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200604 /* Encode the length of the data */
605 data_len = end - beg;
606 intencode(data_len, &cursor);
607 memmove(cursor, beg, data_len);
608 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200609 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200610 break;
611 }
Emeric Brun94900952015-06-11 18:25:54 +0200612 }
613 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200614 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100615 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200616
617 /* Compute datalen */
618 datalen = (cursor - datamsg);
619
620 /* prepare message header */
621 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200622 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200623 cursor = &msg[2];
624 intencode(datalen, &cursor);
625
626 /* move data after header */
627 memmove(cursor, datamsg, datalen);
628
629 /* return header size + data_len */
630 return (cursor - msg) + datalen;
631}
632
633/*
634 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800635 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200636 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
637 * check size)
638 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100639static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200640{
641 int len;
642 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200643 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200644 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200645 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200646 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100647 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200648
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100649 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200650 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200651
652 /* Encode data */
653
654 /* encode local id */
655 intencode(st->local_id, &cursor);
656
657 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100658 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200659 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100660 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200661 cursor += len;
662
663 /* encode table type */
664
Emeric Brunc329cd02020-06-02 11:17:42 +0200665 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200666
667 /* encode table key size */
668 intencode(st->table->key_size, &cursor);
669
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200670 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200671 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200672 /* encode available known data types in table */
673 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
674 if (st->table->data_ofs[data_type]) {
675 switch (stktable_data_types[data_type].std_type) {
676 case STD_T_SINT:
677 case STD_T_UINT:
678 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200679 case STD_T_DICT:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200680 data |= 1 << data_type;
681 break;
Emeric Brun94900952015-06-11 18:25:54 +0200682 case STD_T_FRQP:
683 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200684 intencode(data_type, &chunkq);
685 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200686 break;
687 }
688 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200689 }
690 intencode(data, &cursor);
691
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200692 /* Encode stick-table entries duration. */
693 intencode(st->table->expire, &cursor);
694
695 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 chunk->data = chunkq - chunkp;
697 memcpy(cursor, chunk->area, chunk->data);
698 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200699 }
700
Emeric Brunb3971ab2015-05-12 18:49:09 +0200701 /* Compute datalen */
702 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200703
Emeric Brunb3971ab2015-05-12 18:49:09 +0200704 /* prepare message header */
705 msg[0] = PEER_MSG_CLASS_STICKTABLE;
706 msg[1] = PEER_MSG_STKT_DEFINE;
707 cursor = &msg[2];
708 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200709
Emeric Brunb3971ab2015-05-12 18:49:09 +0200710 /* move data after header */
711 memmove(cursor, datamsg, datalen);
712
713 /* return header size + data_len */
714 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200715}
716
Emeric Brunb3971ab2015-05-12 18:49:09 +0200717/*
718 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
719 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800720 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200721 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
722 * check size)
723 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100724static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200725{
726 unsigned short datalen;
727 char *cursor, *datamsg;
728 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100729 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200730
Frédéric Lécaille39143342019-05-24 14:32:27 +0200731 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200732
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100733 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200734 intencode(st->remote_id, &cursor);
735 netinteger = htonl(st->last_get);
736 memcpy(cursor, &netinteger, sizeof(netinteger));
737 cursor += sizeof(netinteger);
738
739 /* Compute datalen */
740 datalen = (cursor - datamsg);
741
742 /* prepare message header */
743 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200744 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200745 cursor = &msg[2];
746 intencode(datalen, &cursor);
747
748 /* move data after header */
749 memmove(cursor, datamsg, datalen);
750
751 /* return header size + data_len */
752 return (cursor - msg) + datalen;
753}
Emeric Brun2b920a12010-09-23 18:30:22 +0200754
755/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200756 * Function to deinit connected peer
757 */
758void __peer_session_deinit(struct peer *peer)
759{
760 struct stream_interface *si;
761 struct stream *s;
762 struct peers *peers;
763
764 if (!peer->appctx)
765 return;
766
767 si = peer->appctx->owner;
768 if (!si)
769 return;
770
771 s = si_strm(si);
772 if (!s)
773 return;
774
775 peers = strm_fe(s)->parent;
776 if (!peers)
777 return;
778
779 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
780 HA_ATOMIC_SUB(&connected_peers, 1);
781
782 HA_ATOMIC_SUB(&active_peers, 1);
783
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200784 flush_dcache(peer);
785
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200786 /* Re-init current table pointers to force announcement on re-connect */
787 peer->remote_table = peer->last_local_table = NULL;
788 peer->appctx = NULL;
789 if (peer->flags & PEER_F_LEARN_ASSIGN) {
790 /* unassign current peer for learning */
791 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
792 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
793
794 /* reschedule a resync */
795 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
796 }
797 /* reset teaching and learning flags to 0 */
798 peer->flags &= PEER_TEACH_RESET;
799 peer->flags &= PEER_LEARN_RESET;
800 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
801}
802
803/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200804 * Callback to release a session with a peer
805 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200806static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200807{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200808 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200809
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100810 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100811 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200812 return;
813
814 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200815 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100816 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200817 if (peer->appctx == appctx)
818 __peer_session_deinit(peer);
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +0200819 peer->flags &= ~PEER_F_ALIVE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100820 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200821 }
822}
823
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200824/* Retrieve the major and minor versions of peers protocol
825 * announced by a remote peer. <str> is a null-terminated
826 * string with the following format: "<maj_ver>.<min_ver>".
827 */
828static int peer_get_version(const char *str,
829 unsigned int *maj_ver, unsigned int *min_ver)
830{
831 unsigned int majv, minv;
832 const char *pos, *saved;
833 const char *end;
834
835 saved = pos = str;
836 end = str + strlen(str);
837
838 majv = read_uint(&pos, end);
839 if (saved == pos || *pos++ != '.')
840 return -1;
841
842 saved = pos;
843 minv = read_uint(&pos, end);
844 if (saved == pos || pos != end)
845 return -1;
846
847 *maj_ver = majv;
848 *min_ver = minv;
849
850 return 0;
851}
Emeric Brun2b920a12010-09-23 18:30:22 +0200852
853/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100854 * Parse a line terminated by an optional '\r' character, followed by a mandatory
855 * '\n' character.
856 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
857 * a line could not be read because the communication channel is closed.
858 */
859static inline int peer_getline(struct appctx *appctx)
860{
861 int n;
862 struct stream_interface *si = appctx->owner;
863
864 n = co_getline(si_oc(si), trash.area, trash.size);
865 if (!n)
866 return 0;
867
868 if (n < 0 || trash.area[n - 1] != '\n') {
869 appctx->st0 = PEER_SESS_ST_END;
870 return -1;
871 }
872
873 if (n > 1 && (trash.area[n - 2] == '\r'))
874 trash.area[n - 2] = 0;
875 else
876 trash.area[n - 1] = 0;
877
878 co_skip(si_oc(si), n);
879
880 return n;
881}
882
883/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100884 * Send a message after having called <peer_prepare_msg> to build it.
885 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
886 * Returns -1 if there was not enough room left to send the message,
887 * any other negative returned value must be considered as an error with an appcxt st0
888 * returned value equal to PEER_SESS_ST_END.
889 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100890static inline int peer_send_msg(struct appctx *appctx,
891 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
892 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100893{
894 int ret, msglen;
895 struct stream_interface *si = appctx->owner;
896
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100897 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100898 if (!msglen) {
899 /* internal error: message does not fit in trash */
900 appctx->st0 = PEER_SESS_ST_END;
901 return 0;
902 }
903
904 /* message to buffer */
905 ret = ci_putblk(si_ic(si), trash.area, msglen);
906 if (ret <= 0) {
907 if (ret == -1) {
908 /* No more write possible */
909 si_rx_room_blk(si);
910 return -1;
911 }
912 appctx->st0 = PEER_SESS_ST_END;
913 }
914
915 return ret;
916}
917
918/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100919 * Send a hello message.
920 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
921 * Returns -1 if there was not enough room left to send the message,
922 * any other negative returned value must be considered as an error with an appcxt st0
923 * returned value equal to PEER_SESS_ST_END.
924 */
925static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
926{
927 struct peer_prep_params p = {
928 .hello.peer = peer,
929 };
930
931 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
932}
933
934/*
935 * Send a success peer handshake status message.
936 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
937 * Returns -1 if there was not enough room left to send the message,
938 * any other negative returned value must be considered as an error with an appcxt st0
939 * returned value equal to PEER_SESS_ST_END.
940 */
941static inline int peer_send_status_successmsg(struct appctx *appctx)
942{
943 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
944}
945
946/*
947 * Send a peer handshake status error message.
948 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
949 * Returns -1 if there was not enough room left to send the message,
950 * any other negative returned value must be considered as an error with an appcxt st0
951 * returned value equal to PEER_SESS_ST_END.
952 */
953static inline int peer_send_status_errormsg(struct appctx *appctx)
954{
955 struct peer_prep_params p = {
956 .error_status.st1 = appctx->st1,
957 };
958
959 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
960}
961
962/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100963 * Send a stick-table switch message.
964 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
965 * Returns -1 if there was not enough room left to send the message,
966 * any other negative returned value must be considered as an error with an appcxt st0
967 * returned value equal to PEER_SESS_ST_END.
968 */
969static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
970{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100971 struct peer_prep_params p = {
972 .swtch.shared_table = st,
973 };
974
975 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100976}
977
978/*
979 * Send a stick-table update acknowledgement message.
980 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
981 * Returns -1 if there was not enough room left to send the message,
982 * any other negative returned value must be considered as an error with an appcxt st0
983 * returned value equal to PEER_SESS_ST_END.
984 */
985static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
986{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100987 struct peer_prep_params p = {
988 .ack.shared_table = st,
989 };
990
991 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100992}
993
994/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100995 * Send a stick-table update message.
996 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
997 * Returns -1 if there was not enough room left to send the message,
998 * any other negative returned value must be considered as an error with an appcxt st0
999 * returned value equal to PEER_SESS_ST_END.
1000 */
1001static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1002 unsigned int updateid, int use_identifier, int use_timed)
1003{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001004 struct peer_prep_params p = {
1005 .updt.stksess = ts,
1006 .updt.shared_table = st,
1007 .updt.updateid = updateid,
1008 .updt.use_identifier = use_identifier,
1009 .updt.use_timed = use_timed,
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001010 .updt.peer = appctx->ctx.peers.ptr,
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001011 };
1012
1013 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001014}
1015
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001016/*
1017 * Build a peer protocol control class message.
1018 * Returns the number of written bytes used to build the message if succeeded,
1019 * 0 if not.
1020 */
1021static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1022{
1023 if (size < sizeof p->control.head)
1024 return 0;
1025
1026 msg[0] = p->control.head[0];
1027 msg[1] = p->control.head[1];
1028
1029 return 2;
1030}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001031
1032/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001033 * Send a stick-table synchronization request message.
1034 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1035 * Returns -1 if there was not enough room left to send the message,
1036 * any other negative returned value must be considered as an error with an appctx st0
1037 * returned value equal to PEER_SESS_ST_END.
1038 */
1039static inline int peer_send_resync_reqmsg(struct appctx *appctx)
1040{
1041 struct peer_prep_params p = {
1042 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1043 };
1044
1045 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1046}
1047
1048/*
1049 * Send a stick-table synchronization confirmation message.
1050 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1051 * Returns -1 if there was not enough room left to send the message,
1052 * any other negative returned value must be considered as an error with an appctx st0
1053 * returned value equal to PEER_SESS_ST_END.
1054 */
1055static inline int peer_send_resync_confirmsg(struct appctx *appctx)
1056{
1057 struct peer_prep_params p = {
1058 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1059 };
1060
1061 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1062}
1063
1064/*
1065 * Send a stick-table synchronization finished message.
1066 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1067 * Returns -1 if there was not enough room left to send the message,
1068 * any other negative returned value must be considered as an error with an appctx st0
1069 * returned value equal to PEER_SESS_ST_END.
1070 */
Emeric Brundeda2a62020-03-16 10:51:01 +01001071static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001072{
1073 struct peer_prep_params p = {
1074 .control.head = { PEER_MSG_CLASS_CONTROL, },
1075 };
1076
Emeric Brundeda2a62020-03-16 10:51:01 +01001077 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001078 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1079
1080 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1081}
1082
1083/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001084 * Send a heartbeat message.
1085 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1086 * Returns -1 if there was not enough room left to send the message,
1087 * any other negative returned value must be considered as an error with an appctx st0
1088 * returned value equal to PEER_SESS_ST_END.
1089 */
1090static inline int peer_send_heartbeatmsg(struct appctx *appctx)
1091{
1092 struct peer_prep_params p = {
1093 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1094 };
1095
1096 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1097}
1098
1099/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001100 * Build a peer protocol error class message.
1101 * Returns the number of written bytes used to build the message if succeeded,
1102 * 0 if not.
1103 */
1104static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1105{
1106 if (size < sizeof p->error.head)
1107 return 0;
1108
1109 msg[0] = p->error.head[0];
1110 msg[1] = p->error.head[1];
1111
1112 return 2;
1113}
1114
1115/*
1116 * Send a "size limit reached" error message.
1117 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1118 * Returns -1 if there was not enough room left to send the message,
1119 * any other negative returned value must be considered as an error with an appctx st0
1120 * returned value equal to PEER_SESS_ST_END.
1121 */
1122static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1123{
1124 struct peer_prep_params p = {
1125 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1126 };
1127
1128 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1129}
1130
1131/*
1132 * Send a "peer protocol" error message.
1133 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1134 * Returns -1 if there was not enough room left to send the message,
1135 * any other negative returned value must be considered as an error with an appctx st0
1136 * returned value equal to PEER_SESS_ST_END.
1137 */
1138static inline int peer_send_error_protomsg(struct appctx *appctx)
1139{
1140 struct peer_prep_params p = {
1141 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1142 };
1143
1144 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1145}
1146
1147/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001148 * Function used to lookup for recent stick-table updates associated with
1149 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1150 */
1151static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1152{
1153 struct eb32_node *eb;
1154
1155 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1156 if (!eb) {
1157 eb = eb32_first(&st->table->updates);
Emeric Brun129d8fa2021-04-28 11:48:15 +02001158 if (!eb || (eb->key == st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001159 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1160 return NULL;
1161 }
1162 }
1163
Emeric Brun129d8fa2021-04-28 11:48:15 +02001164 /* if distance between the last pushed and the retrieved key
1165 * is greater than the distance last_pushed and the local_update
1166 * this means we are beyond localupdate.
1167 */
1168 if ((eb->key - st->last_pushed) > (st->table->localupdate - st->last_pushed)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001169 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1170 return NULL;
1171 }
1172
1173 return eb32_entry(eb, struct stksess, upd);
1174}
1175
1176/*
1177 * Function used to lookup for recent stick-table updates associated with
1178 * <st> shared stick-table during teach state 1 step.
1179 */
1180static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1181{
1182 struct eb32_node *eb;
1183
1184 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1185 if (!eb) {
1186 st->flags |= SHTABLE_F_TEACH_STAGE1;
1187 eb = eb32_first(&st->table->updates);
1188 if (eb)
1189 st->last_pushed = eb->key - 1;
1190 return NULL;
1191 }
1192
1193 return eb32_entry(eb, struct stksess, upd);
1194}
1195
1196/*
1197 * Function used to lookup for recent stick-table updates associated with
1198 * <st> shared stick-table during teach state 2 step.
1199 */
1200static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1201{
1202 struct eb32_node *eb;
1203
1204 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1205 if (!eb || eb->key > st->teaching_origin) {
1206 st->flags |= SHTABLE_F_TEACH_STAGE2;
1207 return NULL;
1208 }
1209
1210 return eb32_entry(eb, struct stksess, upd);
1211}
1212
1213/*
1214 * Generic function to emit update messages for <st> stick-table when a lesson must
1215 * be taught to the peer <p>.
1216 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1217 * this function, 0 if not.
1218 *
1219 * This function temporary unlock/lock <st> when it sends stick-table updates or
1220 * when decrementing its refcount in case of any error when it sends this updates.
1221 *
1222 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1223 * Returns -1 if there was not enough room left to send the message,
1224 * any other negative returned value must be considered as an error with an appcxt st0
1225 * returned value equal to PEER_SESS_ST_END.
1226 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1227 * unlocked if not already locked when entering this function.
1228 */
1229static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1230 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1231 struct shared_table *st, int locked)
1232{
1233 int ret, new_pushed, use_timed;
1234
1235 ret = 1;
1236 use_timed = 0;
1237 if (st != p->last_local_table) {
1238 ret = peer_send_switchmsg(st, appctx);
1239 if (ret <= 0)
1240 return ret;
1241
1242 p->last_local_table = st;
1243 }
1244
1245 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1246 use_timed = !(p->flags & PEER_F_DWNGRD);
1247
1248 /* We force new pushed to 1 to force identifier in update message */
1249 new_pushed = 1;
1250
1251 if (!locked)
1252 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1253
1254 while (1) {
1255 struct stksess *ts;
1256 unsigned updateid;
1257
1258 /* push local updates */
1259 ts = peer_stksess_lookup(st);
1260 if (!ts)
1261 break;
1262
1263 updateid = ts->upd.key;
1264 ts->ref_cnt++;
1265 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1266
1267 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1268 if (ret <= 0) {
1269 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1270 ts->ref_cnt--;
1271 if (!locked)
1272 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1273 return ret;
1274 }
1275
1276 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1277 ts->ref_cnt--;
1278 st->last_pushed = updateid;
1279
1280 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1281 (int)(st->last_pushed - st->table->commitupdate) > 0)
1282 st->table->commitupdate = st->last_pushed;
1283
1284 /* identifier may not needed in next update message */
1285 new_pushed = 0;
1286 }
1287
1288 out:
1289 if (!locked)
1290 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1291 return 1;
1292}
1293
1294/*
1295 * Function to emit update messages for <st> stick-table when a lesson must
1296 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1297 *
1298 * Note that <st> shared stick-table is locked when calling this function.
1299 *
1300 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1301 * Returns -1 if there was not enough room left to send the message,
1302 * any other negative returned value must be considered as an error with an appcxt st0
1303 * returned value equal to PEER_SESS_ST_END.
1304 */
1305static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1306 struct shared_table *st)
1307{
1308 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1309}
1310
1311/*
1312 * Function to emit update messages for <st> stick-table when a lesson must
1313 * be taught to the peer <p> during teach state 1 step.
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_stage1_msgs(struct appctx *appctx, struct peer *p,
1321 struct shared_table *st)
1322{
1323 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
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_stage2_msgs(struct appctx *appctx, struct peer *p,
1336 struct shared_table *st)
1337{
1338 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1339}
1340
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001341
1342/*
1343 * Function used to parse a stick-table update message after it has been received
1344 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1345 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1346 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1347 * was encountered.
1348 * <exp> must be set if the stick-table entry expires.
1349 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1350 * messages, in this case the stick-table udpate message is received with a stick-table
1351 * update ID.
1352 * <totl> is the length of the stick-table update message computed upon receipt.
1353 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001354static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1355 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001356{
1357 struct stream_interface *si = appctx->owner;
1358 struct shared_table *st = p->remote_table;
1359 struct stksess *ts, *newts;
1360 uint32_t update;
1361 int expire;
1362 unsigned int data_type;
1363 void *data_ptr;
1364
1365 /* Here we have data message */
1366 if (!st)
1367 goto ignore_msg;
1368
1369 expire = MS_TO_TICKS(st->table->expire);
1370
1371 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001372 if (msg_len < sizeof(update))
1373 goto malformed_exit;
1374
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001375 memcpy(&update, *msg_cur, sizeof(update));
1376 *msg_cur += sizeof(update);
1377 st->last_get = htonl(update);
1378 }
1379 else {
1380 st->last_get++;
1381 }
1382
1383 if (exp) {
1384 size_t expire_sz = sizeof expire;
1385
Willy Tarreau1e82a142019-01-29 11:08:06 +01001386 if (*msg_cur + expire_sz > msg_end)
1387 goto malformed_exit;
1388
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001389 memcpy(&expire, *msg_cur, expire_sz);
1390 *msg_cur += expire_sz;
1391 expire = ntohl(expire);
1392 }
1393
1394 newts = stksess_new(st->table, NULL);
1395 if (!newts)
1396 goto ignore_msg;
1397
1398 if (st->table->type == SMP_T_STR) {
1399 unsigned int to_read, to_store;
1400
1401 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001402 if (!*msg_cur)
1403 goto malformed_free_newts;
1404
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001405 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001406 if (*msg_cur + to_store > msg_end)
1407 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001408
1409 memcpy(newts->key.key, *msg_cur, to_store);
1410 newts->key.key[to_store] = 0;
1411 *msg_cur += to_read;
1412 }
1413 else if (st->table->type == SMP_T_SINT) {
1414 unsigned int netinteger;
1415
Willy Tarreau1e82a142019-01-29 11:08:06 +01001416 if (*msg_cur + sizeof(netinteger) > msg_end)
1417 goto malformed_free_newts;
1418
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001419 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1420 netinteger = ntohl(netinteger);
1421 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1422 *msg_cur += sizeof(netinteger);
1423 }
1424 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001425 if (*msg_cur + st->table->key_size > msg_end)
1426 goto malformed_free_newts;
1427
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001428 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1429 *msg_cur += st->table->key_size;
1430 }
1431
1432 /* lookup for existing entry */
1433 ts = stktable_set_entry(st->table, newts);
1434 if (ts != newts) {
1435 stksess_free(st->table, newts);
1436 newts = NULL;
1437 }
1438
1439 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1440
1441 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001442 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001443
1444 if (!((1 << data_type) & st->remote_data))
1445 continue;
1446
Willy Tarreau1e82a142019-01-29 11:08:06 +01001447 decoded_int = intdecode(msg_cur, msg_end);
1448 if (!*msg_cur)
1449 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001450
Willy Tarreau1e82a142019-01-29 11:08:06 +01001451 switch (stktable_data_types[data_type].std_type) {
1452 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001453 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1454 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001455 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001456 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001457
Willy Tarreau1e82a142019-01-29 11:08:06 +01001458 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001459 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1460 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001461 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001462 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001463
Willy Tarreau1e82a142019-01-29 11:08:06 +01001464 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001465 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1466 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001467 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001468 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001469
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001470 case STD_T_FRQP: {
1471 struct freq_ctr_period data;
1472
1473 /* First bit is reserved for the freq_ctr_period lock
1474 Note: here we're still protected by the stksess lock
1475 so we don't need to update the update the freq_ctr_period
1476 using its internal lock */
1477
Willy Tarreau1e82a142019-01-29 11:08:06 +01001478 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001479 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001480 if (!*msg_cur)
1481 goto malformed_unlock;
1482
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001483 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001484 if (!*msg_cur)
1485 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001486
1487 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1488 if (data_ptr)
1489 stktable_data_cast(data_ptr, std_t_frqp) = data;
1490 break;
1491 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001492 case STD_T_DICT: {
1493 struct buffer *chunk;
1494 size_t data_len, value_len;
1495 unsigned int id;
1496 struct dict_entry *de;
1497 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001498 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001499
Frédéric Lécailled9203ca2019-11-13 17:50:34 +01001500 if (!decoded_int) {
1501 /* No entry. */
1502 break;
1503 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001504 data_len = decoded_int;
1505 if (*msg_cur + data_len > msg_end)
1506 goto malformed_unlock;
1507
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001508 /* Compute the end of the current data, <msg_end> being at the end of
1509 * the entire message.
1510 */
1511 end = *msg_cur + data_len;
1512 id = intdecode(msg_cur, end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001513 if (!*msg_cur || !id)
1514 goto malformed_unlock;
1515
1516 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001517 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001518 /* Dictionary entry key without value. */
Frédéric Lécaille506fe342020-11-12 19:53:11 +01001519 if (id > dc->max_entries) {
1520 goto malformed_unlock;
1521 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001522 /* IDs sent over the network are numbered from 1. */
1523 de = dc->rx[id - 1].de;
1524 }
1525 else {
1526 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001527 value_len = intdecode(msg_cur, end);
1528 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001529 unlikely(value_len + 1 >= chunk->size))
1530 goto malformed_unlock;
1531
1532 chunk_memcpy(chunk, *msg_cur, value_len);
1533 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001534 *msg_cur += value_len;
1535
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001536 de = dict_insert(&server_name_dict, chunk->area);
1537 dc->rx[id - 1].de = de;
1538 }
1539 if (de) {
1540 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1541 if (data_ptr)
1542 stktable_data_cast(data_ptr, std_t_dict) = de;
1543 }
1544 break;
1545 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001546 }
1547 }
1548 /* Force new expiration */
1549 ts->expire = tick_add(now_ms, expire);
1550
1551 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1552 stktable_touch_remote(st->table, ts, 1);
1553 return 1;
1554
1555 ignore_msg:
1556 /* skip consumed message */
1557 co_skip(si_oc(si), totl);
1558 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001559
1560 malformed_unlock:
1561 /* malformed message */
1562 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1563 stktable_touch_remote(st->table, ts, 1);
1564 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1565 return 0;
1566
1567 malformed_free_newts:
1568 /* malformed message */
1569 stksess_free(st->table, newts);
1570 malformed_exit:
1571 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1572 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001573}
1574
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001575/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001576 * Function used to parse a stick-table update acknowledgement message after it
1577 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1578 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1579 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1580 * was encountered.
1581 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1582 */
1583static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1584 char **msg_cur, char *msg_end)
1585{
1586 /* ack message */
1587 uint32_t table_id ;
1588 uint32_t update;
1589 struct shared_table *st;
1590
Emeric Brun5347ccd2021-03-04 10:27:10 +01001591 /* ignore ack during teaching process */
1592 if (p->flags & PEER_F_TEACH_PROCESS)
1593 return 1;
1594
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001595 table_id = intdecode(msg_cur, msg_end);
1596 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1597 /* malformed message */
1598 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1599 return 0;
1600 }
1601
1602 memcpy(&update, *msg_cur, sizeof(update));
1603 update = ntohl(update);
1604
1605 for (st = p->tables; st; st = st->next) {
1606 if (st->local_id == table_id) {
1607 st->update = update;
1608 break;
1609 }
1610 }
1611
1612 return 1;
1613}
1614
1615/*
1616 * Function used to parse a stick-table switch message after it has been received
1617 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1618 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1619 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1620 * was encountered.
1621 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1622 */
1623static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1624 char **msg_cur, char *msg_end)
1625{
1626 struct shared_table *st;
1627 int table_id;
1628
1629 table_id = intdecode(msg_cur, msg_end);
1630 if (!*msg_cur) {
1631 /* malformed message */
1632 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1633 return 0;
1634 }
1635
1636 p->remote_table = NULL;
1637 for (st = p->tables; st; st = st->next) {
1638 if (st->remote_id == table_id) {
1639 p->remote_table = st;
1640 break;
1641 }
1642 }
1643
1644 return 1;
1645}
1646
1647/*
1648 * Function used to parse a stick-table definition message after it has been received
1649 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1650 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1651 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1652 * was encountered.
1653 * <totl> is the length of the stick-table update message computed upon receipt.
1654 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1655 */
1656static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1657 char **msg_cur, char *msg_end, int totl)
1658{
1659 struct stream_interface *si = appctx->owner;
1660 int table_id_len;
1661 struct shared_table *st;
1662 int table_type;
1663 int table_keylen;
1664 int table_id;
1665 uint64_t table_data;
1666
1667 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001668 if (!*msg_cur)
1669 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001670
1671 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001672 if (!*msg_cur)
1673 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001674
1675 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001676 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1677 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001678
1679 for (st = p->tables; st; st = st->next) {
1680 /* Reset IDs */
1681 if (st->remote_id == table_id)
1682 st->remote_id = 0;
1683
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001684 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1685 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001686 p->remote_table = st;
1687 }
1688
1689 if (!p->remote_table)
1690 goto ignore_msg;
1691
1692 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001693 if (*msg_cur >= msg_end)
1694 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001695
1696 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001697 if (!*msg_cur)
1698 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001699
1700 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001701 if (!*msg_cur)
1702 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001703
1704 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001705 if (!*msg_cur)
1706 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001707
Emeric Brunc329cd02020-06-02 11:17:42 +02001708 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001709 || p->remote_table->table->key_size != table_keylen) {
1710 p->remote_table = NULL;
1711 goto ignore_msg;
1712 }
1713
1714 p->remote_table->remote_data = table_data;
1715 p->remote_table->remote_id = table_id;
1716 return 1;
1717
1718 ignore_msg:
1719 co_skip(si_oc(si), totl);
1720 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001721
1722 malformed_exit:
1723 /* malformed message */
1724 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1725 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001726}
1727
1728/*
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001729 * Receive a stick-table message or pre-parse any other message.
1730 * The message's header will be sent into <msg_head> which must be at least
1731 * <msg_head_sz> bytes long (at least 7 to store 32-bit variable lengths).
1732 * The first two bytes are always read, and the rest is only read if the
1733 * first bytes indicate a stick-table message. If the message is a stick-table
1734 * message, the varint is decoded and the equivalent number of bytes will be
1735 * copied into the trash at trash.area. <totl> is incremented by the number of
1736 * bytes read EVEN IN CASE OF INCOMPLETE MESSAGES.
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001737 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1738 * -1 if there was an error updating the appctx state st0 accordingly.
1739 */
1740static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1741 uint32_t *msg_len, int *totl)
1742{
1743 int reql;
1744 struct stream_interface *si = appctx->owner;
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001745 char *cur;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001746
1747 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1748 if (reql <= 0) /* closed or EOL not found */
1749 goto incomplete;
1750
1751 *totl += reql;
1752
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02001753 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001754 return 1;
1755
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001756 /* This is a stick-table message, let's go on */
1757
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001758 /* Read and Decode message length */
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001759 msg_head += *totl;
1760 msg_head_sz -= *totl;
1761 reql = co_data(si_oc(si)) - *totl;
1762 if (reql > msg_head_sz)
1763 reql = msg_head_sz;
1764
1765 reql = co_getblk(si_oc(si), msg_head, reql, *totl);
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001766 if (reql <= 0) /* closed */
1767 goto incomplete;
1768
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001769 cur = msg_head;
1770 *msg_len = intdecode(&cur, cur + reql);
1771 if (!cur) {
1772 /* the number is truncated, did we read enough ? */
1773 if (reql < msg_head_sz)
1774 goto incomplete;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001775
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001776 /* malformed message */
1777 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1778 return -1;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001779 }
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01001780 *totl += cur - msg_head;
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001781
1782 /* Read message content */
1783 if (*msg_len) {
1784 if (*msg_len > trash.size) {
1785 /* Status code is not success, abort */
1786 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1787 return -1;
1788 }
1789
1790 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1791 if (reql <= 0) /* closed */
1792 goto incomplete;
1793 *totl += reql;
1794 }
1795
1796 return 1;
1797
1798 incomplete:
Willy Tarreau3660fae2020-11-26 17:06:04 +01001799 if (reql < 0 || (si_oc(si)->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1800 /* there was an error or the message was truncated */
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001801 appctx->st0 = PEER_SESS_ST_END;
1802 return -1;
1803 }
1804
1805 return 0;
1806}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001807
1808/*
1809 * Treat the awaited message with <msg_head> as header.*
1810 * Return 1 if succeeded, 0 if not.
1811 */
1812static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1813 char **msg_cur, char *msg_end, int msg_len, int totl)
1814{
1815 struct stream_interface *si = appctx->owner;
1816 struct stream *s = si_strm(si);
1817 struct peers *peers = strm_fe(s)->parent;
1818
1819 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1820 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1821 struct shared_table *st;
1822 /* Reset message: remote need resync */
1823
1824 /* prepare tables fot a global push */
1825 for (st = peer->tables; st; st = st->next) {
Emeric Brunf4ea7a82021-04-28 09:49:33 +02001826 st->teaching_origin = st->last_pushed = st->update;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001827 st->flags = 0;
1828 }
1829
1830 /* reset teaching flags to 0 */
1831 peer->flags &= PEER_TEACH_RESET;
1832
1833 /* flag to start to teach lesson */
1834 peer->flags |= PEER_F_TEACH_PROCESS;
1835 }
1836 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1837 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1838 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1839 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1840 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1841 }
1842 peer->confirm++;
1843 }
1844 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1845 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1846 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1847 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1848
1849 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001850 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001851 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1852 }
1853 peer->confirm++;
1854 }
1855 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1856 struct shared_table *st;
1857
1858 /* If stopping state */
1859 if (stopping) {
1860 /* Close session, push resync no more needed */
1861 peer->flags |= PEER_F_TEACH_COMPLETE;
1862 appctx->st0 = PEER_SESS_ST_END;
1863 return 0;
1864 }
1865 for (st = peer->tables; st; st = st->next) {
1866 st->update = st->last_pushed = st->teaching_origin;
1867 st->flags = 0;
1868 }
1869
1870 /* reset teaching flags to 0 */
1871 peer->flags &= PEER_TEACH_RESET;
1872 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001873 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001874 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille5d072702019-11-06 11:51:26 +01001875 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001876 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001877 }
1878 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1879 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1880 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1881 return 0;
1882 }
1883 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1884 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1885 return 0;
1886 }
1887 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1888 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1889 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1890 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1891 int update, expire;
1892
1893 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1894 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1895 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1896 msg_cur, msg_end, msg_len, totl))
1897 return 0;
1898
1899 }
1900 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1901 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1902 return 0;
1903 }
1904 }
1905 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1906 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1907 return 0;
1908 }
1909
1910 return 1;
1911}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001912
1913
1914/*
1915 * Send any message to <peer> peer.
1916 * Returns 1 if succeeded, or -1 or 0 if failed.
1917 * -1 means an internal error occured, 0 is for a peer protocol error leading
1918 * to a peer state change (from the peer I/O handler point of view).
1919 */
1920static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1921{
1922 int repl;
1923 struct stream_interface *si = appctx->owner;
1924 struct stream *s = si_strm(si);
1925 struct peers *peers = strm_fe(s)->parent;
1926
1927 /* Need to request a resync */
1928 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1929 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1930 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1931
1932 repl = peer_send_resync_reqmsg(appctx);
1933 if (repl <= 0)
1934 return repl;
1935
1936 peers->flags |= PEERS_F_RESYNC_PROCESS;
1937 }
1938
1939 /* Nothing to read, now we start to write */
1940 if (peer->tables) {
1941 struct shared_table *st;
1942 struct shared_table *last_local_table;
1943
1944 last_local_table = peer->last_local_table;
1945 if (!last_local_table)
1946 last_local_table = peer->tables;
1947 st = last_local_table->next;
1948
1949 while (1) {
1950 if (!st)
1951 st = peer->tables;
1952
1953 /* It remains some updates to ack */
1954 if (st->last_get != st->last_acked) {
1955 repl = peer_send_ackmsg(st, appctx);
1956 if (repl <= 0)
1957 return repl;
1958
1959 st->last_acked = st->last_get;
1960 }
1961
1962 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1963 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1964 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
Emeric Brun129d8fa2021-04-28 11:48:15 +02001965 (st->last_pushed != st->table->localupdate)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001966
1967 repl = peer_send_teach_process_msgs(appctx, peer, st);
1968 if (repl <= 0) {
1969 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1970 return repl;
1971 }
1972 }
1973 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1974 }
Emeric Brun2f96bae2021-04-22 18:13:13 +02001975 else if (!(peer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001976 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1977 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1978 if (repl <= 0)
1979 return repl;
1980 }
1981
1982 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1983 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1984 if (repl <= 0)
1985 return repl;
1986 }
1987 }
1988
1989 if (st == last_local_table)
1990 break;
1991 st = st->next;
1992 }
1993 }
1994
1995 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Emeric Brundeda2a62020-03-16 10:51:01 +01001996 repl = peer_send_resync_finishedmsg(appctx, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001997 if (repl <= 0)
1998 return repl;
1999
2000 /* flag finished message sent */
2001 peer->flags |= PEER_F_TEACH_FINISHED;
2002 }
2003
2004 /* Confirm finished or partial messages */
2005 while (peer->confirm) {
2006 repl = peer_send_resync_confirmsg(appctx);
2007 if (repl <= 0)
2008 return repl;
2009
2010 peer->confirm--;
2011 }
2012
2013 return 1;
2014}
2015
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002016/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002017 * Read and parse a first line of a "hello" peer protocol message.
2018 * Returns 0 if could not read a line, -1 if there was a read error or
2019 * the line is malformed, 1 if succeeded.
2020 */
2021static inline int peer_getline_version(struct appctx *appctx,
2022 unsigned int *maj_ver, unsigned int *min_ver)
2023{
2024 int reql;
2025
2026 reql = peer_getline(appctx);
2027 if (!reql)
2028 return 0;
2029
2030 if (reql < 0)
2031 return -1;
2032
2033 /* test protocol */
2034 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2035 appctx->st0 = PEER_SESS_ST_EXIT;
2036 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2037 return -1;
2038 }
2039 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2040 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2041 appctx->st0 = PEER_SESS_ST_EXIT;
2042 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2043 return -1;
2044 }
2045
2046 return 1;
2047}
2048
2049/*
2050 * Read and parse a second line of a "hello" peer protocol message.
2051 * Returns 0 if could not read a line, -1 if there was a read error or
2052 * the line is malformed, 1 if succeeded.
2053 */
2054static inline int peer_getline_host(struct appctx *appctx)
2055{
2056 int reql;
2057
2058 reql = peer_getline(appctx);
2059 if (!reql)
2060 return 0;
2061
2062 if (reql < 0)
2063 return -1;
2064
2065 /* test hostname match */
2066 if (strcmp(localpeer, trash.area) != 0) {
2067 appctx->st0 = PEER_SESS_ST_EXIT;
2068 appctx->st1 = PEER_SESS_SC_ERRHOST;
2069 return -1;
2070 }
2071
2072 return 1;
2073}
2074
2075/*
2076 * Read and parse a last line of a "hello" peer protocol message.
2077 * Returns 0 if could not read a character, -1 if there was a read error or
2078 * the line is malformed, 1 if succeeded.
2079 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2080 */
2081static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2082{
2083 char *p;
2084 int reql;
2085 struct peer *peer;
2086 struct stream_interface *si = appctx->owner;
2087 struct stream *s = si_strm(si);
2088 struct peers *peers = strm_fe(s)->parent;
2089
2090 reql = peer_getline(appctx);
2091 if (!reql)
2092 return 0;
2093
2094 if (reql < 0)
2095 return -1;
2096
2097 /* parse line "<peer name> <pid> <relative_pid>" */
2098 p = strchr(trash.area, ' ');
2099 if (!p) {
2100 appctx->st0 = PEER_SESS_ST_EXIT;
2101 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2102 return -1;
2103 }
2104 *p = 0;
2105
2106 /* lookup known peer */
2107 for (peer = peers->remote; peer; peer = peer->next) {
2108 if (strcmp(peer->id, trash.area) == 0)
2109 break;
2110 }
2111
2112 /* if unknown peer */
2113 if (!peer) {
2114 appctx->st0 = PEER_SESS_ST_EXIT;
2115 appctx->st1 = PEER_SESS_SC_ERRPEER;
2116 return -1;
2117 }
2118 *curpeer = peer;
2119
2120 return 1;
2121}
2122
2123/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002124 * Init <peer> peer after having accepted it at peer protocol level.
2125 */
2126static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2127{
2128 struct shared_table *st;
2129
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002130 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002131 /* Register status code */
2132 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
2133
2134 /* Awake main task */
2135 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2136
2137 /* Init confirm counter */
2138 peer->confirm = 0;
2139
2140 /* Init cursors */
2141 for (st = peer->tables; st ; st = st->next) {
2142 st->last_get = st->last_acked = 0;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002143 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2144 /* if st->update appears to be in future it means
2145 * that the last acked value is very old and we
2146 * remain unconnected a too long time to use this
2147 * acknowlegement as a reset.
2148 * We should update the protocol to be able to
2149 * signal the remote peer that it needs a full resync.
2150 * Here a partial fix consist to set st->update at
2151 * the max past value
2152 */
2153 if ((int)(st->table->localupdate - st->update) < 0)
2154 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002155 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun9a2847b2021-04-20 14:43:46 +02002156 st->flags = 0;
Emeric Brun053ecea2021-02-23 17:08:08 +01002157 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2158 st->table->commitupdate = st->last_pushed;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002159 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002160 }
2161
2162 /* reset teaching and learning flags to 0 */
2163 peer->flags &= PEER_TEACH_RESET;
2164 peer->flags &= PEER_LEARN_RESET;
2165
2166 /* if current peer is local */
2167 if (peer->local) {
2168 /* if current host need resyncfrom local and no process assined */
2169 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2170 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2171 /* assign local peer for a lesson, consider lesson already requested */
2172 peer->flags |= PEER_F_LEARN_ASSIGN;
2173 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2174 }
2175
2176 }
2177 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2178 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2179 /* assign peer for a lesson */
2180 peer->flags |= PEER_F_LEARN_ASSIGN;
2181 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2182 }
2183}
2184
2185/*
2186 * Init <peer> peer after having connected it at peer protocol level.
2187 */
2188static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2189{
2190 struct shared_table *st;
2191
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002192 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002193 /* Init cursors */
2194 for (st = peer->tables; st ; st = st->next) {
2195 st->last_get = st->last_acked = 0;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002196 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
2197 /* if st->update appears to be in future it means
2198 * that the last acked value is very old and we
2199 * remain unconnected a too long time to use this
2200 * acknowlegement as a reset.
2201 * We should update the protocol to be able to
2202 * signal the remote peer that it needs a full resync.
2203 * Here a partial fix consist to set st->update at
2204 * the max past value.
2205 */
2206 if ((int)(st->table->localupdate - st->update) < 0)
2207 st->update = st->table->localupdate + (2147483648U);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002208 st->teaching_origin = st->last_pushed = st->update;
Emeric Brun9a2847b2021-04-20 14:43:46 +02002209 st->flags = 0;
Emeric Brun053ecea2021-02-23 17:08:08 +01002210 if ((int)(st->last_pushed - st->table->commitupdate) > 0)
2211 st->table->commitupdate = st->last_pushed;
Emeric Bruna0394ab2021-02-23 16:50:53 +01002212 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002213 }
2214
2215 /* Init confirm counter */
2216 peer->confirm = 0;
2217
2218 /* reset teaching and learning flags to 0 */
2219 peer->flags &= PEER_TEACH_RESET;
2220 peer->flags &= PEER_LEARN_RESET;
2221
2222 /* If current peer is local */
2223 if (peer->local) {
2224 /* flag to start to teach lesson */
2225 peer->flags |= PEER_F_TEACH_PROCESS;
2226 }
2227 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2228 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2229 /* If peer is remote and resync from remote is needed,
2230 and no peer currently assigned */
2231
2232 /* assign peer for a lesson */
2233 peer->flags |= PEER_F_LEARN_ASSIGN;
2234 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2235 }
2236}
2237
2238/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002239 * IO Handler to handle message exchance with a peer
2240 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002241static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002242{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002243 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002244 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002245 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002246 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002247 int reql = 0;
2248 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002249 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002250 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002251
Joseph Herlant82b2f542018-11-15 12:19:14 -08002252 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002253 if (si_ic(si)->buf.size == 0) {
2254 si_rx_room_blk(si);
2255 goto out;
2256 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002257
Emeric Brun2b920a12010-09-23 18:30:22 +02002258 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002259 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002260switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002261 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002262 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002263 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002264 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002265 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002266 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002267 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002268 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002269 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002270 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2271 if (reql <= 0) {
2272 if (!reql)
2273 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002274 goto switchstate;
2275 }
2276
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002277 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002278 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002279 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002280 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002281 reql = peer_getline_host(appctx);
2282 if (reql <= 0) {
2283 if (!reql)
2284 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002285 goto switchstate;
2286 }
2287
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002288 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002289 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002290 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002291 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002292 reql = peer_getline_last(appctx, &curpeer);
2293 if (reql <= 0) {
2294 if (!reql)
2295 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002296 goto switchstate;
2297 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002298
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002299 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002300 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002301 if (curpeer->local) {
2302 /* Local connection, reply a retry */
2303 appctx->st0 = PEER_SESS_ST_EXIT;
2304 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2305 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002306 }
Emeric Brun80527f52017-06-19 17:46:37 +02002307
2308 /* we're killing a connection, we must apply a random delay before
2309 * retrying otherwise the other end will do the same and we can loop
2310 * for a while.
2311 */
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002312 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002313 peer_session_forceshutdown(curpeer);
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002314 curpeer->heartbeat = TICK_ETERNITY;
Emeric Brun2b920a12010-09-23 18:30:22 +02002315 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002316 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2317 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2318 curpeer->flags |= PEER_F_DWNGRD;
2319 }
2320 else {
2321 curpeer->flags &= ~PEER_F_DWNGRD;
2322 }
2323 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002324 curpeer->appctx = appctx;
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002325 curpeer->flags |= PEER_F_ALIVE;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002326 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002327 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002328 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002329 /* fall through */
2330 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002331 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002332 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002333 if (!curpeer) {
2334 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002335 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002336 if (curpeer->appctx != appctx) {
2337 appctx->st0 = PEER_SESS_ST_END;
2338 goto switchstate;
2339 }
2340 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002341
2342 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002343 if (repl <= 0) {
2344 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002345 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002346 goto switchstate;
2347 }
2348
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002349 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002350
Emeric Brun2b920a12010-09-23 18:30:22 +02002351 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002352 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002353 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002354 goto switchstate;
2355 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002356 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002357 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002358 if (!curpeer) {
2359 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002360 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002361 if (curpeer->appctx != appctx) {
2362 appctx->st0 = PEER_SESS_ST_END;
2363 goto switchstate;
2364 }
2365 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002366
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002367 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002368 if (repl <= 0) {
2369 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002370 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002371 goto switchstate;
2372 }
2373
2374 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002375 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002376 /* fall through */
2377 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002378 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002379 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002380 if (!curpeer) {
2381 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002382 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002383 if (curpeer->appctx != appctx) {
2384 appctx->st0 = PEER_SESS_ST_END;
2385 goto switchstate;
2386 }
2387 }
2388
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002389 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002390 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002391
Frédéric Lécaillece025572019-01-21 13:38:06 +01002392 reql = peer_getline(appctx);
2393 if (!reql)
2394 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002395
Frédéric Lécaillece025572019-01-21 13:38:06 +01002396 if (reql < 0)
2397 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002398
2399 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002400 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002401
2402 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002403 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002404
2405 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002406 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002407 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002408 }
2409 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002410 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2411 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002412 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002413 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002414 goto switchstate;
2415 }
Olivier Houcharded879892019-03-08 18:53:43 +01002416 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002417 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002418 /* fall through */
2419 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002420 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002421 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002422 char *msg_cur = trash.area;
2423 char *msg_end = trash.area;
Willy Tarreaue6ea2a62020-11-13 14:10:20 +01002424 unsigned char msg_head[7]; // 2 + 5 for varint32
Emeric Brun2b920a12010-09-23 18:30:22 +02002425 int totl = 0;
2426
Willy Tarreau2d372c22018-11-05 17:12:27 +01002427 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002428 if (!curpeer) {
2429 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002430 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002431 if (curpeer->appctx != appctx) {
2432 appctx->st0 = PEER_SESS_ST_END;
2433 goto switchstate;
2434 }
2435 }
2436
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002437 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2438 if (reql <= 0) {
2439 if (reql == -1)
2440 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002441 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002442 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002443
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002444 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002445 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002446 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002447
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002448 curpeer->flags |= PEER_F_ALIVE;
2449
Emeric Brun2b920a12010-09-23 18:30:22 +02002450 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002451 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002452 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002453 goto switchstate;
2454
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002455send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002456 if (curpeer->flags & PEER_F_HEARTBEAT) {
2457 curpeer->flags &= ~PEER_F_HEARTBEAT;
2458 repl = peer_send_heartbeatmsg(appctx);
2459 if (repl <= 0) {
2460 if (repl == -1)
2461 goto out;
2462 goto switchstate;
2463 }
Frédéric Lécaille5d072702019-11-06 11:51:26 +01002464 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002465 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002466 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002467 repl = peer_send_msgs(appctx, curpeer);
2468 if (repl <= 0) {
2469 if (repl == -1)
2470 goto out;
2471 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002472 }
2473
Emeric Brun2b920a12010-09-23 18:30:22 +02002474 /* noting more to do */
2475 goto out;
2476 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002477 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002478 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002479 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002480 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002481 if (peer_send_status_errormsg(appctx) == -1)
2482 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002483 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002484 goto switchstate;
2485 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002486 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002487 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002488 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002489 if (peer_send_error_size_limitmsg(appctx) == -1)
2490 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002491 appctx->st0 = PEER_SESS_ST_END;
2492 goto switchstate;
2493 }
2494 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécaille9a88a832019-11-07 15:22:33 +01002495 if (curpeer)
2496 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002497 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002498 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002499 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002500 if (peer_send_error_protomsg(appctx) == -1)
2501 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002502 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002503 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002504 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002505 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002506 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002507 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002508 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002509 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002510 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002511 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002512 curpeer = NULL;
2513 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002514 si_shutw(si);
2515 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002516 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002517 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002518 }
2519 }
2520 }
2521out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002522 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002523
2524 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002525 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002526 return;
2527}
2528
Willy Tarreau30576452015-04-13 13:50:30 +02002529static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002530 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002531 .name = "<PEER>", /* used for logging */
2532 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002533 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002534};
Emeric Brun2b920a12010-09-23 18:30:22 +02002535
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002536
Emeric Brun2b920a12010-09-23 18:30:22 +02002537/*
2538 * Use this function to force a close of a peer session
2539 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002540static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002541{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002542 struct appctx *appctx = peer->appctx;
2543
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002544 /* Note that the peer sessions which have just been created
2545 * (->st0 == PEER_SESS_ST_CONNECT) must not
2546 * be shutdown, if not, the TCP session will never be closed
2547 * and stay in CLOSE_WAIT state after having been closed by
2548 * the remote side.
2549 */
2550 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002551 return;
2552
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002553 if (appctx->applet != &peer_applet)
2554 return;
2555
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002556 __peer_session_deinit(peer);
2557
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002558 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002559 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002560}
2561
Willy Tarreau91d96282015-03-13 15:47:26 +01002562/* Pre-configures a peers frontend to accept incoming connections */
2563void peers_setup_frontend(struct proxy *fe)
2564{
2565 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002566 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002567 fe->maxconn = 0;
2568 fe->conn_retries = CONN_RETRIES;
2569 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002570 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002571 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002572 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002573 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002574}
2575
Emeric Brun2b920a12010-09-23 18:30:22 +02002576/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002577 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002578 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002579static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002580{
Willy Tarreau04b92862017-09-15 11:01:04 +02002581 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002582 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002583 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002584 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002585 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002586 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002587
Frédéric Lécaille54152712021-01-18 15:14:39 +01002588 peer->new_conn++;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002589 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002590 peer->heartbeat = TICK_ETERNITY;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002591 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002592 s = NULL;
2593
Emeric Brun1138fd02017-06-19 12:38:55 +02002594 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002595 if (!appctx)
2596 goto out_close;
2597
2598 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002599 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002600
Willy Tarreau04b92862017-09-15 11:01:04 +02002601 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002602 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002603 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002604 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002605 }
2606
Willy Tarreau87787ac2017-08-28 16:22:54 +02002607 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002608 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002609 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002610 }
2611
Willy Tarreau342bfb12015-04-05 01:35:34 +02002612 /* The tasks below are normally what is supposed to be done by
2613 * fe->accept().
2614 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002615 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002616
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002617 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002618 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002619 appctx_wakeup(appctx);
2620
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002621 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002622 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002623 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002624
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002625 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002626 * pre-initialized connection in si->conn.
2627 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002628 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002629 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002630
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002631 if (unlikely((cs = cs_new(conn)) == NULL))
2632 goto out_free_conn;
2633
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002634 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002635 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2636
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002637 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002638 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2639 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002640 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002641
Emeric Brun2b920a12010-09-23 18:30:22 +02002642 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002643 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002644
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002645 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002646
Emeric Brunb3971ab2015-05-12 18:49:09 +02002647 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002648 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002649 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002650 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002651
2652 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002653out_free_cs:
2654 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002655 out_free_conn:
2656 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002657 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002658 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002659 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002660 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002661 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002662 out_free_appctx:
2663 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002664 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002665 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002666}
2667
2668/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002669 * Task processing function to manage re-connect, peer session
2670 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002671 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002672static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002673{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002674 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002675 struct peer *ps;
2676 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002677
2678 task->expire = TICK_ETERNITY;
2679
Emeric Brunb3971ab2015-05-12 18:49:09 +02002680 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002681 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002682 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002683 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002684 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002685 return NULL;
2686 }
2687
Emeric Brun80527f52017-06-19 17:46:37 +02002688 /* Acquire lock for all peers of the section */
2689 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002690 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002691
Emeric Brun2b920a12010-09-23 18:30:22 +02002692 if (!stopping) {
2693 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002694
Emeric Bruna9cbe952021-04-21 16:06:35 +02002695 /* resync timeout set to TICK_ETERNITY means we just start
2696 * a new process and timer was not initialized.
2697 * We must arm this timer to switch to a request to a remote
2698 * node if incoming connection from old local process never
2699 * comes.
2700 */
2701 if (peers->resync_timeout == TICK_ETERNITY)
2702 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
2703
Emeric Brunb3971ab2015-05-12 18:49:09 +02002704 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2705 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2706 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002707 /* Resync from local peer needed
2708 no peer was assigned for the lesson
2709 and no old local peer found
2710 or resync timeout expire */
2711
2712 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002713 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002714
2715 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002716 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002717 }
2718
2719 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002720 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002721 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002722 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002723 if (!ps->appctx) {
2724 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002725 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002726 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002727 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002728 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002729 tick_is_expired(ps->reconnect, now_ms))) {
2730 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002731 * or previous peer connection established with success
2732 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002733 * and reconnection timer is expired */
2734
2735 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002736 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002737 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002738 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002739 /* If previous session failed during connection
2740 * but reconnection timer is not expired */
2741
2742 /* reschedule task for reconnect */
2743 task->expire = tick_first(task->expire, ps->reconnect);
2744 }
2745 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002746 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002747 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002748 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002749 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2750 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002751 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2752 /* Resync from a remote is needed
2753 * and no peer was assigned for lesson
2754 * and current peer may be up2date */
2755
2756 /* assign peer for the lesson */
2757 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002758 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002759
Willy Tarreau9df94c22016-10-31 18:42:52 +01002760 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002761 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002762 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002763 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002764 int update_to_push = 0;
2765
Emeric Brunb3971ab2015-05-12 18:49:09 +02002766 /* Awake session if there is data to push */
2767 for (st = ps->tables; st ; st = st->next) {
Emeric Brun129d8fa2021-04-28 11:48:15 +02002768 if (st->last_pushed != st->table->localupdate) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002769 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002770 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002771 /* There is no need to send a heartbeat message
2772 * when some updates must be pushed. The remote
2773 * peer will consider <ps> peer as alive when it will
2774 * receive these updates.
2775 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002776 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002777 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002778 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002779 /* We are going to send updates, let's ensure we will
2780 * come back to send heartbeat messages or to reconnect.
2781 */
2782 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002783 appctx_wakeup(ps->appctx);
2784 break;
2785 }
2786 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002787 /* When there are updates to send we do not reconnect
2788 * and do not send heartbeat message either.
2789 */
2790 if (!update_to_push) {
2791 if (tick_is_expired(ps->reconnect, now_ms)) {
2792 if (ps->flags & PEER_F_ALIVE) {
2793 /* This peer was alive during a 'reconnect' period.
2794 * Flag it as not alive again for the next period.
2795 */
2796 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002797 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002798 }
2799 else {
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002800 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Frédéric Lécaillec77aa3b2020-10-14 11:50:26 +02002801 ps->heartbeat = TICK_ETERNITY;
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002802 peer_session_forceshutdown(ps);
Frédéric Lécaille9a88a832019-11-07 15:22:33 +01002803 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002804 }
2805 }
2806 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2807 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2808 ps->flags |= PEER_F_HEARTBEAT;
2809 appctx_wakeup(ps->appctx);
2810 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002811 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002812 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002813 }
2814 /* else do nothing */
2815 } /* SUCCESSCODE */
2816 } /* !ps->peer->local */
2817 } /* for */
2818
2819 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002820 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2821 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2822 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002823 /* Resync from remote peer needed
2824 * no peer was assigned for the lesson
2825 * and resync timeout expire */
2826
2827 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002828 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002829 }
2830
Emeric Brunb3971ab2015-05-12 18:49:09 +02002831 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002832 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002833 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2834 if (!tick_is_expired(peers->resync_timeout, now_ms))
2835 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002836 }
2837 } /* !stopping */
2838 else {
2839 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002840 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002841 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002842 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002843 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002844 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002845 peers->flags |= PEERS_F_DONOTSTOP;
Emeric Brun2b920a12010-09-23 18:30:22 +02002846
Emeric Brun051c52b2021-04-22 18:20:37 +02002847 /* disconnect all connected peers to process a local sync
2848 * this must be done only the first time we are switching
2849 * in stopping state
Emeric Brun80527f52017-06-19 17:46:37 +02002850 */
Emeric Brun051c52b2021-04-22 18:20:37 +02002851 for (ps = peers->remote; ps; ps = ps->next) {
2852 /* we're killing a connection, we must apply a random delay before
2853 * retrying otherwise the other end will do the same and we can loop
2854 * for a while.
2855 */
2856 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
2857 if (ps->appctx) {
2858 peer_session_forceshutdown(ps);
2859 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002860 }
2861 }
2862 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002863
Emeric Brunb3971ab2015-05-12 18:49:09 +02002864 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002865 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002866 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002867 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002868 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002869 peers->flags &= ~PEERS_F_DONOTSTOP;
2870 for (st = ps->tables; st ; st = st->next)
Emeric Brun692e71d2021-04-23 17:02:22 +02002871 _HA_ATOMIC_SUB(&st->table->refcnt, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002872 }
2873 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002874 else if (!ps->appctx) {
2875 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002876 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002877 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2878 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2879 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002880 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002881 * or previous peer connection was successfully established
2882 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002883 * or during previous connect, peer replies a try again statuscode */
2884
Emeric Brun051c52b2021-04-22 18:20:37 +02002885 /* connect to the local peer if we must push a local sync */
2886 if (peers->flags & PEERS_F_DONOTSTOP) {
2887 peer_session_create(peers, ps);
2888 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002889 }
2890 else {
2891 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002892 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002893 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002894 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002895 peers->flags &= ~PEERS_F_DONOTSTOP;
2896 for (st = ps->tables; st ; st = st->next)
Emeric Brun692e71d2021-04-23 17:02:22 +02002897 _HA_ATOMIC_SUB(&st->table->refcnt, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002898 }
2899 }
2900 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002901 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002902 /* current peer connection is active and established
2903 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002904 for (st = ps->tables; st ; st = st->next) {
Emeric Brun129d8fa2021-04-28 11:48:15 +02002905 if (st->last_pushed != st->table->localupdate) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002906 appctx_wakeup(ps->appctx);
2907 break;
2908 }
2909 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002910 }
2911 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002912
2913 /* Release lock for all peers of the section */
2914 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002915 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002916
Emeric Brun2b920a12010-09-23 18:30:22 +02002917 /* Wakeup for re-connect */
2918 return task;
2919}
2920
Emeric Brunb3971ab2015-05-12 18:49:09 +02002921
Emeric Brun2b920a12010-09-23 18:30:22 +02002922/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002923 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002924 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002925int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002926{
Emeric Brun2b920a12010-09-23 18:30:22 +02002927 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002928
Emeric Brun2b920a12010-09-23 18:30:22 +02002929 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002930 peers->peers_fe->maxconn += 3;
2931 }
2932
Emeric Brunc60def82017-09-27 14:59:38 +02002933 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002934 if (!peers->sync_task)
2935 return 0;
2936
Emeric Brunb3971ab2015-05-12 18:49:09 +02002937 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002938 peers->sync_task->context = (void *)peers;
2939 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2940 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002941 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002942}
2943
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002944/*
2945 * Allocate a cache a dictionary entries used upon transmission.
2946 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002947static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002948{
2949 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002950 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002951
2952 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002953 entries = calloc(max_entries, sizeof *entries);
2954 if (!d || !entries)
2955 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002956
2957 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002958 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002959 d->cached_entries = EB_ROOT_UNIQUE;
2960 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002961
2962 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002963
2964 err:
2965 free(d);
2966 free(entries);
2967 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002968}
2969
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002970static void free_dcache_tx(struct dcache_tx *dc)
2971{
2972 free(dc->entries);
2973 dc->entries = NULL;
2974 free(dc);
2975}
2976
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002977/*
2978 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
2979 * as maximum of entries.
2980 * Return the dictionay cache if succeeded, NULL if not.
2981 * Must be deallocated calling free_dcache().
2982 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002983static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002984{
2985 struct dcache_tx *dc_tx;
2986 struct dcache *dc;
2987 struct dcache_rx *dc_rx;
2988
2989 dc = calloc(1, sizeof *dc);
2990 dc_tx = new_dcache_tx(max_entries);
2991 dc_rx = calloc(max_entries, sizeof *dc_rx);
2992 if (!dc || !dc_tx || !dc_rx)
2993 goto err;
2994
2995 dc->tx = dc_tx;
2996 dc->rx = dc_rx;
2997 dc->max_entries = max_entries;
2998
2999 return dc;
3000
3001 err:
3002 free(dc);
3003 free(dc_tx);
3004 free(dc_rx);
3005 return NULL;
3006}
3007
3008/*
3009 * Deallocate a cache of dictionary entries.
3010 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003011static inline void free_dcache(struct dcache *dc)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003012{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003013 free_dcache_tx(dc->tx);
3014 dc->tx = NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003015 free(dc->rx); dc->rx = NULL;
3016 free(dc);
3017}
3018
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003019
3020/*
3021 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
3022 * entries used upon transmission.
3023 * Return the entry if found, NULL if not.
3024 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003025static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
3026 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003027{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003028 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003029}
3030
3031/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003032 * Flush <dc> cache.
3033 * Always succeeds.
3034 */
3035static inline void flush_dcache(struct peer *peer)
3036{
3037 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003038 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003039
Frédéric Lécailled31dc822020-11-12 21:01:54 +01003040 for (i = 0; i < dc->max_entries; i++) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003041 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécailled31dc822020-11-12 21:01:54 +01003042 dc->tx->entries[i].key = NULL;
3043 }
3044 dc->tx->prev_lookup = NULL;
3045 dc->tx->lru_key = 0;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003046
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003047 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003048}
3049
3050/*
3051 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
3052 * with information provided by <i> dictionary cache entry (especially the value
3053 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003054 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003055 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003056static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003057{
3058 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003059 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003060
3061 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003062
3063 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
3064 o = dc_tx->prev_lookup;
3065 } else {
3066 o = dcache_tx_lookup_value(dc_tx, i);
3067 if (o) {
3068 /* Save it */
3069 dc_tx->prev_lookup = o;
3070 }
3071 }
3072
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003073 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003074 /* Copy the ID. */
3075 i->id = o - dc->tx->entries;
3076 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003077 }
3078
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003079 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02003080 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003081
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003082 ebpt_delete(o);
3083 o->key = i->entry.key;
3084 ebpt_insert(&dc_tx->cached_entries, o);
3085 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003086
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003087 /* Update the index for the next entry to put in cache */
3088 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003089
Frédéric Lécaille74167b22019-05-28 19:02:42 +02003090 return o;
3091}
Emeric Brunb3971ab2015-05-12 18:49:09 +02003092
3093/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02003094 * Allocate a dictionary cache for each peer of <peers> section.
3095 * Return 1 if succeeded, 0 if not.
3096 */
3097int peers_alloc_dcache(struct peers *peers)
3098{
3099 struct peer *p;
3100
3101 for (p = peers->remote; p; p = p->next) {
3102 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3103 if (!p->dcache)
3104 return 0;
3105 }
3106
3107 return 1;
3108}
3109
3110/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003111 * Function used to register a table for sync on a group of peers
3112 *
3113 */
3114void peers_register_table(struct peers *peers, struct stktable *table)
3115{
3116 struct shared_table *st;
3117 struct peer * curpeer;
3118 int id = 0;
3119
3120 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003121 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02003122 st->table = table;
3123 st->next = curpeer->tables;
3124 if (curpeer->tables)
3125 id = curpeer->tables->local_id;
3126 st->local_id = id + 1;
3127
Emeric Brun692e71d2021-04-23 17:02:22 +02003128 /* If peer is local we inc table
3129 * refcnt to protect against flush
3130 * until this process pushed all
3131 * table content to the new one
3132 */
3133 if (curpeer->local)
3134 _HA_ATOMIC_ADD(&st->table->refcnt, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02003135 curpeer->tables = st;
3136 }
3137
3138 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02003139}
3140
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003141/*
3142 * Parse the "show peers" command arguments.
3143 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3144 * error message.
3145 */
3146static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3147{
3148 appctx->ctx.cfgpeers.target = NULL;
3149
3150 if (*args[2]) {
3151 struct peers *p;
3152
3153 for (p = cfg_peers; p; p = p->next) {
3154 if (!strcmp(p->id, args[2])) {
3155 appctx->ctx.cfgpeers.target = p;
3156 break;
3157 }
3158 }
3159
3160 if (!p) {
3161 appctx->ctx.cli.severity = LOG_ERR;
3162 appctx->ctx.cli.msg = "No such peers\n";
3163 appctx->st0 = CLI_ST_PRINT;
3164 return 1;
3165 }
3166 }
3167
3168 return 0;
3169}
3170
3171/*
3172 * This function dumps the peer state information of <peers> "peers" section.
3173 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3174 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3175 */
3176static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3177{
3178 struct tm tm;
3179
3180 get_localtime(peers->last_change, &tm);
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003181 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 +02003182 peers,
3183 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3184 tm.tm_hour, tm.tm_min, tm.tm_sec,
3185 peers->id, peers->state, peers->flags,
3186 peers->resync_timeout ?
3187 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3188 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003189 TICKS_TO_MS(1000)) : "<NEVER>",
3190 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003191
3192 if (ci_putchk(si_ic(si), msg) == -1) {
3193 si_rx_room_blk(si);
3194 return 0;
3195 }
3196
3197 return 1;
3198}
3199
3200/*
3201 * This function dumps <peer> state information.
3202 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3203 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3204 */
3205static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
3206{
3207 struct connection *conn;
3208 char pn[INET6_ADDRSTRLEN];
3209 struct stream_interface *peer_si;
3210 struct stream *peer_s;
3211 struct appctx *appctx;
3212 struct shared_table *st;
3213
3214 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécaille85e72122020-10-05 12:33:07 +02003215 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 +02003216 peer, peer->id,
3217 peer->local ? "local" : "remote",
Frédéric Lécaille85e72122020-10-05 12:33:07 +02003218 peer->appctx ? "active" : "inactive",
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003219 pn, get_host_port(&peer->addr),
3220 statuscode_str(peer->statuscode),
3221 peer->reconnect ?
3222 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3223 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
3224 TICKS_TO_MS(1000)) : "<NEVER>",
Frédéric Lécaille9a88a832019-11-07 15:22:33 +01003225 peer->confirm, peer->tx_hbt, peer->rx_hbt,
3226 peer->no_hbt, peer->new_conn, peer->proto_err);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003227
3228 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3229
3230 appctx = peer->appctx;
3231 if (!appctx)
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003232 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003233
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003234 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3235 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003236
3237 peer_si = peer->appctx->owner;
3238 if (!peer_si)
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003239 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003240
3241 peer_s = si_strm(peer_si);
3242 if (!peer_s)
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003243 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003244
3245 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3246
3247 conn = objt_conn(strm_orig(peer_s));
3248 if (conn)
3249 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3250
3251 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3252 case AF_INET:
3253 case AF_INET6:
3254 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(&conn->addr.from));
3255 break;
3256 case AF_UNIX:
3257 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3258 break;
3259 }
3260
3261 if (conn)
3262 conn_get_to_addr(conn);
3263
3264 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
3265 case AF_INET:
3266 case AF_INET6:
3267 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(&conn->addr.to));
3268 break;
3269 case AF_UNIX:
3270 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3271 break;
3272 }
3273
Frédéric Lécaille586f9f42019-11-06 10:41:03 +01003274 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003275 if (peer->remote_table)
3276 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3277 peer->remote_table,
3278 peer->remote_table->table->id,
3279 peer->remote_table->local_id,
3280 peer->remote_table->remote_id);
3281
3282 if (peer->last_local_table)
3283 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3284 peer->last_local_table,
3285 peer->last_local_table->table->id,
3286 peer->last_local_table->local_id,
3287 peer->last_local_table->remote_id);
3288
3289 if (peer->tables) {
3290 chunk_appendf(&trash, "\n shared tables:");
3291 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003292 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003293 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003294 struct dcache *dcache;
3295
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003296 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003297 dcache = peer->dcache;
3298
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003299 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3300 "flags=0x%x remote_data=0x%llx",
3301 st, st->local_id, st->remote_id,
3302 st->flags, (unsigned long long)st->remote_data);
3303 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3304 " teaching_origin=%u update=%u",
3305 st->last_acked, st->last_pushed, st->last_get,
3306 st->teaching_origin, st->update);
3307 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
Emeric Brun692e71d2021-04-23 17:02:22 +02003308 " commitupdate=%u refcnt=%u",
3309 t, t->id, t->update, t->localupdate, t->commitupdate, t->refcnt);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003310 chunk_appendf(&trash, "\n TX dictionary cache:");
3311 count = 0;
3312 for (i = 0; i < dcache->max_entries; i++) {
3313 struct ebpt_node *node;
3314 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003315
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003316 node = &dcache->tx->entries[i];
3317 if (!node->key)
3318 break;
3319
3320 if (!count++)
3321 chunk_appendf(&trash, "\n ");
3322 de = node->key;
3323 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3324 count &= 0x3;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003325 }
3326 chunk_appendf(&trash, "\n RX dictionary cache:");
3327 count = 0;
3328 for (i = 0; i < dcache->max_entries; i++) {
3329 if (!count++)
3330 chunk_appendf(&trash, "\n ");
3331 chunk_appendf(&trash, " %3u -> %s", i,
3332 dcache->rx[i].de ?
3333 (char *)dcache->rx[i].de->value.key : "-");
3334 count &= 0x3;
3335 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003336 }
3337 }
3338
3339 end:
3340 chunk_appendf(&trash, "\n");
3341 if (ci_putchk(si_ic(si), msg) == -1) {
3342 si_rx_room_blk(si);
3343 return 0;
3344 }
3345
3346 return 1;
3347}
3348
3349/*
3350 * This function dumps all the peers of "peers" section.
3351 * Returns 0 if the output buffer is full and needs to be called
3352 * again, non-zero if not. It proceeds in an isolated thread, so
3353 * there is no thread safety issue here.
3354 */
3355static int cli_io_handler_show_peers(struct appctx *appctx)
3356{
3357 int show_all;
3358 int ret = 0, first_peers = 1;
3359 struct stream_interface *si = appctx->owner;
3360
3361 thread_isolate();
3362
3363 show_all = !appctx->ctx.cfgpeers.target;
3364
3365 chunk_reset(&trash);
3366
3367 while (appctx->st2 != STAT_ST_FIN) {
3368 switch (appctx->st2) {
3369 case STAT_ST_INIT:
3370 if (show_all)
3371 appctx->ctx.cfgpeers.peers = cfg_peers;
3372 else
3373 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3374
3375 appctx->st2 = STAT_ST_LIST;
3376 /* fall through */
3377
3378 case STAT_ST_LIST:
3379 if (!appctx->ctx.cfgpeers.peers) {
3380 /* No more peers list. */
3381 appctx->st2 = STAT_ST_END;
3382 }
3383 else {
3384 if (!first_peers)
3385 chunk_appendf(&trash, "\n");
3386 else
3387 first_peers = 0;
3388 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3389 goto out;
3390
3391 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3392 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3393 appctx->st2 = STAT_ST_INFO;
3394 }
3395 break;
3396
3397 case STAT_ST_INFO:
3398 if (!appctx->ctx.cfgpeers.peer) {
3399 /* End of peer list */
3400 if (show_all)
3401 appctx->st2 = STAT_ST_LIST;
3402 else
3403 appctx->st2 = STAT_ST_END;
3404 }
3405 else {
3406 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
3407 goto out;
3408
3409 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3410 }
3411 break;
3412
3413 case STAT_ST_END:
3414 appctx->st2 = STAT_ST_FIN;
3415 break;
3416 }
3417 }
3418 ret = 1;
3419 out:
3420 thread_release();
3421 return ret;
3422}
3423
3424/*
3425 * CLI keywords.
3426 */
3427static struct cli_kw_list cli_kws = {{ }, {
3428 { { "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, },
3429 {},
3430}};
3431
3432/* Register cli keywords */
3433INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3434