blob: 2f93197439974a41b5c27b0af92b28ca4551581a [file] [log] [blame]
Emeric Brun2b920a12010-09-23 18:30:22 +02001/*
Emeric Brunb3971ab2015-05-12 18:49:09 +02002 * Peer synchro management.
Emeric Brun2b920a12010-09-23 18:30:22 +02003 *
4 * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/time.h>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020026#include <common/standard.h>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020027#include <common/hathreads.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020028
29#include <types/global.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <types/listener.h>
31#include <types/obj_type.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020032#include <types/peers.h>
33
34#include <proto/acl.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020035#include <proto/applet.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020037#include <proto/fd.h>
Willy Tarreaud1d48d42015-03-13 16:15:46 +010038#include <proto/frontend.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020039#include <proto/log.h>
40#include <proto/hdr_idx.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020041#include <proto/mux_pt.h>
Frédéric Lécaille1055e682018-04-26 14:35:21 +020042#include <proto/peers.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020043#include <proto/proxy.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020044#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.h>
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010046#include <proto/signal.h>
47#include <proto/stick_table.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020048#include <proto/stream_interface.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020049#include <proto/task.h>
Emeric Brun2b920a12010-09-23 18:30:22 +020050
51
52/*******************************/
53/* Current peer learning state */
54/*******************************/
55
56/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020057/* Current peers section resync state */
Emeric Brun2b920a12010-09-23 18:30:22 +020058/******************************/
Emeric Brunb3971ab2015-05-12 18:49:09 +020059#define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */
60#define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */
61#define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */
62#define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */
63#define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop
Emeric Brun2b920a12010-09-23 18:30:22 +020064 to push data to new process */
65
Emeric Brunb3971ab2015-05-12 18:49:09 +020066#define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
67#define PEERS_RESYNC_FROMLOCAL 0x00000000
68#define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL
69#define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE)
70
71/***********************************/
72/* Current shared table sync state */
73/***********************************/
74#define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */
75#define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */
Emeric Brun2b920a12010-09-23 18:30:22 +020076
77/******************************/
78/* Remote peer teaching state */
79/******************************/
80#define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */
Emeric Brun2b920a12010-09-23 18:30:22 +020081#define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */
82#define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */
83#define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */
84#define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +020085#define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */
Emeric Brun2b920a12010-09-23 18:30:22 +020086
Emeric Brunb3971ab2015-05-12 18:49:09 +020087#define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */
Emeric Brun2b920a12010-09-23 18:30:22 +020088#define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE)
89
Emeric Brunb3971ab2015-05-12 18:49:09 +020090/*****************************/
91/* Sync message class */
92/*****************************/
93enum {
94 PEER_MSG_CLASS_CONTROL = 0,
95 PEER_MSG_CLASS_ERROR,
96 PEER_MSG_CLASS_STICKTABLE = 10,
97 PEER_MSG_CLASS_RESERVED = 255,
98};
99
100/*****************************/
101/* control message types */
102/*****************************/
103enum {
104 PEER_MSG_CTRL_RESYNCREQ = 0,
105 PEER_MSG_CTRL_RESYNCFINISHED,
106 PEER_MSG_CTRL_RESYNCPARTIAL,
107 PEER_MSG_CTRL_RESYNCCONFIRM,
108};
109
110/*****************************/
111/* error message types */
112/*****************************/
113enum {
114 PEER_MSG_ERR_PROTOCOL = 0,
115 PEER_MSG_ERR_SIZELIMIT,
116};
117
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100118/*
119 * Parameters used by functions to build peer protocol messages. */
120struct peer_prep_params {
121 struct {
122 struct stksess *stksess;
123 struct shared_table *shared_table;
124 unsigned int updateid;
125 int use_identifier;
126 int use_timed;
127 } updt;
128 struct {
129 struct shared_table *shared_table;
130 } swtch;
131 struct {
132 struct shared_table *shared_table;
133 } ack;
134};
Emeric Brunb3971ab2015-05-12 18:49:09 +0200135
136/*******************************/
137/* stick table sync mesg types */
138/* Note: ids >= 128 contains */
139/* id message cotains data */
140/*******************************/
Olivier Houchard33992262018-10-16 18:49:26 +0200141#define PEER_MSG_STKT_UPDATE 0x80
142#define PEER_MSG_STKT_INCUPDATE 0x81
143#define PEER_MSG_STKT_DEFINE 0x82
144#define PEER_MSG_STKT_SWITCH 0x83
145#define PEER_MSG_STKT_ACK 0x84
146#define PEER_MSG_STKT_UPDATE_TIMED 0x85
147#define PEER_MSG_STKT_INCUPDATE_TIMED 0x86
Emeric Brun2b920a12010-09-23 18:30:22 +0200148
149/**********************************/
150/* Peer Session IO handler states */
151/**********************************/
152
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100153enum {
154 PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */
155 PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */
156 PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */
157 PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100158 /* after this point, data were possibly exchanged */
159 PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */
160 PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */
161 PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */
162 PEER_SESS_ST_WAITMSG, /* Wait for data messages */
163 PEER_SESS_ST_EXIT, /* Exit with status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200164 PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */
165 PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100166 PEER_SESS_ST_END, /* Killed session */
167};
Emeric Brun2b920a12010-09-23 18:30:22 +0200168
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100169/***************************************************/
170/* Peer Session status code - part of the protocol */
171/***************************************************/
Emeric Brun2b920a12010-09-23 18:30:22 +0200172
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100173#define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */
174#define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */
Emeric Brun2b920a12010-09-23 18:30:22 +0200175
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100176#define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */
Emeric Brun2b920a12010-09-23 18:30:22 +0200177
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100178#define PEER_SESS_SC_TRYAGAIN 300 /* try again later */
Emeric Brun2b920a12010-09-23 18:30:22 +0200179
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100180#define PEER_SESS_SC_ERRPROTO 501 /* error protocol */
181#define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */
182#define PEER_SESS_SC_ERRHOST 503 /* bad host name */
183#define PEER_SESS_SC_ERRPEER 504 /* unknown peer */
Emeric Brun2b920a12010-09-23 18:30:22 +0200184
185#define PEER_SESSION_PROTO_NAME "HAProxyS"
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200186#define PEER_MAJOR_VER 2
187#define PEER_MINOR_VER 1
188#define PEER_DWNGRD_MINOR_VER 0
Emeric Brun2b920a12010-09-23 18:30:22 +0200189
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +0200190struct peers *cfg_peers = NULL;
Willy Tarreau81bc3b02016-10-31 17:37:39 +0100191static void peer_session_forceshutdown(struct appctx *appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +0200192
Emeric Brun18928af2017-03-29 16:32:53 +0200193/* This function encode an uint64 to 'dynamic' length format.
194 The encoded value is written at address *str, and the
195 caller must assure that size after *str is large enought.
196 At return, the *str is set at the next Byte after then
197 encoded integer. The function returns then length of the
198 encoded integer in Bytes */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200199int intencode(uint64_t i, char **str) {
200 int idx = 0;
201 unsigned char *msg;
202
203 if (!*str)
204 return 0;
205
206 msg = (unsigned char *)*str;
207 if (i < 240) {
208 msg[0] = (unsigned char)i;
209 *str = (char *)&msg[idx+1];
210 return (idx+1);
211 }
212
213 msg[idx] =(unsigned char)i | 240;
214 i = (i - 240) >> 4;
215 while (i >= 128) {
216 msg[++idx] = (unsigned char)i | 128;
217 i = (i - 128) >> 7;
218 }
219 msg[++idx] = (unsigned char)i;
220 *str = (char *)&msg[idx+1];
221 return (idx+1);
222}
223
224
225/* This function returns the decoded integer or 0
226 if decode failed
227 *str point on the beginning of the integer to decode
228 at the end of decoding *str point on the end of the
229 encoded integer or to null if end is reached */
Emeric Brun18928af2017-03-29 16:32:53 +0200230uint64_t intdecode(char **str, char *end)
231{
Emeric Brunb3971ab2015-05-12 18:49:09 +0200232 unsigned char *msg;
Emeric Brun18928af2017-03-29 16:32:53 +0200233 uint64_t i;
234 int shift;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200235
236 if (!*str)
237 return 0;
238
239 msg = (unsigned char *)*str;
Emeric Brun18928af2017-03-29 16:32:53 +0200240 if (msg >= (unsigned char *)end)
241 goto fail;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200242
Emeric Brun18928af2017-03-29 16:32:53 +0200243 i = *(msg++);
244 if (i >= 240) {
245 shift = 4;
246 do {
247 if (msg >= (unsigned char *)end)
248 goto fail;
249 i += (uint64_t)*msg << shift;
250 shift += 7;
251 } while (*(msg++) >= 128);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200252 }
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100253 *str = (char *)msg;
254 return i;
Emeric Brun18928af2017-03-29 16:32:53 +0200255
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +0100256 fail:
257 *str = NULL;
258 return 0;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200259}
Emeric Brun2b920a12010-09-23 18:30:22 +0200260
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200261/* Set the stick-table UPDATE message type byte at <msg_type> address,
262 * depending on <use_identifier> and <use_timed> boolean parameters.
263 * Always successful.
264 */
265static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed)
266{
267 if (use_timed) {
268 if (use_identifier)
269 *msg_type = PEER_MSG_STKT_UPDATE_TIMED;
270 else
271 *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED;
272 }
273 else {
274 if (use_identifier)
275 *msg_type = PEER_MSG_STKT_UPDATE;
276 else
277 *msg_type = PEER_MSG_STKT_INCUPDATE;
278 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200279}
Emeric Brun2b920a12010-09-23 18:30:22 +0200280/*
Emeric Brunb3971ab2015-05-12 18:49:09 +0200281 * This prepare the data update message on the stick session <ts>, <st> is the considered
282 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800283 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200284 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
285 * check size)
Emeric Brun2b920a12010-09-23 18:30:22 +0200286 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100287static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brun2b920a12010-09-23 18:30:22 +0200288{
289 uint32_t netinteger;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200290 unsigned short datalen;
291 char *cursor, *datamsg;
Emeric Brun94900952015-06-11 18:25:54 +0200292 unsigned int data_type;
293 void *data_ptr;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100294 struct stksess *ts;
295 struct shared_table *st;
296 unsigned int updateid;
297 int use_identifier;
298 int use_timed;
299
300 ts = p->updt.stksess;
301 st = p->updt.shared_table;
302 updateid = p->updt.updateid;
303 use_identifier = p->updt.use_identifier;
304 use_timed = p->updt.use_timed;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200305
306 cursor = datamsg = msg + 1 + 5;
307
Emeric Brun2b920a12010-09-23 18:30:22 +0200308 /* construct message */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200309
310 /* check if we need to send the update identifer */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200311 if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) {
Emeric Bruna6a09982015-09-22 15:34:19 +0200312 use_identifier = 1;
Emeric Brun2b920a12010-09-23 18:30:22 +0200313 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200314
315 /* encode update identifier if needed */
316 if (use_identifier) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200317 netinteger = htonl(updateid);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200318 memcpy(cursor, &netinteger, sizeof(netinteger));
319 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200320 }
321
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200322 if (use_timed) {
323 netinteger = htonl(tick_remain(now_ms, ts->expire));
324 memcpy(cursor, &netinteger, sizeof(netinteger));
325 cursor += sizeof(netinteger);
326 }
327
Emeric Brunb3971ab2015-05-12 18:49:09 +0200328 /* encode the key */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200329 if (st->table->type == SMP_T_STR) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200330 int stlen = strlen((char *)ts->key.key);
331
Emeric Brunb3971ab2015-05-12 18:49:09 +0200332 intencode(stlen, &cursor);
333 memcpy(cursor, ts->key.key, stlen);
334 cursor += stlen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200335 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200336 else if (st->table->type == SMP_T_SINT) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200337 netinteger = htonl(*((uint32_t *)ts->key.key));
Emeric Brunb3971ab2015-05-12 18:49:09 +0200338 memcpy(cursor, &netinteger, sizeof(netinteger));
339 cursor += sizeof(netinteger);
Emeric Brun2b920a12010-09-23 18:30:22 +0200340 }
341 else {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200342 memcpy(cursor, ts->key.key, st->table->key_size);
343 cursor += st->table->key_size;
Emeric Brun2b920a12010-09-23 18:30:22 +0200344 }
345
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200347 /* encode values */
Emeric Brun94900952015-06-11 18:25:54 +0200348 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
Emeric Brunb3971ab2015-05-12 18:49:09 +0200349
Emeric Brun94900952015-06-11 18:25:54 +0200350 data_ptr = stktable_data_ptr(st->table, ts, data_type);
351 if (data_ptr) {
352 switch (stktable_data_types[data_type].std_type) {
353 case STD_T_SINT: {
354 int data;
355
356 data = stktable_data_cast(data_ptr, std_t_sint);
357 intencode(data, &cursor);
358 break;
359 }
360 case STD_T_UINT: {
361 unsigned int data;
362
363 data = stktable_data_cast(data_ptr, std_t_uint);
364 intencode(data, &cursor);
365 break;
366 }
367 case STD_T_ULL: {
368 unsigned long long data;
369
370 data = stktable_data_cast(data_ptr, std_t_ull);
371 intencode(data, &cursor);
372 break;
373 }
374 case STD_T_FRQP: {
375 struct freq_ctr_period *frqp;
376
377 frqp = &stktable_data_cast(data_ptr, std_t_frqp);
378 intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor);
379 intencode(frqp->curr_ctr, &cursor);
380 intencode(frqp->prev_ctr, &cursor);
381 break;
382 }
383 }
384 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200385 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100386 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200387
388 /* Compute datalen */
389 datalen = (cursor - datamsg);
390
391 /* prepare message header */
392 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200393 peer_set_update_msg_type(&msg[1], use_identifier, use_timed);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200394 cursor = &msg[2];
395 intencode(datalen, &cursor);
396
397 /* move data after header */
398 memmove(cursor, datamsg, datalen);
399
400 /* return header size + data_len */
401 return (cursor - msg) + datalen;
402}
403
404/*
405 * This prepare the switch table message to targeted share table <st>.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800406 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200407 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
408 * check size)
409 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100410static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200411{
412 int len;
413 unsigned short datalen;
Willy Tarreau83061a82018-07-13 11:56:34 +0200414 struct buffer *chunk;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200415 char *cursor, *datamsg, *chunkp, *chunkq;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200416 uint64_t data = 0;
Emeric Brun94900952015-06-11 18:25:54 +0200417 unsigned int data_type;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100418 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200419
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100420 st = params->swtch.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200421 cursor = datamsg = msg + 2 + 5;
422
423 /* Encode data */
424
425 /* encode local id */
426 intencode(st->local_id, &cursor);
427
428 /* encode table name */
429 len = strlen(st->table->id);
430 intencode(len, &cursor);
431 memcpy(cursor, st->table->id, len);
432 cursor += len;
433
434 /* encode table type */
435
436 intencode(st->table->type, &cursor);
437
438 /* encode table key size */
439 intencode(st->table->key_size, &cursor);
440
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200441 chunk = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200442 chunkp = chunkq = chunk->area;
Emeric Brun94900952015-06-11 18:25:54 +0200443 /* encode available known data types in table */
444 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
445 if (st->table->data_ofs[data_type]) {
446 switch (stktable_data_types[data_type].std_type) {
447 case STD_T_SINT:
448 case STD_T_UINT:
449 case STD_T_ULL:
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200450 data |= 1 << data_type;
451 break;
Emeric Brun94900952015-06-11 18:25:54 +0200452 case STD_T_FRQP:
453 data |= 1 << data_type;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200454 intencode(data_type, &chunkq);
455 intencode(st->table->data_arg[data_type].u, &chunkq);
Emeric Brun94900952015-06-11 18:25:54 +0200456 break;
457 }
458 }
Emeric Brunb3971ab2015-05-12 18:49:09 +0200459 }
460 intencode(data, &cursor);
461
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200462 /* Encode stick-table entries duration. */
463 intencode(st->table->expire, &cursor);
464
465 if (chunkq > chunkp) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 chunk->data = chunkq - chunkp;
467 memcpy(cursor, chunk->area, chunk->data);
468 cursor += chunk->data;
Frédéric Lécaille37a72542017-07-06 15:02:16 +0200469 }
470
Emeric Brunb3971ab2015-05-12 18:49:09 +0200471 /* Compute datalen */
472 datalen = (cursor - datamsg);
Emeric Brun2b920a12010-09-23 18:30:22 +0200473
Emeric Brunb3971ab2015-05-12 18:49:09 +0200474 /* prepare message header */
475 msg[0] = PEER_MSG_CLASS_STICKTABLE;
476 msg[1] = PEER_MSG_STKT_DEFINE;
477 cursor = &msg[2];
478 intencode(datalen, &cursor);
Emeric Brun2b920a12010-09-23 18:30:22 +0200479
Emeric Brunb3971ab2015-05-12 18:49:09 +0200480 /* move data after header */
481 memmove(cursor, datamsg, datalen);
482
483 /* return header size + data_len */
484 return (cursor - msg) + datalen;
Emeric Brun2b920a12010-09-23 18:30:22 +0200485}
486
Emeric Brunb3971ab2015-05-12 18:49:09 +0200487/*
488 * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
489 * stick table.
Joseph Herlant82b2f542018-11-15 12:19:14 -0800490 * <msg> is a buffer of <size> to receive data message content
Emeric Brunb3971ab2015-05-12 18:49:09 +0200491 * If function returns 0, the caller should consider we were unable to encode this message (TODO:
492 * check size)
493 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100494static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
Emeric Brunb3971ab2015-05-12 18:49:09 +0200495{
496 unsigned short datalen;
497 char *cursor, *datamsg;
498 uint32_t netinteger;
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100499 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200500
Emeric Brunb058f1c2015-09-22 15:50:18 +0200501 cursor = datamsg = msg + 2 + 5;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200502
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100503 st = p->ack.shared_table;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200504 intencode(st->remote_id, &cursor);
505 netinteger = htonl(st->last_get);
506 memcpy(cursor, &netinteger, sizeof(netinteger));
507 cursor += sizeof(netinteger);
508
509 /* Compute datalen */
510 datalen = (cursor - datamsg);
511
512 /* prepare message header */
513 msg[0] = PEER_MSG_CLASS_STICKTABLE;
Emeric Brune1ab8082015-08-21 11:48:54 +0200514 msg[1] = PEER_MSG_STKT_ACK;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200515 cursor = &msg[2];
516 intencode(datalen, &cursor);
517
518 /* move data after header */
519 memmove(cursor, datamsg, datalen);
520
521 /* return header size + data_len */
522 return (cursor - msg) + datalen;
523}
Emeric Brun2b920a12010-09-23 18:30:22 +0200524
525/*
526 * Callback to release a session with a peer
527 */
Willy Tarreau00a37f02015-04-13 12:05:19 +0200528static void peer_session_release(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +0200529{
Willy Tarreau00a37f02015-04-13 12:05:19 +0200530 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +0200531 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200532 struct peer *peer = appctx->ctx.peers.ptr;
533 struct peers *peers = strm_fe(s)->parent;
Emeric Brun2b920a12010-09-23 18:30:22 +0200534
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100535 /* appctx->ctx.peers.ptr is not a peer session */
Willy Tarreaue4d927a2013-12-01 12:47:35 +0100536 if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS)
Emeric Brun2b920a12010-09-23 18:30:22 +0200537 return;
538
539 /* peer session identified */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200540 if (peer) {
Willy Tarreau2d372c22018-11-05 17:12:27 +0100541 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
542 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreau199ad242018-11-05 16:31:22 +0100543 HA_ATOMIC_SUB(&active_peers, 1);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100544 HA_SPIN_LOCK(PEER_LOCK, &peer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +0100545 if (peer->appctx == appctx) {
Emeric Brunb157d732015-08-21 12:00:30 +0200546 /* Re-init current table pointers to force announcement on re-connect */
547 peer->remote_table = peer->last_local_table = NULL;
Emeric Brunb3971ab2015-05-12 18:49:09 +0200548 peer->appctx = NULL;
549 if (peer->flags & PEER_F_LEARN_ASSIGN) {
Emeric Brun2b920a12010-09-23 18:30:22 +0200550 /* unassign current peer for learning */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200551 peer->flags &= ~(PEER_F_LEARN_ASSIGN);
552 peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brun2b920a12010-09-23 18:30:22 +0200553
554 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200555 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +0200556 }
557 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +0200558 peer->flags &= PEER_TEACH_RESET;
559 peer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +0200560 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100561 HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +0200562 task_wakeup(peers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +0200563 }
564}
565
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200566/* Retrieve the major and minor versions of peers protocol
567 * announced by a remote peer. <str> is a null-terminated
568 * string with the following format: "<maj_ver>.<min_ver>".
569 */
570static int peer_get_version(const char *str,
571 unsigned int *maj_ver, unsigned int *min_ver)
572{
573 unsigned int majv, minv;
574 const char *pos, *saved;
575 const char *end;
576
577 saved = pos = str;
578 end = str + strlen(str);
579
580 majv = read_uint(&pos, end);
581 if (saved == pos || *pos++ != '.')
582 return -1;
583
584 saved = pos;
585 minv = read_uint(&pos, end);
586 if (saved == pos || pos != end)
587 return -1;
588
589 *maj_ver = majv;
590 *min_ver = minv;
591
592 return 0;
593}
Emeric Brun2b920a12010-09-23 18:30:22 +0200594
595/*
Frédéric Lécaillece025572019-01-21 13:38:06 +0100596 * Parse a line terminated by an optional '\r' character, followed by a mandatory
597 * '\n' character.
598 * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if
599 * a line could not be read because the communication channel is closed.
600 */
601static inline int peer_getline(struct appctx *appctx)
602{
603 int n;
604 struct stream_interface *si = appctx->owner;
605
606 n = co_getline(si_oc(si), trash.area, trash.size);
607 if (!n)
608 return 0;
609
610 if (n < 0 || trash.area[n - 1] != '\n') {
611 appctx->st0 = PEER_SESS_ST_END;
612 return -1;
613 }
614
615 if (n > 1 && (trash.area[n - 2] == '\r'))
616 trash.area[n - 2] = 0;
617 else
618 trash.area[n - 1] = 0;
619
620 co_skip(si_oc(si), n);
621
622 return n;
623}
624
625/*
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100626 * Send a message after having called <peer_prepare_msg> to build it.
627 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
628 * Returns -1 if there was not enough room left to send the message,
629 * any other negative returned value must be considered as an error with an appcxt st0
630 * returned value equal to PEER_SESS_ST_END.
631 */
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100632static inline int peer_send_msg(struct appctx *appctx,
633 int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *),
634 struct peer_prep_params *params)
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100635{
636 int ret, msglen;
637 struct stream_interface *si = appctx->owner;
638
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100639 msglen = peer_prepare_msg(trash.area, trash.size, params);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100640 if (!msglen) {
641 /* internal error: message does not fit in trash */
642 appctx->st0 = PEER_SESS_ST_END;
643 return 0;
644 }
645
646 /* message to buffer */
647 ret = ci_putblk(si_ic(si), trash.area, msglen);
648 if (ret <= 0) {
649 if (ret == -1) {
650 /* No more write possible */
651 si_rx_room_blk(si);
652 return -1;
653 }
654 appctx->st0 = PEER_SESS_ST_END;
655 }
656
657 return ret;
658}
659
660/*
661 * Send a stick-table switch message.
662 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
663 * Returns -1 if there was not enough room left to send the message,
664 * any other negative returned value must be considered as an error with an appcxt st0
665 * returned value equal to PEER_SESS_ST_END.
666 */
667static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx)
668{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100669 struct peer_prep_params p = {
670 .swtch.shared_table = st,
671 };
672
673 return peer_send_msg(appctx, peer_prepare_switchmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100674}
675
676/*
677 * Send a stick-table update acknowledgement message.
678 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
679 * Returns -1 if there was not enough room left to send the message,
680 * any other negative returned value must be considered as an error with an appcxt st0
681 * returned value equal to PEER_SESS_ST_END.
682 */
683static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx)
684{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100685 struct peer_prep_params p = {
686 .ack.shared_table = st,
687 };
688
689 return peer_send_msg(appctx, peer_prepare_ackmsg, &p);
Frédéric Lécailleec44ea82019-01-22 15:54:53 +0100690}
691
692/*
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100693 * Send a stick-table update message.
694 * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
695 * Returns -1 if there was not enough room left to send the message,
696 * any other negative returned value must be considered as an error with an appcxt st0
697 * returned value equal to PEER_SESS_ST_END.
698 */
699static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts,
700 unsigned int updateid, int use_identifier, int use_timed)
701{
Frédéric Lécailled5fe14b2019-01-24 10:33:40 +0100702 struct peer_prep_params p = {
703 .updt.stksess = ts,
704 .updt.shared_table = st,
705 .updt.updateid = updateid,
706 .updt.use_identifier = use_identifier,
707 .updt.use_timed = use_timed,
708 };
709
710 return peer_send_msg(appctx, peer_prepare_updatemsg, &p);
Frédéric Lécaille87f554c2019-01-22 17:26:50 +0100711}
712
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +0100713
714/*
715 * Function used to lookup for recent stick-table updates associated with
716 * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set).
717 */
718static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st)
719{
720 struct eb32_node *eb;
721
722 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
723 if (!eb) {
724 eb = eb32_first(&st->table->updates);
725 if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) {
726 st->table->commitupdate = st->last_pushed = st->table->localupdate;
727 return NULL;
728 }
729 }
730
731 if ((int)(eb->key - st->table->localupdate) > 0) {
732 st->table->commitupdate = st->last_pushed = st->table->localupdate;
733 return NULL;
734 }
735
736 return eb32_entry(eb, struct stksess, upd);
737}
738
739/*
740 * Function used to lookup for recent stick-table updates associated with
741 * <st> shared stick-table during teach state 1 step.
742 */
743static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st)
744{
745 struct eb32_node *eb;
746
747 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
748 if (!eb) {
749 st->flags |= SHTABLE_F_TEACH_STAGE1;
750 eb = eb32_first(&st->table->updates);
751 if (eb)
752 st->last_pushed = eb->key - 1;
753 return NULL;
754 }
755
756 return eb32_entry(eb, struct stksess, upd);
757}
758
759/*
760 * Function used to lookup for recent stick-table updates associated with
761 * <st> shared stick-table during teach state 2 step.
762 */
763static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st)
764{
765 struct eb32_node *eb;
766
767 eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1);
768 if (!eb || eb->key > st->teaching_origin) {
769 st->flags |= SHTABLE_F_TEACH_STAGE2;
770 return NULL;
771 }
772
773 return eb32_entry(eb, struct stksess, upd);
774}
775
776/*
777 * Generic function to emit update messages for <st> stick-table when a lesson must
778 * be taught to the peer <p>.
779 * <locked> must be set to 1 if the shared table <st> is already locked when entering
780 * this function, 0 if not.
781 *
782 * This function temporary unlock/lock <st> when it sends stick-table updates or
783 * when decrementing its refcount in case of any error when it sends this updates.
784 *
785 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
786 * Returns -1 if there was not enough room left to send the message,
787 * any other negative returned value must be considered as an error with an appcxt st0
788 * returned value equal to PEER_SESS_ST_END.
789 * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function
790 * unlocked if not already locked when entering this function.
791 */
792static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p,
793 struct stksess *(*peer_stksess_lookup)(struct shared_table *),
794 struct shared_table *st, int locked)
795{
796 int ret, new_pushed, use_timed;
797
798 ret = 1;
799 use_timed = 0;
800 if (st != p->last_local_table) {
801 ret = peer_send_switchmsg(st, appctx);
802 if (ret <= 0)
803 return ret;
804
805 p->last_local_table = st;
806 }
807
808 if (peer_stksess_lookup != peer_teach_process_stksess_lookup)
809 use_timed = !(p->flags & PEER_F_DWNGRD);
810
811 /* We force new pushed to 1 to force identifier in update message */
812 new_pushed = 1;
813
814 if (!locked)
815 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
816
817 while (1) {
818 struct stksess *ts;
819 unsigned updateid;
820
821 /* push local updates */
822 ts = peer_stksess_lookup(st);
823 if (!ts)
824 break;
825
826 updateid = ts->upd.key;
827 ts->ref_cnt++;
828 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
829
830 ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed);
831 if (ret <= 0) {
832 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
833 ts->ref_cnt--;
834 if (!locked)
835 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
836 return ret;
837 }
838
839 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
840 ts->ref_cnt--;
841 st->last_pushed = updateid;
842
843 if (peer_stksess_lookup == peer_teach_process_stksess_lookup &&
844 (int)(st->last_pushed - st->table->commitupdate) > 0)
845 st->table->commitupdate = st->last_pushed;
846
847 /* identifier may not needed in next update message */
848 new_pushed = 0;
849 }
850
851 out:
852 if (!locked)
853 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
854 return 1;
855}
856
857/*
858 * Function to emit update messages for <st> stick-table when a lesson must
859 * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set).
860 *
861 * Note that <st> shared stick-table is locked when calling this function.
862 *
863 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
864 * Returns -1 if there was not enough room left to send the message,
865 * any other negative returned value must be considered as an error with an appcxt st0
866 * returned value equal to PEER_SESS_ST_END.
867 */
868static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p,
869 struct shared_table *st)
870{
871 return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1);
872}
873
874/*
875 * Function to emit update messages for <st> stick-table when a lesson must
876 * be taught to the peer <p> during teach state 1 step.
877 *
878 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
879 * Returns -1 if there was not enough room left to send the message,
880 * any other negative returned value must be considered as an error with an appcxt st0
881 * returned value equal to PEER_SESS_ST_END.
882 */
883static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p,
884 struct shared_table *st)
885{
886 return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0);
887}
888
889/*
890 * Function to emit update messages for <st> stick-table when a lesson must
891 * be taught to the peer <p> during teach state 1 step.
892 *
893 * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value.
894 * Returns -1 if there was not enough room left to send the message,
895 * any other negative returned value must be considered as an error with an appcxt st0
896 * returned value equal to PEER_SESS_ST_END.
897 */
898static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p,
899 struct shared_table *st)
900{
901 return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0);
902}
903
Frédéric Lécaille168a34b2019-01-23 11:16:57 +0100904
905/*
906 * Function used to parse a stick-table update message after it has been received
907 * by <p> peer with <msg_cur> as address of the pointer to the position in the
908 * receipt buffer with <msg_end> being position of the end of the stick-table message.
909 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
910 * was encountered.
911 * <exp> must be set if the stick-table entry expires.
912 * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table
913 * messages, in this case the stick-table udpate message is received with a stick-table
914 * update ID.
915 * <totl> is the length of the stick-table update message computed upon receipt.
916 */
917static int peer_recv_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp,
918 char **msg_cur, char *msg_end, int msg_len, int totl)
919{
920 struct stream_interface *si = appctx->owner;
921 struct shared_table *st = p->remote_table;
922 struct stksess *ts, *newts;
923 uint32_t update;
924 int expire;
925 unsigned int data_type;
926 void *data_ptr;
927
928 /* Here we have data message */
929 if (!st)
930 goto ignore_msg;
931
932 expire = MS_TO_TICKS(st->table->expire);
933
934 if (updt) {
935 if (msg_len < sizeof(update)) {
936 /* malformed message */
937 appctx->st0 = PEER_SESS_ST_ERRPROTO;
938 return 0;
939 }
940 memcpy(&update, *msg_cur, sizeof(update));
941 *msg_cur += sizeof(update);
942 st->last_get = htonl(update);
943 }
944 else {
945 st->last_get++;
946 }
947
948 if (exp) {
949 size_t expire_sz = sizeof expire;
950
951 if (*msg_cur + expire_sz > msg_end) {
952 appctx->st0 = PEER_SESS_ST_ERRPROTO;
953 return 0;
954 }
955 memcpy(&expire, *msg_cur, expire_sz);
956 *msg_cur += expire_sz;
957 expire = ntohl(expire);
958 }
959
960 newts = stksess_new(st->table, NULL);
961 if (!newts)
962 goto ignore_msg;
963
964 if (st->table->type == SMP_T_STR) {
965 unsigned int to_read, to_store;
966
967 to_read = intdecode(msg_cur, msg_end);
968 if (!*msg_cur) {
969 /* malformed message */
970 stksess_free(st->table, newts);
971 appctx->st0 = PEER_SESS_ST_ERRPROTO;
972 return 0;
973 }
974 to_store = MIN(to_read, st->table->key_size - 1);
975 if (*msg_cur + to_store > msg_end) {
976 /* malformed message */
977 stksess_free(st->table, newts);
978 appctx->st0 = PEER_SESS_ST_ERRPROTO;
979 return 0;
980 }
981
982 memcpy(newts->key.key, *msg_cur, to_store);
983 newts->key.key[to_store] = 0;
984 *msg_cur += to_read;
985 }
986 else if (st->table->type == SMP_T_SINT) {
987 unsigned int netinteger;
988
989 if (*msg_cur + sizeof(netinteger) > msg_end) {
990 /* malformed message */
991 stksess_free(st->table, newts);
992 appctx->st0 = PEER_SESS_ST_ERRPROTO;
993 return 0;
994 }
995 memcpy(&netinteger, *msg_cur, sizeof(netinteger));
996 netinteger = ntohl(netinteger);
997 memcpy(newts->key.key, &netinteger, sizeof(netinteger));
998 *msg_cur += sizeof(netinteger);
999 }
1000 else {
1001 if (*msg_cur + st->table->key_size > msg_end) {
1002 /* malformed message */
1003 stksess_free(st->table, newts);
1004 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1005 return 0;
1006 }
1007 memcpy(newts->key.key, *msg_cur, st->table->key_size);
1008 *msg_cur += st->table->key_size;
1009 }
1010
1011 /* lookup for existing entry */
1012 ts = stktable_set_entry(st->table, newts);
1013 if (ts != newts) {
1014 stksess_free(st->table, newts);
1015 newts = NULL;
1016 }
1017
1018 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1019
1020 for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
1021
1022 if (!((1 << data_type) & st->remote_data))
1023 continue;
1024
1025 switch (stktable_data_types[data_type].std_type) {
1026 case STD_T_SINT: {
1027 int data;
1028
1029 data = intdecode(msg_cur, msg_end);
1030 if (!*msg_cur) {
1031 /* malformed message */
1032 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1033 stktable_touch_remote(st->table, ts, 1);
1034 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1035 return 0;
1036 }
1037
1038 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1039 if (data_ptr)
1040 stktable_data_cast(data_ptr, std_t_sint) = data;
1041 break;
1042 }
1043 case STD_T_UINT: {
1044 unsigned int data;
1045
1046 data = intdecode(msg_cur, msg_end);
1047 if (!*msg_cur) {
1048 /* malformed message */
1049 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1050 stktable_touch_remote(st->table, ts, 1);
1051 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1052 return 0;
1053 }
1054
1055 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1056 if (data_ptr)
1057 stktable_data_cast(data_ptr, std_t_uint) = data;
1058 break;
1059 }
1060 case STD_T_ULL: {
1061 unsigned long long data;
1062
1063 data = intdecode(msg_cur, msg_end);
1064 if (!*msg_cur) {
1065 /* malformed message */
1066 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1067 stktable_touch_remote(st->table, ts, 1);
1068 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1069 return 0;
1070 }
1071
1072 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1073 if (data_ptr)
1074 stktable_data_cast(data_ptr, std_t_ull) = data;
1075 break;
1076 }
1077 case STD_T_FRQP: {
1078 struct freq_ctr_period data;
1079
1080 /* First bit is reserved for the freq_ctr_period lock
1081 Note: here we're still protected by the stksess lock
1082 so we don't need to update the update the freq_ctr_period
1083 using its internal lock */
1084
1085 data.curr_tick = tick_add(now_ms, -intdecode(msg_cur, msg_end)) & ~0x1;
1086 if (!*msg_cur) {
1087 /* malformed message */
1088 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1089 stktable_touch_remote(st->table, ts, 1);
1090 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1091 return 0;
1092 }
1093 data.curr_ctr = intdecode(msg_cur, msg_end);
1094 if (!*msg_cur) {
1095 /* malformed message */
1096 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1097 stktable_touch_remote(st->table, ts, 1);
1098 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1099 return 0;
1100 }
1101 data.prev_ctr = intdecode(msg_cur, msg_end);
1102 if (!*msg_cur) {
1103 /* malformed message */
1104 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1105 stktable_touch_remote(st->table, ts, 1);
1106 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1107 return 0;
1108 }
1109
1110 data_ptr = stktable_data_ptr(st->table, ts, data_type);
1111 if (data_ptr)
1112 stktable_data_cast(data_ptr, std_t_frqp) = data;
1113 break;
1114 }
1115 }
1116 }
1117 /* Force new expiration */
1118 ts->expire = tick_add(now_ms, expire);
1119
1120 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1121 stktable_touch_remote(st->table, ts, 1);
1122 return 1;
1123
1124 ignore_msg:
1125 /* skip consumed message */
1126 co_skip(si_oc(si), totl);
1127 return 0;
1128}
1129
Frédéric Lécaille87f554c2019-01-22 17:26:50 +01001130/*
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001131 * Function used to parse a stick-table update acknowledgement message after it
1132 * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the
1133 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1134 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1135 * was encountered.
1136 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1137 */
1138static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p,
1139 char **msg_cur, char *msg_end)
1140{
1141 /* ack message */
1142 uint32_t table_id ;
1143 uint32_t update;
1144 struct shared_table *st;
1145
1146 table_id = intdecode(msg_cur, msg_end);
1147 if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) {
1148 /* malformed message */
1149 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1150 return 0;
1151 }
1152
1153 memcpy(&update, *msg_cur, sizeof(update));
1154 update = ntohl(update);
1155
1156 for (st = p->tables; st; st = st->next) {
1157 if (st->local_id == table_id) {
1158 st->update = update;
1159 break;
1160 }
1161 }
1162
1163 return 1;
1164}
1165
1166/*
1167 * Function used to parse a stick-table switch message after it has been received
1168 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1169 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1170 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1171 * was encountered.
1172 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1173 */
1174static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p,
1175 char **msg_cur, char *msg_end)
1176{
1177 struct shared_table *st;
1178 int table_id;
1179
1180 table_id = intdecode(msg_cur, msg_end);
1181 if (!*msg_cur) {
1182 /* malformed message */
1183 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1184 return 0;
1185 }
1186
1187 p->remote_table = NULL;
1188 for (st = p->tables; st; st = st->next) {
1189 if (st->remote_id == table_id) {
1190 p->remote_table = st;
1191 break;
1192 }
1193 }
1194
1195 return 1;
1196}
1197
1198/*
1199 * Function used to parse a stick-table definition message after it has been received
1200 * by <p> peer with <msg_cur> as address of the pointer to the position in the
1201 * receipt buffer with <msg_end> being the position of the end of the stick-table message.
1202 * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error
1203 * was encountered.
1204 * <totl> is the length of the stick-table update message computed upon receipt.
1205 * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO.
1206 */
1207static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p,
1208 char **msg_cur, char *msg_end, int totl)
1209{
1210 struct stream_interface *si = appctx->owner;
1211 int table_id_len;
1212 struct shared_table *st;
1213 int table_type;
1214 int table_keylen;
1215 int table_id;
1216 uint64_t table_data;
1217
1218 table_id = intdecode(msg_cur, msg_end);
1219 if (!*msg_cur) {
1220 /* malformed message */
1221 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1222 return 0;
1223 }
1224
1225 table_id_len = intdecode(msg_cur, msg_end);
1226 if (!*msg_cur) {
1227 /* malformed message */
1228 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1229 return 0;
1230 }
1231
1232 p->remote_table = NULL;
1233 if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) {
1234 /* malformed message */
1235 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1236 return 0;
1237 }
1238
1239 for (st = p->tables; st; st = st->next) {
1240 /* Reset IDs */
1241 if (st->remote_id == table_id)
1242 st->remote_id = 0;
1243
1244 if (!p->remote_table && (table_id_len == strlen(st->table->id)) &&
1245 (memcmp(st->table->id, *msg_cur, table_id_len) == 0))
1246 p->remote_table = st;
1247 }
1248
1249 if (!p->remote_table)
1250 goto ignore_msg;
1251
1252 *msg_cur += table_id_len;
1253 if (*msg_cur >= msg_end) {
1254 /* malformed message */
1255 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1256 return 0;
1257 }
1258
1259 table_type = intdecode(msg_cur, msg_end);
1260 if (!*msg_cur) {
1261 /* malformed message */
1262 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1263 return 0;
1264 }
1265
1266 table_keylen = intdecode(msg_cur, msg_end);
1267 if (!*msg_cur) {
1268 /* malformed message */
1269 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1270 return 0;
1271 }
1272
1273 table_data = intdecode(msg_cur, msg_end);
1274 if (!*msg_cur) {
1275 /* malformed message */
1276 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1277 return 0;
1278 }
1279
1280 if (p->remote_table->table->type != table_type
1281 || p->remote_table->table->key_size != table_keylen) {
1282 p->remote_table = NULL;
1283 goto ignore_msg;
1284 }
1285
1286 p->remote_table->remote_data = table_data;
1287 p->remote_table->remote_id = table_id;
1288 return 1;
1289
1290 ignore_msg:
1291 co_skip(si_oc(si), totl);
1292 return 0;
1293}
1294
1295/*
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001296 * Receive a stick-table message.
1297 * Returns 1 if there was no error, if not, returns 0 if not enough data were available,
1298 * -1 if there was an error updating the appctx state st0 accordingly.
1299 */
1300static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz,
1301 uint32_t *msg_len, int *totl)
1302{
1303 int reql;
1304 struct stream_interface *si = appctx->owner;
1305
1306 reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl);
1307 if (reql <= 0) /* closed or EOL not found */
1308 goto incomplete;
1309
1310 *totl += reql;
1311
1312 if ((unsigned int)msg_head[1] < 128)
1313 return 1;
1314
1315 /* Read and Decode message length */
1316 reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl);
1317 if (reql <= 0) /* closed */
1318 goto incomplete;
1319
1320 *totl += reql;
1321
1322 if ((unsigned int)msg_head[2] < 240) {
1323 *msg_len = msg_head[2];
1324 }
1325 else {
1326 int i;
1327 char *cur;
1328 char *end;
1329
1330 for (i = 3 ; i < msg_head_sz ; i++) {
1331 reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl);
1332 if (reql <= 0) /* closed */
1333 goto incomplete;
1334
1335 *totl += reql;
1336
1337 if (!(msg_head[i] & 0x80))
1338 break;
1339 }
1340
1341 if (i == msg_head_sz) {
1342 /* malformed message */
1343 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1344 return -1;
1345 }
1346 end = msg_head + msg_head_sz;
1347 cur = &msg_head[2];
1348 *msg_len = intdecode(&cur, end);
1349 if (!cur) {
1350 /* malformed message */
1351 appctx->st0 = PEER_SESS_ST_ERRPROTO;
1352 return -1;
1353 }
1354 }
1355
1356 /* Read message content */
1357 if (*msg_len) {
1358 if (*msg_len > trash.size) {
1359 /* Status code is not success, abort */
1360 appctx->st0 = PEER_SESS_ST_ERRSIZE;
1361 return -1;
1362 }
1363
1364 reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl);
1365 if (reql <= 0) /* closed */
1366 goto incomplete;
1367 *totl += reql;
1368 }
1369
1370 return 1;
1371
1372 incomplete:
1373 if (reql < 0) {
1374 /* there was an error */
1375 appctx->st0 = PEER_SESS_ST_END;
1376 return -1;
1377 }
1378
1379 return 0;
1380}
1381/*
Emeric Brun2b920a12010-09-23 18:30:22 +02001382 * IO Handler to handle message exchance with a peer
1383 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001384static void peer_io_handler(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02001385{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001386 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02001387 struct stream *s = si_strm(si);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001388 struct peers *curpeers = strm_fe(s)->parent;
Emeric Brun80527f52017-06-19 17:46:37 +02001389 struct peer *curpeer = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02001390 int reql = 0;
1391 int repl = 0;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001392 size_t proto_len = strlen(PEER_SESSION_PROTO_NAME);
1393 unsigned int maj_ver, min_ver;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001394 int prev_state;
Emeric Brun2b920a12010-09-23 18:30:22 +02001395
Joseph Herlant82b2f542018-11-15 12:19:14 -08001396 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001397 if (si_ic(si)->buf.size == 0)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001398 goto full;
1399
Emeric Brun2b920a12010-09-23 18:30:22 +02001400 while (1) {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001401 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001402switchstate:
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001403 maj_ver = min_ver = (unsigned int)-1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001404 switch(appctx->st0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001405 case PEER_SESS_ST_ACCEPT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001406 prev_state = appctx->st0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001407 appctx->ctx.peers.ptr = NULL;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001408 appctx->st0 = PEER_SESS_ST_GETVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001409 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001410 case PEER_SESS_ST_GETVERSION:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001411 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001412
Frédéric Lécaillece025572019-01-21 13:38:06 +01001413 reql = peer_getline(appctx);
1414 if (!reql)
1415 goto out;
1416
1417 if (reql < 0)
1418 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001419
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001420 /* test protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001421 if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001422 appctx->st0 = PEER_SESS_ST_EXIT;
1423 appctx->st1 = PEER_SESS_SC_ERRPROTO;
1424 goto switchstate;
1425 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001426 if (peer_get_version(trash.area + proto_len + 1, &maj_ver, &min_ver) == -1 ||
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001427 maj_ver != PEER_MAJOR_VER || min_ver > PEER_MINOR_VER) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001428 appctx->st0 = PEER_SESS_ST_EXIT;
1429 appctx->st1 = PEER_SESS_SC_ERRVERSION;
Emeric Brun2b920a12010-09-23 18:30:22 +02001430 goto switchstate;
1431 }
1432
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001433 appctx->st0 = PEER_SESS_ST_GETHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001434 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001435 case PEER_SESS_ST_GETHOST:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001436 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001437
Frédéric Lécaillece025572019-01-21 13:38:06 +01001438 reql = peer_getline(appctx);
1439 if (!reql)
1440 goto out;
1441
1442 if (reql < 0)
1443 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001444
1445 /* test hostname match */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001446 if (strcmp(localpeer, trash.area) != 0) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001447 appctx->st0 = PEER_SESS_ST_EXIT;
1448 appctx->st1 = PEER_SESS_SC_ERRHOST;
Emeric Brun2b920a12010-09-23 18:30:22 +02001449 goto switchstate;
1450 }
1451
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001452 appctx->st0 = PEER_SESS_ST_GETPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02001453 /* fall through */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001454 case PEER_SESS_ST_GETPEER: {
Emeric Brun2b920a12010-09-23 18:30:22 +02001455 char *p;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001456
1457 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001458
Frédéric Lécaillece025572019-01-21 13:38:06 +01001459 reql = peer_getline(appctx);
1460 if (!reql)
1461 goto out;
1462
1463 if (reql < 0)
1464 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001465
Emeric Brunb3971ab2015-05-12 18:49:09 +02001466 /* parse line "<peer name> <pid> <relative_pid>" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001467 p = strchr(trash.area, ' ');
Emeric Brun2b920a12010-09-23 18:30:22 +02001468 if (!p) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001469 appctx->st0 = PEER_SESS_ST_EXIT;
1470 appctx->st1 = PEER_SESS_SC_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001471 goto switchstate;
1472 }
1473 *p = 0;
1474
1475 /* lookup known peer */
1476 for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001477 if (strcmp(curpeer->id, trash.area) == 0)
Emeric Brun2b920a12010-09-23 18:30:22 +02001478 break;
1479 }
1480
1481 /* if unknown peer */
1482 if (!curpeer) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001483 appctx->st0 = PEER_SESS_ST_EXIT;
1484 appctx->st1 = PEER_SESS_SC_ERRPEER;
Emeric Brun2b920a12010-09-23 18:30:22 +02001485 goto switchstate;
1486 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001487
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001488 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Willy Tarreau9df94c22016-10-31 18:42:52 +01001489 if (curpeer->appctx && curpeer->appctx != appctx) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001490 if (curpeer->local) {
1491 /* Local connection, reply a retry */
1492 appctx->st0 = PEER_SESS_ST_EXIT;
1493 appctx->st1 = PEER_SESS_SC_TRYAGAIN;
1494 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001495 }
Emeric Brun80527f52017-06-19 17:46:37 +02001496
1497 /* we're killing a connection, we must apply a random delay before
1498 * retrying otherwise the other end will do the same and we can loop
1499 * for a while.
1500 */
1501 curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau81bc3b02016-10-31 17:37:39 +01001502 peer_session_forceshutdown(curpeer->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02001503 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001504 if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
1505 if (min_ver == PEER_DWNGRD_MINOR_VER) {
1506 curpeer->flags |= PEER_F_DWNGRD;
1507 }
1508 else {
1509 curpeer->flags &= ~PEER_F_DWNGRD;
1510 }
1511 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001512 curpeer->appctx = appctx;
1513 appctx->ctx.peers.ptr = curpeer;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001514 appctx->st0 = PEER_SESS_ST_SENDSUCCESS;
Willy Tarreau199ad242018-11-05 16:31:22 +01001515 HA_ATOMIC_ADD(&active_peers, 1);
Emeric Brun2b920a12010-09-23 18:30:22 +02001516 /* fall through */
1517 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001518 case PEER_SESS_ST_SENDSUCCESS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001519 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001520
Willy Tarreau2d372c22018-11-05 17:12:27 +01001521 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001522 if (!curpeer) {
1523 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001524 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001525 if (curpeer->appctx != appctx) {
1526 appctx->st0 = PEER_SESS_ST_END;
1527 goto switchstate;
1528 }
1529 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001530 repl = snprintf(trash.area, trash.size,
1531 "%d\n",
1532 PEER_SESS_SC_SUCCESSCODE);
1533 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001534 if (repl <= 0) {
1535 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001536 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001537 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001538 goto switchstate;
1539 }
1540
1541 /* Register status code */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001542 curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001543
1544 /* Awake main task */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001545 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001546
1547 /* Init confirm counter */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001548 curpeer->confirm = 0;
1549
1550 /* Init cursors */
1551 for (st = curpeer->tables; st ; st = st->next) {
1552 st->last_get = st->last_acked = 0;
1553 st->teaching_origin = st->last_pushed = st->update;
1554 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001555
1556 /* reset teaching and learning flags to 0 */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001557 curpeer->flags &= PEER_TEACH_RESET;
1558 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001559
1560 /* if current peer is local */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001561 if (curpeer->local) {
1562 /* if current host need resyncfrom local and no process assined */
1563 if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL &&
1564 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1565 /* assign local peer for a lesson, consider lesson already requested */
1566 curpeer->flags |= PEER_F_LEARN_ASSIGN;
1567 curpeers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1568 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001569
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001570 }
1571 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1572 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1573 /* assign peer for a lesson */
1574 curpeer->flags |= PEER_F_LEARN_ASSIGN;
1575 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
1576 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001577
1578
Emeric Brun2b920a12010-09-23 18:30:22 +02001579 /* switch to waiting message state */
Willy Tarreau2d372c22018-11-05 17:12:27 +01001580 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001581 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02001582 goto switchstate;
1583 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001584 case PEER_SESS_ST_CONNECT: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001585 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001586 if (!curpeer) {
1587 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001588 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001589 if (curpeer->appctx != appctx) {
1590 appctx->st0 = PEER_SESS_ST_END;
1591 goto switchstate;
1592 }
1593 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001594
1595 /* Send headers */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001596 repl = snprintf(trash.area, trash.size,
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001597 PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
1598 PEER_MAJOR_VER,
1599 (curpeer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER,
Emeric Brunb3971ab2015-05-12 18:49:09 +02001600 curpeer->id,
Emeric Brun2b920a12010-09-23 18:30:22 +02001601 localpeer,
Willy Tarreau7b77c9f2012-01-07 22:52:12 +01001602 (int)getpid(),
Emeric Brunb3971ab2015-05-12 18:49:09 +02001603 relative_pid);
Emeric Brun2b920a12010-09-23 18:30:22 +02001604
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001605 if (repl >= trash.size) {
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001606 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001607 goto switchstate;
1608 }
1609
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001610 repl = ci_putblk(si_ic(si), trash.area, repl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001611 if (repl <= 0) {
1612 if (repl == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001613 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001614 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001615 goto switchstate;
1616 }
1617
1618 /* switch to the waiting statuscode state */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001619 appctx->st0 = PEER_SESS_ST_GETSTATUS;
Emeric Brun2b920a12010-09-23 18:30:22 +02001620 /* fall through */
1621 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001622 case PEER_SESS_ST_GETSTATUS: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001623 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02001624
Willy Tarreau2d372c22018-11-05 17:12:27 +01001625 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001626 if (!curpeer) {
1627 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001628 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001629 if (curpeer->appctx != appctx) {
1630 appctx->st0 = PEER_SESS_ST_END;
1631 goto switchstate;
1632 }
1633 }
1634
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001635 if (si_ic(si)->flags & CF_WRITE_PARTIAL)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001636 curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE;
Emeric Brun2b920a12010-09-23 18:30:22 +02001637
Frédéric Lécaillece025572019-01-21 13:38:06 +01001638 reql = peer_getline(appctx);
1639 if (!reql)
1640 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02001641
Frédéric Lécaillece025572019-01-21 13:38:06 +01001642 if (reql < 0)
1643 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001644
1645 /* Register status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001646 curpeer->statuscode = atoi(trash.area);
Emeric Brun2b920a12010-09-23 18:30:22 +02001647
1648 /* Awake main task */
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001649 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Emeric Brun2b920a12010-09-23 18:30:22 +02001650
1651 /* If status code is success */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001652 if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Emeric Brun2b920a12010-09-23 18:30:22 +02001653 /* Init cursors */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001654 for (st = curpeer->tables; st ; st = st->next) {
1655 st->last_get = st->last_acked = 0;
1656 st->teaching_origin = st->last_pushed = st->update;
1657 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001658
1659 /* Init confirm counter */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001660 curpeer->confirm = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001661
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001662 /* reset teaching and learning flags to 0 */
1663 curpeer->flags &= PEER_TEACH_RESET;
1664 curpeer->flags &= PEER_LEARN_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001665
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001666 /* If current peer is local */
1667 if (curpeer->local) {
1668 /* flag to start to teach lesson */
1669 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brun2b920a12010-09-23 18:30:22 +02001670
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001671 }
1672 else if ((curpeers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE &&
1673 !(curpeers->flags & PEERS_F_RESYNC_ASSIGN)) {
1674 /* If peer is remote and resync from remote is needed,
1675 and no peer currently assigned */
Emeric Brun2b920a12010-09-23 18:30:22 +02001676
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001677 /* assign peer for a lesson */
1678 curpeer->flags |= PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001679 curpeers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02001680 }
1681
1682 }
1683 else {
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001684 if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION)
1685 curpeer->flags |= PEER_F_DWNGRD;
Emeric Brun2b920a12010-09-23 18:30:22 +02001686 /* Status code is not success, abort */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001687 appctx->st0 = PEER_SESS_ST_END;
Emeric Brun2b920a12010-09-23 18:30:22 +02001688 goto switchstate;
1689 }
Willy Tarreau2d372c22018-11-05 17:12:27 +01001690 HA_ATOMIC_ADD(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001691 appctx->st0 = PEER_SESS_ST_WAITMSG;
Emeric Brun2b920a12010-09-23 18:30:22 +02001692 /* fall through */
1693 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001694 case PEER_SESS_ST_WAITMSG: {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001695 uint32_t msg_len = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001696 char *msg_cur = trash.area;
1697 char *msg_end = trash.area;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001698 unsigned char msg_head[7];
Emeric Brun2b920a12010-09-23 18:30:22 +02001699 int totl = 0;
1700
Willy Tarreau2d372c22018-11-05 17:12:27 +01001701 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001702 if (!curpeer) {
1703 curpeer = appctx->ctx.peers.ptr;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001704 HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02001705 if (curpeer->appctx != appctx) {
1706 appctx->st0 = PEER_SESS_ST_END;
1707 goto switchstate;
1708 }
1709 }
1710
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001711 reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl);
1712 if (reql <= 0) {
1713 if (reql == -1)
1714 goto switchstate;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001715 goto incomplete;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001716 }
Willy Tarreau86a446e2013-11-25 23:02:37 +01001717
Frédéric Lécaille95203f22019-01-23 19:38:11 +01001718 msg_end += msg_len;
1719
Emeric Brunb3971ab2015-05-12 18:49:09 +02001720 if (msg_head[0] == PEER_MSG_CLASS_CONTROL) {
1721 if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) {
1722 struct shared_table *st;
1723 /* Reset message: remote need resync */
1724
1725 /* prepare tables fot a global push */
1726 for (st = curpeer->tables; st; st = st->next) {
1727 st->teaching_origin = st->last_pushed = st->table->update;
1728 st->flags = 0;
Willy Tarreau86a446e2013-11-25 23:02:37 +01001729 }
Willy Tarreau72d6c162013-04-11 16:14:13 +02001730
Emeric Brunb3971ab2015-05-12 18:49:09 +02001731 /* reset teaching flags to 0 */
1732 curpeer->flags &= PEER_TEACH_RESET;
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001733
Emeric Brunb3971ab2015-05-12 18:49:09 +02001734 /* flag to start to teach lesson */
1735 curpeer->flags |= PEER_F_TEACH_PROCESS;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001736 }
1737 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001738 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1739 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001740 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
1741 curpeers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE);
Willy Tarreau86a446e2013-11-25 23:02:37 +01001742 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001743 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001744 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001745 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001746 if (curpeer->flags & PEER_F_LEARN_ASSIGN) {
1747 curpeer->flags &= ~PEER_F_LEARN_ASSIGN;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001748 curpeers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001749
1750 curpeer->flags |= PEER_F_LEARN_NOTUP2DATE;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001751 curpeers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
1752 task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG);
Cyril Bonté9a60ff92014-02-16 01:07:07 +01001753 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001754 curpeer->confirm++;
Emeric Brun2b920a12010-09-23 18:30:22 +02001755 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001756 else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) {
Emeric Brun597b26e2016-08-12 11:23:31 +02001757 struct shared_table *st;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001758
1759 /* If stopping state */
1760 if (stopping) {
1761 /* Close session, push resync no more needed */
1762 curpeer->flags |= PEER_F_TEACH_COMPLETE;
1763 appctx->st0 = PEER_SESS_ST_END;
1764 goto switchstate;
1765 }
Emeric Brun597b26e2016-08-12 11:23:31 +02001766 for (st = curpeer->tables; st; st = st->next) {
1767 st->update = st->last_pushed = st->teaching_origin;
1768 st->flags = 0;
1769 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001770
1771 /* reset teaching flags to 0 */
1772 curpeer->flags &= PEER_TEACH_RESET;
Emeric Brun2b920a12010-09-23 18:30:22 +02001773 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001774 }
1775 else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) {
1776 if (msg_head[1] == PEER_MSG_STKT_DEFINE) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001777 if (!peer_treat_definemsg(appctx, curpeer, &msg_cur, msg_end, totl))
1778 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001779 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001780 else if (msg_head[1] == PEER_MSG_STKT_SWITCH) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001781 if (!peer_treat_switchmsg(appctx, curpeer, &msg_cur, msg_end))
Emeric Brunb3971ab2015-05-12 18:49:09 +02001782 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001783 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001784 else if (msg_head[1] == PEER_MSG_STKT_UPDATE
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +02001785 || msg_head[1] == PEER_MSG_STKT_INCUPDATE
1786 || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED
1787 || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) {
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001788 int update, expire;
Emeric Brun2b920a12010-09-23 18:30:22 +02001789
Frédéric Lécaille168a34b2019-01-23 11:16:57 +01001790 update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED;
1791 expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED;
1792 if (!peer_recv_updatemsg(appctx, curpeer, update, expire,
1793 &msg_cur, msg_end, msg_len, totl))
1794 goto switchstate;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001795
Emeric Brunb3971ab2015-05-12 18:49:09 +02001796 }
1797 else if (msg_head[1] == PEER_MSG_STKT_ACK) {
Frédéric Lécailled27b0942019-01-23 17:31:37 +01001798 if (!peer_treat_ackmsg(appctx, curpeer, &msg_cur, msg_end))
Emeric Brunb3971ab2015-05-12 18:49:09 +02001799 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001800 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001801 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001802 else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) {
1803 appctx->st0 = PEER_SESS_ST_ERRPROTO;
Emeric Brun2b920a12010-09-23 18:30:22 +02001804 goto switchstate;
1805 }
1806
Emeric Brunb3971ab2015-05-12 18:49:09 +02001807ignore_msg:
Emeric Brun2b920a12010-09-23 18:30:22 +02001808 /* skip consumed message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001809 co_skip(si_oc(si), totl);
Emeric Brun2b920a12010-09-23 18:30:22 +02001810 /* loop on that state to peek next message */
Willy Tarreau72d6c162013-04-11 16:14:13 +02001811 goto switchstate;
1812
Emeric Brun2b920a12010-09-23 18:30:22 +02001813incomplete:
Willy Tarreau06d80a92017-10-19 14:32:15 +02001814 /* we get here when a co_getblk() returns <= 0 in reql */
Willy Tarreau9d9179b2013-04-11 16:56:44 +02001815
Willy Tarreau72d6c162013-04-11 16:14:13 +02001816 if (reql < 0) {
1817 /* there was an error */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001818 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau72d6c162013-04-11 16:14:13 +02001819 goto switchstate;
1820 }
1821
Emeric Brun2b920a12010-09-23 18:30:22 +02001822
Emeric Brun2b920a12010-09-23 18:30:22 +02001823
Emeric Brunb3971ab2015-05-12 18:49:09 +02001824
Emeric Brun2b920a12010-09-23 18:30:22 +02001825 /* Need to request a resync */
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001826 if ((curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1827 (curpeers->flags & PEERS_F_RESYNC_ASSIGN) &&
1828 !(curpeers->flags & PEERS_F_RESYNC_PROCESS)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001829 unsigned char msg[2];
Emeric Brun2b920a12010-09-23 18:30:22 +02001830
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001831 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001832 msg[0] = PEER_MSG_CLASS_CONTROL;
1833 msg[1] = PEER_MSG_CTRL_RESYNCREQ;
Emeric Brun2b920a12010-09-23 18:30:22 +02001834
Emeric Brunb3971ab2015-05-12 18:49:09 +02001835 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001836 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001837 if (repl <= 0) {
1838 /* no more write possible */
1839 if (repl == -1)
1840 goto full;
1841 appctx->st0 = PEER_SESS_ST_END;
1842 goto switchstate;
1843 }
1844 curpeers->flags |= PEERS_F_RESYNC_PROCESS;
1845 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001846
Emeric Brunb3971ab2015-05-12 18:49:09 +02001847 /* Nothing to read, now we start to write */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001848 if (curpeer->tables) {
1849 struct shared_table *st;
1850 struct shared_table *last_local_table;
Emeric Brun2b920a12010-09-23 18:30:22 +02001851
Emeric Brunb3971ab2015-05-12 18:49:09 +02001852 last_local_table = curpeer->last_local_table;
1853 if (!last_local_table)
1854 last_local_table = curpeer->tables;
1855 st = last_local_table->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001856
Emeric Brunb3971ab2015-05-12 18:49:09 +02001857 while (1) {
1858 if (!st)
1859 st = curpeer->tables;
Emeric Brun2b920a12010-09-23 18:30:22 +02001860
Emeric Brunb3971ab2015-05-12 18:49:09 +02001861 /* It remains some updates to ack */
1862 if (st->last_get != st->last_acked) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001863 repl = peer_send_ackmsg(st, appctx);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001864 if (repl <= 0) {
Frédéric Lécailleec44ea82019-01-22 15:54:53 +01001865 if (repl == -1)
1866 goto out;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001867 goto switchstate;
Emeric Brun2b920a12010-09-23 18:30:22 +02001868 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001869 st->last_acked = st->last_get;
Emeric Brun2b920a12010-09-23 18:30:22 +02001870 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001871
Emeric Brunb3971ab2015-05-12 18:49:09 +02001872 if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001873 HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brunb3971ab2015-05-12 18:49:09 +02001874 if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) &&
1875 ((int)(st->last_pushed - st->table->localupdate) < 0)) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02001876
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001877 repl = peer_send_teach_process_msgs(appctx, curpeer, st);
1878 if (repl <= 0) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001879 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001880 if (repl == -1)
1881 goto out;
1882 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001883 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001884 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001885 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02001886 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001887 else {
1888 if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001889 repl = peer_send_teach_stage1_msgs(appctx, curpeer, st);
1890 if (repl <= 0) {
1891 if (repl == -1)
1892 goto out;
1893 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001894 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001895 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001896
Emeric Brunb3971ab2015-05-12 18:49:09 +02001897 if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) {
Frédéric Lécaille6a8303d2019-01-22 22:25:17 +01001898 repl = peer_send_teach_stage2_msgs(appctx, curpeer, st);
1899 if (repl <= 0) {
1900 if (repl == -1)
1901 goto out;
1902 goto switchstate;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001903 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001904 }
Emeric Brun2b920a12010-09-23 18:30:22 +02001905 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001906
1907 if (st == last_local_table)
1908 break;
1909 st = st->next;
Emeric Brun2b920a12010-09-23 18:30:22 +02001910 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02001911 }
1912
1913
1914 if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) {
1915 unsigned char msg[2];
1916
Frédéric Lécaillea8725ec2019-01-22 10:31:39 +01001917 /* Current peer was elected to request a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001918 msg[0] = PEER_MSG_CLASS_CONTROL;
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001919 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 +02001920 /* process final lesson message */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001921 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brunb3971ab2015-05-12 18:49:09 +02001922 if (repl <= 0) {
1923 /* no more write possible */
1924 if (repl == -1)
1925 goto full;
1926 appctx->st0 = PEER_SESS_ST_END;
1927 goto switchstate;
1928 }
1929 /* flag finished message sent */
1930 curpeer->flags |= PEER_F_TEACH_FINISHED;
1931 }
1932
Emeric Brun597b26e2016-08-12 11:23:31 +02001933 /* Confirm finished or partial messages */
1934 while (curpeer->confirm) {
1935 unsigned char msg[2];
1936
1937 /* There is a confirm messages to send */
1938 msg[0] = PEER_MSG_CLASS_CONTROL;
1939 msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM;
1940
1941 /* message to buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001942 repl = ci_putblk(si_ic(si), (char *)msg, sizeof(msg));
Emeric Brun597b26e2016-08-12 11:23:31 +02001943 if (repl <= 0) {
1944 /* no more write possible */
1945 if (repl == -1)
1946 goto full;
1947 appctx->st0 = PEER_SESS_ST_END;
1948 goto switchstate;
1949 }
1950 curpeer->confirm--;
1951 }
1952
Emeric Brun2b920a12010-09-23 18:30:22 +02001953 /* noting more to do */
1954 goto out;
1955 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001956 case PEER_SESS_ST_EXIT:
Willy Tarreau2d372c22018-11-05 17:12:27 +01001957 if (prev_state == PEER_SESS_ST_WAITMSG)
1958 HA_ATOMIC_SUB(&connected_peers, 1);
1959 prev_state = appctx->st0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001960 repl = snprintf(trash.area, trash.size,
1961 "%d\n", appctx->st1);
1962 if (ci_putblk(si_ic(si), trash.area, repl) == -1)
Willy Tarreaubc18da12015-03-13 14:00:47 +01001963 goto full;
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001964 appctx->st0 = PEER_SESS_ST_END;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001965 goto switchstate;
1966 case PEER_SESS_ST_ERRSIZE: {
1967 unsigned char msg[2];
1968
Willy Tarreau2d372c22018-11-05 17:12:27 +01001969 if (prev_state == PEER_SESS_ST_WAITMSG)
1970 HA_ATOMIC_SUB(&connected_peers, 1);
1971 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001972 msg[0] = PEER_MSG_CLASS_ERROR;
1973 msg[1] = PEER_MSG_ERR_SIZELIMIT;
1974
Willy Tarreau06d80a92017-10-19 14:32:15 +02001975 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001976 goto full;
1977 appctx->st0 = PEER_SESS_ST_END;
1978 goto switchstate;
1979 }
1980 case PEER_SESS_ST_ERRPROTO: {
1981 unsigned char msg[2];
1982
Willy Tarreau2d372c22018-11-05 17:12:27 +01001983 if (prev_state == PEER_SESS_ST_WAITMSG)
1984 HA_ATOMIC_SUB(&connected_peers, 1);
1985 prev_state = appctx->st0;
Emeric Brunb3971ab2015-05-12 18:49:09 +02001986 msg[0] = PEER_MSG_CLASS_ERROR;
1987 msg[1] = PEER_MSG_ERR_PROTOCOL;
1988
Willy Tarreau06d80a92017-10-19 14:32:15 +02001989 if (ci_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1)
Emeric Brunb3971ab2015-05-12 18:49:09 +02001990 goto full;
1991 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau2d372c22018-11-05 17:12:27 +01001992 prev_state = appctx->st0;
Emeric Brun2b920a12010-09-23 18:30:22 +02001993 /* fall through */
Emeric Brunb3971ab2015-05-12 18:49:09 +02001994 }
Willy Tarreaue4d927a2013-12-01 12:47:35 +01001995 case PEER_SESS_ST_END: {
Willy Tarreau2d372c22018-11-05 17:12:27 +01001996 if (prev_state == PEER_SESS_ST_WAITMSG)
1997 HA_ATOMIC_SUB(&connected_peers, 1);
1998 prev_state = appctx->st0;
Emeric Brun80527f52017-06-19 17:46:37 +02001999 if (curpeer) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002000 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002001 curpeer = NULL;
2002 }
Willy Tarreau73b013b2012-05-21 16:31:45 +02002003 si_shutw(si);
2004 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002005 si_ic(si)->flags |= CF_READ_NULL;
Willy Tarreau828824a2015-04-19 17:20:03 +02002006 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002007 }
2008 }
2009 }
2010out:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002011 si_oc(si)->flags |= CF_READ_DONTWAIT;
Emeric Brun80527f52017-06-19 17:46:37 +02002012
2013 if (curpeer)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002014 HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock);
Emeric Brun2b920a12010-09-23 18:30:22 +02002015 return;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002016full:
Willy Tarreaudb398432018-11-15 11:08:52 +01002017 si_rx_room_blk(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01002018 goto out;
Emeric Brun2b920a12010-09-23 18:30:22 +02002019}
2020
Willy Tarreau30576452015-04-13 13:50:30 +02002021static struct applet peer_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002022 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002023 .name = "<PEER>", /* used for logging */
2024 .fct = peer_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07002025 .release = peer_session_release,
Willy Tarreaub24281b2011-02-13 13:16:36 +01002026};
Emeric Brun2b920a12010-09-23 18:30:22 +02002027
2028/*
2029 * Use this function to force a close of a peer session
2030 */
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002031static void peer_session_forceshutdown(struct appctx *appctx)
Emeric Brun2b920a12010-09-23 18:30:22 +02002032{
Frédéric Lécaille5df11902017-06-13 16:39:57 +02002033 /* Note that the peer sessions which have just been created
2034 * (->st0 == PEER_SESS_ST_CONNECT) must not
2035 * be shutdown, if not, the TCP session will never be closed
2036 * and stay in CLOSE_WAIT state after having been closed by
2037 * the remote side.
2038 */
2039 if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002040 return;
2041
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002042 if (appctx->applet != &peer_applet)
2043 return;
2044
Willy Tarreau2d372c22018-11-05 17:12:27 +01002045 if (appctx->st0 == PEER_SESS_ST_WAITMSG)
2046 HA_ATOMIC_SUB(&connected_peers, 1);
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002047 appctx->st0 = PEER_SESS_ST_END;
Willy Tarreau78c0c502016-10-31 17:32:20 +01002048 appctx_wakeup(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002049}
2050
Willy Tarreau91d96282015-03-13 15:47:26 +01002051/* Pre-configures a peers frontend to accept incoming connections */
2052void peers_setup_frontend(struct proxy *fe)
2053{
2054 fe->last_change = now.tv_sec;
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002055 fe->cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau91d96282015-03-13 15:47:26 +01002056 fe->maxconn = 0;
2057 fe->conn_retries = CONN_RETRIES;
2058 fe->timeout.client = MS_TO_TICKS(5000);
Willy Tarreaud1d48d42015-03-13 16:15:46 +01002059 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +01002060 fe->default_target = &peer_applet.obj_type;
Willy Tarreau91d96282015-03-13 15:47:26 +01002061 fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Willy Tarreau0fca4832015-05-01 19:12:05 +02002062 fe->bind_proc = 0; /* will be filled by users */
Willy Tarreau91d96282015-03-13 15:47:26 +01002063}
2064
Emeric Brun2b920a12010-09-23 18:30:22 +02002065/*
Willy Tarreaubd55e312010-11-11 10:55:09 +01002066 * Create a new peer session in assigned state (connect will start automatically)
Emeric Brun2b920a12010-09-23 18:30:22 +02002067 */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002068static struct appctx *peer_session_create(struct peers *peers, struct peer *peer)
Emeric Brun2b920a12010-09-23 18:30:22 +02002069{
Willy Tarreau04b92862017-09-15 11:01:04 +02002070 struct proxy *p = peers->peers_fe; /* attached frontend */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002071 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002072 struct session *sess;
Willy Tarreau87b09662015-04-03 00:22:06 +02002073 struct stream *s;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002074 struct connection *conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002075 struct conn_stream *cs;
Emeric Brun2b920a12010-09-23 18:30:22 +02002076
Emeric Brunb3971ab2015-05-12 18:49:09 +02002077 peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000));
2078 peer->statuscode = PEER_SESS_SC_CONNECTCODE;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002079 s = NULL;
2080
Emeric Brun1138fd02017-06-19 12:38:55 +02002081 appctx = appctx_new(&peer_applet, tid_bit);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002082 if (!appctx)
2083 goto out_close;
2084
2085 appctx->st0 = PEER_SESS_ST_CONNECT;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002086 appctx->ctx.peers.ptr = (void *)peer;
Willy Tarreaud990baf2015-04-05 00:32:03 +02002087
Willy Tarreau04b92862017-09-15 11:01:04 +02002088 sess = session_new(p, NULL, &appctx->obj_type);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002089 if (!sess) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002090 ha_alert("out of memory in peer_session_create().\n");
Willy Tarreaud990baf2015-04-05 00:32:03 +02002091 goto out_free_appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002092 }
2093
Willy Tarreau87787ac2017-08-28 16:22:54 +02002094 if ((s = stream_new(sess, &appctx->obj_type)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002095 ha_alert("Failed to initialize stream in peer_session_create().\n");
Willy Tarreau87787ac2017-08-28 16:22:54 +02002096 goto out_free_sess;
Willy Tarreau8baf9062015-04-05 00:46:36 +02002097 }
2098
Willy Tarreau342bfb12015-04-05 01:35:34 +02002099 /* The tasks below are normally what is supposed to be done by
2100 * fe->accept().
2101 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002102 s->flags = SF_ASSIGNED|SF_ADDR_SET;
Emeric Brun2b920a12010-09-23 18:30:22 +02002103
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002104 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002105 si_cant_get(&s->si[0]);
Willy Tarreau6e2979c2015-04-27 13:21:15 +02002106 appctx_wakeup(appctx);
2107
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002108 /* initiate an outgoing connection */
Willy Tarreaudbd02672017-12-06 17:39:53 +01002109 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002110 si_set_state(&s->si[1], SI_ST_ASS);
Willy Tarreau3ed35ef2013-10-24 11:51:38 +02002111
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002112 /* automatically prepare the stream interface to connect to the
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002113 * pre-initialized connection in si->conn.
2114 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002115 if (unlikely((conn = conn_new()) == NULL))
Willy Tarreau8baf9062015-04-05 00:46:36 +02002116 goto out_free_strm;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002117
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002118 if (unlikely((cs = cs_new(conn)) == NULL))
2119 goto out_free_conn;
2120
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002121 conn->target = s->target = peer_session_target(peer, s);
Willy Tarreaube373152018-09-06 11:45:30 +02002122 memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to));
2123
Frédéric Lécaille1055e682018-04-26 14:35:21 +02002124 conn_prepare(conn, peer->proto, peer_xprt(peer));
Olivier Houchardf502aca2018-12-14 19:42:40 +01002125 conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002126 si_attach_cs(&s->si[1], cs);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002127
Emeric Brun2b920a12010-09-23 18:30:22 +02002128 s->do_log = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002129 s->uniq_id = 0;
Emeric Brun2b920a12010-09-23 18:30:22 +02002130
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002131 s->res.flags |= CF_READ_DONTWAIT;
Willy Tarreau696a2912014-11-24 11:36:57 +01002132
Emeric Brunb3971ab2015-05-12 18:49:09 +02002133 peer->appctx = appctx;
Willy Tarreau87787ac2017-08-28 16:22:54 +02002134 task_wakeup(s->task, TASK_WOKEN_INIT);
Willy Tarreau199ad242018-11-05 16:31:22 +01002135 HA_ATOMIC_ADD(&active_peers, 1);
Willy Tarreau9df94c22016-10-31 18:42:52 +01002136 return appctx;
Emeric Brun2b920a12010-09-23 18:30:22 +02002137
2138 /* Error unrolling */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002139 out_free_conn:
2140 conn_free(conn);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002141 out_free_strm:
Emeric Brun2b920a12010-09-23 18:30:22 +02002142 LIST_DEL(&s->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002143 pool_free(pool_head_stream, s);
Willy Tarreau15b5e142015-04-04 14:38:25 +02002144 out_free_sess:
Willy Tarreau11c36242015-04-04 15:54:03 +02002145 session_free(sess);
Willy Tarreaud990baf2015-04-05 00:32:03 +02002146 out_free_appctx:
2147 appctx_free(appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002148 out_close:
Willy Tarreaub21d08e2016-10-31 17:46:57 +01002149 return NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002150}
2151
2152/*
2153 * Task processing function to manage re-connect and peer session
2154 * tasks wakeup on local update.
2155 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002156static struct task *process_peer_sync(struct task * task, void *context, unsigned short state)
Emeric Brun2b920a12010-09-23 18:30:22 +02002157{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002158 struct peers *peers = context;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002159 struct peer *ps;
2160 struct shared_table *st;
Emeric Brun2b920a12010-09-23 18:30:22 +02002161
2162 task->expire = TICK_ETERNITY;
2163
Emeric Brunb3971ab2015-05-12 18:49:09 +02002164 if (!peers->peers_fe) {
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002165 /* this one was never started, kill it */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002166 signal_unregister_handler(peers->sighandler);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002167 task_delete(peers->sync_task);
2168 task_free(peers->sync_task);
Willy Tarreau37bb7be2015-09-21 15:24:58 +02002169 peers->sync_task = NULL;
Willy Tarreau46dc1ca2015-05-01 18:32:13 +02002170 return NULL;
2171 }
2172
Emeric Brun80527f52017-06-19 17:46:37 +02002173 /* Acquire lock for all peers of the section */
2174 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002175 HA_SPIN_LOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002176
Emeric Brun2b920a12010-09-23 18:30:22 +02002177 if (!stopping) {
2178 /* Normal case (not soft stop)*/
Emeric Brunb3971ab2015-05-12 18:49:09 +02002179
2180 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) &&
2181 (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) &&
2182 !(peers->flags & PEERS_F_RESYNC_ASSIGN)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002183 /* Resync from local peer needed
2184 no peer was assigned for the lesson
2185 and no old local peer found
2186 or resync timeout expire */
2187
2188 /* flag no more resync from local, to try resync from remotes */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002189 peers->flags |= PEERS_F_RESYNC_LOCAL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002190
2191 /* reschedule a resync */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002192 peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000));
Emeric Brun2b920a12010-09-23 18:30:22 +02002193 }
2194
2195 /* For each session */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002196 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002197 /* For each remote peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002198 if (!ps->local) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002199 if (!ps->appctx) {
2200 /* no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002201 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002202 ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002203 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002204 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002205 tick_is_expired(ps->reconnect, now_ms))) {
2206 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002207 * or previous peer connection established with success
2208 * or previous peer connection failed while connecting
Emeric Brun2b920a12010-09-23 18:30:22 +02002209 * and reconnection timer is expired */
2210
2211 /* retry a connect */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002212 ps->appctx = peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002213 }
Willy Tarreaub4e34da2015-05-20 10:39:04 +02002214 else if (!tick_is_expired(ps->reconnect, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002215 /* If previous session failed during connection
2216 * but reconnection timer is not expired */
2217
2218 /* reschedule task for reconnect */
2219 task->expire = tick_first(task->expire, ps->reconnect);
2220 }
2221 /* else do nothing */
Willy Tarreau9df94c22016-10-31 18:42:52 +01002222 } /* !ps->appctx */
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002223 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002224 /* current peer connection is active and established */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002225 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2226 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
Emeric Brun2b920a12010-09-23 18:30:22 +02002227 !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) {
2228 /* Resync from a remote is needed
2229 * and no peer was assigned for lesson
2230 * and current peer may be up2date */
2231
2232 /* assign peer for the lesson */
2233 ps->flags |= PEER_F_LEARN_ASSIGN;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002234 peers->flags |= PEERS_F_RESYNC_ASSIGN;
Emeric Brun2b920a12010-09-23 18:30:22 +02002235
Willy Tarreau9df94c22016-10-31 18:42:52 +01002236 /* wake up peer handler to handle a request of resync */
Willy Tarreaue5843b32015-04-27 18:40:14 +02002237 appctx_wakeup(ps->appctx);
Emeric Brun2b920a12010-09-23 18:30:22 +02002238 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002239 else {
2240 /* Awake session if there is data to push */
2241 for (st = ps->tables; st ; st = st->next) {
2242 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002243 /* wake up the peer handler to push local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002244 appctx_wakeup(ps->appctx);
2245 break;
2246 }
2247 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002248 }
2249 /* else do nothing */
2250 } /* SUCCESSCODE */
2251 } /* !ps->peer->local */
2252 } /* for */
2253
2254 /* Resync from remotes expired: consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002255 if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) &&
2256 !(peers->flags & PEERS_F_RESYNC_ASSIGN) &&
2257 tick_is_expired(peers->resync_timeout, now_ms)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002258 /* Resync from remote peer needed
2259 * no peer was assigned for the lesson
2260 * and resync timeout expire */
2261
2262 /* flag no more resync from remote, consider resync is finished */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002263 peers->flags |= PEERS_F_RESYNC_REMOTE;
Emeric Brun2b920a12010-09-23 18:30:22 +02002264 }
2265
Emeric Brunb3971ab2015-05-12 18:49:09 +02002266 if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002267 /* Resync not finished*/
Frédéric Lécaille5d6e5f82017-05-29 13:47:16 +02002268 /* reschedule task to resync timeout if not expired, to ended resync if needed */
2269 if (!tick_is_expired(peers->resync_timeout, now_ms))
2270 task->expire = tick_first(task->expire, peers->resync_timeout);
Emeric Brun2b920a12010-09-23 18:30:22 +02002271 }
2272 } /* !stopping */
2273 else {
2274 /* soft stop case */
Willy Tarreau086735a2018-11-05 15:09:47 +01002275 if (state & TASK_WOKEN_SIGNAL) {
Joseph Herlant82b2f542018-11-15 12:19:14 -08002276 /* We've just received the signal */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002277 if (!(peers->flags & PEERS_F_DONOTSTOP)) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002278 /* add DO NOT STOP flag if not present */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002279 HA_ATOMIC_ADD(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002280 peers->flags |= PEERS_F_DONOTSTOP;
2281 ps = peers->local;
2282 for (st = ps->tables; st ; st = st->next)
2283 st->table->syncing++;
Emeric Brun2b920a12010-09-23 18:30:22 +02002284 }
2285
2286 /* disconnect all connected peers */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002287 for (ps = peers->remote; ps; ps = ps->next) {
Emeric Brun80527f52017-06-19 17:46:37 +02002288 /* we're killing a connection, we must apply a random delay before
2289 * retrying otherwise the other end will do the same and we can loop
2290 * for a while.
2291 */
2292 ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
Willy Tarreau9df94c22016-10-31 18:42:52 +01002293 if (ps->appctx) {
Willy Tarreau81bc3b02016-10-31 17:37:39 +01002294 peer_session_forceshutdown(ps->appctx);
Willy Tarreaue5843b32015-04-27 18:40:14 +02002295 ps->appctx = NULL;
Emeric Brun2b920a12010-09-23 18:30:22 +02002296 }
2297 }
2298 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002299
Emeric Brunb3971ab2015-05-12 18:49:09 +02002300 ps = peers->local;
Emeric Brun2b920a12010-09-23 18:30:22 +02002301 if (ps->flags & PEER_F_TEACH_COMPLETE) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002302 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002303 /* resync of new process was complete, current process can die now */
Willy Tarreaucea85372017-11-29 14:49:30 +01002304 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002305 peers->flags &= ~PEERS_F_DONOTSTOP;
2306 for (st = ps->tables; st ; st = st->next)
2307 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002308 }
2309 }
Willy Tarreau9df94c22016-10-31 18:42:52 +01002310 else if (!ps->appctx) {
2311 /* If there's no active peer connection */
Emeric Brun2b920a12010-09-23 18:30:22 +02002312 if (ps->statuscode == 0 ||
Willy Tarreaue4d927a2013-12-01 12:47:35 +01002313 ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
2314 ps->statuscode == PEER_SESS_SC_CONNECTEDCODE ||
2315 ps->statuscode == PEER_SESS_SC_TRYAGAIN) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002316 /* connection never tried
Willy Tarreau9df94c22016-10-31 18:42:52 +01002317 * or previous peer connection was successfully established
2318 * or previous tcp connect succeeded but init state incomplete
Emeric Brun2b920a12010-09-23 18:30:22 +02002319 * or during previous connect, peer replies a try again statuscode */
2320
2321 /* connect to the peer */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002322 peer_session_create(peers, ps);
Emeric Brun2b920a12010-09-23 18:30:22 +02002323 }
2324 else {
2325 /* Other error cases */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002326 if (peers->flags & PEERS_F_DONOTSTOP) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002327 /* unable to resync new process, current process can die now */
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002328 HA_ATOMIC_SUB(&jobs, 1);
Emeric Brunb3971ab2015-05-12 18:49:09 +02002329 peers->flags &= ~PEERS_F_DONOTSTOP;
2330 for (st = ps->tables; st ; st = st->next)
2331 st->table->syncing--;
Emeric Brun2b920a12010-09-23 18:30:22 +02002332 }
2333 }
2334 }
Emeric Brunb3971ab2015-05-12 18:49:09 +02002335 else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) {
Willy Tarreau9df94c22016-10-31 18:42:52 +01002336 /* current peer connection is active and established
2337 * wake up all peer handlers to push remaining local updates */
Emeric Brunb3971ab2015-05-12 18:49:09 +02002338 for (st = ps->tables; st ; st = st->next) {
2339 if ((int)(st->last_pushed - st->table->localupdate) < 0) {
Emeric Brunb3971ab2015-05-12 18:49:09 +02002340 appctx_wakeup(ps->appctx);
2341 break;
2342 }
2343 }
Emeric Brun2b920a12010-09-23 18:30:22 +02002344 }
2345 } /* stopping */
Emeric Brun80527f52017-06-19 17:46:37 +02002346
2347 /* Release lock for all peers of the section */
2348 for (ps = peers->remote; ps; ps = ps->next)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002349 HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock);
Emeric Brun80527f52017-06-19 17:46:37 +02002350
Emeric Brun2b920a12010-09-23 18:30:22 +02002351 /* Wakeup for re-connect */
2352 return task;
2353}
2354
Emeric Brunb3971ab2015-05-12 18:49:09 +02002355
Emeric Brun2b920a12010-09-23 18:30:22 +02002356/*
Willy Tarreaud9443442018-10-15 11:18:03 +02002357 * returns 0 in case of error.
Emeric Brun2b920a12010-09-23 18:30:22 +02002358 */
Willy Tarreaud9443442018-10-15 11:18:03 +02002359int peers_init_sync(struct peers *peers)
Emeric Brun2b920a12010-09-23 18:30:22 +02002360{
Emeric Brun2b920a12010-09-23 18:30:22 +02002361 struct peer * curpeer;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002362 struct listener *listener;
Emeric Brun2b920a12010-09-23 18:30:22 +02002363
Emeric Brun2b920a12010-09-23 18:30:22 +02002364 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Emeric Brun2b920a12010-09-23 18:30:22 +02002365 peers->peers_fe->maxconn += 3;
2366 }
2367
Willy Tarreau4348fad2012-09-20 16:48:07 +02002368 list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe)
2369 listener->maxconn = peers->peers_fe->maxconn;
Emeric Brunc60def82017-09-27 14:59:38 +02002370 peers->sync_task = task_new(MAX_THREADS_MASK);
Willy Tarreaud9443442018-10-15 11:18:03 +02002371 if (!peers->sync_task)
2372 return 0;
2373
Emeric Brunb3971ab2015-05-12 18:49:09 +02002374 peers->sync_task->process = process_peer_sync;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002375 peers->sync_task->context = (void *)peers;
2376 peers->sighandler = signal_register_task(0, peers->sync_task, 0);
2377 task_wakeup(peers->sync_task, TASK_WOKEN_INIT);
Willy Tarreaud9443442018-10-15 11:18:03 +02002378 return 1;
Emeric Brunb3971ab2015-05-12 18:49:09 +02002379}
2380
2381
2382
2383/*
2384 * Function used to register a table for sync on a group of peers
2385 *
2386 */
2387void peers_register_table(struct peers *peers, struct stktable *table)
2388{
2389 struct shared_table *st;
2390 struct peer * curpeer;
2391 int id = 0;
2392
2393 for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) {
Vincent Bernat02779b62016-04-03 13:48:43 +02002394 st = calloc(1,sizeof(*st));
Emeric Brunb3971ab2015-05-12 18:49:09 +02002395 st->table = table;
2396 st->next = curpeer->tables;
2397 if (curpeer->tables)
2398 id = curpeer->tables->local_id;
2399 st->local_id = id + 1;
2400
2401 curpeer->tables = st;
2402 }
2403
2404 table->sync_task = peers->sync_task;
Emeric Brun2b920a12010-09-23 18:30:22 +02002405}
2406