blob: 01120e61c1dee625dc80941983b278e2cec71224 [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020027#include <common/hathreads.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020028
29#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <types/listener.h>
31#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
33
34#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020035#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010038#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/log.h>
40#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020041#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020042#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020043#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020044#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010046#include <proto/signal.h>
47#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
51
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
64 to push data to new process */
Emeric Brun2b920a12010-09-23 18:30:22 +020065
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010066#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
67#define PEERS_RESYNC_FROMLOCAL 0x00000000
68#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
69#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
Emeric Brunb3971ab2015-05-12 18:49:09 +020070
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010074#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
75#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
77/******************************/
78/* Remote peer teaching state */
79/******************************/
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010080#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
81#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
82#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
83#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
84#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
85#define PEER_F_ALIVE 0x20000000 /* Used to flag a peer a alive. */
86#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
87#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 +020088
Frédéric Lécailleaba44a22019-03-26 10:18:07 +010089#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
90#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
Emeric Brun2b920a12010-09-23 18:30:22 +020091
Frédéric Lécaille54bff832019-03-26 10:25:20 +010092#define PEER_RESYNC_TIMEOUT 5000 /* 5 seconds */
93#define PEER_RECONNECT_TIMEOUT 5000 /* 5 seconds */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010094#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
95
Emeric Brunb3971ab2015-05-12 18:49:09 +020096/*****************************/
97/* Sync message class */
98/*****************************/
99enum {
100 PEER_MSG_CLASS_CONTROL = 0,
101 PEER_MSG_CLASS_ERROR,
102 PEER_MSG_CLASS_STICKTABLE = 10,
103 PEER_MSG_CLASS_RESERVED = 255,
104};
105
106/*****************************/
107/* control message types */
108/*****************************/
109enum {
110 PEER_MSG_CTRL_RESYNCREQ = 0,
111 PEER_MSG_CTRL_RESYNCFINISHED,
112 PEER_MSG_CTRL_RESYNCPARTIAL,
113 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100114 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200115};
116
117/*****************************/
118/* error message types */
119/*****************************/
120enum {
121 PEER_MSG_ERR_PROTOCOL = 0,
122 PEER_MSG_ERR_SIZELIMIT,
123};
124
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100125/*
126 * Parameters used by functions to build peer protocol messages. */
127struct peer_prep_params {
128 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100129 struct peer *peer;
130 } hello;
131 struct {
132 unsigned int st1;
133 } error_status;
134 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100135 struct stksess *stksess;
136 struct shared_table *shared_table;
137 unsigned int updateid;
138 int use_identifier;
139 int use_timed;
140 } updt;
141 struct {
142 struct shared_table *shared_table;
143 } swtch;
144 struct {
145 struct shared_table *shared_table;
146 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100147 struct {
148 unsigned char head[2];
149 } control;
150 struct {
151 unsigned char head[2];
152 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100153};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200154
155/*******************************/
156/* stick table sync mesg types */
157/* Note: ids >= 128 contains */
158/* id message cotains data */
159/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200160#define PEER_MSG_STKT_UPDATE 0x80
161#define PEER_MSG_STKT_INCUPDATE 0x81
162#define PEER_MSG_STKT_DEFINE 0x82
163#define PEER_MSG_STKT_SWITCH 0x83
164#define PEER_MSG_STKT_ACK 0x84
165#define PEER_MSG_STKT_UPDATE_TIMED 0x85
166#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200167
168/**********************************/
169/* Peer Session IO handler states */
170/**********************************/
171
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100172enum {
173 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
174 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
175 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
176 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100177 /* after this point, data were possibly exchanged */
178 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
179 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
180 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
181 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
182 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200183 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
184 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100185 PEER_SESS_ST_END, /* Killed session */
186};
Emeric Brun2b920a12010-09-23 18:30:22 +0200187
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100188/***************************************************/
189/* Peer Session status code - part of the protocol */
190/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200191
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100192#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
193#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200194
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100195#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200196
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100197#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200198
Frédéric Lécailleaba44a22019-03-26 10:18:07 +0100199#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
200#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
201#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
202#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200203
204#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200205#define PEER_MAJOR_VER 2
206#define PEER_MINOR_VER 1
207#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200208
Willy Tarreau6254a922019-01-29 17:45:23 +0100209static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200210struct peers *cfg_peers = NULL;
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200211static void peer_session_forceshutdown(struct peer *peer);
Emeric Brun2b920a12010-09-23 18:30:22 +0200212
Emeric Brun18928af2017-03-29 16:32:53 +0200213/* This function encode an uint64 to 'dynamic' length format.
214 The encoded value is written at address *str, and the
215 caller must assure that size after *str is large enought.
216 At return, the *str is set at the next Byte after then
217 encoded integer. The function returns then length of the
218 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200219int intencode(uint64_t i, char **str) {
220 int idx = 0;
221 unsigned char *msg;
222
Emeric Brunb3971ab2015-05-12 18:49:09 +0200223 msg = (unsigned char *)*str;
224 if (i < 240) {
225 msg[0] = (unsigned char)i;
226 *str = (char *)&msg[idx+1];
227 return (idx+1);
228 }
229
230 msg[idx] =(unsigned char)i | 240;
231 i = (i - 240) >> 4;
232 while (i >= 128) {
233 msg[++idx] = (unsigned char)i | 128;
234 i = (i - 128) >> 7;
235 }
236 msg[++idx] = (unsigned char)i;
237 *str = (char *)&msg[idx+1];
238 return (idx+1);
239}
240
241
242/* This function returns the decoded integer or 0
243 if decode failed
244 *str point on the beginning of the integer to decode
245 at the end of decoding *str point on the end of the
246 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200247uint64_t intdecode(char **str, char *end)
248{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200249 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200250 uint64_t i;
251 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200252
253 if (!*str)
254 return 0;
255
256 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200257 if (msg >= (unsigned char *)end)
258 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200259
Emeric Brun18928af2017-03-29 16:32:53 +0200260 i = *(msg++);
261 if (i >= 240) {
262 shift = 4;
263 do {
264 if (msg >= (unsigned char *)end)
265 goto fail;
266 i += (uint64_t)*msg << shift;
267 shift += 7;
268 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200269 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100270 *str = (char *)msg;
271 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200272
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100273 fail:
274 *str = NULL;
275 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200276}
Emeric Brun2b920a12010-09-23 18:30:22 +0200277
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100278/*
279 * Build a "hello" peer protocol message.
280 * Return the number of written bytes written to build this messages if succeeded,
281 * 0 if not.
282 */
283static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
284{
285 int min_ver, ret;
286 struct peer *peer;
287
288 peer = p->hello.peer;
289 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
290 /* Prepare headers */
291 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
292 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
293 if (ret >= size)
294 return 0;
295
296 return ret;
297}
298
299/*
300 * Build a "handshake succeeded" status message.
301 * Return the number of written bytes written to build this messages if succeeded,
302 * 0 if not.
303 */
304static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
305{
306 int ret;
307
308 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
309 if (ret >= size)
310 return 0;
311
312 return ret;
313}
314
315/*
316 * Build an error status message.
317 * Return the number of written bytes written to build this messages if succeeded,
318 * 0 if not.
319 */
320static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
321{
322 int ret;
323 unsigned int st1;
324
325 st1 = p->error_status.st1;
326 ret = snprintf(msg, size, "%d\n", st1);
327 if (ret >= size)
328 return 0;
329
330 return ret;
331}
332
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200333/* Set the stick-table UPDATE message type byte at <msg_type> address,
334 * depending on <use_identifier> and <use_timed> boolean parameters.
335 * Always successful.
336 */
337static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
338{
339 if (use_timed) {
340 if (use_identifier)
341 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
342 else
343 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
344 }
345 else {
346 if (use_identifier)
347 *msg_type = PEER_MSG_STKT_UPDATE;
348 else
349 *msg_type = PEER_MSG_STKT_INCUPDATE;
350 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200351}
Emeric Brun2b920a12010-09-23 18:30:22 +0200352/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200353 * This prepare the data update message on the stick session <ts>, <st> is the considered
354 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800355 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200356 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
357 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200358 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100359static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200360{
361 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200362 unsigned short datalen;
363 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200364 unsigned int data_type;
365 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100366 struct stksess *ts;
367 struct shared_table *st;
368 unsigned int updateid;
369 int use_identifier;
370 int use_timed;
371
372 ts = p->updt.stksess;
373 st = p->updt.shared_table;
374 updateid = p->updt.updateid;
375 use_identifier = p->updt.use_identifier;
376 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200377
378 cursor = datamsg = msg + 1 + 5;
379
Emeric Brun2b920a12010-09-23 18:30:22 +0200380 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200381
382 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200383 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200384 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200385 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200386
387 /* encode update identifier if needed */
388 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200389 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200390 memcpy(cursor, &netinteger, sizeof(netinteger));
391 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200392 }
393
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200394 if (use_timed) {
395 netinteger = htonl(tick_remain(now_ms, ts->expire));
396 memcpy(cursor, &netinteger, sizeof(netinteger));
397 cursor += sizeof(netinteger);
398 }
399
Emeric Brunb3971ab2015-05-12 18:49:09 +0200400 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200401 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200402 int stlen = strlen((char *)ts->key.key);
403
Emeric Brunb3971ab2015-05-12 18:49:09 +0200404 intencode(stlen, &cursor);
405 memcpy(cursor, ts->key.key, stlen);
406 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200407 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200408 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200409 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200410 memcpy(cursor, &netinteger, sizeof(netinteger));
411 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200412 }
413 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200414 memcpy(cursor, ts->key.key, st->table->key_size);
415 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200416 }
417
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100418 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200419 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200420 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200421
Emeric Brun94900952015-06-11 18:25:54 +0200422 data_ptr = stktable_data_ptr(st->table, ts, data_type);
423 if (data_ptr) {
424 switch (stktable_data_types[data_type].std_type) {
425 case STD_T_SINT: {
426 int data;
427
428 data = stktable_data_cast(data_ptr, std_t_sint);
429 intencode(data, &cursor);
430 break;
431 }
432 case STD_T_UINT: {
433 unsigned int data;
434
435 data = stktable_data_cast(data_ptr, std_t_uint);
436 intencode(data, &cursor);
437 break;
438 }
439 case STD_T_ULL: {
440 unsigned long long data;
441
442 data = stktable_data_cast(data_ptr, std_t_ull);
443 intencode(data, &cursor);
444 break;
445 }
446 case STD_T_FRQP: {
447 struct freq_ctr_period *frqp;
448
449 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
450 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
451 intencode(frqp->curr_ctr, &cursor);
452 intencode(frqp->prev_ctr, &cursor);
453 break;
454 }
455 }
456 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200457 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100458 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200459
460 /* Compute datalen */
461 datalen = (cursor - datamsg);
462
463 /* prepare message header */
464 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200465 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200466 cursor = &msg[2];
467 intencode(datalen, &cursor);
468
469 /* move data after header */
470 memmove(cursor, datamsg, datalen);
471
472 /* return header size + data_len */
473 return (cursor - msg) + datalen;
474}
475
476/*
477 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800478 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200479 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
480 * check size)
481 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100482static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200483{
484 int len;
485 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200486 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200487 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200488 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200489 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100490 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200491
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100492 st = params->swtch.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200493 cursor = datamsg = msg + 2 + 5;
494
495 /* Encode data */
496
497 /* encode local id */
498 intencode(st->local_id, &cursor);
499
500 /* encode table name */
501 len = strlen(st->table->id);
502 intencode(len, &cursor);
503 memcpy(cursor, st->table->id, len);
504 cursor += len;
505
506 /* encode table type */
507
508 intencode(st->table->type, &cursor);
509
510 /* encode table key size */
511 intencode(st->table->key_size, &cursor);
512
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200513 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200515 /* encode available known data types in table */
516 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
517 if (st->table->data_ofs[data_type]) {
518 switch (stktable_data_types[data_type].std_type) {
519 case STD_T_SINT:
520 case STD_T_UINT:
521 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200522 data |= 1 << data_type;
523 break;
Emeric Brun94900952015-06-11 18:25:54 +0200524 case STD_T_FRQP:
525 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200526 intencode(data_type, &chunkq);
527 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200528 break;
529 }
530 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200531 }
532 intencode(data, &cursor);
533
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200534 /* Encode stick-table entries duration. */
535 intencode(st->table->expire, &cursor);
536
537 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 chunk->data = chunkq - chunkp;
539 memcpy(cursor, chunk->area, chunk->data);
540 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200541 }
542
Emeric Brunb3971ab2015-05-12 18:49:09 +0200543 /* Compute datalen */
544 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200545
Emeric Brunb3971ab2015-05-12 18:49:09 +0200546 /* prepare message header */
547 msg[0] = PEER_MSG_CLASS_STICKTABLE;
548 msg[1] = PEER_MSG_STKT_DEFINE;
549 cursor = &msg[2];
550 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200551
Emeric Brunb3971ab2015-05-12 18:49:09 +0200552 /* move data after header */
553 memmove(cursor, datamsg, datalen);
554
555 /* return header size + data_len */
556 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200557}
558
Emeric Brunb3971ab2015-05-12 18:49:09 +0200559/*
560 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
561 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800562 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200563 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
564 * check size)
565 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100566static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200567{
568 unsigned short datalen;
569 char *cursor, *datamsg;
570 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100571 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200572
Emeric Brunb058f1c2015-09-22 15:50:18 +0200573 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200574
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100575 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200576 intencode(st->remote_id, &cursor);
577 netinteger = htonl(st->last_get);
578 memcpy(cursor, &netinteger, sizeof(netinteger));
579 cursor += sizeof(netinteger);
580
581 /* Compute datalen */
582 datalen = (cursor - datamsg);
583
584 /* prepare message header */
585 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200586 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200587 cursor = &msg[2];
588 intencode(datalen, &cursor);
589
590 /* move data after header */
591 memmove(cursor, datamsg, datalen);
592
593 /* return header size + data_len */
594 return (cursor - msg) + datalen;
595}
Emeric Brun2b920a12010-09-23 18:30:22 +0200596
597/*
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200598 * Function to deinit connected peer
599 */
600void __peer_session_deinit(struct peer *peer)
601{
602 struct stream_interface *si;
603 struct stream *s;
604 struct peers *peers;
605
606 if (!peer->appctx)
607 return;
608
609 si = peer->appctx->owner;
610 if (!si)
611 return;
612
613 s = si_strm(si);
614 if (!s)
615 return;
616
617 peers = strm_fe(s)->parent;
618 if (!peers)
619 return;
620
621 if (peer->appctx->st0 == PEER_SESS_ST_WAITMSG)
622 HA_ATOMIC_SUB(&connected_peers, 1);
623
624 HA_ATOMIC_SUB(&active_peers, 1);
625
626 /* Re-init current table pointers to force announcement on re-connect */
627 peer->remote_table = peer->last_local_table = NULL;
628 peer->appctx = NULL;
629 if (peer->flags & PEER_F_LEARN_ASSIGN) {
630 /* unassign current peer for learning */
631 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
632 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
633
634 /* reschedule a resync */
635 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
636 }
637 /* reset teaching and learning flags to 0 */
638 peer->flags &= PEER_TEACH_RESET;
639 peer->flags &= PEER_LEARN_RESET;
640 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
641}
642
643/*
Emeric Brun2b920a12010-09-23 18:30:22 +0200644 * Callback to release a session with a peer
645 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200646static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200647{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200648 struct peer *peer = appctx->ctx.peers.ptr;
Emeric Brun2b920a12010-09-23 18:30:22 +0200649
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100650 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100651 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200652 return;
653
654 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200655 if (peer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100656 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Emeric Brun9ef2ad72019-04-02 17:22:01 +0200657 if (peer->appctx == appctx)
658 __peer_session_deinit(peer);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100659 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +0200660 }
661}
662
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200663/* Retrieve the major and minor versions of peers protocol
664 * announced by a remote peer. <str> is a null-terminated
665 * string with the following format: "<maj_ver>.<min_ver>".
666 */
667static int peer_get_version(const char *str,
668 unsigned int *maj_ver, unsigned int *min_ver)
669{
670 unsigned int majv, minv;
671 const char *pos, *saved;
672 const char *end;
673
674 saved = pos = str;
675 end = str + strlen(str);
676
677 majv = read_uint(&pos, end);
678 if (saved == pos || *pos++ != '.')
679 return -1;
680
681 saved = pos;
682 minv = read_uint(&pos, end);
683 if (saved == pos || pos != end)
684 return -1;
685
686 *maj_ver = majv;
687 *min_ver = minv;
688
689 return 0;
690}
Emeric Brun2b920a12010-09-23 18:30:22 +0200691
692/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100693 * Parse a line terminated by an optional '\r' character, followed by a mandatory
694 * '\n' character.
695 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
696 * a line could not be read because the communication channel is closed.
697 */
698static inline int peer_getline(struct appctx *appctx)
699{
700 int n;
701 struct stream_interface *si = appctx->owner;
702
703 n = co_getline(si_oc(si), trash.area, trash.size);
704 if (!n)
705 return 0;
706
707 if (n < 0 || trash.area[n - 1] != '\n') {
708 appctx->st0 = PEER_SESS_ST_END;
709 return -1;
710 }
711
712 if (n > 1 && (trash.area[n - 2] == '\r'))
713 trash.area[n - 2] = 0;
714 else
715 trash.area[n - 1] = 0;
716
717 co_skip(si_oc(si), n);
718
719 return n;
720}
721
722/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100723 * Send a message after having called <peer_prepare_msg> to build it.
724 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
725 * Returns -1 if there was not enough room left to send the message,
726 * any other negative returned value must be considered as an error with an appcxt st0
727 * returned value equal to PEER_SESS_ST_END.
728 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100729static inline int peer_send_msg(struct appctx *appctx,
730 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
731 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100732{
733 int ret, msglen;
734 struct stream_interface *si = appctx->owner;
735
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100736 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100737 if (!msglen) {
738 /* internal error: message does not fit in trash */
739 appctx->st0 = PEER_SESS_ST_END;
740 return 0;
741 }
742
743 /* message to buffer */
744 ret = ci_putblk(si_ic(si), trash.area, msglen);
745 if (ret <= 0) {
746 if (ret == -1) {
747 /* No more write possible */
748 si_rx_room_blk(si);
749 return -1;
750 }
751 appctx->st0 = PEER_SESS_ST_END;
752 }
753
754 return ret;
755}
756
757/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100758 * Send a hello message.
759 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
760 * Returns -1 if there was not enough room left to send the message,
761 * any other negative returned value must be considered as an error with an appcxt st0
762 * returned value equal to PEER_SESS_ST_END.
763 */
764static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
765{
766 struct peer_prep_params p = {
767 .hello.peer = peer,
768 };
769
770 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
771}
772
773/*
774 * Send a success peer handshake status message.
775 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
776 * Returns -1 if there was not enough room left to send the message,
777 * any other negative returned value must be considered as an error with an appcxt st0
778 * returned value equal to PEER_SESS_ST_END.
779 */
780static inline int peer_send_status_successmsg(struct appctx *appctx)
781{
782 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
783}
784
785/*
786 * Send a peer handshake status error message.
787 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
788 * Returns -1 if there was not enough room left to send the message,
789 * any other negative returned value must be considered as an error with an appcxt st0
790 * returned value equal to PEER_SESS_ST_END.
791 */
792static inline int peer_send_status_errormsg(struct appctx *appctx)
793{
794 struct peer_prep_params p = {
795 .error_status.st1 = appctx->st1,
796 };
797
798 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
799}
800
801/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100802 * Send a stick-table switch message.
803 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
804 * Returns -1 if there was not enough room left to send the message,
805 * any other negative returned value must be considered as an error with an appcxt st0
806 * returned value equal to PEER_SESS_ST_END.
807 */
808static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
809{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100810 struct peer_prep_params p = {
811 .swtch.shared_table = st,
812 };
813
814 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100815}
816
817/*
818 * Send a stick-table update acknowledgement message.
819 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
820 * Returns -1 if there was not enough room left to send the message,
821 * any other negative returned value must be considered as an error with an appcxt st0
822 * returned value equal to PEER_SESS_ST_END.
823 */
824static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
825{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100826 struct peer_prep_params p = {
827 .ack.shared_table = st,
828 };
829
830 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100831}
832
833/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100834 * Send a stick-table update message.
835 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
836 * Returns -1 if there was not enough room left to send the message,
837 * any other negative returned value must be considered as an error with an appcxt st0
838 * returned value equal to PEER_SESS_ST_END.
839 */
840static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
841 unsigned int updateid, int use_identifier, int use_timed)
842{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100843 struct peer_prep_params p = {
844 .updt.stksess = ts,
845 .updt.shared_table = st,
846 .updt.updateid = updateid,
847 .updt.use_identifier = use_identifier,
848 .updt.use_timed = use_timed,
849 };
850
851 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100852}
853
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100854/*
855 * Build a peer protocol control class message.
856 * Returns the number of written bytes used to build the message if succeeded,
857 * 0 if not.
858 */
859static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
860{
861 if (size < sizeof p->control.head)
862 return 0;
863
864 msg[0] = p->control.head[0];
865 msg[1] = p->control.head[1];
866
867 return 2;
868}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100869
870/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100871 * Send a stick-table synchronization request message.
872 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
873 * Returns -1 if there was not enough room left to send the message,
874 * any other negative returned value must be considered as an error with an appctx st0
875 * returned value equal to PEER_SESS_ST_END.
876 */
877static inline int peer_send_resync_reqmsg(struct appctx *appctx)
878{
879 struct peer_prep_params p = {
880 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
881 };
882
883 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
884}
885
886/*
887 * Send a stick-table synchronization confirmation message.
888 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
889 * Returns -1 if there was not enough room left to send the message,
890 * any other negative returned value must be considered as an error with an appctx st0
891 * returned value equal to PEER_SESS_ST_END.
892 */
893static inline int peer_send_resync_confirmsg(struct appctx *appctx)
894{
895 struct peer_prep_params p = {
896 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
897 };
898
899 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
900}
901
902/*
903 * Send a stick-table synchronization finished message.
904 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
905 * Returns -1 if there was not enough room left to send the message,
906 * any other negative returned value must be considered as an error with an appctx st0
907 * returned value equal to PEER_SESS_ST_END.
908 */
909static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
910{
911 struct peer_prep_params p = {
912 .control.head = { PEER_MSG_CLASS_CONTROL, },
913 };
914
915 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
916 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
917
918 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
919}
920
921/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100922 * Send a heartbeat message.
923 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
924 * Returns -1 if there was not enough room left to send the message,
925 * any other negative returned value must be considered as an error with an appctx st0
926 * returned value equal to PEER_SESS_ST_END.
927 */
928static inline int peer_send_heartbeatmsg(struct appctx *appctx)
929{
930 struct peer_prep_params p = {
931 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
932 };
933
934 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
935}
936
937/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100938 * Build a peer protocol error class message.
939 * Returns the number of written bytes used to build the message if succeeded,
940 * 0 if not.
941 */
942static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
943{
944 if (size < sizeof p->error.head)
945 return 0;
946
947 msg[0] = p->error.head[0];
948 msg[1] = p->error.head[1];
949
950 return 2;
951}
952
953/*
954 * Send a "size limit reached" error message.
955 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
956 * Returns -1 if there was not enough room left to send the message,
957 * any other negative returned value must be considered as an error with an appctx st0
958 * returned value equal to PEER_SESS_ST_END.
959 */
960static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
961{
962 struct peer_prep_params p = {
963 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
964 };
965
966 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
967}
968
969/*
970 * Send a "peer protocol" error message.
971 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
972 * Returns -1 if there was not enough room left to send the message,
973 * any other negative returned value must be considered as an error with an appctx st0
974 * returned value equal to PEER_SESS_ST_END.
975 */
976static inline int peer_send_error_protomsg(struct appctx *appctx)
977{
978 struct peer_prep_params p = {
979 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
980 };
981
982 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
983}
984
985/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100986 * Function used to lookup for recent stick-table updates associated with
987 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
988 */
989static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
990{
991 struct eb32_node *eb;
992
993 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
994 if (!eb) {
995 eb = eb32_first(&st->table->updates);
996 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
997 st->table->commitupdate = st->last_pushed = st->table->localupdate;
998 return NULL;
999 }
1000 }
1001
1002 if ((int)(eb->key - st->table->localupdate) > 0) {
1003 st->table->commitupdate = st->last_pushed = st->table->localupdate;
1004 return NULL;
1005 }
1006
1007 return eb32_entry(eb, struct stksess, upd);
1008}
1009
1010/*
1011 * Function used to lookup for recent stick-table updates associated with
1012 * <st> shared stick-table during teach state 1 step.
1013 */
1014static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
1015{
1016 struct eb32_node *eb;
1017
1018 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1019 if (!eb) {
1020 st->flags |= SHTABLE_F_TEACH_STAGE1;
1021 eb = eb32_first(&st->table->updates);
1022 if (eb)
1023 st->last_pushed = eb->key - 1;
1024 return NULL;
1025 }
1026
1027 return eb32_entry(eb, struct stksess, upd);
1028}
1029
1030/*
1031 * Function used to lookup for recent stick-table updates associated with
1032 * <st> shared stick-table during teach state 2 step.
1033 */
1034static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1035{
1036 struct eb32_node *eb;
1037
1038 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1039 if (!eb || eb->key > st->teaching_origin) {
1040 st->flags |= SHTABLE_F_TEACH_STAGE2;
1041 return NULL;
1042 }
1043
1044 return eb32_entry(eb, struct stksess, upd);
1045}
1046
1047/*
1048 * Generic function to emit update messages for <st> stick-table when a lesson must
1049 * be taught to the peer <p>.
1050 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1051 * this function, 0 if not.
1052 *
1053 * This function temporary unlock/lock <st> when it sends stick-table updates or
1054 * when decrementing its refcount in case of any error when it sends this updates.
1055 *
1056 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1057 * Returns -1 if there was not enough room left to send the message,
1058 * any other negative returned value must be considered as an error with an appcxt st0
1059 * returned value equal to PEER_SESS_ST_END.
1060 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1061 * unlocked if not already locked when entering this function.
1062 */
1063static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1064 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1065 struct shared_table *st, int locked)
1066{
1067 int ret, new_pushed, use_timed;
1068
1069 ret = 1;
1070 use_timed = 0;
1071 if (st != p->last_local_table) {
1072 ret = peer_send_switchmsg(st, appctx);
1073 if (ret <= 0)
1074 return ret;
1075
1076 p->last_local_table = st;
1077 }
1078
1079 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1080 use_timed = !(p->flags & PEER_F_DWNGRD);
1081
1082 /* We force new pushed to 1 to force identifier in update message */
1083 new_pushed = 1;
1084
1085 if (!locked)
1086 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1087
1088 while (1) {
1089 struct stksess *ts;
1090 unsigned updateid;
1091
1092 /* push local updates */
1093 ts = peer_stksess_lookup(st);
1094 if (!ts)
1095 break;
1096
1097 updateid = ts->upd.key;
1098 ts->ref_cnt++;
1099 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1100
1101 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1102 if (ret <= 0) {
1103 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1104 ts->ref_cnt--;
1105 if (!locked)
1106 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1107 return ret;
1108 }
1109
1110 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1111 ts->ref_cnt--;
1112 st->last_pushed = updateid;
1113
1114 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1115 (int)(st->last_pushed - st->table->commitupdate) > 0)
1116 st->table->commitupdate = st->last_pushed;
1117
1118 /* identifier may not needed in next update message */
1119 new_pushed = 0;
1120 }
1121
1122 out:
1123 if (!locked)
1124 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1125 return 1;
1126}
1127
1128/*
1129 * Function to emit update messages for <st> stick-table when a lesson must
1130 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1131 *
1132 * Note that <st> shared stick-table is locked when calling this function.
1133 *
1134 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1135 * Returns -1 if there was not enough room left to send the message,
1136 * any other negative returned value must be considered as an error with an appcxt st0
1137 * returned value equal to PEER_SESS_ST_END.
1138 */
1139static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1140 struct shared_table *st)
1141{
1142 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1143}
1144
1145/*
1146 * Function to emit update messages for <st> stick-table when a lesson must
1147 * be taught to the peer <p> during teach state 1 step.
1148 *
1149 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1150 * Returns -1 if there was not enough room left to send the message,
1151 * any other negative returned value must be considered as an error with an appcxt st0
1152 * returned value equal to PEER_SESS_ST_END.
1153 */
1154static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1155 struct shared_table *st)
1156{
1157 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1158}
1159
1160/*
1161 * Function to emit update messages for <st> stick-table when a lesson must
1162 * be taught to the peer <p> during teach state 1 step.
1163 *
1164 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1165 * Returns -1 if there was not enough room left to send the message,
1166 * any other negative returned value must be considered as an error with an appcxt st0
1167 * returned value equal to PEER_SESS_ST_END.
1168 */
1169static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1170 struct shared_table *st)
1171{
1172 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1173}
1174
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001175
1176/*
1177 * Function used to parse a stick-table update message after it has been received
1178 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1179 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1180 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1181 * was encountered.
1182 * <exp> must be set if the stick-table entry expires.
1183 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1184 * messages, in this case the stick-table udpate message is received with a stick-table
1185 * update ID.
1186 * <totl> is the length of the stick-table update message computed upon receipt.
1187 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001188static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1189 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001190{
1191 struct stream_interface *si = appctx->owner;
1192 struct shared_table *st = p->remote_table;
1193 struct stksess *ts, *newts;
1194 uint32_t update;
1195 int expire;
1196 unsigned int data_type;
1197 void *data_ptr;
1198
1199 /* Here we have data message */
1200 if (!st)
1201 goto ignore_msg;
1202
1203 expire = MS_TO_TICKS(st->table->expire);
1204
1205 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001206 if (msg_len < sizeof(update))
1207 goto malformed_exit;
1208
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001209 memcpy(&update, *msg_cur, sizeof(update));
1210 *msg_cur += sizeof(update);
1211 st->last_get = htonl(update);
1212 }
1213 else {
1214 st->last_get++;
1215 }
1216
1217 if (exp) {
1218 size_t expire_sz = sizeof expire;
1219
Willy Tarreau1e82a142019-01-29 11:08:06 +01001220 if (*msg_cur + expire_sz > msg_end)
1221 goto malformed_exit;
1222
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001223 memcpy(&expire, *msg_cur, expire_sz);
1224 *msg_cur += expire_sz;
1225 expire = ntohl(expire);
1226 }
1227
1228 newts = stksess_new(st->table, NULL);
1229 if (!newts)
1230 goto ignore_msg;
1231
1232 if (st->table->type == SMP_T_STR) {
1233 unsigned int to_read, to_store;
1234
1235 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001236 if (!*msg_cur)
1237 goto malformed_free_newts;
1238
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001239 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001240 if (*msg_cur + to_store > msg_end)
1241 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001242
1243 memcpy(newts->key.key, *msg_cur, to_store);
1244 newts->key.key[to_store] = 0;
1245 *msg_cur += to_read;
1246 }
1247 else if (st->table->type == SMP_T_SINT) {
1248 unsigned int netinteger;
1249
Willy Tarreau1e82a142019-01-29 11:08:06 +01001250 if (*msg_cur + sizeof(netinteger) > msg_end)
1251 goto malformed_free_newts;
1252
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001253 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1254 netinteger = ntohl(netinteger);
1255 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1256 *msg_cur += sizeof(netinteger);
1257 }
1258 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001259 if (*msg_cur + st->table->key_size > msg_end)
1260 goto malformed_free_newts;
1261
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001262 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1263 *msg_cur += st->table->key_size;
1264 }
1265
1266 /* lookup for existing entry */
1267 ts = stktable_set_entry(st->table, newts);
1268 if (ts != newts) {
1269 stksess_free(st->table, newts);
1270 newts = NULL;
1271 }
1272
1273 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1274
1275 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001276 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001277
1278 if (!((1 << data_type) & st->remote_data))
1279 continue;
1280
Willy Tarreau1e82a142019-01-29 11:08:06 +01001281 decoded_int = intdecode(msg_cur, msg_end);
1282 if (!*msg_cur)
1283 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001284
Willy Tarreau1e82a142019-01-29 11:08:06 +01001285 switch (stktable_data_types[data_type].std_type) {
1286 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001287 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1288 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001289 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001290 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001291
Willy Tarreau1e82a142019-01-29 11:08:06 +01001292 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001293 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1294 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001295 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001296 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001297
Willy Tarreau1e82a142019-01-29 11:08:06 +01001298 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001299 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1300 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001301 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001302 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001303
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001304 case STD_T_FRQP: {
1305 struct freq_ctr_period data;
1306
1307 /* First bit is reserved for the freq_ctr_period lock
1308 Note: here we're still protected by the stksess lock
1309 so we don't need to update the update the freq_ctr_period
1310 using its internal lock */
1311
Willy Tarreau1e82a142019-01-29 11:08:06 +01001312 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001313 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001314 if (!*msg_cur)
1315 goto malformed_unlock;
1316
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001317 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001318 if (!*msg_cur)
1319 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001320
1321 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1322 if (data_ptr)
1323 stktable_data_cast(data_ptr, std_t_frqp) = data;
1324 break;
1325 }
1326 }
1327 }
1328 /* Force new expiration */
1329 ts->expire = tick_add(now_ms, expire);
1330
1331 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1332 stktable_touch_remote(st->table, ts, 1);
1333 return 1;
1334
1335 ignore_msg:
1336 /* skip consumed message */
1337 co_skip(si_oc(si), totl);
1338 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001339
1340 malformed_unlock:
1341 /* malformed message */
1342 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1343 stktable_touch_remote(st->table, ts, 1);
1344 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1345 return 0;
1346
1347 malformed_free_newts:
1348 /* malformed message */
1349 stksess_free(st->table, newts);
1350 malformed_exit:
1351 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1352 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001353}
1354
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001355/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001356 * Function used to parse a stick-table update acknowledgement message after it
1357 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1358 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1359 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1360 * was encountered.
1361 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1362 */
1363static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1364 char **msg_cur, char *msg_end)
1365{
1366 /* ack message */
1367 uint32_t table_id ;
1368 uint32_t update;
1369 struct shared_table *st;
1370
1371 table_id = intdecode(msg_cur, msg_end);
1372 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1373 /* malformed message */
1374 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1375 return 0;
1376 }
1377
1378 memcpy(&update, *msg_cur, sizeof(update));
1379 update = ntohl(update);
1380
1381 for (st = p->tables; st; st = st->next) {
1382 if (st->local_id == table_id) {
1383 st->update = update;
1384 break;
1385 }
1386 }
1387
1388 return 1;
1389}
1390
1391/*
1392 * Function used to parse a stick-table switch message after it has been received
1393 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1394 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1395 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1396 * was encountered.
1397 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1398 */
1399static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1400 char **msg_cur, char *msg_end)
1401{
1402 struct shared_table *st;
1403 int table_id;
1404
1405 table_id = intdecode(msg_cur, msg_end);
1406 if (!*msg_cur) {
1407 /* malformed message */
1408 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1409 return 0;
1410 }
1411
1412 p->remote_table = NULL;
1413 for (st = p->tables; st; st = st->next) {
1414 if (st->remote_id == table_id) {
1415 p->remote_table = st;
1416 break;
1417 }
1418 }
1419
1420 return 1;
1421}
1422
1423/*
1424 * Function used to parse a stick-table definition message after it has been received
1425 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1426 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1427 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1428 * was encountered.
1429 * <totl> is the length of the stick-table update message computed upon receipt.
1430 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1431 */
1432static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1433 char **msg_cur, char *msg_end, int totl)
1434{
1435 struct stream_interface *si = appctx->owner;
1436 int table_id_len;
1437 struct shared_table *st;
1438 int table_type;
1439 int table_keylen;
1440 int table_id;
1441 uint64_t table_data;
1442
1443 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001444 if (!*msg_cur)
1445 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001446
1447 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001448 if (!*msg_cur)
1449 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001450
1451 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001452 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1453 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001454
1455 for (st = p->tables; st; st = st->next) {
1456 /* Reset IDs */
1457 if (st->remote_id == table_id)
1458 st->remote_id = 0;
1459
1460 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1461 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1462 p->remote_table = st;
1463 }
1464
1465 if (!p->remote_table)
1466 goto ignore_msg;
1467
1468 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001469 if (*msg_cur >= msg_end)
1470 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001471
1472 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001473 if (!*msg_cur)
1474 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001475
1476 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001477 if (!*msg_cur)
1478 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001479
1480 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001481 if (!*msg_cur)
1482 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001483
1484 if (p->remote_table->table->type != table_type
1485 || p->remote_table->table->key_size != table_keylen) {
1486 p->remote_table = NULL;
1487 goto ignore_msg;
1488 }
1489
1490 p->remote_table->remote_data = table_data;
1491 p->remote_table->remote_id = table_id;
1492 return 1;
1493
1494 ignore_msg:
1495 co_skip(si_oc(si), totl);
1496 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001497
1498 malformed_exit:
1499 /* malformed message */
1500 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1501 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001502}
1503
1504/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001505 * Receive a stick-table message.
1506 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1507 * -1 if there was an error updating the appctx state st0 accordingly.
1508 */
1509static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1510 uint32_t *msg_len, int *totl)
1511{
1512 int reql;
1513 struct stream_interface *si = appctx->owner;
1514
1515 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1516 if (reql <= 0) /* closed or EOL not found */
1517 goto incomplete;
1518
1519 *totl += reql;
1520
1521 if ((unsigned int)msg_head[1] < 128)
1522 return 1;
1523
1524 /* Read and Decode message length */
1525 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1526 if (reql <= 0) /* closed */
1527 goto incomplete;
1528
1529 *totl += reql;
1530
1531 if ((unsigned int)msg_head[2] < 240) {
1532 *msg_len = msg_head[2];
1533 }
1534 else {
1535 int i;
1536 char *cur;
1537 char *end;
1538
1539 for (i = 3 ; i < msg_head_sz ; i++) {
1540 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1541 if (reql <= 0) /* closed */
1542 goto incomplete;
1543
1544 *totl += reql;
1545
1546 if (!(msg_head[i] & 0x80))
1547 break;
1548 }
1549
1550 if (i == msg_head_sz) {
1551 /* malformed message */
1552 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1553 return -1;
1554 }
1555 end = msg_head + msg_head_sz;
1556 cur = &msg_head[2];
1557 *msg_len = intdecode(&cur, end);
1558 if (!cur) {
1559 /* malformed message */
1560 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1561 return -1;
1562 }
1563 }
1564
1565 /* Read message content */
1566 if (*msg_len) {
1567 if (*msg_len > trash.size) {
1568 /* Status code is not success, abort */
1569 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1570 return -1;
1571 }
1572
1573 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1574 if (reql <= 0) /* closed */
1575 goto incomplete;
1576 *totl += reql;
1577 }
1578
1579 return 1;
1580
1581 incomplete:
1582 if (reql < 0) {
1583 /* there was an error */
1584 appctx->st0 = PEER_SESS_ST_END;
1585 return -1;
1586 }
1587
1588 return 0;
1589}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001590
1591/*
1592 * Treat the awaited message with <msg_head> as header.*
1593 * Return 1 if succeeded, 0 if not.
1594 */
1595static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1596 char **msg_cur, char *msg_end, int msg_len, int totl)
1597{
1598 struct stream_interface *si = appctx->owner;
1599 struct stream *s = si_strm(si);
1600 struct peers *peers = strm_fe(s)->parent;
1601
1602 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1603 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1604 struct shared_table *st;
1605 /* Reset message: remote need resync */
1606
1607 /* prepare tables fot a global push */
1608 for (st = peer->tables; st; st = st->next) {
1609 st->teaching_origin = st->last_pushed = st->table->update;
1610 st->flags = 0;
1611 }
1612
1613 /* reset teaching flags to 0 */
1614 peer->flags &= PEER_TEACH_RESET;
1615
1616 /* flag to start to teach lesson */
1617 peer->flags |= PEER_F_TEACH_PROCESS;
1618 }
1619 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1620 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1621 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1622 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1623 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1624 }
1625 peer->confirm++;
1626 }
1627 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1628 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1629 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1630 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1631
1632 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001633 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001634 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1635 }
1636 peer->confirm++;
1637 }
1638 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1639 struct shared_table *st;
1640
1641 /* If stopping state */
1642 if (stopping) {
1643 /* Close session, push resync no more needed */
1644 peer->flags |= PEER_F_TEACH_COMPLETE;
1645 appctx->st0 = PEER_SESS_ST_END;
1646 return 0;
1647 }
1648 for (st = peer->tables; st; st = st->next) {
1649 st->update = st->last_pushed = st->teaching_origin;
1650 st->flags = 0;
1651 }
1652
1653 /* reset teaching flags to 0 */
1654 peer->flags &= PEER_TEACH_RESET;
1655 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001656 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
Frédéric Lécaille54bff832019-03-26 10:25:20 +01001657 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001658 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001659 }
1660 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1661 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1662 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1663 return 0;
1664 }
1665 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1666 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1667 return 0;
1668 }
1669 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1670 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1671 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1672 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1673 int update, expire;
1674
1675 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1676 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1677 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1678 msg_cur, msg_end, msg_len, totl))
1679 return 0;
1680
1681 }
1682 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1683 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1684 return 0;
1685 }
1686 }
1687 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1688 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1689 return 0;
1690 }
1691
1692 return 1;
1693}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001694
1695
1696/*
1697 * Send any message to <peer> peer.
1698 * Returns 1 if succeeded, or -1 or 0 if failed.
1699 * -1 means an internal error occured, 0 is for a peer protocol error leading
1700 * to a peer state change (from the peer I/O handler point of view).
1701 */
1702static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1703{
1704 int repl;
1705 struct stream_interface *si = appctx->owner;
1706 struct stream *s = si_strm(si);
1707 struct peers *peers = strm_fe(s)->parent;
1708
1709 /* Need to request a resync */
1710 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1711 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1712 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1713
1714 repl = peer_send_resync_reqmsg(appctx);
1715 if (repl <= 0)
1716 return repl;
1717
1718 peers->flags |= PEERS_F_RESYNC_PROCESS;
1719 }
1720
1721 /* Nothing to read, now we start to write */
1722 if (peer->tables) {
1723 struct shared_table *st;
1724 struct shared_table *last_local_table;
1725
1726 last_local_table = peer->last_local_table;
1727 if (!last_local_table)
1728 last_local_table = peer->tables;
1729 st = last_local_table->next;
1730
1731 while (1) {
1732 if (!st)
1733 st = peer->tables;
1734
1735 /* It remains some updates to ack */
1736 if (st->last_get != st->last_acked) {
1737 repl = peer_send_ackmsg(st, appctx);
1738 if (repl <= 0)
1739 return repl;
1740
1741 st->last_acked = st->last_get;
1742 }
1743
1744 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1745 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1746 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1747 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1748
1749 repl = peer_send_teach_process_msgs(appctx, peer, st);
1750 if (repl <= 0) {
1751 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1752 return repl;
1753 }
1754 }
1755 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1756 }
1757 else {
1758 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1759 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1760 if (repl <= 0)
1761 return repl;
1762 }
1763
1764 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1765 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1766 if (repl <= 0)
1767 return repl;
1768 }
1769 }
1770
1771 if (st == last_local_table)
1772 break;
1773 st = st->next;
1774 }
1775 }
1776
1777 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1778 repl = peer_send_resync_finishedmsg(appctx, peer);
1779 if (repl <= 0)
1780 return repl;
1781
1782 /* flag finished message sent */
1783 peer->flags |= PEER_F_TEACH_FINISHED;
1784 }
1785
1786 /* Confirm finished or partial messages */
1787 while (peer->confirm) {
1788 repl = peer_send_resync_confirmsg(appctx);
1789 if (repl <= 0)
1790 return repl;
1791
1792 peer->confirm--;
1793 }
1794
1795 return 1;
1796}
1797
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001798/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001799 * Read and parse a first line of a "hello" peer protocol message.
1800 * Returns 0 if could not read a line, -1 if there was a read error or
1801 * the line is malformed, 1 if succeeded.
1802 */
1803static inline int peer_getline_version(struct appctx *appctx,
1804 unsigned int *maj_ver, unsigned int *min_ver)
1805{
1806 int reql;
1807
1808 reql = peer_getline(appctx);
1809 if (!reql)
1810 return 0;
1811
1812 if (reql < 0)
1813 return -1;
1814
1815 /* test protocol */
1816 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1817 appctx->st0 = PEER_SESS_ST_EXIT;
1818 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1819 return -1;
1820 }
1821 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1822 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1823 appctx->st0 = PEER_SESS_ST_EXIT;
1824 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1825 return -1;
1826 }
1827
1828 return 1;
1829}
1830
1831/*
1832 * Read and parse a second line of a "hello" peer protocol message.
1833 * Returns 0 if could not read a line, -1 if there was a read error or
1834 * the line is malformed, 1 if succeeded.
1835 */
1836static inline int peer_getline_host(struct appctx *appctx)
1837{
1838 int reql;
1839
1840 reql = peer_getline(appctx);
1841 if (!reql)
1842 return 0;
1843
1844 if (reql < 0)
1845 return -1;
1846
1847 /* test hostname match */
1848 if (strcmp(localpeer, trash.area) != 0) {
1849 appctx->st0 = PEER_SESS_ST_EXIT;
1850 appctx->st1 = PEER_SESS_SC_ERRHOST;
1851 return -1;
1852 }
1853
1854 return 1;
1855}
1856
1857/*
1858 * Read and parse a last line of a "hello" peer protocol message.
1859 * Returns 0 if could not read a character, -1 if there was a read error or
1860 * the line is malformed, 1 if succeeded.
1861 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1862 */
1863static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1864{
1865 char *p;
1866 int reql;
1867 struct peer *peer;
1868 struct stream_interface *si = appctx->owner;
1869 struct stream *s = si_strm(si);
1870 struct peers *peers = strm_fe(s)->parent;
1871
1872 reql = peer_getline(appctx);
1873 if (!reql)
1874 return 0;
1875
1876 if (reql < 0)
1877 return -1;
1878
1879 /* parse line "<peer name> <pid> <relative_pid>" */
1880 p = strchr(trash.area, ' ');
1881 if (!p) {
1882 appctx->st0 = PEER_SESS_ST_EXIT;
1883 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1884 return -1;
1885 }
1886 *p = 0;
1887
1888 /* lookup known peer */
1889 for (peer = peers->remote; peer; peer = peer->next) {
1890 if (strcmp(peer->id, trash.area) == 0)
1891 break;
1892 }
1893
1894 /* if unknown peer */
1895 if (!peer) {
1896 appctx->st0 = PEER_SESS_ST_EXIT;
1897 appctx->st1 = PEER_SESS_SC_ERRPEER;
1898 return -1;
1899 }
1900 *curpeer = peer;
1901
1902 return 1;
1903}
1904
1905/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01001906 * Init <peer> peer after having accepted it at peer protocol level.
1907 */
1908static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
1909{
1910 struct shared_table *st;
1911
1912 /* Register status code */
1913 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
1914
1915 /* Awake main task */
1916 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1917
1918 /* Init confirm counter */
1919 peer->confirm = 0;
1920
1921 /* Init cursors */
1922 for (st = peer->tables; st ; st = st->next) {
1923 st->last_get = st->last_acked = 0;
1924 st->teaching_origin = st->last_pushed = st->update;
1925 }
1926
1927 /* reset teaching and learning flags to 0 */
1928 peer->flags &= PEER_TEACH_RESET;
1929 peer->flags &= PEER_LEARN_RESET;
1930
1931 /* if current peer is local */
1932 if (peer->local) {
1933 /* if current host need resyncfrom local and no process assined */
1934 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1935 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1936 /* assign local peer for a lesson, consider lesson already requested */
1937 peer->flags |= PEER_F_LEARN_ASSIGN;
1938 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1939 }
1940
1941 }
1942 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1943 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1944 /* assign peer for a lesson */
1945 peer->flags |= PEER_F_LEARN_ASSIGN;
1946 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1947 }
1948}
1949
1950/*
1951 * Init <peer> peer after having connected it at peer protocol level.
1952 */
1953static inline void init_connected_peer(struct peer *peer, struct peers *peers)
1954{
1955 struct shared_table *st;
1956
1957 /* Init cursors */
1958 for (st = peer->tables; st ; st = st->next) {
1959 st->last_get = st->last_acked = 0;
1960 st->teaching_origin = st->last_pushed = st->update;
1961 }
1962
1963 /* Init confirm counter */
1964 peer->confirm = 0;
1965
1966 /* reset teaching and learning flags to 0 */
1967 peer->flags &= PEER_TEACH_RESET;
1968 peer->flags &= PEER_LEARN_RESET;
1969
1970 /* If current peer is local */
1971 if (peer->local) {
1972 /* flag to start to teach lesson */
1973 peer->flags |= PEER_F_TEACH_PROCESS;
1974 }
1975 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1976 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1977 /* If peer is remote and resync from remote is needed,
1978 and no peer currently assigned */
1979
1980 /* assign peer for a lesson */
1981 peer->flags |= PEER_F_LEARN_ASSIGN;
1982 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1983 }
1984}
1985
1986/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001987 * IO Handler to handle message exchance with a peer
1988 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001989static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001990{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001991 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02001992 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001993 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02001994 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001995 int reql = 0;
1996 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001997 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001998 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02001999
Joseph Herlant82b2f542018-11-15 12:19:14 -08002000 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002001 if (si_ic(si)->buf.size == 0) {
2002 si_rx_room_blk(si);
2003 goto out;
2004 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002005
Emeric Brun2b920a12010-09-23 18:30:22 +02002006 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002007 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002008switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002009 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002010 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002011 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002012 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002013 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002014 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002015 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002016 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002017 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002018 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2019 if (reql <= 0) {
2020 if (!reql)
2021 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002022 goto switchstate;
2023 }
2024
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002025 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002026 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002027 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002028 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002029 reql = peer_getline_host(appctx);
2030 if (reql <= 0) {
2031 if (!reql)
2032 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002033 goto switchstate;
2034 }
2035
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002036 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002037 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002038 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002039 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002040 reql = peer_getline_last(appctx, &curpeer);
2041 if (reql <= 0) {
2042 if (!reql)
2043 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002044 goto switchstate;
2045 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002046
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002047 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002048 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002049 if (curpeer->local) {
2050 /* Local connection, reply a retry */
2051 appctx->st0 = PEER_SESS_ST_EXIT;
2052 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2053 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002054 }
Emeric Brun80527f52017-06-19 17:46:37 +02002055
2056 /* we're killing a connection, we must apply a random delay before
2057 * retrying otherwise the other end will do the same and we can loop
2058 * for a while.
2059 */
2060 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002061 peer_session_forceshutdown(curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002062 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002063 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2064 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2065 curpeer->flags |= PEER_F_DWNGRD;
2066 }
2067 else {
2068 curpeer->flags &= ~PEER_F_DWNGRD;
2069 }
2070 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002071 curpeer->appctx = appctx;
2072 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002073 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002074 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002075 /* fall through */
2076 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002077 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002078 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002079 if (!curpeer) {
2080 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002081 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002082 if (curpeer->appctx != appctx) {
2083 appctx->st0 = PEER_SESS_ST_END;
2084 goto switchstate;
2085 }
2086 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002087
2088 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 if (repl <= 0) {
2090 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002091 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002092 goto switchstate;
2093 }
2094
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002095 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002096
Emeric Brun2b920a12010-09-23 18:30:22 +02002097 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002098 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002099 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002100 goto switchstate;
2101 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002102 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002103 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002104 if (!curpeer) {
2105 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002106 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002107 if (curpeer->appctx != appctx) {
2108 appctx->st0 = PEER_SESS_ST_END;
2109 goto switchstate;
2110 }
2111 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002112
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002113 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002114 if (repl <= 0) {
2115 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002116 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002117 goto switchstate;
2118 }
2119
2120 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002121 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002122 /* fall through */
2123 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002124 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002125 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002126 if (!curpeer) {
2127 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002128 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002129 if (curpeer->appctx != appctx) {
2130 appctx->st0 = PEER_SESS_ST_END;
2131 goto switchstate;
2132 }
2133 }
2134
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002135 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002136 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002137
Frédéric Lécaillece025572019-01-21 13:38:06 +01002138 reql = peer_getline(appctx);
2139 if (!reql)
2140 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002141
Frédéric Lécaillece025572019-01-21 13:38:06 +01002142 if (reql < 0)
2143 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002144
2145 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002146 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002147
2148 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002149 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002150
2151 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002152 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002153 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002154 }
2155 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002156 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2157 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002158 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002159 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002160 goto switchstate;
2161 }
Olivier Houcharded879892019-03-08 18:53:43 +01002162 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002163 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002164 /* fall through */
2165 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002166 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002167 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002168 char *msg_cur = trash.area;
2169 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002170 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002171 int totl = 0;
2172
Willy Tarreau2d372c22018-11-05 17:12:27 +01002173 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002174 if (!curpeer) {
2175 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002176 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002177 if (curpeer->appctx != appctx) {
2178 appctx->st0 = PEER_SESS_ST_END;
2179 goto switchstate;
2180 }
2181 }
2182
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002183 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2184 if (reql <= 0) {
2185 if (reql == -1)
2186 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002187 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002188 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002189
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002190 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002191 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002192 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002193
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002194 curpeer->flags |= PEER_F_ALIVE;
2195
Emeric Brun2b920a12010-09-23 18:30:22 +02002196 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002197 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002198 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002199 goto switchstate;
2200
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002201send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002202 if (curpeer->flags & PEER_F_HEARTBEAT) {
2203 curpeer->flags &= ~PEER_F_HEARTBEAT;
2204 repl = peer_send_heartbeatmsg(appctx);
2205 if (repl <= 0) {
2206 if (repl == -1)
2207 goto out;
2208 goto switchstate;
2209 }
2210 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002211 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002212 repl = peer_send_msgs(appctx, curpeer);
2213 if (repl <= 0) {
2214 if (repl == -1)
2215 goto out;
2216 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002217 }
2218
Emeric Brun2b920a12010-09-23 18:30:22 +02002219 /* noting more to do */
2220 goto out;
2221 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002222 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002223 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002224 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002225 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002226 if (peer_send_status_errormsg(appctx) == -1)
2227 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002228 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002229 goto switchstate;
2230 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002231 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002232 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002233 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002234 if (peer_send_error_size_limitmsg(appctx) == -1)
2235 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002236 appctx->st0 = PEER_SESS_ST_END;
2237 goto switchstate;
2238 }
2239 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002240 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002241 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002242 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002243 if (peer_send_error_protomsg(appctx) == -1)
2244 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002245 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002246 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002247 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002248 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002249 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002250 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002251 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002252 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002253 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002254 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002255 curpeer = NULL;
2256 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002257 si_shutw(si);
2258 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002259 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002260 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002261 }
2262 }
2263 }
2264out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002265 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002266
2267 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002268 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002269 return;
2270}
2271
Willy Tarreau30576452015-04-13 13:50:30 +02002272static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002273 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002274 .name = "<PEER>", /* used for logging */
2275 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002276 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002277};
Emeric Brun2b920a12010-09-23 18:30:22 +02002278
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002279
Emeric Brun2b920a12010-09-23 18:30:22 +02002280/*
2281 * Use this function to force a close of a peer session
2282 */
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002283static void peer_session_forceshutdown(struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002284{
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002285 struct appctx *appctx = peer->appctx;
2286
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002287 /* Note that the peer sessions which have just been created
2288 * (->st0 == PEER_SESS_ST_CONNECT) must not
2289 * be shutdown, if not, the TCP session will never be closed
2290 * and stay in CLOSE_WAIT state after having been closed by
2291 * the remote side.
2292 */
2293 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002294 return;
2295
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002296 if (appctx->applet != &peer_applet)
2297 return;
2298
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002299 __peer_session_deinit(peer);
2300
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002301 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002302 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002303}
2304
Willy Tarreau91d96282015-03-13 15:47:26 +01002305/* Pre-configures a peers frontend to accept incoming connections */
2306void peers_setup_frontend(struct proxy *fe)
2307{
2308 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002309 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002310 fe->maxconn = 0;
2311 fe->conn_retries = CONN_RETRIES;
2312 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002313 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002314 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002315 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002316 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002317}
2318
Emeric Brun2b920a12010-09-23 18:30:22 +02002319/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002320 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002321 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002322static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002323{
Willy Tarreau04b92862017-09-15 11:01:04 +02002324 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002326 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002327 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002328 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002329 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002330
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002331 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002332 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002333 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002334 s = NULL;
2335
Emeric Brun1138fd02017-06-19 12:38:55 +02002336 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002337 if (!appctx)
2338 goto out_close;
2339
2340 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002341 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002342
Willy Tarreau04b92862017-09-15 11:01:04 +02002343 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002344 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002345 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002346 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002347 }
2348
Willy Tarreau87787ac2017-08-28 16:22:54 +02002349 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002350 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002351 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002352 }
2353
Willy Tarreau342bfb12015-04-05 01:35:34 +02002354 /* The tasks below are normally what is supposed to be done by
2355 * fe->accept().
2356 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002357 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002358
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002359 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002360 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002361 appctx_wakeup(appctx);
2362
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002363 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002364 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002365 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002366
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002367 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002368 * pre-initialized connection in si->conn.
2369 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002370 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002371 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002372
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002373 if (unlikely((cs = cs_new(conn)) == NULL))
2374 goto out_free_conn;
2375
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002376 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002377 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2378
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002379 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002380 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2381 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002382 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002383
Emeric Brun2b920a12010-09-23 18:30:22 +02002384 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002385 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002386
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002387 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002388
Emeric Brunb3971ab2015-05-12 18:49:09 +02002389 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002390 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002391 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002392 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002393
2394 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002395out_free_cs:
2396 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002397 out_free_conn:
2398 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002399 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002400 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002401 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002402 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002403 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002404 out_free_appctx:
2405 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002406 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002407 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002408}
2409
2410/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002411 * Task processing function to manage re-connect, peer session
2412 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002413 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002414static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002415{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002416 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002417 struct peer *ps;
2418 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002419
2420 task->expire = TICK_ETERNITY;
2421
Emeric Brunb3971ab2015-05-12 18:49:09 +02002422 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002423 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002424 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002425 task_delete(peers->sync_task);
2426 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002427 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002428 return NULL;
2429 }
2430
Emeric Brun80527f52017-06-19 17:46:37 +02002431 /* Acquire lock for all peers of the section */
2432 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002433 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002434
Emeric Brun2b920a12010-09-23 18:30:22 +02002435 if (!stopping) {
2436 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002437
2438 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2439 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2440 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002441 /* Resync from local peer needed
2442 no peer was assigned for the lesson
2443 and no old local peer found
2444 or resync timeout expire */
2445
2446 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002447 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002448
2449 /* reschedule a resync */
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002450 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(PEER_RESYNC_TIMEOUT));
Emeric Brun2b920a12010-09-23 18:30:22 +02002451 }
2452
2453 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002454 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002455 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002456 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002457 if (!ps->appctx) {
2458 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002459 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002460 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002461 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002462 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002463 tick_is_expired(ps->reconnect, now_ms))) {
2464 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002465 * or previous peer connection established with success
2466 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002467 * and reconnection timer is expired */
2468
2469 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002470 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002471 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002472 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002473 /* If previous session failed during connection
2474 * but reconnection timer is not expired */
2475
2476 /* reschedule task for reconnect */
2477 task->expire = tick_first(task->expire, ps->reconnect);
2478 }
2479 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002480 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002481 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002482 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002483 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2484 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002485 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2486 /* Resync from a remote is needed
2487 * and no peer was assigned for lesson
2488 * and current peer may be up2date */
2489
2490 /* assign peer for the lesson */
2491 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002492 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002493
Willy Tarreau9df94c22016-10-31 18:42:52 +01002494 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002495 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002496 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002497 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002498 int update_to_push = 0;
2499
Emeric Brunb3971ab2015-05-12 18:49:09 +02002500 /* Awake session if there is data to push */
2501 for (st = ps->tables; st ; st = st->next) {
2502 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002503 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002504 update_to_push = 1;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002505 /* There is no need to send a heartbeat message
2506 * when some updates must be pushed. The remote
2507 * peer will consider <ps> peer as alive when it will
2508 * receive these updates.
2509 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002510 ps->flags &= ~PEER_F_HEARTBEAT;
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002511 /* Re-schedule another one later. */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002512 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002513 /* We are going to send updates, let's ensure we will
2514 * come back to send heartbeat messages or to reconnect.
2515 */
2516 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002517 appctx_wakeup(ps->appctx);
2518 break;
2519 }
2520 }
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002521 /* When there are updates to send we do not reconnect
2522 * and do not send heartbeat message either.
2523 */
2524 if (!update_to_push) {
2525 if (tick_is_expired(ps->reconnect, now_ms)) {
2526 if (ps->flags & PEER_F_ALIVE) {
2527 /* This peer was alive during a 'reconnect' period.
2528 * Flag it as not alive again for the next period.
2529 */
2530 ps->flags &= ~PEER_F_ALIVE;
Frédéric Lécaille54bff832019-03-26 10:25:20 +01002531 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002532 }
2533 else {
2534 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002535 peer_session_forceshutdown(ps);
Frédéric Lécaille045e0d42019-03-21 11:12:32 +01002536 }
2537 }
2538 else if (tick_is_expired(ps->heartbeat, now_ms)) {
2539 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2540 ps->flags |= PEER_F_HEARTBEAT;
2541 appctx_wakeup(ps->appctx);
2542 }
Frédéric Lécailleb7405c12019-03-27 14:32:39 +01002543 task->expire = tick_first(ps->reconnect, ps->heartbeat);
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002544 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002545 }
2546 /* else do nothing */
2547 } /* SUCCESSCODE */
2548 } /* !ps->peer->local */
2549 } /* for */
2550
2551 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002552 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2553 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2554 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002555 /* Resync from remote peer needed
2556 * no peer was assigned for the lesson
2557 * and resync timeout expire */
2558
2559 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002560 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002561 }
2562
Emeric Brunb3971ab2015-05-12 18:49:09 +02002563 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002564 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002565 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2566 if (!tick_is_expired(peers->resync_timeout, now_ms))
2567 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002568 }
2569 } /* !stopping */
2570 else {
2571 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002572 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002573 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002574 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002575 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002576 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002577 peers->flags |= PEERS_F_DONOTSTOP;
2578 ps = peers->local;
2579 for (st = ps->tables; st ; st = st->next)
2580 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002581 }
2582
2583 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002584 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002585 /* we're killing a connection, we must apply a random delay before
2586 * retrying otherwise the other end will do the same and we can loop
2587 * for a while.
2588 */
2589 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002590 if (ps->appctx) {
Emeric Brun9ef2ad72019-04-02 17:22:01 +02002591 peer_session_forceshutdown(ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002592 }
2593 }
2594 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002595
Emeric Brunb3971ab2015-05-12 18:49:09 +02002596 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002597 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002598 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002599 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002600 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002601 peers->flags &= ~PEERS_F_DONOTSTOP;
2602 for (st = ps->tables; st ; st = st->next)
2603 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002604 }
2605 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002606 else if (!ps->appctx) {
2607 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002608 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002609 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2610 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2611 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002612 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002613 * or previous peer connection was successfully established
2614 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002615 * or during previous connect, peer replies a try again statuscode */
2616
2617 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002618 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002619 }
2620 else {
2621 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002622 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002623 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002624 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002625 peers->flags &= ~PEERS_F_DONOTSTOP;
2626 for (st = ps->tables; st ; st = st->next)
2627 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002628 }
2629 }
2630 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002631 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002632 /* current peer connection is active and established
2633 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002634 for (st = ps->tables; st ; st = st->next) {
2635 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002636 appctx_wakeup(ps->appctx);
2637 break;
2638 }
2639 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002640 }
2641 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002642
2643 /* Release lock for all peers of the section */
2644 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002645 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002646
Emeric Brun2b920a12010-09-23 18:30:22 +02002647 /* Wakeup for re-connect */
2648 return task;
2649}
2650
Emeric Brunb3971ab2015-05-12 18:49:09 +02002651
Emeric Brun2b920a12010-09-23 18:30:22 +02002652/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002653 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002654 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002655int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002656{
Emeric Brun2b920a12010-09-23 18:30:22 +02002657 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002658
Emeric Brun2b920a12010-09-23 18:30:22 +02002659 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002660 peers->peers_fe->maxconn += 3;
2661 }
2662
Emeric Brunc60def82017-09-27 14:59:38 +02002663 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002664 if (!peers->sync_task)
2665 return 0;
2666
Emeric Brunb3971ab2015-05-12 18:49:09 +02002667 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002668 peers->sync_task->context = (void *)peers;
2669 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2670 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002671 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002672}
2673
2674
2675
2676/*
2677 * Function used to register a table for sync on a group of peers
2678 *
2679 */
2680void peers_register_table(struct peers *peers, struct stktable *table)
2681{
2682 struct shared_table *st;
2683 struct peer * curpeer;
2684 int id = 0;
2685
2686 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002687 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002688 st->table = table;
2689 st->next = curpeer->tables;
2690 if (curpeer->tables)
2691 id = curpeer->tables->local_id;
2692 st->local_id = id + 1;
2693
2694 curpeer->tables = st;
2695 }
2696
2697 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002698}
2699