blob: 899d5b4691faf5193d7c710d7dfd31e6f5f056cb [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020024#include <haproxy/dict.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020025#include <haproxy/frontend.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020026#include <haproxy/net_helper.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020027#include <haproxy/obj_type-t.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020028#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020029#include <haproxy/tools.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020030#include <haproxy/thread.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020031
32#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010033#include <types/listener.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020034#include <types/peers.h>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020035#include <types/stats.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020036
37#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020038#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020039#include <proto/channel.h>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020040#include <proto/cli.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020041#include <haproxy/fd.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020042#include <proto/log.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020043#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020044#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020045#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020046#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010047#include <proto/signal.h>
48#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020051
52
53/*******************************/
54/* Current peer learning state */
55/*******************************/
56
57/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020058/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020059/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010060#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
61#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
62#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
63#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
64#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
65 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020066
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010067#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
68#define PEERS_RESYNC_FROMLOCAL 0x00000000
69#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
70#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020071
72/***********************************/
73/* Current shared table sync state */
74/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010075#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
76#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020077
78/******************************/
79/* Remote peer teaching state */
80/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010081#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
82#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
83#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
84#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
85#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
86#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
87#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
88#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 +020089
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010090#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
91#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020092
Frédéric Lécaille54bff832019-03-26 10:25:20 +010093#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
94#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010095#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
96
Emeric Brunb3971ab2015-05-12 18:49:09 +020097/*****************************/
98/* Sync message class */
99/*****************************/
100enum {
101 PEER_MSG_CLASS_CONTROL = 0,
102 PEER_MSG_CLASS_ERROR,
103 PEER_MSG_CLASS_STICKTABLE = 10,
104 PEER_MSG_CLASS_RESERVED = 255,
105};
106
107/*****************************/
108/* control message types */
109/*****************************/
110enum {
111 PEER_MSG_CTRL_RESYNCREQ = 0,
112 PEER_MSG_CTRL_RESYNCFINISHED,
113 PEER_MSG_CTRL_RESYNCPARTIAL,
114 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100115 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200116};
117
118/*****************************/
119/* error message types */
120/*****************************/
121enum {
122 PEER_MSG_ERR_PROTOCOL = 0,
123 PEER_MSG_ERR_SIZELIMIT,
124};
125
Emeric Brun530ba382020-06-02 11:17:42 +0200126/* network key types;
127 * network types were directly and mistakenly
128 * mapped on sample types, to keep backward
129 * compatiblitiy we keep those values but
130 * we now use a internal/network mapping
131 * to avoid further mistakes adding or
132 * modifying internals types
133 */
134enum {
135 PEER_KT_ANY = 0, /* any type */
136 PEER_KT_RESV1, /* UNUSED */
137 PEER_KT_SINT, /* signed 64bits integer type */
138 PEER_KT_RESV3, /* UNUSED */
139 PEER_KT_IPV4, /* ipv4 type */
140 PEER_KT_IPV6, /* ipv6 type */
141 PEER_KT_STR, /* char string type */
142 PEER_KT_BIN, /* buffer type */
143 PEER_KT_TYPES /* number of types, must always be last */
144};
145
146/* Map used to retrieve network type from internal type
147 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
148 */
149static int peer_net_key_type[SMP_TYPES] = {
150 [SMP_T_SINT] = PEER_KT_SINT,
151 [SMP_T_IPV4] = PEER_KT_IPV4,
152 [SMP_T_IPV6] = PEER_KT_IPV6,
153 [SMP_T_STR] = PEER_KT_STR,
154 [SMP_T_BIN] = PEER_KT_BIN,
155};
156
157/* Map used to retrieve internal type from external type
158 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
159 */
160static int peer_int_key_type[PEER_KT_TYPES] = {
161 [PEER_KT_SINT] = SMP_T_SINT,
162 [PEER_KT_IPV4] = SMP_T_IPV4,
163 [PEER_KT_IPV6] = SMP_T_IPV6,
164 [PEER_KT_STR] = SMP_T_STR,
165 [PEER_KT_BIN] = SMP_T_BIN,
166};
167
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100168/*
169 * Parameters used by functions to build peer protocol messages. */
170struct peer_prep_params {
171 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100172 struct peer *peer;
173 } hello;
174 struct {
175 unsigned int st1;
176 } error_status;
177 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100178 struct stksess *stksess;
179 struct shared_table *shared_table;
180 unsigned int updateid;
181 int use_identifier;
182 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200183 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100184 } updt;
185 struct {
186 struct shared_table *shared_table;
187 } swtch;
188 struct {
189 struct shared_table *shared_table;
190 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100191 struct {
192 unsigned char head[2];
193 } control;
194 struct {
195 unsigned char head[2];
196 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100197};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200198
199/*******************************/
200/* stick table sync mesg types */
201/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500202/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200203/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200204#define PEER_MSG_STKT_UPDATE 0x80
205#define PEER_MSG_STKT_INCUPDATE 0x81
206#define PEER_MSG_STKT_DEFINE 0x82
207#define PEER_MSG_STKT_SWITCH 0x83
208#define PEER_MSG_STKT_ACK 0x84
209#define PEER_MSG_STKT_UPDATE_TIMED 0x85
210#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200211/* All the stick-table message identifiers abova have the #7 bit set */
212#define PEER_MSG_STKT_BIT 7
213#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200214
Frédéric Lécaille39143342019-05-24 14:32:27 +0200215/* The maximum length of an encoded data length. */
216#define PEER_MSG_ENC_LENGTH_MAXLEN 5
217
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200218/* Minimum 64-bits value encoded with 2 bytes */
219#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
220/* 3 bytes */
221#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
222/* 4 bytes */
223#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
224/* 5 bytes */
225#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
226/* 6 bytes */
227#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
228/* 7 bytes */
229#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
230/* 8 bytes */
231#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
232/* 9 bytes */
233#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
234/* 10 bytes */
235#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
236
237/* #7 bit used to detect the last byte to be encoded */
238#define PEER_ENC_STOP_BIT 7
239/* The byte minimum value with #7 bit set */
240#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
241/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
242#define PEER_ENC_2BYTES_MIN_BITS 4
243
Frédéric Lécaille39143342019-05-24 14:32:27 +0200244#define PEER_MSG_HEADER_LEN 2
245
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200246#define PEER_STKT_CACHE_MAX_ENTRIES 128
247
Emeric Brun2b920a12010-09-23 18:30:22 +0200248/**********************************/
249/* Peer Session IO handler states */
250/**********************************/
251
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100252enum {
253 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
254 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
255 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
256 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100257 /* after this point, data were possibly exchanged */
258 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
259 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
260 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
261 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
262 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200263 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
264 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100265 PEER_SESS_ST_END, /* Killed session */
266};
Emeric Brun2b920a12010-09-23 18:30:22 +0200267
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100268/***************************************************/
269/* Peer Session status code - part of the protocol */
270/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200271
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100272#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
273#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200274
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100275#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200276
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100277#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200278
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100279#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
280#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
281#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
282#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200283
284#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200285#define PEER_MAJOR_VER 2
286#define PEER_MINOR_VER 1
287#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200288
Willy Tarreau6254a922019-01-29 17:45:23 +0100289static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200290struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200291static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200292
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200293static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
294 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200295static inline void flush_dcache(struct peer *peer);
296
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200297static const char *statuscode_str(int statuscode)
298{
299 switch (statuscode) {
300 case PEER_SESS_SC_CONNECTCODE:
301 return "CONN";
302 case PEER_SESS_SC_CONNECTEDCODE:
303 return "HSHK";
304 case PEER_SESS_SC_SUCCESSCODE:
305 return "ESTA";
306 case PEER_SESS_SC_TRYAGAIN:
307 return "RETR";
308 case PEER_SESS_SC_ERRPROTO:
309 return "PROT";
310 case PEER_SESS_SC_ERRVERSION:
311 return "VERS";
312 case PEER_SESS_SC_ERRHOST:
313 return "NAME";
314 case PEER_SESS_SC_ERRPEER:
315 return "UNKN";
316 default:
317 return "NONE";
318 }
319}
320
Emeric Brun18928af2017-03-29 16:32:53 +0200321/* This function encode an uint64 to 'dynamic' length format.
322 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500323 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200324 At return, the *str is set at the next Byte after then
325 encoded integer. The function returns then length of the
326 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200327int intencode(uint64_t i, char **str) {
328 int idx = 0;
329 unsigned char *msg;
330
Emeric Brunb3971ab2015-05-12 18:49:09 +0200331 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200332 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200333 msg[0] = (unsigned char)i;
334 *str = (char *)&msg[idx+1];
335 return (idx+1);
336 }
337
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200338 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
339 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
340 while (i >= PEER_ENC_STOP_BYTE) {
341 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
342 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200343 }
344 msg[++idx] = (unsigned char)i;
345 *str = (char *)&msg[idx+1];
346 return (idx+1);
347}
348
349
350/* This function returns the decoded integer or 0
351 if decode failed
352 *str point on the beginning of the integer to decode
353 at the end of decoding *str point on the end of the
354 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200355uint64_t intdecode(char **str, char *end)
356{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200357 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200358 uint64_t i;
359 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200360
361 if (!*str)
362 return 0;
363
364 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200365 if (msg >= (unsigned char *)end)
366 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200367
Emeric Brun18928af2017-03-29 16:32:53 +0200368 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200369 if (i >= PEER_ENC_2BYTES_MIN) {
370 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200371 do {
372 if (msg >= (unsigned char *)end)
373 goto fail;
374 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200375 shift += PEER_ENC_STOP_BIT;
376 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200377 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100378 *str = (char *)msg;
379 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200380
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100381 fail:
382 *str = NULL;
383 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200384}
Emeric Brun2b920a12010-09-23 18:30:22 +0200385
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100386/*
387 * Build a "hello" peer protocol message.
388 * Return the number of written bytes written to build this messages if succeeded,
389 * 0 if not.
390 */
391static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
392{
393 int min_ver, ret;
394 struct peer *peer;
395
396 peer = p->hello.peer;
397 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
398 /* Prepare headers */
399 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
400 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
401 if (ret >= size)
402 return 0;
403
404 return ret;
405}
406
407/*
408 * Build a "handshake succeeded" status message.
409 * Return the number of written bytes written to build this messages if succeeded,
410 * 0 if not.
411 */
412static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
413{
414 int ret;
415
416 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
417 if (ret >= size)
418 return 0;
419
420 return ret;
421}
422
423/*
424 * Build an error status message.
425 * Return the number of written bytes written to build this messages if succeeded,
426 * 0 if not.
427 */
428static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
429{
430 int ret;
431 unsigned int st1;
432
433 st1 = p->error_status.st1;
434 ret = snprintf(msg, size, "%d\n", st1);
435 if (ret >= size)
436 return 0;
437
438 return ret;
439}
440
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200441/* Set the stick-table UPDATE message type byte at <msg_type> address,
442 * depending on <use_identifier> and <use_timed> boolean parameters.
443 * Always successful.
444 */
445static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
446{
447 if (use_timed) {
448 if (use_identifier)
449 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
450 else
451 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
452 }
453 else {
454 if (use_identifier)
455 *msg_type = PEER_MSG_STKT_UPDATE;
456 else
457 *msg_type = PEER_MSG_STKT_INCUPDATE;
458 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200459}
Emeric Brun2b920a12010-09-23 18:30:22 +0200460/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200461 * This prepare the data update message on the stick session <ts>, <st> is the considered
462 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800463 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200464 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
465 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200466 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100467static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200468{
469 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200470 unsigned short datalen;
471 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200472 unsigned int data_type;
473 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100474 struct stksess *ts;
475 struct shared_table *st;
476 unsigned int updateid;
477 int use_identifier;
478 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200479 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100480
481 ts = p->updt.stksess;
482 st = p->updt.shared_table;
483 updateid = p->updt.updateid;
484 use_identifier = p->updt.use_identifier;
485 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200486 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200487
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200488 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200489
Emeric Brun2b920a12010-09-23 18:30:22 +0200490 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200491
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500492 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200493 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200494 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200495 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200496
497 /* encode update identifier if needed */
498 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200499 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200500 memcpy(cursor, &netinteger, sizeof(netinteger));
501 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200502 }
503
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200504 if (use_timed) {
505 netinteger = htonl(tick_remain(now_ms, ts->expire));
506 memcpy(cursor, &netinteger, sizeof(netinteger));
507 cursor += sizeof(netinteger);
508 }
509
Emeric Brunb3971ab2015-05-12 18:49:09 +0200510 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200511 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200512 int stlen = strlen((char *)ts->key.key);
513
Emeric Brunb3971ab2015-05-12 18:49:09 +0200514 intencode(stlen, &cursor);
515 memcpy(cursor, ts->key.key, stlen);
516 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200517 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200518 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100519 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200520 memcpy(cursor, &netinteger, sizeof(netinteger));
521 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200522 }
523 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200524 memcpy(cursor, ts->key.key, st->table->key_size);
525 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200526 }
527
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100528 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200529 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200530 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531
Emeric Brun94900952015-06-11 18:25:54 +0200532 data_ptr = stktable_data_ptr(st->table, ts, data_type);
533 if (data_ptr) {
534 switch (stktable_data_types[data_type].std_type) {
535 case STD_T_SINT: {
536 int data;
537
538 data = stktable_data_cast(data_ptr, std_t_sint);
539 intencode(data, &cursor);
540 break;
541 }
542 case STD_T_UINT: {
543 unsigned int data;
544
545 data = stktable_data_cast(data_ptr, std_t_uint);
546 intencode(data, &cursor);
547 break;
548 }
549 case STD_T_ULL: {
550 unsigned long long data;
551
552 data = stktable_data_cast(data_ptr, std_t_ull);
553 intencode(data, &cursor);
554 break;
555 }
556 case STD_T_FRQP: {
557 struct freq_ctr_period *frqp;
558
559 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
560 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
561 intencode(frqp->curr_ctr, &cursor);
562 intencode(frqp->prev_ctr, &cursor);
563 break;
564 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200565 case STD_T_DICT: {
566 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200567 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200568 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200569 char *beg, *end;
570 size_t value_len, data_len;
571 struct dcache *dc;
572
573 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100574 if (!de) {
575 /* No entry */
576 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200577 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100578 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200579
580 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200581 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200582 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200583 if (cached_de == &cde.entry) {
584 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
585 break;
586 /* Encode the length of the remaining data -> 1 */
587 intencode(1, &cursor);
588 /* Encode the cache entry ID */
589 intencode(cde.id + 1, &cursor);
590 }
591 else {
592 /* Leave enough room to encode the remaining data length. */
593 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
594 /* Encode the dictionary entry key */
595 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200596 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200597 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200598 intencode(value_len, &end);
599 /* Copy the data */
600 memcpy(end, de->value.key, value_len);
601 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200602 /* Encode the length of the data */
603 data_len = end - beg;
604 intencode(data_len, &cursor);
605 memmove(cursor, beg, data_len);
606 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200607 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200608 break;
609 }
Emeric Brun94900952015-06-11 18:25:54 +0200610 }
611 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200612 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100613 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200614
615 /* Compute datalen */
616 datalen = (cursor - datamsg);
617
618 /* prepare message header */
619 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200620 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200621 cursor = &msg[2];
622 intencode(datalen, &cursor);
623
624 /* move data after header */
625 memmove(cursor, datamsg, datalen);
626
627 /* return header size + data_len */
628 return (cursor - msg) + datalen;
629}
630
631/*
632 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800633 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200634 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
635 * check size)
636 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100637static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200638{
639 int len;
640 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200641 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200642 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200643 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200644 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100645 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200646
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100647 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200648 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200649
650 /* Encode data */
651
652 /* encode local id */
653 intencode(st->local_id, &cursor);
654
655 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100656 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200657 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100658 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200659 cursor += len;
660
661 /* encode table type */
662
Emeric Brun530ba382020-06-02 11:17:42 +0200663 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200664
665 /* encode table key size */
666 intencode(st->table->key_size, &cursor);
667
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200668 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200669 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200670 /* encode available known data types in table */
671 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
672 if (st->table->data_ofs[data_type]) {
673 switch (stktable_data_types[data_type].std_type) {
674 case STD_T_SINT:
675 case STD_T_UINT:
676 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200677 case STD_T_DICT:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200678 data |= 1 << data_type;
679 break;
Emeric Brun94900952015-06-11 18:25:54 +0200680 case STD_T_FRQP:
681 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200682 intencode(data_type, &chunkq);
683 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200684 break;
685 }
686 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200687 }
688 intencode(data, &cursor);
689
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200690 /* Encode stick-table entries duration. */
691 intencode(st->table->expire, &cursor);
692
693 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200694 chunk->data = chunkq - chunkp;
695 memcpy(cursor, chunk->area, chunk->data);
696 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200697 }
698
Emeric Brunb3971ab2015-05-12 18:49:09 +0200699 /* Compute datalen */
700 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200701
Emeric Brunb3971ab2015-05-12 18:49:09 +0200702 /* prepare message header */
703 msg[0] = PEER_MSG_CLASS_STICKTABLE;
704 msg[1] = PEER_MSG_STKT_DEFINE;
705 cursor = &msg[2];
706 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200707
Emeric Brunb3971ab2015-05-12 18:49:09 +0200708 /* move data after header */
709 memmove(cursor, datamsg, datalen);
710
711 /* return header size + data_len */
712 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200713}
714
Emeric Brunb3971ab2015-05-12 18:49:09 +0200715/*
716 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
717 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800718 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200719 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
720 * check size)
721 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100722static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200723{
724 unsigned short datalen;
725 char *cursor, *datamsg;
726 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100727 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200728
Frédéric Lécaille39143342019-05-24 14:32:27 +0200729 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200730
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100731 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200732 intencode(st->remote_id, &cursor);
733 netinteger = htonl(st->last_get);
734 memcpy(cursor, &netinteger, sizeof(netinteger));
735 cursor += sizeof(netinteger);
736
737 /* Compute datalen */
738 datalen = (cursor - datamsg);
739
740 /* prepare message header */
741 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200742 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200743 cursor = &msg[2];
744 intencode(datalen, &cursor);
745
746 /* move data after header */
747 memmove(cursor, datamsg, datalen);
748
749 /* return header size + data_len */
750 return (cursor - msg) + datalen;
751}
Emeric Brun2b920a12010-09-23 18:30:22 +0200752
753/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200754 * Function to deinit connected peer
755 */
756void __peer_session_deinit(struct peer *peer)
757{
758 struct stream_interface *si;
759 struct stream *s;
760 struct peers *peers;
761
762 if (!peer->appctx)
763 return;
764
765 si = peer->appctx->owner;
766 if (!si)
767 return;
768
769 s = si_strm(si);
770 if (!s)
771 return;
772
773 peers = strm_fe(s)->parent;
774 if (!peers)
775 return;
776
777 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
778 HA_ATOMIC_SUB(&connected_peers, 1);
779
780 HA_ATOMIC_SUB(&active_peers, 1);
781
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200782 flush_dcache(peer);
783
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200784 /* Re-init current table pointers to force announcement on re-connect */
785 peer->remote_table = peer->last_local_table = NULL;
786 peer->appctx = NULL;
787 if (peer->flags & PEER_F_LEARN_ASSIGN) {
788 /* unassign current peer for learning */
789 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
790 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
791
792 /* reschedule a resync */
793 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
794 }
795 /* reset teaching and learning flags to 0 */
796 peer->flags &= PEER_TEACH_RESET;
797 peer->flags &= PEER_LEARN_RESET;
Frédéric Lécaille3585cab2019-11-20 11:17:30 +0100798 /* set this peer as dead from heartbeat point of view */
799 peer->flags &= ~PEER_F_ALIVE;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200800 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);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100819 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200820 }
821}
822
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200823/* Retrieve the major and minor versions of peers protocol
824 * announced by a remote peer. <str> is a null-terminated
825 * string with the following format: "<maj_ver>.<min_ver>".
826 */
827static int peer_get_version(const char *str,
828 unsigned int *maj_ver, unsigned int *min_ver)
829{
830 unsigned int majv, minv;
831 const char *pos, *saved;
832 const char *end;
833
834 saved = pos = str;
835 end = str + strlen(str);
836
837 majv = read_uint(&pos, end);
838 if (saved == pos || *pos++ != '.')
839 return -1;
840
841 saved = pos;
842 minv = read_uint(&pos, end);
843 if (saved == pos || pos != end)
844 return -1;
845
846 *maj_ver = majv;
847 *min_ver = minv;
848
849 return 0;
850}
Emeric Brun2b920a12010-09-23 18:30:22 +0200851
852/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100853 * Parse a line terminated by an optional '\r' character, followed by a mandatory
854 * '\n' character.
855 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
856 * a line could not be read because the communication channel is closed.
857 */
858static inline int peer_getline(struct appctx *appctx)
859{
860 int n;
861 struct stream_interface *si = appctx->owner;
862
863 n = co_getline(si_oc(si), trash.area, trash.size);
864 if (!n)
865 return 0;
866
867 if (n < 0 || trash.area[n - 1] != '\n') {
868 appctx->st0 = PEER_SESS_ST_END;
869 return -1;
870 }
871
872 if (n > 1 && (trash.area[n - 2] == '\r'))
873 trash.area[n - 2] = 0;
874 else
875 trash.area[n - 1] = 0;
876
877 co_skip(si_oc(si), n);
878
879 return n;
880}
881
882/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100883 * Send a message after having called <peer_prepare_msg> to build it.
884 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
885 * Returns -1 if there was not enough room left to send the message,
886 * any other negative returned value must be considered as an error with an appcxt st0
887 * returned value equal to PEER_SESS_ST_END.
888 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100889static inline int peer_send_msg(struct appctx *appctx,
890 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
891 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100892{
893 int ret, msglen;
894 struct stream_interface *si = appctx->owner;
895
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100896 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100897 if (!msglen) {
898 /* internal error: message does not fit in trash */
899 appctx->st0 = PEER_SESS_ST_END;
900 return 0;
901 }
902
903 /* message to buffer */
904 ret = ci_putblk(si_ic(si), trash.area, msglen);
905 if (ret <= 0) {
906 if (ret == -1) {
907 /* No more write possible */
908 si_rx_room_blk(si);
909 return -1;
910 }
911 appctx->st0 = PEER_SESS_ST_END;
912 }
913
914 return ret;
915}
916
917/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100918 * Send a hello message.
919 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
920 * Returns -1 if there was not enough room left to send the message,
921 * any other negative returned value must be considered as an error with an appcxt st0
922 * returned value equal to PEER_SESS_ST_END.
923 */
924static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
925{
926 struct peer_prep_params p = {
927 .hello.peer = peer,
928 };
929
930 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
931}
932
933/*
934 * Send a success peer handshake status message.
935 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
936 * Returns -1 if there was not enough room left to send the message,
937 * any other negative returned value must be considered as an error with an appcxt st0
938 * returned value equal to PEER_SESS_ST_END.
939 */
940static inline int peer_send_status_successmsg(struct appctx *appctx)
941{
942 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
943}
944
945/*
946 * Send a peer handshake status error message.
947 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
948 * Returns -1 if there was not enough room left to send the message,
949 * any other negative returned value must be considered as an error with an appcxt st0
950 * returned value equal to PEER_SESS_ST_END.
951 */
952static inline int peer_send_status_errormsg(struct appctx *appctx)
953{
954 struct peer_prep_params p = {
955 .error_status.st1 = appctx->st1,
956 };
957
958 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
959}
960
961/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100962 * Send a stick-table switch message.
963 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
964 * Returns -1 if there was not enough room left to send the message,
965 * any other negative returned value must be considered as an error with an appcxt st0
966 * returned value equal to PEER_SESS_ST_END.
967 */
968static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
969{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100970 struct peer_prep_params p = {
971 .swtch.shared_table = st,
972 };
973
974 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100975}
976
977/*
978 * Send a stick-table update acknowledgement message.
979 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
980 * Returns -1 if there was not enough room left to send the message,
981 * any other negative returned value must be considered as an error with an appcxt st0
982 * returned value equal to PEER_SESS_ST_END.
983 */
984static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
985{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100986 struct peer_prep_params p = {
987 .ack.shared_table = st,
988 };
989
990 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100991}
992
993/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100994 * Send a stick-table update message.
995 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
996 * Returns -1 if there was not enough room left to send the message,
997 * any other negative returned value must be considered as an error with an appcxt st0
998 * returned value equal to PEER_SESS_ST_END.
999 */
1000static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
1001 unsigned int updateid, int use_identifier, int use_timed)
1002{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001003 struct peer_prep_params p = {
1004 .updt.stksess = ts,
1005 .updt.shared_table = st,
1006 .updt.updateid = updateid,
1007 .updt.use_identifier = use_identifier,
1008 .updt.use_timed = use_timed,
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001009 .updt.peer = appctx->ctx.peers.ptr,
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001010 };
1011
1012 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001013}
1014
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001015/*
1016 * Build a peer protocol control class message.
1017 * Returns the number of written bytes used to build the message if succeeded,
1018 * 0 if not.
1019 */
1020static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1021{
1022 if (size < sizeof p->control.head)
1023 return 0;
1024
1025 msg[0] = p->control.head[0];
1026 msg[1] = p->control.head[1];
1027
1028 return 2;
1029}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001030
1031/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001032 * Send a stick-table synchronization request message.
1033 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1034 * Returns -1 if there was not enough room left to send the message,
1035 * any other negative returned value must be considered as an error with an appctx st0
1036 * returned value equal to PEER_SESS_ST_END.
1037 */
1038static inline int peer_send_resync_reqmsg(struct appctx *appctx)
1039{
1040 struct peer_prep_params p = {
1041 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1042 };
1043
1044 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1045}
1046
1047/*
1048 * Send a stick-table synchronization confirmation message.
1049 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1050 * Returns -1 if there was not enough room left to send the message,
1051 * any other negative returned value must be considered as an error with an appctx st0
1052 * returned value equal to PEER_SESS_ST_END.
1053 */
1054static inline int peer_send_resync_confirmsg(struct appctx *appctx)
1055{
1056 struct peer_prep_params p = {
1057 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1058 };
1059
1060 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1061}
1062
1063/*
1064 * Send a stick-table synchronization finished message.
1065 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1066 * Returns -1 if there was not enough room left to send the message,
1067 * any other negative returned value must be considered as an error with an appctx st0
1068 * returned value equal to PEER_SESS_ST_END.
1069 */
Emeric Brun70de43b2020-03-16 10:51:01 +01001070static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001071{
1072 struct peer_prep_params p = {
1073 .control.head = { PEER_MSG_CLASS_CONTROL, },
1074 };
1075
Emeric Brun70de43b2020-03-16 10:51:01 +01001076 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001077 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1078
1079 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1080}
1081
1082/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001083 * Send a heartbeat message.
1084 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1085 * Returns -1 if there was not enough room left to send the message,
1086 * any other negative returned value must be considered as an error with an appctx st0
1087 * returned value equal to PEER_SESS_ST_END.
1088 */
1089static inline int peer_send_heartbeatmsg(struct appctx *appctx)
1090{
1091 struct peer_prep_params p = {
1092 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1093 };
1094
1095 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1096}
1097
1098/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001099 * Build a peer protocol error class message.
1100 * Returns the number of written bytes used to build the message if succeeded,
1101 * 0 if not.
1102 */
1103static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1104{
1105 if (size < sizeof p->error.head)
1106 return 0;
1107
1108 msg[0] = p->error.head[0];
1109 msg[1] = p->error.head[1];
1110
1111 return 2;
1112}
1113
1114/*
1115 * Send a "size limit reached" error message.
1116 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1117 * Returns -1 if there was not enough room left to send the message,
1118 * any other negative returned value must be considered as an error with an appctx st0
1119 * returned value equal to PEER_SESS_ST_END.
1120 */
1121static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1122{
1123 struct peer_prep_params p = {
1124 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1125 };
1126
1127 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1128}
1129
1130/*
1131 * Send a "peer protocol" error message.
1132 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1133 * Returns -1 if there was not enough room left to send the message,
1134 * any other negative returned value must be considered as an error with an appctx st0
1135 * returned value equal to PEER_SESS_ST_END.
1136 */
1137static inline int peer_send_error_protomsg(struct appctx *appctx)
1138{
1139 struct peer_prep_params p = {
1140 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1141 };
1142
1143 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1144}
1145
1146/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001147 * Function used to lookup for recent stick-table updates associated with
1148 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1149 */
1150static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1151{
1152 struct eb32_node *eb;
1153
1154 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1155 if (!eb) {
1156 eb = eb32_first(&st->table->updates);
1157 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
1158 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1159 return NULL;
1160 }
1161 }
1162
1163 if ((int)(eb->key - st->table->localupdate) > 0) {
1164 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1165 return NULL;
1166 }
1167
1168 return eb32_entry(eb, struct stksess, upd);
1169}
1170
1171/*
1172 * Function used to lookup for recent stick-table updates associated with
1173 * <st> shared stick-table during teach state 1 step.
1174 */
1175static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1176{
1177 struct eb32_node *eb;
1178
1179 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1180 if (!eb) {
1181 st->flags |= SHTABLE_F_TEACH_STAGE1;
1182 eb = eb32_first(&st->table->updates);
1183 if (eb)
1184 st->last_pushed = eb->key - 1;
1185 return NULL;
1186 }
1187
1188 return eb32_entry(eb, struct stksess, upd);
1189}
1190
1191/*
1192 * Function used to lookup for recent stick-table updates associated with
1193 * <st> shared stick-table during teach state 2 step.
1194 */
1195static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1196{
1197 struct eb32_node *eb;
1198
1199 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1200 if (!eb || eb->key > st->teaching_origin) {
1201 st->flags |= SHTABLE_F_TEACH_STAGE2;
1202 return NULL;
1203 }
1204
1205 return eb32_entry(eb, struct stksess, upd);
1206}
1207
1208/*
1209 * Generic function to emit update messages for <st> stick-table when a lesson must
1210 * be taught to the peer <p>.
1211 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1212 * this function, 0 if not.
1213 *
1214 * This function temporary unlock/lock <st> when it sends stick-table updates or
1215 * when decrementing its refcount in case of any error when it sends this updates.
1216 *
1217 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1218 * Returns -1 if there was not enough room left to send the message,
1219 * any other negative returned value must be considered as an error with an appcxt st0
1220 * returned value equal to PEER_SESS_ST_END.
1221 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1222 * unlocked if not already locked when entering this function.
1223 */
1224static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1225 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1226 struct shared_table *st, int locked)
1227{
1228 int ret, new_pushed, use_timed;
1229
1230 ret = 1;
1231 use_timed = 0;
1232 if (st != p->last_local_table) {
1233 ret = peer_send_switchmsg(st, appctx);
1234 if (ret <= 0)
1235 return ret;
1236
1237 p->last_local_table = st;
1238 }
1239
1240 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1241 use_timed = !(p->flags & PEER_F_DWNGRD);
1242
1243 /* We force new pushed to 1 to force identifier in update message */
1244 new_pushed = 1;
1245
1246 if (!locked)
1247 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1248
1249 while (1) {
1250 struct stksess *ts;
1251 unsigned updateid;
1252
1253 /* push local updates */
1254 ts = peer_stksess_lookup(st);
1255 if (!ts)
1256 break;
1257
1258 updateid = ts->upd.key;
1259 ts->ref_cnt++;
1260 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1261
1262 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1263 if (ret <= 0) {
1264 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1265 ts->ref_cnt--;
1266 if (!locked)
1267 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1268 return ret;
1269 }
1270
1271 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1272 ts->ref_cnt--;
1273 st->last_pushed = updateid;
1274
1275 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1276 (int)(st->last_pushed - st->table->commitupdate) > 0)
1277 st->table->commitupdate = st->last_pushed;
1278
1279 /* identifier may not needed in next update message */
1280 new_pushed = 0;
1281 }
1282
1283 out:
1284 if (!locked)
1285 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1286 return 1;
1287}
1288
1289/*
1290 * Function to emit update messages for <st> stick-table when a lesson must
1291 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1292 *
1293 * Note that <st> shared stick-table is locked when calling this function.
1294 *
1295 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1296 * Returns -1 if there was not enough room left to send the message,
1297 * any other negative returned value must be considered as an error with an appcxt st0
1298 * returned value equal to PEER_SESS_ST_END.
1299 */
1300static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1301 struct shared_table *st)
1302{
1303 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1304}
1305
1306/*
1307 * Function to emit update messages for <st> stick-table when a lesson must
1308 * be taught to the peer <p> during teach state 1 step.
1309 *
1310 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1311 * Returns -1 if there was not enough room left to send the message,
1312 * any other negative returned value must be considered as an error with an appcxt st0
1313 * returned value equal to PEER_SESS_ST_END.
1314 */
1315static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1316 struct shared_table *st)
1317{
1318 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1319}
1320
1321/*
1322 * Function to emit update messages for <st> stick-table when a lesson must
1323 * be taught to the peer <p> during teach state 1 step.
1324 *
1325 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1326 * Returns -1 if there was not enough room left to send the message,
1327 * any other negative returned value must be considered as an error with an appcxt st0
1328 * returned value equal to PEER_SESS_ST_END.
1329 */
1330static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1331 struct shared_table *st)
1332{
1333 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1334}
1335
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001336
1337/*
1338 * Function used to parse a stick-table update message after it has been received
1339 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1340 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1341 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1342 * was encountered.
1343 * <exp> must be set if the stick-table entry expires.
1344 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001345 * messages, in this case the stick-table update message is received with a stick-table
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001346 * update ID.
1347 * <totl> is the length of the stick-table update message computed upon receipt.
1348 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001349static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1350 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001351{
1352 struct stream_interface *si = appctx->owner;
1353 struct shared_table *st = p->remote_table;
1354 struct stksess *ts, *newts;
1355 uint32_t update;
1356 int expire;
1357 unsigned int data_type;
1358 void *data_ptr;
1359
1360 /* Here we have data message */
1361 if (!st)
1362 goto ignore_msg;
1363
1364 expire = MS_TO_TICKS(st->table->expire);
1365
1366 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001367 if (msg_len < sizeof(update))
1368 goto malformed_exit;
1369
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001370 memcpy(&update, *msg_cur, sizeof(update));
1371 *msg_cur += sizeof(update);
1372 st->last_get = htonl(update);
1373 }
1374 else {
1375 st->last_get++;
1376 }
1377
1378 if (exp) {
1379 size_t expire_sz = sizeof expire;
1380
Willy Tarreau1e82a142019-01-29 11:08:06 +01001381 if (*msg_cur + expire_sz > msg_end)
1382 goto malformed_exit;
1383
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001384 memcpy(&expire, *msg_cur, expire_sz);
1385 *msg_cur += expire_sz;
1386 expire = ntohl(expire);
1387 }
1388
1389 newts = stksess_new(st->table, NULL);
1390 if (!newts)
1391 goto ignore_msg;
1392
1393 if (st->table->type == SMP_T_STR) {
1394 unsigned int to_read, to_store;
1395
1396 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001397 if (!*msg_cur)
1398 goto malformed_free_newts;
1399
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001400 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001401 if (*msg_cur + to_store > msg_end)
1402 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001403
1404 memcpy(newts->key.key, *msg_cur, to_store);
1405 newts->key.key[to_store] = 0;
1406 *msg_cur += to_read;
1407 }
1408 else if (st->table->type == SMP_T_SINT) {
1409 unsigned int netinteger;
1410
Willy Tarreau1e82a142019-01-29 11:08:06 +01001411 if (*msg_cur + sizeof(netinteger) > msg_end)
1412 goto malformed_free_newts;
1413
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001414 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1415 netinteger = ntohl(netinteger);
1416 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1417 *msg_cur += sizeof(netinteger);
1418 }
1419 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001420 if (*msg_cur + st->table->key_size > msg_end)
1421 goto malformed_free_newts;
1422
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001423 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1424 *msg_cur += st->table->key_size;
1425 }
1426
1427 /* lookup for existing entry */
1428 ts = stktable_set_entry(st->table, newts);
1429 if (ts != newts) {
1430 stksess_free(st->table, newts);
1431 newts = NULL;
1432 }
1433
1434 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1435
1436 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001437 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001438
1439 if (!((1 << data_type) & st->remote_data))
1440 continue;
1441
Willy Tarreau1e82a142019-01-29 11:08:06 +01001442 decoded_int = intdecode(msg_cur, msg_end);
1443 if (!*msg_cur)
1444 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001445
Willy Tarreau1e82a142019-01-29 11:08:06 +01001446 switch (stktable_data_types[data_type].std_type) {
1447 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001448 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1449 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001450 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001451 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001452
Willy Tarreau1e82a142019-01-29 11:08:06 +01001453 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001454 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1455 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001456 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001457 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001458
Willy Tarreau1e82a142019-01-29 11:08:06 +01001459 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001460 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1461 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001462 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001463 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001464
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001465 case STD_T_FRQP: {
1466 struct freq_ctr_period data;
1467
1468 /* First bit is reserved for the freq_ctr_period lock
1469 Note: here we're still protected by the stksess lock
1470 so we don't need to update the update the freq_ctr_period
1471 using its internal lock */
1472
Willy Tarreau1e82a142019-01-29 11:08:06 +01001473 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001474 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001475 if (!*msg_cur)
1476 goto malformed_unlock;
1477
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001478 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001479 if (!*msg_cur)
1480 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001481
1482 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1483 if (data_ptr)
1484 stktable_data_cast(data_ptr, std_t_frqp) = data;
1485 break;
1486 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001487 case STD_T_DICT: {
1488 struct buffer *chunk;
1489 size_t data_len, value_len;
1490 unsigned int id;
1491 struct dict_entry *de;
1492 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001493 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001494
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001495 if (!decoded_int) {
1496 /* No entry. */
1497 break;
1498 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001499 data_len = decoded_int;
1500 if (*msg_cur + data_len > msg_end)
1501 goto malformed_unlock;
1502
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001503 /* Compute the end of the current data, <msg_end> being at the end of
1504 * the entire message.
1505 */
1506 end = *msg_cur + data_len;
1507 id = intdecode(msg_cur, end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001508 if (!*msg_cur || !id)
1509 goto malformed_unlock;
1510
1511 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001512 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001513 /* Dictionary entry key without value. */
1514 if (id > dc->max_entries)
1515 break;
1516 /* IDs sent over the network are numbered from 1. */
1517 de = dc->rx[id - 1].de;
1518 }
1519 else {
1520 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001521 value_len = intdecode(msg_cur, end);
1522 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001523 unlikely(value_len + 1 >= chunk->size))
1524 goto malformed_unlock;
1525
1526 chunk_memcpy(chunk, *msg_cur, value_len);
1527 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001528 *msg_cur += value_len;
1529
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001530 de = dict_insert(&server_name_dict, chunk->area);
1531 dc->rx[id - 1].de = de;
1532 }
1533 if (de) {
1534 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1535 if (data_ptr)
1536 stktable_data_cast(data_ptr, std_t_dict) = de;
1537 }
1538 break;
1539 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001540 }
1541 }
1542 /* Force new expiration */
1543 ts->expire = tick_add(now_ms, expire);
1544
1545 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1546 stktable_touch_remote(st->table, ts, 1);
1547 return 1;
1548
1549 ignore_msg:
1550 /* skip consumed message */
1551 co_skip(si_oc(si), totl);
1552 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001553
1554 malformed_unlock:
1555 /* malformed message */
1556 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1557 stktable_touch_remote(st->table, ts, 1);
1558 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1559 return 0;
1560
1561 malformed_free_newts:
1562 /* malformed message */
1563 stksess_free(st->table, newts);
1564 malformed_exit:
1565 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1566 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001567}
1568
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001569/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001570 * Function used to parse a stick-table update acknowledgement message after it
1571 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1572 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1573 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1574 * was encountered.
1575 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1576 */
1577static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1578 char **msg_cur, char *msg_end)
1579{
1580 /* ack message */
1581 uint32_t table_id ;
1582 uint32_t update;
1583 struct shared_table *st;
1584
1585 table_id = intdecode(msg_cur, msg_end);
1586 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1587 /* malformed message */
1588 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1589 return 0;
1590 }
1591
1592 memcpy(&update, *msg_cur, sizeof(update));
1593 update = ntohl(update);
1594
1595 for (st = p->tables; st; st = st->next) {
1596 if (st->local_id == table_id) {
1597 st->update = update;
1598 break;
1599 }
1600 }
1601
1602 return 1;
1603}
1604
1605/*
1606 * Function used to parse a stick-table switch message after it has been received
1607 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1608 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1609 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1610 * was encountered.
1611 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1612 */
1613static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1614 char **msg_cur, char *msg_end)
1615{
1616 struct shared_table *st;
1617 int table_id;
1618
1619 table_id = intdecode(msg_cur, msg_end);
1620 if (!*msg_cur) {
1621 /* malformed message */
1622 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1623 return 0;
1624 }
1625
1626 p->remote_table = NULL;
1627 for (st = p->tables; st; st = st->next) {
1628 if (st->remote_id == table_id) {
1629 p->remote_table = st;
1630 break;
1631 }
1632 }
1633
1634 return 1;
1635}
1636
1637/*
1638 * Function used to parse a stick-table definition message after it has been received
1639 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1640 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1641 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1642 * was encountered.
1643 * <totl> is the length of the stick-table update message computed upon receipt.
1644 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1645 */
1646static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1647 char **msg_cur, char *msg_end, int totl)
1648{
1649 struct stream_interface *si = appctx->owner;
1650 int table_id_len;
1651 struct shared_table *st;
1652 int table_type;
1653 int table_keylen;
1654 int table_id;
1655 uint64_t table_data;
1656
1657 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001658 if (!*msg_cur)
1659 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001660
1661 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001662 if (!*msg_cur)
1663 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001664
1665 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001666 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1667 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001668
1669 for (st = p->tables; st; st = st->next) {
1670 /* Reset IDs */
1671 if (st->remote_id == table_id)
1672 st->remote_id = 0;
1673
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001674 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1675 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001676 p->remote_table = st;
1677 }
1678
1679 if (!p->remote_table)
1680 goto ignore_msg;
1681
1682 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001683 if (*msg_cur >= msg_end)
1684 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001685
1686 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001687 if (!*msg_cur)
1688 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001689
1690 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001691 if (!*msg_cur)
1692 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001693
1694 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001695 if (!*msg_cur)
1696 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001697
Emeric Brun530ba382020-06-02 11:17:42 +02001698 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001699 || p->remote_table->table->key_size != table_keylen) {
1700 p->remote_table = NULL;
1701 goto ignore_msg;
1702 }
1703
1704 p->remote_table->remote_data = table_data;
1705 p->remote_table->remote_id = table_id;
1706 return 1;
1707
1708 ignore_msg:
1709 co_skip(si_oc(si), totl);
1710 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001711
1712 malformed_exit:
1713 /* malformed message */
1714 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1715 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001716}
1717
1718/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001719 * Receive a stick-table message.
1720 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1721 * -1 if there was an error updating the appctx state st0 accordingly.
1722 */
1723static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1724 uint32_t *msg_len, int *totl)
1725{
1726 int reql;
1727 struct stream_interface *si = appctx->owner;
1728
1729 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1730 if (reql <= 0) /* closed or EOL not found */
1731 goto incomplete;
1732
1733 *totl += reql;
1734
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02001735 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001736 return 1;
1737
1738 /* Read and Decode message length */
1739 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1740 if (reql <= 0) /* closed */
1741 goto incomplete;
1742
1743 *totl += reql;
1744
Frédéric Lécaille32b55732019-06-03 18:29:51 +02001745 if ((unsigned int)msg_head[2] < PEER_ENC_2BYTES_MIN) {
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001746 *msg_len = msg_head[2];
1747 }
1748 else {
1749 int i;
1750 char *cur;
1751 char *end;
1752
1753 for (i = 3 ; i < msg_head_sz ; i++) {
1754 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1755 if (reql <= 0) /* closed */
1756 goto incomplete;
1757
1758 *totl += reql;
1759
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02001760 if (!(msg_head[i] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001761 break;
1762 }
1763
1764 if (i == msg_head_sz) {
1765 /* malformed message */
1766 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1767 return -1;
1768 }
1769 end = msg_head + msg_head_sz;
1770 cur = &msg_head[2];
1771 *msg_len = intdecode(&cur, end);
1772 if (!cur) {
1773 /* malformed message */
1774 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1775 return -1;
1776 }
1777 }
1778
1779 /* Read message content */
1780 if (*msg_len) {
1781 if (*msg_len > trash.size) {
1782 /* Status code is not success, abort */
1783 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1784 return -1;
1785 }
1786
1787 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1788 if (reql <= 0) /* closed */
1789 goto incomplete;
1790 *totl += reql;
1791 }
1792
1793 return 1;
1794
1795 incomplete:
1796 if (reql < 0) {
1797 /* there was an error */
1798 appctx->st0 = PEER_SESS_ST_END;
1799 return -1;
1800 }
1801
1802 return 0;
1803}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001804
1805/*
1806 * Treat the awaited message with <msg_head> as header.*
1807 * Return 1 if succeeded, 0 if not.
1808 */
1809static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1810 char **msg_cur, char *msg_end, int msg_len, int totl)
1811{
1812 struct stream_interface *si = appctx->owner;
1813 struct stream *s = si_strm(si);
1814 struct peers *peers = strm_fe(s)->parent;
1815
1816 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1817 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1818 struct shared_table *st;
1819 /* Reset message: remote need resync */
1820
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001821 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001822 for (st = peer->tables; st; st = st->next) {
1823 st->teaching_origin = st->last_pushed = st->table->update;
1824 st->flags = 0;
1825 }
1826
1827 /* reset teaching flags to 0 */
1828 peer->flags &= PEER_TEACH_RESET;
1829
1830 /* flag to start to teach lesson */
1831 peer->flags |= PEER_F_TEACH_PROCESS;
1832 }
1833 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1834 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1835 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1836 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1837 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1838 }
1839 peer->confirm++;
1840 }
1841 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1842 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1843 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1844 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1845
1846 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001847 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001848 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1849 }
1850 peer->confirm++;
1851 }
1852 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1853 struct shared_table *st;
1854
1855 /* If stopping state */
1856 if (stopping) {
1857 /* Close session, push resync no more needed */
1858 peer->flags |= PEER_F_TEACH_COMPLETE;
1859 appctx->st0 = PEER_SESS_ST_END;
1860 return 0;
1861 }
1862 for (st = peer->tables; st; st = st->next) {
1863 st->update = st->last_pushed = st->teaching_origin;
1864 st->flags = 0;
1865 }
1866
1867 /* reset teaching flags to 0 */
1868 peer->flags &= PEER_TEACH_RESET;
1869 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001870 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001871 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01001872 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001873 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001874 }
1875 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1876 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1877 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1878 return 0;
1879 }
1880 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1881 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1882 return 0;
1883 }
1884 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1885 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1886 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1887 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1888 int update, expire;
1889
1890 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1891 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1892 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1893 msg_cur, msg_end, msg_len, totl))
1894 return 0;
1895
1896 }
1897 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1898 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1899 return 0;
1900 }
1901 }
1902 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1903 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1904 return 0;
1905 }
1906
1907 return 1;
1908}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001909
1910
1911/*
1912 * Send any message to <peer> peer.
1913 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001914 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001915 * to a peer state change (from the peer I/O handler point of view).
1916 */
1917static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1918{
1919 int repl;
1920 struct stream_interface *si = appctx->owner;
1921 struct stream *s = si_strm(si);
1922 struct peers *peers = strm_fe(s)->parent;
1923
1924 /* Need to request a resync */
1925 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1926 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1927 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1928
1929 repl = peer_send_resync_reqmsg(appctx);
1930 if (repl <= 0)
1931 return repl;
1932
1933 peers->flags |= PEERS_F_RESYNC_PROCESS;
1934 }
1935
1936 /* Nothing to read, now we start to write */
1937 if (peer->tables) {
1938 struct shared_table *st;
1939 struct shared_table *last_local_table;
1940
1941 last_local_table = peer->last_local_table;
1942 if (!last_local_table)
1943 last_local_table = peer->tables;
1944 st = last_local_table->next;
1945
1946 while (1) {
1947 if (!st)
1948 st = peer->tables;
1949
1950 /* It remains some updates to ack */
1951 if (st->last_get != st->last_acked) {
1952 repl = peer_send_ackmsg(st, appctx);
1953 if (repl <= 0)
1954 return repl;
1955
1956 st->last_acked = st->last_get;
1957 }
1958
1959 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1960 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1961 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1962 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1963
1964 repl = peer_send_teach_process_msgs(appctx, peer, st);
1965 if (repl <= 0) {
1966 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1967 return repl;
1968 }
1969 }
1970 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1971 }
1972 else {
1973 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1974 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1975 if (repl <= 0)
1976 return repl;
1977 }
1978
1979 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1980 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1981 if (repl <= 0)
1982 return repl;
1983 }
1984 }
1985
1986 if (st == last_local_table)
1987 break;
1988 st = st->next;
1989 }
1990 }
1991
1992 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Emeric Brun70de43b2020-03-16 10:51:01 +01001993 repl = peer_send_resync_finishedmsg(appctx, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001994 if (repl <= 0)
1995 return repl;
1996
1997 /* flag finished message sent */
1998 peer->flags |= PEER_F_TEACH_FINISHED;
1999 }
2000
2001 /* Confirm finished or partial messages */
2002 while (peer->confirm) {
2003 repl = peer_send_resync_confirmsg(appctx);
2004 if (repl <= 0)
2005 return repl;
2006
2007 peer->confirm--;
2008 }
2009
2010 return 1;
2011}
2012
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002013/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002014 * Read and parse a first line of a "hello" peer protocol message.
2015 * Returns 0 if could not read a line, -1 if there was a read error or
2016 * the line is malformed, 1 if succeeded.
2017 */
2018static inline int peer_getline_version(struct appctx *appctx,
2019 unsigned int *maj_ver, unsigned int *min_ver)
2020{
2021 int reql;
2022
2023 reql = peer_getline(appctx);
2024 if (!reql)
2025 return 0;
2026
2027 if (reql < 0)
2028 return -1;
2029
2030 /* test protocol */
2031 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2032 appctx->st0 = PEER_SESS_ST_EXIT;
2033 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2034 return -1;
2035 }
2036 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2037 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2038 appctx->st0 = PEER_SESS_ST_EXIT;
2039 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2040 return -1;
2041 }
2042
2043 return 1;
2044}
2045
2046/*
2047 * Read and parse a second line of a "hello" peer protocol message.
2048 * Returns 0 if could not read a line, -1 if there was a read error or
2049 * the line is malformed, 1 if succeeded.
2050 */
2051static inline int peer_getline_host(struct appctx *appctx)
2052{
2053 int reql;
2054
2055 reql = peer_getline(appctx);
2056 if (!reql)
2057 return 0;
2058
2059 if (reql < 0)
2060 return -1;
2061
2062 /* test hostname match */
2063 if (strcmp(localpeer, trash.area) != 0) {
2064 appctx->st0 = PEER_SESS_ST_EXIT;
2065 appctx->st1 = PEER_SESS_SC_ERRHOST;
2066 return -1;
2067 }
2068
2069 return 1;
2070}
2071
2072/*
2073 * Read and parse a last line of a "hello" peer protocol message.
2074 * Returns 0 if could not read a character, -1 if there was a read error or
2075 * the line is malformed, 1 if succeeded.
2076 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2077 */
2078static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2079{
2080 char *p;
2081 int reql;
2082 struct peer *peer;
2083 struct stream_interface *si = appctx->owner;
2084 struct stream *s = si_strm(si);
2085 struct peers *peers = strm_fe(s)->parent;
2086
2087 reql = peer_getline(appctx);
2088 if (!reql)
2089 return 0;
2090
2091 if (reql < 0)
2092 return -1;
2093
2094 /* parse line "<peer name> <pid> <relative_pid>" */
2095 p = strchr(trash.area, ' ');
2096 if (!p) {
2097 appctx->st0 = PEER_SESS_ST_EXIT;
2098 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2099 return -1;
2100 }
2101 *p = 0;
2102
2103 /* lookup known peer */
2104 for (peer = peers->remote; peer; peer = peer->next) {
2105 if (strcmp(peer->id, trash.area) == 0)
2106 break;
2107 }
2108
2109 /* if unknown peer */
2110 if (!peer) {
2111 appctx->st0 = PEER_SESS_ST_EXIT;
2112 appctx->st1 = PEER_SESS_SC_ERRPEER;
2113 return -1;
2114 }
2115 *curpeer = peer;
2116
2117 return 1;
2118}
2119
2120/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002121 * Init <peer> peer after having accepted it at peer protocol level.
2122 */
2123static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2124{
2125 struct shared_table *st;
2126
2127 /* Register status code */
2128 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
2129
2130 /* Awake main task */
2131 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2132
2133 /* Init confirm counter */
2134 peer->confirm = 0;
2135
2136 /* Init cursors */
2137 for (st = peer->tables; st ; st = st->next) {
2138 st->last_get = st->last_acked = 0;
2139 st->teaching_origin = st->last_pushed = st->update;
2140 }
2141
2142 /* reset teaching and learning flags to 0 */
2143 peer->flags &= PEER_TEACH_RESET;
2144 peer->flags &= PEER_LEARN_RESET;
2145
2146 /* if current peer is local */
2147 if (peer->local) {
2148 /* if current host need resyncfrom local and no process assined */
2149 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2150 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2151 /* assign local peer for a lesson, consider lesson already requested */
2152 peer->flags |= PEER_F_LEARN_ASSIGN;
2153 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2154 }
2155
2156 }
2157 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2158 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2159 /* assign peer for a lesson */
2160 peer->flags |= PEER_F_LEARN_ASSIGN;
2161 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2162 }
2163}
2164
2165/*
2166 * Init <peer> peer after having connected it at peer protocol level.
2167 */
2168static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2169{
2170 struct shared_table *st;
2171
2172 /* Init cursors */
2173 for (st = peer->tables; st ; st = st->next) {
2174 st->last_get = st->last_acked = 0;
2175 st->teaching_origin = st->last_pushed = st->update;
2176 }
2177
2178 /* Init confirm counter */
2179 peer->confirm = 0;
2180
2181 /* reset teaching and learning flags to 0 */
2182 peer->flags &= PEER_TEACH_RESET;
2183 peer->flags &= PEER_LEARN_RESET;
2184
2185 /* If current peer is local */
2186 if (peer->local) {
2187 /* flag to start to teach lesson */
2188 peer->flags |= PEER_F_TEACH_PROCESS;
2189 }
2190 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2191 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2192 /* If peer is remote and resync from remote is needed,
2193 and no peer currently assigned */
2194
2195 /* assign peer for a lesson */
2196 peer->flags |= PEER_F_LEARN_ASSIGN;
2197 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2198 }
2199}
2200
2201/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002202 * IO Handler to handle message exchance with a peer
2203 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002204static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002205{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002206 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002207 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002208 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002209 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002210 int reql = 0;
2211 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002212 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002213 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002214
Joseph Herlant82b2f542018-11-15 12:19:14 -08002215 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002216 if (si_ic(si)->buf.size == 0) {
2217 si_rx_room_blk(si);
2218 goto out;
2219 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002220
Emeric Brun2b920a12010-09-23 18:30:22 +02002221 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002222 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002223switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002224 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002225 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002226 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002227 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002228 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002229 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002230 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002231 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002232 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002233 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2234 if (reql <= 0) {
2235 if (!reql)
2236 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002237 goto switchstate;
2238 }
2239
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002240 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002241 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002242 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002243 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002244 reql = peer_getline_host(appctx);
2245 if (reql <= 0) {
2246 if (!reql)
2247 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002248 goto switchstate;
2249 }
2250
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002251 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002252 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002253 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002254 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002255 reql = peer_getline_last(appctx, &curpeer);
2256 if (reql <= 0) {
2257 if (!reql)
2258 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002259 goto switchstate;
2260 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002261
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002262 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002263 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002264 if (curpeer->local) {
2265 /* Local connection, reply a retry */
2266 appctx->st0 = PEER_SESS_ST_EXIT;
2267 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2268 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002269 }
Emeric Brun80527f52017-06-19 17:46:37 +02002270
2271 /* we're killing a connection, we must apply a random delay before
2272 * retrying otherwise the other end will do the same and we can loop
2273 * for a while.
2274 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002275 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002276 peer_session_forceshutdown(curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002277 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002278 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2279 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2280 curpeer->flags |= PEER_F_DWNGRD;
2281 }
2282 else {
2283 curpeer->flags &= ~PEER_F_DWNGRD;
2284 }
2285 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002286 curpeer->appctx = appctx;
2287 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002288 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002289 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002290 /* fall through */
2291 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002292 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002293 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002294 if (!curpeer) {
2295 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002296 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002297 if (curpeer->appctx != appctx) {
2298 appctx->st0 = PEER_SESS_ST_END;
2299 goto switchstate;
2300 }
2301 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002302
2303 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002304 if (repl <= 0) {
2305 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002306 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002307 goto switchstate;
2308 }
2309
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002310 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002311
Emeric Brun2b920a12010-09-23 18:30:22 +02002312 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002313 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002314 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002315 goto switchstate;
2316 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002317 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002318 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002319 if (!curpeer) {
2320 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002321 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002322 if (curpeer->appctx != appctx) {
2323 appctx->st0 = PEER_SESS_ST_END;
2324 goto switchstate;
2325 }
2326 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002327
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002328 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002329 if (repl <= 0) {
2330 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002331 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002332 goto switchstate;
2333 }
2334
2335 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002336 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002337 /* fall through */
2338 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002339 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002340 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002341 if (!curpeer) {
2342 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002343 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002344 if (curpeer->appctx != appctx) {
2345 appctx->st0 = PEER_SESS_ST_END;
2346 goto switchstate;
2347 }
2348 }
2349
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002350 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002351 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002352
Frédéric Lécaillece025572019-01-21 13:38:06 +01002353 reql = peer_getline(appctx);
2354 if (!reql)
2355 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002356
Frédéric Lécaillece025572019-01-21 13:38:06 +01002357 if (reql < 0)
2358 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002359
2360 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002361 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002362
2363 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002364 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002365
2366 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002367 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002368 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002369 }
2370 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002371 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2372 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002373 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002374 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002375 goto switchstate;
2376 }
Olivier Houcharded879892019-03-08 18:53:43 +01002377 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002378 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002379 /* fall through */
2380 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002381 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002382 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002383 char *msg_cur = trash.area;
2384 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002385 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002386 int totl = 0;
2387
Willy Tarreau2d372c22018-11-05 17:12:27 +01002388 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002389 if (!curpeer) {
2390 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002391 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002392 if (curpeer->appctx != appctx) {
2393 appctx->st0 = PEER_SESS_ST_END;
2394 goto switchstate;
2395 }
2396 }
2397
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002398 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2399 if (reql <= 0) {
2400 if (reql == -1)
2401 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002402 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002403 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002404
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002405 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002406 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002407 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002408
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002409 curpeer->flags |= PEER_F_ALIVE;
2410
Emeric Brun2b920a12010-09-23 18:30:22 +02002411 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002412 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002413 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002414 goto switchstate;
2415
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002416send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002417 if (curpeer->flags & PEER_F_HEARTBEAT) {
2418 curpeer->flags &= ~PEER_F_HEARTBEAT;
2419 repl = peer_send_heartbeatmsg(appctx);
2420 if (repl <= 0) {
2421 if (repl == -1)
2422 goto out;
2423 goto switchstate;
2424 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002425 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002426 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002427 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002428 repl = peer_send_msgs(appctx, curpeer);
2429 if (repl <= 0) {
2430 if (repl == -1)
2431 goto out;
2432 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002433 }
2434
Emeric Brun2b920a12010-09-23 18:30:22 +02002435 /* noting more to do */
2436 goto out;
2437 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002438 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002439 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002440 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002441 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002442 if (peer_send_status_errormsg(appctx) == -1)
2443 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002444 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002445 goto switchstate;
2446 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002447 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002448 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002449 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002450 if (peer_send_error_size_limitmsg(appctx) == -1)
2451 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002452 appctx->st0 = PEER_SESS_ST_END;
2453 goto switchstate;
2454 }
2455 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002456 if (curpeer)
2457 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002458 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002459 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002460 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002461 if (peer_send_error_protomsg(appctx) == -1)
2462 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002463 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002464 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002465 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002466 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002467 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002468 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002469 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002470 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002471 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002472 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002473 curpeer = NULL;
2474 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002475 si_shutw(si);
2476 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002477 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002478 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002479 }
2480 }
2481 }
2482out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002483 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002484
2485 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002486 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002487 return;
2488}
2489
Willy Tarreau30576452015-04-13 13:50:30 +02002490static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002491 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002492 .name = "<PEER>", /* used for logging */
2493 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002494 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002495};
Emeric Brun2b920a12010-09-23 18:30:22 +02002496
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002497
Emeric Brun2b920a12010-09-23 18:30:22 +02002498/*
2499 * Use this function to force a close of a peer session
2500 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002501static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002502{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002503 struct appctx *appctx = peer->appctx;
2504
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002505 /* Note that the peer sessions which have just been created
2506 * (->st0 == PEER_SESS_ST_CONNECT) must not
2507 * be shutdown, if not, the TCP session will never be closed
2508 * and stay in CLOSE_WAIT state after having been closed by
2509 * the remote side.
2510 */
2511 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002512 return;
2513
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002514 if (appctx->applet != &peer_applet)
2515 return;
2516
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002517 __peer_session_deinit(peer);
2518
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002519 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002520 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002521}
2522
Willy Tarreau91d96282015-03-13 15:47:26 +01002523/* Pre-configures a peers frontend to accept incoming connections */
2524void peers_setup_frontend(struct proxy *fe)
2525{
2526 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002527 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002528 fe->maxconn = 0;
2529 fe->conn_retries = CONN_RETRIES;
2530 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002531 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002532 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002533 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002534 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002535}
2536
Emeric Brun2b920a12010-09-23 18:30:22 +02002537/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002538 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002539 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002540static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002541{
Willy Tarreau04b92862017-09-15 11:01:04 +02002542 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002543 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002544 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002545 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02002546
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002547 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002548 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002549 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002550 s = NULL;
2551
Emeric Brun1138fd02017-06-19 12:38:55 +02002552 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002553 if (!appctx)
2554 goto out_close;
2555
2556 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002557 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002558
Willy Tarreau04b92862017-09-15 11:01:04 +02002559 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002560 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002561 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002562 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002563 }
2564
Willy Tarreau87787ac2017-08-28 16:22:54 +02002565 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002566 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002567 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002568 }
2569
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002570 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002571 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002572 appctx_wakeup(appctx);
2573
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002574 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002575 s->target = peer_session_target(peer, s);
2576 if (!sockaddr_alloc(&s->target_addr))
2577 goto out_free_strm;
2578 *s->target_addr = peer->addr;
Willy Tarreau02efeda2019-07-18 17:21:24 +02002579 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Willy Tarreaudbd02672017-12-06 17:39:53 +01002580 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002581
Emeric Brun2b920a12010-09-23 18:30:22 +02002582 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002583 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002584
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002585 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002586
Emeric Brunb3971ab2015-05-12 18:49:09 +02002587 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002588 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002589 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002590 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002591
2592 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02002593 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002594 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002595 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002596 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002597 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002598 out_free_appctx:
2599 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002600 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002601 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002602}
2603
2604/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002605 * Task processing function to manage re-connect, peer session
2606 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002607 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002608static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002609{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002610 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002611 struct peer *ps;
2612 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002613
2614 task->expire = TICK_ETERNITY;
2615
Emeric Brunb3971ab2015-05-12 18:49:09 +02002616 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002617 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002618 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002619 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002620 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002621 return NULL;
2622 }
2623
Emeric Brun80527f52017-06-19 17:46:37 +02002624 /* Acquire lock for all peers of the section */
2625 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002626 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002627
Emeric Brun2b920a12010-09-23 18:30:22 +02002628 if (!stopping) {
2629 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002630
2631 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2632 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2633 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002634 /* Resync from local peer needed
2635 no peer was assigned for the lesson
2636 and no old local peer found
2637 or resync timeout expire */
2638
2639 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002640 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002641
2642 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002643 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002644 }
2645
2646 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002647 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002648 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002649 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002650 if (!ps->appctx) {
2651 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002652 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002653 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002654 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002655 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002656 tick_is_expired(ps->reconnect, now_ms))) {
2657 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002658 * or previous peer connection established with success
2659 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002660 * and reconnection timer is expired */
2661
2662 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002663 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002664 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002665 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002666 /* If previous session failed during connection
2667 * but reconnection timer is not expired */
2668
2669 /* reschedule task for reconnect */
2670 task->expire = tick_first(task->expire, ps->reconnect);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002671 ps->new_conn++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002672 }
2673 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002674 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002675 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002676 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002677 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2678 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002679 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2680 /* Resync from a remote is needed
2681 * and no peer was assigned for lesson
2682 * and current peer may be up2date */
2683
2684 /* assign peer for the lesson */
2685 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002686 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002687
Willy Tarreau9df94c22016-10-31 18:42:52 +01002688 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002689 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002690 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002691 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002692 int update_to_push = 0;
2693
Emeric Brunb3971ab2015-05-12 18:49:09 +02002694 /* Awake session if there is data to push */
2695 for (st = ps->tables; st ; st = st->next) {
2696 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002697 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002698 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002699 /* There is no need to send a heartbeat message
2700 * when some updates must be pushed. The remote
2701 * peer will consider <ps> peer as alive when it will
2702 * receive these updates.
2703 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002704 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002705 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002706 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002707 /* We are going to send updates, let's ensure we will
2708 * come back to send heartbeat messages or to reconnect.
2709 */
2710 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002711 appctx_wakeup(ps->appctx);
2712 break;
2713 }
2714 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002715 /* When there are updates to send we do not reconnect
2716 * and do not send heartbeat message either.
2717 */
2718 if (!update_to_push) {
2719 if (tick_is_expired(ps->reconnect, now_ms)) {
2720 if (ps->flags & PEER_F_ALIVE) {
2721 /* This peer was alive during a 'reconnect' period.
2722 * Flag it as not alive again for the next period.
2723 */
2724 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002725 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002726 }
2727 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01002728 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002729 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002730 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002731 }
2732 }
2733 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2734 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2735 ps->flags |= PEER_F_HEARTBEAT;
2736 appctx_wakeup(ps->appctx);
2737 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002738 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002739 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002740 }
2741 /* else do nothing */
2742 } /* SUCCESSCODE */
2743 } /* !ps->peer->local */
2744 } /* for */
2745
2746 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002747 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2748 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2749 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002750 /* Resync from remote peer needed
2751 * no peer was assigned for the lesson
2752 * and resync timeout expire */
2753
2754 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002755 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002756 }
2757
Emeric Brunb3971ab2015-05-12 18:49:09 +02002758 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002759 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002760 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2761 if (!tick_is_expired(peers->resync_timeout, now_ms))
2762 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002763 }
2764 } /* !stopping */
2765 else {
2766 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002767 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002768 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002769 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002770 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002771 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002772 peers->flags |= PEERS_F_DONOTSTOP;
2773 ps = peers->local;
2774 for (st = ps->tables; st ; st = st->next)
2775 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002776 }
2777
2778 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002779 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002780 /* we're killing a connection, we must apply a random delay before
2781 * retrying otherwise the other end will do the same and we can loop
2782 * for a while.
2783 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002784 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002785 if (ps->appctx) {
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002786 peer_session_forceshutdown(ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002787 }
2788 }
2789 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002790
Emeric Brunb3971ab2015-05-12 18:49:09 +02002791 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002792 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002793 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002794 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002795 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002796 peers->flags &= ~PEERS_F_DONOTSTOP;
2797 for (st = ps->tables; st ; st = st->next)
2798 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002799 }
2800 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002801 else if (!ps->appctx) {
2802 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002803 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002804 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2805 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2806 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002807 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002808 * or previous peer connection was successfully established
2809 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002810 * or during previous connect, peer replies a try again statuscode */
2811
2812 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002813 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002814 }
2815 else {
2816 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002817 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002818 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002819 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002820 peers->flags &= ~PEERS_F_DONOTSTOP;
2821 for (st = ps->tables; st ; st = st->next)
2822 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002823 }
2824 }
2825 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002826 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002827 /* current peer connection is active and established
2828 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002829 for (st = ps->tables; st ; st = st->next) {
2830 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002831 appctx_wakeup(ps->appctx);
2832 break;
2833 }
2834 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002835 }
2836 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002837
2838 /* Release lock for all peers of the section */
2839 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002840 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002841
Emeric Brun2b920a12010-09-23 18:30:22 +02002842 /* Wakeup for re-connect */
2843 return task;
2844}
2845
Emeric Brunb3971ab2015-05-12 18:49:09 +02002846
Emeric Brun2b920a12010-09-23 18:30:22 +02002847/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002848 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002849 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002850int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002851{
Emeric Brun2b920a12010-09-23 18:30:22 +02002852 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002853
Emeric Brun2b920a12010-09-23 18:30:22 +02002854 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002855 peers->peers_fe->maxconn += 3;
2856 }
2857
Emeric Brunc60def82017-09-27 14:59:38 +02002858 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002859 if (!peers->sync_task)
2860 return 0;
2861
Emeric Brunb3971ab2015-05-12 18:49:09 +02002862 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002863 peers->sync_task->context = (void *)peers;
2864 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2865 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002866 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002867}
2868
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002869/*
2870 * Allocate a cache a dictionary entries used upon transmission.
2871 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002872static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002873{
2874 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002875 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002876
2877 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002878 entries = calloc(max_entries, sizeof *entries);
2879 if (!d || !entries)
2880 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002881
2882 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002883 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002884 d->cached_entries = EB_ROOT_UNIQUE;
2885 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002886
2887 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002888
2889 err:
2890 free(d);
2891 free(entries);
2892 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002893}
2894
2895/*
2896 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
2897 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002898 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002899 * Must be deallocated calling free_dcache().
2900 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002901static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002902{
2903 struct dcache_tx *dc_tx;
2904 struct dcache *dc;
2905 struct dcache_rx *dc_rx;
2906
2907 dc = calloc(1, sizeof *dc);
2908 dc_tx = new_dcache_tx(max_entries);
2909 dc_rx = calloc(max_entries, sizeof *dc_rx);
2910 if (!dc || !dc_tx || !dc_rx)
2911 goto err;
2912
2913 dc->tx = dc_tx;
2914 dc->rx = dc_rx;
2915 dc->max_entries = max_entries;
2916
2917 return dc;
2918
2919 err:
2920 free(dc);
2921 free(dc_tx);
2922 free(dc_rx);
2923 return NULL;
2924}
2925
2926/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002927 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
2928 * entries used upon transmission.
2929 * Return the entry if found, NULL if not.
2930 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002931static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
2932 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002933{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002934 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002935}
2936
2937/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002938 * Flush <dc> cache.
2939 * Always succeeds.
2940 */
2941static inline void flush_dcache(struct peer *peer)
2942{
2943 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002944 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002945
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002946 for (i = 0; i < dc->max_entries; i++)
2947 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002948
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002949 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002950}
2951
2952/*
2953 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
2954 * with information provided by <i> dictionary cache entry (especially the value
2955 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002956 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002957 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002958static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002959{
2960 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002961 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002962
2963 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002964
2965 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
2966 o = dc_tx->prev_lookup;
2967 } else {
2968 o = dcache_tx_lookup_value(dc_tx, i);
2969 if (o) {
2970 /* Save it */
2971 dc_tx->prev_lookup = o;
2972 }
2973 }
2974
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002975 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002976 /* Copy the ID. */
2977 i->id = o - dc->tx->entries;
2978 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002979 }
2980
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002981 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002982 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002983
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002984 ebpt_delete(o);
2985 o->key = i->entry.key;
2986 ebpt_insert(&dc_tx->cached_entries, o);
2987 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002988
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002989 /* Update the index for the next entry to put in cache */
2990 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002991
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002992 return o;
2993}
Emeric Brunb3971ab2015-05-12 18:49:09 +02002994
2995/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002996 * Allocate a dictionary cache for each peer of <peers> section.
2997 * Return 1 if succeeded, 0 if not.
2998 */
2999int peers_alloc_dcache(struct peers *peers)
3000{
3001 struct peer *p;
3002
3003 for (p = peers->remote; p; p = p->next) {
3004 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3005 if (!p->dcache)
3006 return 0;
3007 }
3008
3009 return 1;
3010}
3011
3012/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003013 * Function used to register a table for sync on a group of peers
3014 *
3015 */
3016void peers_register_table(struct peers *peers, struct stktable *table)
3017{
3018 struct shared_table *st;
3019 struct peer * curpeer;
3020 int id = 0;
3021
3022 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003023 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02003024 st->table = table;
3025 st->next = curpeer->tables;
3026 if (curpeer->tables)
3027 id = curpeer->tables->local_id;
3028 st->local_id = id + 1;
3029
3030 curpeer->tables = st;
3031 }
3032
3033 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02003034}
3035
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003036/*
3037 * Parse the "show peers" command arguments.
3038 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3039 * error message.
3040 */
3041static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3042{
3043 appctx->ctx.cfgpeers.target = NULL;
3044
3045 if (*args[2]) {
3046 struct peers *p;
3047
3048 for (p = cfg_peers; p; p = p->next) {
3049 if (!strcmp(p->id, args[2])) {
3050 appctx->ctx.cfgpeers.target = p;
3051 break;
3052 }
3053 }
3054
Willy Tarreau9d008692019-08-09 11:21:01 +02003055 if (!p)
3056 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003057 }
3058
3059 return 0;
3060}
3061
3062/*
3063 * This function dumps the peer state information of <peers> "peers" section.
3064 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3065 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3066 */
3067static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3068{
3069 struct tm tm;
3070
3071 get_localtime(peers->last_change, &tm);
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003072 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 +02003073 peers,
3074 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3075 tm.tm_hour, tm.tm_min, tm.tm_sec,
3076 peers->id, peers->state, peers->flags,
3077 peers->resync_timeout ?
3078 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3079 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003080 TICKS_TO_MS(1000)) : "<NEVER>",
3081 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003082
3083 if (ci_putchk(si_ic(si), msg) == -1) {
3084 si_rx_room_blk(si);
3085 return 0;
3086 }
3087
3088 return 1;
3089}
3090
3091/*
3092 * This function dumps <peer> state information.
3093 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3094 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3095 */
3096static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
3097{
3098 struct connection *conn;
3099 char pn[INET6_ADDRSTRLEN];
3100 struct stream_interface *peer_si;
3101 struct stream *peer_s;
3102 struct appctx *appctx;
3103 struct shared_table *st;
3104
3105 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003106 chunk_appendf(msg, " %p: id=%s(%s) addr=%s:%d 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 +02003107 peer, peer->id,
3108 peer->local ? "local" : "remote",
3109 pn, get_host_port(&peer->addr),
3110 statuscode_str(peer->statuscode),
3111 peer->reconnect ?
3112 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3113 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
3114 TICKS_TO_MS(1000)) : "<NEVER>",
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003115 peer->confirm, peer->tx_hbt, peer->rx_hbt,
3116 peer->no_hbt, peer->new_conn, peer->proto_err);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003117
3118 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3119
3120 appctx = peer->appctx;
3121 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003122 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003123
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003124 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3125 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003126
3127 peer_si = peer->appctx->owner;
3128 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003129 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003130
3131 peer_s = si_strm(peer_si);
3132 if (!peer_s)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003133 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003134
3135 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3136
3137 conn = objt_conn(strm_orig(peer_s));
3138 if (conn)
3139 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3140
Willy Tarreau3ca14902019-07-17 14:53:15 +02003141 switch (conn && conn_get_src(conn) ? addr_to_str(conn->src, pn, sizeof(pn)) : AF_UNSPEC) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003142 case AF_INET:
3143 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003144 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003145 break;
3146 case AF_UNIX:
3147 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3148 break;
3149 }
3150
Willy Tarreau3ca14902019-07-17 14:53:15 +02003151 switch (conn && conn_get_dst(conn) ? addr_to_str(conn->dst, pn, sizeof(pn)) : AF_UNSPEC) {
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003152 case AF_INET:
3153 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003154 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003155 break;
3156 case AF_UNIX:
3157 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3158 break;
3159 }
3160
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003161 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003162 if (peer->remote_table)
3163 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3164 peer->remote_table,
3165 peer->remote_table->table->id,
3166 peer->remote_table->local_id,
3167 peer->remote_table->remote_id);
3168
3169 if (peer->last_local_table)
3170 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3171 peer->last_local_table,
3172 peer->last_local_table->table->id,
3173 peer->last_local_table->local_id,
3174 peer->last_local_table->remote_id);
3175
3176 if (peer->tables) {
3177 chunk_appendf(&trash, "\n shared tables:");
3178 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003179 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003180 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003181 struct dcache *dcache;
3182
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003183 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003184 dcache = peer->dcache;
3185
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003186 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3187 "flags=0x%x remote_data=0x%llx",
3188 st, st->local_id, st->remote_id,
3189 st->flags, (unsigned long long)st->remote_data);
3190 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3191 " teaching_origin=%u update=%u",
3192 st->last_acked, st->last_pushed, st->last_get,
3193 st->teaching_origin, st->update);
3194 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
3195 " commitupdate=%u syncing=%u",
3196 t, t->id, t->update, t->localupdate, t->commitupdate, t->syncing);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003197 chunk_appendf(&trash, "\n TX dictionary cache:");
3198 count = 0;
3199 for (i = 0; i < dcache->max_entries; i++) {
3200 struct ebpt_node *node;
3201 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003202
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003203 node = &dcache->tx->entries[i];
3204 if (!node->key)
3205 break;
3206
3207 if (!count++)
3208 chunk_appendf(&trash, "\n ");
3209 de = node->key;
3210 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3211 count &= 0x3;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003212 }
3213 chunk_appendf(&trash, "\n RX dictionary cache:");
3214 count = 0;
3215 for (i = 0; i < dcache->max_entries; i++) {
3216 if (!count++)
3217 chunk_appendf(&trash, "\n ");
3218 chunk_appendf(&trash, " %3u -> %s", i,
3219 dcache->rx[i].de ?
3220 (char *)dcache->rx[i].de->value.key : "-");
3221 count &= 0x3;
3222 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003223 }
3224 }
3225
3226 end:
3227 chunk_appendf(&trash, "\n");
3228 if (ci_putchk(si_ic(si), msg) == -1) {
3229 si_rx_room_blk(si);
3230 return 0;
3231 }
3232
3233 return 1;
3234}
3235
3236/*
3237 * This function dumps all the peers of "peers" section.
3238 * Returns 0 if the output buffer is full and needs to be called
3239 * again, non-zero if not. It proceeds in an isolated thread, so
3240 * there is no thread safety issue here.
3241 */
3242static int cli_io_handler_show_peers(struct appctx *appctx)
3243{
3244 int show_all;
3245 int ret = 0, first_peers = 1;
3246 struct stream_interface *si = appctx->owner;
3247
3248 thread_isolate();
3249
3250 show_all = !appctx->ctx.cfgpeers.target;
3251
3252 chunk_reset(&trash);
3253
3254 while (appctx->st2 != STAT_ST_FIN) {
3255 switch (appctx->st2) {
3256 case STAT_ST_INIT:
3257 if (show_all)
3258 appctx->ctx.cfgpeers.peers = cfg_peers;
3259 else
3260 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3261
3262 appctx->st2 = STAT_ST_LIST;
3263 /* fall through */
3264
3265 case STAT_ST_LIST:
3266 if (!appctx->ctx.cfgpeers.peers) {
3267 /* No more peers list. */
3268 appctx->st2 = STAT_ST_END;
3269 }
3270 else {
3271 if (!first_peers)
3272 chunk_appendf(&trash, "\n");
3273 else
3274 first_peers = 0;
3275 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3276 goto out;
3277
3278 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3279 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3280 appctx->st2 = STAT_ST_INFO;
3281 }
3282 break;
3283
3284 case STAT_ST_INFO:
3285 if (!appctx->ctx.cfgpeers.peer) {
3286 /* End of peer list */
3287 if (show_all)
3288 appctx->st2 = STAT_ST_LIST;
3289 else
3290 appctx->st2 = STAT_ST_END;
3291 }
3292 else {
3293 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
3294 goto out;
3295
3296 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3297 }
3298 break;
3299
3300 case STAT_ST_END:
3301 appctx->st2 = STAT_ST_FIN;
3302 break;
3303 }
3304 }
3305 ret = 1;
3306 out:
3307 thread_release();
3308 return ret;
3309}
3310
3311/*
3312 * CLI keywords.
3313 */
3314static struct cli_kw_list cli_kws = {{ }, {
3315 { { "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, },
3316 {},
3317}};
3318
3319/* Register cli keywords */
3320INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3321