blob: 04cd0460ad5f680a43e382fd48ef352ebe178c98 [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écaille523cc9e2016-10-12 17:30:30 +020085#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 +020086
Emeric Brunb3971ab2015-05-12 18:49:09 +020087#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 +020088#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
89
Emeric Brunb3971ab2015-05-12 18:49:09 +020090/*****************************/
91/* Sync message class */
92/*****************************/
93enum {
94 PEER_MSG_CLASS_CONTROL = 0,
95 PEER_MSG_CLASS_ERROR,
96 PEER_MSG_CLASS_STICKTABLE = 10,
97 PEER_MSG_CLASS_RESERVED = 255,
98};
99
100/*****************************/
101/* control message types */
102/*****************************/
103enum {
104 PEER_MSG_CTRL_RESYNCREQ = 0,
105 PEER_MSG_CTRL_RESYNCFINISHED,
106 PEER_MSG_CTRL_RESYNCPARTIAL,
107 PEER_MSG_CTRL_RESYNCCONFIRM,
108};
109
110/*****************************/
111/* error message types */
112/*****************************/
113enum {
114 PEER_MSG_ERR_PROTOCOL = 0,
115 PEER_MSG_ERR_SIZELIMIT,
116};
117
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100118/*
119 * Parameters used by functions to build peer protocol messages. */
120struct peer_prep_params {
121 struct {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100122 struct peer *peer;
123 } hello;
124 struct {
125 unsigned int st1;
126 } error_status;
127 struct {
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100128 struct stksess *stksess;
129 struct shared_table *shared_table;
130 unsigned int updateid;
131 int use_identifier;
132 int use_timed;
133 } updt;
134 struct {
135 struct shared_table *shared_table;
136 } swtch;
137 struct {
138 struct shared_table *shared_table;
139 } ack;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100140 struct {
141 unsigned char head[2];
142 } control;
143 struct {
144 unsigned char head[2];
145 } error;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100146};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200147
148/*******************************/
149/* stick table sync mesg types */
150/* Note: ids >= 128 contains */
151/* id message cotains data */
152/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200153#define PEER_MSG_STKT_UPDATE 0x80
154#define PEER_MSG_STKT_INCUPDATE 0x81
155#define PEER_MSG_STKT_DEFINE 0x82
156#define PEER_MSG_STKT_SWITCH 0x83
157#define PEER_MSG_STKT_ACK 0x84
158#define PEER_MSG_STKT_UPDATE_TIMED 0x85
159#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200160
161/**********************************/
162/* Peer Session IO handler states */
163/**********************************/
164
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100165enum {
166 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
167 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
168 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
169 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100170 /* after this point, data were possibly exchanged */
171 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
172 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
173 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
174 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
175 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200176 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
177 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100178 PEER_SESS_ST_END, /* Killed session */
179};
Emeric Brun2b920a12010-09-23 18:30:22 +0200180
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100181/***************************************************/
182/* Peer Session status code - part of the protocol */
183/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200184
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100185#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
186#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200187
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100188#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200189
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100190#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200191
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100192#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
193#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
194#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
195#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200196
197#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200198#define PEER_MAJOR_VER 2
199#define PEER_MINOR_VER 1
200#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200201
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200202struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100203static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200204
Emeric Brun18928af2017-03-29 16:32:53 +0200205/* This function encode an uint64 to 'dynamic' length format.
206 The encoded value is written at address *str, and the
207 caller must assure that size after *str is large enought.
208 At return, the *str is set at the next Byte after then
209 encoded integer. The function returns then length of the
210 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200211int intencode(uint64_t i, char **str) {
212 int idx = 0;
213 unsigned char *msg;
214
215 if (!*str)
216 return 0;
217
218 msg = (unsigned char *)*str;
219 if (i < 240) {
220 msg[0] = (unsigned char)i;
221 *str = (char *)&msg[idx+1];
222 return (idx+1);
223 }
224
225 msg[idx] =(unsigned char)i | 240;
226 i = (i - 240) >> 4;
227 while (i >= 128) {
228 msg[++idx] = (unsigned char)i | 128;
229 i = (i - 128) >> 7;
230 }
231 msg[++idx] = (unsigned char)i;
232 *str = (char *)&msg[idx+1];
233 return (idx+1);
234}
235
236
237/* This function returns the decoded integer or 0
238 if decode failed
239 *str point on the beginning of the integer to decode
240 at the end of decoding *str point on the end of the
241 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200242uint64_t intdecode(char **str, char *end)
243{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200244 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200245 uint64_t i;
246 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200247
248 if (!*str)
249 return 0;
250
251 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200252 if (msg >= (unsigned char *)end)
253 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200254
Emeric Brun18928af2017-03-29 16:32:53 +0200255 i = *(msg++);
256 if (i >= 240) {
257 shift = 4;
258 do {
259 if (msg >= (unsigned char *)end)
260 goto fail;
261 i += (uint64_t)*msg << shift;
262 shift += 7;
263 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200264 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100265 *str = (char *)msg;
266 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200267
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100268 fail:
269 *str = NULL;
270 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200271}
Emeric Brun2b920a12010-09-23 18:30:22 +0200272
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100273/*
274 * Build a "hello" peer protocol message.
275 * Return the number of written bytes written to build this messages if succeeded,
276 * 0 if not.
277 */
278static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
279{
280 int min_ver, ret;
281 struct peer *peer;
282
283 peer = p->hello.peer;
284 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
285 /* Prepare headers */
286 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
287 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
288 if (ret >= size)
289 return 0;
290
291 return ret;
292}
293
294/*
295 * Build a "handshake succeeded" status message.
296 * Return the number of written bytes written to build this messages if succeeded,
297 * 0 if not.
298 */
299static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
300{
301 int ret;
302
303 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
304 if (ret >= size)
305 return 0;
306
307 return ret;
308}
309
310/*
311 * Build an error status message.
312 * Return the number of written bytes written to build this messages if succeeded,
313 * 0 if not.
314 */
315static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
316{
317 int ret;
318 unsigned int st1;
319
320 st1 = p->error_status.st1;
321 ret = snprintf(msg, size, "%d\n", st1);
322 if (ret >= size)
323 return 0;
324
325 return ret;
326}
327
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200328/* Set the stick-table UPDATE message type byte at <msg_type> address,
329 * depending on <use_identifier> and <use_timed> boolean parameters.
330 * Always successful.
331 */
332static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
333{
334 if (use_timed) {
335 if (use_identifier)
336 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
337 else
338 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
339 }
340 else {
341 if (use_identifier)
342 *msg_type = PEER_MSG_STKT_UPDATE;
343 else
344 *msg_type = PEER_MSG_STKT_INCUPDATE;
345 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200346}
Emeric Brun2b920a12010-09-23 18:30:22 +0200347/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200348 * This prepare the data update message on the stick session <ts>, <st> is the considered
349 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800350 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200351 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
352 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200353 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100354static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200355{
356 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200357 unsigned short datalen;
358 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200359 unsigned int data_type;
360 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100361 struct stksess *ts;
362 struct shared_table *st;
363 unsigned int updateid;
364 int use_identifier;
365 int use_timed;
366
367 ts = p->updt.stksess;
368 st = p->updt.shared_table;
369 updateid = p->updt.updateid;
370 use_identifier = p->updt.use_identifier;
371 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200372
373 cursor = datamsg = msg + 1 + 5;
374
Emeric Brun2b920a12010-09-23 18:30:22 +0200375 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200376
377 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200378 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200379 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200380 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200381
382 /* encode update identifier if needed */
383 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200384 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200385 memcpy(cursor, &netinteger, sizeof(netinteger));
386 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200387 }
388
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200389 if (use_timed) {
390 netinteger = htonl(tick_remain(now_ms, ts->expire));
391 memcpy(cursor, &netinteger, sizeof(netinteger));
392 cursor += sizeof(netinteger);
393 }
394
Emeric Brunb3971ab2015-05-12 18:49:09 +0200395 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200396 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200397 int stlen = strlen((char *)ts->key.key);
398
Emeric Brunb3971ab2015-05-12 18:49:09 +0200399 intencode(stlen, &cursor);
400 memcpy(cursor, ts->key.key, stlen);
401 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200402 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200403 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200404 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200405 memcpy(cursor, &netinteger, sizeof(netinteger));
406 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200407 }
408 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200409 memcpy(cursor, ts->key.key, st->table->key_size);
410 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200411 }
412
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100413 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200414 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200415 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200416
Emeric Brun94900952015-06-11 18:25:54 +0200417 data_ptr = stktable_data_ptr(st->table, ts, data_type);
418 if (data_ptr) {
419 switch (stktable_data_types[data_type].std_type) {
420 case STD_T_SINT: {
421 int data;
422
423 data = stktable_data_cast(data_ptr, std_t_sint);
424 intencode(data, &cursor);
425 break;
426 }
427 case STD_T_UINT: {
428 unsigned int data;
429
430 data = stktable_data_cast(data_ptr, std_t_uint);
431 intencode(data, &cursor);
432 break;
433 }
434 case STD_T_ULL: {
435 unsigned long long data;
436
437 data = stktable_data_cast(data_ptr, std_t_ull);
438 intencode(data, &cursor);
439 break;
440 }
441 case STD_T_FRQP: {
442 struct freq_ctr_period *frqp;
443
444 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
445 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
446 intencode(frqp->curr_ctr, &cursor);
447 intencode(frqp->prev_ctr, &cursor);
448 break;
449 }
450 }
451 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200452 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100453 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200454
455 /* Compute datalen */
456 datalen = (cursor - datamsg);
457
458 /* prepare message header */
459 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200460 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200461 cursor = &msg[2];
462 intencode(datalen, &cursor);
463
464 /* move data after header */
465 memmove(cursor, datamsg, datalen);
466
467 /* return header size + data_len */
468 return (cursor - msg) + datalen;
469}
470
471/*
472 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800473 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200474 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
475 * check size)
476 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100477static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200478{
479 int len;
480 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200481 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200482 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200483 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200484 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100485 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200486
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100487 st = params->swtch.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200488 cursor = datamsg = msg + 2 + 5;
489
490 /* Encode data */
491
492 /* encode local id */
493 intencode(st->local_id, &cursor);
494
495 /* encode table name */
496 len = strlen(st->table->id);
497 intencode(len, &cursor);
498 memcpy(cursor, st->table->id, len);
499 cursor += len;
500
501 /* encode table type */
502
503 intencode(st->table->type, &cursor);
504
505 /* encode table key size */
506 intencode(st->table->key_size, &cursor);
507
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200508 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200510 /* encode available known data types in table */
511 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
512 if (st->table->data_ofs[data_type]) {
513 switch (stktable_data_types[data_type].std_type) {
514 case STD_T_SINT:
515 case STD_T_UINT:
516 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200517 data |= 1 << data_type;
518 break;
Emeric Brun94900952015-06-11 18:25:54 +0200519 case STD_T_FRQP:
520 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200521 intencode(data_type, &chunkq);
522 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200523 break;
524 }
525 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 }
527 intencode(data, &cursor);
528
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200529 /* Encode stick-table entries duration. */
530 intencode(st->table->expire, &cursor);
531
532 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200533 chunk->data = chunkq - chunkp;
534 memcpy(cursor, chunk->area, chunk->data);
535 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200536 }
537
Emeric Brunb3971ab2015-05-12 18:49:09 +0200538 /* Compute datalen */
539 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200540
Emeric Brunb3971ab2015-05-12 18:49:09 +0200541 /* prepare message header */
542 msg[0] = PEER_MSG_CLASS_STICKTABLE;
543 msg[1] = PEER_MSG_STKT_DEFINE;
544 cursor = &msg[2];
545 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200546
Emeric Brunb3971ab2015-05-12 18:49:09 +0200547 /* move data after header */
548 memmove(cursor, datamsg, datalen);
549
550 /* return header size + data_len */
551 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200552}
553
Emeric Brunb3971ab2015-05-12 18:49:09 +0200554/*
555 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
556 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800557 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200558 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
559 * check size)
560 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100561static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200562{
563 unsigned short datalen;
564 char *cursor, *datamsg;
565 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100566 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200567
Emeric Brunb058f1c2015-09-22 15:50:18 +0200568 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200569
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100570 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200571 intencode(st->remote_id, &cursor);
572 netinteger = htonl(st->last_get);
573 memcpy(cursor, &netinteger, sizeof(netinteger));
574 cursor += sizeof(netinteger);
575
576 /* Compute datalen */
577 datalen = (cursor - datamsg);
578
579 /* prepare message header */
580 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200581 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200582 cursor = &msg[2];
583 intencode(datalen, &cursor);
584
585 /* move data after header */
586 memmove(cursor, datamsg, datalen);
587
588 /* return header size + data_len */
589 return (cursor - msg) + datalen;
590}
Emeric Brun2b920a12010-09-23 18:30:22 +0200591
592/*
593 * Callback to release a session with a peer
594 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200595static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200596{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200597 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200598 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200599 struct peer *peer = appctx->ctx.peers.ptr;
600 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200601
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100602 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100603 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200604 return;
605
606 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200607 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100608 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
609 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100610 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100611 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100612 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200613 /* Re-init current table pointers to force announcement on re-connect */
614 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200615 peer->appctx = NULL;
616 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200617 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200618 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
619 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200620
621 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200622 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200623 }
624 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200625 peer->flags &= PEER_TEACH_RESET;
626 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200627 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100628 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200629 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200630 }
631}
632
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200633/* Retrieve the major and minor versions of peers protocol
634 * announced by a remote peer. <str> is a null-terminated
635 * string with the following format: "<maj_ver>.<min_ver>".
636 */
637static int peer_get_version(const char *str,
638 unsigned int *maj_ver, unsigned int *min_ver)
639{
640 unsigned int majv, minv;
641 const char *pos, *saved;
642 const char *end;
643
644 saved = pos = str;
645 end = str + strlen(str);
646
647 majv = read_uint(&pos, end);
648 if (saved == pos || *pos++ != '.')
649 return -1;
650
651 saved = pos;
652 minv = read_uint(&pos, end);
653 if (saved == pos || pos != end)
654 return -1;
655
656 *maj_ver = majv;
657 *min_ver = minv;
658
659 return 0;
660}
Emeric Brun2b920a12010-09-23 18:30:22 +0200661
662/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100663 * Parse a line terminated by an optional '\r' character, followed by a mandatory
664 * '\n' character.
665 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
666 * a line could not be read because the communication channel is closed.
667 */
668static inline int peer_getline(struct appctx *appctx)
669{
670 int n;
671 struct stream_interface *si = appctx->owner;
672
673 n = co_getline(si_oc(si), trash.area, trash.size);
674 if (!n)
675 return 0;
676
677 if (n < 0 || trash.area[n - 1] != '\n') {
678 appctx->st0 = PEER_SESS_ST_END;
679 return -1;
680 }
681
682 if (n > 1 && (trash.area[n - 2] == '\r'))
683 trash.area[n - 2] = 0;
684 else
685 trash.area[n - 1] = 0;
686
687 co_skip(si_oc(si), n);
688
689 return n;
690}
691
692/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100693 * Send a message after having called <peer_prepare_msg> to build it.
694 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
695 * Returns -1 if there was not enough room left to send the message,
696 * any other negative returned value must be considered as an error with an appcxt st0
697 * returned value equal to PEER_SESS_ST_END.
698 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100699static inline int peer_send_msg(struct appctx *appctx,
700 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
701 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100702{
703 int ret, msglen;
704 struct stream_interface *si = appctx->owner;
705
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100706 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100707 if (!msglen) {
708 /* internal error: message does not fit in trash */
709 appctx->st0 = PEER_SESS_ST_END;
710 return 0;
711 }
712
713 /* message to buffer */
714 ret = ci_putblk(si_ic(si), trash.area, msglen);
715 if (ret <= 0) {
716 if (ret == -1) {
717 /* No more write possible */
718 si_rx_room_blk(si);
719 return -1;
720 }
721 appctx->st0 = PEER_SESS_ST_END;
722 }
723
724 return ret;
725}
726
727/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100728 * Send a hello message.
729 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
730 * Returns -1 if there was not enough room left to send the message,
731 * any other negative returned value must be considered as an error with an appcxt st0
732 * returned value equal to PEER_SESS_ST_END.
733 */
734static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
735{
736 struct peer_prep_params p = {
737 .hello.peer = peer,
738 };
739
740 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
741}
742
743/*
744 * Send a success peer handshake status message.
745 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
746 * Returns -1 if there was not enough room left to send the message,
747 * any other negative returned value must be considered as an error with an appcxt st0
748 * returned value equal to PEER_SESS_ST_END.
749 */
750static inline int peer_send_status_successmsg(struct appctx *appctx)
751{
752 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
753}
754
755/*
756 * Send a peer handshake status error message.
757 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
758 * Returns -1 if there was not enough room left to send the message,
759 * any other negative returned value must be considered as an error with an appcxt st0
760 * returned value equal to PEER_SESS_ST_END.
761 */
762static inline int peer_send_status_errormsg(struct appctx *appctx)
763{
764 struct peer_prep_params p = {
765 .error_status.st1 = appctx->st1,
766 };
767
768 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
769}
770
771/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100772 * Send a stick-table switch message.
773 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
774 * Returns -1 if there was not enough room left to send the message,
775 * any other negative returned value must be considered as an error with an appcxt st0
776 * returned value equal to PEER_SESS_ST_END.
777 */
778static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
779{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100780 struct peer_prep_params p = {
781 .swtch.shared_table = st,
782 };
783
784 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100785}
786
787/*
788 * Send a stick-table update acknowledgement message.
789 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
790 * Returns -1 if there was not enough room left to send the message,
791 * any other negative returned value must be considered as an error with an appcxt st0
792 * returned value equal to PEER_SESS_ST_END.
793 */
794static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
795{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100796 struct peer_prep_params p = {
797 .ack.shared_table = st,
798 };
799
800 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100801}
802
803/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100804 * Send a stick-table update message.
805 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
806 * Returns -1 if there was not enough room left to send the message,
807 * any other negative returned value must be considered as an error with an appcxt st0
808 * returned value equal to PEER_SESS_ST_END.
809 */
810static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
811 unsigned int updateid, int use_identifier, int use_timed)
812{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100813 struct peer_prep_params p = {
814 .updt.stksess = ts,
815 .updt.shared_table = st,
816 .updt.updateid = updateid,
817 .updt.use_identifier = use_identifier,
818 .updt.use_timed = use_timed,
819 };
820
821 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100822}
823
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100824/*
825 * Build a peer protocol control class message.
826 * Returns the number of written bytes used to build the message if succeeded,
827 * 0 if not.
828 */
829static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
830{
831 if (size < sizeof p->control.head)
832 return 0;
833
834 msg[0] = p->control.head[0];
835 msg[1] = p->control.head[1];
836
837 return 2;
838}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100839
840/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100841 * Send a stick-table synchronization request message.
842 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
843 * Returns -1 if there was not enough room left to send the message,
844 * any other negative returned value must be considered as an error with an appctx st0
845 * returned value equal to PEER_SESS_ST_END.
846 */
847static inline int peer_send_resync_reqmsg(struct appctx *appctx)
848{
849 struct peer_prep_params p = {
850 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
851 };
852
853 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
854}
855
856/*
857 * Send a stick-table synchronization confirmation message.
858 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
859 * Returns -1 if there was not enough room left to send the message,
860 * any other negative returned value must be considered as an error with an appctx st0
861 * returned value equal to PEER_SESS_ST_END.
862 */
863static inline int peer_send_resync_confirmsg(struct appctx *appctx)
864{
865 struct peer_prep_params p = {
866 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
867 };
868
869 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
870}
871
872/*
873 * Send a stick-table synchronization finished message.
874 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
875 * Returns -1 if there was not enough room left to send the message,
876 * any other negative returned value must be considered as an error with an appctx st0
877 * returned value equal to PEER_SESS_ST_END.
878 */
879static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
880{
881 struct peer_prep_params p = {
882 .control.head = { PEER_MSG_CLASS_CONTROL, },
883 };
884
885 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
886 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
887
888 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
889}
890
891/*
892 * Build a peer protocol error class message.
893 * Returns the number of written bytes used to build the message if succeeded,
894 * 0 if not.
895 */
896static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
897{
898 if (size < sizeof p->error.head)
899 return 0;
900
901 msg[0] = p->error.head[0];
902 msg[1] = p->error.head[1];
903
904 return 2;
905}
906
907/*
908 * Send a "size limit reached" error message.
909 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
910 * Returns -1 if there was not enough room left to send the message,
911 * any other negative returned value must be considered as an error with an appctx st0
912 * returned value equal to PEER_SESS_ST_END.
913 */
914static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
915{
916 struct peer_prep_params p = {
917 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
918 };
919
920 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
921}
922
923/*
924 * Send a "peer protocol" error message.
925 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
926 * Returns -1 if there was not enough room left to send the message,
927 * any other negative returned value must be considered as an error with an appctx st0
928 * returned value equal to PEER_SESS_ST_END.
929 */
930static inline int peer_send_error_protomsg(struct appctx *appctx)
931{
932 struct peer_prep_params p = {
933 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
934 };
935
936 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
937}
938
939/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100940 * Function used to lookup for recent stick-table updates associated with
941 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
942 */
943static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
944{
945 struct eb32_node *eb;
946
947 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
948 if (!eb) {
949 eb = eb32_first(&st->table->updates);
950 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
951 st->table->commitupdate = st->last_pushed = st->table->localupdate;
952 return NULL;
953 }
954 }
955
956 if ((int)(eb->key - st->table->localupdate) > 0) {
957 st->table->commitupdate = st->last_pushed = st->table->localupdate;
958 return NULL;
959 }
960
961 return eb32_entry(eb, struct stksess, upd);
962}
963
964/*
965 * Function used to lookup for recent stick-table updates associated with
966 * <st> shared stick-table during teach state 1 step.
967 */
968static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
969{
970 struct eb32_node *eb;
971
972 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
973 if (!eb) {
974 st->flags |= SHTABLE_F_TEACH_STAGE1;
975 eb = eb32_first(&st->table->updates);
976 if (eb)
977 st->last_pushed = eb->key - 1;
978 return NULL;
979 }
980
981 return eb32_entry(eb, struct stksess, upd);
982}
983
984/*
985 * Function used to lookup for recent stick-table updates associated with
986 * <st> shared stick-table during teach state 2 step.
987 */
988static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
989{
990 struct eb32_node *eb;
991
992 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
993 if (!eb || eb->key > st->teaching_origin) {
994 st->flags |= SHTABLE_F_TEACH_STAGE2;
995 return NULL;
996 }
997
998 return eb32_entry(eb, struct stksess, upd);
999}
1000
1001/*
1002 * Generic function to emit update messages for <st> stick-table when a lesson must
1003 * be taught to the peer <p>.
1004 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1005 * this function, 0 if not.
1006 *
1007 * This function temporary unlock/lock <st> when it sends stick-table updates or
1008 * when decrementing its refcount in case of any error when it sends this updates.
1009 *
1010 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1011 * Returns -1 if there was not enough room left to send the message,
1012 * any other negative returned value must be considered as an error with an appcxt st0
1013 * returned value equal to PEER_SESS_ST_END.
1014 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1015 * unlocked if not already locked when entering this function.
1016 */
1017static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1018 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1019 struct shared_table *st, int locked)
1020{
1021 int ret, new_pushed, use_timed;
1022
1023 ret = 1;
1024 use_timed = 0;
1025 if (st != p->last_local_table) {
1026 ret = peer_send_switchmsg(st, appctx);
1027 if (ret <= 0)
1028 return ret;
1029
1030 p->last_local_table = st;
1031 }
1032
1033 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1034 use_timed = !(p->flags & PEER_F_DWNGRD);
1035
1036 /* We force new pushed to 1 to force identifier in update message */
1037 new_pushed = 1;
1038
1039 if (!locked)
1040 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1041
1042 while (1) {
1043 struct stksess *ts;
1044 unsigned updateid;
1045
1046 /* push local updates */
1047 ts = peer_stksess_lookup(st);
1048 if (!ts)
1049 break;
1050
1051 updateid = ts->upd.key;
1052 ts->ref_cnt++;
1053 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1054
1055 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1056 if (ret <= 0) {
1057 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1058 ts->ref_cnt--;
1059 if (!locked)
1060 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1061 return ret;
1062 }
1063
1064 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1065 ts->ref_cnt--;
1066 st->last_pushed = updateid;
1067
1068 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1069 (int)(st->last_pushed - st->table->commitupdate) > 0)
1070 st->table->commitupdate = st->last_pushed;
1071
1072 /* identifier may not needed in next update message */
1073 new_pushed = 0;
1074 }
1075
1076 out:
1077 if (!locked)
1078 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1079 return 1;
1080}
1081
1082/*
1083 * Function to emit update messages for <st> stick-table when a lesson must
1084 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1085 *
1086 * Note that <st> shared stick-table is locked when calling this function.
1087 *
1088 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1089 * Returns -1 if there was not enough room left to send the message,
1090 * any other negative returned value must be considered as an error with an appcxt st0
1091 * returned value equal to PEER_SESS_ST_END.
1092 */
1093static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1094 struct shared_table *st)
1095{
1096 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1097}
1098
1099/*
1100 * Function to emit update messages for <st> stick-table when a lesson must
1101 * be taught to the peer <p> during teach state 1 step.
1102 *
1103 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1104 * Returns -1 if there was not enough room left to send the message,
1105 * any other negative returned value must be considered as an error with an appcxt st0
1106 * returned value equal to PEER_SESS_ST_END.
1107 */
1108static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1109 struct shared_table *st)
1110{
1111 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1112}
1113
1114/*
1115 * Function to emit update messages for <st> stick-table when a lesson must
1116 * be taught to the peer <p> during teach state 1 step.
1117 *
1118 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1119 * Returns -1 if there was not enough room left to send the message,
1120 * any other negative returned value must be considered as an error with an appcxt st0
1121 * returned value equal to PEER_SESS_ST_END.
1122 */
1123static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1124 struct shared_table *st)
1125{
1126 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1127}
1128
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001129
1130/*
1131 * Function used to parse a stick-table update message after it has been received
1132 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1133 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1134 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1135 * was encountered.
1136 * <exp> must be set if the stick-table entry expires.
1137 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1138 * messages, in this case the stick-table udpate message is received with a stick-table
1139 * update ID.
1140 * <totl> is the length of the stick-table update message computed upon receipt.
1141 */
1142static int peer_recv_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1143 char **msg_cur, char *msg_end, int msg_len, int totl)
1144{
1145 struct stream_interface *si = appctx->owner;
1146 struct shared_table *st = p->remote_table;
1147 struct stksess *ts, *newts;
1148 uint32_t update;
1149 int expire;
1150 unsigned int data_type;
1151 void *data_ptr;
1152
1153 /* Here we have data message */
1154 if (!st)
1155 goto ignore_msg;
1156
1157 expire = MS_TO_TICKS(st->table->expire);
1158
1159 if (updt) {
1160 if (msg_len < sizeof(update)) {
1161 /* malformed message */
1162 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1163 return 0;
1164 }
1165 memcpy(&update, *msg_cur, sizeof(update));
1166 *msg_cur += sizeof(update);
1167 st->last_get = htonl(update);
1168 }
1169 else {
1170 st->last_get++;
1171 }
1172
1173 if (exp) {
1174 size_t expire_sz = sizeof expire;
1175
1176 if (*msg_cur + expire_sz > msg_end) {
1177 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1178 return 0;
1179 }
1180 memcpy(&expire, *msg_cur, expire_sz);
1181 *msg_cur += expire_sz;
1182 expire = ntohl(expire);
1183 }
1184
1185 newts = stksess_new(st->table, NULL);
1186 if (!newts)
1187 goto ignore_msg;
1188
1189 if (st->table->type == SMP_T_STR) {
1190 unsigned int to_read, to_store;
1191
1192 to_read = intdecode(msg_cur, msg_end);
1193 if (!*msg_cur) {
1194 /* malformed message */
1195 stksess_free(st->table, newts);
1196 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1197 return 0;
1198 }
1199 to_store = MIN(to_read, st->table->key_size - 1);
1200 if (*msg_cur + to_store > msg_end) {
1201 /* malformed message */
1202 stksess_free(st->table, newts);
1203 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1204 return 0;
1205 }
1206
1207 memcpy(newts->key.key, *msg_cur, to_store);
1208 newts->key.key[to_store] = 0;
1209 *msg_cur += to_read;
1210 }
1211 else if (st->table->type == SMP_T_SINT) {
1212 unsigned int netinteger;
1213
1214 if (*msg_cur + sizeof(netinteger) > msg_end) {
1215 /* malformed message */
1216 stksess_free(st->table, newts);
1217 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1218 return 0;
1219 }
1220 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1221 netinteger = ntohl(netinteger);
1222 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1223 *msg_cur += sizeof(netinteger);
1224 }
1225 else {
1226 if (*msg_cur + st->table->key_size > msg_end) {
1227 /* malformed message */
1228 stksess_free(st->table, newts);
1229 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1230 return 0;
1231 }
1232 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1233 *msg_cur += st->table->key_size;
1234 }
1235
1236 /* lookup for existing entry */
1237 ts = stktable_set_entry(st->table, newts);
1238 if (ts != newts) {
1239 stksess_free(st->table, newts);
1240 newts = NULL;
1241 }
1242
1243 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1244
1245 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
1246
1247 if (!((1 << data_type) & st->remote_data))
1248 continue;
1249
1250 switch (stktable_data_types[data_type].std_type) {
1251 case STD_T_SINT: {
1252 int data;
1253
1254 data = intdecode(msg_cur, msg_end);
1255 if (!*msg_cur) {
1256 /* malformed message */
1257 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1258 stktable_touch_remote(st->table, ts, 1);
1259 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1260 return 0;
1261 }
1262
1263 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1264 if (data_ptr)
1265 stktable_data_cast(data_ptr, std_t_sint) = data;
1266 break;
1267 }
1268 case STD_T_UINT: {
1269 unsigned int data;
1270
1271 data = intdecode(msg_cur, msg_end);
1272 if (!*msg_cur) {
1273 /* malformed message */
1274 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1275 stktable_touch_remote(st->table, ts, 1);
1276 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1277 return 0;
1278 }
1279
1280 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1281 if (data_ptr)
1282 stktable_data_cast(data_ptr, std_t_uint) = data;
1283 break;
1284 }
1285 case STD_T_ULL: {
1286 unsigned long long data;
1287
1288 data = intdecode(msg_cur, msg_end);
1289 if (!*msg_cur) {
1290 /* malformed message */
1291 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1292 stktable_touch_remote(st->table, ts, 1);
1293 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1294 return 0;
1295 }
1296
1297 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1298 if (data_ptr)
1299 stktable_data_cast(data_ptr, std_t_ull) = data;
1300 break;
1301 }
1302 case STD_T_FRQP: {
1303 struct freq_ctr_period data;
1304
1305 /* First bit is reserved for the freq_ctr_period lock
1306 Note: here we're still protected by the stksess lock
1307 so we don't need to update the update the freq_ctr_period
1308 using its internal lock */
1309
1310 data.curr_tick = tick_add(now_ms, -intdecode(msg_cur, msg_end)) & ~0x1;
1311 if (!*msg_cur) {
1312 /* malformed message */
1313 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1314 stktable_touch_remote(st->table, ts, 1);
1315 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1316 return 0;
1317 }
1318 data.curr_ctr = intdecode(msg_cur, msg_end);
1319 if (!*msg_cur) {
1320 /* malformed message */
1321 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1322 stktable_touch_remote(st->table, ts, 1);
1323 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1324 return 0;
1325 }
1326 data.prev_ctr = intdecode(msg_cur, msg_end);
1327 if (!*msg_cur) {
1328 /* malformed message */
1329 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1330 stktable_touch_remote(st->table, ts, 1);
1331 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1332 return 0;
1333 }
1334
1335 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1336 if (data_ptr)
1337 stktable_data_cast(data_ptr, std_t_frqp) = data;
1338 break;
1339 }
1340 }
1341 }
1342 /* Force new expiration */
1343 ts->expire = tick_add(now_ms, expire);
1344
1345 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1346 stktable_touch_remote(st->table, ts, 1);
1347 return 1;
1348
1349 ignore_msg:
1350 /* skip consumed message */
1351 co_skip(si_oc(si), totl);
1352 return 0;
1353}
1354
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001355/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001356 * Function used to parse a stick-table update acknowledgement message after it
1357 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1358 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1359 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1360 * was encountered.
1361 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1362 */
1363static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1364 char **msg_cur, char *msg_end)
1365{
1366 /* ack message */
1367 uint32_t table_id ;
1368 uint32_t update;
1369 struct shared_table *st;
1370
1371 table_id = intdecode(msg_cur, msg_end);
1372 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1373 /* malformed message */
1374 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1375 return 0;
1376 }
1377
1378 memcpy(&update, *msg_cur, sizeof(update));
1379 update = ntohl(update);
1380
1381 for (st = p->tables; st; st = st->next) {
1382 if (st->local_id == table_id) {
1383 st->update = update;
1384 break;
1385 }
1386 }
1387
1388 return 1;
1389}
1390
1391/*
1392 * Function used to parse a stick-table switch message after it has been received
1393 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1394 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1395 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1396 * was encountered.
1397 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1398 */
1399static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1400 char **msg_cur, char *msg_end)
1401{
1402 struct shared_table *st;
1403 int table_id;
1404
1405 table_id = intdecode(msg_cur, msg_end);
1406 if (!*msg_cur) {
1407 /* malformed message */
1408 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1409 return 0;
1410 }
1411
1412 p->remote_table = NULL;
1413 for (st = p->tables; st; st = st->next) {
1414 if (st->remote_id == table_id) {
1415 p->remote_table = st;
1416 break;
1417 }
1418 }
1419
1420 return 1;
1421}
1422
1423/*
1424 * Function used to parse a stick-table definition message after it has been received
1425 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1426 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1427 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1428 * was encountered.
1429 * <totl> is the length of the stick-table update message computed upon receipt.
1430 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1431 */
1432static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1433 char **msg_cur, char *msg_end, int totl)
1434{
1435 struct stream_interface *si = appctx->owner;
1436 int table_id_len;
1437 struct shared_table *st;
1438 int table_type;
1439 int table_keylen;
1440 int table_id;
1441 uint64_t table_data;
1442
1443 table_id = intdecode(msg_cur, msg_end);
1444 if (!*msg_cur) {
1445 /* malformed message */
1446 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1447 return 0;
1448 }
1449
1450 table_id_len = intdecode(msg_cur, msg_end);
1451 if (!*msg_cur) {
1452 /* malformed message */
1453 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1454 return 0;
1455 }
1456
1457 p->remote_table = NULL;
1458 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1459 /* malformed message */
1460 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1461 return 0;
1462 }
1463
1464 for (st = p->tables; st; st = st->next) {
1465 /* Reset IDs */
1466 if (st->remote_id == table_id)
1467 st->remote_id = 0;
1468
1469 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1470 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1471 p->remote_table = st;
1472 }
1473
1474 if (!p->remote_table)
1475 goto ignore_msg;
1476
1477 *msg_cur += table_id_len;
1478 if (*msg_cur >= msg_end) {
1479 /* malformed message */
1480 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1481 return 0;
1482 }
1483
1484 table_type = intdecode(msg_cur, msg_end);
1485 if (!*msg_cur) {
1486 /* malformed message */
1487 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1488 return 0;
1489 }
1490
1491 table_keylen = intdecode(msg_cur, msg_end);
1492 if (!*msg_cur) {
1493 /* malformed message */
1494 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1495 return 0;
1496 }
1497
1498 table_data = intdecode(msg_cur, msg_end);
1499 if (!*msg_cur) {
1500 /* malformed message */
1501 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1502 return 0;
1503 }
1504
1505 if (p->remote_table->table->type != table_type
1506 || p->remote_table->table->key_size != table_keylen) {
1507 p->remote_table = NULL;
1508 goto ignore_msg;
1509 }
1510
1511 p->remote_table->remote_data = table_data;
1512 p->remote_table->remote_id = table_id;
1513 return 1;
1514
1515 ignore_msg:
1516 co_skip(si_oc(si), totl);
1517 return 0;
1518}
1519
1520/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001521 * Receive a stick-table message.
1522 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1523 * -1 if there was an error updating the appctx state st0 accordingly.
1524 */
1525static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1526 uint32_t *msg_len, int *totl)
1527{
1528 int reql;
1529 struct stream_interface *si = appctx->owner;
1530
1531 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1532 if (reql <= 0) /* closed or EOL not found */
1533 goto incomplete;
1534
1535 *totl += reql;
1536
1537 if ((unsigned int)msg_head[1] < 128)
1538 return 1;
1539
1540 /* Read and Decode message length */
1541 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1542 if (reql <= 0) /* closed */
1543 goto incomplete;
1544
1545 *totl += reql;
1546
1547 if ((unsigned int)msg_head[2] < 240) {
1548 *msg_len = msg_head[2];
1549 }
1550 else {
1551 int i;
1552 char *cur;
1553 char *end;
1554
1555 for (i = 3 ; i < msg_head_sz ; i++) {
1556 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1557 if (reql <= 0) /* closed */
1558 goto incomplete;
1559
1560 *totl += reql;
1561
1562 if (!(msg_head[i] & 0x80))
1563 break;
1564 }
1565
1566 if (i == msg_head_sz) {
1567 /* malformed message */
1568 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1569 return -1;
1570 }
1571 end = msg_head + msg_head_sz;
1572 cur = &msg_head[2];
1573 *msg_len = intdecode(&cur, end);
1574 if (!cur) {
1575 /* malformed message */
1576 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1577 return -1;
1578 }
1579 }
1580
1581 /* Read message content */
1582 if (*msg_len) {
1583 if (*msg_len > trash.size) {
1584 /* Status code is not success, abort */
1585 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1586 return -1;
1587 }
1588
1589 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1590 if (reql <= 0) /* closed */
1591 goto incomplete;
1592 *totl += reql;
1593 }
1594
1595 return 1;
1596
1597 incomplete:
1598 if (reql < 0) {
1599 /* there was an error */
1600 appctx->st0 = PEER_SESS_ST_END;
1601 return -1;
1602 }
1603
1604 return 0;
1605}
1606/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001607 * IO Handler to handle message exchance with a peer
1608 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001609static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001610{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001611 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02001612 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001613 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02001614 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001615 int reql = 0;
1616 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001617 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
1618 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001619 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02001620
Joseph Herlant82b2f542018-11-15 12:19:14 -08001621 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001622 if (si_ic(si)->buf.size == 0) {
1623 si_rx_room_blk(si);
1624 goto out;
1625 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001626
Emeric Brun2b920a12010-09-23 18:30:22 +02001627 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001628 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001629switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001630 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001631 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001632 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001633 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001634 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001635 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001636 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001637 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001638 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001639
Frédéric Lécaillece025572019-01-21 13:38:06 +01001640 reql = peer_getline(appctx);
1641 if (!reql)
1642 goto out;
1643
1644 if (reql < 0)
1645 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001646
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001647 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001648 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001649 appctx->st0 = PEER_SESS_ST_EXIT;
1650 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1651 goto switchstate;
1652 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001653 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001654 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001655 appctx->st0 = PEER_SESS_ST_EXIT;
1656 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001657 goto switchstate;
1658 }
1659
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001660 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001661 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001662 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001663 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001664
Frédéric Lécaillece025572019-01-21 13:38:06 +01001665 reql = peer_getline(appctx);
1666 if (!reql)
1667 goto out;
1668
1669 if (reql < 0)
1670 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001671
1672 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001673 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001674 appctx->st0 = PEER_SESS_ST_EXIT;
1675 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001676 goto switchstate;
1677 }
1678
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001679 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02001680 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001681 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +02001682 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001683
1684 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001685
Frédéric Lécaillece025572019-01-21 13:38:06 +01001686 reql = peer_getline(appctx);
1687 if (!reql)
1688 goto out;
1689
1690 if (reql < 0)
1691 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001692
Emeric Brunb3971ab2015-05-12 18:49:09 +02001693 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001694 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +02001695 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001696 appctx->st0 = PEER_SESS_ST_EXIT;
1697 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001698 goto switchstate;
1699 }
1700 *p = 0;
1701
1702 /* lookup known peer */
1703 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001704 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +02001705 break;
1706 }
1707
1708 /* if unknown peer */
1709 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001710 appctx->st0 = PEER_SESS_ST_EXIT;
1711 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02001712 goto switchstate;
1713 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001714
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001715 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001716 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001717 if (curpeer->local) {
1718 /* Local connection, reply a retry */
1719 appctx->st0 = PEER_SESS_ST_EXIT;
1720 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
1721 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001722 }
Emeric Brun80527f52017-06-19 17:46:37 +02001723
1724 /* we're killing a connection, we must apply a random delay before
1725 * retrying otherwise the other end will do the same and we can loop
1726 * for a while.
1727 */
1728 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001729 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001730 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001731 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
1732 if (min_ver == PEER_DWNGRD_MINOR_VER) {
1733 curpeer->flags |= PEER_F_DWNGRD;
1734 }
1735 else {
1736 curpeer->flags &= ~PEER_F_DWNGRD;
1737 }
1738 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001739 curpeer->appctx = appctx;
1740 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001741 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +01001742 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001743 /* fall through */
1744 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001745 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001746 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001747
Willy Tarreau2d372c22018-11-05 17:12:27 +01001748 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001749 if (!curpeer) {
1750 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001751 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001752 if (curpeer->appctx != appctx) {
1753 appctx->st0 = PEER_SESS_ST_END;
1754 goto switchstate;
1755 }
1756 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001757
1758 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001759 if (repl <= 0) {
1760 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001761 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001762 goto switchstate;
1763 }
1764
1765 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001766 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001767
1768 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001769 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001770
1771 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001772 curpeer->confirm = 0;
1773
1774 /* Init cursors */
1775 for (st = curpeer->tables; st ; st = st->next) {
1776 st->last_get = st->last_acked = 0;
1777 st->teaching_origin = st->last_pushed = st->update;
1778 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001779
1780 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001781 curpeer->flags &= PEER_TEACH_RESET;
1782 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001783
1784 /* if current peer is local */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001785 if (curpeer->local) {
1786 /* if current host need resyncfrom local and no process assined */
1787 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1788 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1789 /* assign local peer for a lesson, consider lesson already requested */
1790 curpeer->flags |= PEER_F_LEARN_ASSIGN;
1791 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1792 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001793
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001794 }
1795 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1796 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1797 /* assign peer for a lesson */
1798 curpeer->flags |= PEER_F_LEARN_ASSIGN;
1799 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
1800 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001801
1802
Emeric Brun2b920a12010-09-23 18:30:22 +02001803 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +01001804 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001805 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02001806 goto switchstate;
1807 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001808 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001809 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001810 if (!curpeer) {
1811 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001812 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001813 if (curpeer->appctx != appctx) {
1814 appctx->st0 = PEER_SESS_ST_END;
1815 goto switchstate;
1816 }
1817 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001818
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001819 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02001820 if (repl <= 0) {
1821 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001822 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001823 goto switchstate;
1824 }
1825
1826 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001827 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02001828 /* fall through */
1829 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001830 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001831 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001832
Willy Tarreau2d372c22018-11-05 17:12:27 +01001833 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001834 if (!curpeer) {
1835 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001836 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001837 if (curpeer->appctx != appctx) {
1838 appctx->st0 = PEER_SESS_ST_END;
1839 goto switchstate;
1840 }
1841 }
1842
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001843 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001844 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001845
Frédéric Lécaillece025572019-01-21 13:38:06 +01001846 reql = peer_getline(appctx);
1847 if (!reql)
1848 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001849
Frédéric Lécaillece025572019-01-21 13:38:06 +01001850 if (reql < 0)
1851 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001852
1853 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001854 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02001855
1856 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001857 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001858
1859 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001860 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001861 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001862 for (st = curpeer->tables; st ; st = st->next) {
1863 st->last_get = st->last_acked = 0;
1864 st->teaching_origin = st->last_pushed = st->update;
1865 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001866
1867 /* Init confirm counter */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001868 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001869
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001870 /* reset teaching and learning flags to 0 */
1871 curpeer->flags &= PEER_TEACH_RESET;
1872 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001873
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001874 /* If current peer is local */
1875 if (curpeer->local) {
1876 /* flag to start to teach lesson */
1877 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +02001878
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001879 }
1880 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1881 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1882 /* If peer is remote and resync from remote is needed,
1883 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +02001884
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001885 /* assign peer for a lesson */
1886 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001887 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001888 }
1889
1890 }
1891 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001892 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
1893 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02001894 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001895 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001896 goto switchstate;
1897 }
Willy Tarreau2d372c22018-11-05 17:12:27 +01001898 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001899 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02001900 /* fall through */
1901 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001902 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001903 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001904 char *msg_cur = trash.area;
1905 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001906 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02001907 int totl = 0;
1908
Willy Tarreau2d372c22018-11-05 17:12:27 +01001909 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001910 if (!curpeer) {
1911 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001912 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001913 if (curpeer->appctx != appctx) {
1914 appctx->st0 = PEER_SESS_ST_END;
1915 goto switchstate;
1916 }
1917 }
1918
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001919 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
1920 if (reql <= 0) {
1921 if (reql == -1)
1922 goto switchstate;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001923 goto incomplete;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001924 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01001925
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001926 msg_end += msg_len;
1927
Emeric Brunb3971ab2015-05-12 18:49:09 +02001928 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1929 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1930 struct shared_table *st;
1931 /* Reset message: remote need resync */
1932
1933 /* prepare tables fot a global push */
1934 for (st = curpeer->tables; st; st = st->next) {
1935 st->teaching_origin = st->last_pushed = st->table->update;
1936 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001937 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001938
Emeric Brunb3971ab2015-05-12 18:49:09 +02001939 /* reset teaching flags to 0 */
1940 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001941
Emeric Brunb3971ab2015-05-12 18:49:09 +02001942 /* flag to start to teach lesson */
1943 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001944 }
1945 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001946 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1947 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001948 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1949 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001950 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001951 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001952 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001953 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001954 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1955 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001956 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001957
1958 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001959 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1960 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001961 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001962 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001963 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001964 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001965 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001966
1967 /* If stopping state */
1968 if (stopping) {
1969 /* Close session, push resync no more needed */
1970 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1971 appctx->st0 = PEER_SESS_ST_END;
1972 goto switchstate;
1973 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001974 for (st = curpeer->tables; st; st = st->next) {
1975 st->update = st->last_pushed = st->teaching_origin;
1976 st->flags = 0;
1977 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001978
1979 /* reset teaching flags to 0 */
1980 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001981 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001982 }
1983 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1984 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001985 if (!peer_treat_definemsg(appctx, curpeer, &msg_cur, msg_end, totl))
1986 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001987 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001988 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001989 if (!peer_treat_switchmsg(appctx, curpeer, &msg_cur, msg_end))
Emeric Brunb3971ab2015-05-12 18:49:09 +02001990 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001991 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001992 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001993 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1994 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1995 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001996 int update, expire;
Emeric Brun2b920a12010-09-23 18:30:22 +02001997
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001998 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1999 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
2000 if (!peer_recv_updatemsg(appctx, curpeer, update, expire,
2001 &msg_cur, msg_end, msg_len, totl))
2002 goto switchstate;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002003
Emeric Brunb3971ab2015-05-12 18:49:09 +02002004 }
2005 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01002006 if (!peer_treat_ackmsg(appctx, curpeer, &msg_cur, msg_end))
Emeric Brunb3971ab2015-05-12 18:49:09 +02002007 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002008 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002009 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002010 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
2011 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02002012 goto switchstate;
2013 }
2014
Emeric Brunb3971ab2015-05-12 18:49:09 +02002015ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02002016 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002017 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002018 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002019 goto switchstate;
2020
Emeric Brun2b920a12010-09-23 18:30:22 +02002021incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02002022 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02002023
Willy Tarreau72d6c162013-04-11 16:14:13 +02002024 if (reql < 0) {
2025 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002026 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02002027 goto switchstate;
2028 }
2029
Emeric Brun2b920a12010-09-23 18:30:22 +02002030
Emeric Brun2b920a12010-09-23 18:30:22 +02002031
Emeric Brunb3971ab2015-05-12 18:49:09 +02002032
Emeric Brun2b920a12010-09-23 18:30:22 +02002033 /* Need to request a resync */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01002034 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
2035 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
2036 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002037
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002038 repl = peer_send_resync_reqmsg(appctx);
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01002039 if (repl <= 0) {
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01002040 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002041 goto out;
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01002042 goto switchstate;
2043 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002044
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01002045 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
2046 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002047
Emeric Brunb3971ab2015-05-12 18:49:09 +02002048 /* Nothing to read, now we start to write */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002049 if (curpeer->tables) {
2050 struct shared_table *st;
2051 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02002052
Emeric Brunb3971ab2015-05-12 18:49:09 +02002053 last_local_table = curpeer->last_local_table;
2054 if (!last_local_table)
2055 last_local_table = curpeer->tables;
2056 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02002057
Emeric Brunb3971ab2015-05-12 18:49:09 +02002058 while (1) {
2059 if (!st)
2060 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02002061
Emeric Brunb3971ab2015-05-12 18:49:09 +02002062 /* It remains some updates to ack */
2063 if (st->last_get != st->last_acked) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01002064 repl = peer_send_ackmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002065 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01002066 if (repl == -1)
2067 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002068 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002069 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002070 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02002071 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002072
Emeric Brunb3971ab2015-05-12 18:49:09 +02002073 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002074 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002075 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
2076 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002077
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01002078 repl = peer_send_teach_process_msgs(appctx, curpeer, st);
2079 if (repl <= 0) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002080 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01002081 if (repl == -1)
2082 goto out;
2083 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002084 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002085 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002086 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002087 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002088 else {
2089 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01002090 repl = peer_send_teach_stage1_msgs(appctx, curpeer, st);
2091 if (repl <= 0) {
2092 if (repl == -1)
2093 goto out;
2094 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002095 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002096 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002097
Emeric Brunb3971ab2015-05-12 18:49:09 +02002098 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01002099 repl = peer_send_teach_stage2_msgs(appctx, curpeer, st);
2100 if (repl <= 0) {
2101 if (repl == -1)
2102 goto out;
2103 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002104 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002105 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002106 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002107
2108 if (st == last_local_table)
2109 break;
2110 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02002111 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002112 }
2113
2114
2115 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002116 repl = peer_send_resync_finishedmsg(appctx, curpeer);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002117 if (repl <= 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002118 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002119 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002120 goto switchstate;
2121 }
2122 /* flag finished message sent */
2123 curpeer->flags |= PEER_F_TEACH_FINISHED;
2124 }
2125
Emeric Brun597b26e2016-08-12 11:23:31 +02002126 /* Confirm finished or partial messages */
2127 while (curpeer->confirm) {
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002128 repl = peer_send_resync_confirmsg(appctx);
Emeric Brun597b26e2016-08-12 11:23:31 +02002129 if (repl <= 0) {
Emeric Brun597b26e2016-08-12 11:23:31 +02002130 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002131 goto out;
Emeric Brun597b26e2016-08-12 11:23:31 +02002132 goto switchstate;
2133 }
2134 curpeer->confirm--;
2135 }
2136
Emeric Brun2b920a12010-09-23 18:30:22 +02002137 /* noting more to do */
2138 goto out;
2139 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002140 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002141 if (prev_state == PEER_SESS_ST_WAITMSG)
2142 HA_ATOMIC_SUB(&connected_peers, 1);
2143 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002144 if (peer_send_status_errormsg(appctx) == -1)
2145 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002146 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002147 goto switchstate;
2148 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002149 if (prev_state == PEER_SESS_ST_WAITMSG)
2150 HA_ATOMIC_SUB(&connected_peers, 1);
2151 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002152 if (peer_send_error_size_limitmsg(appctx) == -1)
2153 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002154 appctx->st0 = PEER_SESS_ST_END;
2155 goto switchstate;
2156 }
2157 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002158 if (prev_state == PEER_SESS_ST_WAITMSG)
2159 HA_ATOMIC_SUB(&connected_peers, 1);
2160 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002161 if (peer_send_error_protomsg(appctx) == -1)
2162 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002163 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002164 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002165 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002166 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002167 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002168 if (prev_state == PEER_SESS_ST_WAITMSG)
2169 HA_ATOMIC_SUB(&connected_peers, 1);
2170 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002171 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002172 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002173 curpeer = NULL;
2174 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002175 si_shutw(si);
2176 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002177 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002178 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002179 }
2180 }
2181 }
2182out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002183 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002184
2185 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002186 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002187 return;
2188}
2189
Willy Tarreau30576452015-04-13 13:50:30 +02002190static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002191 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002192 .name = "<PEER>", /* used for logging */
2193 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002194 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002195};
Emeric Brun2b920a12010-09-23 18:30:22 +02002196
2197/*
2198 * Use this function to force a close of a peer session
2199 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002200static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002201{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002202 /* Note that the peer sessions which have just been created
2203 * (->st0 == PEER_SESS_ST_CONNECT) must not
2204 * be shutdown, if not, the TCP session will never be closed
2205 * and stay in CLOSE_WAIT state after having been closed by
2206 * the remote side.
2207 */
2208 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002209 return;
2210
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002211 if (appctx->applet != &peer_applet)
2212 return;
2213
Willy Tarreau2d372c22018-11-05 17:12:27 +01002214 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
2215 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002216 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002217 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002218}
2219
Willy Tarreau91d96282015-03-13 15:47:26 +01002220/* Pre-configures a peers frontend to accept incoming connections */
2221void peers_setup_frontend(struct proxy *fe)
2222{
2223 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002224 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002225 fe->maxconn = 0;
2226 fe->conn_retries = CONN_RETRIES;
2227 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002228 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002229 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002230 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002231 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002232}
2233
Emeric Brun2b920a12010-09-23 18:30:22 +02002234/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002235 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002236 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002237static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002238{
Willy Tarreau04b92862017-09-15 11:01:04 +02002239 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002240 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002241 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002242 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002243 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002244 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002245
Emeric Brunb3971ab2015-05-12 18:49:09 +02002246 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
2247 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002248 s = NULL;
2249
Emeric Brun1138fd02017-06-19 12:38:55 +02002250 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002251 if (!appctx)
2252 goto out_close;
2253
2254 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002255 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002256
Willy Tarreau04b92862017-09-15 11:01:04 +02002257 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002258 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002259 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002260 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002261 }
2262
Willy Tarreau87787ac2017-08-28 16:22:54 +02002263 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002264 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002265 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002266 }
2267
Willy Tarreau342bfb12015-04-05 01:35:34 +02002268 /* The tasks below are normally what is supposed to be done by
2269 * fe->accept().
2270 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002271 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002272
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002273 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002274 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002275 appctx_wakeup(appctx);
2276
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002277 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002278 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002279 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002280
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002281 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002282 * pre-initialized connection in si->conn.
2283 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002284 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002285 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002286
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002287 if (unlikely((cs = cs_new(conn)) == NULL))
2288 goto out_free_conn;
2289
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002290 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002291 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2292
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002293 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01002294 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002295 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002296
Emeric Brun2b920a12010-09-23 18:30:22 +02002297 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002298 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002299
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002300 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002301
Emeric Brunb3971ab2015-05-12 18:49:09 +02002302 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002303 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01002304 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002305 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002306
2307 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002308 out_free_conn:
2309 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002310 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002311 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002312 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002313 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002314 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002315 out_free_appctx:
2316 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002317 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002318 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002319}
2320
2321/*
2322 * Task processing function to manage re-connect and peer session
2323 * tasks wakeup on local update.
2324 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002325static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002326{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002327 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002328 struct peer *ps;
2329 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002330
2331 task->expire = TICK_ETERNITY;
2332
Emeric Brunb3971ab2015-05-12 18:49:09 +02002333 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002334 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002335 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002336 task_delete(peers->sync_task);
2337 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002338 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002339 return NULL;
2340 }
2341
Emeric Brun80527f52017-06-19 17:46:37 +02002342 /* Acquire lock for all peers of the section */
2343 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002344 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002345
Emeric Brun2b920a12010-09-23 18:30:22 +02002346 if (!stopping) {
2347 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002348
2349 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2350 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2351 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002352 /* Resync from local peer needed
2353 no peer was assigned for the lesson
2354 and no old local peer found
2355 or resync timeout expire */
2356
2357 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002358 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002359
2360 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002361 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002362 }
2363
2364 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002365 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002366 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002367 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002368 if (!ps->appctx) {
2369 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002370 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002371 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002372 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002373 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002374 tick_is_expired(ps->reconnect, now_ms))) {
2375 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002376 * or previous peer connection established with success
2377 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002378 * and reconnection timer is expired */
2379
2380 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002381 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002382 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002383 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002384 /* If previous session failed during connection
2385 * but reconnection timer is not expired */
2386
2387 /* reschedule task for reconnect */
2388 task->expire = tick_first(task->expire, ps->reconnect);
2389 }
2390 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002391 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002392 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002393 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002394 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2395 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002396 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2397 /* Resync from a remote is needed
2398 * and no peer was assigned for lesson
2399 * and current peer may be up2date */
2400
2401 /* assign peer for the lesson */
2402 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002403 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002404
Willy Tarreau9df94c22016-10-31 18:42:52 +01002405 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002406 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002407 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002408 else {
2409 /* Awake session if there is data to push */
2410 for (st = ps->tables; st ; st = st->next) {
2411 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002412 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002413 appctx_wakeup(ps->appctx);
2414 break;
2415 }
2416 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002417 }
2418 /* else do nothing */
2419 } /* SUCCESSCODE */
2420 } /* !ps->peer->local */
2421 } /* for */
2422
2423 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002424 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2425 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2426 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002427 /* Resync from remote peer needed
2428 * no peer was assigned for the lesson
2429 * and resync timeout expire */
2430
2431 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002432 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002433 }
2434
Emeric Brunb3971ab2015-05-12 18:49:09 +02002435 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002436 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002437 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2438 if (!tick_is_expired(peers->resync_timeout, now_ms))
2439 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002440 }
2441 } /* !stopping */
2442 else {
2443 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002444 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002445 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002446 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002447 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002448 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002449 peers->flags |= PEERS_F_DONOTSTOP;
2450 ps = peers->local;
2451 for (st = ps->tables; st ; st = st->next)
2452 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002453 }
2454
2455 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002456 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002457 /* we're killing a connection, we must apply a random delay before
2458 * retrying otherwise the other end will do the same and we can loop
2459 * for a while.
2460 */
2461 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002462 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002463 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002464 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002465 }
2466 }
2467 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002468
Emeric Brunb3971ab2015-05-12 18:49:09 +02002469 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002470 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002471 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002472 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002473 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002474 peers->flags &= ~PEERS_F_DONOTSTOP;
2475 for (st = ps->tables; st ; st = st->next)
2476 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002477 }
2478 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002479 else if (!ps->appctx) {
2480 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002481 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002482 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2483 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2484 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002485 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002486 * or previous peer connection was successfully established
2487 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002488 * or during previous connect, peer replies a try again statuscode */
2489
2490 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002491 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002492 }
2493 else {
2494 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002495 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002496 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002497 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002498 peers->flags &= ~PEERS_F_DONOTSTOP;
2499 for (st = ps->tables; st ; st = st->next)
2500 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002501 }
2502 }
2503 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002504 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002505 /* current peer connection is active and established
2506 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002507 for (st = ps->tables; st ; st = st->next) {
2508 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002509 appctx_wakeup(ps->appctx);
2510 break;
2511 }
2512 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002513 }
2514 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002515
2516 /* Release lock for all peers of the section */
2517 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002518 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002519
Emeric Brun2b920a12010-09-23 18:30:22 +02002520 /* Wakeup for re-connect */
2521 return task;
2522}
2523
Emeric Brunb3971ab2015-05-12 18:49:09 +02002524
Emeric Brun2b920a12010-09-23 18:30:22 +02002525/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002526 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002527 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002528int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002529{
Emeric Brun2b920a12010-09-23 18:30:22 +02002530 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002531 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002532
Emeric Brun2b920a12010-09-23 18:30:22 +02002533 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002534 peers->peers_fe->maxconn += 3;
2535 }
2536
Willy Tarreau4348fad2012-09-20 16:48:07 +02002537 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2538 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002539 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002540 if (!peers->sync_task)
2541 return 0;
2542
Emeric Brunb3971ab2015-05-12 18:49:09 +02002543 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002544 peers->sync_task->context = (void *)peers;
2545 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2546 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002547 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002548}
2549
2550
2551
2552/*
2553 * Function used to register a table for sync on a group of peers
2554 *
2555 */
2556void peers_register_table(struct peers *peers, struct stktable *table)
2557{
2558 struct shared_table *st;
2559 struct peer * curpeer;
2560 int id = 0;
2561
2562 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002563 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002564 st->table = table;
2565 st->next = curpeer->tables;
2566 if (curpeer->tables)
2567 id = curpeer->tables->local_id;
2568 st->local_id = id + 1;
2569
2570 curpeer->tables = st;
2571 }
2572
2573 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002574}
2575