blob: 3724346347135b5027f080265daa3f8cb5ad975e [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
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020033#include <types/stats.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020034
35#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020036#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020037#include <proto/channel.h>
Frédéric Lécaille95679dc2019-04-15 10:25:27 +020038#include <proto/cli.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020039#include <haproxy/fd.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020040#include <proto/log.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020041#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020042#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020043#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020044#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010045#include <proto/signal.h>
46#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020047#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049
50
51/*******************************/
52/* Current peer learning state */
53/*******************************/
54
55/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020056/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020057/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010058#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
59#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
60#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
61#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
62#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
63 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020064
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010065#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
66#define PEERS_RESYNC_FROMLOCAL 0x00000000
67#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
68#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020069
70/***********************************/
71/* Current shared table sync state */
72/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010073#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
74#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020075
76/******************************/
77/* Remote peer teaching state */
78/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010079#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
80#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
81#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
82#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
83#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
84#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
85#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
86#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 +020087
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010088#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
89#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020090
Frédéric Lécaille54bff832019-03-26 10:25:20 +010091#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
92#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010093#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
94
Emeric Brunb3971ab2015-05-12 18:49:09 +020095/*****************************/
96/* Sync message class */
97/*****************************/
98enum {
99 PEER_MSG_CLASS_CONTROL = 0,
100 PEER_MSG_CLASS_ERROR,
101 PEER_MSG_CLASS_STICKTABLE = 10,
102 PEER_MSG_CLASS_RESERVED = 255,
103};
104
105/*****************************/
106/* control message types */
107/*****************************/
108enum {
109 PEER_MSG_CTRL_RESYNCREQ = 0,
110 PEER_MSG_CTRL_RESYNCFINISHED,
111 PEER_MSG_CTRL_RESYNCPARTIAL,
112 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100113 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200114};
115
116/*****************************/
117/* error message types */
118/*****************************/
119enum {
120 PEER_MSG_ERR_PROTOCOL = 0,
121 PEER_MSG_ERR_SIZELIMIT,
122};
123
Emeric Brun530ba382020-06-02 11:17:42 +0200124/* network key types;
125 * network types were directly and mistakenly
126 * mapped on sample types, to keep backward
127 * compatiblitiy we keep those values but
128 * we now use a internal/network mapping
129 * to avoid further mistakes adding or
130 * modifying internals types
131 */
132enum {
133 PEER_KT_ANY = 0, /* any type */
134 PEER_KT_RESV1, /* UNUSED */
135 PEER_KT_SINT, /* signed 64bits integer type */
136 PEER_KT_RESV3, /* UNUSED */
137 PEER_KT_IPV4, /* ipv4 type */
138 PEER_KT_IPV6, /* ipv6 type */
139 PEER_KT_STR, /* char string type */
140 PEER_KT_BIN, /* buffer type */
141 PEER_KT_TYPES /* number of types, must always be last */
142};
143
144/* Map used to retrieve network type from internal type
145 * Note: Undeclared mapping maps entry to PEER_KT_ANY == 0
146 */
147static int peer_net_key_type[SMP_TYPES] = {
148 [SMP_T_SINT] = PEER_KT_SINT,
149 [SMP_T_IPV4] = PEER_KT_IPV4,
150 [SMP_T_IPV6] = PEER_KT_IPV6,
151 [SMP_T_STR] = PEER_KT_STR,
152 [SMP_T_BIN] = PEER_KT_BIN,
153};
154
155/* Map used to retrieve internal type from external type
156 * Note: Undeclared mapping maps entry to SMP_T_ANY == 0
157 */
158static int peer_int_key_type[PEER_KT_TYPES] = {
159 [PEER_KT_SINT] = SMP_T_SINT,
160 [PEER_KT_IPV4] = SMP_T_IPV4,
161 [PEER_KT_IPV6] = SMP_T_IPV6,
162 [PEER_KT_STR] = SMP_T_STR,
163 [PEER_KT_BIN] = SMP_T_BIN,
164};
165
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100166/*
167 * Parameters used by functions to build peer protocol messages. */
168struct peer_prep_params {
169 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100170 struct peer *peer;
171 } hello;
172 struct {
173 unsigned int st1;
174 } error_status;
175 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100176 struct stksess *stksess;
177 struct shared_table *shared_table;
178 unsigned int updateid;
179 int use_identifier;
180 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200181 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100182 } updt;
183 struct {
184 struct shared_table *shared_table;
185 } swtch;
186 struct {
187 struct shared_table *shared_table;
188 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100189 struct {
190 unsigned char head[2];
191 } control;
192 struct {
193 unsigned char head[2];
194 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100195};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200196
197/*******************************/
198/* stick table sync mesg types */
199/* Note: ids >= 128 contains */
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500200/* id message contains data */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200201/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200202#define PEER_MSG_STKT_UPDATE 0x80
203#define PEER_MSG_STKT_INCUPDATE 0x81
204#define PEER_MSG_STKT_DEFINE 0x82
205#define PEER_MSG_STKT_SWITCH 0x83
206#define PEER_MSG_STKT_ACK 0x84
207#define PEER_MSG_STKT_UPDATE_TIMED 0x85
208#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +0200209/* All the stick-table message identifiers abova have the #7 bit set */
210#define PEER_MSG_STKT_BIT 7
211#define PEER_MSG_STKT_BIT_MASK (1 << PEER_MSG_STKT_BIT)
Emeric Brun2b920a12010-09-23 18:30:22 +0200212
Frédéric Lécaille39143342019-05-24 14:32:27 +0200213/* The maximum length of an encoded data length. */
214#define PEER_MSG_ENC_LENGTH_MAXLEN 5
215
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200216/* Minimum 64-bits value encoded with 2 bytes */
217#define PEER_ENC_2BYTES_MIN 0xf0 /* 0xf0 (or 240) */
218/* 3 bytes */
219#define PEER_ENC_3BYTES_MIN ((1ULL << 11) | PEER_ENC_2BYTES_MIN) /* 0x8f0 (or 2288) */
220/* 4 bytes */
221#define PEER_ENC_4BYTES_MIN ((1ULL << 18) | PEER_ENC_3BYTES_MIN) /* 0x408f0 (or 264432) */
222/* 5 bytes */
223#define PEER_ENC_5BYTES_MIN ((1ULL << 25) | PEER_ENC_4BYTES_MIN) /* 0x20408f0 (or 33818864) */
224/* 6 bytes */
225#define PEER_ENC_6BYTES_MIN ((1ULL << 32) | PEER_ENC_5BYTES_MIN) /* 0x1020408f0 (or 4328786160) */
226/* 7 bytes */
227#define PEER_ENC_7BYTES_MIN ((1ULL << 39) | PEER_ENC_6BYTES_MIN) /* 0x81020408f0 (or 554084600048) */
228/* 8 bytes */
229#define PEER_ENC_8BYTES_MIN ((1ULL << 46) | PEER_ENC_7BYTES_MIN) /* 0x4081020408f0 (or 70922828777712) */
230/* 9 bytes */
231#define PEER_ENC_9BYTES_MIN ((1ULL << 53) | PEER_ENC_8BYTES_MIN) /* 0x204081020408f0 (or 9078122083518704) */
232/* 10 bytes */
233#define PEER_ENC_10BYTES_MIN ((1ULL << 60) | PEER_ENC_9BYTES_MIN) /* 0x10204081020408f0 (or 1161999626690365680) */
234
235/* #7 bit used to detect the last byte to be encoded */
236#define PEER_ENC_STOP_BIT 7
237/* The byte minimum value with #7 bit set */
238#define PEER_ENC_STOP_BYTE (1 << PEER_ENC_STOP_BIT)
239/* The left most number of bits set for PEER_ENC_2BYTES_MIN */
240#define PEER_ENC_2BYTES_MIN_BITS 4
241
Frédéric Lécaille39143342019-05-24 14:32:27 +0200242#define PEER_MSG_HEADER_LEN 2
243
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200244#define PEER_STKT_CACHE_MAX_ENTRIES 128
245
Emeric Brun2b920a12010-09-23 18:30:22 +0200246/**********************************/
247/* Peer Session IO handler states */
248/**********************************/
249
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100250enum {
251 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
252 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
253 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
254 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100255 /* after this point, data were possibly exchanged */
256 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
257 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
258 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
259 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
260 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200261 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
262 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100263 PEER_SESS_ST_END, /* Killed session */
264};
Emeric Brun2b920a12010-09-23 18:30:22 +0200265
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100266/***************************************************/
267/* Peer Session status code - part of the protocol */
268/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200269
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100270#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
271#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200272
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100273#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200274
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100275#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200276
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100277#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
278#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
279#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
280#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200281
282#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200283#define PEER_MAJOR_VER 2
284#define PEER_MINOR_VER 1
285#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200286
Willy Tarreau6254a922019-01-29 17:45:23 +0100287static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200288struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200289static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200290
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200291static struct ebpt_node *dcache_tx_insert(struct dcache *dc,
292 struct dcache_tx_entry *i);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200293static inline void flush_dcache(struct peer *peer);
294
Frédéric Lécaille95679dc2019-04-15 10:25:27 +0200295static const char *statuscode_str(int statuscode)
296{
297 switch (statuscode) {
298 case PEER_SESS_SC_CONNECTCODE:
299 return "CONN";
300 case PEER_SESS_SC_CONNECTEDCODE:
301 return "HSHK";
302 case PEER_SESS_SC_SUCCESSCODE:
303 return "ESTA";
304 case PEER_SESS_SC_TRYAGAIN:
305 return "RETR";
306 case PEER_SESS_SC_ERRPROTO:
307 return "PROT";
308 case PEER_SESS_SC_ERRVERSION:
309 return "VERS";
310 case PEER_SESS_SC_ERRHOST:
311 return "NAME";
312 case PEER_SESS_SC_ERRPEER:
313 return "UNKN";
314 default:
315 return "NONE";
316 }
317}
318
Emeric Brun18928af2017-03-29 16:32:53 +0200319/* This function encode an uint64 to 'dynamic' length format.
320 The encoded value is written at address *str, and the
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500321 caller must assure that size after *str is large enough.
Emeric Brun18928af2017-03-29 16:32:53 +0200322 At return, the *str is set at the next Byte after then
323 encoded integer. The function returns then length of the
324 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200325int intencode(uint64_t i, char **str) {
326 int idx = 0;
327 unsigned char *msg;
328
Emeric Brunb3971ab2015-05-12 18:49:09 +0200329 msg = (unsigned char *)*str;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200330 if (i < PEER_ENC_2BYTES_MIN) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200331 msg[0] = (unsigned char)i;
332 *str = (char *)&msg[idx+1];
333 return (idx+1);
334 }
335
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200336 msg[idx] =(unsigned char)i | PEER_ENC_2BYTES_MIN;
337 i = (i - PEER_ENC_2BYTES_MIN) >> PEER_ENC_2BYTES_MIN_BITS;
338 while (i >= PEER_ENC_STOP_BYTE) {
339 msg[++idx] = (unsigned char)i | PEER_ENC_STOP_BYTE;
340 i = (i - PEER_ENC_STOP_BYTE) >> PEER_ENC_STOP_BIT;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200341 }
342 msg[++idx] = (unsigned char)i;
343 *str = (char *)&msg[idx+1];
344 return (idx+1);
345}
346
347
348/* This function returns the decoded integer or 0
349 if decode failed
350 *str point on the beginning of the integer to decode
351 at the end of decoding *str point on the end of the
352 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200353uint64_t intdecode(char **str, char *end)
354{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200355 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200356 uint64_t i;
357 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200358
359 if (!*str)
360 return 0;
361
362 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200363 if (msg >= (unsigned char *)end)
364 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200365
Emeric Brun18928af2017-03-29 16:32:53 +0200366 i = *(msg++);
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200367 if (i >= PEER_ENC_2BYTES_MIN) {
368 shift = PEER_ENC_2BYTES_MIN_BITS;
Emeric Brun18928af2017-03-29 16:32:53 +0200369 do {
370 if (msg >= (unsigned char *)end)
371 goto fail;
372 i += (uint64_t)*msg << shift;
Frédéric Lécaille32b55732019-06-03 18:29:51 +0200373 shift += PEER_ENC_STOP_BIT;
374 } while (*(msg++) >= PEER_ENC_STOP_BYTE);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200375 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100376 *str = (char *)msg;
377 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200378
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100379 fail:
380 *str = NULL;
381 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200382}
Emeric Brun2b920a12010-09-23 18:30:22 +0200383
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100384/*
385 * Build a "hello" peer protocol message.
386 * Return the number of written bytes written to build this messages if succeeded,
387 * 0 if not.
388 */
389static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
390{
391 int min_ver, ret;
392 struct peer *peer;
393
394 peer = p->hello.peer;
395 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
396 /* Prepare headers */
397 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
398 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
399 if (ret >= size)
400 return 0;
401
402 return ret;
403}
404
405/*
406 * Build a "handshake succeeded" status message.
407 * Return the number of written bytes written to build this messages if succeeded,
408 * 0 if not.
409 */
410static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
411{
412 int ret;
413
414 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
415 if (ret >= size)
416 return 0;
417
418 return ret;
419}
420
421/*
422 * Build an error status message.
423 * Return the number of written bytes written to build this messages if succeeded,
424 * 0 if not.
425 */
426static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
427{
428 int ret;
429 unsigned int st1;
430
431 st1 = p->error_status.st1;
432 ret = snprintf(msg, size, "%d\n", st1);
433 if (ret >= size)
434 return 0;
435
436 return ret;
437}
438
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200439/* Set the stick-table UPDATE message type byte at <msg_type> address,
440 * depending on <use_identifier> and <use_timed> boolean parameters.
441 * Always successful.
442 */
443static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
444{
445 if (use_timed) {
446 if (use_identifier)
447 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
448 else
449 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
450 }
451 else {
452 if (use_identifier)
453 *msg_type = PEER_MSG_STKT_UPDATE;
454 else
455 *msg_type = PEER_MSG_STKT_INCUPDATE;
456 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200457}
Emeric Brun2b920a12010-09-23 18:30:22 +0200458/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200459 * This prepare the data update message on the stick session <ts>, <st> is the considered
460 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800461 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200462 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
463 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200464 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100465static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200466{
467 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200468 unsigned short datalen;
469 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200470 unsigned int data_type;
471 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100472 struct stksess *ts;
473 struct shared_table *st;
474 unsigned int updateid;
475 int use_identifier;
476 int use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200477 struct peer *peer;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100478
479 ts = p->updt.stksess;
480 st = p->updt.shared_table;
481 updateid = p->updt.updateid;
482 use_identifier = p->updt.use_identifier;
483 use_timed = p->updt.use_timed;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200484 peer = p->updt.peer;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200485
Frédéric Lécaille0e8db972019-05-24 14:34:34 +0200486 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200487
Emeric Brun2b920a12010-09-23 18:30:22 +0200488 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200489
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500490 /* check if we need to send the update identifier */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200491 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200492 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200493 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200494
495 /* encode update identifier if needed */
496 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200497 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200498 memcpy(cursor, &netinteger, sizeof(netinteger));
499 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200500 }
501
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200502 if (use_timed) {
503 netinteger = htonl(tick_remain(now_ms, ts->expire));
504 memcpy(cursor, &netinteger, sizeof(netinteger));
505 cursor += sizeof(netinteger);
506 }
507
Emeric Brunb3971ab2015-05-12 18:49:09 +0200508 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200509 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200510 int stlen = strlen((char *)ts->key.key);
511
Emeric Brunb3971ab2015-05-12 18:49:09 +0200512 intencode(stlen, &cursor);
513 memcpy(cursor, ts->key.key, stlen);
514 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200515 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200516 else if (st->table->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +0100517 netinteger = htonl(read_u32(ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200518 memcpy(cursor, &netinteger, sizeof(netinteger));
519 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200520 }
521 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200522 memcpy(cursor, ts->key.key, st->table->key_size);
523 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200524 }
525
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100526 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200527 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200528 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200529
Emeric Brun94900952015-06-11 18:25:54 +0200530 data_ptr = stktable_data_ptr(st->table, ts, data_type);
531 if (data_ptr) {
532 switch (stktable_data_types[data_type].std_type) {
533 case STD_T_SINT: {
534 int data;
535
536 data = stktable_data_cast(data_ptr, std_t_sint);
537 intencode(data, &cursor);
538 break;
539 }
540 case STD_T_UINT: {
541 unsigned int data;
542
543 data = stktable_data_cast(data_ptr, std_t_uint);
544 intencode(data, &cursor);
545 break;
546 }
547 case STD_T_ULL: {
548 unsigned long long data;
549
550 data = stktable_data_cast(data_ptr, std_t_ull);
551 intencode(data, &cursor);
552 break;
553 }
554 case STD_T_FRQP: {
555 struct freq_ctr_period *frqp;
556
557 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
558 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
559 intencode(frqp->curr_ctr, &cursor);
560 intencode(frqp->prev_ctr, &cursor);
561 break;
562 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200563 case STD_T_DICT: {
564 struct dict_entry *de;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200565 struct ebpt_node *cached_de;
Willy Tarreau237f8ae2019-06-06 16:40:43 +0200566 struct dcache_tx_entry cde = { };
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200567 char *beg, *end;
568 size_t value_len, data_len;
569 struct dcache *dc;
570
571 de = stktable_data_cast(data_ptr, std_t_dict);
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100572 if (!de) {
573 /* No entry */
574 intencode(0, &cursor);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200575 break;
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +0100576 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200577
578 dc = peer->dcache;
Frédéric Lécaille6c391982019-06-06 11:34:03 +0200579 cde.entry.key = de;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200580 cached_de = dcache_tx_insert(dc, &cde);
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200581 if (cached_de == &cde.entry) {
582 if (cde.id + 1 >= PEER_ENC_2BYTES_MIN)
583 break;
584 /* Encode the length of the remaining data -> 1 */
585 intencode(1, &cursor);
586 /* Encode the cache entry ID */
587 intencode(cde.id + 1, &cursor);
588 }
589 else {
590 /* Leave enough room to encode the remaining data length. */
591 end = beg = cursor + PEER_MSG_ENC_LENGTH_MAXLEN;
592 /* Encode the dictionary entry key */
593 intencode(cde.id + 1, &end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200594 /* Encode the length of the dictionary entry data */
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200595 value_len = de->len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200596 intencode(value_len, &end);
597 /* Copy the data */
598 memcpy(end, de->value.key, value_len);
599 end += value_len;
Frédéric Lécaillefd827932019-06-07 10:34:04 +0200600 /* Encode the length of the data */
601 data_len = end - beg;
602 intencode(data_len, &cursor);
603 memmove(cursor, beg, data_len);
604 cursor += data_len;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200605 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200606 break;
607 }
Emeric Brun94900952015-06-11 18:25:54 +0200608 }
609 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200610 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100611 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200612
613 /* Compute datalen */
614 datalen = (cursor - datamsg);
615
616 /* prepare message header */
617 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200618 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200619 cursor = &msg[2];
620 intencode(datalen, &cursor);
621
622 /* move data after header */
623 memmove(cursor, datamsg, datalen);
624
625 /* return header size + data_len */
626 return (cursor - msg) + datalen;
627}
628
629/*
630 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800631 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200632 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
633 * check size)
634 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100635static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200636{
637 int len;
638 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200639 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200640 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200641 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200642 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100643 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200644
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100645 st = params->swtch.shared_table;
Frédéric Lécaille39143342019-05-24 14:32:27 +0200646 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200647
648 /* Encode data */
649
650 /* encode local id */
651 intencode(st->local_id, &cursor);
652
653 /* encode table name */
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100654 len = strlen(st->table->nid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200655 intencode(len, &cursor);
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +0100656 memcpy(cursor, st->table->nid, len);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200657 cursor += len;
658
659 /* encode table type */
660
Emeric Brun530ba382020-06-02 11:17:42 +0200661 intencode(peer_net_key_type[st->table->type], &cursor);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200662
663 /* encode table key size */
664 intencode(st->table->key_size, &cursor);
665
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200666 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200667 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200668 /* encode available known data types in table */
669 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
670 if (st->table->data_ofs[data_type]) {
671 switch (stktable_data_types[data_type].std_type) {
672 case STD_T_SINT:
673 case STD_T_UINT:
674 case STD_T_ULL:
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200675 case STD_T_DICT:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200676 data |= 1 << data_type;
677 break;
Emeric Brun94900952015-06-11 18:25:54 +0200678 case STD_T_FRQP:
679 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200680 intencode(data_type, &chunkq);
681 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200682 break;
683 }
684 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200685 }
686 intencode(data, &cursor);
687
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200688 /* Encode stick-table entries duration. */
689 intencode(st->table->expire, &cursor);
690
691 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200692 chunk->data = chunkq - chunkp;
693 memcpy(cursor, chunk->area, chunk->data);
694 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200695 }
696
Emeric Brunb3971ab2015-05-12 18:49:09 +0200697 /* Compute datalen */
698 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200699
Emeric Brunb3971ab2015-05-12 18:49:09 +0200700 /* prepare message header */
701 msg[0] = PEER_MSG_CLASS_STICKTABLE;
702 msg[1] = PEER_MSG_STKT_DEFINE;
703 cursor = &msg[2];
704 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200705
Emeric Brunb3971ab2015-05-12 18:49:09 +0200706 /* move data after header */
707 memmove(cursor, datamsg, datalen);
708
709 /* return header size + data_len */
710 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200711}
712
Emeric Brunb3971ab2015-05-12 18:49:09 +0200713/*
714 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
715 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800716 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200717 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
718 * check size)
719 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100720static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200721{
722 unsigned short datalen;
723 char *cursor, *datamsg;
724 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100725 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200726
Frédéric Lécaille39143342019-05-24 14:32:27 +0200727 cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200728
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100729 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200730 intencode(st->remote_id, &cursor);
731 netinteger = htonl(st->last_get);
732 memcpy(cursor, &netinteger, sizeof(netinteger));
733 cursor += sizeof(netinteger);
734
735 /* Compute datalen */
736 datalen = (cursor - datamsg);
737
738 /* prepare message header */
739 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200740 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200741 cursor = &msg[2];
742 intencode(datalen, &cursor);
743
744 /* move data after header */
745 memmove(cursor, datamsg, datalen);
746
747 /* return header size + data_len */
748 return (cursor - msg) + datalen;
749}
Emeric Brun2b920a12010-09-23 18:30:22 +0200750
751/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200752 * Function to deinit connected peer
753 */
754void __peer_session_deinit(struct peer *peer)
755{
756 struct stream_interface *si;
757 struct stream *s;
758 struct peers *peers;
759
760 if (!peer->appctx)
761 return;
762
763 si = peer->appctx->owner;
764 if (!si)
765 return;
766
767 s = si_strm(si);
768 if (!s)
769 return;
770
771 peers = strm_fe(s)->parent;
772 if (!peers)
773 return;
774
775 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
776 HA_ATOMIC_SUB(&connected_peers, 1);
777
778 HA_ATOMIC_SUB(&active_peers, 1);
779
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +0200780 flush_dcache(peer);
781
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200782 /* Re-init current table pointers to force announcement on re-connect */
783 peer->remote_table = peer->last_local_table = NULL;
784 peer->appctx = NULL;
785 if (peer->flags & PEER_F_LEARN_ASSIGN) {
786 /* unassign current peer for learning */
787 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
788 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
789
790 /* reschedule a resync */
791 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
792 }
793 /* reset teaching and learning flags to 0 */
794 peer->flags &= PEER_TEACH_RESET;
795 peer->flags &= PEER_LEARN_RESET;
Frédéric Lécaille3585cab2019-11-20 11:17:30 +0100796 /* set this peer as dead from heartbeat point of view */
797 peer->flags &= ~PEER_F_ALIVE;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200798 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
799}
800
801/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200802 * Callback to release a session with a peer
803 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200804static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200805{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200806 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200807
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100808 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100809 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200810 return;
811
812 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200813 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100814 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200815 if (peer->appctx == appctx)
816 __peer_session_deinit(peer);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100817 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200818 }
819}
820
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200821/* Retrieve the major and minor versions of peers protocol
822 * announced by a remote peer. <str> is a null-terminated
823 * string with the following format: "<maj_ver>.<min_ver>".
824 */
825static int peer_get_version(const char *str,
826 unsigned int *maj_ver, unsigned int *min_ver)
827{
828 unsigned int majv, minv;
829 const char *pos, *saved;
830 const char *end;
831
832 saved = pos = str;
833 end = str + strlen(str);
834
835 majv = read_uint(&pos, end);
836 if (saved == pos || *pos++ != '.')
837 return -1;
838
839 saved = pos;
840 minv = read_uint(&pos, end);
841 if (saved == pos || pos != end)
842 return -1;
843
844 *maj_ver = majv;
845 *min_ver = minv;
846
847 return 0;
848}
Emeric Brun2b920a12010-09-23 18:30:22 +0200849
850/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100851 * Parse a line terminated by an optional '\r' character, followed by a mandatory
852 * '\n' character.
853 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
854 * a line could not be read because the communication channel is closed.
855 */
856static inline int peer_getline(struct appctx *appctx)
857{
858 int n;
859 struct stream_interface *si = appctx->owner;
860
861 n = co_getline(si_oc(si), trash.area, trash.size);
862 if (!n)
863 return 0;
864
865 if (n < 0 || trash.area[n - 1] != '\n') {
866 appctx->st0 = PEER_SESS_ST_END;
867 return -1;
868 }
869
870 if (n > 1 && (trash.area[n - 2] == '\r'))
871 trash.area[n - 2] = 0;
872 else
873 trash.area[n - 1] = 0;
874
875 co_skip(si_oc(si), n);
876
877 return n;
878}
879
880/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100881 * Send a message after having called <peer_prepare_msg> to build it.
882 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
883 * Returns -1 if there was not enough room left to send the message,
884 * any other negative returned value must be considered as an error with an appcxt st0
885 * returned value equal to PEER_SESS_ST_END.
886 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100887static inline int peer_send_msg(struct appctx *appctx,
888 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
889 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100890{
891 int ret, msglen;
892 struct stream_interface *si = appctx->owner;
893
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100894 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100895 if (!msglen) {
896 /* internal error: message does not fit in trash */
897 appctx->st0 = PEER_SESS_ST_END;
898 return 0;
899 }
900
901 /* message to buffer */
902 ret = ci_putblk(si_ic(si), trash.area, msglen);
903 if (ret <= 0) {
904 if (ret == -1) {
905 /* No more write possible */
906 si_rx_room_blk(si);
907 return -1;
908 }
909 appctx->st0 = PEER_SESS_ST_END;
910 }
911
912 return ret;
913}
914
915/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100916 * Send a hello message.
917 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
918 * Returns -1 if there was not enough room left to send the message,
919 * any other negative returned value must be considered as an error with an appcxt st0
920 * returned value equal to PEER_SESS_ST_END.
921 */
922static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
923{
924 struct peer_prep_params p = {
925 .hello.peer = peer,
926 };
927
928 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
929}
930
931/*
932 * Send a success peer handshake status message.
933 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
934 * Returns -1 if there was not enough room left to send the message,
935 * any other negative returned value must be considered as an error with an appcxt st0
936 * returned value equal to PEER_SESS_ST_END.
937 */
938static inline int peer_send_status_successmsg(struct appctx *appctx)
939{
940 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
941}
942
943/*
944 * Send a peer handshake status error message.
945 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
946 * Returns -1 if there was not enough room left to send the message,
947 * any other negative returned value must be considered as an error with an appcxt st0
948 * returned value equal to PEER_SESS_ST_END.
949 */
950static inline int peer_send_status_errormsg(struct appctx *appctx)
951{
952 struct peer_prep_params p = {
953 .error_status.st1 = appctx->st1,
954 };
955
956 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
957}
958
959/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100960 * Send a stick-table switch message.
961 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
962 * Returns -1 if there was not enough room left to send the message,
963 * any other negative returned value must be considered as an error with an appcxt st0
964 * returned value equal to PEER_SESS_ST_END.
965 */
966static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
967{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100968 struct peer_prep_params p = {
969 .swtch.shared_table = st,
970 };
971
972 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100973}
974
975/*
976 * Send a stick-table update acknowledgement message.
977 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
978 * Returns -1 if there was not enough room left to send the message,
979 * any other negative returned value must be considered as an error with an appcxt st0
980 * returned value equal to PEER_SESS_ST_END.
981 */
982static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
983{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100984 struct peer_prep_params p = {
985 .ack.shared_table = st,
986 };
987
988 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100989}
990
991/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100992 * Send a stick-table update message.
993 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
994 * Returns -1 if there was not enough room left to send the message,
995 * any other negative returned value must be considered as an error with an appcxt st0
996 * returned value equal to PEER_SESS_ST_END.
997 */
998static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
999 unsigned int updateid, int use_identifier, int use_timed)
1000{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001001 struct peer_prep_params p = {
1002 .updt.stksess = ts,
1003 .updt.shared_table = st,
1004 .updt.updateid = updateid,
1005 .updt.use_identifier = use_identifier,
1006 .updt.use_timed = use_timed,
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001007 .updt.peer = appctx->ctx.peers.ptr,
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +01001008 };
1009
1010 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001011}
1012
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001013/*
1014 * Build a peer protocol control class message.
1015 * Returns the number of written bytes used to build the message if succeeded,
1016 * 0 if not.
1017 */
1018static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
1019{
1020 if (size < sizeof p->control.head)
1021 return 0;
1022
1023 msg[0] = p->control.head[0];
1024 msg[1] = p->control.head[1];
1025
1026 return 2;
1027}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001028
1029/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001030 * Send a stick-table synchronization request message.
1031 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1032 * Returns -1 if there was not enough room left to send the message,
1033 * any other negative returned value must be considered as an error with an appctx st0
1034 * returned value equal to PEER_SESS_ST_END.
1035 */
1036static inline int peer_send_resync_reqmsg(struct appctx *appctx)
1037{
1038 struct peer_prep_params p = {
1039 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
1040 };
1041
1042 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1043}
1044
1045/*
1046 * Send a stick-table synchronization confirmation message.
1047 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1048 * Returns -1 if there was not enough room left to send the message,
1049 * any other negative returned value must be considered as an error with an appctx st0
1050 * returned value equal to PEER_SESS_ST_END.
1051 */
1052static inline int peer_send_resync_confirmsg(struct appctx *appctx)
1053{
1054 struct peer_prep_params p = {
1055 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
1056 };
1057
1058 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1059}
1060
1061/*
1062 * Send a stick-table synchronization finished message.
1063 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1064 * Returns -1 if there was not enough room left to send the message,
1065 * any other negative returned value must be considered as an error with an appctx st0
1066 * returned value equal to PEER_SESS_ST_END.
1067 */
Emeric Brun70de43b2020-03-16 10:51:01 +01001068static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peers *peers)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001069{
1070 struct peer_prep_params p = {
1071 .control.head = { PEER_MSG_CLASS_CONTROL, },
1072 };
1073
Emeric Brun70de43b2020-03-16 10:51:01 +01001074 p.control.head[1] = (peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001075 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
1076
1077 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1078}
1079
1080/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001081 * Send a heartbeat message.
1082 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
1083 * Returns -1 if there was not enough room left to send the message,
1084 * any other negative returned value must be considered as an error with an appctx st0
1085 * returned value equal to PEER_SESS_ST_END.
1086 */
1087static inline int peer_send_heartbeatmsg(struct appctx *appctx)
1088{
1089 struct peer_prep_params p = {
1090 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
1091 };
1092
1093 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
1094}
1095
1096/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001097 * Build a peer protocol error class message.
1098 * Returns the number of written bytes used to build the message if succeeded,
1099 * 0 if not.
1100 */
1101static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
1102{
1103 if (size < sizeof p->error.head)
1104 return 0;
1105
1106 msg[0] = p->error.head[0];
1107 msg[1] = p->error.head[1];
1108
1109 return 2;
1110}
1111
1112/*
1113 * Send a "size limit reached" error message.
1114 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1115 * Returns -1 if there was not enough room left to send the message,
1116 * any other negative returned value must be considered as an error with an appctx st0
1117 * returned value equal to PEER_SESS_ST_END.
1118 */
1119static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
1120{
1121 struct peer_prep_params p = {
1122 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
1123 };
1124
1125 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1126}
1127
1128/*
1129 * Send a "peer protocol" error message.
1130 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1131 * Returns -1 if there was not enough room left to send the message,
1132 * any other negative returned value must be considered as an error with an appctx st0
1133 * returned value equal to PEER_SESS_ST_END.
1134 */
1135static inline int peer_send_error_protomsg(struct appctx *appctx)
1136{
1137 struct peer_prep_params p = {
1138 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
1139 };
1140
1141 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
1142}
1143
1144/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001145 * Function used to lookup for recent stick-table updates associated with
1146 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
1147 */
1148static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
1149{
1150 struct eb32_node *eb;
1151
1152 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1153 if (!eb) {
1154 eb = eb32_first(&st->table->updates);
1155 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
1156 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1157 return NULL;
1158 }
1159 }
1160
1161 if ((int)(eb->key - st->table->localupdate) > 0) {
1162 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1163 return NULL;
1164 }
1165
1166 return eb32_entry(eb, struct stksess, upd);
1167}
1168
1169/*
1170 * Function used to lookup for recent stick-table updates associated with
1171 * <st> shared stick-table during teach state 1 step.
1172 */
1173static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1174{
1175 struct eb32_node *eb;
1176
1177 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1178 if (!eb) {
1179 st->flags |= SHTABLE_F_TEACH_STAGE1;
1180 eb = eb32_first(&st->table->updates);
1181 if (eb)
1182 st->last_pushed = eb->key - 1;
1183 return NULL;
1184 }
1185
1186 return eb32_entry(eb, struct stksess, upd);
1187}
1188
1189/*
1190 * Function used to lookup for recent stick-table updates associated with
1191 * <st> shared stick-table during teach state 2 step.
1192 */
1193static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1194{
1195 struct eb32_node *eb;
1196
1197 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1198 if (!eb || eb->key > st->teaching_origin) {
1199 st->flags |= SHTABLE_F_TEACH_STAGE2;
1200 return NULL;
1201 }
1202
1203 return eb32_entry(eb, struct stksess, upd);
1204}
1205
1206/*
1207 * Generic function to emit update messages for <st> stick-table when a lesson must
1208 * be taught to the peer <p>.
1209 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1210 * this function, 0 if not.
1211 *
1212 * This function temporary unlock/lock <st> when it sends stick-table updates or
1213 * when decrementing its refcount in case of any error when it sends this updates.
1214 *
1215 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1216 * Returns -1 if there was not enough room left to send the message,
1217 * any other negative returned value must be considered as an error with an appcxt st0
1218 * returned value equal to PEER_SESS_ST_END.
1219 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1220 * unlocked if not already locked when entering this function.
1221 */
1222static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1223 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1224 struct shared_table *st, int locked)
1225{
1226 int ret, new_pushed, use_timed;
1227
1228 ret = 1;
1229 use_timed = 0;
1230 if (st != p->last_local_table) {
1231 ret = peer_send_switchmsg(st, appctx);
1232 if (ret <= 0)
1233 return ret;
1234
1235 p->last_local_table = st;
1236 }
1237
1238 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1239 use_timed = !(p->flags & PEER_F_DWNGRD);
1240
1241 /* We force new pushed to 1 to force identifier in update message */
1242 new_pushed = 1;
1243
1244 if (!locked)
1245 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1246
1247 while (1) {
1248 struct stksess *ts;
1249 unsigned updateid;
1250
1251 /* push local updates */
1252 ts = peer_stksess_lookup(st);
1253 if (!ts)
1254 break;
1255
1256 updateid = ts->upd.key;
1257 ts->ref_cnt++;
1258 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1259
1260 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1261 if (ret <= 0) {
1262 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1263 ts->ref_cnt--;
1264 if (!locked)
1265 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1266 return ret;
1267 }
1268
1269 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1270 ts->ref_cnt--;
1271 st->last_pushed = updateid;
1272
1273 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1274 (int)(st->last_pushed - st->table->commitupdate) > 0)
1275 st->table->commitupdate = st->last_pushed;
1276
1277 /* identifier may not needed in next update message */
1278 new_pushed = 0;
1279 }
1280
1281 out:
1282 if (!locked)
1283 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1284 return 1;
1285}
1286
1287/*
1288 * Function to emit update messages for <st> stick-table when a lesson must
1289 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1290 *
1291 * Note that <st> shared stick-table is locked when calling this function.
1292 *
1293 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1294 * Returns -1 if there was not enough room left to send the message,
1295 * any other negative returned value must be considered as an error with an appcxt st0
1296 * returned value equal to PEER_SESS_ST_END.
1297 */
1298static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1299 struct shared_table *st)
1300{
1301 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1302}
1303
1304/*
1305 * Function to emit update messages for <st> stick-table when a lesson must
1306 * be taught to the peer <p> during teach state 1 step.
1307 *
1308 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1309 * Returns -1 if there was not enough room left to send the message,
1310 * any other negative returned value must be considered as an error with an appcxt st0
1311 * returned value equal to PEER_SESS_ST_END.
1312 */
1313static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1314 struct shared_table *st)
1315{
1316 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1317}
1318
1319/*
1320 * Function to emit update messages for <st> stick-table when a lesson must
1321 * be taught to the peer <p> during teach state 1 step.
1322 *
1323 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1324 * Returns -1 if there was not enough room left to send the message,
1325 * any other negative returned value must be considered as an error with an appcxt st0
1326 * returned value equal to PEER_SESS_ST_END.
1327 */
1328static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1329 struct shared_table *st)
1330{
1331 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1332}
1333
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001334
1335/*
1336 * Function used to parse a stick-table update message after it has been received
1337 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1338 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1339 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1340 * was encountered.
1341 * <exp> must be set if the stick-table entry expires.
1342 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001343 * 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 +01001344 * update ID.
1345 * <totl> is the length of the stick-table update message computed upon receipt.
1346 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001347static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1348 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001349{
1350 struct stream_interface *si = appctx->owner;
1351 struct shared_table *st = p->remote_table;
1352 struct stksess *ts, *newts;
1353 uint32_t update;
1354 int expire;
1355 unsigned int data_type;
1356 void *data_ptr;
1357
1358 /* Here we have data message */
1359 if (!st)
1360 goto ignore_msg;
1361
1362 expire = MS_TO_TICKS(st->table->expire);
1363
1364 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001365 if (msg_len < sizeof(update))
1366 goto malformed_exit;
1367
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001368 memcpy(&update, *msg_cur, sizeof(update));
1369 *msg_cur += sizeof(update);
1370 st->last_get = htonl(update);
1371 }
1372 else {
1373 st->last_get++;
1374 }
1375
1376 if (exp) {
1377 size_t expire_sz = sizeof expire;
1378
Willy Tarreau1e82a142019-01-29 11:08:06 +01001379 if (*msg_cur + expire_sz > msg_end)
1380 goto malformed_exit;
1381
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001382 memcpy(&expire, *msg_cur, expire_sz);
1383 *msg_cur += expire_sz;
1384 expire = ntohl(expire);
1385 }
1386
1387 newts = stksess_new(st->table, NULL);
1388 if (!newts)
1389 goto ignore_msg;
1390
1391 if (st->table->type == SMP_T_STR) {
1392 unsigned int to_read, to_store;
1393
1394 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001395 if (!*msg_cur)
1396 goto malformed_free_newts;
1397
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001398 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001399 if (*msg_cur + to_store > msg_end)
1400 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001401
1402 memcpy(newts->key.key, *msg_cur, to_store);
1403 newts->key.key[to_store] = 0;
1404 *msg_cur += to_read;
1405 }
1406 else if (st->table->type == SMP_T_SINT) {
1407 unsigned int netinteger;
1408
Willy Tarreau1e82a142019-01-29 11:08:06 +01001409 if (*msg_cur + sizeof(netinteger) > msg_end)
1410 goto malformed_free_newts;
1411
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001412 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1413 netinteger = ntohl(netinteger);
1414 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1415 *msg_cur += sizeof(netinteger);
1416 }
1417 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001418 if (*msg_cur + st->table->key_size > msg_end)
1419 goto malformed_free_newts;
1420
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001421 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1422 *msg_cur += st->table->key_size;
1423 }
1424
1425 /* lookup for existing entry */
1426 ts = stktable_set_entry(st->table, newts);
1427 if (ts != newts) {
1428 stksess_free(st->table, newts);
1429 newts = NULL;
1430 }
1431
1432 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1433
1434 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001435 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001436
1437 if (!((1 << data_type) & st->remote_data))
1438 continue;
1439
Willy Tarreau1e82a142019-01-29 11:08:06 +01001440 decoded_int = intdecode(msg_cur, msg_end);
1441 if (!*msg_cur)
1442 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001443
Willy Tarreau1e82a142019-01-29 11:08:06 +01001444 switch (stktable_data_types[data_type].std_type) {
1445 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001446 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1447 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001448 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001449 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001450
Willy Tarreau1e82a142019-01-29 11:08:06 +01001451 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001452 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1453 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001454 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001455 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001456
Willy Tarreau1e82a142019-01-29 11:08:06 +01001457 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001458 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1459 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001460 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001461 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001462
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001463 case STD_T_FRQP: {
1464 struct freq_ctr_period data;
1465
1466 /* First bit is reserved for the freq_ctr_period lock
1467 Note: here we're still protected by the stksess lock
1468 so we don't need to update the update the freq_ctr_period
1469 using its internal lock */
1470
Willy Tarreau1e82a142019-01-29 11:08:06 +01001471 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001472 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001473 if (!*msg_cur)
1474 goto malformed_unlock;
1475
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001476 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001477 if (!*msg_cur)
1478 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001479
1480 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1481 if (data_ptr)
1482 stktable_data_cast(data_ptr, std_t_frqp) = data;
1483 break;
1484 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001485 case STD_T_DICT: {
1486 struct buffer *chunk;
1487 size_t data_len, value_len;
1488 unsigned int id;
1489 struct dict_entry *de;
1490 struct dcache *dc;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001491 char *end;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001492
Frédéric Lécailleaf9990f2019-11-13 17:50:34 +01001493 if (!decoded_int) {
1494 /* No entry. */
1495 break;
1496 }
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001497 data_len = decoded_int;
1498 if (*msg_cur + data_len > msg_end)
1499 goto malformed_unlock;
1500
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001501 /* Compute the end of the current data, <msg_end> being at the end of
1502 * the entire message.
1503 */
1504 end = *msg_cur + data_len;
1505 id = intdecode(msg_cur, end);
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001506 if (!*msg_cur || !id)
1507 goto malformed_unlock;
1508
1509 dc = p->dcache;
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001510 if (*msg_cur == end) {
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001511 /* Dictionary entry key without value. */
1512 if (id > dc->max_entries)
1513 break;
1514 /* IDs sent over the network are numbered from 1. */
1515 de = dc->rx[id - 1].de;
1516 }
1517 else {
1518 chunk = get_trash_chunk();
Frédéric Lécaille344e9482019-06-05 10:20:09 +02001519 value_len = intdecode(msg_cur, end);
1520 if (!*msg_cur || *msg_cur + value_len > end ||
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001521 unlikely(value_len + 1 >= chunk->size))
1522 goto malformed_unlock;
1523
1524 chunk_memcpy(chunk, *msg_cur, value_len);
1525 chunk->area[chunk->data] = '\0';
Frédéric Lécaille56aec0d2019-06-06 14:14:15 +02001526 *msg_cur += value_len;
1527
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02001528 de = dict_insert(&server_name_dict, chunk->area);
1529 dc->rx[id - 1].de = de;
1530 }
1531 if (de) {
1532 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1533 if (data_ptr)
1534 stktable_data_cast(data_ptr, std_t_dict) = de;
1535 }
1536 break;
1537 }
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001538 }
1539 }
1540 /* Force new expiration */
1541 ts->expire = tick_add(now_ms, expire);
1542
1543 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1544 stktable_touch_remote(st->table, ts, 1);
1545 return 1;
1546
1547 ignore_msg:
1548 /* skip consumed message */
1549 co_skip(si_oc(si), totl);
1550 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001551
1552 malformed_unlock:
1553 /* malformed message */
1554 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1555 stktable_touch_remote(st->table, ts, 1);
1556 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1557 return 0;
1558
1559 malformed_free_newts:
1560 /* malformed message */
1561 stksess_free(st->table, newts);
1562 malformed_exit:
1563 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1564 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001565}
1566
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001567/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001568 * Function used to parse a stick-table update acknowledgement message after it
1569 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1570 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1571 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1572 * was encountered.
1573 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1574 */
1575static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1576 char **msg_cur, char *msg_end)
1577{
1578 /* ack message */
1579 uint32_t table_id ;
1580 uint32_t update;
1581 struct shared_table *st;
1582
1583 table_id = intdecode(msg_cur, msg_end);
1584 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1585 /* malformed message */
1586 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1587 return 0;
1588 }
1589
1590 memcpy(&update, *msg_cur, sizeof(update));
1591 update = ntohl(update);
1592
1593 for (st = p->tables; st; st = st->next) {
1594 if (st->local_id == table_id) {
1595 st->update = update;
1596 break;
1597 }
1598 }
1599
1600 return 1;
1601}
1602
1603/*
1604 * Function used to parse a stick-table switch message after it has been received
1605 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1606 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1607 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1608 * was encountered.
1609 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1610 */
1611static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1612 char **msg_cur, char *msg_end)
1613{
1614 struct shared_table *st;
1615 int table_id;
1616
1617 table_id = intdecode(msg_cur, msg_end);
1618 if (!*msg_cur) {
1619 /* malformed message */
1620 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1621 return 0;
1622 }
1623
1624 p->remote_table = NULL;
1625 for (st = p->tables; st; st = st->next) {
1626 if (st->remote_id == table_id) {
1627 p->remote_table = st;
1628 break;
1629 }
1630 }
1631
1632 return 1;
1633}
1634
1635/*
1636 * Function used to parse a stick-table definition message after it has been received
1637 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1638 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1639 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1640 * was encountered.
1641 * <totl> is the length of the stick-table update message computed upon receipt.
1642 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1643 */
1644static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1645 char **msg_cur, char *msg_end, int totl)
1646{
1647 struct stream_interface *si = appctx->owner;
1648 int table_id_len;
1649 struct shared_table *st;
1650 int table_type;
1651 int table_keylen;
1652 int table_id;
1653 uint64_t table_data;
1654
1655 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001656 if (!*msg_cur)
1657 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001658
1659 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001660 if (!*msg_cur)
1661 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001662
1663 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001664 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1665 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001666
1667 for (st = p->tables; st; st = st->next) {
1668 /* Reset IDs */
1669 if (st->remote_id == table_id)
1670 st->remote_id = 0;
1671
Frédéric Lécaille7fcc24d2019-03-20 15:09:45 +01001672 if (!p->remote_table && (table_id_len == strlen(st->table->nid)) &&
1673 (memcmp(st->table->nid, *msg_cur, table_id_len) == 0))
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001674 p->remote_table = st;
1675 }
1676
1677 if (!p->remote_table)
1678 goto ignore_msg;
1679
1680 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001681 if (*msg_cur >= msg_end)
1682 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001683
1684 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001685 if (!*msg_cur)
1686 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001687
1688 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001689 if (!*msg_cur)
1690 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001691
1692 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001693 if (!*msg_cur)
1694 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001695
Emeric Brun530ba382020-06-02 11:17:42 +02001696 if (p->remote_table->table->type != peer_int_key_type[table_type]
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001697 || p->remote_table->table->key_size != table_keylen) {
1698 p->remote_table = NULL;
1699 goto ignore_msg;
1700 }
1701
1702 p->remote_table->remote_data = table_data;
1703 p->remote_table->remote_id = table_id;
1704 return 1;
1705
1706 ignore_msg:
1707 co_skip(si_oc(si), totl);
1708 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001709
1710 malformed_exit:
1711 /* malformed message */
1712 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1713 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001714}
1715
1716/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001717 * Receive a stick-table message.
1718 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1719 * -1 if there was an error updating the appctx state st0 accordingly.
1720 */
1721static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1722 uint32_t *msg_len, int *totl)
1723{
1724 int reql;
1725 struct stream_interface *si = appctx->owner;
1726
1727 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1728 if (reql <= 0) /* closed or EOL not found */
1729 goto incomplete;
1730
1731 *totl += reql;
1732
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02001733 if (!(msg_head[1] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001734 return 1;
1735
1736 /* Read and Decode message length */
1737 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1738 if (reql <= 0) /* closed */
1739 goto incomplete;
1740
1741 *totl += reql;
1742
Frédéric Lécaille32b55732019-06-03 18:29:51 +02001743 if ((unsigned int)msg_head[2] < PEER_ENC_2BYTES_MIN) {
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001744 *msg_len = msg_head[2];
1745 }
1746 else {
1747 int i;
1748 char *cur;
1749 char *end;
1750
1751 for (i = 3 ; i < msg_head_sz ; i++) {
1752 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1753 if (reql <= 0) /* closed */
1754 goto incomplete;
1755
1756 *totl += reql;
1757
Frédéric Lécaille36fb77e2019-06-04 08:28:19 +02001758 if (!(msg_head[i] & PEER_MSG_STKT_BIT_MASK))
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001759 break;
1760 }
1761
1762 if (i == msg_head_sz) {
1763 /* malformed message */
1764 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1765 return -1;
1766 }
1767 end = msg_head + msg_head_sz;
1768 cur = &msg_head[2];
1769 *msg_len = intdecode(&cur, end);
1770 if (!cur) {
1771 /* malformed message */
1772 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1773 return -1;
1774 }
1775 }
1776
1777 /* Read message content */
1778 if (*msg_len) {
1779 if (*msg_len > trash.size) {
1780 /* Status code is not success, abort */
1781 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1782 return -1;
1783 }
1784
1785 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1786 if (reql <= 0) /* closed */
1787 goto incomplete;
1788 *totl += reql;
1789 }
1790
1791 return 1;
1792
1793 incomplete:
1794 if (reql < 0) {
1795 /* there was an error */
1796 appctx->st0 = PEER_SESS_ST_END;
1797 return -1;
1798 }
1799
1800 return 0;
1801}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001802
1803/*
1804 * Treat the awaited message with <msg_head> as header.*
1805 * Return 1 if succeeded, 0 if not.
1806 */
1807static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1808 char **msg_cur, char *msg_end, int msg_len, int totl)
1809{
1810 struct stream_interface *si = appctx->owner;
1811 struct stream *s = si_strm(si);
1812 struct peers *peers = strm_fe(s)->parent;
1813
1814 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1815 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1816 struct shared_table *st;
1817 /* Reset message: remote need resync */
1818
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001819 /* prepare tables for a global push */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001820 for (st = peer->tables; st; st = st->next) {
1821 st->teaching_origin = st->last_pushed = st->table->update;
1822 st->flags = 0;
1823 }
1824
1825 /* reset teaching flags to 0 */
1826 peer->flags &= PEER_TEACH_RESET;
1827
1828 /* flag to start to teach lesson */
1829 peer->flags |= PEER_F_TEACH_PROCESS;
1830 }
1831 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1832 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1833 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1834 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1835 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1836 }
1837 peer->confirm++;
1838 }
1839 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1840 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1841 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1842 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1843
1844 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001845 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001846 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1847 }
1848 peer->confirm++;
1849 }
1850 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1851 struct shared_table *st;
1852
1853 /* If stopping state */
1854 if (stopping) {
1855 /* Close session, push resync no more needed */
1856 peer->flags |= PEER_F_TEACH_COMPLETE;
1857 appctx->st0 = PEER_SESS_ST_END;
1858 return 0;
1859 }
1860 for (st = peer->tables; st; st = st->next) {
1861 st->update = st->last_pushed = st->teaching_origin;
1862 st->flags = 0;
1863 }
1864
1865 /* reset teaching flags to 0 */
1866 peer->flags &= PEER_TEACH_RESET;
1867 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001868 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001869 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01001870 peer->rx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001871 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001872 }
1873 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1874 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1875 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1876 return 0;
1877 }
1878 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1879 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1880 return 0;
1881 }
1882 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1883 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1884 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1885 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1886 int update, expire;
1887
1888 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1889 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1890 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1891 msg_cur, msg_end, msg_len, totl))
1892 return 0;
1893
1894 }
1895 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1896 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1897 return 0;
1898 }
1899 }
1900 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1901 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1902 return 0;
1903 }
1904
1905 return 1;
1906}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001907
1908
1909/*
1910 * Send any message to <peer> peer.
1911 * Returns 1 if succeeded, or -1 or 0 if failed.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001912 * -1 means an internal error occurred, 0 is for a peer protocol error leading
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001913 * to a peer state change (from the peer I/O handler point of view).
1914 */
1915static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1916{
1917 int repl;
1918 struct stream_interface *si = appctx->owner;
1919 struct stream *s = si_strm(si);
1920 struct peers *peers = strm_fe(s)->parent;
1921
1922 /* Need to request a resync */
1923 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1924 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1925 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1926
1927 repl = peer_send_resync_reqmsg(appctx);
1928 if (repl <= 0)
1929 return repl;
1930
1931 peers->flags |= PEERS_F_RESYNC_PROCESS;
1932 }
1933
1934 /* Nothing to read, now we start to write */
1935 if (peer->tables) {
1936 struct shared_table *st;
1937 struct shared_table *last_local_table;
1938
1939 last_local_table = peer->last_local_table;
1940 if (!last_local_table)
1941 last_local_table = peer->tables;
1942 st = last_local_table->next;
1943
1944 while (1) {
1945 if (!st)
1946 st = peer->tables;
1947
1948 /* It remains some updates to ack */
1949 if (st->last_get != st->last_acked) {
1950 repl = peer_send_ackmsg(st, appctx);
1951 if (repl <= 0)
1952 return repl;
1953
1954 st->last_acked = st->last_get;
1955 }
1956
1957 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1958 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1959 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1960 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1961
1962 repl = peer_send_teach_process_msgs(appctx, peer, st);
1963 if (repl <= 0) {
1964 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1965 return repl;
1966 }
1967 }
1968 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1969 }
1970 else {
1971 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1972 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1973 if (repl <= 0)
1974 return repl;
1975 }
1976
1977 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1978 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1979 if (repl <= 0)
1980 return repl;
1981 }
1982 }
1983
1984 if (st == last_local_table)
1985 break;
1986 st = st->next;
1987 }
1988 }
1989
1990 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
Emeric Brun70de43b2020-03-16 10:51:01 +01001991 repl = peer_send_resync_finishedmsg(appctx, peers);
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001992 if (repl <= 0)
1993 return repl;
1994
1995 /* flag finished message sent */
1996 peer->flags |= PEER_F_TEACH_FINISHED;
1997 }
1998
1999 /* Confirm finished or partial messages */
2000 while (peer->confirm) {
2001 repl = peer_send_resync_confirmsg(appctx);
2002 if (repl <= 0)
2003 return repl;
2004
2005 peer->confirm--;
2006 }
2007
2008 return 1;
2009}
2010
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002011/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002012 * Read and parse a first line of a "hello" peer protocol message.
2013 * Returns 0 if could not read a line, -1 if there was a read error or
2014 * the line is malformed, 1 if succeeded.
2015 */
2016static inline int peer_getline_version(struct appctx *appctx,
2017 unsigned int *maj_ver, unsigned int *min_ver)
2018{
2019 int reql;
2020
2021 reql = peer_getline(appctx);
2022 if (!reql)
2023 return 0;
2024
2025 if (reql < 0)
2026 return -1;
2027
2028 /* test protocol */
2029 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
2030 appctx->st0 = PEER_SESS_ST_EXIT;
2031 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2032 return -1;
2033 }
2034 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
2035 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
2036 appctx->st0 = PEER_SESS_ST_EXIT;
2037 appctx->st1 = PEER_SESS_SC_ERRVERSION;
2038 return -1;
2039 }
2040
2041 return 1;
2042}
2043
2044/*
2045 * Read and parse a second line of a "hello" peer protocol message.
2046 * Returns 0 if could not read a line, -1 if there was a read error or
2047 * the line is malformed, 1 if succeeded.
2048 */
2049static inline int peer_getline_host(struct appctx *appctx)
2050{
2051 int reql;
2052
2053 reql = peer_getline(appctx);
2054 if (!reql)
2055 return 0;
2056
2057 if (reql < 0)
2058 return -1;
2059
2060 /* test hostname match */
2061 if (strcmp(localpeer, trash.area) != 0) {
2062 appctx->st0 = PEER_SESS_ST_EXIT;
2063 appctx->st1 = PEER_SESS_SC_ERRHOST;
2064 return -1;
2065 }
2066
2067 return 1;
2068}
2069
2070/*
2071 * Read and parse a last line of a "hello" peer protocol message.
2072 * Returns 0 if could not read a character, -1 if there was a read error or
2073 * the line is malformed, 1 if succeeded.
2074 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
2075 */
2076static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
2077{
2078 char *p;
2079 int reql;
2080 struct peer *peer;
2081 struct stream_interface *si = appctx->owner;
2082 struct stream *s = si_strm(si);
2083 struct peers *peers = strm_fe(s)->parent;
2084
2085 reql = peer_getline(appctx);
2086 if (!reql)
2087 return 0;
2088
2089 if (reql < 0)
2090 return -1;
2091
2092 /* parse line "<peer name> <pid> <relative_pid>" */
2093 p = strchr(trash.area, ' ');
2094 if (!p) {
2095 appctx->st0 = PEER_SESS_ST_EXIT;
2096 appctx->st1 = PEER_SESS_SC_ERRPROTO;
2097 return -1;
2098 }
2099 *p = 0;
2100
2101 /* lookup known peer */
2102 for (peer = peers->remote; peer; peer = peer->next) {
2103 if (strcmp(peer->id, trash.area) == 0)
2104 break;
2105 }
2106
2107 /* if unknown peer */
2108 if (!peer) {
2109 appctx->st0 = PEER_SESS_ST_EXIT;
2110 appctx->st1 = PEER_SESS_SC_ERRPEER;
2111 return -1;
2112 }
2113 *curpeer = peer;
2114
2115 return 1;
2116}
2117
2118/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002119 * Init <peer> peer after having accepted it at peer protocol level.
2120 */
2121static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
2122{
2123 struct shared_table *st;
2124
2125 /* Register status code */
2126 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
2127
2128 /* Awake main task */
2129 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
2130
2131 /* Init confirm counter */
2132 peer->confirm = 0;
2133
2134 /* Init cursors */
2135 for (st = peer->tables; st ; st = st->next) {
2136 st->last_get = st->last_acked = 0;
2137 st->teaching_origin = st->last_pushed = st->update;
2138 }
2139
2140 /* reset teaching and learning flags to 0 */
2141 peer->flags &= PEER_TEACH_RESET;
2142 peer->flags &= PEER_LEARN_RESET;
2143
2144 /* if current peer is local */
2145 if (peer->local) {
2146 /* if current host need resyncfrom local and no process assined */
2147 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
2148 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2149 /* assign local peer for a lesson, consider lesson already requested */
2150 peer->flags |= PEER_F_LEARN_ASSIGN;
2151 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
2152 }
2153
2154 }
2155 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2156 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2157 /* assign peer for a lesson */
2158 peer->flags |= PEER_F_LEARN_ASSIGN;
2159 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2160 }
2161}
2162
2163/*
2164 * Init <peer> peer after having connected it at peer protocol level.
2165 */
2166static inline void init_connected_peer(struct peer *peer, struct peers *peers)
2167{
2168 struct shared_table *st;
2169
2170 /* Init cursors */
2171 for (st = peer->tables; st ; st = st->next) {
2172 st->last_get = st->last_acked = 0;
2173 st->teaching_origin = st->last_pushed = st->update;
2174 }
2175
2176 /* Init confirm counter */
2177 peer->confirm = 0;
2178
2179 /* reset teaching and learning flags to 0 */
2180 peer->flags &= PEER_TEACH_RESET;
2181 peer->flags &= PEER_LEARN_RESET;
2182
2183 /* If current peer is local */
2184 if (peer->local) {
2185 /* flag to start to teach lesson */
2186 peer->flags |= PEER_F_TEACH_PROCESS;
2187 }
2188 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
2189 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
2190 /* If peer is remote and resync from remote is needed,
2191 and no peer currently assigned */
2192
2193 /* assign peer for a lesson */
2194 peer->flags |= PEER_F_LEARN_ASSIGN;
2195 peers->flags |= PEERS_F_RESYNC_ASSIGN;
2196 }
2197}
2198
2199/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002200 * IO Handler to handle message exchance with a peer
2201 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002202static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002203{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002204 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002205 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002206 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002207 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002208 int reql = 0;
2209 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002210 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002211 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002212
Joseph Herlant82b2f542018-11-15 12:19:14 -08002213 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002214 if (si_ic(si)->buf.size == 0) {
2215 si_rx_room_blk(si);
2216 goto out;
2217 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002218
Emeric Brun2b920a12010-09-23 18:30:22 +02002219 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002220 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002221switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002222 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002223 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002224 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002225 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002227 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002228 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002229 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002230 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002231 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2232 if (reql <= 0) {
2233 if (!reql)
2234 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002235 goto switchstate;
2236 }
2237
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002238 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002239 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002240 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002241 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002242 reql = peer_getline_host(appctx);
2243 if (reql <= 0) {
2244 if (!reql)
2245 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002246 goto switchstate;
2247 }
2248
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002249 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002250 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002251 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002252 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002253 reql = peer_getline_last(appctx, &curpeer);
2254 if (reql <= 0) {
2255 if (!reql)
2256 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002257 goto switchstate;
2258 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002259
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002260 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002261 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002262 if (curpeer->local) {
2263 /* Local connection, reply a retry */
2264 appctx->st0 = PEER_SESS_ST_EXIT;
2265 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2266 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002267 }
Emeric Brun80527f52017-06-19 17:46:37 +02002268
2269 /* we're killing a connection, we must apply a random delay before
2270 * retrying otherwise the other end will do the same and we can loop
2271 * for a while.
2272 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002273 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002274 peer_session_forceshutdown(curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002275 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002276 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2277 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2278 curpeer->flags |= PEER_F_DWNGRD;
2279 }
2280 else {
2281 curpeer->flags &= ~PEER_F_DWNGRD;
2282 }
2283 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002284 curpeer->appctx = appctx;
2285 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002286 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002287 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002288 /* fall through */
2289 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002290 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002291 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002292 if (!curpeer) {
2293 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002294 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002295 if (curpeer->appctx != appctx) {
2296 appctx->st0 = PEER_SESS_ST_END;
2297 goto switchstate;
2298 }
2299 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002300
2301 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002302 if (repl <= 0) {
2303 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002304 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002305 goto switchstate;
2306 }
2307
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002308 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002309
Emeric Brun2b920a12010-09-23 18:30:22 +02002310 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002311 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002312 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002313 goto switchstate;
2314 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002315 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002316 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002317 if (!curpeer) {
2318 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002319 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002320 if (curpeer->appctx != appctx) {
2321 appctx->st0 = PEER_SESS_ST_END;
2322 goto switchstate;
2323 }
2324 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002325
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002326 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002327 if (repl <= 0) {
2328 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002329 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002330 goto switchstate;
2331 }
2332
2333 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002334 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002335 /* fall through */
2336 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002337 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002338 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002339 if (!curpeer) {
2340 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002341 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002342 if (curpeer->appctx != appctx) {
2343 appctx->st0 = PEER_SESS_ST_END;
2344 goto switchstate;
2345 }
2346 }
2347
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002348 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002349 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002350
Frédéric Lécaillece025572019-01-21 13:38:06 +01002351 reql = peer_getline(appctx);
2352 if (!reql)
2353 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002354
Frédéric Lécaillece025572019-01-21 13:38:06 +01002355 if (reql < 0)
2356 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002357
2358 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002359 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002360
2361 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002362 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002363
2364 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002365 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002366 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002367 }
2368 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002369 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2370 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002371 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002372 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002373 goto switchstate;
2374 }
Olivier Houcharded879892019-03-08 18:53:43 +01002375 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002376 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002377 /* fall through */
2378 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002379 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002380 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002381 char *msg_cur = trash.area;
2382 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002383 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002384 int totl = 0;
2385
Willy Tarreau2d372c22018-11-05 17:12:27 +01002386 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002387 if (!curpeer) {
2388 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002389 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002390 if (curpeer->appctx != appctx) {
2391 appctx->st0 = PEER_SESS_ST_END;
2392 goto switchstate;
2393 }
2394 }
2395
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002396 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2397 if (reql <= 0) {
2398 if (reql == -1)
2399 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002400 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002401 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002402
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002403 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002404 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002405 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002406
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002407 curpeer->flags |= PEER_F_ALIVE;
2408
Emeric Brun2b920a12010-09-23 18:30:22 +02002409 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002410 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002411 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002412 goto switchstate;
2413
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002414send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002415 if (curpeer->flags & PEER_F_HEARTBEAT) {
2416 curpeer->flags &= ~PEER_F_HEARTBEAT;
2417 repl = peer_send_heartbeatmsg(appctx);
2418 if (repl <= 0) {
2419 if (repl == -1)
2420 goto out;
2421 goto switchstate;
2422 }
Frédéric Lécaille33cab3c2019-11-06 11:51:26 +01002423 curpeer->tx_hbt++;
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002424 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002425 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002426 repl = peer_send_msgs(appctx, curpeer);
2427 if (repl <= 0) {
2428 if (repl == -1)
2429 goto out;
2430 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002431 }
2432
Emeric Brun2b920a12010-09-23 18:30:22 +02002433 /* noting more to do */
2434 goto out;
2435 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002436 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002437 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002438 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002439 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002440 if (peer_send_status_errormsg(appctx) == -1)
2441 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002442 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002443 goto switchstate;
2444 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002445 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002446 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002447 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002448 if (peer_send_error_size_limitmsg(appctx) == -1)
2449 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002450 appctx->st0 = PEER_SESS_ST_END;
2451 goto switchstate;
2452 }
2453 case PEER_SESS_ST_ERRPROTO: {
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002454 if (curpeer)
2455 curpeer->proto_err++;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002456 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002457 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002458 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002459 if (peer_send_error_protomsg(appctx) == -1)
2460 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002461 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002462 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002463 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002464 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002465 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002466 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002467 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002468 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002469 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002470 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002471 curpeer = NULL;
2472 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002473 si_shutw(si);
2474 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002475 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002476 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002477 }
2478 }
2479 }
2480out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002481 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002482
2483 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002484 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002485 return;
2486}
2487
Willy Tarreau30576452015-04-13 13:50:30 +02002488static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002489 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002490 .name = "<PEER>", /* used for logging */
2491 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002492 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002493};
Emeric Brun2b920a12010-09-23 18:30:22 +02002494
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002495
Emeric Brun2b920a12010-09-23 18:30:22 +02002496/*
2497 * Use this function to force a close of a peer session
2498 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002499static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002500{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002501 struct appctx *appctx = peer->appctx;
2502
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002503 /* Note that the peer sessions which have just been created
2504 * (->st0 == PEER_SESS_ST_CONNECT) must not
2505 * be shutdown, if not, the TCP session will never be closed
2506 * and stay in CLOSE_WAIT state after having been closed by
2507 * the remote side.
2508 */
2509 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002510 return;
2511
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002512 if (appctx->applet != &peer_applet)
2513 return;
2514
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002515 __peer_session_deinit(peer);
2516
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002517 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002518 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002519}
2520
Willy Tarreau91d96282015-03-13 15:47:26 +01002521/* Pre-configures a peers frontend to accept incoming connections */
2522void peers_setup_frontend(struct proxy *fe)
2523{
2524 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002525 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002526 fe->maxconn = 0;
2527 fe->conn_retries = CONN_RETRIES;
2528 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002529 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002530 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002531 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002532 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002533}
2534
Emeric Brun2b920a12010-09-23 18:30:22 +02002535/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002536 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002537 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002538static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002539{
Willy Tarreau04b92862017-09-15 11:01:04 +02002540 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002541 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002542 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002543 struct stream *s;
Emeric Brun2b920a12010-09-23 18:30:22 +02002544
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002545 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002546 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002547 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002548 s = NULL;
2549
Emeric Brun1138fd02017-06-19 12:38:55 +02002550 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002551 if (!appctx)
2552 goto out_close;
2553
2554 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002555 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002556
Willy Tarreau04b92862017-09-15 11:01:04 +02002557 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002558 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002559 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002560 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002561 }
2562
Willy Tarreau87787ac2017-08-28 16:22:54 +02002563 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002564 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002565 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002566 }
2567
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002568 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002569 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002570 appctx_wakeup(appctx);
2571
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002572 /* initiate an outgoing connection */
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002573 s->target = peer_session_target(peer, s);
2574 if (!sockaddr_alloc(&s->target_addr))
2575 goto out_free_strm;
2576 *s->target_addr = peer->addr;
Willy Tarreau02efeda2019-07-18 17:21:24 +02002577 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Willy Tarreaudbd02672017-12-06 17:39:53 +01002578 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002579
Emeric Brun2b920a12010-09-23 18:30:22 +02002580 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002581 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002582
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002583 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002584
Emeric Brunb3971ab2015-05-12 18:49:09 +02002585 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002586 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002587 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002588 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002589
2590 /* Error unrolling */
Willy Tarreau15b5e142015-04-04 14:38:25 +02002591 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002592 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002593 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002594 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002595 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002596 out_free_appctx:
2597 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002598 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002599 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002600}
2601
2602/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002603 * Task processing function to manage re-connect, peer session
2604 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002605 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002606static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002607{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002608 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002609 struct peer *ps;
2610 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002611
2612 task->expire = TICK_ETERNITY;
2613
Emeric Brunb3971ab2015-05-12 18:49:09 +02002614 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002615 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002616 signal_unregister_handler(peers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +02002617 task_destroy(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002618 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002619 return NULL;
2620 }
2621
Emeric Brun80527f52017-06-19 17:46:37 +02002622 /* Acquire lock for all peers of the section */
2623 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002624 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002625
Emeric Brun2b920a12010-09-23 18:30:22 +02002626 if (!stopping) {
2627 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002628
2629 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2630 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2631 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002632 /* Resync from local peer needed
2633 no peer was assigned for the lesson
2634 and no old local peer found
2635 or resync timeout expire */
2636
2637 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002638 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002639
2640 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002641 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002642 }
2643
2644 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002645 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002646 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002647 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002648 if (!ps->appctx) {
2649 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002650 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002651 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002652 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002653 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002654 tick_is_expired(ps->reconnect, now_ms))) {
2655 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002656 * or previous peer connection established with success
2657 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002658 * and reconnection timer is expired */
2659
2660 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002661 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002662 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002663 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002664 /* If previous session failed during connection
2665 * but reconnection timer is not expired */
2666
2667 /* reschedule task for reconnect */
2668 task->expire = tick_first(task->expire, ps->reconnect);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002669 ps->new_conn++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002670 }
2671 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002672 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002673 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002674 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002675 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2676 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002677 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2678 /* Resync from a remote is needed
2679 * and no peer was assigned for lesson
2680 * and current peer may be up2date */
2681
2682 /* assign peer for the lesson */
2683 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002684 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002685
Willy Tarreau9df94c22016-10-31 18:42:52 +01002686 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002687 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002688 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002689 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002690 int update_to_push = 0;
2691
Emeric Brunb3971ab2015-05-12 18:49:09 +02002692 /* Awake session if there is data to push */
2693 for (st = ps->tables; st ; st = st->next) {
2694 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002695 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002696 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002697 /* There is no need to send a heartbeat message
2698 * when some updates must be pushed. The remote
2699 * peer will consider <ps> peer as alive when it will
2700 * receive these updates.
2701 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002702 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002703 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002704 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002705 /* We are going to send updates, let's ensure we will
2706 * come back to send heartbeat messages or to reconnect.
2707 */
2708 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002709 appctx_wakeup(ps->appctx);
2710 break;
2711 }
2712 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002713 /* When there are updates to send we do not reconnect
2714 * and do not send heartbeat message either.
2715 */
2716 if (!update_to_push) {
2717 if (tick_is_expired(ps->reconnect, now_ms)) {
2718 if (ps->flags & PEER_F_ALIVE) {
2719 /* This peer was alive during a 'reconnect' period.
2720 * Flag it as not alive again for the next period.
2721 */
2722 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002723 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002724 }
2725 else {
Willy Tarreau52bf8392020-03-08 00:42:37 +01002726 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002727 peer_session_forceshutdown(ps);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01002728 ps->no_hbt++;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002729 }
2730 }
2731 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2732 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2733 ps->flags |= PEER_F_HEARTBEAT;
2734 appctx_wakeup(ps->appctx);
2735 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002736 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002737 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002738 }
2739 /* else do nothing */
2740 } /* SUCCESSCODE */
2741 } /* !ps->peer->local */
2742 } /* for */
2743
2744 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002745 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2746 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2747 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002748 /* Resync from remote peer needed
2749 * no peer was assigned for the lesson
2750 * and resync timeout expire */
2751
2752 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002753 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002754 }
2755
Emeric Brunb3971ab2015-05-12 18:49:09 +02002756 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002757 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002758 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2759 if (!tick_is_expired(peers->resync_timeout, now_ms))
2760 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002761 }
2762 } /* !stopping */
2763 else {
2764 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002765 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002766 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002767 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002768 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002769 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002770 peers->flags |= PEERS_F_DONOTSTOP;
2771 ps = peers->local;
2772 for (st = ps->tables; st ; st = st->next)
2773 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002774 }
2775
2776 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002777 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002778 /* we're killing a connection, we must apply a random delay before
2779 * retrying otherwise the other end will do the same and we can loop
2780 * for a while.
2781 */
Willy Tarreau52bf8392020-03-08 00:42:37 +01002782 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002783 if (ps->appctx) {
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002784 peer_session_forceshutdown(ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002785 }
2786 }
2787 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002788
Emeric Brunb3971ab2015-05-12 18:49:09 +02002789 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002790 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002791 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002792 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002793 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002794 peers->flags &= ~PEERS_F_DONOTSTOP;
2795 for (st = ps->tables; st ; st = st->next)
2796 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002797 }
2798 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002799 else if (!ps->appctx) {
2800 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002801 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002802 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2803 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2804 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002805 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002806 * or previous peer connection was successfully established
2807 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002808 * or during previous connect, peer replies a try again statuscode */
2809
2810 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002811 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002812 }
2813 else {
2814 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002815 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002816 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002817 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002818 peers->flags &= ~PEERS_F_DONOTSTOP;
2819 for (st = ps->tables; st ; st = st->next)
2820 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002821 }
2822 }
2823 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002824 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002825 /* current peer connection is active and established
2826 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002827 for (st = ps->tables; st ; st = st->next) {
2828 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002829 appctx_wakeup(ps->appctx);
2830 break;
2831 }
2832 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002833 }
2834 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002835
2836 /* Release lock for all peers of the section */
2837 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002838 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002839
Emeric Brun2b920a12010-09-23 18:30:22 +02002840 /* Wakeup for re-connect */
2841 return task;
2842}
2843
Emeric Brunb3971ab2015-05-12 18:49:09 +02002844
Emeric Brun2b920a12010-09-23 18:30:22 +02002845/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002846 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002847 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002848int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002849{
Emeric Brun2b920a12010-09-23 18:30:22 +02002850 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002851
Emeric Brun2b920a12010-09-23 18:30:22 +02002852 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002853 peers->peers_fe->maxconn += 3;
2854 }
2855
Emeric Brunc60def82017-09-27 14:59:38 +02002856 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002857 if (!peers->sync_task)
2858 return 0;
2859
Emeric Brunb3971ab2015-05-12 18:49:09 +02002860 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002861 peers->sync_task->context = (void *)peers;
2862 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2863 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002864 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002865}
2866
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002867/*
2868 * Allocate a cache a dictionary entries used upon transmission.
2869 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002870static struct dcache_tx *new_dcache_tx(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002871{
2872 struct dcache_tx *d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002873 struct ebpt_node *entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002874
2875 d = malloc(sizeof *d);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002876 entries = calloc(max_entries, sizeof *entries);
2877 if (!d || !entries)
2878 goto err;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002879
2880 d->lru_key = 0;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002881 d->prev_lookup = NULL;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002882 d->cached_entries = EB_ROOT_UNIQUE;
2883 d->entries = entries;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002884
2885 return d;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002886
2887 err:
2888 free(d);
2889 free(entries);
2890 return NULL;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002891}
2892
2893/*
2894 * Allocate a cache of dictionary entries with <name> as name and <max_entries>
2895 * as maximum of entries.
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002896 * Return the dictionary cache if succeeded, NULL if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002897 * Must be deallocated calling free_dcache().
2898 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002899static struct dcache *new_dcache(size_t max_entries)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002900{
2901 struct dcache_tx *dc_tx;
2902 struct dcache *dc;
2903 struct dcache_rx *dc_rx;
2904
2905 dc = calloc(1, sizeof *dc);
2906 dc_tx = new_dcache_tx(max_entries);
2907 dc_rx = calloc(max_entries, sizeof *dc_rx);
2908 if (!dc || !dc_tx || !dc_rx)
2909 goto err;
2910
2911 dc->tx = dc_tx;
2912 dc->rx = dc_rx;
2913 dc->max_entries = max_entries;
2914
2915 return dc;
2916
2917 err:
2918 free(dc);
2919 free(dc_tx);
2920 free(dc_rx);
2921 return NULL;
2922}
2923
2924/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002925 * Look for the dictionary entry with the value of <i> in <d> cache of dictionary
2926 * entries used upon transmission.
2927 * Return the entry if found, NULL if not.
2928 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002929static struct ebpt_node *dcache_tx_lookup_value(struct dcache_tx *d,
2930 struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002931{
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002932 return ebpt_lookup(&d->cached_entries, i->entry.key);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002933}
2934
2935/*
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002936 * Flush <dc> cache.
2937 * Always succeeds.
2938 */
2939static inline void flush_dcache(struct peer *peer)
2940{
2941 int i;
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002942 struct dcache *dc = peer->dcache;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002943
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002944 for (i = 0; i < dc->max_entries; i++)
2945 ebpt_delete(&dc->tx->entries[i]);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002946
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002947 memset(dc->rx, 0, dc->max_entries * sizeof *dc->rx);
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002948}
2949
2950/*
2951 * Insert a dictionary entry in <dc> cache part used upon transmission (->tx)
2952 * with information provided by <i> dictionary cache entry (especially the value
2953 * to be inserted if not already). Return <i> if already present in the cache
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002954 * or something different of <i> if not.
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002955 */
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002956static struct ebpt_node *dcache_tx_insert(struct dcache *dc, struct dcache_tx_entry *i)
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002957{
2958 struct dcache_tx *dc_tx;
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002959 struct ebpt_node *o;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002960
2961 dc_tx = dc->tx;
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002962
2963 if (dc_tx->prev_lookup && dc_tx->prev_lookup->key == i->entry.key) {
2964 o = dc_tx->prev_lookup;
2965 } else {
2966 o = dcache_tx_lookup_value(dc_tx, i);
2967 if (o) {
2968 /* Save it */
2969 dc_tx->prev_lookup = o;
2970 }
2971 }
2972
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002973 if (o) {
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002974 /* Copy the ID. */
2975 i->id = o - dc->tx->entries;
2976 return &i->entry;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002977 }
2978
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002979 /* The new entry to put in cache */
Frédéric Lécailleb65717f2019-06-07 14:25:25 +02002980 dc_tx->prev_lookup = o = &dc_tx->entries[dc_tx->lru_key];
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002981
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002982 ebpt_delete(o);
2983 o->key = i->entry.key;
2984 ebpt_insert(&dc_tx->cached_entries, o);
2985 i->id = dc_tx->lru_key;
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002986
Frédéric Lécaille6c391982019-06-06 11:34:03 +02002987 /* Update the index for the next entry to put in cache */
2988 dc_tx->lru_key = (dc_tx->lru_key + 1) & (dc->max_entries - 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002989
Frédéric Lécaille74167b22019-05-28 19:02:42 +02002990 return o;
2991}
Emeric Brunb3971ab2015-05-12 18:49:09 +02002992
2993/*
Frédéric Lécaille8d78fa72019-05-20 18:22:52 +02002994 * Allocate a dictionary cache for each peer of <peers> section.
2995 * Return 1 if succeeded, 0 if not.
2996 */
2997int peers_alloc_dcache(struct peers *peers)
2998{
2999 struct peer *p;
3000
3001 for (p = peers->remote; p; p = p->next) {
3002 p->dcache = new_dcache(PEER_STKT_CACHE_MAX_ENTRIES);
3003 if (!p->dcache)
3004 return 0;
3005 }
3006
3007 return 1;
3008}
3009
3010/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02003011 * Function used to register a table for sync on a group of peers
3012 *
3013 */
3014void peers_register_table(struct peers *peers, struct stktable *table)
3015{
3016 struct shared_table *st;
3017 struct peer * curpeer;
3018 int id = 0;
3019
3020 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02003021 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02003022 st->table = table;
3023 st->next = curpeer->tables;
3024 if (curpeer->tables)
3025 id = curpeer->tables->local_id;
3026 st->local_id = id + 1;
3027
3028 curpeer->tables = st;
3029 }
3030
3031 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02003032}
3033
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003034/*
3035 * Parse the "show peers" command arguments.
3036 * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
3037 * error message.
3038 */
3039static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
3040{
3041 appctx->ctx.cfgpeers.target = NULL;
3042
3043 if (*args[2]) {
3044 struct peers *p;
3045
3046 for (p = cfg_peers; p; p = p->next) {
3047 if (!strcmp(p->id, args[2])) {
3048 appctx->ctx.cfgpeers.target = p;
3049 break;
3050 }
3051 }
3052
Willy Tarreau9d008692019-08-09 11:21:01 +02003053 if (!p)
3054 return cli_err(appctx, "No such peers\n");
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003055 }
3056
3057 return 0;
3058}
3059
3060/*
3061 * This function dumps the peer state information of <peers> "peers" section.
3062 * Returns 0 if the output buffer is full and needs to be called again, non-zero if not.
3063 * Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3064 */
3065static int peers_dump_head(struct buffer *msg, struct stream_interface *si, struct peers *peers)
3066{
3067 struct tm tm;
3068
3069 get_localtime(peers->last_change, &tm);
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003070 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 +02003071 peers,
3072 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3073 tm.tm_hour, tm.tm_min, tm.tm_sec,
3074 peers->id, peers->state, peers->flags,
3075 peers->resync_timeout ?
3076 tick_is_expired(peers->resync_timeout, now_ms) ? "<PAST>" :
3077 human_time(TICKS_TO_MS(peers->resync_timeout - now_ms),
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003078 TICKS_TO_MS(1000)) : "<NEVER>",
3079 peers->sync_task ? peers->sync_task->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003080
3081 if (ci_putchk(si_ic(si), msg) == -1) {
3082 si_rx_room_blk(si);
3083 return 0;
3084 }
3085
3086 return 1;
3087}
3088
3089/*
3090 * This function dumps <peer> state information.
3091 * Returns 0 if the output buffer is full and needs to be called again, non-zero
3092 * if not. Dedicated to be called by cli_io_handler_show_peers() cli I/O handler.
3093 */
3094static int peers_dump_peer(struct buffer *msg, struct stream_interface *si, struct peer *peer)
3095{
3096 struct connection *conn;
3097 char pn[INET6_ADDRSTRLEN];
3098 struct stream_interface *peer_si;
3099 struct stream *peer_s;
3100 struct appctx *appctx;
3101 struct shared_table *st;
3102
3103 addr_to_str(&peer->addr, pn, sizeof pn);
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003104 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 +02003105 peer, peer->id,
3106 peer->local ? "local" : "remote",
3107 pn, get_host_port(&peer->addr),
3108 statuscode_str(peer->statuscode),
3109 peer->reconnect ?
3110 tick_is_expired(peer->reconnect, now_ms) ? "<PAST>" :
3111 human_time(TICKS_TO_MS(peer->reconnect - now_ms),
3112 TICKS_TO_MS(1000)) : "<NEVER>",
Frédéric Lécailleec1c10b2019-11-07 15:22:33 +01003113 peer->confirm, peer->tx_hbt, peer->rx_hbt,
3114 peer->no_hbt, peer->new_conn, peer->proto_err);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003115
3116 chunk_appendf(&trash, " flags=0x%x", peer->flags);
3117
3118 appctx = peer->appctx;
3119 if (!appctx)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003120 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003121
Emeric Brun0bbec0f2019-04-18 11:39:43 +02003122 chunk_appendf(&trash, " appctx:%p st0=%d st1=%d task_calls=%u", appctx, appctx->st0, appctx->st1,
3123 appctx->t ? appctx->t->calls : 0);
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003124
3125 peer_si = peer->appctx->owner;
3126 if (!peer_si)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003127 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003128
3129 peer_s = si_strm(peer_si);
3130 if (!peer_s)
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003131 goto table_info;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003132
3133 chunk_appendf(&trash, " state=%s", si_state_str(si_opposite(peer_si)->state));
3134
3135 conn = objt_conn(strm_orig(peer_s));
3136 if (conn)
3137 chunk_appendf(&trash, "\n xprt=%s", conn_get_xprt_name(conn));
3138
Willy Tarreau3ca14902019-07-17 14:53:15 +02003139 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 +02003140 case AF_INET:
3141 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003142 chunk_appendf(&trash, " src=%s:%d", pn, get_host_port(conn->src));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003143 break;
3144 case AF_UNIX:
3145 chunk_appendf(&trash, " src=unix:%d", strm_li(peer_s)->luid);
3146 break;
3147 }
3148
Willy Tarreau3ca14902019-07-17 14:53:15 +02003149 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 +02003150 case AF_INET:
3151 case AF_INET6:
Willy Tarreau3ca14902019-07-17 14:53:15 +02003152 chunk_appendf(&trash, " addr=%s:%d", pn, get_host_port(conn->dst));
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003153 break;
3154 case AF_UNIX:
3155 chunk_appendf(&trash, " addr=unix:%d", strm_li(peer_s)->luid);
3156 break;
3157 }
3158
Frédéric Lécaille470502b2019-11-06 10:41:03 +01003159 table_info:
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003160 if (peer->remote_table)
3161 chunk_appendf(&trash, "\n remote_table:%p id=%s local_id=%d remote_id=%d",
3162 peer->remote_table,
3163 peer->remote_table->table->id,
3164 peer->remote_table->local_id,
3165 peer->remote_table->remote_id);
3166
3167 if (peer->last_local_table)
3168 chunk_appendf(&trash, "\n last_local_table:%p id=%s local_id=%d remote_id=%d",
3169 peer->last_local_table,
3170 peer->last_local_table->table->id,
3171 peer->last_local_table->local_id,
3172 peer->last_local_table->remote_id);
3173
3174 if (peer->tables) {
3175 chunk_appendf(&trash, "\n shared tables:");
3176 for (st = peer->tables; st; st = st->next) {
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003177 int i, count;
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003178 struct stktable *t;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003179 struct dcache *dcache;
3180
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003181 t = st->table;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003182 dcache = peer->dcache;
3183
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003184 chunk_appendf(&trash, "\n %p local_id=%d remote_id=%d "
3185 "flags=0x%x remote_data=0x%llx",
3186 st, st->local_id, st->remote_id,
3187 st->flags, (unsigned long long)st->remote_data);
3188 chunk_appendf(&trash, "\n last_acked=%u last_pushed=%u last_get=%u"
3189 " teaching_origin=%u update=%u",
3190 st->last_acked, st->last_pushed, st->last_get,
3191 st->teaching_origin, st->update);
3192 chunk_appendf(&trash, "\n table:%p id=%s update=%u localupdate=%u"
3193 " commitupdate=%u syncing=%u",
3194 t, t->id, t->update, t->localupdate, t->commitupdate, t->syncing);
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003195 chunk_appendf(&trash, "\n TX dictionary cache:");
3196 count = 0;
3197 for (i = 0; i < dcache->max_entries; i++) {
3198 struct ebpt_node *node;
3199 struct dict_entry *de;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003200
Frédéric Lécaille6c391982019-06-06 11:34:03 +02003201 node = &dcache->tx->entries[i];
3202 if (!node->key)
3203 break;
3204
3205 if (!count++)
3206 chunk_appendf(&trash, "\n ");
3207 de = node->key;
3208 chunk_appendf(&trash, " %3u -> %s", i, (char *)de->value.key);
3209 count &= 0x3;
Frédéric Lécaille62b0b0b2019-05-29 16:20:41 +02003210 }
3211 chunk_appendf(&trash, "\n RX dictionary cache:");
3212 count = 0;
3213 for (i = 0; i < dcache->max_entries; i++) {
3214 if (!count++)
3215 chunk_appendf(&trash, "\n ");
3216 chunk_appendf(&trash, " %3u -> %s", i,
3217 dcache->rx[i].de ?
3218 (char *)dcache->rx[i].de->value.key : "-");
3219 count &= 0x3;
3220 }
Frédéric Lécaille95679dc2019-04-15 10:25:27 +02003221 }
3222 }
3223
3224 end:
3225 chunk_appendf(&trash, "\n");
3226 if (ci_putchk(si_ic(si), msg) == -1) {
3227 si_rx_room_blk(si);
3228 return 0;
3229 }
3230
3231 return 1;
3232}
3233
3234/*
3235 * This function dumps all the peers of "peers" section.
3236 * Returns 0 if the output buffer is full and needs to be called
3237 * again, non-zero if not. It proceeds in an isolated thread, so
3238 * there is no thread safety issue here.
3239 */
3240static int cli_io_handler_show_peers(struct appctx *appctx)
3241{
3242 int show_all;
3243 int ret = 0, first_peers = 1;
3244 struct stream_interface *si = appctx->owner;
3245
3246 thread_isolate();
3247
3248 show_all = !appctx->ctx.cfgpeers.target;
3249
3250 chunk_reset(&trash);
3251
3252 while (appctx->st2 != STAT_ST_FIN) {
3253 switch (appctx->st2) {
3254 case STAT_ST_INIT:
3255 if (show_all)
3256 appctx->ctx.cfgpeers.peers = cfg_peers;
3257 else
3258 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target;
3259
3260 appctx->st2 = STAT_ST_LIST;
3261 /* fall through */
3262
3263 case STAT_ST_LIST:
3264 if (!appctx->ctx.cfgpeers.peers) {
3265 /* No more peers list. */
3266 appctx->st2 = STAT_ST_END;
3267 }
3268 else {
3269 if (!first_peers)
3270 chunk_appendf(&trash, "\n");
3271 else
3272 first_peers = 0;
3273 if (!peers_dump_head(&trash, si, appctx->ctx.cfgpeers.peers))
3274 goto out;
3275
3276 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote;
3277 appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next;
3278 appctx->st2 = STAT_ST_INFO;
3279 }
3280 break;
3281
3282 case STAT_ST_INFO:
3283 if (!appctx->ctx.cfgpeers.peer) {
3284 /* End of peer list */
3285 if (show_all)
3286 appctx->st2 = STAT_ST_LIST;
3287 else
3288 appctx->st2 = STAT_ST_END;
3289 }
3290 else {
3291 if (!peers_dump_peer(&trash, si, appctx->ctx.cfgpeers.peer))
3292 goto out;
3293
3294 appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next;
3295 }
3296 break;
3297
3298 case STAT_ST_END:
3299 appctx->st2 = STAT_ST_FIN;
3300 break;
3301 }
3302 }
3303 ret = 1;
3304 out:
3305 thread_release();
3306 return ret;
3307}
3308
3309/*
3310 * CLI keywords.
3311 */
3312static struct cli_kw_list cli_kws = {{ }, {
3313 { { "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, },
3314 {},
3315}};
3316
3317/* Register cli keywords */
3318INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3319