blob: 68116646f7b9f18c69d43c672c16299b3916090b [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/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020059#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
Emeric Brun2b920a12010-09-23 18:30:22 +020064 to push data to new process */
65
Emeric Brunb3971ab2015-05-12 18:49:09 +020066#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)
70
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
74#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/******************************/
80#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020081#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 */
Frédéric Lécaille645635d2019-02-11 17:49:39 +010085#define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020086#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020087
Emeric Brunb3971ab2015-05-12 18:49:09 +020088#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
Emeric Brun2b920a12010-09-23 18:30:22 +020089#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
90
Frédéric Lécaille645635d2019-02-11 17:49:39 +010091#define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */
92
Emeric Brunb3971ab2015-05-12 18:49:09 +020093/*****************************/
94/* Sync message class */
95/*****************************/
96enum {
97 PEER_MSG_CLASS_CONTROL = 0,
98 PEER_MSG_CLASS_ERROR,
99 PEER_MSG_CLASS_STICKTABLE = 10,
100 PEER_MSG_CLASS_RESERVED = 255,
101};
102
103/*****************************/
104/* control message types */
105/*****************************/
106enum {
107 PEER_MSG_CTRL_RESYNCREQ = 0,
108 PEER_MSG_CTRL_RESYNCFINISHED,
109 PEER_MSG_CTRL_RESYNCPARTIAL,
110 PEER_MSG_CTRL_RESYNCCONFIRM,
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100111 PEER_MSG_CTRL_HEARTBEAT,
Emeric Brunb3971ab2015-05-12 18:49:09 +0200112};
113
114/*****************************/
115/* error message types */
116/*****************************/
117enum {
118 PEER_MSG_ERR_PROTOCOL = 0,
119 PEER_MSG_ERR_SIZELIMIT,
120};
121
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100122/*
123 * Parameters used by functions to build peer protocol messages. */
124struct peer_prep_params {
125 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100126 struct peer *peer;
127 } hello;
128 struct {
129 unsigned int st1;
130 } error_status;
131 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100132 struct stksess *stksess;
133 struct shared_table *shared_table;
134 unsigned int updateid;
135 int use_identifier;
136 int use_timed;
137 } updt;
138 struct {
139 struct shared_table *shared_table;
140 } swtch;
141 struct {
142 struct shared_table *shared_table;
143 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100144 struct {
145 unsigned char head[2];
146 } control;
147 struct {
148 unsigned char head[2];
149 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100150};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200151
152/*******************************/
153/* stick table sync mesg types */
154/* Note: ids >= 128 contains */
155/* id message cotains data */
156/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200157#define PEER_MSG_STKT_UPDATE 0x80
158#define PEER_MSG_STKT_INCUPDATE 0x81
159#define PEER_MSG_STKT_DEFINE 0x82
160#define PEER_MSG_STKT_SWITCH 0x83
161#define PEER_MSG_STKT_ACK 0x84
162#define PEER_MSG_STKT_UPDATE_TIMED 0x85
163#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200164
165/**********************************/
166/* Peer Session IO handler states */
167/**********************************/
168
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100169enum {
170 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
171 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
172 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
173 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100174 /* after this point, data were possibly exchanged */
175 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
176 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
177 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
178 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
179 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200180 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
181 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100182 PEER_SESS_ST_END, /* Killed session */
183};
Emeric Brun2b920a12010-09-23 18:30:22 +0200184
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100185/***************************************************/
186/* Peer Session status code - part of the protocol */
187/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200188
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100189#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
190#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200191
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100192#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200193
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100194#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200195
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100196#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
197#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
198#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
199#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200200
201#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200202#define PEER_MAJOR_VER 2
203#define PEER_MINOR_VER 1
204#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200205
Willy Tarreau6254a922019-01-29 17:45:23 +0100206static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200207struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100208static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200209
Emeric Brun18928af2017-03-29 16:32:53 +0200210/* This function encode an uint64 to 'dynamic' length format.
211 The encoded value is written at address *str, and the
212 caller must assure that size after *str is large enought.
213 At return, the *str is set at the next Byte after then
214 encoded integer. The function returns then length of the
215 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200216int intencode(uint64_t i, char **str) {
217 int idx = 0;
218 unsigned char *msg;
219
Emeric Brunb3971ab2015-05-12 18:49:09 +0200220 msg = (unsigned char *)*str;
221 if (i < 240) {
222 msg[0] = (unsigned char)i;
223 *str = (char *)&msg[idx+1];
224 return (idx+1);
225 }
226
227 msg[idx] =(unsigned char)i | 240;
228 i = (i - 240) >> 4;
229 while (i >= 128) {
230 msg[++idx] = (unsigned char)i | 128;
231 i = (i - 128) >> 7;
232 }
233 msg[++idx] = (unsigned char)i;
234 *str = (char *)&msg[idx+1];
235 return (idx+1);
236}
237
238
239/* This function returns the decoded integer or 0
240 if decode failed
241 *str point on the beginning of the integer to decode
242 at the end of decoding *str point on the end of the
243 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200244uint64_t intdecode(char **str, char *end)
245{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200246 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200247 uint64_t i;
248 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200249
250 if (!*str)
251 return 0;
252
253 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200254 if (msg >= (unsigned char *)end)
255 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200256
Emeric Brun18928af2017-03-29 16:32:53 +0200257 i = *(msg++);
258 if (i >= 240) {
259 shift = 4;
260 do {
261 if (msg >= (unsigned char *)end)
262 goto fail;
263 i += (uint64_t)*msg << shift;
264 shift += 7;
265 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200266 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100267 *str = (char *)msg;
268 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200269
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100270 fail:
271 *str = NULL;
272 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200273}
Emeric Brun2b920a12010-09-23 18:30:22 +0200274
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100275/*
276 * Build a "hello" peer protocol message.
277 * Return the number of written bytes written to build this messages if succeeded,
278 * 0 if not.
279 */
280static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
281{
282 int min_ver, ret;
283 struct peer *peer;
284
285 peer = p->hello.peer;
286 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
287 /* Prepare headers */
288 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
289 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
290 if (ret >= size)
291 return 0;
292
293 return ret;
294}
295
296/*
297 * Build a "handshake succeeded" status message.
298 * Return the number of written bytes written to build this messages if succeeded,
299 * 0 if not.
300 */
301static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
302{
303 int ret;
304
305 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
306 if (ret >= size)
307 return 0;
308
309 return ret;
310}
311
312/*
313 * Build an error status message.
314 * Return the number of written bytes written to build this messages if succeeded,
315 * 0 if not.
316 */
317static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
318{
319 int ret;
320 unsigned int st1;
321
322 st1 = p->error_status.st1;
323 ret = snprintf(msg, size, "%d\n", st1);
324 if (ret >= size)
325 return 0;
326
327 return ret;
328}
329
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200330/* Set the stick-table UPDATE message type byte at <msg_type> address,
331 * depending on <use_identifier> and <use_timed> boolean parameters.
332 * Always successful.
333 */
334static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
335{
336 if (use_timed) {
337 if (use_identifier)
338 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
339 else
340 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
341 }
342 else {
343 if (use_identifier)
344 *msg_type = PEER_MSG_STKT_UPDATE;
345 else
346 *msg_type = PEER_MSG_STKT_INCUPDATE;
347 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200348}
Emeric Brun2b920a12010-09-23 18:30:22 +0200349/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200350 * This prepare the data update message on the stick session <ts>, <st> is the considered
351 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800352 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200353 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
354 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200355 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100356static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200357{
358 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200359 unsigned short datalen;
360 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200361 unsigned int data_type;
362 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100363 struct stksess *ts;
364 struct shared_table *st;
365 unsigned int updateid;
366 int use_identifier;
367 int use_timed;
368
369 ts = p->updt.stksess;
370 st = p->updt.shared_table;
371 updateid = p->updt.updateid;
372 use_identifier = p->updt.use_identifier;
373 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200374
375 cursor = datamsg = msg + 1 + 5;
376
Emeric Brun2b920a12010-09-23 18:30:22 +0200377 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200378
379 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200380 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200381 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200382 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200383
384 /* encode update identifier if needed */
385 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200386 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200387 memcpy(cursor, &netinteger, sizeof(netinteger));
388 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200389 }
390
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200391 if (use_timed) {
392 netinteger = htonl(tick_remain(now_ms, ts->expire));
393 memcpy(cursor, &netinteger, sizeof(netinteger));
394 cursor += sizeof(netinteger);
395 }
396
Emeric Brunb3971ab2015-05-12 18:49:09 +0200397 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200398 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200399 int stlen = strlen((char *)ts->key.key);
400
Emeric Brunb3971ab2015-05-12 18:49:09 +0200401 intencode(stlen, &cursor);
402 memcpy(cursor, ts->key.key, stlen);
403 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200404 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200405 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200406 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200407 memcpy(cursor, &netinteger, sizeof(netinteger));
408 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200409 }
410 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200411 memcpy(cursor, ts->key.key, st->table->key_size);
412 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200413 }
414
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100415 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200416 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200417 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200418
Emeric Brun94900952015-06-11 18:25:54 +0200419 data_ptr = stktable_data_ptr(st->table, ts, data_type);
420 if (data_ptr) {
421 switch (stktable_data_types[data_type].std_type) {
422 case STD_T_SINT: {
423 int data;
424
425 data = stktable_data_cast(data_ptr, std_t_sint);
426 intencode(data, &cursor);
427 break;
428 }
429 case STD_T_UINT: {
430 unsigned int data;
431
432 data = stktable_data_cast(data_ptr, std_t_uint);
433 intencode(data, &cursor);
434 break;
435 }
436 case STD_T_ULL: {
437 unsigned long long data;
438
439 data = stktable_data_cast(data_ptr, std_t_ull);
440 intencode(data, &cursor);
441 break;
442 }
443 case STD_T_FRQP: {
444 struct freq_ctr_period *frqp;
445
446 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
447 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
448 intencode(frqp->curr_ctr, &cursor);
449 intencode(frqp->prev_ctr, &cursor);
450 break;
451 }
452 }
453 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200454 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100455 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200456
457 /* Compute datalen */
458 datalen = (cursor - datamsg);
459
460 /* prepare message header */
461 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200462 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200463 cursor = &msg[2];
464 intencode(datalen, &cursor);
465
466 /* move data after header */
467 memmove(cursor, datamsg, datalen);
468
469 /* return header size + data_len */
470 return (cursor - msg) + datalen;
471}
472
473/*
474 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800475 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200476 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
477 * check size)
478 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100479static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200480{
481 int len;
482 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200483 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200484 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200485 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200486 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100487 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200488
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100489 st = params->swtch.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200490 cursor = datamsg = msg + 2 + 5;
491
492 /* Encode data */
493
494 /* encode local id */
495 intencode(st->local_id, &cursor);
496
497 /* encode table name */
498 len = strlen(st->table->id);
499 intencode(len, &cursor);
500 memcpy(cursor, st->table->id, len);
501 cursor += len;
502
503 /* encode table type */
504
505 intencode(st->table->type, &cursor);
506
507 /* encode table key size */
508 intencode(st->table->key_size, &cursor);
509
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200510 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200512 /* encode available known data types in table */
513 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
514 if (st->table->data_ofs[data_type]) {
515 switch (stktable_data_types[data_type].std_type) {
516 case STD_T_SINT:
517 case STD_T_UINT:
518 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200519 data |= 1 << data_type;
520 break;
Emeric Brun94900952015-06-11 18:25:54 +0200521 case STD_T_FRQP:
522 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200523 intencode(data_type, &chunkq);
524 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200525 break;
526 }
527 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200528 }
529 intencode(data, &cursor);
530
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200531 /* Encode stick-table entries duration. */
532 intencode(st->table->expire, &cursor);
533
534 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200535 chunk->data = chunkq - chunkp;
536 memcpy(cursor, chunk->area, chunk->data);
537 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200538 }
539
Emeric Brunb3971ab2015-05-12 18:49:09 +0200540 /* Compute datalen */
541 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200542
Emeric Brunb3971ab2015-05-12 18:49:09 +0200543 /* prepare message header */
544 msg[0] = PEER_MSG_CLASS_STICKTABLE;
545 msg[1] = PEER_MSG_STKT_DEFINE;
546 cursor = &msg[2];
547 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200548
Emeric Brunb3971ab2015-05-12 18:49:09 +0200549 /* move data after header */
550 memmove(cursor, datamsg, datalen);
551
552 /* return header size + data_len */
553 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200554}
555
Emeric Brunb3971ab2015-05-12 18:49:09 +0200556/*
557 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
558 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800559 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200560 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
561 * check size)
562 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100563static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200564{
565 unsigned short datalen;
566 char *cursor, *datamsg;
567 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100568 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200569
Emeric Brunb058f1c2015-09-22 15:50:18 +0200570 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200571
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100572 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200573 intencode(st->remote_id, &cursor);
574 netinteger = htonl(st->last_get);
575 memcpy(cursor, &netinteger, sizeof(netinteger));
576 cursor += sizeof(netinteger);
577
578 /* Compute datalen */
579 datalen = (cursor - datamsg);
580
581 /* prepare message header */
582 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200583 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200584 cursor = &msg[2];
585 intencode(datalen, &cursor);
586
587 /* move data after header */
588 memmove(cursor, datamsg, datalen);
589
590 /* return header size + data_len */
591 return (cursor - msg) + datalen;
592}
Emeric Brun2b920a12010-09-23 18:30:22 +0200593
594/*
595 * Callback to release a session with a peer
596 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200597static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200598{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200599 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200600 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200601 struct peer *peer = appctx->ctx.peers.ptr;
602 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200603
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100604 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100605 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200606 return;
607
608 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200609 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100610 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +0100611 _HA_ATOMIC_SUB(&connected_peers, 1);
612 _HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100613 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100614 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200615 /* Re-init current table pointers to force announcement on re-connect */
616 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200617 peer->appctx = NULL;
618 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200619 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200620 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
621 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200622
623 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200624 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200625 }
626 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200627 peer->flags &= PEER_TEACH_RESET;
628 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200629 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100630 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200631 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200632 }
633}
634
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200635/* Retrieve the major and minor versions of peers protocol
636 * announced by a remote peer. <str> is a null-terminated
637 * string with the following format: "<maj_ver>.<min_ver>".
638 */
639static int peer_get_version(const char *str,
640 unsigned int *maj_ver, unsigned int *min_ver)
641{
642 unsigned int majv, minv;
643 const char *pos, *saved;
644 const char *end;
645
646 saved = pos = str;
647 end = str + strlen(str);
648
649 majv = read_uint(&pos, end);
650 if (saved == pos || *pos++ != '.')
651 return -1;
652
653 saved = pos;
654 minv = read_uint(&pos, end);
655 if (saved == pos || pos != end)
656 return -1;
657
658 *maj_ver = majv;
659 *min_ver = minv;
660
661 return 0;
662}
Emeric Brun2b920a12010-09-23 18:30:22 +0200663
664/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100665 * Parse a line terminated by an optional '\r' character, followed by a mandatory
666 * '\n' character.
667 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
668 * a line could not be read because the communication channel is closed.
669 */
670static inline int peer_getline(struct appctx *appctx)
671{
672 int n;
673 struct stream_interface *si = appctx->owner;
674
675 n = co_getline(si_oc(si), trash.area, trash.size);
676 if (!n)
677 return 0;
678
679 if (n < 0 || trash.area[n - 1] != '\n') {
680 appctx->st0 = PEER_SESS_ST_END;
681 return -1;
682 }
683
684 if (n > 1 && (trash.area[n - 2] == '\r'))
685 trash.area[n - 2] = 0;
686 else
687 trash.area[n - 1] = 0;
688
689 co_skip(si_oc(si), n);
690
691 return n;
692}
693
694/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100695 * Send a message after having called <peer_prepare_msg> to build it.
696 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
697 * Returns -1 if there was not enough room left to send the message,
698 * any other negative returned value must be considered as an error with an appcxt st0
699 * returned value equal to PEER_SESS_ST_END.
700 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100701static inline int peer_send_msg(struct appctx *appctx,
702 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
703 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100704{
705 int ret, msglen;
706 struct stream_interface *si = appctx->owner;
707
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100708 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100709 if (!msglen) {
710 /* internal error: message does not fit in trash */
711 appctx->st0 = PEER_SESS_ST_END;
712 return 0;
713 }
714
715 /* message to buffer */
716 ret = ci_putblk(si_ic(si), trash.area, msglen);
717 if (ret <= 0) {
718 if (ret == -1) {
719 /* No more write possible */
720 si_rx_room_blk(si);
721 return -1;
722 }
723 appctx->st0 = PEER_SESS_ST_END;
724 }
725
726 return ret;
727}
728
729/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100730 * Send a hello message.
731 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
732 * Returns -1 if there was not enough room left to send the message,
733 * any other negative returned value must be considered as an error with an appcxt st0
734 * returned value equal to PEER_SESS_ST_END.
735 */
736static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
737{
738 struct peer_prep_params p = {
739 .hello.peer = peer,
740 };
741
742 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
743}
744
745/*
746 * Send a success peer handshake status message.
747 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
748 * Returns -1 if there was not enough room left to send the message,
749 * any other negative returned value must be considered as an error with an appcxt st0
750 * returned value equal to PEER_SESS_ST_END.
751 */
752static inline int peer_send_status_successmsg(struct appctx *appctx)
753{
754 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
755}
756
757/*
758 * Send a peer handshake status error 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_status_errormsg(struct appctx *appctx)
765{
766 struct peer_prep_params p = {
767 .error_status.st1 = appctx->st1,
768 };
769
770 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
771}
772
773/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100774 * Send a stick-table switch 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_switchmsg(struct shared_table *st, struct appctx *appctx)
781{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100782 struct peer_prep_params p = {
783 .swtch.shared_table = st,
784 };
785
786 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100787}
788
789/*
790 * Send a stick-table update acknowledgement message.
791 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
792 * Returns -1 if there was not enough room left to send the message,
793 * any other negative returned value must be considered as an error with an appcxt st0
794 * returned value equal to PEER_SESS_ST_END.
795 */
796static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
797{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100798 struct peer_prep_params p = {
799 .ack.shared_table = st,
800 };
801
802 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100803}
804
805/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100806 * Send a stick-table update message.
807 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
808 * Returns -1 if there was not enough room left to send the message,
809 * any other negative returned value must be considered as an error with an appcxt st0
810 * returned value equal to PEER_SESS_ST_END.
811 */
812static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
813 unsigned int updateid, int use_identifier, int use_timed)
814{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100815 struct peer_prep_params p = {
816 .updt.stksess = ts,
817 .updt.shared_table = st,
818 .updt.updateid = updateid,
819 .updt.use_identifier = use_identifier,
820 .updt.use_timed = use_timed,
821 };
822
823 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100824}
825
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100826/*
827 * Build a peer protocol control class message.
828 * Returns the number of written bytes used to build the message if succeeded,
829 * 0 if not.
830 */
831static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
832{
833 if (size < sizeof p->control.head)
834 return 0;
835
836 msg[0] = p->control.head[0];
837 msg[1] = p->control.head[1];
838
839 return 2;
840}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100841
842/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100843 * Send a stick-table synchronization request message.
844 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
845 * Returns -1 if there was not enough room left to send the message,
846 * any other negative returned value must be considered as an error with an appctx st0
847 * returned value equal to PEER_SESS_ST_END.
848 */
849static inline int peer_send_resync_reqmsg(struct appctx *appctx)
850{
851 struct peer_prep_params p = {
852 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
853 };
854
855 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
856}
857
858/*
859 * Send a stick-table synchronization confirmation message.
860 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
861 * Returns -1 if there was not enough room left to send the message,
862 * any other negative returned value must be considered as an error with an appctx st0
863 * returned value equal to PEER_SESS_ST_END.
864 */
865static inline int peer_send_resync_confirmsg(struct appctx *appctx)
866{
867 struct peer_prep_params p = {
868 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
869 };
870
871 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
872}
873
874/*
875 * Send a stick-table synchronization finished message.
876 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
877 * Returns -1 if there was not enough room left to send the message,
878 * any other negative returned value must be considered as an error with an appctx st0
879 * returned value equal to PEER_SESS_ST_END.
880 */
881static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
882{
883 struct peer_prep_params p = {
884 .control.head = { PEER_MSG_CLASS_CONTROL, },
885 };
886
887 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
888 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
889
890 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
891}
892
893/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +0100894 * Send a heartbeat message.
895 * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value.
896 * Returns -1 if there was not enough room left to send the message,
897 * any other negative returned value must be considered as an error with an appctx st0
898 * returned value equal to PEER_SESS_ST_END.
899 */
900static inline int peer_send_heartbeatmsg(struct appctx *appctx)
901{
902 struct peer_prep_params p = {
903 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, },
904 };
905
906 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
907}
908
909/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100910 * Build a peer protocol error class message.
911 * Returns the number of written bytes used to build the message if succeeded,
912 * 0 if not.
913 */
914static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
915{
916 if (size < sizeof p->error.head)
917 return 0;
918
919 msg[0] = p->error.head[0];
920 msg[1] = p->error.head[1];
921
922 return 2;
923}
924
925/*
926 * Send a "size limit reached" error message.
927 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
928 * Returns -1 if there was not enough room left to send the message,
929 * any other negative returned value must be considered as an error with an appctx st0
930 * returned value equal to PEER_SESS_ST_END.
931 */
932static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
933{
934 struct peer_prep_params p = {
935 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
936 };
937
938 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
939}
940
941/*
942 * Send a "peer protocol" error message.
943 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
944 * Returns -1 if there was not enough room left to send the message,
945 * any other negative returned value must be considered as an error with an appctx st0
946 * returned value equal to PEER_SESS_ST_END.
947 */
948static inline int peer_send_error_protomsg(struct appctx *appctx)
949{
950 struct peer_prep_params p = {
951 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
952 };
953
954 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
955}
956
957/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100958 * Function used to lookup for recent stick-table updates associated with
959 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
960 */
961static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
962{
963 struct eb32_node *eb;
964
965 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
966 if (!eb) {
967 eb = eb32_first(&st->table->updates);
968 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
969 st->table->commitupdate = st->last_pushed = st->table->localupdate;
970 return NULL;
971 }
972 }
973
974 if ((int)(eb->key - st->table->localupdate) > 0) {
975 st->table->commitupdate = st->last_pushed = st->table->localupdate;
976 return NULL;
977 }
978
979 return eb32_entry(eb, struct stksess, upd);
980}
981
982/*
983 * Function used to lookup for recent stick-table updates associated with
984 * <st> shared stick-table during teach state 1 step.
985 */
986static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
987{
988 struct eb32_node *eb;
989
990 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
991 if (!eb) {
992 st->flags |= SHTABLE_F_TEACH_STAGE1;
993 eb = eb32_first(&st->table->updates);
994 if (eb)
995 st->last_pushed = eb->key - 1;
996 return NULL;
997 }
998
999 return eb32_entry(eb, struct stksess, upd);
1000}
1001
1002/*
1003 * Function used to lookup for recent stick-table updates associated with
1004 * <st> shared stick-table during teach state 2 step.
1005 */
1006static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
1007{
1008 struct eb32_node *eb;
1009
1010 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
1011 if (!eb || eb->key > st->teaching_origin) {
1012 st->flags |= SHTABLE_F_TEACH_STAGE2;
1013 return NULL;
1014 }
1015
1016 return eb32_entry(eb, struct stksess, upd);
1017}
1018
1019/*
1020 * Generic function to emit update messages for <st> stick-table when a lesson must
1021 * be taught to the peer <p>.
1022 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1023 * this function, 0 if not.
1024 *
1025 * This function temporary unlock/lock <st> when it sends stick-table updates or
1026 * when decrementing its refcount in case of any error when it sends this updates.
1027 *
1028 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1029 * Returns -1 if there was not enough room left to send the message,
1030 * any other negative returned value must be considered as an error with an appcxt st0
1031 * returned value equal to PEER_SESS_ST_END.
1032 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1033 * unlocked if not already locked when entering this function.
1034 */
1035static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1036 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1037 struct shared_table *st, int locked)
1038{
1039 int ret, new_pushed, use_timed;
1040
1041 ret = 1;
1042 use_timed = 0;
1043 if (st != p->last_local_table) {
1044 ret = peer_send_switchmsg(st, appctx);
1045 if (ret <= 0)
1046 return ret;
1047
1048 p->last_local_table = st;
1049 }
1050
1051 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1052 use_timed = !(p->flags & PEER_F_DWNGRD);
1053
1054 /* We force new pushed to 1 to force identifier in update message */
1055 new_pushed = 1;
1056
1057 if (!locked)
1058 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1059
1060 while (1) {
1061 struct stksess *ts;
1062 unsigned updateid;
1063
1064 /* push local updates */
1065 ts = peer_stksess_lookup(st);
1066 if (!ts)
1067 break;
1068
1069 updateid = ts->upd.key;
1070 ts->ref_cnt++;
1071 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1072
1073 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1074 if (ret <= 0) {
1075 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1076 ts->ref_cnt--;
1077 if (!locked)
1078 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1079 return ret;
1080 }
1081
1082 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1083 ts->ref_cnt--;
1084 st->last_pushed = updateid;
1085
1086 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1087 (int)(st->last_pushed - st->table->commitupdate) > 0)
1088 st->table->commitupdate = st->last_pushed;
1089
1090 /* identifier may not needed in next update message */
1091 new_pushed = 0;
1092 }
1093
1094 out:
1095 if (!locked)
1096 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1097 return 1;
1098}
1099
1100/*
1101 * Function to emit update messages for <st> stick-table when a lesson must
1102 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1103 *
1104 * Note that <st> shared stick-table is locked when calling this function.
1105 *
1106 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1107 * Returns -1 if there was not enough room left to send the message,
1108 * any other negative returned value must be considered as an error with an appcxt st0
1109 * returned value equal to PEER_SESS_ST_END.
1110 */
1111static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1112 struct shared_table *st)
1113{
1114 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1115}
1116
1117/*
1118 * Function to emit update messages for <st> stick-table when a lesson must
1119 * be taught to the peer <p> during teach state 1 step.
1120 *
1121 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1122 * Returns -1 if there was not enough room left to send the message,
1123 * any other negative returned value must be considered as an error with an appcxt st0
1124 * returned value equal to PEER_SESS_ST_END.
1125 */
1126static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1127 struct shared_table *st)
1128{
1129 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1130}
1131
1132/*
1133 * Function to emit update messages for <st> stick-table when a lesson must
1134 * be taught to the peer <p> during teach state 1 step.
1135 *
1136 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1137 * Returns -1 if there was not enough room left to send the message,
1138 * any other negative returned value must be considered as an error with an appcxt st0
1139 * returned value equal to PEER_SESS_ST_END.
1140 */
1141static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1142 struct shared_table *st)
1143{
1144 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1145}
1146
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001147
1148/*
1149 * Function used to parse a stick-table update message after it has been received
1150 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1151 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1152 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1153 * was encountered.
1154 * <exp> must be set if the stick-table entry expires.
1155 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1156 * messages, in this case the stick-table udpate message is received with a stick-table
1157 * update ID.
1158 * <totl> is the length of the stick-table update message computed upon receipt.
1159 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001160static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1161 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001162{
1163 struct stream_interface *si = appctx->owner;
1164 struct shared_table *st = p->remote_table;
1165 struct stksess *ts, *newts;
1166 uint32_t update;
1167 int expire;
1168 unsigned int data_type;
1169 void *data_ptr;
1170
1171 /* Here we have data message */
1172 if (!st)
1173 goto ignore_msg;
1174
1175 expire = MS_TO_TICKS(st->table->expire);
1176
1177 if (updt) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001178 if (msg_len < sizeof(update))
1179 goto malformed_exit;
1180
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001181 memcpy(&update, *msg_cur, sizeof(update));
1182 *msg_cur += sizeof(update);
1183 st->last_get = htonl(update);
1184 }
1185 else {
1186 st->last_get++;
1187 }
1188
1189 if (exp) {
1190 size_t expire_sz = sizeof expire;
1191
Willy Tarreau1e82a142019-01-29 11:08:06 +01001192 if (*msg_cur + expire_sz > msg_end)
1193 goto malformed_exit;
1194
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001195 memcpy(&expire, *msg_cur, expire_sz);
1196 *msg_cur += expire_sz;
1197 expire = ntohl(expire);
1198 }
1199
1200 newts = stksess_new(st->table, NULL);
1201 if (!newts)
1202 goto ignore_msg;
1203
1204 if (st->table->type == SMP_T_STR) {
1205 unsigned int to_read, to_store;
1206
1207 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001208 if (!*msg_cur)
1209 goto malformed_free_newts;
1210
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001211 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001212 if (*msg_cur + to_store > msg_end)
1213 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001214
1215 memcpy(newts->key.key, *msg_cur, to_store);
1216 newts->key.key[to_store] = 0;
1217 *msg_cur += to_read;
1218 }
1219 else if (st->table->type == SMP_T_SINT) {
1220 unsigned int netinteger;
1221
Willy Tarreau1e82a142019-01-29 11:08:06 +01001222 if (*msg_cur + sizeof(netinteger) > msg_end)
1223 goto malformed_free_newts;
1224
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001225 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1226 netinteger = ntohl(netinteger);
1227 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1228 *msg_cur += sizeof(netinteger);
1229 }
1230 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001231 if (*msg_cur + st->table->key_size > msg_end)
1232 goto malformed_free_newts;
1233
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001234 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1235 *msg_cur += st->table->key_size;
1236 }
1237
1238 /* lookup for existing entry */
1239 ts = stktable_set_entry(st->table, newts);
1240 if (ts != newts) {
1241 stksess_free(st->table, newts);
1242 newts = NULL;
1243 }
1244
1245 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1246
1247 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001248 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001249
1250 if (!((1 << data_type) & st->remote_data))
1251 continue;
1252
Willy Tarreau1e82a142019-01-29 11:08:06 +01001253 decoded_int = intdecode(msg_cur, msg_end);
1254 if (!*msg_cur)
1255 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001256
Willy Tarreau1e82a142019-01-29 11:08:06 +01001257 switch (stktable_data_types[data_type].std_type) {
1258 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001259 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1260 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001261 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001262 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001263
Willy Tarreau1e82a142019-01-29 11:08:06 +01001264 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001265 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1266 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001267 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001268 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001269
Willy Tarreau1e82a142019-01-29 11:08:06 +01001270 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001271 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1272 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001273 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001274 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001275
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001276 case STD_T_FRQP: {
1277 struct freq_ctr_period data;
1278
1279 /* First bit is reserved for the freq_ctr_period lock
1280 Note: here we're still protected by the stksess lock
1281 so we don't need to update the update the freq_ctr_period
1282 using its internal lock */
1283
Willy Tarreau1e82a142019-01-29 11:08:06 +01001284 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001285 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001286 if (!*msg_cur)
1287 goto malformed_unlock;
1288
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001289 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001290 if (!*msg_cur)
1291 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001292
1293 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1294 if (data_ptr)
1295 stktable_data_cast(data_ptr, std_t_frqp) = data;
1296 break;
1297 }
1298 }
1299 }
1300 /* Force new expiration */
1301 ts->expire = tick_add(now_ms, expire);
1302
1303 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1304 stktable_touch_remote(st->table, ts, 1);
1305 return 1;
1306
1307 ignore_msg:
1308 /* skip consumed message */
1309 co_skip(si_oc(si), totl);
1310 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001311
1312 malformed_unlock:
1313 /* malformed message */
1314 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1315 stktable_touch_remote(st->table, ts, 1);
1316 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1317 return 0;
1318
1319 malformed_free_newts:
1320 /* malformed message */
1321 stksess_free(st->table, newts);
1322 malformed_exit:
1323 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1324 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001325}
1326
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001327/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001328 * Function used to parse a stick-table update acknowledgement message after it
1329 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1330 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1331 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1332 * was encountered.
1333 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1334 */
1335static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1336 char **msg_cur, char *msg_end)
1337{
1338 /* ack message */
1339 uint32_t table_id ;
1340 uint32_t update;
1341 struct shared_table *st;
1342
1343 table_id = intdecode(msg_cur, msg_end);
1344 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1345 /* malformed message */
1346 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1347 return 0;
1348 }
1349
1350 memcpy(&update, *msg_cur, sizeof(update));
1351 update = ntohl(update);
1352
1353 for (st = p->tables; st; st = st->next) {
1354 if (st->local_id == table_id) {
1355 st->update = update;
1356 break;
1357 }
1358 }
1359
1360 return 1;
1361}
1362
1363/*
1364 * Function used to parse a stick-table switch message after it has been received
1365 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1366 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1367 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1368 * was encountered.
1369 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1370 */
1371static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1372 char **msg_cur, char *msg_end)
1373{
1374 struct shared_table *st;
1375 int table_id;
1376
1377 table_id = intdecode(msg_cur, msg_end);
1378 if (!*msg_cur) {
1379 /* malformed message */
1380 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1381 return 0;
1382 }
1383
1384 p->remote_table = NULL;
1385 for (st = p->tables; st; st = st->next) {
1386 if (st->remote_id == table_id) {
1387 p->remote_table = st;
1388 break;
1389 }
1390 }
1391
1392 return 1;
1393}
1394
1395/*
1396 * Function used to parse a stick-table definition message after it has been received
1397 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1398 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1399 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1400 * was encountered.
1401 * <totl> is the length of the stick-table update message computed upon receipt.
1402 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1403 */
1404static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1405 char **msg_cur, char *msg_end, int totl)
1406{
1407 struct stream_interface *si = appctx->owner;
1408 int table_id_len;
1409 struct shared_table *st;
1410 int table_type;
1411 int table_keylen;
1412 int table_id;
1413 uint64_t table_data;
1414
1415 table_id = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001416 if (!*msg_cur)
1417 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001418
1419 table_id_len = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001420 if (!*msg_cur)
1421 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001422
1423 p->remote_table = NULL;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001424 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end)
1425 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001426
1427 for (st = p->tables; st; st = st->next) {
1428 /* Reset IDs */
1429 if (st->remote_id == table_id)
1430 st->remote_id = 0;
1431
1432 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1433 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1434 p->remote_table = st;
1435 }
1436
1437 if (!p->remote_table)
1438 goto ignore_msg;
1439
1440 *msg_cur += table_id_len;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001441 if (*msg_cur >= msg_end)
1442 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001443
1444 table_type = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001445 if (!*msg_cur)
1446 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001447
1448 table_keylen = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001449 if (!*msg_cur)
1450 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001451
1452 table_data = intdecode(msg_cur, msg_end);
Willy Tarreau6f731f32019-01-29 11:11:23 +01001453 if (!*msg_cur)
1454 goto malformed_exit;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001455
1456 if (p->remote_table->table->type != table_type
1457 || p->remote_table->table->key_size != table_keylen) {
1458 p->remote_table = NULL;
1459 goto ignore_msg;
1460 }
1461
1462 p->remote_table->remote_data = table_data;
1463 p->remote_table->remote_id = table_id;
1464 return 1;
1465
1466 ignore_msg:
1467 co_skip(si_oc(si), totl);
1468 return 0;
Willy Tarreau6f731f32019-01-29 11:11:23 +01001469
1470 malformed_exit:
1471 /* malformed message */
1472 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1473 return 0;
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001474}
1475
1476/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001477 * Receive a stick-table message.
1478 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1479 * -1 if there was an error updating the appctx state st0 accordingly.
1480 */
1481static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1482 uint32_t *msg_len, int *totl)
1483{
1484 int reql;
1485 struct stream_interface *si = appctx->owner;
1486
1487 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1488 if (reql <= 0) /* closed or EOL not found */
1489 goto incomplete;
1490
1491 *totl += reql;
1492
1493 if ((unsigned int)msg_head[1] < 128)
1494 return 1;
1495
1496 /* Read and Decode message length */
1497 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1498 if (reql <= 0) /* closed */
1499 goto incomplete;
1500
1501 *totl += reql;
1502
1503 if ((unsigned int)msg_head[2] < 240) {
1504 *msg_len = msg_head[2];
1505 }
1506 else {
1507 int i;
1508 char *cur;
1509 char *end;
1510
1511 for (i = 3 ; i < msg_head_sz ; i++) {
1512 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1513 if (reql <= 0) /* closed */
1514 goto incomplete;
1515
1516 *totl += reql;
1517
1518 if (!(msg_head[i] & 0x80))
1519 break;
1520 }
1521
1522 if (i == msg_head_sz) {
1523 /* malformed message */
1524 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1525 return -1;
1526 }
1527 end = msg_head + msg_head_sz;
1528 cur = &msg_head[2];
1529 *msg_len = intdecode(&cur, end);
1530 if (!cur) {
1531 /* malformed message */
1532 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1533 return -1;
1534 }
1535 }
1536
1537 /* Read message content */
1538 if (*msg_len) {
1539 if (*msg_len > trash.size) {
1540 /* Status code is not success, abort */
1541 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1542 return -1;
1543 }
1544
1545 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1546 if (reql <= 0) /* closed */
1547 goto incomplete;
1548 *totl += reql;
1549 }
1550
1551 return 1;
1552
1553 incomplete:
1554 if (reql < 0) {
1555 /* there was an error */
1556 appctx->st0 = PEER_SESS_ST_END;
1557 return -1;
1558 }
1559
1560 return 0;
1561}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001562
1563/*
1564 * Treat the awaited message with <msg_head> as header.*
1565 * Return 1 if succeeded, 0 if not.
1566 */
1567static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1568 char **msg_cur, char *msg_end, int msg_len, int totl)
1569{
1570 struct stream_interface *si = appctx->owner;
1571 struct stream *s = si_strm(si);
1572 struct peers *peers = strm_fe(s)->parent;
1573
1574 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1575 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1576 struct shared_table *st;
1577 /* Reset message: remote need resync */
1578
1579 /* prepare tables fot a global push */
1580 for (st = peer->tables; st; st = st->next) {
1581 st->teaching_origin = st->last_pushed = st->table->update;
1582 st->flags = 0;
1583 }
1584
1585 /* reset teaching flags to 0 */
1586 peer->flags &= PEER_TEACH_RESET;
1587
1588 /* flag to start to teach lesson */
1589 peer->flags |= PEER_F_TEACH_PROCESS;
1590 }
1591 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1592 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1593 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1594 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1595 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1596 }
1597 peer->confirm++;
1598 }
1599 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1600 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1601 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1602 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1603
1604 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
1605 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1606 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1607 }
1608 peer->confirm++;
1609 }
1610 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1611 struct shared_table *st;
1612
1613 /* If stopping state */
1614 if (stopping) {
1615 /* Close session, push resync no more needed */
1616 peer->flags |= PEER_F_TEACH_COMPLETE;
1617 appctx->st0 = PEER_SESS_ST_END;
1618 return 0;
1619 }
1620 for (st = peer->tables; st; st = st->next) {
1621 st->update = st->last_pushed = st->teaching_origin;
1622 st->flags = 0;
1623 }
1624
1625 /* reset teaching flags to 0 */
1626 peer->flags &= PEER_TEACH_RESET;
1627 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01001628 else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) {
1629 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
1630 }
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001631 }
1632 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1633 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1634 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1635 return 0;
1636 }
1637 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1638 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1639 return 0;
1640 }
1641 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1642 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1643 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1644 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1645 int update, expire;
1646
1647 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1648 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1649 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1650 msg_cur, msg_end, msg_len, totl))
1651 return 0;
1652
1653 }
1654 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1655 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1656 return 0;
1657 }
1658 }
1659 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1660 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1661 return 0;
1662 }
1663
1664 return 1;
1665}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001666
1667
1668/*
1669 * Send any message to <peer> peer.
1670 * Returns 1 if succeeded, or -1 or 0 if failed.
1671 * -1 means an internal error occured, 0 is for a peer protocol error leading
1672 * to a peer state change (from the peer I/O handler point of view).
1673 */
1674static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1675{
1676 int repl;
1677 struct stream_interface *si = appctx->owner;
1678 struct stream *s = si_strm(si);
1679 struct peers *peers = strm_fe(s)->parent;
1680
1681 /* Need to request a resync */
1682 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1683 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1684 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1685
1686 repl = peer_send_resync_reqmsg(appctx);
1687 if (repl <= 0)
1688 return repl;
1689
1690 peers->flags |= PEERS_F_RESYNC_PROCESS;
1691 }
1692
1693 /* Nothing to read, now we start to write */
1694 if (peer->tables) {
1695 struct shared_table *st;
1696 struct shared_table *last_local_table;
1697
1698 last_local_table = peer->last_local_table;
1699 if (!last_local_table)
1700 last_local_table = peer->tables;
1701 st = last_local_table->next;
1702
1703 while (1) {
1704 if (!st)
1705 st = peer->tables;
1706
1707 /* It remains some updates to ack */
1708 if (st->last_get != st->last_acked) {
1709 repl = peer_send_ackmsg(st, appctx);
1710 if (repl <= 0)
1711 return repl;
1712
1713 st->last_acked = st->last_get;
1714 }
1715
1716 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1717 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1718 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1719 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1720
1721 repl = peer_send_teach_process_msgs(appctx, peer, st);
1722 if (repl <= 0) {
1723 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1724 return repl;
1725 }
1726 }
1727 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1728 }
1729 else {
1730 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1731 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1732 if (repl <= 0)
1733 return repl;
1734 }
1735
1736 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1737 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1738 if (repl <= 0)
1739 return repl;
1740 }
1741 }
1742
1743 if (st == last_local_table)
1744 break;
1745 st = st->next;
1746 }
1747 }
1748
1749 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1750 repl = peer_send_resync_finishedmsg(appctx, peer);
1751 if (repl <= 0)
1752 return repl;
1753
1754 /* flag finished message sent */
1755 peer->flags |= PEER_F_TEACH_FINISHED;
1756 }
1757
1758 /* Confirm finished or partial messages */
1759 while (peer->confirm) {
1760 repl = peer_send_resync_confirmsg(appctx);
1761 if (repl <= 0)
1762 return repl;
1763
1764 peer->confirm--;
1765 }
1766
1767 return 1;
1768}
1769
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001770/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001771 * Read and parse a first line of a "hello" peer protocol message.
1772 * Returns 0 if could not read a line, -1 if there was a read error or
1773 * the line is malformed, 1 if succeeded.
1774 */
1775static inline int peer_getline_version(struct appctx *appctx,
1776 unsigned int *maj_ver, unsigned int *min_ver)
1777{
1778 int reql;
1779
1780 reql = peer_getline(appctx);
1781 if (!reql)
1782 return 0;
1783
1784 if (reql < 0)
1785 return -1;
1786
1787 /* test protocol */
1788 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1789 appctx->st0 = PEER_SESS_ST_EXIT;
1790 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1791 return -1;
1792 }
1793 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1794 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1795 appctx->st0 = PEER_SESS_ST_EXIT;
1796 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1797 return -1;
1798 }
1799
1800 return 1;
1801}
1802
1803/*
1804 * Read and parse a second line of a "hello" peer protocol message.
1805 * Returns 0 if could not read a line, -1 if there was a read error or
1806 * the line is malformed, 1 if succeeded.
1807 */
1808static inline int peer_getline_host(struct appctx *appctx)
1809{
1810 int reql;
1811
1812 reql = peer_getline(appctx);
1813 if (!reql)
1814 return 0;
1815
1816 if (reql < 0)
1817 return -1;
1818
1819 /* test hostname match */
1820 if (strcmp(localpeer, trash.area) != 0) {
1821 appctx->st0 = PEER_SESS_ST_EXIT;
1822 appctx->st1 = PEER_SESS_SC_ERRHOST;
1823 return -1;
1824 }
1825
1826 return 1;
1827}
1828
1829/*
1830 * Read and parse a last line of a "hello" peer protocol message.
1831 * Returns 0 if could not read a character, -1 if there was a read error or
1832 * the line is malformed, 1 if succeeded.
1833 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1834 */
1835static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1836{
1837 char *p;
1838 int reql;
1839 struct peer *peer;
1840 struct stream_interface *si = appctx->owner;
1841 struct stream *s = si_strm(si);
1842 struct peers *peers = strm_fe(s)->parent;
1843
1844 reql = peer_getline(appctx);
1845 if (!reql)
1846 return 0;
1847
1848 if (reql < 0)
1849 return -1;
1850
1851 /* parse line "<peer name> <pid> <relative_pid>" */
1852 p = strchr(trash.area, ' ');
1853 if (!p) {
1854 appctx->st0 = PEER_SESS_ST_EXIT;
1855 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1856 return -1;
1857 }
1858 *p = 0;
1859
1860 /* lookup known peer */
1861 for (peer = peers->remote; peer; peer = peer->next) {
1862 if (strcmp(peer->id, trash.area) == 0)
1863 break;
1864 }
1865
1866 /* if unknown peer */
1867 if (!peer) {
1868 appctx->st0 = PEER_SESS_ST_EXIT;
1869 appctx->st1 = PEER_SESS_SC_ERRPEER;
1870 return -1;
1871 }
1872 *curpeer = peer;
1873
1874 return 1;
1875}
1876
1877/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01001878 * Init <peer> peer after having accepted it at peer protocol level.
1879 */
1880static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
1881{
1882 struct shared_table *st;
1883
1884 /* Register status code */
1885 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
1886
1887 /* Awake main task */
1888 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1889
1890 /* Init confirm counter */
1891 peer->confirm = 0;
1892
1893 /* Init cursors */
1894 for (st = peer->tables; st ; st = st->next) {
1895 st->last_get = st->last_acked = 0;
1896 st->teaching_origin = st->last_pushed = st->update;
1897 }
1898
1899 /* reset teaching and learning flags to 0 */
1900 peer->flags &= PEER_TEACH_RESET;
1901 peer->flags &= PEER_LEARN_RESET;
1902
1903 /* if current peer is local */
1904 if (peer->local) {
1905 /* if current host need resyncfrom local and no process assined */
1906 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1907 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1908 /* assign local peer for a lesson, consider lesson already requested */
1909 peer->flags |= PEER_F_LEARN_ASSIGN;
1910 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1911 }
1912
1913 }
1914 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1915 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1916 /* assign peer for a lesson */
1917 peer->flags |= PEER_F_LEARN_ASSIGN;
1918 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1919 }
1920}
1921
1922/*
1923 * Init <peer> peer after having connected it at peer protocol level.
1924 */
1925static inline void init_connected_peer(struct peer *peer, struct peers *peers)
1926{
1927 struct shared_table *st;
1928
1929 /* Init cursors */
1930 for (st = peer->tables; st ; st = st->next) {
1931 st->last_get = st->last_acked = 0;
1932 st->teaching_origin = st->last_pushed = st->update;
1933 }
1934
1935 /* Init confirm counter */
1936 peer->confirm = 0;
1937
1938 /* reset teaching and learning flags to 0 */
1939 peer->flags &= PEER_TEACH_RESET;
1940 peer->flags &= PEER_LEARN_RESET;
1941
1942 /* If current peer is local */
1943 if (peer->local) {
1944 /* flag to start to teach lesson */
1945 peer->flags |= PEER_F_TEACH_PROCESS;
1946 }
1947 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1948 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1949 /* If peer is remote and resync from remote is needed,
1950 and no peer currently assigned */
1951
1952 /* assign peer for a lesson */
1953 peer->flags |= PEER_F_LEARN_ASSIGN;
1954 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1955 }
1956}
1957
1958/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001959 * IO Handler to handle message exchance with a peer
1960 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001961static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001962{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001963 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02001964 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001965 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02001966 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001967 int reql = 0;
1968 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001969 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001970 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02001971
Joseph Herlant82b2f542018-11-15 12:19:14 -08001972 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001973 if (si_ic(si)->buf.size == 0) {
1974 si_rx_room_blk(si);
1975 goto out;
1976 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001977
Emeric Brun2b920a12010-09-23 18:30:22 +02001978 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001979 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001980switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001981 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001982 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001983 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001984 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001985 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001986 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001987 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001988 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001989 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001990 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
1991 if (reql <= 0) {
1992 if (!reql)
1993 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001994 goto switchstate;
1995 }
1996
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001997 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001998 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001999 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002000 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002001 reql = peer_getline_host(appctx);
2002 if (reql <= 0) {
2003 if (!reql)
2004 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002005 goto switchstate;
2006 }
2007
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002008 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002009 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002010 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002011 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002012 reql = peer_getline_last(appctx, &curpeer);
2013 if (reql <= 0) {
2014 if (!reql)
2015 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002016 goto switchstate;
2017 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002018
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002019 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002020 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002021 if (curpeer->local) {
2022 /* Local connection, reply a retry */
2023 appctx->st0 = PEER_SESS_ST_EXIT;
2024 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2025 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002026 }
Emeric Brun80527f52017-06-19 17:46:37 +02002027
2028 /* we're killing a connection, we must apply a random delay before
2029 * retrying otherwise the other end will do the same and we can loop
2030 * for a while.
2031 */
2032 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002033 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002034 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002035 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2036 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2037 curpeer->flags |= PEER_F_DWNGRD;
2038 }
2039 else {
2040 curpeer->flags &= ~PEER_F_DWNGRD;
2041 }
2042 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002043 curpeer->appctx = appctx;
2044 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002045 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Olivier Houcharded879892019-03-08 18:53:43 +01002046 _HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002047 /* fall through */
2048 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002049 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002050 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002051 if (!curpeer) {
2052 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002053 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002054 if (curpeer->appctx != appctx) {
2055 appctx->st0 = PEER_SESS_ST_END;
2056 goto switchstate;
2057 }
2058 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002059
2060 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002061 if (repl <= 0) {
2062 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002063 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002064 goto switchstate;
2065 }
2066
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002067 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002068
Emeric Brun2b920a12010-09-23 18:30:22 +02002069 /* switch to waiting message state */
Olivier Houcharded879892019-03-08 18:53:43 +01002070 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002071 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002072 goto switchstate;
2073 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002074 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002075 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002076 if (!curpeer) {
2077 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002078 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002079 if (curpeer->appctx != appctx) {
2080 appctx->st0 = PEER_SESS_ST_END;
2081 goto switchstate;
2082 }
2083 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002084
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002085 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002086 if (repl <= 0) {
2087 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002088 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 goto switchstate;
2090 }
2091
2092 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002093 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002094 /* fall through */
2095 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002096 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002097 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002098 if (!curpeer) {
2099 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002100 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002101 if (curpeer->appctx != appctx) {
2102 appctx->st0 = PEER_SESS_ST_END;
2103 goto switchstate;
2104 }
2105 }
2106
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002107 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002108 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002109
Frédéric Lécaillece025572019-01-21 13:38:06 +01002110 reql = peer_getline(appctx);
2111 if (!reql)
2112 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002113
Frédéric Lécaillece025572019-01-21 13:38:06 +01002114 if (reql < 0)
2115 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002116
2117 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002118 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002119
2120 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002121 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002122
2123 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002124 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002125 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002126 }
2127 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002128 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2129 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002130 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002131 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002132 goto switchstate;
2133 }
Olivier Houcharded879892019-03-08 18:53:43 +01002134 _HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002135 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002136 /* fall through */
2137 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002138 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002139 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002140 char *msg_cur = trash.area;
2141 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002142 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002143 int totl = 0;
2144
Willy Tarreau2d372c22018-11-05 17:12:27 +01002145 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002146 if (!curpeer) {
2147 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002149 if (curpeer->appctx != appctx) {
2150 appctx->st0 = PEER_SESS_ST_END;
2151 goto switchstate;
2152 }
2153 }
2154
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002155 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2156 if (reql <= 0) {
2157 if (reql == -1)
2158 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002159 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002160 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002161
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002162 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002163 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002164 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002165
Emeric Brun2b920a12010-09-23 18:30:22 +02002166 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002167 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002168 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002169 goto switchstate;
2170
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002171send_msgs:
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002172 if (curpeer->flags & PEER_F_HEARTBEAT) {
2173 curpeer->flags &= ~PEER_F_HEARTBEAT;
2174 repl = peer_send_heartbeatmsg(appctx);
2175 if (repl <= 0) {
2176 if (repl == -1)
2177 goto out;
2178 goto switchstate;
2179 }
2180 }
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002181 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002182 repl = peer_send_msgs(appctx, curpeer);
2183 if (repl <= 0) {
2184 if (repl == -1)
2185 goto out;
2186 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002187 }
2188
Emeric Brun2b920a12010-09-23 18:30:22 +02002189 /* noting more to do */
2190 goto out;
2191 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002192 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002193 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002194 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002195 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002196 if (peer_send_status_errormsg(appctx) == -1)
2197 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002198 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002199 goto switchstate;
2200 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002201 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002202 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002203 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002204 if (peer_send_error_size_limitmsg(appctx) == -1)
2205 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002206 appctx->st0 = PEER_SESS_ST_END;
2207 goto switchstate;
2208 }
2209 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002210 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002211 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002212 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002213 if (peer_send_error_protomsg(appctx) == -1)
2214 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002215 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002216 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002217 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002218 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002219 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002220 if (prev_state == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002221 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau2d372c22018-11-05 17:12:27 +01002222 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002223 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002224 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002225 curpeer = NULL;
2226 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002227 si_shutw(si);
2228 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002229 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002230 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002231 }
2232 }
2233 }
2234out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002235 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002236
2237 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002238 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002239 return;
2240}
2241
Willy Tarreau30576452015-04-13 13:50:30 +02002242static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002243 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002244 .name = "<PEER>", /* used for logging */
2245 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002246 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002247};
Emeric Brun2b920a12010-09-23 18:30:22 +02002248
2249/*
2250 * Use this function to force a close of a peer session
2251 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002252static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002253{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002254 /* Note that the peer sessions which have just been created
2255 * (->st0 == PEER_SESS_ST_CONNECT) must not
2256 * be shutdown, if not, the TCP session will never be closed
2257 * and stay in CLOSE_WAIT state after having been closed by
2258 * the remote side.
2259 */
2260 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002261 return;
2262
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002263 if (appctx->applet != &peer_applet)
2264 return;
2265
Willy Tarreau2d372c22018-11-05 17:12:27 +01002266 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
Olivier Houcharded879892019-03-08 18:53:43 +01002267 _HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002268 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002269 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002270}
2271
Willy Tarreau91d96282015-03-13 15:47:26 +01002272/* Pre-configures a peers frontend to accept incoming connections */
2273void peers_setup_frontend(struct proxy *fe)
2274{
2275 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002276 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002277 fe->maxconn = 0;
2278 fe->conn_retries = CONN_RETRIES;
2279 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002280 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002281 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002282 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002283 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002284}
2285
Emeric Brun2b920a12010-09-23 18:30:22 +02002286/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002287 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002288 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002289static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002290{
Willy Tarreau04b92862017-09-15 11:01:04 +02002291 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002292 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002293 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002294 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002295 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002296 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002297
Emeric Brunb3971ab2015-05-12 18:49:09 +02002298 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002299 peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002300 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002301 s = NULL;
2302
Emeric Brun1138fd02017-06-19 12:38:55 +02002303 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002304 if (!appctx)
2305 goto out_close;
2306
2307 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002308 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002309
Willy Tarreau04b92862017-09-15 11:01:04 +02002310 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002311 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002312 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002313 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002314 }
2315
Willy Tarreau87787ac2017-08-28 16:22:54 +02002316 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002317 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002318 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002319 }
2320
Willy Tarreau342bfb12015-04-05 01:35:34 +02002321 /* The tasks below are normally what is supposed to be done by
2322 * fe->accept().
2323 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002324 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002325
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002326 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002327 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002328 appctx_wakeup(appctx);
2329
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002330 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002331 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002332 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002333
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002334 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002335 * pre-initialized connection in si->conn.
2336 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002337 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002338 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002339
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002340 if (unlikely((cs = cs_new(conn)) == NULL))
2341 goto out_free_conn;
2342
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002343 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002344 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2345
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002346 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardef60ff32019-01-29 19:00:33 +01002347 if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0)
2348 goto out_free_cs;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002349 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002350
Emeric Brun2b920a12010-09-23 18:30:22 +02002351 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002352 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002353
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002354 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002355
Emeric Brunb3971ab2015-05-12 18:49:09 +02002356 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002357 task_wakeup(s->task, TASK_WOKEN_INIT);
Olivier Houcharded879892019-03-08 18:53:43 +01002358 _HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002359 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002360
2361 /* Error unrolling */
Olivier Houchardef60ff32019-01-29 19:00:33 +01002362out_free_cs:
2363 cs_free(cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002364 out_free_conn:
2365 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002366 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002367 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002368 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002369 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002370 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002371 out_free_appctx:
2372 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002373 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002374 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002375}
2376
2377/*
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002378 * Task processing function to manage re-connect, peer session
2379 * tasks wakeup on local update and heartbeat.
Emeric Brun2b920a12010-09-23 18:30:22 +02002380 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002381static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002382{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002383 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002384 struct peer *ps;
2385 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002386
2387 task->expire = TICK_ETERNITY;
2388
Emeric Brunb3971ab2015-05-12 18:49:09 +02002389 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002390 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002391 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002392 task_delete(peers->sync_task);
2393 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002394 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002395 return NULL;
2396 }
2397
Emeric Brun80527f52017-06-19 17:46:37 +02002398 /* Acquire lock for all peers of the section */
2399 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002400 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002401
Emeric Brun2b920a12010-09-23 18:30:22 +02002402 if (!stopping) {
2403 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002404
2405 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2406 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2407 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002408 /* Resync from local peer needed
2409 no peer was assigned for the lesson
2410 and no old local peer found
2411 or resync timeout expire */
2412
2413 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002414 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002415
2416 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002417 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002418 }
2419
2420 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002421 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002422 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002423 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002424 if (!ps->appctx) {
2425 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002426 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002427 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002428 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002429 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002430 tick_is_expired(ps->reconnect, now_ms))) {
2431 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002432 * or previous peer connection established with success
2433 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002434 * and reconnection timer is expired */
2435
2436 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002437 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002438 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002439 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002440 /* If previous session failed during connection
2441 * but reconnection timer is not expired */
2442
2443 /* reschedule task for reconnect */
2444 task->expire = tick_first(task->expire, ps->reconnect);
2445 }
2446 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002447 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002448 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002449 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002450 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2451 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002452 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2453 /* Resync from a remote is needed
2454 * and no peer was assigned for lesson
2455 * and current peer may be up2date */
2456
2457 /* assign peer for the lesson */
2458 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002459 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002460
Willy Tarreau9df94c22016-10-31 18:42:52 +01002461 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002462 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002463 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002464 else {
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002465 int update_to_push = 0;
2466
Emeric Brunb3971ab2015-05-12 18:49:09 +02002467 /* Awake session if there is data to push */
2468 for (st = ps->tables; st ; st = st->next) {
2469 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002470 /* wake up the peer handler to push local updates */
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002471 update_to_push = 1;
2472 ps->flags &= ~PEER_F_HEARTBEAT;
2473 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002474 appctx_wakeup(ps->appctx);
2475 break;
2476 }
2477 }
Frédéric Lécaille645635d2019-02-11 17:49:39 +01002478 if (!update_to_push && tick_is_expired(ps->heartbeat, now_ms)) {
2479 ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT));
2480 ps->flags |= PEER_F_HEARTBEAT;
2481 appctx_wakeup(ps->appctx);
2482 }
2483 task->expire = tick_first(task->expire, ps->heartbeat);
Emeric Brun2b920a12010-09-23 18:30:22 +02002484 }
2485 /* else do nothing */
2486 } /* SUCCESSCODE */
2487 } /* !ps->peer->local */
2488 } /* for */
2489
2490 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002491 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2492 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2493 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002494 /* Resync from remote peer needed
2495 * no peer was assigned for the lesson
2496 * and resync timeout expire */
2497
2498 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002499 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002500 }
2501
Emeric Brunb3971ab2015-05-12 18:49:09 +02002502 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002503 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002504 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2505 if (!tick_is_expired(peers->resync_timeout, now_ms))
2506 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002507 }
2508 } /* !stopping */
2509 else {
2510 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002511 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002512 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002513 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002514 /* add DO NOT STOP flag if not present */
Olivier Houcharded879892019-03-08 18:53:43 +01002515 _HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002516 peers->flags |= PEERS_F_DONOTSTOP;
2517 ps = peers->local;
2518 for (st = ps->tables; st ; st = st->next)
2519 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002520 }
2521
2522 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002523 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002524 /* we're killing a connection, we must apply a random delay before
2525 * retrying otherwise the other end will do the same and we can loop
2526 * for a while.
2527 */
2528 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002529 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002530 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002531 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002532 }
2533 }
2534 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002535
Emeric Brunb3971ab2015-05-12 18:49:09 +02002536 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002537 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002538 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002539 /* resync of new process was complete, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002540 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002541 peers->flags &= ~PEERS_F_DONOTSTOP;
2542 for (st = ps->tables; st ; st = st->next)
2543 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002544 }
2545 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002546 else if (!ps->appctx) {
2547 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002548 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002549 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2550 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2551 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002552 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002553 * or previous peer connection was successfully established
2554 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002555 * or during previous connect, peer replies a try again statuscode */
2556
2557 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002558 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002559 }
2560 else {
2561 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002562 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002563 /* unable to resync new process, current process can die now */
Olivier Houcharded879892019-03-08 18:53:43 +01002564 _HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002565 peers->flags &= ~PEERS_F_DONOTSTOP;
2566 for (st = ps->tables; st ; st = st->next)
2567 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002568 }
2569 }
2570 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002571 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002572 /* current peer connection is active and established
2573 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002574 for (st = ps->tables; st ; st = st->next) {
2575 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002576 appctx_wakeup(ps->appctx);
2577 break;
2578 }
2579 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002580 }
2581 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002582
2583 /* Release lock for all peers of the section */
2584 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002585 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002586
Emeric Brun2b920a12010-09-23 18:30:22 +02002587 /* Wakeup for re-connect */
2588 return task;
2589}
2590
Emeric Brunb3971ab2015-05-12 18:49:09 +02002591
Emeric Brun2b920a12010-09-23 18:30:22 +02002592/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002593 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002594 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002595int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002596{
Emeric Brun2b920a12010-09-23 18:30:22 +02002597 struct peer * curpeer;
Emeric Brun2b920a12010-09-23 18:30:22 +02002598
Emeric Brun2b920a12010-09-23 18:30:22 +02002599 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002600 peers->peers_fe->maxconn += 3;
2601 }
2602
Emeric Brunc60def82017-09-27 14:59:38 +02002603 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002604 if (!peers->sync_task)
2605 return 0;
2606
Emeric Brunb3971ab2015-05-12 18:49:09 +02002607 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002608 peers->sync_task->context = (void *)peers;
2609 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2610 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002611 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002612}
2613
2614
2615
2616/*
2617 * Function used to register a table for sync on a group of peers
2618 *
2619 */
2620void peers_register_table(struct peers *peers, struct stktable *table)
2621{
2622 struct shared_table *st;
2623 struct peer * curpeer;
2624 int id = 0;
2625
2626 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002627 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002628 st->table = table;
2629 st->next = curpeer->tables;
2630 if (curpeer->tables)
2631 id = curpeer->tables->local_id;
2632 st->local_id = id + 1;
2633
2634 curpeer->tables = st;
2635 }
2636
2637 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002638}
2639