blob: bbd9dfd7eef86a43dc2426791aa5674b7bff1855 [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écaille3f0fb9d2019-01-25 08:30:29 +0100202size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200203struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100204static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200205
Emeric Brun18928af2017-03-29 16:32:53 +0200206/* This function encode an uint64 to 'dynamic' length format.
207 The encoded value is written at address *str, and the
208 caller must assure that size after *str is large enought.
209 At return, the *str is set at the next Byte after then
210 encoded integer. The function returns then length of the
211 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200212int intencode(uint64_t i, char **str) {
213 int idx = 0;
214 unsigned char *msg;
215
216 if (!*str)
217 return 0;
218
219 msg = (unsigned char *)*str;
220 if (i < 240) {
221 msg[0] = (unsigned char)i;
222 *str = (char *)&msg[idx+1];
223 return (idx+1);
224 }
225
226 msg[idx] =(unsigned char)i | 240;
227 i = (i - 240) >> 4;
228 while (i >= 128) {
229 msg[++idx] = (unsigned char)i | 128;
230 i = (i - 128) >> 7;
231 }
232 msg[++idx] = (unsigned char)i;
233 *str = (char *)&msg[idx+1];
234 return (idx+1);
235}
236
237
238/* This function returns the decoded integer or 0
239 if decode failed
240 *str point on the beginning of the integer to decode
241 at the end of decoding *str point on the end of the
242 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200243uint64_t intdecode(char **str, char *end)
244{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200245 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200246 uint64_t i;
247 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200248
249 if (!*str)
250 return 0;
251
252 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200253 if (msg >= (unsigned char *)end)
254 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200255
Emeric Brun18928af2017-03-29 16:32:53 +0200256 i = *(msg++);
257 if (i >= 240) {
258 shift = 4;
259 do {
260 if (msg >= (unsigned char *)end)
261 goto fail;
262 i += (uint64_t)*msg << shift;
263 shift += 7;
264 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200265 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100266 *str = (char *)msg;
267 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200268
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100269 fail:
270 *str = NULL;
271 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200272}
Emeric Brun2b920a12010-09-23 18:30:22 +0200273
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100274/*
275 * Build a "hello" peer protocol message.
276 * Return the number of written bytes written to build this messages if succeeded,
277 * 0 if not.
278 */
279static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p)
280{
281 int min_ver, ret;
282 struct peer *peer;
283
284 peer = p->hello.peer;
285 min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
286 /* Prepare headers */
287 ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
288 PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid);
289 if (ret >= size)
290 return 0;
291
292 return ret;
293}
294
295/*
296 * Build a "handshake succeeded" status message.
297 * Return the number of written bytes written to build this messages if succeeded,
298 * 0 if not.
299 */
300static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p)
301{
302 int ret;
303
304 ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE);
305 if (ret >= size)
306 return 0;
307
308 return ret;
309}
310
311/*
312 * Build an error status message.
313 * Return the number of written bytes written to build this messages if succeeded,
314 * 0 if not.
315 */
316static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p)
317{
318 int ret;
319 unsigned int st1;
320
321 st1 = p->error_status.st1;
322 ret = snprintf(msg, size, "%d\n", st1);
323 if (ret >= size)
324 return 0;
325
326 return ret;
327}
328
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200329/* Set the stick-table UPDATE message type byte at <msg_type> address,
330 * depending on <use_identifier> and <use_timed> boolean parameters.
331 * Always successful.
332 */
333static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
334{
335 if (use_timed) {
336 if (use_identifier)
337 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
338 else
339 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
340 }
341 else {
342 if (use_identifier)
343 *msg_type = PEER_MSG_STKT_UPDATE;
344 else
345 *msg_type = PEER_MSG_STKT_INCUPDATE;
346 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200347}
Emeric Brun2b920a12010-09-23 18:30:22 +0200348/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200349 * This prepare the data update message on the stick session <ts>, <st> is the considered
350 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800351 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200352 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
353 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200354 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100355static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200356{
357 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200358 unsigned short datalen;
359 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200360 unsigned int data_type;
361 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100362 struct stksess *ts;
363 struct shared_table *st;
364 unsigned int updateid;
365 int use_identifier;
366 int use_timed;
367
368 ts = p->updt.stksess;
369 st = p->updt.shared_table;
370 updateid = p->updt.updateid;
371 use_identifier = p->updt.use_identifier;
372 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200373
374 cursor = datamsg = msg + 1 + 5;
375
Emeric Brun2b920a12010-09-23 18:30:22 +0200376 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200377
378 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200379 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200380 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200381 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200382
383 /* encode update identifier if needed */
384 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200385 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200386 memcpy(cursor, &netinteger, sizeof(netinteger));
387 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200388 }
389
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200390 if (use_timed) {
391 netinteger = htonl(tick_remain(now_ms, ts->expire));
392 memcpy(cursor, &netinteger, sizeof(netinteger));
393 cursor += sizeof(netinteger);
394 }
395
Emeric Brunb3971ab2015-05-12 18:49:09 +0200396 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200397 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200398 int stlen = strlen((char *)ts->key.key);
399
Emeric Brunb3971ab2015-05-12 18:49:09 +0200400 intencode(stlen, &cursor);
401 memcpy(cursor, ts->key.key, stlen);
402 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200403 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200404 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200405 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200406 memcpy(cursor, &netinteger, sizeof(netinteger));
407 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200408 }
409 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200410 memcpy(cursor, ts->key.key, st->table->key_size);
411 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200412 }
413
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100414 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200415 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200416 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200417
Emeric Brun94900952015-06-11 18:25:54 +0200418 data_ptr = stktable_data_ptr(st->table, ts, data_type);
419 if (data_ptr) {
420 switch (stktable_data_types[data_type].std_type) {
421 case STD_T_SINT: {
422 int data;
423
424 data = stktable_data_cast(data_ptr, std_t_sint);
425 intencode(data, &cursor);
426 break;
427 }
428 case STD_T_UINT: {
429 unsigned int data;
430
431 data = stktable_data_cast(data_ptr, std_t_uint);
432 intencode(data, &cursor);
433 break;
434 }
435 case STD_T_ULL: {
436 unsigned long long data;
437
438 data = stktable_data_cast(data_ptr, std_t_ull);
439 intencode(data, &cursor);
440 break;
441 }
442 case STD_T_FRQP: {
443 struct freq_ctr_period *frqp;
444
445 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
446 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
447 intencode(frqp->curr_ctr, &cursor);
448 intencode(frqp->prev_ctr, &cursor);
449 break;
450 }
451 }
452 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200453 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100454 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200455
456 /* Compute datalen */
457 datalen = (cursor - datamsg);
458
459 /* prepare message header */
460 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200461 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200462 cursor = &msg[2];
463 intencode(datalen, &cursor);
464
465 /* move data after header */
466 memmove(cursor, datamsg, datalen);
467
468 /* return header size + data_len */
469 return (cursor - msg) + datalen;
470}
471
472/*
473 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800474 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200475 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
476 * check size)
477 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100478static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200479{
480 int len;
481 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200482 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200483 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200484 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200485 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100486 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200487
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100488 st = params->swtch.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200489 cursor = datamsg = msg + 2 + 5;
490
491 /* Encode data */
492
493 /* encode local id */
494 intencode(st->local_id, &cursor);
495
496 /* encode table name */
497 len = strlen(st->table->id);
498 intencode(len, &cursor);
499 memcpy(cursor, st->table->id, len);
500 cursor += len;
501
502 /* encode table type */
503
504 intencode(st->table->type, &cursor);
505
506 /* encode table key size */
507 intencode(st->table->key_size, &cursor);
508
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200509 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200510 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200511 /* encode available known data types in table */
512 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
513 if (st->table->data_ofs[data_type]) {
514 switch (stktable_data_types[data_type].std_type) {
515 case STD_T_SINT:
516 case STD_T_UINT:
517 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200518 data |= 1 << data_type;
519 break;
Emeric Brun94900952015-06-11 18:25:54 +0200520 case STD_T_FRQP:
521 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200522 intencode(data_type, &chunkq);
523 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200524 break;
525 }
526 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200527 }
528 intencode(data, &cursor);
529
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200530 /* Encode stick-table entries duration. */
531 intencode(st->table->expire, &cursor);
532
533 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200534 chunk->data = chunkq - chunkp;
535 memcpy(cursor, chunk->area, chunk->data);
536 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200537 }
538
Emeric Brunb3971ab2015-05-12 18:49:09 +0200539 /* Compute datalen */
540 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200541
Emeric Brunb3971ab2015-05-12 18:49:09 +0200542 /* prepare message header */
543 msg[0] = PEER_MSG_CLASS_STICKTABLE;
544 msg[1] = PEER_MSG_STKT_DEFINE;
545 cursor = &msg[2];
546 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200547
Emeric Brunb3971ab2015-05-12 18:49:09 +0200548 /* move data after header */
549 memmove(cursor, datamsg, datalen);
550
551 /* return header size + data_len */
552 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200553}
554
Emeric Brunb3971ab2015-05-12 18:49:09 +0200555/*
556 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
557 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800558 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200559 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
560 * check size)
561 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100562static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200563{
564 unsigned short datalen;
565 char *cursor, *datamsg;
566 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100567 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200568
Emeric Brunb058f1c2015-09-22 15:50:18 +0200569 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200570
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100571 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200572 intencode(st->remote_id, &cursor);
573 netinteger = htonl(st->last_get);
574 memcpy(cursor, &netinteger, sizeof(netinteger));
575 cursor += sizeof(netinteger);
576
577 /* Compute datalen */
578 datalen = (cursor - datamsg);
579
580 /* prepare message header */
581 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200582 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200583 cursor = &msg[2];
584 intencode(datalen, &cursor);
585
586 /* move data after header */
587 memmove(cursor, datamsg, datalen);
588
589 /* return header size + data_len */
590 return (cursor - msg) + datalen;
591}
Emeric Brun2b920a12010-09-23 18:30:22 +0200592
593/*
594 * Callback to release a session with a peer
595 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200596static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200597{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200598 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200599 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200600 struct peer *peer = appctx->ctx.peers.ptr;
601 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200602
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100603 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100604 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200605 return;
606
607 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200608 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100609 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
610 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100611 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100612 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100613 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200614 /* Re-init current table pointers to force announcement on re-connect */
615 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200616 peer->appctx = NULL;
617 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200618 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200619 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
620 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200621
622 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200623 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200624 }
625 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200626 peer->flags &= PEER_TEACH_RESET;
627 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200628 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100629 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200630 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200631 }
632}
633
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200634/* Retrieve the major and minor versions of peers protocol
635 * announced by a remote peer. <str> is a null-terminated
636 * string with the following format: "<maj_ver>.<min_ver>".
637 */
638static int peer_get_version(const char *str,
639 unsigned int *maj_ver, unsigned int *min_ver)
640{
641 unsigned int majv, minv;
642 const char *pos, *saved;
643 const char *end;
644
645 saved = pos = str;
646 end = str + strlen(str);
647
648 majv = read_uint(&pos, end);
649 if (saved == pos || *pos++ != '.')
650 return -1;
651
652 saved = pos;
653 minv = read_uint(&pos, end);
654 if (saved == pos || pos != end)
655 return -1;
656
657 *maj_ver = majv;
658 *min_ver = minv;
659
660 return 0;
661}
Emeric Brun2b920a12010-09-23 18:30:22 +0200662
663/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100664 * Parse a line terminated by an optional '\r' character, followed by a mandatory
665 * '\n' character.
666 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
667 * a line could not be read because the communication channel is closed.
668 */
669static inline int peer_getline(struct appctx *appctx)
670{
671 int n;
672 struct stream_interface *si = appctx->owner;
673
674 n = co_getline(si_oc(si), trash.area, trash.size);
675 if (!n)
676 return 0;
677
678 if (n < 0 || trash.area[n - 1] != '\n') {
679 appctx->st0 = PEER_SESS_ST_END;
680 return -1;
681 }
682
683 if (n > 1 && (trash.area[n - 2] == '\r'))
684 trash.area[n - 2] = 0;
685 else
686 trash.area[n - 1] = 0;
687
688 co_skip(si_oc(si), n);
689
690 return n;
691}
692
693/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100694 * Send a message after having called <peer_prepare_msg> to build it.
695 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
696 * Returns -1 if there was not enough room left to send the message,
697 * any other negative returned value must be considered as an error with an appcxt st0
698 * returned value equal to PEER_SESS_ST_END.
699 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100700static inline int peer_send_msg(struct appctx *appctx,
701 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
702 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100703{
704 int ret, msglen;
705 struct stream_interface *si = appctx->owner;
706
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100707 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100708 if (!msglen) {
709 /* internal error: message does not fit in trash */
710 appctx->st0 = PEER_SESS_ST_END;
711 return 0;
712 }
713
714 /* message to buffer */
715 ret = ci_putblk(si_ic(si), trash.area, msglen);
716 if (ret <= 0) {
717 if (ret == -1) {
718 /* No more write possible */
719 si_rx_room_blk(si);
720 return -1;
721 }
722 appctx->st0 = PEER_SESS_ST_END;
723 }
724
725 return ret;
726}
727
728/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100729 * Send a hello message.
730 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
731 * Returns -1 if there was not enough room left to send the message,
732 * any other negative returned value must be considered as an error with an appcxt st0
733 * returned value equal to PEER_SESS_ST_END.
734 */
735static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer)
736{
737 struct peer_prep_params p = {
738 .hello.peer = peer,
739 };
740
741 return peer_send_msg(appctx, peer_prepare_hellomsg, &p);
742}
743
744/*
745 * Send a success peer handshake status message.
746 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
747 * Returns -1 if there was not enough room left to send the message,
748 * any other negative returned value must be considered as an error with an appcxt st0
749 * returned value equal to PEER_SESS_ST_END.
750 */
751static inline int peer_send_status_successmsg(struct appctx *appctx)
752{
753 return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL);
754}
755
756/*
757 * Send a peer handshake status error message.
758 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
759 * Returns -1 if there was not enough room left to send the message,
760 * any other negative returned value must be considered as an error with an appcxt st0
761 * returned value equal to PEER_SESS_ST_END.
762 */
763static inline int peer_send_status_errormsg(struct appctx *appctx)
764{
765 struct peer_prep_params p = {
766 .error_status.st1 = appctx->st1,
767 };
768
769 return peer_send_msg(appctx, peer_prepare_status_errormsg, &p);
770}
771
772/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100773 * Send a stick-table switch message.
774 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
775 * Returns -1 if there was not enough room left to send the message,
776 * any other negative returned value must be considered as an error with an appcxt st0
777 * returned value equal to PEER_SESS_ST_END.
778 */
779static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
780{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100781 struct peer_prep_params p = {
782 .swtch.shared_table = st,
783 };
784
785 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100786}
787
788/*
789 * Send a stick-table update acknowledgement message.
790 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
791 * Returns -1 if there was not enough room left to send the message,
792 * any other negative returned value must be considered as an error with an appcxt st0
793 * returned value equal to PEER_SESS_ST_END.
794 */
795static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
796{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100797 struct peer_prep_params p = {
798 .ack.shared_table = st,
799 };
800
801 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100802}
803
804/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100805 * Send a stick-table update message.
806 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
807 * Returns -1 if there was not enough room left to send the message,
808 * any other negative returned value must be considered as an error with an appcxt st0
809 * returned value equal to PEER_SESS_ST_END.
810 */
811static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
812 unsigned int updateid, int use_identifier, int use_timed)
813{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100814 struct peer_prep_params p = {
815 .updt.stksess = ts,
816 .updt.shared_table = st,
817 .updt.updateid = updateid,
818 .updt.use_identifier = use_identifier,
819 .updt.use_timed = use_timed,
820 };
821
822 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100823}
824
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100825/*
826 * Build a peer protocol control class message.
827 * Returns the number of written bytes used to build the message if succeeded,
828 * 0 if not.
829 */
830static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p)
831{
832 if (size < sizeof p->control.head)
833 return 0;
834
835 msg[0] = p->control.head[0];
836 msg[1] = p->control.head[1];
837
838 return 2;
839}
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100840
841/*
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +0100842 * Send a stick-table synchronization request message.
843 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
844 * Returns -1 if there was not enough room left to send the message,
845 * any other negative returned value must be considered as an error with an appctx st0
846 * returned value equal to PEER_SESS_ST_END.
847 */
848static inline int peer_send_resync_reqmsg(struct appctx *appctx)
849{
850 struct peer_prep_params p = {
851 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, },
852 };
853
854 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
855}
856
857/*
858 * Send a stick-table synchronization confirmation message.
859 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
860 * Returns -1 if there was not enough room left to send the message,
861 * any other negative returned value must be considered as an error with an appctx st0
862 * returned value equal to PEER_SESS_ST_END.
863 */
864static inline int peer_send_resync_confirmsg(struct appctx *appctx)
865{
866 struct peer_prep_params p = {
867 .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, },
868 };
869
870 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
871}
872
873/*
874 * Send a stick-table synchronization finished message.
875 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
876 * Returns -1 if there was not enough room left to send the message,
877 * any other negative returned value must be considered as an error with an appctx st0
878 * returned value equal to PEER_SESS_ST_END.
879 */
880static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peer *peer)
881{
882 struct peer_prep_params p = {
883 .control.head = { PEER_MSG_CLASS_CONTROL, },
884 };
885
886 p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ?
887 PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
888
889 return peer_send_msg(appctx, peer_prepare_control_msg, &p);
890}
891
892/*
893 * Build a peer protocol error class message.
894 * Returns the number of written bytes used to build the message if succeeded,
895 * 0 if not.
896 */
897static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p)
898{
899 if (size < sizeof p->error.head)
900 return 0;
901
902 msg[0] = p->error.head[0];
903 msg[1] = p->error.head[1];
904
905 return 2;
906}
907
908/*
909 * Send a "size limit reached" error message.
910 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
911 * Returns -1 if there was not enough room left to send the message,
912 * any other negative returned value must be considered as an error with an appctx st0
913 * returned value equal to PEER_SESS_ST_END.
914 */
915static inline int peer_send_error_size_limitmsg(struct appctx *appctx)
916{
917 struct peer_prep_params p = {
918 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, },
919 };
920
921 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
922}
923
924/*
925 * Send a "peer protocol" error message.
926 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
927 * Returns -1 if there was not enough room left to send the message,
928 * any other negative returned value must be considered as an error with an appctx st0
929 * returned value equal to PEER_SESS_ST_END.
930 */
931static inline int peer_send_error_protomsg(struct appctx *appctx)
932{
933 struct peer_prep_params p = {
934 .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, },
935 };
936
937 return peer_send_msg(appctx, peer_prepare_error_msg, &p);
938}
939
940/*
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100941 * Function used to lookup for recent stick-table updates associated with
942 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
943 */
944static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
945{
946 struct eb32_node *eb;
947
948 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
949 if (!eb) {
950 eb = eb32_first(&st->table->updates);
951 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
952 st->table->commitupdate = st->last_pushed = st->table->localupdate;
953 return NULL;
954 }
955 }
956
957 if ((int)(eb->key - st->table->localupdate) > 0) {
958 st->table->commitupdate = st->last_pushed = st->table->localupdate;
959 return NULL;
960 }
961
962 return eb32_entry(eb, struct stksess, upd);
963}
964
965/*
966 * Function used to lookup for recent stick-table updates associated with
967 * <st> shared stick-table during teach state 1 step.
968 */
969static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
970{
971 struct eb32_node *eb;
972
973 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
974 if (!eb) {
975 st->flags |= SHTABLE_F_TEACH_STAGE1;
976 eb = eb32_first(&st->table->updates);
977 if (eb)
978 st->last_pushed = eb->key - 1;
979 return NULL;
980 }
981
982 return eb32_entry(eb, struct stksess, upd);
983}
984
985/*
986 * Function used to lookup for recent stick-table updates associated with
987 * <st> shared stick-table during teach state 2 step.
988 */
989static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
990{
991 struct eb32_node *eb;
992
993 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
994 if (!eb || eb->key > st->teaching_origin) {
995 st->flags |= SHTABLE_F_TEACH_STAGE2;
996 return NULL;
997 }
998
999 return eb32_entry(eb, struct stksess, upd);
1000}
1001
1002/*
1003 * Generic function to emit update messages for <st> stick-table when a lesson must
1004 * be taught to the peer <p>.
1005 * <locked> must be set to 1 if the shared table <st> is already locked when entering
1006 * this function, 0 if not.
1007 *
1008 * This function temporary unlock/lock <st> when it sends stick-table updates or
1009 * when decrementing its refcount in case of any error when it sends this updates.
1010 *
1011 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1012 * Returns -1 if there was not enough room left to send the message,
1013 * any other negative returned value must be considered as an error with an appcxt st0
1014 * returned value equal to PEER_SESS_ST_END.
1015 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
1016 * unlocked if not already locked when entering this function.
1017 */
1018static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
1019 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
1020 struct shared_table *st, int locked)
1021{
1022 int ret, new_pushed, use_timed;
1023
1024 ret = 1;
1025 use_timed = 0;
1026 if (st != p->last_local_table) {
1027 ret = peer_send_switchmsg(st, appctx);
1028 if (ret <= 0)
1029 return ret;
1030
1031 p->last_local_table = st;
1032 }
1033
1034 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
1035 use_timed = !(p->flags & PEER_F_DWNGRD);
1036
1037 /* We force new pushed to 1 to force identifier in update message */
1038 new_pushed = 1;
1039
1040 if (!locked)
1041 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1042
1043 while (1) {
1044 struct stksess *ts;
1045 unsigned updateid;
1046
1047 /* push local updates */
1048 ts = peer_stksess_lookup(st);
1049 if (!ts)
1050 break;
1051
1052 updateid = ts->upd.key;
1053 ts->ref_cnt++;
1054 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1055
1056 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
1057 if (ret <= 0) {
1058 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1059 ts->ref_cnt--;
1060 if (!locked)
1061 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1062 return ret;
1063 }
1064
1065 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1066 ts->ref_cnt--;
1067 st->last_pushed = updateid;
1068
1069 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
1070 (int)(st->last_pushed - st->table->commitupdate) > 0)
1071 st->table->commitupdate = st->last_pushed;
1072
1073 /* identifier may not needed in next update message */
1074 new_pushed = 0;
1075 }
1076
1077 out:
1078 if (!locked)
1079 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1080 return 1;
1081}
1082
1083/*
1084 * Function to emit update messages for <st> stick-table when a lesson must
1085 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
1086 *
1087 * Note that <st> shared stick-table is locked when calling this function.
1088 *
1089 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1090 * Returns -1 if there was not enough room left to send the message,
1091 * any other negative returned value must be considered as an error with an appcxt st0
1092 * returned value equal to PEER_SESS_ST_END.
1093 */
1094static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
1095 struct shared_table *st)
1096{
1097 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
1098}
1099
1100/*
1101 * Function to emit update messages for <st> stick-table when a lesson must
1102 * be taught to the peer <p> during teach state 1 step.
1103 *
1104 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1105 * Returns -1 if there was not enough room left to send the message,
1106 * any other negative returned value must be considered as an error with an appcxt st0
1107 * returned value equal to PEER_SESS_ST_END.
1108 */
1109static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
1110 struct shared_table *st)
1111{
1112 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
1113}
1114
1115/*
1116 * Function to emit update messages for <st> stick-table when a lesson must
1117 * be taught to the peer <p> during teach state 1 step.
1118 *
1119 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
1120 * Returns -1 if there was not enough room left to send the message,
1121 * any other negative returned value must be considered as an error with an appcxt st0
1122 * returned value equal to PEER_SESS_ST_END.
1123 */
1124static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
1125 struct shared_table *st)
1126{
1127 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
1128}
1129
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001130
1131/*
1132 * Function used to parse a stick-table update message after it has been received
1133 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1134 * receipt buffer with <msg_end> being position of the end of the stick-table message.
1135 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1136 * was encountered.
1137 * <exp> must be set if the stick-table entry expires.
1138 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
1139 * messages, in this case the stick-table udpate message is received with a stick-table
1140 * update ID.
1141 * <totl> is the length of the stick-table update message computed upon receipt.
1142 */
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001143static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
1144 char **msg_cur, char *msg_end, int msg_len, int totl)
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001145{
1146 struct stream_interface *si = appctx->owner;
1147 struct shared_table *st = p->remote_table;
1148 struct stksess *ts, *newts;
1149 uint32_t update;
1150 int expire;
1151 unsigned int data_type;
1152 void *data_ptr;
1153
1154 /* Here we have data message */
1155 if (!st)
1156 goto ignore_msg;
1157
1158 expire = MS_TO_TICKS(st->table->expire);
1159
1160 if (updt) {
1161 if (msg_len < sizeof(update)) {
1162 /* malformed message */
1163 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1164 return 0;
1165 }
1166 memcpy(&update, *msg_cur, sizeof(update));
1167 *msg_cur += sizeof(update);
1168 st->last_get = htonl(update);
1169 }
1170 else {
1171 st->last_get++;
1172 }
1173
1174 if (exp) {
1175 size_t expire_sz = sizeof expire;
1176
1177 if (*msg_cur + expire_sz > msg_end) {
1178 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1179 return 0;
1180 }
1181 memcpy(&expire, *msg_cur, expire_sz);
1182 *msg_cur += expire_sz;
1183 expire = ntohl(expire);
1184 }
1185
1186 newts = stksess_new(st->table, NULL);
1187 if (!newts)
1188 goto ignore_msg;
1189
1190 if (st->table->type == SMP_T_STR) {
1191 unsigned int to_read, to_store;
1192
1193 to_read = intdecode(msg_cur, msg_end);
1194 if (!*msg_cur) {
1195 /* malformed message */
1196 stksess_free(st->table, newts);
1197 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1198 return 0;
1199 }
1200 to_store = MIN(to_read, st->table->key_size - 1);
1201 if (*msg_cur + to_store > msg_end) {
1202 /* malformed message */
1203 stksess_free(st->table, newts);
1204 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1205 return 0;
1206 }
1207
1208 memcpy(newts->key.key, *msg_cur, to_store);
1209 newts->key.key[to_store] = 0;
1210 *msg_cur += to_read;
1211 }
1212 else if (st->table->type == SMP_T_SINT) {
1213 unsigned int netinteger;
1214
1215 if (*msg_cur + sizeof(netinteger) > msg_end) {
1216 /* malformed message */
1217 stksess_free(st->table, newts);
1218 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1219 return 0;
1220 }
1221 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1222 netinteger = ntohl(netinteger);
1223 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1224 *msg_cur += sizeof(netinteger);
1225 }
1226 else {
1227 if (*msg_cur + st->table->key_size > msg_end) {
1228 /* malformed message */
1229 stksess_free(st->table, newts);
1230 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1231 return 0;
1232 }
1233 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1234 *msg_cur += st->table->key_size;
1235 }
1236
1237 /* lookup for existing entry */
1238 ts = stktable_set_entry(st->table, newts);
1239 if (ts != newts) {
1240 stksess_free(st->table, newts);
1241 newts = NULL;
1242 }
1243
1244 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1245
1246 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
1247
1248 if (!((1 << data_type) & st->remote_data))
1249 continue;
1250
1251 switch (stktable_data_types[data_type].std_type) {
1252 case STD_T_SINT: {
1253 int data;
1254
1255 data = intdecode(msg_cur, msg_end);
1256 if (!*msg_cur) {
1257 /* malformed message */
1258 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1259 stktable_touch_remote(st->table, ts, 1);
1260 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1261 return 0;
1262 }
1263
1264 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1265 if (data_ptr)
1266 stktable_data_cast(data_ptr, std_t_sint) = data;
1267 break;
1268 }
1269 case STD_T_UINT: {
1270 unsigned int data;
1271
1272 data = intdecode(msg_cur, msg_end);
1273 if (!*msg_cur) {
1274 /* malformed message */
1275 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1276 stktable_touch_remote(st->table, ts, 1);
1277 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1278 return 0;
1279 }
1280
1281 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1282 if (data_ptr)
1283 stktable_data_cast(data_ptr, std_t_uint) = data;
1284 break;
1285 }
1286 case STD_T_ULL: {
1287 unsigned long long data;
1288
1289 data = intdecode(msg_cur, msg_end);
1290 if (!*msg_cur) {
1291 /* malformed message */
1292 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1293 stktable_touch_remote(st->table, ts, 1);
1294 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1295 return 0;
1296 }
1297
1298 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1299 if (data_ptr)
1300 stktable_data_cast(data_ptr, std_t_ull) = data;
1301 break;
1302 }
1303 case STD_T_FRQP: {
1304 struct freq_ctr_period data;
1305
1306 /* First bit is reserved for the freq_ctr_period lock
1307 Note: here we're still protected by the stksess lock
1308 so we don't need to update the update the freq_ctr_period
1309 using its internal lock */
1310
1311 data.curr_tick = tick_add(now_ms, -intdecode(msg_cur, msg_end)) & ~0x1;
1312 if (!*msg_cur) {
1313 /* malformed message */
1314 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1315 stktable_touch_remote(st->table, ts, 1);
1316 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1317 return 0;
1318 }
1319 data.curr_ctr = intdecode(msg_cur, msg_end);
1320 if (!*msg_cur) {
1321 /* malformed message */
1322 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1323 stktable_touch_remote(st->table, ts, 1);
1324 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1325 return 0;
1326 }
1327 data.prev_ctr = intdecode(msg_cur, msg_end);
1328 if (!*msg_cur) {
1329 /* malformed message */
1330 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1331 stktable_touch_remote(st->table, ts, 1);
1332 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1333 return 0;
1334 }
1335
1336 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1337 if (data_ptr)
1338 stktable_data_cast(data_ptr, std_t_frqp) = data;
1339 break;
1340 }
1341 }
1342 }
1343 /* Force new expiration */
1344 ts->expire = tick_add(now_ms, expire);
1345
1346 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1347 stktable_touch_remote(st->table, ts, 1);
1348 return 1;
1349
1350 ignore_msg:
1351 /* skip consumed message */
1352 co_skip(si_oc(si), totl);
1353 return 0;
1354}
1355
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001356/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001357 * Function used to parse a stick-table update acknowledgement message after it
1358 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1359 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1360 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1361 * was encountered.
1362 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1363 */
1364static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1365 char **msg_cur, char *msg_end)
1366{
1367 /* ack message */
1368 uint32_t table_id ;
1369 uint32_t update;
1370 struct shared_table *st;
1371
1372 table_id = intdecode(msg_cur, msg_end);
1373 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1374 /* malformed message */
1375 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1376 return 0;
1377 }
1378
1379 memcpy(&update, *msg_cur, sizeof(update));
1380 update = ntohl(update);
1381
1382 for (st = p->tables; st; st = st->next) {
1383 if (st->local_id == table_id) {
1384 st->update = update;
1385 break;
1386 }
1387 }
1388
1389 return 1;
1390}
1391
1392/*
1393 * Function used to parse a stick-table switch message after it has been received
1394 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1395 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1396 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1397 * was encountered.
1398 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1399 */
1400static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1401 char **msg_cur, char *msg_end)
1402{
1403 struct shared_table *st;
1404 int table_id;
1405
1406 table_id = intdecode(msg_cur, msg_end);
1407 if (!*msg_cur) {
1408 /* malformed message */
1409 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1410 return 0;
1411 }
1412
1413 p->remote_table = NULL;
1414 for (st = p->tables; st; st = st->next) {
1415 if (st->remote_id == table_id) {
1416 p->remote_table = st;
1417 break;
1418 }
1419 }
1420
1421 return 1;
1422}
1423
1424/*
1425 * Function used to parse a stick-table definition message after it has been received
1426 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1427 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1428 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1429 * was encountered.
1430 * <totl> is the length of the stick-table update message computed upon receipt.
1431 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1432 */
1433static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1434 char **msg_cur, char *msg_end, int totl)
1435{
1436 struct stream_interface *si = appctx->owner;
1437 int table_id_len;
1438 struct shared_table *st;
1439 int table_type;
1440 int table_keylen;
1441 int table_id;
1442 uint64_t table_data;
1443
1444 table_id = intdecode(msg_cur, msg_end);
1445 if (!*msg_cur) {
1446 /* malformed message */
1447 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1448 return 0;
1449 }
1450
1451 table_id_len = intdecode(msg_cur, msg_end);
1452 if (!*msg_cur) {
1453 /* malformed message */
1454 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1455 return 0;
1456 }
1457
1458 p->remote_table = NULL;
1459 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1460 /* malformed message */
1461 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1462 return 0;
1463 }
1464
1465 for (st = p->tables; st; st = st->next) {
1466 /* Reset IDs */
1467 if (st->remote_id == table_id)
1468 st->remote_id = 0;
1469
1470 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1471 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1472 p->remote_table = st;
1473 }
1474
1475 if (!p->remote_table)
1476 goto ignore_msg;
1477
1478 *msg_cur += table_id_len;
1479 if (*msg_cur >= msg_end) {
1480 /* malformed message */
1481 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1482 return 0;
1483 }
1484
1485 table_type = intdecode(msg_cur, msg_end);
1486 if (!*msg_cur) {
1487 /* malformed message */
1488 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1489 return 0;
1490 }
1491
1492 table_keylen = intdecode(msg_cur, msg_end);
1493 if (!*msg_cur) {
1494 /* malformed message */
1495 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1496 return 0;
1497 }
1498
1499 table_data = intdecode(msg_cur, msg_end);
1500 if (!*msg_cur) {
1501 /* malformed message */
1502 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1503 return 0;
1504 }
1505
1506 if (p->remote_table->table->type != table_type
1507 || p->remote_table->table->key_size != table_keylen) {
1508 p->remote_table = NULL;
1509 goto ignore_msg;
1510 }
1511
1512 p->remote_table->remote_data = table_data;
1513 p->remote_table->remote_id = table_id;
1514 return 1;
1515
1516 ignore_msg:
1517 co_skip(si_oc(si), totl);
1518 return 0;
1519}
1520
1521/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001522 * Receive a stick-table message.
1523 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1524 * -1 if there was an error updating the appctx state st0 accordingly.
1525 */
1526static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1527 uint32_t *msg_len, int *totl)
1528{
1529 int reql;
1530 struct stream_interface *si = appctx->owner;
1531
1532 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1533 if (reql <= 0) /* closed or EOL not found */
1534 goto incomplete;
1535
1536 *totl += reql;
1537
1538 if ((unsigned int)msg_head[1] < 128)
1539 return 1;
1540
1541 /* Read and Decode message length */
1542 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1543 if (reql <= 0) /* closed */
1544 goto incomplete;
1545
1546 *totl += reql;
1547
1548 if ((unsigned int)msg_head[2] < 240) {
1549 *msg_len = msg_head[2];
1550 }
1551 else {
1552 int i;
1553 char *cur;
1554 char *end;
1555
1556 for (i = 3 ; i < msg_head_sz ; i++) {
1557 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1558 if (reql <= 0) /* closed */
1559 goto incomplete;
1560
1561 *totl += reql;
1562
1563 if (!(msg_head[i] & 0x80))
1564 break;
1565 }
1566
1567 if (i == msg_head_sz) {
1568 /* malformed message */
1569 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1570 return -1;
1571 }
1572 end = msg_head + msg_head_sz;
1573 cur = &msg_head[2];
1574 *msg_len = intdecode(&cur, end);
1575 if (!cur) {
1576 /* malformed message */
1577 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1578 return -1;
1579 }
1580 }
1581
1582 /* Read message content */
1583 if (*msg_len) {
1584 if (*msg_len > trash.size) {
1585 /* Status code is not success, abort */
1586 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1587 return -1;
1588 }
1589
1590 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1591 if (reql <= 0) /* closed */
1592 goto incomplete;
1593 *totl += reql;
1594 }
1595
1596 return 1;
1597
1598 incomplete:
1599 if (reql < 0) {
1600 /* there was an error */
1601 appctx->st0 = PEER_SESS_ST_END;
1602 return -1;
1603 }
1604
1605 return 0;
1606}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001607
1608/*
1609 * Treat the awaited message with <msg_head> as header.*
1610 * Return 1 if succeeded, 0 if not.
1611 */
1612static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1613 char **msg_cur, char *msg_end, int msg_len, int totl)
1614{
1615 struct stream_interface *si = appctx->owner;
1616 struct stream *s = si_strm(si);
1617 struct peers *peers = strm_fe(s)->parent;
1618
1619 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1620 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1621 struct shared_table *st;
1622 /* Reset message: remote need resync */
1623
1624 /* prepare tables fot a global push */
1625 for (st = peer->tables; st; st = st->next) {
1626 st->teaching_origin = st->last_pushed = st->table->update;
1627 st->flags = 0;
1628 }
1629
1630 /* reset teaching flags to 0 */
1631 peer->flags &= PEER_TEACH_RESET;
1632
1633 /* flag to start to teach lesson */
1634 peer->flags |= PEER_F_TEACH_PROCESS;
1635 }
1636 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1637 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1638 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1639 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1640 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1641 }
1642 peer->confirm++;
1643 }
1644 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1645 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1646 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1647 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1648
1649 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
1650 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1651 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1652 }
1653 peer->confirm++;
1654 }
1655 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1656 struct shared_table *st;
1657
1658 /* If stopping state */
1659 if (stopping) {
1660 /* Close session, push resync no more needed */
1661 peer->flags |= PEER_F_TEACH_COMPLETE;
1662 appctx->st0 = PEER_SESS_ST_END;
1663 return 0;
1664 }
1665 for (st = peer->tables; st; st = st->next) {
1666 st->update = st->last_pushed = st->teaching_origin;
1667 st->flags = 0;
1668 }
1669
1670 /* reset teaching flags to 0 */
1671 peer->flags &= PEER_TEACH_RESET;
1672 }
1673 }
1674 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1675 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1676 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1677 return 0;
1678 }
1679 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1680 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1681 return 0;
1682 }
1683 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1684 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1685 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1686 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1687 int update, expire;
1688
1689 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1690 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1691 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1692 msg_cur, msg_end, msg_len, totl))
1693 return 0;
1694
1695 }
1696 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1697 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1698 return 0;
1699 }
1700 }
1701 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1702 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1703 return 0;
1704 }
1705
1706 return 1;
1707}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001708
1709
1710/*
1711 * Send any message to <peer> peer.
1712 * Returns 1 if succeeded, or -1 or 0 if failed.
1713 * -1 means an internal error occured, 0 is for a peer protocol error leading
1714 * to a peer state change (from the peer I/O handler point of view).
1715 */
1716static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1717{
1718 int repl;
1719 struct stream_interface *si = appctx->owner;
1720 struct stream *s = si_strm(si);
1721 struct peers *peers = strm_fe(s)->parent;
1722
1723 /* Need to request a resync */
1724 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1725 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1726 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1727
1728 repl = peer_send_resync_reqmsg(appctx);
1729 if (repl <= 0)
1730 return repl;
1731
1732 peers->flags |= PEERS_F_RESYNC_PROCESS;
1733 }
1734
1735 /* Nothing to read, now we start to write */
1736 if (peer->tables) {
1737 struct shared_table *st;
1738 struct shared_table *last_local_table;
1739
1740 last_local_table = peer->last_local_table;
1741 if (!last_local_table)
1742 last_local_table = peer->tables;
1743 st = last_local_table->next;
1744
1745 while (1) {
1746 if (!st)
1747 st = peer->tables;
1748
1749 /* It remains some updates to ack */
1750 if (st->last_get != st->last_acked) {
1751 repl = peer_send_ackmsg(st, appctx);
1752 if (repl <= 0)
1753 return repl;
1754
1755 st->last_acked = st->last_get;
1756 }
1757
1758 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1759 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1760 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1761 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1762
1763 repl = peer_send_teach_process_msgs(appctx, peer, st);
1764 if (repl <= 0) {
1765 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1766 return repl;
1767 }
1768 }
1769 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1770 }
1771 else {
1772 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1773 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1774 if (repl <= 0)
1775 return repl;
1776 }
1777
1778 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1779 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1780 if (repl <= 0)
1781 return repl;
1782 }
1783 }
1784
1785 if (st == last_local_table)
1786 break;
1787 st = st->next;
1788 }
1789 }
1790
1791 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1792 repl = peer_send_resync_finishedmsg(appctx, peer);
1793 if (repl <= 0)
1794 return repl;
1795
1796 /* flag finished message sent */
1797 peer->flags |= PEER_F_TEACH_FINISHED;
1798 }
1799
1800 /* Confirm finished or partial messages */
1801 while (peer->confirm) {
1802 repl = peer_send_resync_confirmsg(appctx);
1803 if (repl <= 0)
1804 return repl;
1805
1806 peer->confirm--;
1807 }
1808
1809 return 1;
1810}
1811
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001812/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001813 * Read and parse a first line of a "hello" peer protocol message.
1814 * Returns 0 if could not read a line, -1 if there was a read error or
1815 * the line is malformed, 1 if succeeded.
1816 */
1817static inline int peer_getline_version(struct appctx *appctx,
1818 unsigned int *maj_ver, unsigned int *min_ver)
1819{
1820 int reql;
1821
1822 reql = peer_getline(appctx);
1823 if (!reql)
1824 return 0;
1825
1826 if (reql < 0)
1827 return -1;
1828
1829 /* test protocol */
1830 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1831 appctx->st0 = PEER_SESS_ST_EXIT;
1832 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1833 return -1;
1834 }
1835 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1836 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1837 appctx->st0 = PEER_SESS_ST_EXIT;
1838 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1839 return -1;
1840 }
1841
1842 return 1;
1843}
1844
1845/*
1846 * Read and parse a second line of a "hello" peer protocol message.
1847 * Returns 0 if could not read a line, -1 if there was a read error or
1848 * the line is malformed, 1 if succeeded.
1849 */
1850static inline int peer_getline_host(struct appctx *appctx)
1851{
1852 int reql;
1853
1854 reql = peer_getline(appctx);
1855 if (!reql)
1856 return 0;
1857
1858 if (reql < 0)
1859 return -1;
1860
1861 /* test hostname match */
1862 if (strcmp(localpeer, trash.area) != 0) {
1863 appctx->st0 = PEER_SESS_ST_EXIT;
1864 appctx->st1 = PEER_SESS_SC_ERRHOST;
1865 return -1;
1866 }
1867
1868 return 1;
1869}
1870
1871/*
1872 * Read and parse a last line of a "hello" peer protocol message.
1873 * Returns 0 if could not read a character, -1 if there was a read error or
1874 * the line is malformed, 1 if succeeded.
1875 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1876 */
1877static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1878{
1879 char *p;
1880 int reql;
1881 struct peer *peer;
1882 struct stream_interface *si = appctx->owner;
1883 struct stream *s = si_strm(si);
1884 struct peers *peers = strm_fe(s)->parent;
1885
1886 reql = peer_getline(appctx);
1887 if (!reql)
1888 return 0;
1889
1890 if (reql < 0)
1891 return -1;
1892
1893 /* parse line "<peer name> <pid> <relative_pid>" */
1894 p = strchr(trash.area, ' ');
1895 if (!p) {
1896 appctx->st0 = PEER_SESS_ST_EXIT;
1897 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1898 return -1;
1899 }
1900 *p = 0;
1901
1902 /* lookup known peer */
1903 for (peer = peers->remote; peer; peer = peer->next) {
1904 if (strcmp(peer->id, trash.area) == 0)
1905 break;
1906 }
1907
1908 /* if unknown peer */
1909 if (!peer) {
1910 appctx->st0 = PEER_SESS_ST_EXIT;
1911 appctx->st1 = PEER_SESS_SC_ERRPEER;
1912 return -1;
1913 }
1914 *curpeer = peer;
1915
1916 return 1;
1917}
1918
1919/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01001920 * Init <peer> peer after having accepted it at peer protocol level.
1921 */
1922static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
1923{
1924 struct shared_table *st;
1925
1926 /* Register status code */
1927 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
1928
1929 /* Awake main task */
1930 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1931
1932 /* Init confirm counter */
1933 peer->confirm = 0;
1934
1935 /* Init cursors */
1936 for (st = peer->tables; st ; st = st->next) {
1937 st->last_get = st->last_acked = 0;
1938 st->teaching_origin = st->last_pushed = st->update;
1939 }
1940
1941 /* reset teaching and learning flags to 0 */
1942 peer->flags &= PEER_TEACH_RESET;
1943 peer->flags &= PEER_LEARN_RESET;
1944
1945 /* if current peer is local */
1946 if (peer->local) {
1947 /* if current host need resyncfrom local and no process assined */
1948 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1949 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1950 /* assign local peer for a lesson, consider lesson already requested */
1951 peer->flags |= PEER_F_LEARN_ASSIGN;
1952 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1953 }
1954
1955 }
1956 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1957 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1958 /* assign peer for a lesson */
1959 peer->flags |= PEER_F_LEARN_ASSIGN;
1960 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1961 }
1962}
1963
1964/*
1965 * Init <peer> peer after having connected it at peer protocol level.
1966 */
1967static inline void init_connected_peer(struct peer *peer, struct peers *peers)
1968{
1969 struct shared_table *st;
1970
1971 /* Init cursors */
1972 for (st = peer->tables; st ; st = st->next) {
1973 st->last_get = st->last_acked = 0;
1974 st->teaching_origin = st->last_pushed = st->update;
1975 }
1976
1977 /* Init confirm counter */
1978 peer->confirm = 0;
1979
1980 /* reset teaching and learning flags to 0 */
1981 peer->flags &= PEER_TEACH_RESET;
1982 peer->flags &= PEER_LEARN_RESET;
1983
1984 /* If current peer is local */
1985 if (peer->local) {
1986 /* flag to start to teach lesson */
1987 peer->flags |= PEER_F_TEACH_PROCESS;
1988 }
1989 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1990 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1991 /* If peer is remote and resync from remote is needed,
1992 and no peer currently assigned */
1993
1994 /* assign peer for a lesson */
1995 peer->flags |= PEER_F_LEARN_ASSIGN;
1996 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1997 }
1998}
1999
2000/*
Emeric Brun2b920a12010-09-23 18:30:22 +02002001 * IO Handler to handle message exchance with a peer
2002 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002003static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002004{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002005 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02002006 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002007 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02002008 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002009 int reql = 0;
2010 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002011 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002012 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02002013
Joseph Herlant82b2f542018-11-15 12:19:14 -08002014 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002015 if (si_ic(si)->buf.size == 0) {
2016 si_rx_room_blk(si);
2017 goto out;
2018 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002019
Emeric Brun2b920a12010-09-23 18:30:22 +02002020 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002021 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002022switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002023 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002024 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002025 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002026 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002027 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002028 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02002029 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002030 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002031 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002032 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
2033 if (reql <= 0) {
2034 if (!reql)
2035 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002036 goto switchstate;
2037 }
2038
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002039 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02002040 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002041 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002042 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002043 reql = peer_getline_host(appctx);
2044 if (reql <= 0) {
2045 if (!reql)
2046 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002047 goto switchstate;
2048 }
2049
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002050 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002051 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002052 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002053 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002054 reql = peer_getline_last(appctx, &curpeer);
2055 if (reql <= 0) {
2056 if (!reql)
2057 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002058 goto switchstate;
2059 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002060
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002061 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002062 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002063 if (curpeer->local) {
2064 /* Local connection, reply a retry */
2065 appctx->st0 = PEER_SESS_ST_EXIT;
2066 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2067 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002068 }
Emeric Brun80527f52017-06-19 17:46:37 +02002069
2070 /* we're killing a connection, we must apply a random delay before
2071 * retrying otherwise the other end will do the same and we can loop
2072 * for a while.
2073 */
2074 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002075 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002076 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002077 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2078 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2079 curpeer->flags |= PEER_F_DWNGRD;
2080 }
2081 else {
2082 curpeer->flags &= ~PEER_F_DWNGRD;
2083 }
2084 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002085 curpeer->appctx = appctx;
2086 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002087 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +01002088 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002089 /* fall through */
2090 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002091 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002092 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002093 if (!curpeer) {
2094 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002095 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002096 if (curpeer->appctx != appctx) {
2097 appctx->st0 = PEER_SESS_ST_END;
2098 goto switchstate;
2099 }
2100 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002101
2102 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002103 if (repl <= 0) {
2104 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002105 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002106 goto switchstate;
2107 }
2108
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002109 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002110
Emeric Brun2b920a12010-09-23 18:30:22 +02002111 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +01002112 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002113 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002114 goto switchstate;
2115 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002116 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002117 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002118 if (!curpeer) {
2119 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002120 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002121 if (curpeer->appctx != appctx) {
2122 appctx->st0 = PEER_SESS_ST_END;
2123 goto switchstate;
2124 }
2125 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002126
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002127 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002128 if (repl <= 0) {
2129 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002130 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002131 goto switchstate;
2132 }
2133
2134 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002135 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002136 /* fall through */
2137 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002138 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002139 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002140 if (!curpeer) {
2141 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002142 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002143 if (curpeer->appctx != appctx) {
2144 appctx->st0 = PEER_SESS_ST_END;
2145 goto switchstate;
2146 }
2147 }
2148
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002149 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002150 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002151
Frédéric Lécaillece025572019-01-21 13:38:06 +01002152 reql = peer_getline(appctx);
2153 if (!reql)
2154 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002155
Frédéric Lécaillece025572019-01-21 13:38:06 +01002156 if (reql < 0)
2157 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002158
2159 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002160 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002161
2162 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002163 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002164
2165 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002166 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002167 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002168 }
2169 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002170 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2171 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002172 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002173 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002174 goto switchstate;
2175 }
Willy Tarreau2d372c22018-11-05 17:12:27 +01002176 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002177 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002178 /* fall through */
2179 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002180 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002181 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002182 char *msg_cur = trash.area;
2183 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002184 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002185 int totl = 0;
2186
Willy Tarreau2d372c22018-11-05 17:12:27 +01002187 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002188 if (!curpeer) {
2189 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002190 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002191 if (curpeer->appctx != appctx) {
2192 appctx->st0 = PEER_SESS_ST_END;
2193 goto switchstate;
2194 }
2195 }
2196
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002197 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2198 if (reql <= 0) {
2199 if (reql == -1)
2200 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002201 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002202 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002203
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002204 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002205 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002206 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002207
Emeric Brun2b920a12010-09-23 18:30:22 +02002208 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002209 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002210 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002211 goto switchstate;
2212
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002213send_msgs:
2214 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002215 repl = peer_send_msgs(appctx, curpeer);
2216 if (repl <= 0) {
2217 if (repl == -1)
2218 goto out;
2219 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002220 }
2221
Emeric Brun2b920a12010-09-23 18:30:22 +02002222 /* noting more to do */
2223 goto out;
2224 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002225 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002226 if (prev_state == PEER_SESS_ST_WAITMSG)
2227 HA_ATOMIC_SUB(&connected_peers, 1);
2228 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002229 if (peer_send_status_errormsg(appctx) == -1)
2230 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002231 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002232 goto switchstate;
2233 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002234 if (prev_state == PEER_SESS_ST_WAITMSG)
2235 HA_ATOMIC_SUB(&connected_peers, 1);
2236 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002237 if (peer_send_error_size_limitmsg(appctx) == -1)
2238 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002239 appctx->st0 = PEER_SESS_ST_END;
2240 goto switchstate;
2241 }
2242 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002243 if (prev_state == PEER_SESS_ST_WAITMSG)
2244 HA_ATOMIC_SUB(&connected_peers, 1);
2245 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002246 if (peer_send_error_protomsg(appctx) == -1)
2247 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002248 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002249 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002250 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002251 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002252 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002253 if (prev_state == PEER_SESS_ST_WAITMSG)
2254 HA_ATOMIC_SUB(&connected_peers, 1);
2255 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002256 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002257 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002258 curpeer = NULL;
2259 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002260 si_shutw(si);
2261 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002262 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002263 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002264 }
2265 }
2266 }
2267out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002268 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002269
2270 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002271 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002272 return;
2273}
2274
Willy Tarreau30576452015-04-13 13:50:30 +02002275static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002276 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002277 .name = "<PEER>", /* used for logging */
2278 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002279 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002280};
Emeric Brun2b920a12010-09-23 18:30:22 +02002281
2282/*
2283 * Use this function to force a close of a peer session
2284 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002285static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002286{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002287 /* Note that the peer sessions which have just been created
2288 * (->st0 == PEER_SESS_ST_CONNECT) must not
2289 * be shutdown, if not, the TCP session will never be closed
2290 * and stay in CLOSE_WAIT state after having been closed by
2291 * the remote side.
2292 */
2293 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002294 return;
2295
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002296 if (appctx->applet != &peer_applet)
2297 return;
2298
Willy Tarreau2d372c22018-11-05 17:12:27 +01002299 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
2300 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002301 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002302 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002303}
2304
Willy Tarreau91d96282015-03-13 15:47:26 +01002305/* Pre-configures a peers frontend to accept incoming connections */
2306void peers_setup_frontend(struct proxy *fe)
2307{
2308 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002309 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002310 fe->maxconn = 0;
2311 fe->conn_retries = CONN_RETRIES;
2312 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002313 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002314 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002315 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002316 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002317}
2318
Emeric Brun2b920a12010-09-23 18:30:22 +02002319/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002320 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002321 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002322static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002323{
Willy Tarreau04b92862017-09-15 11:01:04 +02002324 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002326 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002327 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002328 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002329 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002330
Emeric Brunb3971ab2015-05-12 18:49:09 +02002331 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
2332 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002333 s = NULL;
2334
Emeric Brun1138fd02017-06-19 12:38:55 +02002335 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002336 if (!appctx)
2337 goto out_close;
2338
2339 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002340 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002341
Willy Tarreau04b92862017-09-15 11:01:04 +02002342 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002343 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002344 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002345 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002346 }
2347
Willy Tarreau87787ac2017-08-28 16:22:54 +02002348 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002349 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002350 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002351 }
2352
Willy Tarreau342bfb12015-04-05 01:35:34 +02002353 /* The tasks below are normally what is supposed to be done by
2354 * fe->accept().
2355 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002356 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002357
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002358 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002359 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002360 appctx_wakeup(appctx);
2361
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002362 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002363 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002364 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002365
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002366 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002367 * pre-initialized connection in si->conn.
2368 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002369 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002370 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002371
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002372 if (unlikely((cs = cs_new(conn)) == NULL))
2373 goto out_free_conn;
2374
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002375 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002376 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2377
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002378 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01002379 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002380 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002381
Emeric Brun2b920a12010-09-23 18:30:22 +02002382 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002383 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002384
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002385 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002386
Emeric Brunb3971ab2015-05-12 18:49:09 +02002387 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002388 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01002389 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002390 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002391
2392 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002393 out_free_conn:
2394 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002395 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002396 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002397 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002398 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002399 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002400 out_free_appctx:
2401 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002402 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002403 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002404}
2405
2406/*
2407 * Task processing function to manage re-connect and peer session
2408 * tasks wakeup on local update.
2409 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002410static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002411{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002412 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002413 struct peer *ps;
2414 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002415
2416 task->expire = TICK_ETERNITY;
2417
Emeric Brunb3971ab2015-05-12 18:49:09 +02002418 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002419 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002420 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002421 task_delete(peers->sync_task);
2422 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002423 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002424 return NULL;
2425 }
2426
Emeric Brun80527f52017-06-19 17:46:37 +02002427 /* Acquire lock for all peers of the section */
2428 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002429 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002430
Emeric Brun2b920a12010-09-23 18:30:22 +02002431 if (!stopping) {
2432 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002433
2434 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2435 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2436 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002437 /* Resync from local peer needed
2438 no peer was assigned for the lesson
2439 and no old local peer found
2440 or resync timeout expire */
2441
2442 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002443 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002444
2445 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002446 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002447 }
2448
2449 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002450 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002451 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002452 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002453 if (!ps->appctx) {
2454 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002455 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002456 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002457 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002458 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002459 tick_is_expired(ps->reconnect, now_ms))) {
2460 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002461 * or previous peer connection established with success
2462 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002463 * and reconnection timer is expired */
2464
2465 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002466 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002467 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002468 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002469 /* If previous session failed during connection
2470 * but reconnection timer is not expired */
2471
2472 /* reschedule task for reconnect */
2473 task->expire = tick_first(task->expire, ps->reconnect);
2474 }
2475 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002476 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002477 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002478 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002479 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2480 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002481 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2482 /* Resync from a remote is needed
2483 * and no peer was assigned for lesson
2484 * and current peer may be up2date */
2485
2486 /* assign peer for the lesson */
2487 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002488 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002489
Willy Tarreau9df94c22016-10-31 18:42:52 +01002490 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002491 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002492 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002493 else {
2494 /* Awake session if there is data to push */
2495 for (st = ps->tables; st ; st = st->next) {
2496 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002497 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002498 appctx_wakeup(ps->appctx);
2499 break;
2500 }
2501 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002502 }
2503 /* else do nothing */
2504 } /* SUCCESSCODE */
2505 } /* !ps->peer->local */
2506 } /* for */
2507
2508 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002509 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2510 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2511 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002512 /* Resync from remote peer needed
2513 * no peer was assigned for the lesson
2514 * and resync timeout expire */
2515
2516 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002517 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002518 }
2519
Emeric Brunb3971ab2015-05-12 18:49:09 +02002520 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002521 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002522 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2523 if (!tick_is_expired(peers->resync_timeout, now_ms))
2524 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002525 }
2526 } /* !stopping */
2527 else {
2528 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002529 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002530 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002531 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002532 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002533 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002534 peers->flags |= PEERS_F_DONOTSTOP;
2535 ps = peers->local;
2536 for (st = ps->tables; st ; st = st->next)
2537 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002538 }
2539
2540 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002541 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002542 /* we're killing a connection, we must apply a random delay before
2543 * retrying otherwise the other end will do the same and we can loop
2544 * for a while.
2545 */
2546 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002547 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002548 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002549 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002550 }
2551 }
2552 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002553
Emeric Brunb3971ab2015-05-12 18:49:09 +02002554 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002555 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002556 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002557 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002558 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002559 peers->flags &= ~PEERS_F_DONOTSTOP;
2560 for (st = ps->tables; st ; st = st->next)
2561 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002562 }
2563 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002564 else if (!ps->appctx) {
2565 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002566 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002567 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2568 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2569 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002570 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002571 * or previous peer connection was successfully established
2572 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002573 * or during previous connect, peer replies a try again statuscode */
2574
2575 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002576 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002577 }
2578 else {
2579 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002580 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002581 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002582 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002583 peers->flags &= ~PEERS_F_DONOTSTOP;
2584 for (st = ps->tables; st ; st = st->next)
2585 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002586 }
2587 }
2588 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002589 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002590 /* current peer connection is active and established
2591 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002592 for (st = ps->tables; st ; st = st->next) {
2593 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002594 appctx_wakeup(ps->appctx);
2595 break;
2596 }
2597 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002598 }
2599 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002600
2601 /* Release lock for all peers of the section */
2602 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002603 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002604
Emeric Brun2b920a12010-09-23 18:30:22 +02002605 /* Wakeup for re-connect */
2606 return task;
2607}
2608
Emeric Brunb3971ab2015-05-12 18:49:09 +02002609
Emeric Brun2b920a12010-09-23 18:30:22 +02002610/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002611 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002612 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002613int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002614{
Emeric Brun2b920a12010-09-23 18:30:22 +02002615 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002616 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002617
Emeric Brun2b920a12010-09-23 18:30:22 +02002618 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002619 peers->peers_fe->maxconn += 3;
2620 }
2621
Willy Tarreau4348fad2012-09-20 16:48:07 +02002622 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2623 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002624 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002625 if (!peers->sync_task)
2626 return 0;
2627
Emeric Brunb3971ab2015-05-12 18:49:09 +02002628 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002629 peers->sync_task->context = (void *)peers;
2630 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2631 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002632 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002633}
2634
2635
2636
2637/*
2638 * Function used to register a table for sync on a group of peers
2639 *
2640 */
2641void peers_register_table(struct peers *peers, struct stktable *table)
2642{
2643 struct shared_table *st;
2644 struct peer * curpeer;
2645 int id = 0;
2646
2647 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002648 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002649 st->table = table;
2650 st->next = curpeer->tables;
2651 if (curpeer->tables)
2652 id = curpeer->tables->local_id;
2653 st->local_id = id + 1;
2654
2655 curpeer->tables = st;
2656 }
2657
2658 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002659}
2660