blob: bc9ac3e73d191f66f70c2f89b276c142c2ec6a6b [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) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001161 if (msg_len < sizeof(update))
1162 goto malformed_exit;
1163
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001164 memcpy(&update, *msg_cur, sizeof(update));
1165 *msg_cur += sizeof(update);
1166 st->last_get = htonl(update);
1167 }
1168 else {
1169 st->last_get++;
1170 }
1171
1172 if (exp) {
1173 size_t expire_sz = sizeof expire;
1174
Willy Tarreau1e82a142019-01-29 11:08:06 +01001175 if (*msg_cur + expire_sz > msg_end)
1176 goto malformed_exit;
1177
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001178 memcpy(&expire, *msg_cur, expire_sz);
1179 *msg_cur += expire_sz;
1180 expire = ntohl(expire);
1181 }
1182
1183 newts = stksess_new(st->table, NULL);
1184 if (!newts)
1185 goto ignore_msg;
1186
1187 if (st->table->type == SMP_T_STR) {
1188 unsigned int to_read, to_store;
1189
1190 to_read = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001191 if (!*msg_cur)
1192 goto malformed_free_newts;
1193
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001194 to_store = MIN(to_read, st->table->key_size - 1);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001195 if (*msg_cur + to_store > msg_end)
1196 goto malformed_free_newts;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001197
1198 memcpy(newts->key.key, *msg_cur, to_store);
1199 newts->key.key[to_store] = 0;
1200 *msg_cur += to_read;
1201 }
1202 else if (st->table->type == SMP_T_SINT) {
1203 unsigned int netinteger;
1204
Willy Tarreau1e82a142019-01-29 11:08:06 +01001205 if (*msg_cur + sizeof(netinteger) > msg_end)
1206 goto malformed_free_newts;
1207
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001208 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
1209 netinteger = ntohl(netinteger);
1210 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
1211 *msg_cur += sizeof(netinteger);
1212 }
1213 else {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001214 if (*msg_cur + st->table->key_size > msg_end)
1215 goto malformed_free_newts;
1216
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001217 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1218 *msg_cur += st->table->key_size;
1219 }
1220
1221 /* lookup for existing entry */
1222 ts = stktable_set_entry(st->table, newts);
1223 if (ts != newts) {
1224 stksess_free(st->table, newts);
1225 newts = NULL;
1226 }
1227
1228 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1229
1230 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Willy Tarreau1e82a142019-01-29 11:08:06 +01001231 uint64_t decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001232
1233 if (!((1 << data_type) & st->remote_data))
1234 continue;
1235
Willy Tarreau1e82a142019-01-29 11:08:06 +01001236 decoded_int = intdecode(msg_cur, msg_end);
1237 if (!*msg_cur)
1238 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001239
Willy Tarreau1e82a142019-01-29 11:08:06 +01001240 switch (stktable_data_types[data_type].std_type) {
1241 case STD_T_SINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001242 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1243 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001244 stktable_data_cast(data_ptr, std_t_sint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001245 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001246
Willy Tarreau1e82a142019-01-29 11:08:06 +01001247 case STD_T_UINT:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001248 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1249 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001250 stktable_data_cast(data_ptr, std_t_uint) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001251 break;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001252
Willy Tarreau1e82a142019-01-29 11:08:06 +01001253 case STD_T_ULL:
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001254 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1255 if (data_ptr)
Willy Tarreau1e82a142019-01-29 11:08:06 +01001256 stktable_data_cast(data_ptr, std_t_ull) = decoded_int;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001257 break;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001258
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001259 case STD_T_FRQP: {
1260 struct freq_ctr_period data;
1261
1262 /* First bit is reserved for the freq_ctr_period lock
1263 Note: here we're still protected by the stksess lock
1264 so we don't need to update the update the freq_ctr_period
1265 using its internal lock */
1266
Willy Tarreau1e82a142019-01-29 11:08:06 +01001267 data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001268 data.curr_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001269 if (!*msg_cur)
1270 goto malformed_unlock;
1271
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001272 data.prev_ctr = intdecode(msg_cur, msg_end);
Willy Tarreau1e82a142019-01-29 11:08:06 +01001273 if (!*msg_cur)
1274 goto malformed_unlock;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001275
1276 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1277 if (data_ptr)
1278 stktable_data_cast(data_ptr, std_t_frqp) = data;
1279 break;
1280 }
1281 }
1282 }
1283 /* Force new expiration */
1284 ts->expire = tick_add(now_ms, expire);
1285
1286 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1287 stktable_touch_remote(st->table, ts, 1);
1288 return 1;
1289
1290 ignore_msg:
1291 /* skip consumed message */
1292 co_skip(si_oc(si), totl);
1293 return 0;
Willy Tarreau1e82a142019-01-29 11:08:06 +01001294
1295 malformed_unlock:
1296 /* malformed message */
1297 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1298 stktable_touch_remote(st->table, ts, 1);
1299 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1300 return 0;
1301
1302 malformed_free_newts:
1303 /* malformed message */
1304 stksess_free(st->table, newts);
1305 malformed_exit:
1306 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1307 return 0;
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001308}
1309
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001310/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001311 * Function used to parse a stick-table update acknowledgement message after it
1312 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1313 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1314 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1315 * was encountered.
1316 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1317 */
1318static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1319 char **msg_cur, char *msg_end)
1320{
1321 /* ack message */
1322 uint32_t table_id ;
1323 uint32_t update;
1324 struct shared_table *st;
1325
1326 table_id = intdecode(msg_cur, msg_end);
1327 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1328 /* malformed message */
1329 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1330 return 0;
1331 }
1332
1333 memcpy(&update, *msg_cur, sizeof(update));
1334 update = ntohl(update);
1335
1336 for (st = p->tables; st; st = st->next) {
1337 if (st->local_id == table_id) {
1338 st->update = update;
1339 break;
1340 }
1341 }
1342
1343 return 1;
1344}
1345
1346/*
1347 * Function used to parse a stick-table switch message after it has been received
1348 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1349 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1350 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1351 * was encountered.
1352 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1353 */
1354static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1355 char **msg_cur, char *msg_end)
1356{
1357 struct shared_table *st;
1358 int table_id;
1359
1360 table_id = intdecode(msg_cur, msg_end);
1361 if (!*msg_cur) {
1362 /* malformed message */
1363 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1364 return 0;
1365 }
1366
1367 p->remote_table = NULL;
1368 for (st = p->tables; st; st = st->next) {
1369 if (st->remote_id == table_id) {
1370 p->remote_table = st;
1371 break;
1372 }
1373 }
1374
1375 return 1;
1376}
1377
1378/*
1379 * Function used to parse a stick-table definition message after it has been received
1380 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1381 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1382 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1383 * was encountered.
1384 * <totl> is the length of the stick-table update message computed upon receipt.
1385 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1386 */
1387static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1388 char **msg_cur, char *msg_end, int totl)
1389{
1390 struct stream_interface *si = appctx->owner;
1391 int table_id_len;
1392 struct shared_table *st;
1393 int table_type;
1394 int table_keylen;
1395 int table_id;
1396 uint64_t table_data;
1397
1398 table_id = intdecode(msg_cur, msg_end);
1399 if (!*msg_cur) {
1400 /* malformed message */
1401 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1402 return 0;
1403 }
1404
1405 table_id_len = intdecode(msg_cur, msg_end);
1406 if (!*msg_cur) {
1407 /* malformed message */
1408 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1409 return 0;
1410 }
1411
1412 p->remote_table = NULL;
1413 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1414 /* malformed message */
1415 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1416 return 0;
1417 }
1418
1419 for (st = p->tables; st; st = st->next) {
1420 /* Reset IDs */
1421 if (st->remote_id == table_id)
1422 st->remote_id = 0;
1423
1424 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1425 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1426 p->remote_table = st;
1427 }
1428
1429 if (!p->remote_table)
1430 goto ignore_msg;
1431
1432 *msg_cur += table_id_len;
1433 if (*msg_cur >= msg_end) {
1434 /* malformed message */
1435 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1436 return 0;
1437 }
1438
1439 table_type = intdecode(msg_cur, msg_end);
1440 if (!*msg_cur) {
1441 /* malformed message */
1442 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1443 return 0;
1444 }
1445
1446 table_keylen = intdecode(msg_cur, msg_end);
1447 if (!*msg_cur) {
1448 /* malformed message */
1449 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1450 return 0;
1451 }
1452
1453 table_data = intdecode(msg_cur, msg_end);
1454 if (!*msg_cur) {
1455 /* malformed message */
1456 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1457 return 0;
1458 }
1459
1460 if (p->remote_table->table->type != table_type
1461 || p->remote_table->table->key_size != table_keylen) {
1462 p->remote_table = NULL;
1463 goto ignore_msg;
1464 }
1465
1466 p->remote_table->remote_data = table_data;
1467 p->remote_table->remote_id = table_id;
1468 return 1;
1469
1470 ignore_msg:
1471 co_skip(si_oc(si), totl);
1472 return 0;
1473}
1474
1475/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001476 * Receive a stick-table message.
1477 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1478 * -1 if there was an error updating the appctx state st0 accordingly.
1479 */
1480static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1481 uint32_t *msg_len, int *totl)
1482{
1483 int reql;
1484 struct stream_interface *si = appctx->owner;
1485
1486 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1487 if (reql <= 0) /* closed or EOL not found */
1488 goto incomplete;
1489
1490 *totl += reql;
1491
1492 if ((unsigned int)msg_head[1] < 128)
1493 return 1;
1494
1495 /* Read and Decode message length */
1496 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1497 if (reql <= 0) /* closed */
1498 goto incomplete;
1499
1500 *totl += reql;
1501
1502 if ((unsigned int)msg_head[2] < 240) {
1503 *msg_len = msg_head[2];
1504 }
1505 else {
1506 int i;
1507 char *cur;
1508 char *end;
1509
1510 for (i = 3 ; i < msg_head_sz ; i++) {
1511 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1512 if (reql <= 0) /* closed */
1513 goto incomplete;
1514
1515 *totl += reql;
1516
1517 if (!(msg_head[i] & 0x80))
1518 break;
1519 }
1520
1521 if (i == msg_head_sz) {
1522 /* malformed message */
1523 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1524 return -1;
1525 }
1526 end = msg_head + msg_head_sz;
1527 cur = &msg_head[2];
1528 *msg_len = intdecode(&cur, end);
1529 if (!cur) {
1530 /* malformed message */
1531 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1532 return -1;
1533 }
1534 }
1535
1536 /* Read message content */
1537 if (*msg_len) {
1538 if (*msg_len > trash.size) {
1539 /* Status code is not success, abort */
1540 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1541 return -1;
1542 }
1543
1544 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1545 if (reql <= 0) /* closed */
1546 goto incomplete;
1547 *totl += reql;
1548 }
1549
1550 return 1;
1551
1552 incomplete:
1553 if (reql < 0) {
1554 /* there was an error */
1555 appctx->st0 = PEER_SESS_ST_END;
1556 return -1;
1557 }
1558
1559 return 0;
1560}
Frédéric Lécaille444243c2019-01-24 15:40:11 +01001561
1562/*
1563 * Treat the awaited message with <msg_head> as header.*
1564 * Return 1 if succeeded, 0 if not.
1565 */
1566static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head,
1567 char **msg_cur, char *msg_end, int msg_len, int totl)
1568{
1569 struct stream_interface *si = appctx->owner;
1570 struct stream *s = si_strm(si);
1571 struct peers *peers = strm_fe(s)->parent;
1572
1573 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1574 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1575 struct shared_table *st;
1576 /* Reset message: remote need resync */
1577
1578 /* prepare tables fot a global push */
1579 for (st = peer->tables; st; st = st->next) {
1580 st->teaching_origin = st->last_pushed = st->table->update;
1581 st->flags = 0;
1582 }
1583
1584 /* reset teaching flags to 0 */
1585 peer->flags &= PEER_TEACH_RESET;
1586
1587 /* flag to start to teach lesson */
1588 peer->flags |= PEER_F_TEACH_PROCESS;
1589 }
1590 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
1591 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1592 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1593 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1594 peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
1595 }
1596 peer->confirm++;
1597 }
1598 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
1599 if (peer->flags & PEER_F_LEARN_ASSIGN) {
1600 peer->flags &= ~PEER_F_LEARN_ASSIGN;
1601 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1602
1603 peer->flags |= PEER_F_LEARN_NOTUP2DATE;
1604 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1605 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1606 }
1607 peer->confirm++;
1608 }
1609 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
1610 struct shared_table *st;
1611
1612 /* If stopping state */
1613 if (stopping) {
1614 /* Close session, push resync no more needed */
1615 peer->flags |= PEER_F_TEACH_COMPLETE;
1616 appctx->st0 = PEER_SESS_ST_END;
1617 return 0;
1618 }
1619 for (st = peer->tables; st; st = st->next) {
1620 st->update = st->last_pushed = st->teaching_origin;
1621 st->flags = 0;
1622 }
1623
1624 /* reset teaching flags to 0 */
1625 peer->flags &= PEER_TEACH_RESET;
1626 }
1627 }
1628 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1629 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
1630 if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl))
1631 return 0;
1632 }
1633 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
1634 if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end))
1635 return 0;
1636 }
1637 else if (msg_head[1] == PEER_MSG_STKT_UPDATE ||
1638 msg_head[1] == PEER_MSG_STKT_INCUPDATE ||
1639 msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED ||
1640 msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
1641 int update, expire;
1642
1643 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1644 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1645 if (!peer_treat_updatemsg(appctx, peer, update, expire,
1646 msg_cur, msg_end, msg_len, totl))
1647 return 0;
1648
1649 }
1650 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
1651 if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end))
1652 return 0;
1653 }
1654 }
1655 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1656 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1657 return 0;
1658 }
1659
1660 return 1;
1661}
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01001662
1663
1664/*
1665 * Send any message to <peer> peer.
1666 * Returns 1 if succeeded, or -1 or 0 if failed.
1667 * -1 means an internal error occured, 0 is for a peer protocol error leading
1668 * to a peer state change (from the peer I/O handler point of view).
1669 */
1670static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer)
1671{
1672 int repl;
1673 struct stream_interface *si = appctx->owner;
1674 struct stream *s = si_strm(si);
1675 struct peers *peers = strm_fe(s)->parent;
1676
1677 /* Need to request a resync */
1678 if ((peer->flags & PEER_F_LEARN_ASSIGN) &&
1679 (peers->flags & PEERS_F_RESYNC_ASSIGN) &&
1680 !(peers->flags & PEERS_F_RESYNC_PROCESS)) {
1681
1682 repl = peer_send_resync_reqmsg(appctx);
1683 if (repl <= 0)
1684 return repl;
1685
1686 peers->flags |= PEERS_F_RESYNC_PROCESS;
1687 }
1688
1689 /* Nothing to read, now we start to write */
1690 if (peer->tables) {
1691 struct shared_table *st;
1692 struct shared_table *last_local_table;
1693
1694 last_local_table = peer->last_local_table;
1695 if (!last_local_table)
1696 last_local_table = peer->tables;
1697 st = last_local_table->next;
1698
1699 while (1) {
1700 if (!st)
1701 st = peer->tables;
1702
1703 /* It remains some updates to ack */
1704 if (st->last_get != st->last_acked) {
1705 repl = peer_send_ackmsg(st, appctx);
1706 if (repl <= 0)
1707 return repl;
1708
1709 st->last_acked = st->last_get;
1710 }
1711
1712 if (!(peer->flags & PEER_F_TEACH_PROCESS)) {
1713 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
1714 if (!(peer->flags & PEER_F_LEARN_ASSIGN) &&
1715 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
1716
1717 repl = peer_send_teach_process_msgs(appctx, peer, st);
1718 if (repl <= 0) {
1719 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1720 return repl;
1721 }
1722 }
1723 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
1724 }
1725 else {
1726 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
1727 repl = peer_send_teach_stage1_msgs(appctx, peer, st);
1728 if (repl <= 0)
1729 return repl;
1730 }
1731
1732 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
1733 repl = peer_send_teach_stage2_msgs(appctx, peer, st);
1734 if (repl <= 0)
1735 return repl;
1736 }
1737 }
1738
1739 if (st == last_local_table)
1740 break;
1741 st = st->next;
1742 }
1743 }
1744
1745 if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) {
1746 repl = peer_send_resync_finishedmsg(appctx, peer);
1747 if (repl <= 0)
1748 return repl;
1749
1750 /* flag finished message sent */
1751 peer->flags |= PEER_F_TEACH_FINISHED;
1752 }
1753
1754 /* Confirm finished or partial messages */
1755 while (peer->confirm) {
1756 repl = peer_send_resync_confirmsg(appctx);
1757 if (repl <= 0)
1758 return repl;
1759
1760 peer->confirm--;
1761 }
1762
1763 return 1;
1764}
1765
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001766/*
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001767 * Read and parse a first line of a "hello" peer protocol message.
1768 * Returns 0 if could not read a line, -1 if there was a read error or
1769 * the line is malformed, 1 if succeeded.
1770 */
1771static inline int peer_getline_version(struct appctx *appctx,
1772 unsigned int *maj_ver, unsigned int *min_ver)
1773{
1774 int reql;
1775
1776 reql = peer_getline(appctx);
1777 if (!reql)
1778 return 0;
1779
1780 if (reql < 0)
1781 return -1;
1782
1783 /* test protocol */
1784 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
1785 appctx->st0 = PEER_SESS_ST_EXIT;
1786 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1787 return -1;
1788 }
1789 if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 ||
1790 *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) {
1791 appctx->st0 = PEER_SESS_ST_EXIT;
1792 appctx->st1 = PEER_SESS_SC_ERRVERSION;
1793 return -1;
1794 }
1795
1796 return 1;
1797}
1798
1799/*
1800 * Read and parse a second line of a "hello" peer protocol message.
1801 * Returns 0 if could not read a line, -1 if there was a read error or
1802 * the line is malformed, 1 if succeeded.
1803 */
1804static inline int peer_getline_host(struct appctx *appctx)
1805{
1806 int reql;
1807
1808 reql = peer_getline(appctx);
1809 if (!reql)
1810 return 0;
1811
1812 if (reql < 0)
1813 return -1;
1814
1815 /* test hostname match */
1816 if (strcmp(localpeer, trash.area) != 0) {
1817 appctx->st0 = PEER_SESS_ST_EXIT;
1818 appctx->st1 = PEER_SESS_SC_ERRHOST;
1819 return -1;
1820 }
1821
1822 return 1;
1823}
1824
1825/*
1826 * Read and parse a last line of a "hello" peer protocol message.
1827 * Returns 0 if could not read a character, -1 if there was a read error or
1828 * the line is malformed, 1 if succeeded.
1829 * Set <curpeer> accordingly (the remote peer sending the "hello" message).
1830 */
1831static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer)
1832{
1833 char *p;
1834 int reql;
1835 struct peer *peer;
1836 struct stream_interface *si = appctx->owner;
1837 struct stream *s = si_strm(si);
1838 struct peers *peers = strm_fe(s)->parent;
1839
1840 reql = peer_getline(appctx);
1841 if (!reql)
1842 return 0;
1843
1844 if (reql < 0)
1845 return -1;
1846
1847 /* parse line "<peer name> <pid> <relative_pid>" */
1848 p = strchr(trash.area, ' ');
1849 if (!p) {
1850 appctx->st0 = PEER_SESS_ST_EXIT;
1851 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1852 return -1;
1853 }
1854 *p = 0;
1855
1856 /* lookup known peer */
1857 for (peer = peers->remote; peer; peer = peer->next) {
1858 if (strcmp(peer->id, trash.area) == 0)
1859 break;
1860 }
1861
1862 /* if unknown peer */
1863 if (!peer) {
1864 appctx->st0 = PEER_SESS_ST_EXIT;
1865 appctx->st1 = PEER_SESS_SC_ERRPEER;
1866 return -1;
1867 }
1868 *curpeer = peer;
1869
1870 return 1;
1871}
1872
1873/*
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01001874 * Init <peer> peer after having accepted it at peer protocol level.
1875 */
1876static inline void init_accepted_peer(struct peer *peer, struct peers *peers)
1877{
1878 struct shared_table *st;
1879
1880 /* Register status code */
1881 peer->statuscode = PEER_SESS_SC_SUCCESSCODE;
1882
1883 /* Awake main task */
1884 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
1885
1886 /* Init confirm counter */
1887 peer->confirm = 0;
1888
1889 /* Init cursors */
1890 for (st = peer->tables; st ; st = st->next) {
1891 st->last_get = st->last_acked = 0;
1892 st->teaching_origin = st->last_pushed = st->update;
1893 }
1894
1895 /* reset teaching and learning flags to 0 */
1896 peer->flags &= PEER_TEACH_RESET;
1897 peer->flags &= PEER_LEARN_RESET;
1898
1899 /* if current peer is local */
1900 if (peer->local) {
1901 /* if current host need resyncfrom local and no process assined */
1902 if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1903 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1904 /* assign local peer for a lesson, consider lesson already requested */
1905 peer->flags |= PEER_F_LEARN_ASSIGN;
1906 peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1907 }
1908
1909 }
1910 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1911 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1912 /* assign peer for a lesson */
1913 peer->flags |= PEER_F_LEARN_ASSIGN;
1914 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1915 }
1916}
1917
1918/*
1919 * Init <peer> peer after having connected it at peer protocol level.
1920 */
1921static inline void init_connected_peer(struct peer *peer, struct peers *peers)
1922{
1923 struct shared_table *st;
1924
1925 /* Init cursors */
1926 for (st = peer->tables; st ; st = st->next) {
1927 st->last_get = st->last_acked = 0;
1928 st->teaching_origin = st->last_pushed = st->update;
1929 }
1930
1931 /* Init confirm counter */
1932 peer->confirm = 0;
1933
1934 /* reset teaching and learning flags to 0 */
1935 peer->flags &= PEER_TEACH_RESET;
1936 peer->flags &= PEER_LEARN_RESET;
1937
1938 /* If current peer is local */
1939 if (peer->local) {
1940 /* flag to start to teach lesson */
1941 peer->flags |= PEER_F_TEACH_PROCESS;
1942 }
1943 else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1944 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
1945 /* If peer is remote and resync from remote is needed,
1946 and no peer currently assigned */
1947
1948 /* assign peer for a lesson */
1949 peer->flags |= PEER_F_LEARN_ASSIGN;
1950 peers->flags |= PEERS_F_RESYNC_ASSIGN;
1951 }
1952}
1953
1954/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001955 * IO Handler to handle message exchance with a peer
1956 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001957static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001958{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001959 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02001960 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001961 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02001962 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001963 int reql = 0;
1964 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001965 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001966 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02001967
Joseph Herlant82b2f542018-11-15 12:19:14 -08001968 /* Check if the input buffer is available. */
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01001969 if (si_ic(si)->buf.size == 0) {
1970 si_rx_room_blk(si);
1971 goto out;
1972 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001973
Emeric Brun2b920a12010-09-23 18:30:22 +02001974 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001975 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001976switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001977 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001978 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001979 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001980 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001981 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001982 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001983 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001984 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001985 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001986 reql = peer_getline_version(appctx, &maj_ver, &min_ver);
1987 if (reql <= 0) {
1988 if (!reql)
1989 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001990 goto switchstate;
1991 }
1992
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001993 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001994 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001995 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001996 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01001997 reql = peer_getline_host(appctx);
1998 if (reql <= 0) {
1999 if (!reql)
2000 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002001 goto switchstate;
2002 }
2003
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002004 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02002005 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002006 case PEER_SESS_ST_GETPEER: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002007 prev_state = appctx->st0;
Frédéric Lécaille3f0fb9d2019-01-25 08:30:29 +01002008 reql = peer_getline_last(appctx, &curpeer);
2009 if (reql <= 0) {
2010 if (!reql)
2011 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002012 goto switchstate;
2013 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002014
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002015 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002016 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002017 if (curpeer->local) {
2018 /* Local connection, reply a retry */
2019 appctx->st0 = PEER_SESS_ST_EXIT;
2020 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
2021 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002022 }
Emeric Brun80527f52017-06-19 17:46:37 +02002023
2024 /* we're killing a connection, we must apply a random delay before
2025 * retrying otherwise the other end will do the same and we can loop
2026 * for a while.
2027 */
2028 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002029 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002030 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002031 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
2032 if (min_ver == PEER_DWNGRD_MINOR_VER) {
2033 curpeer->flags |= PEER_F_DWNGRD;
2034 }
2035 else {
2036 curpeer->flags &= ~PEER_F_DWNGRD;
2037 }
2038 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002039 curpeer->appctx = appctx;
2040 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002041 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +01002042 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02002043 /* fall through */
2044 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002045 case PEER_SESS_ST_SENDSUCCESS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002046 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002047 if (!curpeer) {
2048 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002049 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002050 if (curpeer->appctx != appctx) {
2051 appctx->st0 = PEER_SESS_ST_END;
2052 goto switchstate;
2053 }
2054 }
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002055
2056 repl = peer_send_status_successmsg(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002057 if (repl <= 0) {
2058 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002059 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002060 goto switchstate;
2061 }
2062
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002063 init_accepted_peer(curpeer, curpeers);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002064
Emeric Brun2b920a12010-09-23 18:30:22 +02002065 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +01002066 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002067 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002068 goto switchstate;
2069 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002070 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002071 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002072 if (!curpeer) {
2073 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002074 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002075 if (curpeer->appctx != appctx) {
2076 appctx->st0 = PEER_SESS_ST_END;
2077 goto switchstate;
2078 }
2079 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002080
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002081 repl = peer_send_hellomsg(appctx, curpeer);
Emeric Brun2b920a12010-09-23 18:30:22 +02002082 if (repl <= 0) {
2083 if (repl == -1)
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002084 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002085 goto switchstate;
2086 }
2087
2088 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002089 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02002090 /* fall through */
2091 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002092 case PEER_SESS_ST_GETSTATUS: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002093 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002094 if (!curpeer) {
2095 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002096 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002097 if (curpeer->appctx != appctx) {
2098 appctx->st0 = PEER_SESS_ST_END;
2099 goto switchstate;
2100 }
2101 }
2102
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002103 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02002104 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002105
Frédéric Lécaillece025572019-01-21 13:38:06 +01002106 reql = peer_getline(appctx);
2107 if (!reql)
2108 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002109
Frédéric Lécaillece025572019-01-21 13:38:06 +01002110 if (reql < 0)
2111 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02002112
2113 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002114 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02002115
2116 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02002117 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02002118
2119 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002120 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Frédéric Lécaille4b2fd9b2019-01-25 08:58:41 +01002121 init_connected_peer(curpeer, curpeers);
Emeric Brun2b920a12010-09-23 18:30:22 +02002122 }
2123 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02002124 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
2125 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02002126 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002127 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02002128 goto switchstate;
2129 }
Willy Tarreau2d372c22018-11-05 17:12:27 +01002130 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002131 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02002132 /* fall through */
2133 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002134 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002135 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002136 char *msg_cur = trash.area;
2137 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002138 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02002139 int totl = 0;
2140
Willy Tarreau2d372c22018-11-05 17:12:27 +01002141 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002142 if (!curpeer) {
2143 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002144 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002145 if (curpeer->appctx != appctx) {
2146 appctx->st0 = PEER_SESS_ST_END;
2147 goto switchstate;
2148 }
2149 }
2150
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002151 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
2152 if (reql <= 0) {
2153 if (reql == -1)
2154 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002155 goto send_msgs;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002156 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01002157
Frédéric Lécaille95203f22019-01-23 19:38:11 +01002158 msg_end += msg_len;
Frédéric Lécaille444243c2019-01-24 15:40:11 +01002159 if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl))
Emeric Brun2b920a12010-09-23 18:30:22 +02002160 goto switchstate;
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002161
Emeric Brun2b920a12010-09-23 18:30:22 +02002162 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002163 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02002164 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02002165 goto switchstate;
2166
Frédéric Lécaillebe825e52019-01-24 18:28:44 +01002167send_msgs:
2168 /* we get here when a peer_recv_msg() returns 0 in reql */
Frédéric Lécaille25e1d5e2019-01-24 17:33:48 +01002169 repl = peer_send_msgs(appctx, curpeer);
2170 if (repl <= 0) {
2171 if (repl == -1)
2172 goto out;
2173 goto switchstate;
Emeric Brun597b26e2016-08-12 11:23:31 +02002174 }
2175
Emeric Brun2b920a12010-09-23 18:30:22 +02002176 /* noting more to do */
2177 goto out;
2178 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002179 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01002180 if (prev_state == PEER_SESS_ST_WAITMSG)
2181 HA_ATOMIC_SUB(&connected_peers, 1);
2182 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002183 if (peer_send_status_errormsg(appctx) == -1)
2184 goto out;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002185 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002186 goto switchstate;
2187 case PEER_SESS_ST_ERRSIZE: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002188 if (prev_state == PEER_SESS_ST_WAITMSG)
2189 HA_ATOMIC_SUB(&connected_peers, 1);
2190 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002191 if (peer_send_error_size_limitmsg(appctx) == -1)
2192 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002193 appctx->st0 = PEER_SESS_ST_END;
2194 goto switchstate;
2195 }
2196 case PEER_SESS_ST_ERRPROTO: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002197 if (prev_state == PEER_SESS_ST_WAITMSG)
2198 HA_ATOMIC_SUB(&connected_peers, 1);
2199 prev_state = appctx->st0;
Frédéric Lécaille7d0ceee2019-01-24 14:24:05 +01002200 if (peer_send_error_protomsg(appctx) == -1)
2201 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002202 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01002203 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002204 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002205 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002206 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01002207 if (prev_state == PEER_SESS_ST_WAITMSG)
2208 HA_ATOMIC_SUB(&connected_peers, 1);
2209 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02002210 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002211 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002212 curpeer = NULL;
2213 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002214 si_shutw(si);
2215 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002216 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002217 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002218 }
2219 }
2220 }
2221out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002222 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002223
2224 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002225 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002226 return;
2227}
2228
Willy Tarreau30576452015-04-13 13:50:30 +02002229static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002230 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002231 .name = "<PEER>", /* used for logging */
2232 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002233 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002234};
Emeric Brun2b920a12010-09-23 18:30:22 +02002235
2236/*
2237 * Use this function to force a close of a peer session
2238 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002239static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002240{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002241 /* Note that the peer sessions which have just been created
2242 * (->st0 == PEER_SESS_ST_CONNECT) must not
2243 * be shutdown, if not, the TCP session will never be closed
2244 * and stay in CLOSE_WAIT state after having been closed by
2245 * the remote side.
2246 */
2247 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002248 return;
2249
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002250 if (appctx->applet != &peer_applet)
2251 return;
2252
Willy Tarreau2d372c22018-11-05 17:12:27 +01002253 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
2254 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002255 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002256 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002257}
2258
Willy Tarreau91d96282015-03-13 15:47:26 +01002259/* Pre-configures a peers frontend to accept incoming connections */
2260void peers_setup_frontend(struct proxy *fe)
2261{
2262 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002263 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002264 fe->maxconn = 0;
2265 fe->conn_retries = CONN_RETRIES;
2266 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002267 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002268 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002269 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002270 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002271}
2272
Emeric Brun2b920a12010-09-23 18:30:22 +02002273/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002274 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002275 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002276static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002277{
Willy Tarreau04b92862017-09-15 11:01:04 +02002278 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002279 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002280 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002281 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002282 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002283 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002284
Emeric Brunb3971ab2015-05-12 18:49:09 +02002285 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
2286 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002287 s = NULL;
2288
Emeric Brun1138fd02017-06-19 12:38:55 +02002289 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002290 if (!appctx)
2291 goto out_close;
2292
2293 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002294 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002295
Willy Tarreau04b92862017-09-15 11:01:04 +02002296 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002297 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002298 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002299 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002300 }
2301
Willy Tarreau87787ac2017-08-28 16:22:54 +02002302 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002303 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002304 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002305 }
2306
Willy Tarreau342bfb12015-04-05 01:35:34 +02002307 /* The tasks below are normally what is supposed to be done by
2308 * fe->accept().
2309 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002310 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002311
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002312 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002313 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002314 appctx_wakeup(appctx);
2315
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002316 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002317 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002318 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002319
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002320 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002321 * pre-initialized connection in si->conn.
2322 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002323 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002324 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002325
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002326 if (unlikely((cs = cs_new(conn)) == NULL))
2327 goto out_free_conn;
2328
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002329 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002330 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2331
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002332 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01002333 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002334 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002335
Emeric Brun2b920a12010-09-23 18:30:22 +02002336 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002337 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002338
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002339 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002340
Emeric Brunb3971ab2015-05-12 18:49:09 +02002341 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002342 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01002343 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002344 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002345
2346 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002347 out_free_conn:
2348 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002349 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002350 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002351 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002352 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002353 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002354 out_free_appctx:
2355 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002356 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002357 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002358}
2359
2360/*
2361 * Task processing function to manage re-connect and peer session
2362 * tasks wakeup on local update.
2363 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002364static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002365{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002366 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002367 struct peer *ps;
2368 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002369
2370 task->expire = TICK_ETERNITY;
2371
Emeric Brunb3971ab2015-05-12 18:49:09 +02002372 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002373 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002374 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002375 task_delete(peers->sync_task);
2376 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002377 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002378 return NULL;
2379 }
2380
Emeric Brun80527f52017-06-19 17:46:37 +02002381 /* Acquire lock for all peers of the section */
2382 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002383 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002384
Emeric Brun2b920a12010-09-23 18:30:22 +02002385 if (!stopping) {
2386 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002387
2388 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2389 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2390 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002391 /* Resync from local peer needed
2392 no peer was assigned for the lesson
2393 and no old local peer found
2394 or resync timeout expire */
2395
2396 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002397 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002398
2399 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002400 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002401 }
2402
2403 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002404 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002405 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002406 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002407 if (!ps->appctx) {
2408 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002409 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002410 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002411 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002412 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002413 tick_is_expired(ps->reconnect, now_ms))) {
2414 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002415 * or previous peer connection established with success
2416 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002417 * and reconnection timer is expired */
2418
2419 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002420 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002421 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002422 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002423 /* If previous session failed during connection
2424 * but reconnection timer is not expired */
2425
2426 /* reschedule task for reconnect */
2427 task->expire = tick_first(task->expire, ps->reconnect);
2428 }
2429 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002430 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002431 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002432 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002433 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2434 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002435 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2436 /* Resync from a remote is needed
2437 * and no peer was assigned for lesson
2438 * and current peer may be up2date */
2439
2440 /* assign peer for the lesson */
2441 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002442 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002443
Willy Tarreau9df94c22016-10-31 18:42:52 +01002444 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002445 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002446 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002447 else {
2448 /* Awake session if there is data to push */
2449 for (st = ps->tables; st ; st = st->next) {
2450 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002451 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002452 appctx_wakeup(ps->appctx);
2453 break;
2454 }
2455 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002456 }
2457 /* else do nothing */
2458 } /* SUCCESSCODE */
2459 } /* !ps->peer->local */
2460 } /* for */
2461
2462 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002463 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2464 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2465 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002466 /* Resync from remote peer needed
2467 * no peer was assigned for the lesson
2468 * and resync timeout expire */
2469
2470 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002471 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002472 }
2473
Emeric Brunb3971ab2015-05-12 18:49:09 +02002474 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002475 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002476 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2477 if (!tick_is_expired(peers->resync_timeout, now_ms))
2478 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002479 }
2480 } /* !stopping */
2481 else {
2482 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002483 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002484 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002485 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002486 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002487 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002488 peers->flags |= PEERS_F_DONOTSTOP;
2489 ps = peers->local;
2490 for (st = ps->tables; st ; st = st->next)
2491 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002492 }
2493
2494 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002495 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002496 /* we're killing a connection, we must apply a random delay before
2497 * retrying otherwise the other end will do the same and we can loop
2498 * for a while.
2499 */
2500 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002501 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002502 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002503 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002504 }
2505 }
2506 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002507
Emeric Brunb3971ab2015-05-12 18:49:09 +02002508 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002509 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002510 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002511 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002512 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002513 peers->flags &= ~PEERS_F_DONOTSTOP;
2514 for (st = ps->tables; st ; st = st->next)
2515 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002516 }
2517 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002518 else if (!ps->appctx) {
2519 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002520 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002521 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2522 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2523 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002524 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002525 * or previous peer connection was successfully established
2526 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002527 * or during previous connect, peer replies a try again statuscode */
2528
2529 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002530 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002531 }
2532 else {
2533 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002534 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002535 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002536 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002537 peers->flags &= ~PEERS_F_DONOTSTOP;
2538 for (st = ps->tables; st ; st = st->next)
2539 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002540 }
2541 }
2542 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002543 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002544 /* current peer connection is active and established
2545 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002546 for (st = ps->tables; st ; st = st->next) {
2547 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002548 appctx_wakeup(ps->appctx);
2549 break;
2550 }
2551 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002552 }
2553 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002554
2555 /* Release lock for all peers of the section */
2556 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002557 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002558
Emeric Brun2b920a12010-09-23 18:30:22 +02002559 /* Wakeup for re-connect */
2560 return task;
2561}
2562
Emeric Brunb3971ab2015-05-12 18:49:09 +02002563
Emeric Brun2b920a12010-09-23 18:30:22 +02002564/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002565 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002566 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002567int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002568{
Emeric Brun2b920a12010-09-23 18:30:22 +02002569 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002570 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002571
Emeric Brun2b920a12010-09-23 18:30:22 +02002572 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002573 peers->peers_fe->maxconn += 3;
2574 }
2575
Willy Tarreau4348fad2012-09-20 16:48:07 +02002576 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2577 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002578 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002579 if (!peers->sync_task)
2580 return 0;
2581
Emeric Brunb3971ab2015-05-12 18:49:09 +02002582 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002583 peers->sync_task->context = (void *)peers;
2584 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2585 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002586 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002587}
2588
2589
2590
2591/*
2592 * Function used to register a table for sync on a group of peers
2593 *
2594 */
2595void peers_register_table(struct peers *peers, struct stktable *table)
2596{
2597 struct shared_table *st;
2598 struct peer * curpeer;
2599 int id = 0;
2600
2601 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002602 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002603 st->table = table;
2604 st->next = curpeer->tables;
2605 if (curpeer->tables)
2606 id = curpeer->tables->local_id;
2607 st->local_id = id + 1;
2608
2609 curpeer->tables = st;
2610 }
2611
2612 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002613}
2614