blob: 1378852fc6bb55c814f8fca04a4bf8b4fc77db7d [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
118
119/*******************************/
120/* stick table sync mesg types */
121/* Note: ids >= 128 contains */
122/* id message cotains data */
123/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200124#define PEER_MSG_STKT_UPDATE 0x80
125#define PEER_MSG_STKT_INCUPDATE 0x81
126#define PEER_MSG_STKT_DEFINE 0x82
127#define PEER_MSG_STKT_SWITCH 0x83
128#define PEER_MSG_STKT_ACK 0x84
129#define PEER_MSG_STKT_UPDATE_TIMED 0x85
130#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200131
132/**********************************/
133/* Peer Session IO handler states */
134/**********************************/
135
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100136enum {
137 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
138 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
139 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
140 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100141 /* after this point, data were possibly exchanged */
142 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
143 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
144 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
145 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
146 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200147 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
148 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100149 PEER_SESS_ST_END, /* Killed session */
150};
Emeric Brun2b920a12010-09-23 18:30:22 +0200151
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100152/***************************************************/
153/* Peer Session status code - part of the protocol */
154/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200155
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100156#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
157#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200158
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100159#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200160
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100161#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200162
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100163#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
164#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
165#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
166#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200167
168#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200169#define PEER_MAJOR_VER 2
170#define PEER_MINOR_VER 1
171#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200172
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200173struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100174static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200175
Emeric Brun18928af2017-03-29 16:32:53 +0200176/* This function encode an uint64 to 'dynamic' length format.
177 The encoded value is written at address *str, and the
178 caller must assure that size after *str is large enought.
179 At return, the *str is set at the next Byte after then
180 encoded integer. The function returns then length of the
181 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200182int intencode(uint64_t i, char **str) {
183 int idx = 0;
184 unsigned char *msg;
185
186 if (!*str)
187 return 0;
188
189 msg = (unsigned char *)*str;
190 if (i < 240) {
191 msg[0] = (unsigned char)i;
192 *str = (char *)&msg[idx+1];
193 return (idx+1);
194 }
195
196 msg[idx] =(unsigned char)i | 240;
197 i = (i - 240) >> 4;
198 while (i >= 128) {
199 msg[++idx] = (unsigned char)i | 128;
200 i = (i - 128) >> 7;
201 }
202 msg[++idx] = (unsigned char)i;
203 *str = (char *)&msg[idx+1];
204 return (idx+1);
205}
206
207
208/* This function returns the decoded integer or 0
209 if decode failed
210 *str point on the beginning of the integer to decode
211 at the end of decoding *str point on the end of the
212 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200213uint64_t intdecode(char **str, char *end)
214{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200215 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200216 uint64_t i;
217 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200218
219 if (!*str)
220 return 0;
221
222 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200223 if (msg >= (unsigned char *)end)
224 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200225
Emeric Brun18928af2017-03-29 16:32:53 +0200226 i = *(msg++);
227 if (i >= 240) {
228 shift = 4;
229 do {
230 if (msg >= (unsigned char *)end)
231 goto fail;
232 i += (uint64_t)*msg << shift;
233 shift += 7;
234 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200235 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100236 *str = (char *)msg;
237 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200238
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100239 fail:
240 *str = NULL;
241 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200242}
Emeric Brun2b920a12010-09-23 18:30:22 +0200243
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200244/* Set the stick-table UPDATE message type byte at <msg_type> address,
245 * depending on <use_identifier> and <use_timed> boolean parameters.
246 * Always successful.
247 */
248static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
249{
250 if (use_timed) {
251 if (use_identifier)
252 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
253 else
254 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
255 }
256 else {
257 if (use_identifier)
258 *msg_type = PEER_MSG_STKT_UPDATE;
259 else
260 *msg_type = PEER_MSG_STKT_INCUPDATE;
261 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200262}
Emeric Brun2b920a12010-09-23 18:30:22 +0200263/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200264 * This prepare the data update message on the stick session <ts>, <st> is the considered
265 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800266 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200267 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
268 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200269 */
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100270static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size,
271 unsigned int updateid, int use_identifier, int use_timed)
Emeric Brun2b920a12010-09-23 18:30:22 +0200272{
273 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200274 unsigned short datalen;
275 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200276 unsigned int data_type;
277 void *data_ptr;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200278
279 cursor = datamsg = msg + 1 + 5;
280
Emeric Brun2b920a12010-09-23 18:30:22 +0200281 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200282
283 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200284 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200285 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200286 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200287
288 /* encode update identifier if needed */
289 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200290 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200291 memcpy(cursor, &netinteger, sizeof(netinteger));
292 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200293 }
294
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200295 if (use_timed) {
296 netinteger = htonl(tick_remain(now_ms, ts->expire));
297 memcpy(cursor, &netinteger, sizeof(netinteger));
298 cursor += sizeof(netinteger);
299 }
300
Emeric Brunb3971ab2015-05-12 18:49:09 +0200301 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200302 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200303 int stlen = strlen((char *)ts->key.key);
304
Emeric Brunb3971ab2015-05-12 18:49:09 +0200305 intencode(stlen, &cursor);
306 memcpy(cursor, ts->key.key, stlen);
307 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200308 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200309 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200310 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200311 memcpy(cursor, &netinteger, sizeof(netinteger));
312 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 }
314 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200315 memcpy(cursor, ts->key.key, st->table->key_size);
316 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200317 }
318
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200320 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200321 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200322
Emeric Brun94900952015-06-11 18:25:54 +0200323 data_ptr = stktable_data_ptr(st->table, ts, data_type);
324 if (data_ptr) {
325 switch (stktable_data_types[data_type].std_type) {
326 case STD_T_SINT: {
327 int data;
328
329 data = stktable_data_cast(data_ptr, std_t_sint);
330 intencode(data, &cursor);
331 break;
332 }
333 case STD_T_UINT: {
334 unsigned int data;
335
336 data = stktable_data_cast(data_ptr, std_t_uint);
337 intencode(data, &cursor);
338 break;
339 }
340 case STD_T_ULL: {
341 unsigned long long data;
342
343 data = stktable_data_cast(data_ptr, std_t_ull);
344 intencode(data, &cursor);
345 break;
346 }
347 case STD_T_FRQP: {
348 struct freq_ctr_period *frqp;
349
350 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
351 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
352 intencode(frqp->curr_ctr, &cursor);
353 intencode(frqp->prev_ctr, &cursor);
354 break;
355 }
356 }
357 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200358 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100359 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200360
361 /* Compute datalen */
362 datalen = (cursor - datamsg);
363
364 /* prepare message header */
365 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200366 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200367 cursor = &msg[2];
368 intencode(datalen, &cursor);
369
370 /* move data after header */
371 memmove(cursor, datamsg, datalen);
372
373 /* return header size + data_len */
374 return (cursor - msg) + datalen;
375}
376
377/*
378 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800379 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200380 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
381 * check size)
382 */
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100383static int peer_prepare_switchmsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size,
384 unsigned int param1, int param2, int param3)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200385{
386 int len;
387 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200388 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200389 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200390 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200391 unsigned int data_type;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200392
393 cursor = datamsg = msg + 2 + 5;
394
395 /* Encode data */
396
397 /* encode local id */
398 intencode(st->local_id, &cursor);
399
400 /* encode table name */
401 len = strlen(st->table->id);
402 intencode(len, &cursor);
403 memcpy(cursor, st->table->id, len);
404 cursor += len;
405
406 /* encode table type */
407
408 intencode(st->table->type, &cursor);
409
410 /* encode table key size */
411 intencode(st->table->key_size, &cursor);
412
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200413 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200415 /* encode available known data types in table */
416 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
417 if (st->table->data_ofs[data_type]) {
418 switch (stktable_data_types[data_type].std_type) {
419 case STD_T_SINT:
420 case STD_T_UINT:
421 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200422 data |= 1 << data_type;
423 break;
Emeric Brun94900952015-06-11 18:25:54 +0200424 case STD_T_FRQP:
425 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200426 intencode(data_type, &chunkq);
427 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200428 break;
429 }
430 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200431 }
432 intencode(data, &cursor);
433
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200434 /* Encode stick-table entries duration. */
435 intencode(st->table->expire, &cursor);
436
437 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200438 chunk->data = chunkq - chunkp;
439 memcpy(cursor, chunk->area, chunk->data);
440 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200441 }
442
Emeric Brunb3971ab2015-05-12 18:49:09 +0200443 /* Compute datalen */
444 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200445
Emeric Brunb3971ab2015-05-12 18:49:09 +0200446 /* prepare message header */
447 msg[0] = PEER_MSG_CLASS_STICKTABLE;
448 msg[1] = PEER_MSG_STKT_DEFINE;
449 cursor = &msg[2];
450 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200451
Emeric Brunb3971ab2015-05-12 18:49:09 +0200452 /* move data after header */
453 memmove(cursor, datamsg, datalen);
454
455 /* return header size + data_len */
456 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200457}
458
Emeric Brunb3971ab2015-05-12 18:49:09 +0200459/*
460 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
461 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800462 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200463 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
464 * check size)
465 */
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100466static int peer_prepare_ackmsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size,
467 unsigned int param1, int param2, int param3)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200468{
469 unsigned short datalen;
470 char *cursor, *datamsg;
471 uint32_t netinteger;
472
Emeric Brunb058f1c2015-09-22 15:50:18 +0200473 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200474
475 intencode(st->remote_id, &cursor);
476 netinteger = htonl(st->last_get);
477 memcpy(cursor, &netinteger, sizeof(netinteger));
478 cursor += sizeof(netinteger);
479
480 /* Compute datalen */
481 datalen = (cursor - datamsg);
482
483 /* prepare message header */
484 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200485 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200486 cursor = &msg[2];
487 intencode(datalen, &cursor);
488
489 /* move data after header */
490 memmove(cursor, datamsg, datalen);
491
492 /* return header size + data_len */
493 return (cursor - msg) + datalen;
494}
Emeric Brun2b920a12010-09-23 18:30:22 +0200495
496/*
497 * Callback to release a session with a peer
498 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200499static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200500{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200501 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200502 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200503 struct peer *peer = appctx->ctx.peers.ptr;
504 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200505
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100506 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100507 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200508 return;
509
510 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200511 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100512 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
513 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100514 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100515 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100516 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200517 /* Re-init current table pointers to force announcement on re-connect */
518 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200519 peer->appctx = NULL;
520 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200521 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200522 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
523 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200524
525 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200526 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200527 }
528 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200529 peer->flags &= PEER_TEACH_RESET;
530 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200531 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100532 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200533 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200534 }
535}
536
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200537/* Retrieve the major and minor versions of peers protocol
538 * announced by a remote peer. <str> is a null-terminated
539 * string with the following format: "<maj_ver>.<min_ver>".
540 */
541static int peer_get_version(const char *str,
542 unsigned int *maj_ver, unsigned int *min_ver)
543{
544 unsigned int majv, minv;
545 const char *pos, *saved;
546 const char *end;
547
548 saved = pos = str;
549 end = str + strlen(str);
550
551 majv = read_uint(&pos, end);
552 if (saved == pos || *pos++ != '.')
553 return -1;
554
555 saved = pos;
556 minv = read_uint(&pos, end);
557 if (saved == pos || pos != end)
558 return -1;
559
560 *maj_ver = majv;
561 *min_ver = minv;
562
563 return 0;
564}
Emeric Brun2b920a12010-09-23 18:30:22 +0200565
566/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100567 * Parse a line terminated by an optional '\r' character, followed by a mandatory
568 * '\n' character.
569 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
570 * a line could not be read because the communication channel is closed.
571 */
572static inline int peer_getline(struct appctx *appctx)
573{
574 int n;
575 struct stream_interface *si = appctx->owner;
576
577 n = co_getline(si_oc(si), trash.area, trash.size);
578 if (!n)
579 return 0;
580
581 if (n < 0 || trash.area[n - 1] != '\n') {
582 appctx->st0 = PEER_SESS_ST_END;
583 return -1;
584 }
585
586 if (n > 1 && (trash.area[n - 2] == '\r'))
587 trash.area[n - 2] = 0;
588 else
589 trash.area[n - 1] = 0;
590
591 co_skip(si_oc(si), n);
592
593 return n;
594}
595
596/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100597 * Send a message after having called <peer_prepare_msg> to build it.
598 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
599 * Returns -1 if there was not enough room left to send the message,
600 * any other negative returned value must be considered as an error with an appcxt st0
601 * returned value equal to PEER_SESS_ST_END.
602 */
603static inline int peer_send_msg(struct shared_table *st, struct appctx *appctx,
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100604 int (*peer_prepare_msg)(struct stksess *, struct shared_table *,
605 char *, size_t,
606 unsigned int, int, int),
607 struct stksess *ts, unsigned int param1, int param2, int param3)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100608{
609 int ret, msglen;
610 struct stream_interface *si = appctx->owner;
611
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100612 msglen = peer_prepare_msg(ts, st, trash.area, trash.size, param1, param2, param3);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100613 if (!msglen) {
614 /* internal error: message does not fit in trash */
615 appctx->st0 = PEER_SESS_ST_END;
616 return 0;
617 }
618
619 /* message to buffer */
620 ret = ci_putblk(si_ic(si), trash.area, msglen);
621 if (ret <= 0) {
622 if (ret == -1) {
623 /* No more write possible */
624 si_rx_room_blk(si);
625 return -1;
626 }
627 appctx->st0 = PEER_SESS_ST_END;
628 }
629
630 return ret;
631}
632
633/*
634 * Send a stick-table switch message.
635 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
636 * Returns -1 if there was not enough room left to send the message,
637 * any other negative returned value must be considered as an error with an appcxt st0
638 * returned value equal to PEER_SESS_ST_END.
639 */
640static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
641{
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100642 return peer_send_msg(st, appctx, peer_prepare_switchmsg, NULL, -1, -1, -1);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100643}
644
645/*
646 * Send a stick-table update acknowledgement message.
647 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
648 * Returns -1 if there was not enough room left to send the message,
649 * any other negative returned value must be considered as an error with an appcxt st0
650 * returned value equal to PEER_SESS_ST_END.
651 */
652static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
653{
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100654 return peer_send_msg(st, appctx, peer_prepare_ackmsg, NULL, -1, -1, -1);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100655}
656
657/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100658 * Send a stick-table update message.
659 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
660 * Returns -1 if there was not enough room left to send the message,
661 * any other negative returned value must be considered as an error with an appcxt st0
662 * returned value equal to PEER_SESS_ST_END.
663 */
664static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
665 unsigned int updateid, int use_identifier, int use_timed)
666{
667 return peer_send_msg(st, appctx, peer_prepare_updatemsg, ts, updateid, use_identifier, use_timed);
668}
669
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100670
671/*
672 * Function used to lookup for recent stick-table updates associated with
673 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
674 */
675static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
676{
677 struct eb32_node *eb;
678
679 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
680 if (!eb) {
681 eb = eb32_first(&st->table->updates);
682 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
683 st->table->commitupdate = st->last_pushed = st->table->localupdate;
684 return NULL;
685 }
686 }
687
688 if ((int)(eb->key - st->table->localupdate) > 0) {
689 st->table->commitupdate = st->last_pushed = st->table->localupdate;
690 return NULL;
691 }
692
693 return eb32_entry(eb, struct stksess, upd);
694}
695
696/*
697 * Function used to lookup for recent stick-table updates associated with
698 * <st> shared stick-table during teach state 1 step.
699 */
700static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
701{
702 struct eb32_node *eb;
703
704 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
705 if (!eb) {
706 st->flags |= SHTABLE_F_TEACH_STAGE1;
707 eb = eb32_first(&st->table->updates);
708 if (eb)
709 st->last_pushed = eb->key - 1;
710 return NULL;
711 }
712
713 return eb32_entry(eb, struct stksess, upd);
714}
715
716/*
717 * Function used to lookup for recent stick-table updates associated with
718 * <st> shared stick-table during teach state 2 step.
719 */
720static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
721{
722 struct eb32_node *eb;
723
724 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
725 if (!eb || eb->key > st->teaching_origin) {
726 st->flags |= SHTABLE_F_TEACH_STAGE2;
727 return NULL;
728 }
729
730 return eb32_entry(eb, struct stksess, upd);
731}
732
733/*
734 * Generic function to emit update messages for <st> stick-table when a lesson must
735 * be taught to the peer <p>.
736 * <locked> must be set to 1 if the shared table <st> is already locked when entering
737 * this function, 0 if not.
738 *
739 * This function temporary unlock/lock <st> when it sends stick-table updates or
740 * when decrementing its refcount in case of any error when it sends this updates.
741 *
742 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
743 * Returns -1 if there was not enough room left to send the message,
744 * any other negative returned value must be considered as an error with an appcxt st0
745 * returned value equal to PEER_SESS_ST_END.
746 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
747 * unlocked if not already locked when entering this function.
748 */
749static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
750 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
751 struct shared_table *st, int locked)
752{
753 int ret, new_pushed, use_timed;
754
755 ret = 1;
756 use_timed = 0;
757 if (st != p->last_local_table) {
758 ret = peer_send_switchmsg(st, appctx);
759 if (ret <= 0)
760 return ret;
761
762 p->last_local_table = st;
763 }
764
765 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
766 use_timed = !(p->flags & PEER_F_DWNGRD);
767
768 /* We force new pushed to 1 to force identifier in update message */
769 new_pushed = 1;
770
771 if (!locked)
772 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
773
774 while (1) {
775 struct stksess *ts;
776 unsigned updateid;
777
778 /* push local updates */
779 ts = peer_stksess_lookup(st);
780 if (!ts)
781 break;
782
783 updateid = ts->upd.key;
784 ts->ref_cnt++;
785 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
786
787 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
788 if (ret <= 0) {
789 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
790 ts->ref_cnt--;
791 if (!locked)
792 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
793 return ret;
794 }
795
796 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
797 ts->ref_cnt--;
798 st->last_pushed = updateid;
799
800 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
801 (int)(st->last_pushed - st->table->commitupdate) > 0)
802 st->table->commitupdate = st->last_pushed;
803
804 /* identifier may not needed in next update message */
805 new_pushed = 0;
806 }
807
808 out:
809 if (!locked)
810 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
811 return 1;
812}
813
814/*
815 * Function to emit update messages for <st> stick-table when a lesson must
816 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
817 *
818 * Note that <st> shared stick-table is locked when calling this function.
819 *
820 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
821 * Returns -1 if there was not enough room left to send the message,
822 * any other negative returned value must be considered as an error with an appcxt st0
823 * returned value equal to PEER_SESS_ST_END.
824 */
825static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
826 struct shared_table *st)
827{
828 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
829}
830
831/*
832 * Function to emit update messages for <st> stick-table when a lesson must
833 * be taught to the peer <p> during teach state 1 step.
834 *
835 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
836 * Returns -1 if there was not enough room left to send the message,
837 * any other negative returned value must be considered as an error with an appcxt st0
838 * returned value equal to PEER_SESS_ST_END.
839 */
840static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
841 struct shared_table *st)
842{
843 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
844}
845
846/*
847 * Function to emit update messages for <st> stick-table when a lesson must
848 * be taught to the peer <p> during teach state 1 step.
849 *
850 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
851 * Returns -1 if there was not enough room left to send the message,
852 * any other negative returned value must be considered as an error with an appcxt st0
853 * returned value equal to PEER_SESS_ST_END.
854 */
855static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
856 struct shared_table *st)
857{
858 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
859}
860
Frédéric Lécaille168a34b2019-01-23 11:16:57 +0100861
862/*
863 * Function used to parse a stick-table update message after it has been received
864 * by <p> peer with <msg_cur> as address of the pointer to the position in the
865 * receipt buffer with <msg_end> being position of the end of the stick-table message.
866 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
867 * was encountered.
868 * <exp> must be set if the stick-table entry expires.
869 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
870 * messages, in this case the stick-table udpate message is received with a stick-table
871 * update ID.
872 * <totl> is the length of the stick-table update message computed upon receipt.
873 */
874static int peer_recv_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
875 char **msg_cur, char *msg_end, int msg_len, int totl)
876{
877 struct stream_interface *si = appctx->owner;
878 struct shared_table *st = p->remote_table;
879 struct stksess *ts, *newts;
880 uint32_t update;
881 int expire;
882 unsigned int data_type;
883 void *data_ptr;
884
885 /* Here we have data message */
886 if (!st)
887 goto ignore_msg;
888
889 expire = MS_TO_TICKS(st->table->expire);
890
891 if (updt) {
892 if (msg_len < sizeof(update)) {
893 /* malformed message */
894 appctx->st0 = PEER_SESS_ST_ERRPROTO;
895 return 0;
896 }
897 memcpy(&update, *msg_cur, sizeof(update));
898 *msg_cur += sizeof(update);
899 st->last_get = htonl(update);
900 }
901 else {
902 st->last_get++;
903 }
904
905 if (exp) {
906 size_t expire_sz = sizeof expire;
907
908 if (*msg_cur + expire_sz > msg_end) {
909 appctx->st0 = PEER_SESS_ST_ERRPROTO;
910 return 0;
911 }
912 memcpy(&expire, *msg_cur, expire_sz);
913 *msg_cur += expire_sz;
914 expire = ntohl(expire);
915 }
916
917 newts = stksess_new(st->table, NULL);
918 if (!newts)
919 goto ignore_msg;
920
921 if (st->table->type == SMP_T_STR) {
922 unsigned int to_read, to_store;
923
924 to_read = intdecode(msg_cur, msg_end);
925 if (!*msg_cur) {
926 /* malformed message */
927 stksess_free(st->table, newts);
928 appctx->st0 = PEER_SESS_ST_ERRPROTO;
929 return 0;
930 }
931 to_store = MIN(to_read, st->table->key_size - 1);
932 if (*msg_cur + to_store > msg_end) {
933 /* malformed message */
934 stksess_free(st->table, newts);
935 appctx->st0 = PEER_SESS_ST_ERRPROTO;
936 return 0;
937 }
938
939 memcpy(newts->key.key, *msg_cur, to_store);
940 newts->key.key[to_store] = 0;
941 *msg_cur += to_read;
942 }
943 else if (st->table->type == SMP_T_SINT) {
944 unsigned int netinteger;
945
946 if (*msg_cur + sizeof(netinteger) > msg_end) {
947 /* malformed message */
948 stksess_free(st->table, newts);
949 appctx->st0 = PEER_SESS_ST_ERRPROTO;
950 return 0;
951 }
952 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
953 netinteger = ntohl(netinteger);
954 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
955 *msg_cur += sizeof(netinteger);
956 }
957 else {
958 if (*msg_cur + st->table->key_size > msg_end) {
959 /* malformed message */
960 stksess_free(st->table, newts);
961 appctx->st0 = PEER_SESS_ST_ERRPROTO;
962 return 0;
963 }
964 memcpy(newts->key.key, *msg_cur, st->table->key_size);
965 *msg_cur += st->table->key_size;
966 }
967
968 /* lookup for existing entry */
969 ts = stktable_set_entry(st->table, newts);
970 if (ts != newts) {
971 stksess_free(st->table, newts);
972 newts = NULL;
973 }
974
975 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
976
977 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
978
979 if (!((1 << data_type) & st->remote_data))
980 continue;
981
982 switch (stktable_data_types[data_type].std_type) {
983 case STD_T_SINT: {
984 int data;
985
986 data = intdecode(msg_cur, msg_end);
987 if (!*msg_cur) {
988 /* malformed message */
989 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
990 stktable_touch_remote(st->table, ts, 1);
991 appctx->st0 = PEER_SESS_ST_ERRPROTO;
992 return 0;
993 }
994
995 data_ptr = stktable_data_ptr(st->table, ts, data_type);
996 if (data_ptr)
997 stktable_data_cast(data_ptr, std_t_sint) = data;
998 break;
999 }
1000 case STD_T_UINT: {
1001 unsigned int data;
1002
1003 data = intdecode(msg_cur, msg_end);
1004 if (!*msg_cur) {
1005 /* malformed message */
1006 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1007 stktable_touch_remote(st->table, ts, 1);
1008 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1009 return 0;
1010 }
1011
1012 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1013 if (data_ptr)
1014 stktable_data_cast(data_ptr, std_t_uint) = data;
1015 break;
1016 }
1017 case STD_T_ULL: {
1018 unsigned long long data;
1019
1020 data = intdecode(msg_cur, msg_end);
1021 if (!*msg_cur) {
1022 /* malformed message */
1023 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1024 stktable_touch_remote(st->table, ts, 1);
1025 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1026 return 0;
1027 }
1028
1029 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1030 if (data_ptr)
1031 stktable_data_cast(data_ptr, std_t_ull) = data;
1032 break;
1033 }
1034 case STD_T_FRQP: {
1035 struct freq_ctr_period data;
1036
1037 /* First bit is reserved for the freq_ctr_period lock
1038 Note: here we're still protected by the stksess lock
1039 so we don't need to update the update the freq_ctr_period
1040 using its internal lock */
1041
1042 data.curr_tick = tick_add(now_ms, -intdecode(msg_cur, msg_end)) & ~0x1;
1043 if (!*msg_cur) {
1044 /* malformed message */
1045 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1046 stktable_touch_remote(st->table, ts, 1);
1047 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1048 return 0;
1049 }
1050 data.curr_ctr = intdecode(msg_cur, msg_end);
1051 if (!*msg_cur) {
1052 /* malformed message */
1053 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1054 stktable_touch_remote(st->table, ts, 1);
1055 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1056 return 0;
1057 }
1058 data.prev_ctr = intdecode(msg_cur, msg_end);
1059 if (!*msg_cur) {
1060 /* malformed message */
1061 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1062 stktable_touch_remote(st->table, ts, 1);
1063 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1064 return 0;
1065 }
1066
1067 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1068 if (data_ptr)
1069 stktable_data_cast(data_ptr, std_t_frqp) = data;
1070 break;
1071 }
1072 }
1073 }
1074 /* Force new expiration */
1075 ts->expire = tick_add(now_ms, expire);
1076
1077 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1078 stktable_touch_remote(st->table, ts, 1);
1079 return 1;
1080
1081 ignore_msg:
1082 /* skip consumed message */
1083 co_skip(si_oc(si), totl);
1084 return 0;
1085}
1086
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001087/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001088 * Function used to parse a stick-table update acknowledgement message after it
1089 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1090 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1091 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1092 * was encountered.
1093 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1094 */
1095static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1096 char **msg_cur, char *msg_end)
1097{
1098 /* ack message */
1099 uint32_t table_id ;
1100 uint32_t update;
1101 struct shared_table *st;
1102
1103 table_id = intdecode(msg_cur, msg_end);
1104 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1105 /* malformed message */
1106 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1107 return 0;
1108 }
1109
1110 memcpy(&update, *msg_cur, sizeof(update));
1111 update = ntohl(update);
1112
1113 for (st = p->tables; st; st = st->next) {
1114 if (st->local_id == table_id) {
1115 st->update = update;
1116 break;
1117 }
1118 }
1119
1120 return 1;
1121}
1122
1123/*
1124 * Function used to parse a stick-table switch message after it has been received
1125 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1126 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1127 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1128 * was encountered.
1129 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1130 */
1131static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1132 char **msg_cur, char *msg_end)
1133{
1134 struct shared_table *st;
1135 int table_id;
1136
1137 table_id = intdecode(msg_cur, msg_end);
1138 if (!*msg_cur) {
1139 /* malformed message */
1140 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1141 return 0;
1142 }
1143
1144 p->remote_table = NULL;
1145 for (st = p->tables; st; st = st->next) {
1146 if (st->remote_id == table_id) {
1147 p->remote_table = st;
1148 break;
1149 }
1150 }
1151
1152 return 1;
1153}
1154
1155/*
1156 * Function used to parse a stick-table definition message after it has been received
1157 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1158 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1159 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1160 * was encountered.
1161 * <totl> is the length of the stick-table update message computed upon receipt.
1162 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1163 */
1164static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1165 char **msg_cur, char *msg_end, int totl)
1166{
1167 struct stream_interface *si = appctx->owner;
1168 int table_id_len;
1169 struct shared_table *st;
1170 int table_type;
1171 int table_keylen;
1172 int table_id;
1173 uint64_t table_data;
1174
1175 table_id = intdecode(msg_cur, msg_end);
1176 if (!*msg_cur) {
1177 /* malformed message */
1178 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1179 return 0;
1180 }
1181
1182 table_id_len = intdecode(msg_cur, msg_end);
1183 if (!*msg_cur) {
1184 /* malformed message */
1185 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1186 return 0;
1187 }
1188
1189 p->remote_table = NULL;
1190 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1191 /* malformed message */
1192 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1193 return 0;
1194 }
1195
1196 for (st = p->tables; st; st = st->next) {
1197 /* Reset IDs */
1198 if (st->remote_id == table_id)
1199 st->remote_id = 0;
1200
1201 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1202 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1203 p->remote_table = st;
1204 }
1205
1206 if (!p->remote_table)
1207 goto ignore_msg;
1208
1209 *msg_cur += table_id_len;
1210 if (*msg_cur >= msg_end) {
1211 /* malformed message */
1212 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1213 return 0;
1214 }
1215
1216 table_type = intdecode(msg_cur, msg_end);
1217 if (!*msg_cur) {
1218 /* malformed message */
1219 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1220 return 0;
1221 }
1222
1223 table_keylen = intdecode(msg_cur, msg_end);
1224 if (!*msg_cur) {
1225 /* malformed message */
1226 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1227 return 0;
1228 }
1229
1230 table_data = intdecode(msg_cur, msg_end);
1231 if (!*msg_cur) {
1232 /* malformed message */
1233 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1234 return 0;
1235 }
1236
1237 if (p->remote_table->table->type != table_type
1238 || p->remote_table->table->key_size != table_keylen) {
1239 p->remote_table = NULL;
1240 goto ignore_msg;
1241 }
1242
1243 p->remote_table->remote_data = table_data;
1244 p->remote_table->remote_id = table_id;
1245 return 1;
1246
1247 ignore_msg:
1248 co_skip(si_oc(si), totl);
1249 return 0;
1250}
1251
1252/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001253 * Receive a stick-table message.
1254 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1255 * -1 if there was an error updating the appctx state st0 accordingly.
1256 */
1257static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1258 uint32_t *msg_len, int *totl)
1259{
1260 int reql;
1261 struct stream_interface *si = appctx->owner;
1262
1263 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1264 if (reql <= 0) /* closed or EOL not found */
1265 goto incomplete;
1266
1267 *totl += reql;
1268
1269 if ((unsigned int)msg_head[1] < 128)
1270 return 1;
1271
1272 /* Read and Decode message length */
1273 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1274 if (reql <= 0) /* closed */
1275 goto incomplete;
1276
1277 *totl += reql;
1278
1279 if ((unsigned int)msg_head[2] < 240) {
1280 *msg_len = msg_head[2];
1281 }
1282 else {
1283 int i;
1284 char *cur;
1285 char *end;
1286
1287 for (i = 3 ; i < msg_head_sz ; i++) {
1288 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1289 if (reql <= 0) /* closed */
1290 goto incomplete;
1291
1292 *totl += reql;
1293
1294 if (!(msg_head[i] & 0x80))
1295 break;
1296 }
1297
1298 if (i == msg_head_sz) {
1299 /* malformed message */
1300 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1301 return -1;
1302 }
1303 end = msg_head + msg_head_sz;
1304 cur = &msg_head[2];
1305 *msg_len = intdecode(&cur, end);
1306 if (!cur) {
1307 /* malformed message */
1308 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1309 return -1;
1310 }
1311 }
1312
1313 /* Read message content */
1314 if (*msg_len) {
1315 if (*msg_len > trash.size) {
1316 /* Status code is not success, abort */
1317 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1318 return -1;
1319 }
1320
1321 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1322 if (reql <= 0) /* closed */
1323 goto incomplete;
1324 *totl += reql;
1325 }
1326
1327 return 1;
1328
1329 incomplete:
1330 if (reql < 0) {
1331 /* there was an error */
1332 appctx->st0 = PEER_SESS_ST_END;
1333 return -1;
1334 }
1335
1336 return 0;
1337}
1338/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001339 * IO Handler to handle message exchance with a peer
1340 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001341static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001342{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001343 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02001344 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001345 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02001346 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001347 int reql = 0;
1348 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001349 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
1350 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001351 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02001352
Joseph Herlant82b2f542018-11-15 12:19:14 -08001353 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001354 if (si_ic(si)->buf.size == 0)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001355 goto full;
1356
Emeric Brun2b920a12010-09-23 18:30:22 +02001357 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001358 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001359switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001360 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001361 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001362 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001363 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001364 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001365 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001366 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001367 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001368 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001369
Frédéric Lécaillece025572019-01-21 13:38:06 +01001370 reql = peer_getline(appctx);
1371 if (!reql)
1372 goto out;
1373
1374 if (reql < 0)
1375 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001376
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001377 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001378 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001379 appctx->st0 = PEER_SESS_ST_EXIT;
1380 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1381 goto switchstate;
1382 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001383 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001384 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001385 appctx->st0 = PEER_SESS_ST_EXIT;
1386 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001387 goto switchstate;
1388 }
1389
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001390 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001391 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001392 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001393 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001394
Frédéric Lécaillece025572019-01-21 13:38:06 +01001395 reql = peer_getline(appctx);
1396 if (!reql)
1397 goto out;
1398
1399 if (reql < 0)
1400 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001401
1402 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001403 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001404 appctx->st0 = PEER_SESS_ST_EXIT;
1405 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001406 goto switchstate;
1407 }
1408
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001409 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02001410 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001411 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +02001412 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001413
1414 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001415
Frédéric Lécaillece025572019-01-21 13:38:06 +01001416 reql = peer_getline(appctx);
1417 if (!reql)
1418 goto out;
1419
1420 if (reql < 0)
1421 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001422
Emeric Brunb3971ab2015-05-12 18:49:09 +02001423 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001424 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +02001425 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001426 appctx->st0 = PEER_SESS_ST_EXIT;
1427 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001428 goto switchstate;
1429 }
1430 *p = 0;
1431
1432 /* lookup known peer */
1433 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001434 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +02001435 break;
1436 }
1437
1438 /* if unknown peer */
1439 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001440 appctx->st0 = PEER_SESS_ST_EXIT;
1441 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02001442 goto switchstate;
1443 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001444
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001445 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001446 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001447 if (curpeer->local) {
1448 /* Local connection, reply a retry */
1449 appctx->st0 = PEER_SESS_ST_EXIT;
1450 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
1451 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001452 }
Emeric Brun80527f52017-06-19 17:46:37 +02001453
1454 /* we're killing a connection, we must apply a random delay before
1455 * retrying otherwise the other end will do the same and we can loop
1456 * for a while.
1457 */
1458 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001459 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001460 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001461 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
1462 if (min_ver == PEER_DWNGRD_MINOR_VER) {
1463 curpeer->flags |= PEER_F_DWNGRD;
1464 }
1465 else {
1466 curpeer->flags &= ~PEER_F_DWNGRD;
1467 }
1468 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001469 curpeer->appctx = appctx;
1470 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001471 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +01001472 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001473 /* fall through */
1474 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001475 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001476 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001477
Willy Tarreau2d372c22018-11-05 17:12:27 +01001478 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001479 if (!curpeer) {
1480 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001481 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001482 if (curpeer->appctx != appctx) {
1483 appctx->st0 = PEER_SESS_ST_END;
1484 goto switchstate;
1485 }
1486 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001487 repl = snprintf(trash.area, trash.size,
1488 "%d\n",
1489 PEER_SESS_SC_SUCCESSCODE);
1490 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001491 if (repl <= 0) {
1492 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001493 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001494 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001495 goto switchstate;
1496 }
1497
1498 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001499 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001500
1501 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001502 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001503
1504 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001505 curpeer->confirm = 0;
1506
1507 /* Init cursors */
1508 for (st = curpeer->tables; st ; st = st->next) {
1509 st->last_get = st->last_acked = 0;
1510 st->teaching_origin = st->last_pushed = st->update;
1511 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001512
1513 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001514 curpeer->flags &= PEER_TEACH_RESET;
1515 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001516
1517 /* if current peer is local */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001518 if (curpeer->local) {
1519 /* if current host need resyncfrom local and no process assined */
1520 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1521 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1522 /* assign local peer for a lesson, consider lesson already requested */
1523 curpeer->flags |= PEER_F_LEARN_ASSIGN;
1524 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1525 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001526
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001527 }
1528 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1529 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1530 /* assign peer for a lesson */
1531 curpeer->flags |= PEER_F_LEARN_ASSIGN;
1532 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
1533 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001534
1535
Emeric Brun2b920a12010-09-23 18:30:22 +02001536 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +01001537 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001538 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02001539 goto switchstate;
1540 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001541 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001542 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001543 if (!curpeer) {
1544 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001545 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001546 if (curpeer->appctx != appctx) {
1547 appctx->st0 = PEER_SESS_ST_END;
1548 goto switchstate;
1549 }
1550 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001551
1552 /* Send headers */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001553 repl = snprintf(trash.area, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001554 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
1555 PEER_MAJOR_VER,
1556 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +02001557 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +02001558 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +01001559 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +02001560 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +02001561
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001562 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001563 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001564 goto switchstate;
1565 }
1566
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001568 if (repl <= 0) {
1569 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001570 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001571 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001572 goto switchstate;
1573 }
1574
1575 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001576 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02001577 /* fall through */
1578 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001579 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001580 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001581
Willy Tarreau2d372c22018-11-05 17:12:27 +01001582 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001583 if (!curpeer) {
1584 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001585 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001586 if (curpeer->appctx != appctx) {
1587 appctx->st0 = PEER_SESS_ST_END;
1588 goto switchstate;
1589 }
1590 }
1591
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001592 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001593 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001594
Frédéric Lécaillece025572019-01-21 13:38:06 +01001595 reql = peer_getline(appctx);
1596 if (!reql)
1597 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001598
Frédéric Lécaillece025572019-01-21 13:38:06 +01001599 if (reql < 0)
1600 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001601
1602 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001603 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02001604
1605 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001606 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001607
1608 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001609 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001610 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001611 for (st = curpeer->tables; st ; st = st->next) {
1612 st->last_get = st->last_acked = 0;
1613 st->teaching_origin = st->last_pushed = st->update;
1614 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001615
1616 /* Init confirm counter */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001617 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001618
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001619 /* reset teaching and learning flags to 0 */
1620 curpeer->flags &= PEER_TEACH_RESET;
1621 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001622
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001623 /* If current peer is local */
1624 if (curpeer->local) {
1625 /* flag to start to teach lesson */
1626 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +02001627
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001628 }
1629 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1630 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1631 /* If peer is remote and resync from remote is needed,
1632 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +02001633
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001634 /* assign peer for a lesson */
1635 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001636 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001637 }
1638
1639 }
1640 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001641 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
1642 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02001643 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001644 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001645 goto switchstate;
1646 }
Willy Tarreau2d372c22018-11-05 17:12:27 +01001647 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001648 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02001649 /* fall through */
1650 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001651 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001652 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001653 char *msg_cur = trash.area;
1654 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001655 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02001656 int totl = 0;
1657
Willy Tarreau2d372c22018-11-05 17:12:27 +01001658 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001659 if (!curpeer) {
1660 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001661 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001662 if (curpeer->appctx != appctx) {
1663 appctx->st0 = PEER_SESS_ST_END;
1664 goto switchstate;
1665 }
1666 }
1667
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001668 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
1669 if (reql <= 0) {
1670 if (reql == -1)
1671 goto switchstate;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001672 goto incomplete;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001673 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01001674
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001675 msg_end += msg_len;
1676
Emeric Brunb3971ab2015-05-12 18:49:09 +02001677 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1678 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1679 struct shared_table *st;
1680 /* Reset message: remote need resync */
1681
1682 /* prepare tables fot a global push */
1683 for (st = curpeer->tables; st; st = st->next) {
1684 st->teaching_origin = st->last_pushed = st->table->update;
1685 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001686 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001687
Emeric Brunb3971ab2015-05-12 18:49:09 +02001688 /* reset teaching flags to 0 */
1689 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001690
Emeric Brunb3971ab2015-05-12 18:49:09 +02001691 /* flag to start to teach lesson */
1692 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001693 }
1694 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001695 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1696 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001697 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1698 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001699 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001700 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001701 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001702 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001703 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1704 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001705 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001706
1707 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001708 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1709 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001710 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001711 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001712 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001713 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001714 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001715
1716 /* If stopping state */
1717 if (stopping) {
1718 /* Close session, push resync no more needed */
1719 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1720 appctx->st0 = PEER_SESS_ST_END;
1721 goto switchstate;
1722 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001723 for (st = curpeer->tables; st; st = st->next) {
1724 st->update = st->last_pushed = st->teaching_origin;
1725 st->flags = 0;
1726 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001727
1728 /* reset teaching flags to 0 */
1729 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001730 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001731 }
1732 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1733 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001734 if (!peer_treat_definemsg(appctx, curpeer, &msg_cur, msg_end, totl))
1735 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001736 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001737 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001738 if (!peer_treat_switchmsg(appctx, curpeer, &msg_cur, msg_end))
Emeric Brunb3971ab2015-05-12 18:49:09 +02001739 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001740 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001741 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001742 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1743 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1744 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001745 int update, expire;
Emeric Brun2b920a12010-09-23 18:30:22 +02001746
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001747 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1748 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1749 if (!peer_recv_updatemsg(appctx, curpeer, update, expire,
1750 &msg_cur, msg_end, msg_len, totl))
1751 goto switchstate;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001752
Emeric Brunb3971ab2015-05-12 18:49:09 +02001753 }
1754 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001755 if (!peer_treat_ackmsg(appctx, curpeer, &msg_cur, msg_end))
Emeric Brunb3971ab2015-05-12 18:49:09 +02001756 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001757 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001758 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001759 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1760 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001761 goto switchstate;
1762 }
1763
Emeric Brunb3971ab2015-05-12 18:49:09 +02001764ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001765 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001766 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001767 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001768 goto switchstate;
1769
Emeric Brun2b920a12010-09-23 18:30:22 +02001770incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001771 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001772
Willy Tarreau72d6c162013-04-11 16:14:13 +02001773 if (reql < 0) {
1774 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001775 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001776 goto switchstate;
1777 }
1778
Emeric Brun2b920a12010-09-23 18:30:22 +02001779
Emeric Brun2b920a12010-09-23 18:30:22 +02001780
Emeric Brunb3971ab2015-05-12 18:49:09 +02001781
Emeric Brun2b920a12010-09-23 18:30:22 +02001782 /* Need to request a resync */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001783 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1784 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1785 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001786 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001787
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001788 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001789 msg[0] = PEER_MSG_CLASS_CONTROL;
1790 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001791
Emeric Brunb3971ab2015-05-12 18:49:09 +02001792 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001793 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001794 if (repl <= 0) {
1795 /* no more write possible */
1796 if (repl == -1)
1797 goto full;
1798 appctx->st0 = PEER_SESS_ST_END;
1799 goto switchstate;
1800 }
1801 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
1802 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001803
Emeric Brunb3971ab2015-05-12 18:49:09 +02001804 /* Nothing to read, now we start to write */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001805 if (curpeer->tables) {
1806 struct shared_table *st;
1807 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001808
Emeric Brunb3971ab2015-05-12 18:49:09 +02001809 last_local_table = curpeer->last_local_table;
1810 if (!last_local_table)
1811 last_local_table = curpeer->tables;
1812 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001813
Emeric Brunb3971ab2015-05-12 18:49:09 +02001814 while (1) {
1815 if (!st)
1816 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001817
Emeric Brunb3971ab2015-05-12 18:49:09 +02001818 /* It remains some updates to ack */
1819 if (st->last_get != st->last_acked) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001820 repl = peer_send_ackmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001821 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001822 if (repl == -1)
1823 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001824 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001825 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001826 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001827 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001828
Emeric Brunb3971ab2015-05-12 18:49:09 +02001829 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001830 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001831 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1832 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001833
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001834 repl = peer_send_teach_process_msgs(appctx, curpeer, st);
1835 if (repl <= 0) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001836 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001837 if (repl == -1)
1838 goto out;
1839 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001840 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001841 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001842 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001843 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001844 else {
1845 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001846 repl = peer_send_teach_stage1_msgs(appctx, curpeer, st);
1847 if (repl <= 0) {
1848 if (repl == -1)
1849 goto out;
1850 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001851 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001852 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001853
Emeric Brunb3971ab2015-05-12 18:49:09 +02001854 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001855 repl = peer_send_teach_stage2_msgs(appctx, curpeer, st);
1856 if (repl <= 0) {
1857 if (repl == -1)
1858 goto out;
1859 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001860 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001861 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001862 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001863
1864 if (st == last_local_table)
1865 break;
1866 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001867 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001868 }
1869
1870
1871 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1872 unsigned char msg[2];
1873
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001874 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001875 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001876 msg[1] = ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001877 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001878 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001879 if (repl <= 0) {
1880 /* no more write possible */
1881 if (repl == -1)
1882 goto full;
1883 appctx->st0 = PEER_SESS_ST_END;
1884 goto switchstate;
1885 }
1886 /* flag finished message sent */
1887 curpeer->flags |= PEER_F_TEACH_FINISHED;
1888 }
1889
Emeric Brun597b26e2016-08-12 11:23:31 +02001890 /* Confirm finished or partial messages */
1891 while (curpeer->confirm) {
1892 unsigned char msg[2];
1893
1894 /* There is a confirm messages to send */
1895 msg[0] = PEER_MSG_CLASS_CONTROL;
1896 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1897
1898 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001899 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001900 if (repl <= 0) {
1901 /* no more write possible */
1902 if (repl == -1)
1903 goto full;
1904 appctx->st0 = PEER_SESS_ST_END;
1905 goto switchstate;
1906 }
1907 curpeer->confirm--;
1908 }
1909
Emeric Brun2b920a12010-09-23 18:30:22 +02001910 /* noting more to do */
1911 goto out;
1912 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001913 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001914 if (prev_state == PEER_SESS_ST_WAITMSG)
1915 HA_ATOMIC_SUB(&connected_peers, 1);
1916 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001917 repl = snprintf(trash.area, trash.size,
1918 "%d\n", appctx->st1);
1919 if (ci_putblk(si_ic(si), trash.area, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001920 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001921 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001922 goto switchstate;
1923 case PEER_SESS_ST_ERRSIZE: {
1924 unsigned char msg[2];
1925
Willy Tarreau2d372c22018-11-05 17:12:27 +01001926 if (prev_state == PEER_SESS_ST_WAITMSG)
1927 HA_ATOMIC_SUB(&connected_peers, 1);
1928 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001929 msg[0] = PEER_MSG_CLASS_ERROR;
1930 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1931
Willy Tarreau06d80a92017-10-19 14:32:15 +02001932 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001933 goto full;
1934 appctx->st0 = PEER_SESS_ST_END;
1935 goto switchstate;
1936 }
1937 case PEER_SESS_ST_ERRPROTO: {
1938 unsigned char msg[2];
1939
Willy Tarreau2d372c22018-11-05 17:12:27 +01001940 if (prev_state == PEER_SESS_ST_WAITMSG)
1941 HA_ATOMIC_SUB(&connected_peers, 1);
1942 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001943 msg[0] = PEER_MSG_CLASS_ERROR;
1944 msg[1] = PEER_MSG_ERR_PROTOCOL;
1945
Willy Tarreau06d80a92017-10-19 14:32:15 +02001946 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001947 goto full;
1948 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001949 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001950 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001951 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001952 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001953 if (prev_state == PEER_SESS_ST_WAITMSG)
1954 HA_ATOMIC_SUB(&connected_peers, 1);
1955 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001956 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001957 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001958 curpeer = NULL;
1959 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02001960 si_shutw(si);
1961 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001962 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02001963 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001964 }
1965 }
1966 }
1967out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001968 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02001969
1970 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001971 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001972 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001973full:
Willy Tarreaudb398432018-11-15 11:08:52 +01001974 si_rx_room_blk(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001975 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001976}
1977
Willy Tarreau30576452015-04-13 13:50:30 +02001978static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001979 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001980 .name = "<PEER>", /* used for logging */
1981 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07001982 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01001983};
Emeric Brun2b920a12010-09-23 18:30:22 +02001984
1985/*
1986 * Use this function to force a close of a peer session
1987 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001988static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001989{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02001990 /* Note that the peer sessions which have just been created
1991 * (->st0 == PEER_SESS_ST_CONNECT) must not
1992 * be shutdown, if not, the TCP session will never be closed
1993 * and stay in CLOSE_WAIT state after having been closed by
1994 * the remote side.
1995 */
1996 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001997 return;
1998
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001999 if (appctx->applet != &peer_applet)
2000 return;
2001
Willy Tarreau2d372c22018-11-05 17:12:27 +01002002 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
2003 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002004 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002005 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002006}
2007
Willy Tarreau91d96282015-03-13 15:47:26 +01002008/* Pre-configures a peers frontend to accept incoming connections */
2009void peers_setup_frontend(struct proxy *fe)
2010{
2011 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002012 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002013 fe->maxconn = 0;
2014 fe->conn_retries = CONN_RETRIES;
2015 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002016 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002017 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002018 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002019 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002020}
2021
Emeric Brun2b920a12010-09-23 18:30:22 +02002022/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002023 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002024 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002025static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002026{
Willy Tarreau04b92862017-09-15 11:01:04 +02002027 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002028 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002029 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002030 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002031 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002032 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002033
Emeric Brunb3971ab2015-05-12 18:49:09 +02002034 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
2035 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002036 s = NULL;
2037
Emeric Brun1138fd02017-06-19 12:38:55 +02002038 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002039 if (!appctx)
2040 goto out_close;
2041
2042 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002043 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002044
Willy Tarreau04b92862017-09-15 11:01:04 +02002045 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002046 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002047 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002048 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002049 }
2050
Willy Tarreau87787ac2017-08-28 16:22:54 +02002051 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002052 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002053 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002054 }
2055
Willy Tarreau342bfb12015-04-05 01:35:34 +02002056 /* The tasks below are normally what is supposed to be done by
2057 * fe->accept().
2058 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002059 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002060
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002061 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002062 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002063 appctx_wakeup(appctx);
2064
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002065 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002066 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002067 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002068
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002069 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002070 * pre-initialized connection in si->conn.
2071 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002072 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002073 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002074
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002075 if (unlikely((cs = cs_new(conn)) == NULL))
2076 goto out_free_conn;
2077
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002078 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002079 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2080
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002081 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01002082 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002083 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002084
Emeric Brun2b920a12010-09-23 18:30:22 +02002085 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002086 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002087
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002088 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002089
Emeric Brunb3971ab2015-05-12 18:49:09 +02002090 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002091 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01002092 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002093 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002094
2095 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002096 out_free_conn:
2097 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002098 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002099 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002100 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002101 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002102 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002103 out_free_appctx:
2104 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002105 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002106 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002107}
2108
2109/*
2110 * Task processing function to manage re-connect and peer session
2111 * tasks wakeup on local update.
2112 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002113static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002114{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002115 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002116 struct peer *ps;
2117 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002118
2119 task->expire = TICK_ETERNITY;
2120
Emeric Brunb3971ab2015-05-12 18:49:09 +02002121 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002122 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002123 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002124 task_delete(peers->sync_task);
2125 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002126 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002127 return NULL;
2128 }
2129
Emeric Brun80527f52017-06-19 17:46:37 +02002130 /* Acquire lock for all peers of the section */
2131 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002132 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002133
Emeric Brun2b920a12010-09-23 18:30:22 +02002134 if (!stopping) {
2135 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002136
2137 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2138 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2139 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002140 /* Resync from local peer needed
2141 no peer was assigned for the lesson
2142 and no old local peer found
2143 or resync timeout expire */
2144
2145 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002146 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002147
2148 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002149 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002150 }
2151
2152 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002153 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002154 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002155 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002156 if (!ps->appctx) {
2157 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002158 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002159 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002160 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002161 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002162 tick_is_expired(ps->reconnect, now_ms))) {
2163 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002164 * or previous peer connection established with success
2165 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002166 * and reconnection timer is expired */
2167
2168 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002169 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002170 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002171 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002172 /* If previous session failed during connection
2173 * but reconnection timer is not expired */
2174
2175 /* reschedule task for reconnect */
2176 task->expire = tick_first(task->expire, ps->reconnect);
2177 }
2178 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002179 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002180 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002181 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002182 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2183 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002184 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2185 /* Resync from a remote is needed
2186 * and no peer was assigned for lesson
2187 * and current peer may be up2date */
2188
2189 /* assign peer for the lesson */
2190 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002191 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002192
Willy Tarreau9df94c22016-10-31 18:42:52 +01002193 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002194 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002195 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002196 else {
2197 /* Awake session if there is data to push */
2198 for (st = ps->tables; st ; st = st->next) {
2199 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002200 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002201 appctx_wakeup(ps->appctx);
2202 break;
2203 }
2204 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002205 }
2206 /* else do nothing */
2207 } /* SUCCESSCODE */
2208 } /* !ps->peer->local */
2209 } /* for */
2210
2211 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002212 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2213 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2214 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002215 /* Resync from remote peer needed
2216 * no peer was assigned for the lesson
2217 * and resync timeout expire */
2218
2219 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002220 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002221 }
2222
Emeric Brunb3971ab2015-05-12 18:49:09 +02002223 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002224 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002225 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2226 if (!tick_is_expired(peers->resync_timeout, now_ms))
2227 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002228 }
2229 } /* !stopping */
2230 else {
2231 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002232 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002233 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002234 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002235 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002236 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002237 peers->flags |= PEERS_F_DONOTSTOP;
2238 ps = peers->local;
2239 for (st = ps->tables; st ; st = st->next)
2240 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002241 }
2242
2243 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002244 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002245 /* we're killing a connection, we must apply a random delay before
2246 * retrying otherwise the other end will do the same and we can loop
2247 * for a while.
2248 */
2249 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002250 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002251 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002252 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002253 }
2254 }
2255 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002256
Emeric Brunb3971ab2015-05-12 18:49:09 +02002257 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002258 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002259 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002260 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002261 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002262 peers->flags &= ~PEERS_F_DONOTSTOP;
2263 for (st = ps->tables; st ; st = st->next)
2264 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002265 }
2266 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002267 else if (!ps->appctx) {
2268 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002269 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002270 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2271 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2272 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002273 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002274 * or previous peer connection was successfully established
2275 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002276 * or during previous connect, peer replies a try again statuscode */
2277
2278 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002279 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002280 }
2281 else {
2282 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002283 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002284 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002285 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002286 peers->flags &= ~PEERS_F_DONOTSTOP;
2287 for (st = ps->tables; st ; st = st->next)
2288 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002289 }
2290 }
2291 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002292 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002293 /* current peer connection is active and established
2294 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002295 for (st = ps->tables; st ; st = st->next) {
2296 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002297 appctx_wakeup(ps->appctx);
2298 break;
2299 }
2300 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002301 }
2302 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002303
2304 /* Release lock for all peers of the section */
2305 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002306 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002307
Emeric Brun2b920a12010-09-23 18:30:22 +02002308 /* Wakeup for re-connect */
2309 return task;
2310}
2311
Emeric Brunb3971ab2015-05-12 18:49:09 +02002312
Emeric Brun2b920a12010-09-23 18:30:22 +02002313/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002314 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002315 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002316int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002317{
Emeric Brun2b920a12010-09-23 18:30:22 +02002318 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002319 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002320
Emeric Brun2b920a12010-09-23 18:30:22 +02002321 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002322 peers->peers_fe->maxconn += 3;
2323 }
2324
Willy Tarreau4348fad2012-09-20 16:48:07 +02002325 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2326 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002327 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002328 if (!peers->sync_task)
2329 return 0;
2330
Emeric Brunb3971ab2015-05-12 18:49:09 +02002331 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002332 peers->sync_task->context = (void *)peers;
2333 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2334 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002335 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002336}
2337
2338
2339
2340/*
2341 * Function used to register a table for sync on a group of peers
2342 *
2343 */
2344void peers_register_table(struct peers *peers, struct stktable *table)
2345{
2346 struct shared_table *st;
2347 struct peer * curpeer;
2348 int id = 0;
2349
2350 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002351 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002352 st->table = table;
2353 st->next = curpeer->tables;
2354 if (curpeer->tables)
2355 id = curpeer->tables->local_id;
2356 st->local_id = id + 1;
2357
2358 curpeer->tables = st;
2359 }
2360
2361 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002362}
2363